Skip to content

Rewrite the calls to a transformed method to launch its kernel - #83

Merged
stratika merged 1 commit into
mainfrom
feature/rewrite-kernel-call-sites
Nov 26, 2026
Merged

stratika merged 1 commit into
mainfrom
feature/rewrite-kernel-call-sites

Conversation

@stratika

Copy link
Copy Markdown
Contributor

Summary

Until now, accepting a transformed method put the kernel in the file, but its callers still called it as plain Java, so the kernel ran sequentially on the CPU. Now, when the kernel replaces the method, the calls to it in the same file are rewritten to launch it with TornadoVM. The preview shows the rewritten calls in the diff, so the developer can review them before accepting. An Also rewrite the calls in this file… checkbox (on by default) leaves them as they are.

The plugin does the rewrite itself; the model is not asked. An earlier version asked the model to rewrite the calling methods. With a small local model (gemma 2B), it built a new TaskGraph for every call instead of replaying, and in one answer it deleted unrelated code from main. The strategy below is mechanical, so the plugin applies it exactly, with any model and without an extra model request.

The strategy

matrixMultiply(a, b, c, size);
System.out.println(c[0]);
matrixMultiply(a, b, c, size);

becomes

TaskGraph taskGraph = new TaskGraph("s0")
        .transferToDevice(DataTransferMode.FIRST_EXECUTION, a, b)
        .task("t0", Example::matrixMultiply, a, b, c, size)
        .transferToHost(DataTransferMode.EVERY_EXECUTION, c);
try (TornadoExecutionPlan executionPlan = new TornadoExecutionPlan(taskGraph.snapshot())) {
    executionPlan.execute();
    System.out.println(c[0]);
    executionPlan.execute();
} catch (TornadoExecutionPlanException e) {
    throw new RuntimeException(e);
}
  • Replay: the first call builds the TaskGraph and the plan and executes it. The later calls with the same arguments only replay the plan with execute().
  • Loops: a call in a loop whose arguments don't change gets its plan built before the loop. If an argument changes between calls (a scalar reassigned, other arrays), a new TaskGraph starts from that point.
  • Transfers: each array the kernel reads goes to the device once (FIRST_EXECUTION). It goes on every execution (EVERY_EXECUTION) instead if the code between the executions changes it. Arrays the kernel writes come back after every execution, and an output-only array is not sent to the device.
  • Kernel API / Tile API: the kernel is launched with a GridScheduler on the grid it declares, rounded up to the work-group size.
  • Exception: TornadoExecutionPlanException is caught and rethrown unchecked only when the method doesn't handle it, so the method's signature stays the same.
  • Unchanged code: variables declared between the calls and used after them are declared before the try. Graph names avoid those already in the file, and nothing else in the method changes.
  • Calls left alone: a call whose value is used, one in a kernel or outside a method, or one whose array argument is not a variable stays as it is, and the preview says why. Calls in other files are counted in the preview but not changed.

Testing

  • ./gradlew test: all 322 tests pass. Ten new tests in CallSiteRewriterTest cover:
    • replay, and Accept giving exactly the preview's text;
    • a loop;
    • an input changed between calls;
    • other arguments, a call that is a loop's whole body, and a call in a lambda;
    • a changed scalar, with a variable declared before the try;
    • the Kernel API grid;
    • existing TaskGraph names and the surrounding code being kept;
    • the skip reasons, and the rounding.
  • The rewrite of six of those cases was compiled against TornadoVM 8.2.0 and run on an Apple M4 Pro (Metal). Each gave the same results as the original Java code. All 17 kernel launches ran on the GPU, with the expected number of TaskGraphs and replays.
  • The rewrite of TestCuBlasWorkspace (tornado-cublas) leaves its cuBLAS TaskGraph untouched and turns its two matrixMultiplyJava calls into one TaskGraph executed twice.
  • Not yet tried by hand in the IDE.

When the kernel replaces the method, the calls to it in the same file are
rewritten to launch the kernel with TornadoVM, and the preview shows them
in the diff; a checkbox leaves them as they are. The first call builds a
TaskGraph and a TornadoExecutionPlan in a try-with-resources block and
executes it, and the later calls with the same arguments only replay the
plan with execute(). A call in a loop whose arguments do not change gets
its plan built before the loop; a change to an argument starts a new
TaskGraph.

The arrays the kernel reads are transferred to the device once, or at
every execution if the code changes them between executions, and the
arrays it writes come back after every execution. Kernel API and Tile API
kernels are launched on their grid with a GridScheduler, rounded up to the
work-group size. The plan's TornadoExecutionPlanException is caught and
rethrown unchecked unless the method handles it, and variables declared
between the calls and used after them are declared before the try block.

The plugin does the rewrite itself: only the calls change, and the code
around them is kept. A call whose value is used, one in a kernel or outside
a method, or one with an array argument that is not a variable is left as
it is, and the preview says why.
@stratika
stratika merged commit afd2b2c into main Nov 26, 2026
4 checks passed
@stratika
stratika deleted the feature/rewrite-kernel-call-sites branch November 26, 2026 16:37
This was referenced Nov 27, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant