JDK-8277850 : C2: optimize mask checks in counted loops
  • Type: Enhancement
  • Component: hotspot
  • Sub-Component: compiler
  • Affected Version: 17,18
  • Priority: P4
  • Status: Resolved
  • Resolution: Fixed
  • Submitted: 2021-11-25
  • Updated: 2022-06-17
  • Resolved: 2021-12-08
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 18 JDK 19
18 b28Fixed 19Fixed
Related Reports
Relates :  
Relates :  
Relates :  
Relates :  
Description
The memory access API supports custom alignment constraints, which are checked upon memory access, using the following formula:

((segmentBaseAddress + accessedOffset) & alignmentMask) == 0

However, when accessing a segment using a var handle obtained from a layout featuring a non-trivial alignment mask, access performance is slower than in the case where the alignment mask is 0.

The attached patch adds a benchmark which shows the problem; the benchmark compares accessing a segment using a 4-byte aligned vs. a 1-byte aligned layout:

```
Benchmark                                                Mode Cnt  Score   Error  Units
LoopOverNonConstant.segment_loop_instance_index          avgt 30  0.229 ? 0.001  ms/op
LoopOverNonConstant.segment_loop_instance_index_aligned  avgt 30  0.329 ? 0.005  ms/op
```

As it can be seen, access with alignment constraints is slower.

This is mildly surprising - after all, in the above formula, segmentBaseAddress is a loop invariant - whereas accessedOffset typically depends on the loop variable, so existing BCE logic should kick in and detect that the offset is always aligned (given the loop stride).
Comments
Changeset: 8af3b27c Author: Roland Westrelin <roland@openjdk.org> Date: 2021-12-08 16:39:24 +0000 URL: https://git.openjdk.java.net/jdk/commit/8af3b27ce98bcb9cf0c155c98d6b9a9bc159aafe
08-12-2021

Turning this into a performance RFE.
26-11-2021