RFR: 8286190: Add test to verify constant folding for Enum fields

John Rose john.r.rose at oracle.com
Thu May 5 20:35:33 UTC 2022


This reminds me that our enum switches do not allow constant folding, 
because of our translation strategy.

The root cause is that the T.S. open-codes the discrimination logic for 
switches, and it happens to use Java arrays, which cannot be 
constant-folded.  There is a way to “trust” an array element, called 
`@Stable`, but it can only be used inside a JDK runtime support method, 
not in a random classfile generated by javac.

So the T.S. should encapsulate switch discrimination logic so that a 
runtime support routine can use appropriate technology, such as stable 
arrays and/or perfect hashes and/or binary searches.  It’s not really 
javac’s job to build such things, and when it tries it just gets in 
the way of doing a better job in the runtime.

https://bugs.openjdk.java.net/browse/JDK-8161250 tracks this general 
issue.

These points relate to this bug only in that, if we had our ducks in a 
row with translation of switch, we could test also for constant-folding 
of switch logic.

On 5 May 2022, at 8:45, Vladimir Kozlov wrote:

> On Thu, 5 May 2022 13:35:37 GMT, Aleksey Shipilev <shade at openjdk.org> 
> wrote:
>
>> There is the 
>> [JDK-8161245](https://bugs.openjdk.java.net/browse/JDK-8161245) to 
>> make compilers trust Enum final fields. It was implicitly implemented 
>> by [JDK-8234049](https://bugs.openjdk.java.net/browse/JDK-8234049), 
>> which added the wildcard trust for everything in java/lang:
>> https://github.com/openjdk/jdk/blob/c5a0687f80367a3a284dfd56781c371826264d3b/src/hotspot/share/ci/ciField.cpp#L230
>>
>> It would be better to have the explicit test that verifies the 
>> constant folding of Enum fields indeed happens.
>>
>> Additional testing:
>>  - [x] Linux x86_64 fastdebug, new test passes
>>  - [x] Linux x86_64 release, new test passes
>>  - [x] Linux x86_32 fastdebug, new test passes
>
> Nice find. So we are doing ENUM constant folding since JDK 14 and did 
> not notice it ;)
>
> Test is good. Thanks!
>
> -------------
>
> Marked as reviewed by kvn (Reviewer).
>
> PR: https://git.openjdk.java.net/jdk/pull/8551


More information about the hotspot-compiler-dev mailing list