RFR: 8367531: Template Framework: use scopes and tokens instead of misbehaving immediate-return-queries [v11]
Christian Hagedorn
chagedorn at openjdk.org
Wed Nov 5 11:52:28 UTC 2025
On Tue, 4 Nov 2025 16:04:22 GMT, Emanuel Peter <epeter at openjdk.org> wrote:
>> I got some feedback from users of the Template Framework, especially @galderz . And personally, I already was slightly unsatisfied by some of the issues described below, but did not expect it to be as bad as it is.
>>
>> So I'm sorry, but I think we need to do a significant re-design. It is now still early enough, and only trivial changes are required for the "real" uses of the framework. Only the framework internal tests require significant changes.
>>
>> Many thanks to @galderz for trying out the framework, and reporting the issues. And thanks to @chhagedorn for spending a few hours in an offline meeting discussing the issue.
>>
>> **Major issue with Template Framework: lambda vs token order**
>>
>> The template rendering involves some state, such as keeping track of hashtag replacements, names and fuel cost.
>> Some methods have side-effects (`addDataName`, `let`, ...) and others are simple queries (`sample`, ...).
>> Sadly, the first version of the template framework was not very consistent, and created tokens (deferred evaluation, during token evaluation) for some, and for others it queried the state and returned the result immediately (during lambda execution). One nasty consequence is that an immediately returning query can "float" above a state affecting token. For example, `addDataName` generated a token (so that we know if it is to be added for the template frame or a hook anchoring frame), but answered sampling queries immediately (because that means we can use the returned value immediately and make decisions based on it immediately, which is nice). Looking at the example below, this had the confusing result that `addDataName` only generates a token at first, then `sample` does not have that name available yet, and only later during token evaluation is the name actually added.
>>
>> var testTemplate = Template.make(() -> body(
>> ...
>> addDataName("name", someType, MUTABLE),
>> let("name", dataNames(MUTABLE).exactOf(someType).sample().name()),
>> ...
>> ));
>>
>>
>> **Two possible solutions: all-in on lambda execution or all-in on tokens**
>>
>> First, I thought I want to go all-in on lambda execution, and have everything have immediate effect and return results immediately. This would have the nice effect that the user feels like they are directly in control of the execution order. But I did not find a good way without exposing too many internals to the user, or getting rid of the nice "token lists" we currently have inside Templates (the...
>
> Emanuel Peter has updated the pull request incrementally with one additional commit since the last revision:
>
> small adjustments after call with Roberto and Christian
Comments for `TestTemplate.java`. It was more a skim of the file and I stopped here and there for having a closer look but I trust you here that you covered all the functionality - at least it suggests that you were quite thorough :-)
test/hotspot/jtreg/testlibrary_tests/template_framework/tests/TestTemplate.java line 469:
> 467: var hook1 = new Hook("Hook1");
> 468:
> 469: var template0 = Template.make(() -> scope("t0 isAnchored: ", hook1.isAnchored(a -> scope(a)), "\n"));
You could directly use `Template::scope` instead of `a -> scope(a)`. Same below.
test/hotspot/jtreg/testlibrary_tests/template_framework/tests/TestTemplate.java line 1259:
> 1257: let("global", "GLOBAL"),
> 1258: "g: #global. $a\n",
> 1259: // Create a dummy DataName soe we can create the scope.
Suggestion:
// Create a dummy DataName so we can create the scope.
test/hotspot/jtreg/testlibrary_tests/template_framework/tests/TestTemplate.java line 1334:
> 1332: dataNames(MUTABLE_OR_IMMUTABLE).exactOf(myInt).hasAny(h -> scope(h)),
> 1333: ", ",
> 1334: dataNames(MUTABLE_OR_IMMUTABLE).exactOf(myInt).count(c -> scope(c)),
Here you can simplify it again:
Suggestion:
dataNames(MUTABLE_OR_IMMUTABLE).exactOf(myInt).hasAny(Template::scope),
", ",
dataNames(MUTABLE_OR_IMMUTABLE).exactOf(myInt).count(Template::scope),
test/hotspot/jtreg/testlibrary_tests/template_framework/tests/TestTemplate.java line 1423:
> 1421: dataNames(mutability).exactOf(myInt).hasAny(h -> scope(h)),
> 1422: ", ",
> 1423: dataNames(mutability).exactOf(myInt).count(c -> scope(c)),
Suggestion:
dataNames(mutability).exactOf(myInt).hasAny(Template::scope),
", ",
dataNames(mutability).exactOf(myInt).count(Template::scope),
test/hotspot/jtreg/testlibrary_tests/template_framework/tests/TestTemplate.java line 1570:
> 1568: dataNames(mutability).exactOf(myInt).hasAny(h -> scope(h)),
> 1569: ", ",
> 1570: dataNames(mutability).exactOf(myInt).count(c -> scope(c)),
There are many more occurrences further down. Feel free to fix them but I guess it's not that important.
Suggestion:
dataNames(mutability).exactOf(myInt).hasAny(Template::scope),
", ",
dataNames(mutability).exactOf(myInt).count(Template::scope),
test/hotspot/jtreg/testlibrary_tests/template_framework/tests/TestTemplate.java line 1901:
> 1899: "int #v1 = x + 1;\n"
> 1900: )),
> 1901: // Using "transparentScope", is is available.
"it is available"? Same below
test/hotspot/jtreg/testlibrary_tests/template_framework/tests/TestTemplate.java line 2632:
> 2630: "q: #q.\n",
> 2631: listDataNames.asToken(),
> 2632: // A "setFuelCostScope" nesting behaves the same as "transparentScope", as we are not useing fuel here.
Suggestion:
// A "setFuelCostScope" nesting behaves the same as "transparentScope", as we are not using fuel here.
-------------
PR Review: https://git.openjdk.org/jdk/pull/27255#pullrequestreview-3420983428
PR Review Comment: https://git.openjdk.org/jdk/pull/27255#discussion_r2493782215
PR Review Comment: https://git.openjdk.org/jdk/pull/27255#discussion_r2493829389
PR Review Comment: https://git.openjdk.org/jdk/pull/27255#discussion_r2493840342
PR Review Comment: https://git.openjdk.org/jdk/pull/27255#discussion_r2493851152
PR Review Comment: https://git.openjdk.org/jdk/pull/27255#discussion_r2493852455
PR Review Comment: https://git.openjdk.org/jdk/pull/27255#discussion_r2493863474
PR Review Comment: https://git.openjdk.org/jdk/pull/27255#discussion_r2493870923
More information about the hotspot-compiler-dev
mailing list