[mlir][bug][RemoveDeadValues][fuzzer-found] live result contradicts dead-operand cleanup assumption
Version
LLVM/MLIR 24.0.0git, commit c275ebd64bf5b05e36a3a8e8b8daa32ba9a52aac (optimized build with assertions).
Bug Description
Observed failure: live result contradicts dead-operand cleanup assumption. The input passes verification without the pass, but the command below crashes. This bug was found by a fuzzer.
Likely Cause
processSimpleOp assumes that an operation with any operand in nonLiveSet must have all results non-live. In these reproductions, a result still reaches a live memory write or branch condition. The cleanup pass asserts on the contradictory liveness state instead of preserving the live result or reconciling the analysis.
Steps to Reproduce
mlir-opt -remove-dead-values=canonicalize=1 reproducer.mlir -o /dev/null
Reproducer (reproducer.mlir)
module {
func.func private @f(%arg0: memref<i32>) {
%c0 = arith.constant 0 : i32
%c1 = arith.constant 1 : i32
%sum = arith.addi %c1, %c0 : i32
%base, %offset = memref.extract_strided_metadata %arg0 : memref<i32> -> memref<i32>, index
%cast = memref.reinterpret_cast %base to offset: [%offset], sizes: [], strides: [] : memref<i32> to memref<i32, strided<[], offset: ?>>
memref.store %sum, %cast[] : memref<i32, strided<[], offset: ?>>
return
}
func.func @main(%arg0: memref<i32>) {
call @f(%arg0) : (memref<i32>) -> ()
return
}
}
Stack Dump (full stderr)
mlir-opt: mlir/lib/Transforms/RemoveDeadValues.cpp:236: void {anonymous}::processSimpleOp(mlir::Operation*, mlir::dataflow::RunLivenessAnalysis&, mlir::DenseSet<mlir::Value>&, {anonymous}::RDVFinalCleanupList&): Assertion `!hasLive(op->getResults(), nonLiveSet, la) && "expected the op to have no live results"' failed.
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: mlir-opt -remove-dead-values=canonicalize=1 reproducer.mlir -o /dev/null
#0 0x0000562ea4bdd699 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x1fa0699)
#1 0x0000562ea4bd9e9c llvm::sys::RunSignalHandlers() (mlir-opt+0x1f9ce9c)
#2 0x0000562ea4bda006 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007fe5f34c2520 (libc.so.6+0x42520)
#4 0x00007fe5f35169bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#5 0x00007fe5f35169bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#6 0x00007fe5f35169bc pthread_kill ./nptl/pthread_kill.c:89:10
#7 0x00007fe5f34c2476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007fe5f34a87f3 abort ./stdlib/abort.c:81:7
#9 0x00007fe5f34a871b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007fe5f34b9e96 (libc.so.6+0x39e96)
#11 0x0000562eacfbde89 (anonymous namespace)::processSimpleOp(mlir::Operation*, mlir::dataflow::RunLivenessAnalysis&, llvm::DenseSet<mlir::Value, llvm::DenseMapInfo<mlir::Value, void>>&, (anonymous namespace)::RDVFinalCleanupList&) RemoveDeadValues.cpp:0:0
#12 0x0000562eacfc24a8 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.1) RemoveDeadValues.cpp:0:0
#13 0x0000562eacfc24a8 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.1) RemoveDeadValues.cpp:0:0
#14 0x0000562eacfc355e (anonymous namespace)::RemoveDeadValues::runOnOperation() RemoveDeadValues.cpp:0:0
#15 0x0000562ead0dc449 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0xa49f449)
#16 0x0000562ead0dc7b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0xa49f7b4)
#17 0x0000562ead0de6f2 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (mlir-opt+0xa4a16f2)
#18 0x0000562ead0df6c9 mlir::PassManager::run(mlir::Operation*) (mlir-opt+0xa4a26c9)
#19 0x0000562ea4ca39b3 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#20 0x0000562ea4ca42de processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, mlir::SourceMgrDiagnosticVerifierHandler*, llvm::ThreadPoolInterface*) MlirOptMain.cpp:0:0
#21 0x0000562ea4ca4521 llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::'lambda'(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#22 0x0000562ead4aea2d mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (mlir-opt+0xa871a2d)
#23 0x0000562ea4c9f2da mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (.part.0) MlirOptMain.cpp:0:0
#24 0x0000562ea4ca4af8 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x2067af8)
#25 0x0000562ea4ca4d54 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x2067d54)
#26 0x0000562ea4b515cc main (mlir-opt+0x1f145cc)
#27 0x00007fe5f34a9d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#28 0x00007fe5f34a9e40 call_init ./csu/../csu/libc-start.c:128:20
#29 0x00007fe5f34a9e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#30 0x0000562ea4bbcf35 _start (mlir-opt+0x1f7ff35)
@llvm/issue-subscribers-mlir
Author: Kirito (Kiritobl)
LLVM/MLIR 24.0.0git, commit c275ebd64bf5b05e36a3a8e8b8daa32ba9a52aac (optimized build with assertions).
Bug Description
Observed failure: live result contradicts dead-operand cleanup assumption. The input passes verification without the pass, but the command below crashes. This bug was found by a fuzzer.
Likely Cause
processSimpleOp assumes that an operation with any operand in nonLiveSet must have all results non-live. In these reproductions, a result still reaches a live memory write or branch condition. The cleanup pass asserts on the contradictory liveness state instead of preserving the live result or reconciling the analysis.
Steps to Reproduce
mlir-opt -remove-dead-values=canonicalize=1 reproducer.mlir -o /dev/null
Reproducer (reproducer.mlir)
module {
func.func private @<!-- -->f(%arg0: memref<i32>) {
%c0 = arith.constant 0 : i32
%c1 = arith.constant 1 : i32
%sum = arith.addi %c1, %c0 : i32
%base, %offset = memref.extract_strided_metadata %arg0 : memref<i32> -> memref<i32>, index
%cast = memref.reinterpret_cast %base to offset: [%offset], sizes: [], strides: [] : memref<i32> to memref<i32, strided<[], offset: ?>>
memref.store %sum, %cast[] : memref<i32, strided<[], offset: ?>>
return
}
func.func @<!-- -->main(%arg0: memref<i32>) {
call @<!-- -->f(%arg0) : (memref<i32>) -> ()
return
}
}
Stack Dump (full stderr)
mlir-opt: mlir/lib/Transforms/RemoveDeadValues.cpp:236: void {anonymous}::processSimpleOp(mlir::Operation*, mlir::dataflow::RunLivenessAnalysis&, mlir::DenseSet<mlir::Value>&, {anonymous}::RDVFinalCleanupList&): Assertion `!hasLive(op->getResults(), nonLiveSet, la) && "expected the op to have no live results"' failed.
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: mlir-opt -remove-dead-values=canonicalize=1 reproducer.mlir -o /dev/null
#<!-- -->0 0x0000562ea4bdd699 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x1fa0699)
#<!-- -->1 0x0000562ea4bd9e9c llvm::sys::RunSignalHandlers() (mlir-opt+0x1f9ce9c)
#<!-- -->2 0x0000562ea4bda006 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#<!-- -->3 0x00007fe5f34c2520 (libc.so.6+0x42520)
#<!-- -->4 0x00007fe5f35169bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#<!-- -->5 0x00007fe5f35169bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#<!-- -->6 0x00007fe5f35169bc pthread_kill ./nptl/pthread_kill.c:89:10
#<!-- -->7 0x00007fe5f34c2476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#<!-- -->8 0x00007fe5f34a87f3 abort ./stdlib/abort.c:81:7
#<!-- -->9 0x00007fe5f34a871b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#<!-- -->10 0x00007fe5f34b9e96 (libc.so.6+0x39e96)
#<!-- -->11 0x0000562eacfbde89 (anonymous namespace)::processSimpleOp(mlir::Operation*, mlir::dataflow::RunLivenessAnalysis&, llvm::DenseSet<mlir::Value, llvm::DenseMapInfo<mlir::Value, void>>&, (anonymous namespace)::RDVFinalCleanupList&) RemoveDeadValues.cpp:0:0
#<!-- -->12 0x0000562eacfc24a8 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.1) RemoveDeadValues.cpp:0:0
#<!-- -->13 0x0000562eacfc24a8 void mlir::detail::walk<mlir::ForwardIterator>(mlir::Operation*, llvm::function_ref<void (mlir::Operation*)>, mlir::WalkOrder) (.constprop.1) RemoveDeadValues.cpp:0:0
#<!-- -->14 0x0000562eacfc355e (anonymous namespace)::RemoveDeadValues::runOnOperation() RemoveDeadValues.cpp:0:0
#<!-- -->15 0x0000562ead0dc449 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0xa49f449)
#<!-- -->16 0x0000562ead0dc7b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0xa49f7b4)
#<!-- -->17 0x0000562ead0de6f2 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (mlir-opt+0xa4a16f2)
#<!-- -->18 0x0000562ead0df6c9 mlir::PassManager::run(mlir::Operation*) (mlir-opt+0xa4a26c9)
#<!-- -->19 0x0000562ea4ca39b3 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#<!-- -->20 0x0000562ea4ca42de processBuffer(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, mlir::MlirOptMainConfig const&, mlir::DialectRegistry&, mlir::SourceMgrDiagnosticVerifierHandler*, llvm::ThreadPoolInterface*) MlirOptMain.cpp:0:0
#<!-- -->21 0x0000562ea4ca4521 llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>::callback_fn<mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::'lambda'(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, llvm::raw_ostream&)>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#<!-- -->22 0x0000562ead4aea2d mlir::splitAndProcessBuffer(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (mlir-opt+0xa871a2d)
#<!-- -->23 0x0000562ea4c9f2da mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (.part.0) MlirOptMain.cpp:0:0
#<!-- -->24 0x0000562ea4ca4af8 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x2067af8)
#<!-- -->25 0x0000562ea4ca4d54 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x2067d54)
#<!-- -->26 0x0000562ea4b515cc main (mlir-opt+0x1f145cc)
#<!-- -->27 0x00007fe5f34a9d90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#<!-- -->28 0x00007fe5f34a9e40 call_init ./csu/../csu/libc-start.c:128:20
#<!-- -->29 0x00007fe5f34a9e40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#<!-- -->30 0x0000562ea4bbcf35 _start (mlir-opt+0x1f7ff35)
Adding another verifier-accepted reproducer for this assertion on LLVM/MLIR 24.0.0git, commit 982197fad060f461277020c0ca413d93a5f2a8a6(optimized build with assertions). It also contains memref.extract_strided_metadata followed by memref.reinterpret_cast, but the operation at the assertion is memref.load rather than the arith.addi in the original report.
#map = affine_map<(d0) -> (d0)>
#map0 = affine_map<(d0) -> ()>
func.func @f(%buf: memref<10xf32>) {
%base, %offset, %sizes, %strides = memref.extract_strided_metadata %buf : memref<10xf32> -> memref<f32>, index, index, index
%view = memref.reinterpret_cast %base to offset: [%offset], sizes: [%sizes], strides: [%strides] : memref<f32> to memref<?xf32, strided<[?], offset: ?>>
%i = arith.constant 0 : index
%x = memref.load %view[%i] : memref<?xf32, strided<[?], offset: ?>>
%c = arith.constant 0.0 : f32
%v = vector.broadcast %c : f32 to vector<2xf32>
%inserted = vector.insert %x, %v [0] : f32 into vector<2xf32>
%mask = vector.constant_mask [1] : vector<2xi1>
%r = vector.mask %mask { vector.contract {indexing_maps = [#map, #map, #map0], iterator_types = ["reduction"], kind = #vector.kind<mul>} %inserted, %inserted, %x : vector<2xf32>, vector<2xf32> into f32 } : vector<2xi1> -> f32
memref.store %r, %buf[%i] : memref<10xf32>
return
}
To reproduce:
mlir-opt -remove-dead-values=canonicalize=1 reproducer.mlir -o /dev/null
mlir-opt reproducer.mlir -o /dev/null succeeds. The command above aborts with SIGABRT at RemoveDeadValues.cpp:236:
Assertion `!hasLive(op->getResults(), nonLiveSet, la) && "expected the op to have no live results"' failed.
With --debug-only=remove-dead-values, processSimpleOp reports dead operands for memref.load %view[%i] and then reports its result as live. The result is consumed by vector.contract inside the vector.mask region. This reaches the same dead-operand/live-result contradiction described in this issue. Running --canonicalize before --remove-dead-values avoids the crash for this reproducer.
This may be another manifestation of the same underlying problem; the precise source of the inconsistent liveness state still needs confirmation.