[MLIR][bug][Vector][founded by fuzzer] UnrollContractionPattern casts a scalar vector.contract result to VectorType
Version
git commit hash: c275ebd64bf5b05e36a3a8e8b8daa32ba9a52aac (LLVM 24.0.0git, optimized build with assertions)
Bug Description
mlir-opt --test-vector-unrolling-patterns=unroll-based-on-type aborts on a valid reduction vector.contract whose accumulator and result are scalar f32. The input passes verification without the pass.
Crash Chain & Root Cause
Location: mlir/lib/Dialect/Vector/Transforms/VectorUnroll.cpp, UnrollContractionPattern::matchAndRewrite() (line 289 at this commit).
--test-vector-unrolling-patterns=unroll-based-on-type
→ UnrollContractionPattern::matchAndRewrite()
→ getTargetShape(options, contractOp) succeeds
→ cast<VectorType>(contractOp.getResultType())
→ result type is scalar f32
→ Casting.h:560: cast<Ty>() argument of incompatible type
UnrollContractionPattern accepts this contraction for unrolling, then unconditionally casts its scalar result type to VectorType. The pattern assumes a vector result and should reject scalar results before that cast or support them throughout the rewrite.
Steps to Reproduce
mlir-opt --test-vector-unrolling-patterns=unroll-based-on-type reproducer.mlir -o /dev/null
Reproducer (reproducer.mlir)
#map = affine_map<(d0) -> (d0)>
#map0 = affine_map<(d0) -> ()>
module {
func.func @f(%a: vector<4xf32>, %b: vector<4xf32>, %acc: f32) -> f32 {
%r = vector.contract {indexing_maps = [#map, #map, #map0], iterator_types = ["reduction"], kind = #vector.kind<add>} %a, %b, %acc : vector<4xf32>, vector<4xf32> into f32
return %r : f32
}
}
Stack Dump
mlir-opt: llvm/include/llvm/Support/Casting.h:560: decltype(auto) llvm::cast(const From&) [with To = mlir::VectorType; From = mlir::Type]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' 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 --test-vector-unrolling-patterns=unroll-based-on-type reproducer.mlir -o /dev/null
#0 0x0000562edef0d699 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x1fa0699)
#1 0x0000562edef09e9c llvm::sys::RunSignalHandlers() (mlir-opt+0x1f9ce9c)
#2 0x0000562edef0a006 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#3 0x00007faaeac28520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#4 0x00007faaeac7c9bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#5 0x00007faaeac7c9bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#6 0x00007faaeac7c9bc pthread_kill ./nptl/pthread_kill.c:89:10
#7 0x00007faaeac28476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#8 0x00007faaeac0e7f3 abort ./stdlib/abort.c:81:7
#9 0x00007faaeac0e71b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#10 0x00007faaeac1fe96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#11 0x0000562ee0573876 (mlir-opt+0x3606876)
#12 0x0000562ee554eb5d (anonymous namespace)::UnrollContractionPattern::matchAndRewrite(mlir::vector::ContractionOp, mlir::PatternRewriter&) const VectorUnroll.cpp:0:0
#13 0x0000562ee73a4551 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&)>)::'lambda'()::operator()() const PatternApplicator.cpp:0:0
#14 0x0000562ee73a5bca 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+0xa438bca)
#15 0x0000562ee736df8a (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#16 0x0000562ee7372336 mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (mlir-opt+0xa405336)
#17 0x0000562edf84f9f2 mlir::applyPatternsGreedily(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (.constprop.0) TestVectorTransforms.cpp:0:0
#18 0x0000562edf85a313 (anonymous namespace)::TestVectorUnrollingPatterns::runOnOperation() TestVectorTransforms.cpp:0:0
#19 0x0000562ee740c449 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0xa49f449)
#20 0x0000562ee740c7b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0xa49f7b4)
#21 0x0000562ee740d026 mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::'lambda12'(mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::OpPMInfo&)::operator()(mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::OpPMInfo&) const Pass.cpp:0:0
#22 0x0000562ee740b39c mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool) (mlir-opt+0xa49e39c)
#23 0x0000562ee740c321 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0xa49f321)
#24 0x0000562ee740c7b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0xa49f7b4)
#25 0x0000562ee740e6f2 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (mlir-opt+0xa4a16f2)
#26 0x0000562ee740f6c9 mlir::PassManager::run(mlir::Operation*) (mlir-opt+0xa4a26c9)
#27 0x0000562edefd39b3 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#28 0x0000562edefd42de 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
#29 0x0000562edefd4521 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
#30 0x0000562ee77dea2d 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)
#31 0x0000562edefcf2da 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
#32 0x0000562edefd4af8 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x2067af8)
#33 0x0000562edefd4d54 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x2067d54)
#34 0x0000562edee815cc main (mlir-opt+0x1f145cc)
#35 0x00007faaeac0fd90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#36 0x00007faaeac0fe40 call_init ./csu/../csu/libc-start.c:128:20
#37 0x00007faaeac0fe40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#38 0x0000562edeeecf35 _start (mlir-opt+0x1f7ff35)
@llvm/issue-subscribers-mlir
Author: Kirito (Kiritobl)
git commit hash: c275ebd64bf5b05e36a3a8e8b8daa32ba9a52aac (LLVM 24.0.0git, optimized build with assertions)
Bug Description
mlir-opt --test-vector-unrolling-patterns=unroll-based-on-type aborts on a valid reduction vector.contract whose accumulator and result are scalar f32. The input passes verification without the pass.
Crash Chain & Root Cause
Location: mlir/lib/Dialect/Vector/Transforms/VectorUnroll.cpp, UnrollContractionPattern::matchAndRewrite() (line 289 at this commit).
--test-vector-unrolling-patterns=unroll-based-on-type
→ UnrollContractionPattern::matchAndRewrite()
→ getTargetShape(options, contractOp) succeeds
→ cast<VectorType>(contractOp.getResultType())
→ result type is scalar f32
→ Casting.h:560: cast<Ty>() argument of incompatible type
UnrollContractionPattern accepts this contraction for unrolling, then unconditionally casts its scalar result type to VectorType. The pattern assumes a vector result and should reject scalar results before that cast or support them throughout the rewrite.
Steps to Reproduce
mlir-opt --test-vector-unrolling-patterns=unroll-based-on-type reproducer.mlir -o /dev/null
Reproducer (reproducer.mlir)
#map = affine_map<(d0) -> (d0)>
#map0 = affine_map<(d0) -> ()>
module {
func.func @<!-- -->f(%a: vector<4xf32>, %b: vector<4xf32>, %acc: f32) -> f32 {
%r = vector.contract {indexing_maps = [#map, #map, #map0], iterator_types = ["reduction"], kind = #vector.kind<add>} %a, %b, %acc : vector<4xf32>, vector<4xf32> into f32
return %r : f32
}
}
Stack Dump
mlir-opt: llvm/include/llvm/Support/Casting.h:560: decltype(auto) llvm::cast(const From&) [with To = mlir::VectorType; From = mlir::Type]: Assertion `isa<To>(Val) && "cast<Ty>() argument of incompatible type!"' 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 --test-vector-unrolling-patterns=unroll-based-on-type reproducer.mlir -o /dev/null
#<!-- -->0 0x0000562edef0d699 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x1fa0699)
#<!-- -->1 0x0000562edef09e9c llvm::sys::RunSignalHandlers() (mlir-opt+0x1f9ce9c)
#<!-- -->2 0x0000562edef0a006 SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
#<!-- -->3 0x00007faaeac28520 (/lib/x86_64-linux-gnu/libc.so.6+0x42520)
#<!-- -->4 0x00007faaeac7c9bc __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
#<!-- -->5 0x00007faaeac7c9bc __pthread_kill_internal ./nptl/pthread_kill.c:78:10
#<!-- -->6 0x00007faaeac7c9bc pthread_kill ./nptl/pthread_kill.c:89:10
#<!-- -->7 0x00007faaeac28476 gsignal ./signal/../sysdeps/posix/raise.c:27:6
#<!-- -->8 0x00007faaeac0e7f3 abort ./stdlib/abort.c:81:7
#<!-- -->9 0x00007faaeac0e71b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#<!-- -->10 0x00007faaeac1fe96 (/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
#<!-- -->11 0x0000562ee0573876 (mlir-opt+0x3606876)
#<!-- -->12 0x0000562ee554eb5d (anonymous namespace)::UnrollContractionPattern::matchAndRewrite(mlir::vector::ContractionOp, mlir::PatternRewriter&) const VectorUnroll.cpp:0:0
#<!-- -->13 0x0000562ee73a4551 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&)>)::'lambda'()::operator()() const PatternApplicator.cpp:0:0
#<!-- -->14 0x0000562ee73a5bca 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+0xa438bca)
#<!-- -->15 0x0000562ee736df8a (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->16 0x0000562ee7372336 mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (mlir-opt+0xa405336)
#<!-- -->17 0x0000562edf84f9f2 mlir::applyPatternsGreedily(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (.constprop.0) TestVectorTransforms.cpp:0:0
#<!-- -->18 0x0000562edf85a313 (anonymous namespace)::TestVectorUnrollingPatterns::runOnOperation() TestVectorTransforms.cpp:0:0
#<!-- -->19 0x0000562ee740c449 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0xa49f449)
#<!-- -->20 0x0000562ee740c7b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0xa49f7b4)
#<!-- -->21 0x0000562ee740d026 mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::'lambda12'(mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::OpPMInfo&)::operator()(mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool)::OpPMInfo&) const Pass.cpp:0:0
#<!-- -->22 0x0000562ee740b39c mlir::detail::OpToOpPassAdaptor::runOnOperationAsyncImpl(bool) (mlir-opt+0xa49e39c)
#<!-- -->23 0x0000562ee740c321 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (mlir-opt+0xa49f321)
#<!-- -->24 0x0000562ee740c7b4 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (mlir-opt+0xa49f7b4)
#<!-- -->25 0x0000562ee740e6f2 mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (mlir-opt+0xa4a16f2)
#<!-- -->26 0x0000562ee740f6c9 mlir::PassManager::run(mlir::Operation*) (mlir-opt+0xa4a26c9)
#<!-- -->27 0x0000562edefd39b3 performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#<!-- -->28 0x0000562edefd42de 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
#<!-- -->29 0x0000562edefd4521 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
#<!-- -->30 0x0000562ee77dea2d 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)
#<!-- -->31 0x0000562edefcf2da 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
#<!-- -->32 0x0000562edefd4af8 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x2067af8)
#<!-- -->33 0x0000562edefd4d54 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (mlir-opt+0x2067d54)
#<!-- -->34 0x0000562edee815cc main (mlir-opt+0x1f145cc)
#<!-- -->35 0x00007faaeac0fd90 __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:58:16
#<!-- -->36 0x00007faaeac0fe40 call_init ./csu/../csu/libc-start.c:128:20
#<!-- -->37 0x00007faaeac0fe40 __libc_start_main ./csu/../csu/libc-start.c:379:5
#<!-- -->38 0x0000562edeeecf35 _start (mlir-opt+0x1f7ff35)
looking into this
Folks, I'm sorry, but given how much we're currently struggling with review bandwidth in MLIR, I'll be deprioritising fuzzer-generated issues unless there's a clear motivation or practical use case.
In general, while fuzzing can help uncover genuine bugs, it also draws our attention towards corner cases that may never arise in practice, and away from real use cases.
@StarOne01, I appreciate your enthusiasm and contributions! However, I'd encourage you to focus more on helping us identify the right design before proposing fixes. Specifically, in this case:
- Does unrolling such contractions actually make sense?
If yes, please implement it. If not, please explain why in #232203.
While I appreciate that one issue and one PR might seem like a relatively small amount of noise, we should be clear about our expectations. Every issue and PR requires some maintainer attention, and that adds up quickly.
Thanks for understanding!
got it @banach-space 🫡, I'll try to settle the design question before a patch from now.
i think unrolling a full contraction does make sense.
i initially thought this would affect a small set of operations but now i realize anything that goes through a full contraction ends up here. UnrollMultiReductionPattern already does exactly this for scalar results (added in april #188633) before which it was bailing out.
will update the PR accordingly.