RFR: 8307084: C2: Vectorized drain loop is not executed for some small trip counts

Fei Gao fgao at openjdk.org
Wed Aug 13 12:38:50 UTC 2025


In C2's loop optimization, for a counted loop, if we have any of these conditions (RCE, unrolling) met, we switch to the
`pre-main-post-loop` model. Then a counted loop could be split into `pre-main-post` loops. Meanwhile, C2 inserts minimum trip guards (a.k.a. zero-trip guards) before the main loop and the post loop. These guards test if the remaining trip count is less than the loop stride (after unrolling). If yes, the execution jumps over the loop code to avoid loop over-running. For example, if a main loop is unrolled to `8x`, the main loop guard tests if the loop has less than `8` iterations and then decide which way to go.

Usually, the vectorized main loop will be super-unrolled after vectorization. In such cases, the main loop's stride is going to be further multiplied. After the main loop is super-unrolled, the minimum trip guard test will be updated. Assuming one vector can operate `8` iterations and the super-unrolling count is `4`, the trip guard of the main loop will test if remaining trip is less than `8 * 4 = 32`.

To avoid the scalar post loop running too many iterations after super-unrolling, C2 clones the main loop before super-unrolling to create a vectorized drain loop. The newly inserted post loop also has a minimum trip guard. And, both trip guards of the main loop and the vectorized drain loop jump to the scalar post loop.

The problem here is, if the remaining trip count when exiting from the pre-loop is relatively small but larger than the vector length, the vectorized drain loop will never be executed. Because the minimum trip guard test of main loop fails, the execution will jump over both the main loop and the vectorized drain loop. For example, in the above case, a loop still has `25` iterations after the pre-loop, we may run `3` rounds of the vectorized drain loop but it's impossible. It would be better if the minimum trip guard test of the main loop does not jump over the vectorized drain loop.

This patch is to improve it by modifying the control flow when the minimum trip guard test of the main loop fails. Obviously, we need to sync all data uses and control uses to adjust to the change of control flow.

The whole process is done by the function `insert_post_loop()`.

We introduce a new `CloneLoopMode`, `InsertVectorizedDrain`. When we're cloning the vector main loop to vectorized drain loop with mode `InsertVectorizedDrain`:

1. The fall-in control flow to the vectorized drain loop comes from a `RegionNode` merging exits from pre-loop and main-loop, implemented in `insert_post_loop()`.

2. All fall-in values to the vectorized drain loop come from (one or more) `Phi`s merging exit values from pre-loop and main-loop, implemented by `get_vectorized_drain_input()`.

3. All control uses of exits from old-loop now should use new `RegionNode`s that merge `RegionNode`s which merge exits from pre-loop and main-loop and exits from the new-loop (vectorized drain loop) equivalents, implemented by `fix_ctrl_uses_for_vectorized_drain()`.

4. All data uses of values from old-loop now should use new `Phi`s that merge two inputs:

- `Phi`s, which in their turn merge values from pre-loop and main-loop,
- and values from the new-loop (vectorized drain loop) equivalents.

This is implemented by `handle_data_uses_for_vectorized_drain()`.

We also add a new micro-benchmark to test the performance gain. Here are the performance results from different vector-length machines.

**Average Time on 128-bit machine​:**

![128-addb](https://github.com/user-attachments/assets/05fdf7b4-e4cd-4372-a400-f1ac5b4be328)
![128-adds](https://github.com/user-attachments/assets/c5c65277-fb4d-4d54-b443-7e44901618ff)
![128-addi](https://github.com/user-attachments/assets/34c5c296-2f1d-42dc-a0b9-ffe3b14b3d29)
![128-addl](https://github.com/user-attachments/assets/0559d9c1-77d7-4120-9638-ee187a71e485)

**Average Time on 256-bit machine​:**

![256-addb](https://github.com/user-attachments/assets/2f88dedd-9371-4a75-bf6a-a6901b8eb689)
![256-adds](https://github.com/user-attachments/assets/36ce000e-517f-4a1f-b3ba-5f6708709426)
![256-addi](https://github.com/user-attachments/assets/cfb9e7f9-ae9b-4797-97c2-8451733c938f)
![256-addl](https://github.com/user-attachments/assets/bba0f61a-b401-4ccd-bdb2-7b4daee1c197)

**Average Time on 512-bit machine​:**

![512-addb](https://github.com/user-attachments/assets/aedd1f9f-dc53-4dc5-9ba4-b8bbcff1265c)
![512-adds](https://github.com/user-attachments/assets/ed4ad9f6-5349-4f69-ae98-b4f87ccb3a85)
![512-addi](https://github.com/user-attachments/assets/b418304c-641c-4b29-9c0f-0df5e53ddc11)
![512-addl](https://github.com/user-attachments/assets/8008cb7a-36eb-4913-b205-ccfe8444c58c)

Tier 1- 3 passed on aarch64 and x86.

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

Commit messages:
 - Clean up comments for consistency and add spacing for readability
 - Fix some corner case failures and refined part of code
 - Merge branch 'master' into optimize-atomic-post
 - Refine ascii art, rename some variables and resolve conflicts
 - Merge branch 'master' into optimize-atomic-post
 - Add necessary ASCII art, refactor insert_post_loop() and rename
 - Merge branch 'master' into optimize-atomic-post
 - 8307084: C2: Vector atomic post loop is not executed for some small trip counts

Changes: https://git.openjdk.org/jdk/pull/22629/files
  Webrev: https://webrevs.openjdk.org/?repo=jdk&pr=22629&range=00
  Issue: https://bugs.openjdk.org/browse/JDK-8307084
  Stats: 1542 lines in 8 files changed: 1358 ins; 59 del; 125 mod
  Patch: https://git.openjdk.org/jdk/pull/22629.diff
  Fetch: git fetch https://git.openjdk.org/jdk.git pull/22629/head:pull/22629

PR: https://git.openjdk.org/jdk/pull/22629


More information about the hotspot-compiler-dev mailing list