RFR: 8360389: Support printing from C2 compiled code [v9]

Benoît Maillard bmaillard at openjdk.org
Tue Sep 2 13:30:38 UTC 2025


> This PR adds support for printf-style debugging from C2 compiled code. This is implemented as a runtime call to a C++ method that prints the values of the nodes passed as arguments. The runtime C++ method is implemented with the help of variadic templates, as it is expected to support various combinations of argument types.
> 
> ## Usage
> 
> Suppose we have this piece of Java code, that simply computes an arithmetic operation, and
> that we compile `square` with C2.
> 
> class Square {
>     static int square(int a) {
>         return a * a;
>     }
> 
>     public static void main(String[] args) {
>         square(9);
>     }
> }
> 
> 
> We would like to inspect the node that contains the value returned in this method.
> We can add a call to `Compile::make_debug_print` and pass a message, the IGVN instance, as well as the node(s) that we would like to inspect.
> 
> ```c++
> void Compile::return_values(JVMState* jvms) {
>   GraphKit kit(jvms);
>   Node* ret = new ReturnNode(TypeFunc::Parms,
>                              kit.control(),
>                              kit.i_o(),
>                              kit.reset_memory(),
>                              kit.frameptr(),
>                              kit.returnadr());
>   // Add zero or 1 return values
>   int ret_size = tf()->range()->cnt() - TypeFunc::Parms;
> 
>   
>   Node* return_value;
>   if (ret_size > 0) {
>     kit.inc_sp(-ret_size);  // pop the return value(s)
>     kit.sync_jvms();
>     return_value = kit.argument(0);
>     ret->add_req(return_value);
> 
>     // <-------------------- Simply insert this
>     C->make_debug_print<jint>("return: ", initial_gvn(), return_value);
>   }
>   
>   // bind it to root
>   root()->add_req(ret);
>   record_for_igvn(ret);
>   initial_gvn()->transform(ret);
> }
> 
> 
> We can then call run our code with `-XX:CompileCommand="compileonly,Square::square`
> and we get the following output:
> 
> 
> return:
> int 81
> 
> 
> This case is of course trivial, but more useful examples are shown later.
> 
> ## Implementation
> 
> The debugging capability is implemented as a runtime call to a C++ printing method. For this, `Compile::make_debug_print` inserts a `CallLeafNode` into the graph and rewires control flow as needed.
> 
> The actual printing is handled by the `SharedRuntime::debug_print` method, written with a variadic template to support various argument type combinations. A pointer to this runtime method is obtained at compile time and is passed to the `CallLeafNode` constructor.
> 
> The first argument to the runtime printing method is the printing message, a static string encoded as a `...

Benoît Maillard has updated the pull request with a new target base due to a merge or a rebase. The incremental webrev excludes the unrelated changes brought in by the merge/rebase. The pull request contains 51 additional commits since the last revision:

 - Automatically insert placeholder nodes for jlong and jdouble
 - Merge branch 'master' into JDK-8360389
 - Fix mismatch with #ifdef COMPILER2 between .hpp and .cpp
 - Avoid having root as control candidate to prevent issue when printing a constant
 - Fix missing candidates.size() == 0 case when constants are involved
 - Replace one more \n by print_cr
 - Add #ifdef COMPILER2 for runtime methods
 - Use print_cr instead of \n
 - Update src/hotspot/share/opto/compile.cpp
   
   Co-authored-by: Manuel Hässig <manuel at haessig.org>
 - Update src/hotspot/share/runtime/sharedRuntime.cpp
   
   Co-authored-by: Manuel Hässig <manuel at haessig.org>
 - ... and 41 more: https://git.openjdk.org/jdk/compare/fccb79ca...13da04e1

-------------

Changes:
  - all: https://git.openjdk.org/jdk/pull/26475/files
  - new: https://git.openjdk.org/jdk/pull/26475/files/233c89a0..13da04e1

Webrevs:
 - full: https://webrevs.openjdk.org/?repo=jdk&pr=26475&range=08
 - incr: https://webrevs.openjdk.org/?repo=jdk&pr=26475&range=07-08

  Stats: 88672 lines in 2189 files changed: 55916 ins; 22598 del; 10158 mod
  Patch: https://git.openjdk.org/jdk/pull/26475.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/26475/head:pull/26475

PR: https://git.openjdk.org/jdk/pull/26475


More information about the hotspot-dev mailing list