RFR: 8351515: C2 incorrectly removes double negation for double and float
Manuel Hässig
duke at openjdk.org
Fri Mar 21 12:09:40 UTC 2025
On Fri, 21 Mar 2025 11:04:16 GMT, Hannes Greule <hgreule at openjdk.org> wrote:
>> # Issue Summary
>>
>> A fuzzer run discovered that code of the form `0 - (0 - x)` yields the wrong result for `x = -0.0`. According to the [Java Language Spec 15.8.2](https://docs.oracle.com/javase/specs/jls/se23/html/jls-15.html#jls-15.18.2) the sum of floating point zeroes of opposite sign is positive zero. Hence, `(0 - (0 - (-0.0))` must result in `+0.0`, but due to the folding of all double negations in `SubNode::Identity` this was optimized to `-0.0`
>>
>> # Changeset overview
>>
>> To fix this issue, I excluded floating point numbers from the folding of double negations, which also includes `Float16` values. This might seem excessive at first glance, but we do not track range information for floating point types. Hence, we could still perform the folding of the double negation if a floating point constant is not `-0.0`. However, constant folding already takes care of this.
>>
>> Changes:
>> - IR-Framework: fix `IRNode.SUB`not matching `SubHFNode`
>> - Add a regression IR-test
>> - Exclude floating point `SubNodes` from folding double negations
>>
>> # Testing
>> - [Github Actions](https://github.com/mhaessig/jdk/actions/runs/13989207348)
>> - `tier1` through `tier5` plus Oracle internal testing
>
> Negation here can be a bit of a confusing term since the [JLS § 15.15.4](https://docs.oracle.com/javase/specs/jls/se23/html/jls-15.html#jls-15.15.4) says:
>> For floating-point values, negation is *not* the same as subtraction from zero, because if x is +0.0, then 0.0-x is +0.0, but -x is -0.0
>
> This is also the reason why NegF/D nodes are used separately (NegI/L nodes exist but seem to be unused). The change itself looks good, but maybe it's worth to clarify the comment? Or somehow reference the comment here: https://github.com/openjdk/jdk/blob/b32be18bf940eb6eb9805390fd72e0de175c912a/src/hotspot/share/opto/subnode.hpp#L473-L478
@SirYwell thanks for the pointer to the negation section of the spec. I improved the comment by referencing spec and differentiating subtraction from negation.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/24150#issuecomment-2743181121
More information about the hotspot-compiler-dev
mailing list