Repository navigation
Rewrite the calls to a transformed method to launch its kernel - #83
Merged
Merged
Conversation
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.
This was referenced Nov 27, 2026
Merged
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
becomes
execute().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.GridScheduleron the grid it declares, rounded up to the work-group size.TornadoExecutionPlanExceptionis caught and rethrown unchecked only when the method doesn't handle it, so the method's signature stays the same.try. Graph names avoid those already in the file, and nothing else in the method changes.Testing
./gradlew test: all 322 tests pass. Ten new tests inCallSiteRewriterTestcover:try;TestCuBlasWorkspace(tornado-cublas) leaves its cuBLAS TaskGraph untouched and turns its twomatrixMultiplyJavacalls into one TaskGraph executed twice.