[mlir][bug] linalg-specialize-generic-ops asserts on arith.select with a captured scalar
Version
git commit hash: 2a96d5737524012839e786699a0b327f22d1cfee
Bug Description
mlir-opt --linalg-specialize-generic-ops aborts on a verifier-accepted linalg.generic whose body uses arith.select with the second region argument as its condition and a captured scalar as one value. The same input runs successfully with no pass.
Crash Chain & Root Cause
Location: mlir/lib/Dialect/Linalg/Transforms/Specialize.cpp (areBinOpsSwapped).
Crash flow:
`--linalg-specialize-generic-ops`
→ `specializeLinalgElementwise()`
→ `areBinOpsSwapped()` for a two-input generic
→ first body operand is not block argument 0
→ remaining operands do not match the swapped pair
→ assertion: binary op uses just one block arg
The pass assumes a two-input generic body uses its two input block arguments in ordinary or swapped order before it verifies that assumption. Here the body is a valid arith.select with a captured scalar, so the swapped-operand assertion fires. This shape should be left unspecialized or handled explicitly.
Steps to Reproduce
Run the following command with the provided reproducer MLIR:
mlir-opt --linalg-specialize-generic-ops reproducer.mlir -o /dev/null
Reproducer (reproducer.mlir)
#map = affine_map<(d0) -> (d0)>
module {
func.func @f(%a: tensor<2xi32>, %mask: tensor<2xi1>, %out: tensor<2xi32>, %c: i32) -> tensor<2xi32> {
%r = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%a, %mask : tensor<2xi32>, tensor<2xi1>) outs(%out : tensor<2xi32>) {
^bb0(%x: i32, %m: i1, %z: i32):
%v = arith.select %m, %c, %x : i32
linalg.yield %v : i32
} -> tensor<2xi32>
return %r : tensor<2xi32>
}
}
Stack Dump
mlir-opt: Specialize.cpp:61: bool areBinOpsSwapped(mlir::linalg::GenericOp, bool): Assertion `op->getOpOperand(0 + isTernary).get() == body->getArgument(1 + isTernary) && op->getOpOperand(1 + isTernary).get() == body->getArgument(0 + isTernary) && "binary op uses just one block arg"' failed.
Stack dump:
0. Program arguments: mlir-opt --linalg-specialize-generic-ops reproducer.mlir -o <output>
#0 0x000055d2db1c1a1a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x202aa1a)
#1 0x000055d2db1be7ff llvm::sys::RunSignalHandlers() (mlir-opt+0x20277ff)
#2 0x000055d2db1c280b SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007fe54ec8c520 (libc.so.6+0x42520)
#4 0x00007fe54ece09bc __pthread_kill_implementation pthread_kill.c:44:76
#5 0x00007fe54ece09bc __pthread_kill_internal pthread_kill.c:78:10
#6 0x00007fe54ece09bc pthread_kill pthread_kill.c:89:10
#7 0x00007fe54ec8c476 gsignal raise.c:27:6
#8 0x00007fe54ec727f3 abort abort.c:81:7
#9 0x00007fe54ec7271b _nl_load_domain loadmsgcat.c:1177:9
#10 0x00007fe54ec83e96 (libc.so.6+0x39e96)
#11 0x000055d2e0c1c88f mlir::linalg::specializeGenericOp(mlir::RewriterBase&, mlir::linalg::GenericOp, bool) (mlir-opt+0x7a8588f)
#12 0x000055d2e0c296d9 mlir::linalg::LinalgSpecializationPattern::matchAndRewrite(mlir::linalg::GenericOp, mlir::PatternRewriter&) const (mlir-opt+0x7a926d9)
#13 0x000055d2e2f26f1d void llvm::function_ref<void ()>::callback_fn<mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>)::$_6>(long) PatternApplicator.cpp:0:0
#14 0x000055d2e2f237b3 mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>) (mlir-opt+0x9d8c7b3)
#15 0x000055d2e2efb3b6 (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#16 0x000055d2e2efa435 void llvm::function_ref<void ()>::callback_fn<(anonymous namespace)::RegionPatternRewriteDriver::simplify(bool*) &&::$_14>(long) GreedyPatternRewriteDriver.cpp:0:0
#17 0x000055d2e2ef7426 mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (mlir-opt+0x9d60426)
#18 0x000055d2e0c291f8 (anonymous namespace)::LinalgSpecializeGenericOpsPass::runOnOperation() Specialize.cpp:0:0
#19 0x000055d2e2f8154a mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0x9dea54a)
#20 0x000055d2e2f82219 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0x9deb219)
#21 0x000055d2e2f89263 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (mlir-opt+0x9df2263)
#22 0x000055d2e2f88ac1 mlir::PassManager::run(mlir::Operation*) (mlir-opt+0x9df1ac1)
#23 0x000055d2db271a55 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#24 0x000055d2db270d2c 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&)::$_3>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#25 0x000055d2e32c2df7 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+0xa12bdf7)
#26 0x000055d2db267d72 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (mlir-opt+0x20d0d72)
#27 0x000055d2db268171 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x20d1171)
#28 0x000055d2db2683ad mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x20d13ad)
#29 0x000055d2db1a7aa2 main (mlir-opt+0x2010aa2)
#30 0x00007fe54ec73d90 __libc_start_call_main libc_start_call_main.h:58:16
#31 0x00007fe54ec73e40 call_init libc-start.c:128:20
#32 0x00007fe54ec73e40 __libc_start_main libc-start.c:379:5
#33 0x000055d2db1a75d5 _start (mlir-opt+0x20105d5)
@llvm/issue-subscribers-mlir
Author: Peixiang Qin (Kiritobl)
git commit hash: 2a96d5737524012839e786699a0b327f22d1cfee
Bug Description
mlir-opt --linalg-specialize-generic-ops aborts on a verifier-accepted linalg.generic whose body uses arith.select with the second region argument as its condition and a captured scalar as one value. The same input runs successfully with no pass.
Crash Chain & Root Cause
Location: mlir/lib/Dialect/Linalg/Transforms/Specialize.cpp (areBinOpsSwapped).
Crash flow:
`--linalg-specialize-generic-ops`
→ `specializeLinalgElementwise()`
→ `areBinOpsSwapped()` for a two-input generic
→ first body operand is not block argument 0
→ remaining operands do not match the swapped pair
→ assertion: binary op uses just one block arg
The pass assumes a two-input generic body uses its two input block arguments in ordinary or swapped order before it verifies that assumption. Here the body is a valid arith.select with a captured scalar, so the swapped-operand assertion fires. This shape should be left unspecialized or handled explicitly.
Steps to Reproduce
Run the following command with the provided reproducer MLIR:
mlir-opt --linalg-specialize-generic-ops reproducer.mlir -o /dev/null
Reproducer (reproducer.mlir)
#map = affine_map<(d0) -> (d0)>
module {
func.func @<!-- -->f(%a: tensor<2xi32>, %mask: tensor<2xi1>, %out: tensor<2xi32>, %c: i32) -> tensor<2xi32> {
%r = linalg.generic {indexing_maps = [#map, #map, #map], iterator_types = ["parallel"]} ins(%a, %mask : tensor<2xi32>, tensor<2xi1>) outs(%out : tensor<2xi32>) {
^bb0(%x: i32, %m: i1, %z: i32):
%v = arith.select %m, %c, %x : i32
linalg.yield %v : i32
} -> tensor<2xi32>
return %r : tensor<2xi32>
}
}
Stack Dump
mlir-opt: Specialize.cpp:61: bool areBinOpsSwapped(mlir::linalg::GenericOp, bool): Assertion `op->getOpOperand(0 + isTernary).get() == body->getArgument(1 + isTernary) && op->getOpOperand(1 + isTernary).get() == body->getArgument(0 + isTernary) && "binary op uses just one block arg"' failed.
Stack dump:
0. Program arguments: mlir-opt --linalg-specialize-generic-ops reproducer.mlir -o <output>
#<!-- -->0 0x000055d2db1c1a1a llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x202aa1a)
#<!-- -->1 0x000055d2db1be7ff llvm::sys::RunSignalHandlers() (mlir-opt+0x20277ff)
#<!-- -->2 0x000055d2db1c280b SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#<!-- -->3 0x00007fe54ec8c520 (libc.so.6+0x42520)
#<!-- -->4 0x00007fe54ece09bc __pthread_kill_implementation pthread_kill.c:44:76
#<!-- -->5 0x00007fe54ece09bc __pthread_kill_internal pthread_kill.c:78:10
#<!-- -->6 0x00007fe54ece09bc pthread_kill pthread_kill.c:89:10
#<!-- -->7 0x00007fe54ec8c476 gsignal raise.c:27:6
#<!-- -->8 0x00007fe54ec727f3 abort abort.c:81:7
#<!-- -->9 0x00007fe54ec7271b _nl_load_domain loadmsgcat.c:1177:9
#<!-- -->10 0x00007fe54ec83e96 (libc.so.6+0x39e96)
#<!-- -->11 0x000055d2e0c1c88f mlir::linalg::specializeGenericOp(mlir::RewriterBase&, mlir::linalg::GenericOp, bool) (mlir-opt+0x7a8588f)
#<!-- -->12 0x000055d2e0c296d9 mlir::linalg::LinalgSpecializationPattern::matchAndRewrite(mlir::linalg::GenericOp, mlir::PatternRewriter&) const (mlir-opt+0x7a926d9)
#<!-- -->13 0x000055d2e2f26f1d void llvm::function_ref<void ()>::callback_fn<mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>)::$_6>(long) PatternApplicator.cpp:0:0
#<!-- -->14 0x000055d2e2f237b3 mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&, llvm::function_ref<bool (mlir::Pattern const&)>, llvm::function_ref<void (mlir::Pattern const&)>, llvm::function_ref<llvm::LogicalResult (mlir::Pattern const&)>) (mlir-opt+0x9d8c7b3)
#<!-- -->15 0x000055d2e2efb3b6 (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->16 0x000055d2e2efa435 void llvm::function_ref<void ()>::callback_fn<(anonymous namespace)::RegionPatternRewriteDriver::simplify(bool*) &&::$_14>(long) GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->17 0x000055d2e2ef7426 mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (mlir-opt+0x9d60426)
#<!-- -->18 0x000055d2e0c291f8 (anonymous namespace)::LinalgSpecializeGenericOpsPass::runOnOperation() Specialize.cpp:0:0
#<!-- -->19 0x000055d2e2f8154a mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0x9dea54a)
#<!-- -->20 0x000055d2e2f82219 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0x9deb219)
#<!-- -->21 0x000055d2e2f89263 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (mlir-opt+0x9df2263)
#<!-- -->22 0x000055d2e2f88ac1 mlir::PassManager::run(mlir::Operation*) (mlir-opt+0x9df1ac1)
#<!-- -->23 0x000055d2db271a55 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#<!-- -->24 0x000055d2db270d2c 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&)::$_3>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#<!-- -->25 0x000055d2e32c2df7 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+0xa12bdf7)
#<!-- -->26 0x000055d2db267d72 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (mlir-opt+0x20d0d72)
#<!-- -->27 0x000055d2db268171 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x20d1171)
#<!-- -->28 0x000055d2db2683ad mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x20d13ad)
#<!-- -->29 0x000055d2db1a7aa2 main (mlir-opt+0x2010aa2)
#<!-- -->30 0x00007fe54ec73d90 __libc_start_call_main libc_start_call_main.h:58:16
#<!-- -->31 0x00007fe54ec73e40 call_init libc-start.c:128:20
#<!-- -->32 0x00007fe54ec73e40 __libc_start_main libc-start.c:379:5
#<!-- -->33 0x000055d2db1a75d5 _start (mlir-opt+0x20105d5)