[llvm] [VectorCombine] Merge duplicate shuffle instruction nodes (PR #182768)
Yunbo Ni via llvm-commits
llvm-commits at lists.llvm.org
Thu Mar 5 10:50:18 PST 2026
cardigan1008 wrote:
Hi @kuroyukiasuna , there is a related crash case:
```llvm
define <8 x i32> @test3(<4 x i32> %0, <4 x i32> %1, <4 x i32> %2, <4 x i32> %3) {
entry:
%v0 = add <4 x i32> %0, %1
br label %block2
block2:
%v1 = add <4 x i32> %2, %3
br i1 true, label %block3, label %block4
block3:
%4 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %v0, <4 x i32> %v1)
%5 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %v1, <4 x i32> %v0)
%6 = shufflevector <4 x i32> %4, <4 x i32> %5, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
br label %end
block4:
%7 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %v0, <4 x i32> %v1)
%8 = call <4 x i32> @llvm.smax.v4i32(<4 x i32> %v1, <4 x i32> %v0)
%9 = shufflevector <4 x i32> %7, <4 x i32> %8, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
br label %end
end:
%res = phi <8 x i32> [ %6, %block3 ], [ %9, %block4 ]
ret <8 x i32> %res
}
declare <4 x i32> @llvm.smax.v4i32(<4 x i32>, <4 x i32>)
```
With opt built on this patch, a crash occurred:
```sh
Instruction does not dominate all uses!
%4 = shufflevector <4 x i32> %v0, <4 x i32> %v1, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
%7 = call <8 x i32> @llvm.smax.v8i32(<8 x i32> %4, <8 x i32> %5)
Instruction does not dominate all uses!
%5 = shufflevector <4 x i32> %v1, <4 x i32> %v0, <8 x i32> <i32 0, i32 1, i32 2, i32 3, i32 4, i32 5, i32 6, i32 7>
%7 = call <8 x i32> @llvm.smax.v8i32(<8 x i32> %4, <8 x i32> %5)
LLVM ERROR: Broken module found, compilation aborted!
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace and instructions to reproduce the bug.
Stack dump:
0. Program arguments: ./build/llvm-build/pr/182768/bin/opt -S -passes=vector-combine src.ll
1. Running pass "verify" on module "src.ll"
#0 0x00007ec7a21366e6 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/libLLVMSupport.so.23.0git+0x3366e6)
#1 0x00007ec7a2136b59 PrintStackTraceSignalHandler(void*) Signals.cpp:0:0
#2 0x00007ec7a213319c llvm::sys::RunSignalHandlers() (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/libLLVMSupport.so.23.0git+0x33319c)
#3 0x00007ec7a21356bc SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#4 0x00007ec7a1a45330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#5 0x00007ec7a1a9eb2c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#6 0x00007ec7a1a9eb2c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#7 0x00007ec7a1a9eb2c pthread_kill ./nptl/pthread_kill.c:89:10
#8 0x00007ec7a1a4527e raise ./signal/../sysdeps/posix/raise.c:27:6
#9 0x00007ec7a1a288ff abort ./stdlib/abort.c:81:7
#10 0x00007ec7a1fae16a llvm::report_fatal_error(llvm::Twine const&, bool) (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/libLLVMSupport.so.23.0git+0x1ae16a)
#11 0x00007ec7a1fadf41 llvm::report_fatal_error(llvm::StringRef, bool) (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/libLLVMSupport.so.23.0git+0x1adf41)
#12 0x00007ec796416a06 llvm::VerifierPass::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/../lib/libLLVMCore.so.23.0git+0x616a06)
#13 0x00007ec7a24a698f llvm::detail::PassModel<llvm::Module, llvm::VerifierPass, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/libLLVMOptDriver.so.23.0git+0x5398f)
#14 0x00007ec796358af3 llvm::PassManager<llvm::Module, llvm::AnalysisManager<llvm::Module>>::run(llvm::Module&, llvm::AnalysisManager<llvm::Module>&) (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/../lib/libLLVMCore.so.23.0git+0x558af3)
#15 0x00007ec7a247c03a llvm::runPassPipeline(llvm::StringRef, llvm::Module&, llvm::TargetMachine*, llvm::TargetLibraryInfoImpl*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::ToolOutputFile*, llvm::StringRef, llvm::ArrayRef<llvm::PassPlugin>, llvm::ArrayRef<std::function<void (llvm::PassBuilder&)>>, llvm::opt_tool::OutputKind, llvm::opt_tool::VerifierKind, bool, bool, bool, bool, bool, bool, bool, bool) (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/libLLVMOptDriver.so.23.0git+0x2903a)
#16 0x00007ec7a24b37fc optMain (/data/yunboni/projects/Archer/build/llvm-build/pr/182768/bin/../lib/libLLVMOptDriver.so.23.0git+0x607fc)
#17 0x000056a4de33d1e8 main (./build/llvm-build/pr/182768/bin/opt+0x11e8)
#18 0x00007ec7a1a2a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#19 0x00007ec7a1a2a28b call_init ./csu/../csu/libc-start.c:128:20
#20 0x00007ec7a1a2a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#21 0x000056a4de33d0a5 _start (./build/llvm-build/pr/182768/bin/opt+0x10a5)
```
> Note: This is a review assisted with a self-built agent. The reproducer was validated manually. Please let me know if anything is wrong.
The provided test case triggers the bug because it creates a scenario where `VectorCombine` attempts to deduplicate a `shufflevector` instruction across two independent basic blocks (`block3` and `block4`). The operands for the shuffle, `%v0` and `%v1`, are defined in different basic blocks (`entry` and `block2`, respectively). When `VectorCombine` processes the first foldable pattern in `block3`, it calls `findEarliestInsertionPoint(%v0, %v1)`. Since `%v0` and `%v1` are in different blocks, the function falls back to returning the current insertion point, which is inside `block3`. The newly created shuffle is then cached globally with the key `{%v0, %v1, Mask}`.
Later, when `VectorCombine` processes the identical foldable pattern in `block4`, it looks up the cache using the same key `{%v0, %v1, Mask}` and finds the shuffle created in `block3`. It reuses this cached shuffle for the fold in `block4`. However, `block3` does not dominate `block4` (they are parallel branches). As a result, the reused shuffle instruction in `block4` uses a value defined in `block3`, leading to a dominance violation and causing the LLVM verifier to crash.
The weakness in the fix lies in the `findEarliestInsertionPoint` function and its interaction with the global `ShuffleCache`. The fix assumes that if `V0` and `V1` are in different blocks, the current insertion point is a safe "earliest" point that will dominate all future reuses of the cached shuffle. This assumption is fundamentally flawed because the current insertion point is only guaranteed to dominate the current use, not all potential future uses across the entire function. By caching a locally-dominated instruction globally, the fix introduces the possibility of reusing an instruction in a context where it is not dominated by its definition, violating LLVM IR's SSA dominance properties.
https://github.com/llvm/llvm-project/pull/182768
More information about the llvm-commits
mailing list