[code-reflection] RFR: Drop unused local variables from generated op method

Maurizio Cimadamore mcimadamore at openjdk.org
Mon Apr 28 18:37:04 UTC 2025


On Mon, 28 Apr 2025 18:28:37 GMT, Maurizio Cimadamore <mcimadamore at openjdk.org> wrote:

> The generated op method contains some local variables that are never used.
> This PR fixes that, further reducing the number of generated locals per synthetic op method.
> While this is not a groundbreaking improvement, in some complex tests I've seen a reduction of locals up to 20%.

src/jdk.incubator.code/share/classes/jdk/incubator/code/internal/CodeModelToAST.java line 124:

> 122: 
> 123:         java.util.List<Value> rootValues = funcOp.traverse(new ArrayList<>(), (l, ce) -> {
> 124:             boolean isRoot = switch (ce) {

I've rewrote the code here, as the condition `uses().size() != 1` was very subtle, and picking up things with no usages which, in principle, should not be "roots". Now the list of root values we expect is captured explicitly in the code.

src/jdk.incubator.code/share/classes/jdk/incubator/code/internal/CodeModelToAST.java line 146:

> 144:                     valueToTree.put(root, tree);
> 145:                 } else {
> 146:                     tree = treeMaker.Exec(e);

This is the heart of the fix - if there's no usage, just emit as expression statement. I have tried with an alternate fix which, in case of exactly one use, would "inline" the use (avoiding the local). While this delivered massive reduction in the number of locals, the generated model was no longer compatible as some values were being reordered.

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

PR Review Comment: https://git.openjdk.org/babylon/pull/413#discussion_r2064261368
PR Review Comment: https://git.openjdk.org/babylon/pull/413#discussion_r2064268094


More information about the babylon-dev mailing list