llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[Loop Vectorizer] Loop Vectorized with Gather Instead of Load Due to SUB => XOR Transformation

Open mmarjieh opened this issue 2 years ago • 4 comments

Starting from Clang 18, I get a masked_gather instead of regular load in vectorizer.

Reproducer: Top of Trunk Clang: https://godbolt.org/z/aj91K1eaf See LoopVectorize debug prints:

LAA: Bad stride - Not an AddRecExpr pointer %arrayidx = getelementptr inbounds i32, ptr %a1, i64 %idxprom, !dbg !26 SCEV: ((4 * %idxprom) + %a1)

Clang 17: https://godbolt.org/z/Y9W56Wds5

I believe that the issue is happening due to instcombine transforming SUB -> XOR. Does it load unscevable pattern?

mmarjieh avatar Apr 10 '24 07:04 mmarjieh

Looks like InstCombine used computeKnownBits to prove that the upper bits were 0 using the dominating condition from the < 256 check created by the loop

I tried to teach SCEV to reverse the transform, but I think loop rotation prevented the fact from being provable by computeKnownBits later.

CC: @nikic

topperc avatar Apr 10 '24 17:04 topperc

Dominating conditions are only available in InstCombine, so if they are used to infer KnownBits, you usually cannot undo them easily.

I think an acceptable fix here would be to skip use of dominating conditions in the sub -> xor canonicalization. (For purely canonicalizing transforms, if there is no flag to undo, it's probably best not to use dominating conditions.)

nikic avatar Apr 11 '24 00:04 nikic

Dominating conditions are only available in InstCombine, so if they are used to infer KnownBits, you usually cannot undo them easily.

I think an acceptable fix here would be to skip use of dominating conditions in the sub -> xor canonicalization. (For purely canonicalizing transforms, if there is no flag to undo, it's probably best not to use dominating conditions.)

Is there an existing way to disable it?

topperc avatar Apr 12 '24 03:04 topperc

@topperc You'd want to add a getWithoutDomCondCache() method to SimplifyQuery for this, which nulls out DC.

nikic avatar Apr 12 '24 09:04 nikic