llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

[MLIR][Affine] `AffineLoadOp::fold` crashes on zero-sized constant memref during canonicalization

Open frida-albrecht opened this issue 2 months ago • 3 comments

Description

mlir-opt crashes with an assertion failure when running -canonicalize on an affine.load that reads from a constant global memref<0xi8>. The crash occurs in AffineLoadOp::fold, which attempts to constant-fold the load and eventually indexes into a DenseElementsAttr representing a zero-sized shaped type.

Tested commit

3b8ee89

Test case

module {
  memref.global "private" constant @__constant_0xi8 : memref<0xi8, #spirv.storage_class<CrossWorkgroup>> = dense<>
  func.func private @main(%arg1: tensor<1xi32>, %arg2: tensor<1xi8>, %arg3: tensor<1xi16>, %arg4: tensor<1xi16>) -> tensor<1xi32> attributes {llvm.arm_locally_streaming, llvm.arm_out_za, llvm.arm_streaming, llvm.emit_c_interface} {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %0 = llvm.mlir.constant(0 : i32) : i32
    %1 = llvm.mlir.constant(-1 : i32) : i32
    %2 = bufferization.to_buffer %arg1 : tensor<1xi32> to memref<1xi32, strided<[?], offset: ?>, #spirv.storage_class<CrossWorkgroup>>
    %3 = bufferization.to_buffer %arg1 : tensor<1xi32> to memref<1xi32, strided<[?], offset: ?>, #spirv.storage_class<CrossWorkgroup>>
    %4 = memref.get_global @__constant_0xi8 : memref<0xi8, #spirv.storage_class<CrossWorkgroup>>
    %collapse_shape = memref.collapse_shape %3 [] : memref<1xi32, strided<[?], offset: ?>, #spirv.storage_class<CrossWorkgroup>> into memref<i32, strided<[], offset: ?>, #spirv.storage_class<CrossWorkgroup>>
    %alloc = memref.alloc() {} : memref<1xi32, #spirv.storage_class<CrossWorkgroup>>
    gpu.launch blocks(%arg5, %arg6, %arg7) in (%arg11 = %c1, %arg12 = %c1, %arg13 = %c1) threads(%arg8, %arg9, %arg10) in (%arg14 = %c1, %arg15 = %c1, %arg16 = %c1) {
      %6 = affine.load %2[%arg5] : memref<1xi32, strided<[?], offset: ?>, #spirv.storage_class<CrossWorkgroup>>
      %7 = affine.load %collapse_shape[] : memref<i32, strided<[], offset: ?>, #spirv.storage_class<CrossWorkgroup>>
      %8 = affine.load %4[%arg5] : memref<0xi8, #spirv.storage_class<CrossWorkgroup>>
      %9 = tosa.apply_scale %6, %7, %8 {rounding_mode = SINGLE_ROUND} : (i32, i32, i8) -> i32
      %10 = llvm.intr.smax(%9, %0) : (i32, i32) -> i32
      %11 = llvm.intr.smin(%10, %1) : (i32, i32) -> i32
      affine.store %11, %alloc[%arg5] : memref<1xi32, #spirv.storage_class<CrossWorkgroup>>
      affine.store %11, %alloc[%arg5] : memref<1xi32, #spirv.storage_class<CrossWorkgroup>>
      affine.store %11, %alloc[%arg5] : memref<1xi32, #spirv.storage_class<CrossWorkgroup>>
      gpu.terminator
    }
    %5 = bufferization.to_tensor %alloc : memref<1xi32, #spirv.storage_class<CrossWorkgroup>> to tensor<1xi32>
    return %5 : tensor<1xi32>
  }
}

Steps to reproduce

mlir-opt test.mlir -canonicalize

stack trace

mlir-opt: /home/workdir/llvm-project-latest/mlir/lib/IR/BuiltinAttributeInterfaces.cpp:58: static uint64_t mlir::ElementsAttr::getFlattenedIndex(Type, ArrayRef<uint64_t>): Assertion `isValidIndex(shapeType, index) && "expected valid multi-dimensional index"' 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.mlir -canonicalize
 #0 0x00005562fa3a5d0b llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (./mlir-opt+0x1fc2d0b)
 #1 0x00005562fa3a2b11 llvm::sys::RunSignalHandlers() (./mlir-opt+0x1fbfb11)
 #2 0x00005562fa3a6a3b SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x00007f378678f520 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #4 0x00007f37867e39fc pthread_kill (/usr/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #5 0x00007f378678f476 gsignal (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #6 0x00007f37867757f3 abort (/usr/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #7 0x00007f378677571b (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #8 0x00007f3786786e96 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #9 0x0000556302805bd1 mlir::ElementsAttr::getFlattenedIndex(mlir::Type, llvm::ArrayRef<unsigned long>) (./mlir-opt+0xa422bd1)
#10 0x000055630211cc3b mlir::detail::ElementsAttrRange<mlir::DenseElementsAttr::AttributeElementIterator>::operator[](llvm::ArrayRef<unsigned long>) const (./mlir-opt+0x9d39c3b)
#11 0x000055630211c9a7 mlir::affine::AffineLoadOp::fold(mlir::affine::AffineLoadOpGenericAdaptor<llvm::ArrayRef<mlir::Attribute>>) (./mlir-opt+0x9d399a7)
#12 0x00005563021753f1 llvm::LogicalResult mlir::Op<mlir::affine::AffineLoadOp, mlir::OpTrait::ZeroRegions, mlir::OpTrait::OneResult, mlir::OpTrait::OneTypedResult<mlir::Type>::Impl, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::AtLeastNOperands<1u>::Impl, mlir::OpTrait::OpInvariants, mlir::BytecodeOpInterface::Trait, mlir::affine::AffineReadOpInterface::Trait, mlir::affine::AffineMapAccessInterface::Trait, mlir::AlignmentAttrOpInterface::Trait, mlir::OpTrait::MemRefsNormalizable, mlir::MemoryEffectOpInterface::Trait>::foldSingleResultHook<mlir::affine::AffineLoadOp>(mlir::Operation*, llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (./mlir-opt+0x9d923f1)
#13 0x00005563028be56b mlir::Operation::fold(llvm::ArrayRef<mlir::Attribute>, llvm::SmallVectorImpl<mlir::OpFoldResult>&) (./mlir-opt+0xa4db56b)
#14 0x00005563028be8e6 mlir::Operation::fold(llvm::SmallVectorImpl<mlir::OpFoldResult>&) (./mlir-opt+0xa4db8e6)
#15 0x000055630251e1ac (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#16 0x000055630251d890 void llvm::function_ref<void ()>::callback_fn<(anonymous namespace)::RegionPatternRewriteDriver::simplify(bool*) &&::$_2>(long) GreedyPatternRewriteDriver.cpp:0:0
#17 0x000055630251a8b5 mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (./mlir-opt+0xa1378b5)
#18 0x000055630249c3a5 (anonymous namespace)::Canonicalizer::runOnOperation() Canonicalizer.cpp:0:0
#19 0x00005563025a53ee mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (./mlir-opt+0xa1c23ee)
#20 0x00005563025a6364 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (./mlir-opt+0xa1c3364)
#21 0x00005563025ad0fa mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (./mlir-opt+0xa1ca0fa)
#22 0x00005563025aca5d mlir::PassManager::run(mlir::Operation*) (./mlir-opt+0xa1c9a5d)
#23 0x00005562fa45158f performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#24 0x00005562fa450761 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&)::$_0>(long, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) MlirOptMain.cpp:0:0
#25 0x00005563028ffec6 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+0xa51cec6)
#26 0x00005562fa446ba6 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (./mlir-opt+0x2063ba6)
#27 0x00005562fa446f7a mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (./mlir-opt+0x2063f7a)
#28 0x00005562fa4471eb mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (./mlir-opt+0x20641eb)
#29 0x00005562fa38b7cd main (./mlir-opt+0x1fa87cd)
#30 0x00007f3786776d90 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#31 0x00007f3786776e40 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#32 0x00005562fa38b325 _start (./mlir-opt+0x1fa8325)
Aborted (core dumped)

frida-albrecht avatar Sep 16 '26 08:09 frida-albrecht

@llvm/issue-subscribers-mlir

Author: frida-albrecht

### Description

mlir-opt crashes with an assertion failure when running -canonicalize on an affine.load that reads from a constant global memref<0xi8>. The crash occurs in AffineLoadOp::fold, which attempts to constant-fold the load and eventually indexes into a DenseElementsAttr representing a zero-sized shaped type.

Tested commit

3b8ee89

Test case

module {
  memref.global "private" constant @<!-- -->__constant_0xi8 : memref&lt;0xi8, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt; = dense&lt;&gt;
  func.func private @<!-- -->main(%arg1: tensor&lt;1xi32&gt;, %arg2: tensor&lt;1xi8&gt;, %arg3: tensor&lt;1xi16&gt;, %arg4: tensor&lt;1xi16&gt;) -&gt; tensor&lt;1xi32&gt; attributes {llvm.arm_locally_streaming, llvm.arm_out_za, llvm.arm_streaming, llvm.emit_c_interface} {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %0 = llvm.mlir.constant(0 : i32) : i32
    %1 = llvm.mlir.constant(-1 : i32) : i32
    %2 = bufferization.to_buffer %arg1 : tensor&lt;1xi32&gt; to memref&lt;1xi32, strided&lt;[?], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    %3 = bufferization.to_buffer %arg1 : tensor&lt;1xi32&gt; to memref&lt;1xi32, strided&lt;[?], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    %4 = memref.get_global @<!-- -->__constant_0xi8 : memref&lt;0xi8, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    %collapse_shape = memref.collapse_shape %3 [] : memref&lt;1xi32, strided&lt;[?], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt; into memref&lt;i32, strided&lt;[], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    %alloc = memref.alloc() {} : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    gpu.launch blocks(%arg5, %arg6, %arg7) in (%arg11 = %c1, %arg12 = %c1, %arg13 = %c1) threads(%arg8, %arg9, %arg10) in (%arg14 = %c1, %arg15 = %c1, %arg16 = %c1) {
      %6 = affine.load %2[%arg5] : memref&lt;1xi32, strided&lt;[?], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      %7 = affine.load %collapse_shape[] : memref&lt;i32, strided&lt;[], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      %8 = affine.load %4[%arg5] : memref&lt;0xi8, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      %9 = tosa.apply_scale %6, %7, %8 {rounding_mode = SINGLE_ROUND} : (i32, i32, i8) -&gt; i32
      %10 = llvm.intr.smax(%9, %0) : (i32, i32) -&gt; i32
      %11 = llvm.intr.smin(%10, %1) : (i32, i32) -&gt; i32
      affine.store %11, %alloc[%arg5] : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      affine.store %11, %alloc[%arg5] : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      affine.store %11, %alloc[%arg5] : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      gpu.terminator
    }
    %5 = bufferization.to_tensor %alloc : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt; to tensor&lt;1xi32&gt;
    return %5 : tensor&lt;1xi32&gt;
  }
}

Steps to reproduce

mlir-opt test.mlir -canonicalize

stack trace

mlir-opt: /home/workdir/llvm-project-latest/mlir/lib/IR/BuiltinAttributeInterfaces.cpp:58: static uint64_t mlir::ElementsAttr::getFlattenedIndex(Type, ArrayRef&lt;uint64_t&gt;): Assertion `isValidIndex(shapeType, index) &amp;&amp; "expected valid multi-dimensional index"' 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.mlir -canonicalize
 #<!-- -->0 0x00005562fa3a5d0b llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (./mlir-opt+0x1fc2d0b)
 #<!-- -->1 0x00005562fa3a2b11 llvm::sys::RunSignalHandlers() (./mlir-opt+0x1fbfb11)
 #<!-- -->2 0x00005562fa3a6a3b SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #<!-- -->3 0x00007f378678f520 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #<!-- -->4 0x00007f37867e39fc pthread_kill (/usr/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #<!-- -->5 0x00007f378678f476 gsignal (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #<!-- -->6 0x00007f37867757f3 abort (/usr/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #<!-- -->7 0x00007f378677571b (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #<!-- -->8 0x00007f3786786e96 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #<!-- -->9 0x0000556302805bd1 mlir::ElementsAttr::getFlattenedIndex(mlir::Type, llvm::ArrayRef&lt;unsigned long&gt;) (./mlir-opt+0xa422bd1)
#<!-- -->10 0x000055630211cc3b mlir::detail::ElementsAttrRange&lt;mlir::DenseElementsAttr::AttributeElementIterator&gt;::operator[](llvm::ArrayRef&lt;unsigned long&gt;) const (./mlir-opt+0x9d39c3b)
#<!-- -->11 0x000055630211c9a7 mlir::affine::AffineLoadOp::fold(mlir::affine::AffineLoadOpGenericAdaptor&lt;llvm::ArrayRef&lt;mlir::Attribute&gt;&gt;) (./mlir-opt+0x9d399a7)
#<!-- -->12 0x00005563021753f1 llvm::LogicalResult mlir::Op&lt;mlir::affine::AffineLoadOp, mlir::OpTrait::ZeroRegions, mlir::OpTrait::OneResult, mlir::OpTrait::OneTypedResult&lt;mlir::Type&gt;::Impl, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::AtLeastNOperands&lt;1u&gt;::Impl, mlir::OpTrait::OpInvariants, mlir::BytecodeOpInterface::Trait, mlir::affine::AffineReadOpInterface::Trait, mlir::affine::AffineMapAccessInterface::Trait, mlir::AlignmentAttrOpInterface::Trait, mlir::OpTrait::MemRefsNormalizable, mlir::MemoryEffectOpInterface::Trait&gt;::foldSingleResultHook&lt;mlir::affine::AffineLoadOp&gt;(mlir::Operation*, llvm::ArrayRef&lt;mlir::Attribute&gt;, llvm::SmallVectorImpl&lt;mlir::OpFoldResult&gt;&amp;) (./mlir-opt+0x9d923f1)
#<!-- -->13 0x00005563028be56b mlir::Operation::fold(llvm::ArrayRef&lt;mlir::Attribute&gt;, llvm::SmallVectorImpl&lt;mlir::OpFoldResult&gt;&amp;) (./mlir-opt+0xa4db56b)
#<!-- -->14 0x00005563028be8e6 mlir::Operation::fold(llvm::SmallVectorImpl&lt;mlir::OpFoldResult&gt;&amp;) (./mlir-opt+0xa4db8e6)
#<!-- -->15 0x000055630251e1ac (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->16 0x000055630251d890 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;(anonymous namespace)::RegionPatternRewriteDriver::simplify(bool*) &amp;&amp;::$_2&gt;(long) GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->17 0x000055630251a8b5 mlir::applyPatternsGreedily(mlir::Region&amp;, mlir::FrozenRewritePatternSet const&amp;, mlir::GreedyRewriteConfig, bool*) (./mlir-opt+0xa1378b5)
#<!-- -->18 0x000055630249c3a5 (anonymous namespace)::Canonicalizer::runOnOperation() Canonicalizer.cpp:0:0
#<!-- -->19 0x00005563025a53ee mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (./mlir-opt+0xa1c23ee)
#<!-- -->20 0x00005563025a6364 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&amp;, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (./mlir-opt+0xa1c3364)
#<!-- -->21 0x00005563025ad0fa mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (./mlir-opt+0xa1ca0fa)
#<!-- -->22 0x00005563025aca5d mlir::PassManager::run(mlir::Operation*) (./mlir-opt+0xa1c9a5d)
#<!-- -->23 0x00005562fa45158f performActions(llvm::raw_ostream&amp;, std::shared_ptr&lt;llvm::SourceMgr&gt; const&amp;, mlir::MLIRContext*, mlir::MlirOptMainConfig const&amp;) MlirOptMain.cpp:0:0
#<!-- -->24 0x00005562fa450761 llvm::LogicalResult llvm::function_ref&lt;llvm::LogicalResult (std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, llvm::MemoryBufferRef const&amp;, llvm::raw_ostream&amp;)&gt;::callback_fn&lt;mlir::MlirOptMain(llvm::raw_ostream&amp;, std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, mlir::DialectRegistry&amp;, mlir::MlirOptMainConfig const&amp;)::$_0&gt;(long, std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, llvm::MemoryBufferRef const&amp;, llvm::raw_ostream&amp;) MlirOptMain.cpp:0:0
#<!-- -->25 0x00005563028ffec6 mlir::splitAndProcessBuffer(std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, llvm::function_ref&lt;llvm::LogicalResult (std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, llvm::MemoryBufferRef const&amp;, llvm::raw_ostream&amp;)&gt;, llvm::raw_ostream&amp;, llvm::StringRef, llvm::StringRef) (./mlir-opt+0xa51cec6)
#<!-- -->26 0x00005562fa446ba6 mlir::MlirOptMain(llvm::raw_ostream&amp;, std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, mlir::DialectRegistry&amp;, mlir::MlirOptMainConfig const&amp;) (./mlir-opt+0x2063ba6)
#<!-- -->27 0x00005562fa446f7a mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&amp;) (./mlir-opt+0x2063f7a)
#<!-- -->28 0x00005562fa4471eb mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&amp;) (./mlir-opt+0x20641eb)
#<!-- -->29 0x00005562fa38b7cd main (./mlir-opt+0x1fa87cd)
#<!-- -->30 0x00007f3786776d90 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#<!-- -->31 0x00007f3786776e40 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#<!-- -->32 0x00005562fa38b325 _start (./mlir-opt+0x1fa8325)
Aborted (core dumped)

I started to work on this, may you please assign it to me

purnima-nlp avatar Sep 16 '26 11:09 purnima-nlp

@llvm/issue-subscribers-mlir-affine

Author: frida-albrecht

### Description

mlir-opt crashes with an assertion failure when running -canonicalize on an affine.load that reads from a constant global memref<0xi8>. The crash occurs in AffineLoadOp::fold, which attempts to constant-fold the load and eventually indexes into a DenseElementsAttr representing a zero-sized shaped type.

Tested commit

3b8ee89

Test case

module {
  memref.global "private" constant @<!-- -->__constant_0xi8 : memref&lt;0xi8, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt; = dense&lt;&gt;
  func.func private @<!-- -->main(%arg1: tensor&lt;1xi32&gt;, %arg2: tensor&lt;1xi8&gt;, %arg3: tensor&lt;1xi16&gt;, %arg4: tensor&lt;1xi16&gt;) -&gt; tensor&lt;1xi32&gt; attributes {llvm.arm_locally_streaming, llvm.arm_out_za, llvm.arm_streaming, llvm.emit_c_interface} {
    %c0 = arith.constant 0 : index
    %c1 = arith.constant 1 : index
    %0 = llvm.mlir.constant(0 : i32) : i32
    %1 = llvm.mlir.constant(-1 : i32) : i32
    %2 = bufferization.to_buffer %arg1 : tensor&lt;1xi32&gt; to memref&lt;1xi32, strided&lt;[?], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    %3 = bufferization.to_buffer %arg1 : tensor&lt;1xi32&gt; to memref&lt;1xi32, strided&lt;[?], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    %4 = memref.get_global @<!-- -->__constant_0xi8 : memref&lt;0xi8, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    %collapse_shape = memref.collapse_shape %3 [] : memref&lt;1xi32, strided&lt;[?], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt; into memref&lt;i32, strided&lt;[], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    %alloc = memref.alloc() {} : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
    gpu.launch blocks(%arg5, %arg6, %arg7) in (%arg11 = %c1, %arg12 = %c1, %arg13 = %c1) threads(%arg8, %arg9, %arg10) in (%arg14 = %c1, %arg15 = %c1, %arg16 = %c1) {
      %6 = affine.load %2[%arg5] : memref&lt;1xi32, strided&lt;[?], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      %7 = affine.load %collapse_shape[] : memref&lt;i32, strided&lt;[], offset: ?&gt;, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      %8 = affine.load %4[%arg5] : memref&lt;0xi8, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      %9 = tosa.apply_scale %6, %7, %8 {rounding_mode = SINGLE_ROUND} : (i32, i32, i8) -&gt; i32
      %10 = llvm.intr.smax(%9, %0) : (i32, i32) -&gt; i32
      %11 = llvm.intr.smin(%10, %1) : (i32, i32) -&gt; i32
      affine.store %11, %alloc[%arg5] : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      affine.store %11, %alloc[%arg5] : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      affine.store %11, %alloc[%arg5] : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt;
      gpu.terminator
    }
    %5 = bufferization.to_tensor %alloc : memref&lt;1xi32, #spirv.storage_class&lt;CrossWorkgroup&gt;&gt; to tensor&lt;1xi32&gt;
    return %5 : tensor&lt;1xi32&gt;
  }
}

Steps to reproduce

mlir-opt test.mlir -canonicalize

stack trace

mlir-opt: /home/workdir/llvm-project-latest/mlir/lib/IR/BuiltinAttributeInterfaces.cpp:58: static uint64_t mlir::ElementsAttr::getFlattenedIndex(Type, ArrayRef&lt;uint64_t&gt;): Assertion `isValidIndex(shapeType, index) &amp;&amp; "expected valid multi-dimensional index"' 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.mlir -canonicalize
 #<!-- -->0 0x00005562fa3a5d0b llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (./mlir-opt+0x1fc2d0b)
 #<!-- -->1 0x00005562fa3a2b11 llvm::sys::RunSignalHandlers() (./mlir-opt+0x1fbfb11)
 #<!-- -->2 0x00005562fa3a6a3b SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #<!-- -->3 0x00007f378678f520 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42520)
 #<!-- -->4 0x00007f37867e39fc pthread_kill (/usr/lib/x86_64-linux-gnu/libc.so.6+0x969fc)
 #<!-- -->5 0x00007f378678f476 gsignal (/usr/lib/x86_64-linux-gnu/libc.so.6+0x42476)
 #<!-- -->6 0x00007f37867757f3 abort (/usr/lib/x86_64-linux-gnu/libc.so.6+0x287f3)
 #<!-- -->7 0x00007f378677571b (/usr/lib/x86_64-linux-gnu/libc.so.6+0x2871b)
 #<!-- -->8 0x00007f3786786e96 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x39e96)
 #<!-- -->9 0x0000556302805bd1 mlir::ElementsAttr::getFlattenedIndex(mlir::Type, llvm::ArrayRef&lt;unsigned long&gt;) (./mlir-opt+0xa422bd1)
#<!-- -->10 0x000055630211cc3b mlir::detail::ElementsAttrRange&lt;mlir::DenseElementsAttr::AttributeElementIterator&gt;::operator[](llvm::ArrayRef&lt;unsigned long&gt;) const (./mlir-opt+0x9d39c3b)
#<!-- -->11 0x000055630211c9a7 mlir::affine::AffineLoadOp::fold(mlir::affine::AffineLoadOpGenericAdaptor&lt;llvm::ArrayRef&lt;mlir::Attribute&gt;&gt;) (./mlir-opt+0x9d399a7)
#<!-- -->12 0x00005563021753f1 llvm::LogicalResult mlir::Op&lt;mlir::affine::AffineLoadOp, mlir::OpTrait::ZeroRegions, mlir::OpTrait::OneResult, mlir::OpTrait::OneTypedResult&lt;mlir::Type&gt;::Impl, mlir::OpTrait::ZeroSuccessors, mlir::OpTrait::AtLeastNOperands&lt;1u&gt;::Impl, mlir::OpTrait::OpInvariants, mlir::BytecodeOpInterface::Trait, mlir::affine::AffineReadOpInterface::Trait, mlir::affine::AffineMapAccessInterface::Trait, mlir::AlignmentAttrOpInterface::Trait, mlir::OpTrait::MemRefsNormalizable, mlir::MemoryEffectOpInterface::Trait&gt;::foldSingleResultHook&lt;mlir::affine::AffineLoadOp&gt;(mlir::Operation*, llvm::ArrayRef&lt;mlir::Attribute&gt;, llvm::SmallVectorImpl&lt;mlir::OpFoldResult&gt;&amp;) (./mlir-opt+0x9d923f1)
#<!-- -->13 0x00005563028be56b mlir::Operation::fold(llvm::ArrayRef&lt;mlir::Attribute&gt;, llvm::SmallVectorImpl&lt;mlir::OpFoldResult&gt;&amp;) (./mlir-opt+0xa4db56b)
#<!-- -->14 0x00005563028be8e6 mlir::Operation::fold(llvm::SmallVectorImpl&lt;mlir::OpFoldResult&gt;&amp;) (./mlir-opt+0xa4db8e6)
#<!-- -->15 0x000055630251e1ac (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->16 0x000055630251d890 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;(anonymous namespace)::RegionPatternRewriteDriver::simplify(bool*) &amp;&amp;::$_2&gt;(long) GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->17 0x000055630251a8b5 mlir::applyPatternsGreedily(mlir::Region&amp;, mlir::FrozenRewritePatternSet const&amp;, mlir::GreedyRewriteConfig, bool*) (./mlir-opt+0xa1378b5)
#<!-- -->18 0x000055630249c3a5 (anonymous namespace)::Canonicalizer::runOnOperation() Canonicalizer.cpp:0:0
#<!-- -->19 0x00005563025a53ee mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (./mlir-opt+0xa1c23ee)
#<!-- -->20 0x00005563025a6364 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&amp;, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (./mlir-opt+0xa1c3364)
#<!-- -->21 0x00005563025ad0fa mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) (./mlir-opt+0xa1ca0fa)
#<!-- -->22 0x00005563025aca5d mlir::PassManager::run(mlir::Operation*) (./mlir-opt+0xa1c9a5d)
#<!-- -->23 0x00005562fa45158f performActions(llvm::raw_ostream&amp;, std::shared_ptr&lt;llvm::SourceMgr&gt; const&amp;, mlir::MLIRContext*, mlir::MlirOptMainConfig const&amp;) MlirOptMain.cpp:0:0
#<!-- -->24 0x00005562fa450761 llvm::LogicalResult llvm::function_ref&lt;llvm::LogicalResult (std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, llvm::MemoryBufferRef const&amp;, llvm::raw_ostream&amp;)&gt;::callback_fn&lt;mlir::MlirOptMain(llvm::raw_ostream&amp;, std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, mlir::DialectRegistry&amp;, mlir::MlirOptMainConfig const&amp;)::$_0&gt;(long, std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, llvm::MemoryBufferRef const&amp;, llvm::raw_ostream&amp;) MlirOptMain.cpp:0:0
#<!-- -->25 0x00005563028ffec6 mlir::splitAndProcessBuffer(std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, llvm::function_ref&lt;llvm::LogicalResult (std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, llvm::MemoryBufferRef const&amp;, llvm::raw_ostream&amp;)&gt;, llvm::raw_ostream&amp;, llvm::StringRef, llvm::StringRef) (./mlir-opt+0xa51cec6)
#<!-- -->26 0x00005562fa446ba6 mlir::MlirOptMain(llvm::raw_ostream&amp;, std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, mlir::DialectRegistry&amp;, mlir::MlirOptMainConfig const&amp;) (./mlir-opt+0x2063ba6)
#<!-- -->27 0x00005562fa446f7a mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&amp;) (./mlir-opt+0x2063f7a)
#<!-- -->28 0x00005562fa4471eb mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&amp;) (./mlir-opt+0x20641eb)
#<!-- -->29 0x00005562fa38b7cd main (./mlir-opt+0x1fa87cd)
#<!-- -->30 0x00007f3786776d90 (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29d90)
#<!-- -->31 0x00007f3786776e40 __libc_start_main (/usr/lib/x86_64-linux-gnu/libc.so.6+0x29e40)
#<!-- -->32 0x00005562fa38b325 _start (./mlir-opt+0x1fa8325)
Aborted (core dumped)