Issue 185248
Summary [MLIR] Crash in DimOfForallOp rewrite when source type is not a tensor
Labels mlir
Assignees
Reporter jiayiw2008
    Hi, I found a crash bug when lowering a gpu program. Here's a minimal code example to reproduce it:

Git version: 47766d7f8c397da857d9f78db36c71e8a2ca1fbf

```llvm
module {
  func.func @removeCycles(%arg0: memref<?xi32>, %arg1: i32) {
    %c0 = arith.constant 0 : index
    %0 = gpu.thread_id  x
    %1 = gpu.block_id  x
    %2 = gpu.block_dim  x
    %memref_shape = shape.shape_of %arg0 : memref<?xi32> -> tensor<1xindex>
    %dim_size = tensor.extract %memref_shape[%c0] : tensor<1xindex>
    %cond = arith.cmpi slt, %dim_size, %1 : index
 cf.cond_br %cond, ^bb1, ^bb2
  ^bb1:
    %c10 = arith.constant 10 : i32
 memref.store %c10, %arg0[%1] : memref<?xi32>
    cf.br ^bb3
  ^bb2:
 %c20 = arith.constant 20 : i32
    memref.store %c20, %arg0[%0] : memref<?xi32>
    cf.br ^bb3
  ^bb3:
    scf.for %i = %c0 to %2 step %c0 {
      %sum = arith.addi %0, %1 : index
      %conv = arith.index_cast %sum : index to i32
      memref.store %conv, %arg0[%i] : memref<?xi32>
 }
    gpu.barrier
    return
  }
}
```

#### Stack Trace
```bash
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 segmentationTree.mlir -lower-affine -gpu-lower-to-nvvm-pipeline
 #0 0x0000566c2e4a3bed llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) /llvm/lib/Support/Unix/Signals.inc:880:11
 #1 0x0000566c2e4a411b PrintStackTraceSignalHandler(void*) /llvm/lib/Support/Unix/Signals.inc:962:1
 #2 0x0000566c2e4a1fb4 llvm::sys::RunSignalHandlers() /llvm/lib/Support/Signals.cpp:108:5
 #3 0x0000566c2e4a48b9 SignalHandler(int, siginfo_t*, void*) /llvm/lib/Support/Unix/Signals.inc:448:38
 #4 0x000078b277245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #5 0x000078b27729eb2c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #6 0x000078b27729eb2c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #7 0x000078b27729eb2c pthread_kill ./nptl/pthread_kill.c:89:10
 #8 0x000078b27724527e raise ./signal/../sysdeps/posix/raise.c:27:6
 #9 0x000078b2772288ff abort ./stdlib/abort.c:81:7
#10 0x000078b27722881b _nl_load_domain ./intl/loadmsgcat.c:1177:9
#11 0x000078b27723b517 (/lib/x86_64-linux-gnu/libc.so.6+0x3b517)
#12 0x0000566c2f3f7924 decltype(auto) llvm::cast<mlir::detail::TypedValue<mlir::TensorType>, mlir::Value>(mlir::Value const&) /llvm/include/llvm/Support/Casting.h:561:43
#13 0x0000566c2fad75c6 mlir::tensor::DimOp::getSource() /build/tools/mlir/include/mlir/Dialect/Tensor/IR/TensorOps.h.inc:1791:12
#14 0x0000566c3c5559dd (anonymous namespace)::DimOfForallOp::matchAndRewrite(mlir::tensor::DimOp, mlir::PatternRewriter&) const /mlir/lib/Dialect/SCF/IR/SCF.cpp:1404:27
#15 0x0000566c2fad741b mlir::detail::OpOrInterfaceRewritePatternBase<mlir::tensor::DimOp>::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&) const /mlir/include/mlir/IR/PatternMatch.h:299:12
#16 0x0000566c3ccff738 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&)>)::$_0::operator()() const /mlir/lib/Rewrite/PatternApplicator.cpp:223:31
#17 0x0000566c3ccff3e5 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&)>)::$_0>(long) /llvm/include/llvm/ADT/STLFunctionalExtras.h:46:5
#18 0x0000566c2e4c9fb9 llvm::function_ref<void ()>::operator()() const /llvm/include/llvm/ADT/STLFunctionalExtras.h:69:5
#19 0x0000566c3cd00f3b void mlir::MLIRContext::executeAction<mlir::ApplyPatternAction, mlir::Pattern const&>(llvm::function_ref<void ()>, llvm::ArrayRef<mlir::IRUnit>, mlir::Pattern const&) /mlir/include/mlir/IR/MLIRContext.h:291:3
#20 0x0000566c3ccfd7ca 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/lib/Rewrite/PatternApplicator.cpp:242:9
#21 0x0000566c3ccb5be5 (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() /mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp:619:17
#22 0x0000566c3ccb4ef7 (anonymous namespace)::RegionPatternRewriteDriver::simplify(bool*) &&::$_2::operator()() const /mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp:889:31
#23 0x0000566c3ccb4e65 void llvm::function_ref<void ()>::callback_fn<(anonymous namespace)::RegionPatternRewriteDriver::simplify(bool*) &&::$_2>(long) /llvm/include/llvm/ADT/STLFunctionalExtras.h:46:5
#24 0x0000566c2e4c9fb9 llvm::function_ref<void ()>::operator()() const /llvm/include/llvm/ADT/STLFunctionalExtras.h:69:5
#25 0x0000566c3ccb455b void mlir::MLIRContext::executeAction<(anonymous namespace)::GreedyPatternRewriteIteration, long&>(llvm::function_ref<void ()>, llvm::ArrayRef<mlir::IRUnit>, long&) /mlir/include/mlir/IR/MLIRContext.h:291:3
#26 0x0000566c3ccb272e (anonymous namespace)::RegionPatternRewriteDriver::simplify(bool*) && /mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp:902:3
#27 0x0000566c3ccb2324 mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) /mlir/lib/Transforms/Utils/GreedyPatternRewriteDriver.cpp:934:47
#28 0x0000566c2e69bd00 mlir::applyPatternsGreedily(mlir::Operation*, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) /mlir/include/mlir/Transforms/GreedyPatternRewriteDriver.h:212:15
#29 0x0000566c3cbd0c82 (anonymous namespace)::Canonicalizer::runOnOperation() /mlir/lib/Transforms/Canonicalizer.cpp:63:9
#30 0x0000566c3cdc11d4 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int)::$_3::operator()() const /mlir/lib/Pass/Pass.cpp:0:19
#31 0x0000566c3cdc1175 void llvm::function_ref<void ()>::callback_fn<mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int)::$_3>(long) /llvm/include/llvm/ADT/STLFunctionalExtras.h:46:5
#32 0x0000566c2e4c9fb9 llvm::function_ref<void ()>::operator()() const /llvm/include/llvm/ADT/STLFunctionalExtras.h:69:5
#33 0x0000566c3cdc467b void mlir::MLIRContext::executeAction<mlir::PassExecutionAction, mlir::Pass&>(llvm::function_ref<void ()>, llvm::ArrayRef<mlir::IRUnit>, mlir::Pass&) /mlir/include/mlir/IR/MLIRContext.h:291:3
#34 0x0000566c3cdb861e mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) /mlir/lib/Pass/Pass.cpp:619:17
#35 0x0000566c3cdb8d3a mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) /mlir/lib/Pass/Pass.cpp:688:16
#36 0x0000566c3cdbe61d mlir::PassManager::runPasses(mlir::Operation*, mlir::AnalysisManager) /mlir/lib/Pass/Pass.cpp:1123:10
#37 0x0000566c3cdbdba6 mlir::PassManager::run(mlir::Operation*) /mlir/lib/Pass/Pass.cpp:1097:60
#38 0x0000566c2e5aeb9b performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) /mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:596:17
#39 0x0000566c2e5ae271 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*) /mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:681:12
#40 0x0000566c2e5adf2c mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&)::$_0::operator()(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef, llvm::raw_ostream&) const /mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:779:12
#41 0x0000566c2e5ade36 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&) /llvm/include/llvm/ADT/STLFunctionalExtras.h:46:12
#42 0x0000566c3d3299da llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&)>::operator()(std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, llvm::MemoryBufferRef const&, llvm::raw_ostream&) const /llvm/include/llvm/ADT/STLFunctionalExtras.h:69:12
#43 0x0000566c3d328d9a 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/lib/Support/ToolUtilities.cpp:30:12
#44 0x0000566c2e5a9ae6 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) /mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:784:26
#45 0x0000566c2e5a9ee8 mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) /mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:830:14
#46 0x0000566c2e5aa0b8 mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) /mlir/lib/Tools/mlir-opt/MlirOptMain.cpp:846:10
#47 0x0000566c2e4787d5 main /mlir/tools/mlir-opt/mlir-opt.cpp:343:33
#48 0x000078b27722a1ca __libc_start_call_main ./csu/../sysdeps/nptl/libc_start_call_main.h:74:3
#49 0x000078b27722a28b call_init ./csu/../csu/libc-start.c:128:20
#50 0x000078b27722a28b __libc_start_main ./csu/../csu/libc-start.c:347:5
#51 0x0000566c2e478645 _start (./mlir-opt+0x47e6645)
Aborted
```

#### Reproduction Command
```bash
mlir-opt segmentationTree.mlir -lower-affine -gpu-lower-to-nvvm-pipeline
```
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs

Reply via email to