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

[VectorCombine] Failure to merge duplicate new nodes

Open RKSimon opened this issue 1 year ago • 7 comments

Vectorcombine fails to completely concatenate these 2 x v4f32 chains

define <8 x float> @fmachain(<4 x float> %a0, <4 x float> %a1) {
  %d0 = fadd <4 x float> %a0, splat (float -1.000000e+00)
  %l0 = call <4 x float> @llvm.fma.v4f32(<4 x float> %d0, <4 x float> splat (float 0x3F8DE8D040000000), <4 x float> splat (float 0xBFB3715EE0000000))
  %l1 = call <4 x float> @llvm.fma.v4f32(<4 x float> %l0, <4 x float> %d0, <4 x float> splat (float 0x3FC82778A0000000))
  %l2 = call <4 x float> @llvm.fma.v4f32(<4 x float> %l1, <4 x float> %d0, <4 x float> splat (float 0xBFD493F7E0000000))
  %l3 = call <4 x float> @llvm.fma.v4f32(<4 x float> %l2, <4 x float> %d0, <4 x float> splat (float 0x3FDE311220000000))
  %l4 = call <4 x float> @llvm.fma.v4f32(<4 x float> %l3, <4 x float> %d0, <4 x float> splat (float 0xBFE70BF2A0000000))
  %l5 = call <4 x float> @llvm.fma.v4f32(<4 x float> %l4, <4 x float> %d0, <4 x float> splat (float 0x3FF71507C0000000))

  %d1 = fadd <4 x float> %a1, splat (float -1.000000e+00)
  %h0 = call <4 x float> @llvm.fma.v4f32(<4 x float> %d1, <4 x float> splat (float 0x3F8DE8D040000000), <4 x float> splat (float 0xBFB3715EE0000000))
  %h1 = call <4 x float> @llvm.fma.v4f32(<4 x float> %h0, <4 x float> %d1, <4 x float> splat (float 0x3FC82778A0000000))
  %h2 = call <4 x float> @llvm.fma.v4f32(<4 x float> %h1, <4 x float> %d1, <4 x float> splat (float 0xBFD493F7E0000000))
  %h3 = call <4 x float> @llvm.fma.v4f32(<4 x float> %h2, <4 x float> %d1, <4 x float> splat (float 0x3FDE311220000000))
  %h4 = call <4 x float> @llvm.fma.v4f32(<4 x float> %h3, <4 x float> %d1, <4 x float> splat (float 0xBFE70BF2A0000000))
  %h5 = call <4 x float> @llvm.fma.v4f32(<4 x float> %h4, <4 x float> %d1, <4 x float> splat (float 0x3FF71507C0000000))

  %res = shufflevector <4 x float> %l5, <4 x float> %h5, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>

  ret <8 x float> %res
}

-passes=vector-combine -mtriple=x86_64-- -mcpu=x86-64-v3

define <8 x float> @fmachain(<4 x float> %a0, <4 x float> %a1) #0 {
  %d0 = fadd <4 x float> %a0, splat (float -1.000000e+00)
  %d1 = fadd <4 x float> %a1, splat (float -1.000000e+00)
  %1 = shufflevector <4 x float> %d0, <4 x float> %d1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %2 = call <8 x float> @llvm.fma.v8f32(<8 x float> %1, <8 x float> splat (float 0x3F8DE8D040000000), <8 x float> splat (float 0xBFB3715EE0000000))
  %3 = shufflevector <4 x float> %d0, <4 x float> %d1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %4 = call <8 x float> @llvm.fma.v8f32(<8 x float> %2, <8 x float> %3, <8 x float> splat (float 0x3FC82778A0000000))
  %5 = shufflevector <4 x float> %d0, <4 x float> %d1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %6 = call <8 x float> @llvm.fma.v8f32(<8 x float> %4, <8 x float> %5, <8 x float> splat (float 0xBFD493F7E0000000))
  %7 = shufflevector <4 x float> %d0, <4 x float> %d1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %8 = call <8 x float> @llvm.fma.v8f32(<8 x float> %6, <8 x float> %7, <8 x float> splat (float 0x3FDE311220000000))
  %9 = shufflevector <4 x float> %d0, <4 x float> %d1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %10 = call <8 x float> @llvm.fma.v8f32(<8 x float> %8, <8 x float> %9, <8 x float> splat (float 0xBFE70BF2A0000000))
  %11 = shufflevector <4 x float> %d0, <4 x float> %d1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %res = call <8 x float> @llvm.fma.v8f32(<8 x float> %10, <8 x float> %11, <8 x float> splat (float 0x3FF71507C0000000))
  ret <8 x float> %res
}

Each time it merges an FMA intrinsic it creates yet another shufflevector <4 x float> %d0, <4 x float> %d1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7> instruction instead of reusing any existing nodes it has previously created. The multiple uses of the %d and %d1 values prevent them being merged as needed:

define <8 x float> @fmachain(<4 x float> %a0, <4 x float> %a1) #0 {
  %1 = shufflevector <4 x float> %a0, <4 x float> %a1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %2 = fadd <8 x float> %1, splat (float -1.000000e+00) 
  %3 = call <8 x float> @llvm.fma.v8f32(<8 x float> %2, <8 x float> splat (float 0x3F8DE8D040000000), <8 x float> splat (float 0xBFB3715EE0000000))
  %4 = call <8 x float> @llvm.fma.v8f32(<8 x float> %3, <8 x float> %3, <8 x float> splat (float 0x3FC82778A0000000))
  %5 = call <8 x float> @llvm.fma.v8f32(<8 x float> %4, <8 x float> %5, <8 x float> splat (float 0xBFD493F7E0000000))
  %6 = call <8 x float> @llvm.fma.v8f32(<8 x float> %5, <8 x float> %7, <8 x float> splat (float 0x3FDE311220000000))
  %7 = call <8 x float> @llvm.fma.v8f32(<8 x float> %6, <8 x float> %9, <8 x float> splat (float 0xBFE70BF2A0000000))
  %res = call <8 x float> @llvm.fma.v8f32(<8 x float> %10, <8 x float> %11, <8 x float> splat (float 0x3FF71507C0000000))
  ret <8 x float> %res
}

We don't need to end up with an implementation like EarlyCSE, but it would be good to have a basic numbering system paired with the InstructionWorkList to at least avoid repeatedly creating the same instruction over and over again.

RKSimon avatar Dec 04 '25 14:12 RKSimon

I can take a crack at this, if that's okay : )

Go for it - but this might not be an easy fix, and may require some alterations to either InstructionWorklist or how VectorCombine uses it.

RKSimon avatar Dec 15 '25 08:12 RKSimon

@RKSimon Oops, wrong account. Account above is my work account. Please reassign to this one, thanks!

kuroyukiasuna avatar Dec 16 '25 01:12 kuroyukiasuna

@RKSimon Apologies, was very busy in the last few weeks. Coming back to tackle this one, I did some initial attempts with a simple cache in the VectorCombine, and found out there's the dominance issue:

Instruction does not dominate all uses!
  %6 = shufflevector <4 x float> %d0, <4 x float> %d1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
  %1 = call <8 x float> @llvm.fma.v8f32(<8 x float> %6, <8 x float> splat (float 0x3F8DE8D040000000), <8 x float> splat (float 0xBFB3715EE0000000))

According to your previous comment, the suggestion is to modify the worklist. I'm thinking about 2 different options:

  1. Add the cache to InstructionWorklist instead, and create a Worklist.getOrCreateInstruction API for VectorCombine to consume;
  2. Track insertion sequence, enfore dominance when processing instructions in the worklist

Does these options generally align with what you are picturing? I'm leaning toward option 1, let me know what you are thinking, thanks!

kuroyukiasuna avatar Feb 19 '26 04:02 kuroyukiasuna

This was why I didn't make this a GFI - I didn't know how this should be solved!

My guess was that we'd have to change how we use the InstructionWorklist class somehow - @dtcxzyw might have some suggestions

RKSimon avatar Feb 19 '26 09:02 RKSimon

Add the cache to InstructionWorklist instead, and create a Worklist.getOrCreateInstruction API for VectorCombine to consume; Track insertion sequence, enfore dominance when processing instructions in the worklist

+1 for the first solution (IIRC it maintains a map for CSE), although it may still miss some optimizations. The second one is a bit complicated. As we push users of an instruction after simplification, we have to maintain another worklist. We cannot reuse instructions created in the current worklist when iterating the next worklist.

As for the meta issue https://github.com/llvm/llvm-project/issues/174602, it suffers from imprecise cost modeling when involving multi-uses. I believe the best solution so far is E-graph. It can efficiently represent the whole search space expanded by rewrite rules (saturation) and pick the global optimal expression (extraction). Since the number of vector operations is not particularly large, and we have a complete cost model, I think the e-graph method can deliver better performance while introducing negligible compile-time impact.

Here are some papers/links you may be interested in:

It is not the first time someone wants to implement equality saturation in LLVM. I just think VectorCombine is a good start point.

dtcxzyw avatar Feb 20 '26 17:02 dtcxzyw

@RKSimon @dtcxzyw I used a more localized approach: still having the cache inside the VectorCombine class itself, but have the logic for getting/creating a shuffle node to look for the earliest safe insertion point. Together with an RAII guard on Builder's current insertion point I was able to resolve the dominance issue. Please take a look at the PR and let me know if it's a good approach.

kuroyukiasuna avatar Feb 22 '26 19:02 kuroyukiasuna

@RKSimon @dtcxzyw Hello folks, just a gentle reminder on the pending review PR. Appreciated!

kuroyukiasuna avatar Apr 12 '26 02:04 kuroyukiasuna