RFR: 8298935: fix cyclic dependency bug in create_pack logic in SuperWord::find_adjacent_refs

Fei Gao fgao at openjdk.org
Thu Feb 9 08:44:36 UTC 2023


On Tue, 31 Jan 2023 18:26:52 GMT, Emanuel Peter <epeter at openjdk.org> wrote:

> Cyclic dependencies are not handled correctly in all cases. Three examples:
> 
> https://github.com/openjdk/jdk/blob/0a834cd991a2f94b784ee4abde06825486fcb97f/test/hotspot/jtreg/compiler/loopopts/superword/TestCyclicDependency.java#L270-L277
> 
> And this, compiled with `-XX:CompileCommand=option,compiler.vectorization.TestOptionVectorizeIR::test*,Vectorize`:
> https://github.com/openjdk/jdk/blob/0a834cd991a2f94b784ee4abde06825486fcb97f/test/hotspot/jtreg/compiler/vectorization/TestOptionVectorizeIR.java#L173-L180
> 
> And for `vmIntrinsics::_forEachRemaining` compile option `Vectorize` is always enabled:
> https://github.com/openjdk/jdk/blob/0a834cd991a2f94b784ee4abde06825486fcb97f/test/hotspot/jtreg/compiler/vectorization/TestForEachRem.java#L69-L73
> 
> All of these examples are vectorized, despite the cyclic dependency of distance 2. The cyclic dependency is dropped, instead the emitted vector code implements a shift by 2, instead of repeating the same 2 values.
> 
> **Analysis**
> 
> The `create_pack` logic in `SuperWord::find_adjacent_refs` is broken in two ways:
> 
> - When the compile directive `Vectorize` is on, or we compile `vmIntrinsics::_forEachRemaining` we have `_do_vector_loop == true`. When that is the case, we blindly trust that there is no cyclic dependency larger than distance 1. Distance 1 would already be detected by the `independence(s1, s2)` checks we do for all adjacent memops. But for larger distances, we rely on `memory_alignment == 0`. But the compile directive avoids these checks.
> - If `best_align_to_mem_ref` is of a different type, and we have `memory_alignment(mem_ref, best_align_to_mem_ref) == 0`, we do not check if `mem_ref` has `memory_alignment == 0` for all other refs of the same type. In the example `TestCyclicDependency::test2`, we have `best_align_to_mem_ref` as the `StoreF`. Then we assess the `StoreI`, which is not aligned with it, but it is of a different type, so we accept it too. Finally, we look at `LoadI`, which has perfect alignment with the `StoreF`, so we accept it too (even though it is in conflict with the `StoreI`).
> 
> Generally, the nested if-statements are confusing and buggy. I propose to fix and refactor the code.
> 
> I also propose to only allow the compile directive `Vectorize` only if `vectors_should_be_aligned() == false`. If all vector operations have to be `vector_width` aligned, then they also have to be mutually aligned, and we cannot have patterns like `v[i] = v[i] + v[i+1]` for which the compile directive was introduced in the first place https://github.com/openjdk/jdk/commit/c7d33de202203b6da544f2e0f9a13952381b32dd.
> 
> **Solution**
> 
> First, I implemented `SuperWord::verify_packs` which catches cyclic dependencies just before scheduling. The idea is to reassess every pack, and check if all memops in it are mutually independent. Turns out that per vector pack, it suffices to do a single BFS over the nodes in the block (see `SuperWord::find_dependence`). With this verification in place we at least get an assert instead of wrong execution.
> 
> I then refactored and fixed the `create_pack` code, and put the logic all in `SuperWord::is_mem_ref_alignment_ok`. With the added comments, I hope the logic is more straight forward and readable. If `_do_vector_loop == true`, then I filter the vector packs again in `SuperWord::combine_packs`, since we are at that point not sure that the packs are actually independent, we only know that adjacient memops are independent.
> 
> **Testing**
> 
> I added a few more regression tests, and am running tier1-3, plus some stress testing.
> 
> However, I need help from someone who can test this on **ARM32** and **SPARC**, basically machines that have `vectors_should_be_aligned() == false`. I would love to have additional testing on those machine, and some reviews.
> 
> **Discussion / Future Work**
> 
> I wonder if we should have `_do_vector_loop == true` by default, since it allows more vectorization. With the added filtering, we are sure that we do not schedule packs with cyclic dependencies. We would have to evaluate performance and other side-effects of course. What do you think?

> We initially generate pairs in find_adjacent_refs . These are guaranteed to be independent.
But how do we guarantee that the packs stay independent when we do combine_packs?
Would we not have to check independent for each additional memop we add to a pack?
Because if we combine (a,b) with (b,c), we only know that a indep b and b indep c , but how would we know that a indep c?

Hi @eme64, superword combines the results from https://github.com/openjdk/jdk/blob/4c9de876bffe5abb94db1c8c2b919d8243317ff8/src/hotspot/share/opto/superword.cpp#L646
and https://github.com/openjdk/jdk/blob/4c9de876bffe5abb94db1c8c2b919d8243317ff8/src/hotspot/share/opto/superword.cpp#L665 to guarantee there is no data dependency within one combined pack.

For example, in the case like

// int[] a, b;
for (int i = start; i < limit; i++) {
  b[i+OFFSET] = a[i];
}

it supposes that, if it holds `OFFSET * element_size_in_bytes % MaxVectorSize == 0`, vector load and vector store won't access overlapped memory within one vector execution. That also means that there won't be dependency among nodes in the future combined pack, because the dependency between `a` and `c` you mentioned above actually comes from memory dependency between load and store nodes. The algorithm works for all cases in the form above, and array `a` and `b` have the same data type.

But the case you proposed hits the bug of the algorithm. In your case, `find_adjacent_refs()` visits in the order: StoreF nodes, StoreI nodes and then LoadI nodes. So it naturally takes a StoreF node as `best_align_to_mem_ref` here and the pointer won't be updated. Then, it takes all memory nodes as potential packs because it thinks that whether LoadI or StoreI are independent accesses of StoreF, which is obviously true since arrays of different data types can't be the same one. In fact, it should also decide if there is potential overlapped access between LoadI and StoreI. And we know the overlapping does exist. I'm wondering if we can improve the algorithm by further comparing memory nodes with a special `best_align_to_mem_ref_of_the_same_type` for the same data type, if existing. 

Anyway, never mind. Your fix looks good to me! 

Thanks.

Sorry to clarify:

 _if it holds `OFFSET * element_size_in_bytes % MaxVectorSize == 0`, vector load and vector store won't access overlapped memory within one vector execution._, which means vector load and vector store won't access **partially overlapped** memory within one vector execution. They're still allowed to access **completely overlapped** memory with one vector execution, namely `b[i] = a[i]`.

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

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


More information about the hotspot-compiler-dev mailing list