[SDAG] Divide-by-zero crash in visitSUBSAT when usubsat is given known zero value
Running llc on the following IR:
target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-i128:128-f80:128-n8:16:32:64-S128"
target triple = "x86_64-unknown-linux-gnu"
define <4 x i32> @do_stuff(<4 x i32> %v3) {
bb0:
%v5 = xor <4 x i32> %v3, %v3
%v7 = call <4 x i32> @llvm.usub.sat.v4i32(<4 x i32> %v5, <4 x i32> %v3)
ret <4 x i32> %v7
}
; Function Attrs: nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none)
declare <4 x i32> @llvm.usub.sat.v4i32(<4 x i32>, <4 x i32>) #0
attributes #0 = { nocallback nocreateundeforpoison nofree nosync nounwind speculatable willreturn memory(none) }
Godbolt link: https://godbolt.org/z/TovMnh5zT
will crash in DAGCombiner::visitSUBSAT:
0. Program arguments: /opt/compiler-explorer/clang-trunk/bin/llc -o /app/output.s -x86-asm-syntax=intel <source>
1. Running pass 'Function Pass Manager' on module '<source>'.
2. Running pass 'X86 DAG->DAG Instruction Selection' on function '@do_stuff'
#0 0x0000000003c44798 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/opt/compiler-explorer/clang-trunk/bin/llc+0x3c44798)
#1 0x0000000003c4183d SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#2 0x00007c58da245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
#3 0x00000000037f1603 (anonymous namespace)::DAGCombiner::visitSUBSAT(llvm::SDNode*) DAGCombiner.cpp:0:0
#4 0x000000000389c088 (anonymous namespace)::DAGCombiner::visit(llvm::SDNode*) DAGCombiner.cpp:0:0
#5 0x000000000389eaa3 (anonymous namespace)::DAGCombiner::combine(llvm::SDNode*) DAGCombiner.cpp:0:0
#6 0x00000000038a0365 llvm::SelectionDAG::Combine(llvm::CombineLevel, llvm::BatchAAResults*, llvm::CodeGenOptLevel) (/opt/compiler-explorer/clang-trunk/bin/llc+0x38a0365)
#7 0x00000000039e0192 llvm::SelectionDAGISel::CodeGenAndEmitDAG() (/opt/compiler-explorer/clang-trunk/bin/llc+0x39e0192)
#8 0x00000000039e260f llvm::SelectionDAGISel::SelectAllBasicBlocks(llvm::Function const&) (/opt/compiler-explorer/clang-trunk/bin/llc+0x39e260f)
#9 0x00000000039e4035 llvm::SelectionDAGISel::runOnMachineFunction(llvm::MachineFunction&) (/opt/compiler-explorer/clang-trunk/bin/llc+0x39e4035)
...
Program terminated with signal: SIGFPE
Looking at the code, it seems like a known-zero value will have ActiveBits be zero, and NarrowBits will also end up as zero, which leads to a divide-by-zero crash.
https://github.com/llvm/llvm-project/blob/4ccc07bc4d106cbc800be1fa2bb9b0dd456369b5/llvm/lib/CodeGen/SelectionDAG/DAGCombiner.cpp#L4726-L4730
A bisect confirmed this started happening with 9584b932c86d8c234856c13a185bc02d8afeb317 (PR: https://github.com/llvm/llvm-project/pull/206592 )
I confirmed this still happens on latest trunk (0c8f1121a2bb2095227dab641e66e5ea4c238e87)
Not sure what the code should do in the case of a known zero: I'd assume just skip it and let other passes take care of it?
For disclosure: this code was found by a fuzzer that was built partly using LLM generative tooling, but this issue and its analysis are done by myself and I manually reproduced the bug to verify its occurrence
cc @sipher-01
Let me see it
Hi @Benjins ,
Added NarrowBits != 0 guard in visitSUBSAT to prevent divide-by-zero on known-zero LHS in latest PR.