Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Mlir] --linalg-specialize-generic-ops crashes in Casting.h:566 #122094

Open
Emilyaxe opened this issue Jan 8, 2025 · 7 comments · May be fixed by #122449
Open

[Mlir] --linalg-specialize-generic-ops crashes in Casting.h:566 #122094

Emilyaxe opened this issue Jan 8, 2025 · 7 comments · May be fixed by #122449
Assignees
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] mlir:linalg

Comments

@Emilyaxe
Copy link

Emilyaxe commented Jan 8, 2025

git version: e4372c4

system: Ubuntu 18.04.6 LTS

reproduce with: mlir-opt a.mlir --linalg-specialize-generic-ops

a.mlir:

#map = affine_map<(d0, d1, d2) -> (0, d1, 0)>
#map1 = affine_map<(d0, d1, d2) -> (d0, d1, d2)>
#map2 = affine_map<(d0, d1) -> (d0)>
#map3 = affine_map<(d0, d1) -> (d1)>
#map4 = affine_map<(d0, d1) -> ()>
#map5 = affine_map<(d0, d1) -> (d0, d1)>
#map6 = affine_map<(d0, d1, d2) -> (0, d1, d2)>
#map7 = affine_map<(d0, d1, d2) -> (0, 0, 0)>
#map8 = affine_map<(d0, d1, d2) -> (d0, d2)>
#map9 = affine_map<(d0, d1, d2) -> (d2, d1)>
#map10 = affine_map<(d0, d1, d2) -> (d0, d1)>
module {
  func.func private @printMemrefI32(tensor<*xi32>)
  func.func private @printMemrefF32(tensor<*xf32>)
  func.func private @func1(%arg0: index, %arg1: tensor<1x4x26xi32>, %arg2: tensor<1x4x26xi32>) -> ( tensor<1x4x4xi32>) {
    %true = index.bool.constant true
    %cst = arith.constant dense<1> : tensor<1x4x1xi32>
    %cst_0 = arith.constant dense<0> : tensor<1x4x1xi32>
    %cst_1 = arith.constant dense<1> : tensor<1x4x4xi32>
    %cst_2 = arith.constant dense<6> : tensor<1x4x4xi32>
    %cst_3 = arith.constant dense<0> : tensor<1x4x4xi32>
    %c0_i32 = arith.constant 0 : i32
    %cst_4 = arith.constant dense<1.000000e+00> : tensor<1x4x1xf32>
    %cst_5 = arith.constant dense<0.000000e+00> : tensor<1x4x1xf32>
    %cst_6 = arith.constant dense<9968> : tensor<1x4x26xi32>
    %0 = tensor.empty() : tensor<1x4xi32>
    %c2147483647_i32 = arith.constant 2147483647 : i32
    %1 = linalg.fill ins(%c2147483647_i32 : i32) outs(%0 : tensor<1x4xi32>) -> tensor<1x4xi32>
    %reduced = linalg.reduce ins(%cst_6 : tensor<1x4x26xi32>) outs(%1 : tensor<1x4xi32>) dimensions = [2] 
      (%in: i32, %init: i32) {
        %51 = arith.minsi %in, %init : i32
        linalg.yield %51 : i32
      }
    %expanded = tensor.expand_shape %reduced [[0], [1, 2]] output_shape [1, 4, 1] : tensor<1x4xi32> into tensor<1x4x1xi32>
    %collapsed = tensor.collapse_shape %expanded [[0, 1, 2]] : tensor<1x4x1xi32> into tensor<4xi32>
    %collapsed_7 = tensor.collapse_shape %expanded [[0, 1, 2]] : tensor<1x4x1xi32> into tensor<4xi32>
    %12 = tensor.empty() : tensor<4x4xi32>
    %13 = linalg.generic {indexing_maps = [#map2, #map3, #map4, #map5], iterator_types = ["parallel", "parallel"]} ins(%collapsed_7, %collapsed, %c0_i32 : tensor<4xi32>, tensor<4xi32>, i32) outs(%12 : tensor<4x4xi32>) {
    ^bb0(%in: i32, %in_11: i32, %in_12: i32, %out: i32):
      %51 = arith.muli %in, %in_11 : i32
      %52 = arith.addi %in_12, %51 : i32
      linalg.yield %52 : i32
    } -> tensor<4x4xi32>
    %expanded_8 = tensor.expand_shape %13 [[0, 1], [2]] output_shape [1, 4, 4] : tensor<4x4xi32> into tensor<1x4x4xi32>
    %14 = scf.while (%arg3 = %cst_3) : (tensor<1x4x4xi32>) -> tensor<1x4x4xi32> {
      %51 = tensor.empty() : tensor<1x4x4xi1>
      %52 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%cst_2, %arg3 : tensor<1x4x4xi32>, tensor<1x4x4xi32>) outs(%51 : tensor<1x4x4xi1>) {
      ^bb0(%in: i32, %in_11: i32, %out: i1):
        %53 = arith.cmpi sgt, %in, %in_11 : i32
        linalg.yield %53 : i1
      } -> tensor<1x4x4xi1>
      %extracted = tensor.extract %52[%arg0, %arg0, %arg0] : tensor<1x4x4xi1>
      %from_elements = tensor.from_elements %extracted : tensor<i1>
      scf.condition(%extracted) %arg3 : tensor<1x4x4xi32>
    } do {
    ^bb0(%arg3: tensor<1x4x4xi32>):
      %51 = tensor.empty() : tensor<1x4x4xi32>
      %52 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%arg3, %cst_1 : tensor<1x4x4xi32>, tensor<1x4x4xi32>) outs(%51 : tensor<1x4x4xi32>) {
      ^bb0(%in: i32, %in_11: i32, %out: i32):
        %53 = arith.addi %in, %in_11 : i32
        linalg.yield %53 : i32
      } -> tensor<1x4x4xi32>
      scf.yield %52 : tensor<1x4x4xi32>
    }
    %45 = tensor.empty() : tensor<1x4x4xi32>
    %46 = linalg.generic {indexing_maps = [#map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%14 : tensor<1x4x4xi32>) outs(%45 : tensor<1x4x4xi32>) {
    ^bb0(%in: i32, %out: i32):
      %c0_i32_11 = arith.constant 0 : i32
      %51 = arith.subi %c0_i32_11, %in : i32
      %52 = arith.maxsi %in, %51 : i32
      linalg.yield %52 : i32
    } -> tensor<1x4x4xi32>
    %47 = tensor.empty() : tensor<1x4x4xi32>
    %48 = linalg.generic {indexing_maps = [#map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%46 : tensor<1x4x4xi32>) outs(%47 : tensor<1x4x4xi32>) {
    ^bb0(%in: i32, %out: i32):
      %c0_i32_11 = arith.constant 0 : i32
      %c16_i32 = arith.constant 16 : i32
      %51 = arith.maxsi %c0_i32_11, %in : i32
      %52 = arith.minsi %c16_i32, %51 : i32
      linalg.yield %52 : i32
    } -> tensor<1x4x4xi32>
    %49 = tensor.empty() : tensor<1x4x4xi32>
    %50 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%expanded_8, %48 : tensor<1x4x4xi32>, tensor<1x4x4xi32>) outs(%49 : tensor<1x4x4xi32>) {
    ^bb0(%in: i32, %in_11: i32, %out: i32):
      %51 = arith.shrui %in, %in_11 : i32
      linalg.yield %51 : i32
    } -> tensor<1x4x4xi32>
    return  %50 :  tensor<1x4x4xi32>
  }
}


stack trace:

 mlir-opt: /data/szy/MLIR/llvm-release/llvm-project/llvm/include/llvm/Support/Casting.h:566: decltype(auto) llvm::cast(const From &) [To = mlir::RankedTensorType, 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.
Stack dump:
0.      Program arguments: /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt --linalg-specialize-generic-ops a.mlir
 #0 0x0000564311abfce8 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11f2ce8)
 #1 0x0000564311abd7fe llvm::sys::RunSignalHandlers() (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11f07fe)
 #2 0x0000564311ac067d SignalHandler(int) Signals.cpp:0:0
 #3 0x00007fd71bc7e420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #4 0x00007fd71b2bb00b raise /build/glibc-LcI20x/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #5 0x00007fd71b29a859 abort /build/glibc-LcI20x/glibc-2.31/stdlib/abort.c:81:7
 #6 0x00007fd71b29a729 get_sysdep_segment_value /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:509:8
 #7 0x00007fd71b29a729 _nl_load_domain /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:970:34
 #8 0x00007fd71b2abfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #9 0x0000564312cb67f5 (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x23e97f5)
#10 0x0000564312cb5120 (anonymous namespace)::DecomposeProjectedPermutation::matchAndRewrite(mlir::linalg::GenericOp, mlir::PatternRewriter&) const DecomposeGenericByUnfoldingPermutation.cpp:0:0
#11 0x0000564317b39d31 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) PatternApplicator.cpp:0:0
#12 0x0000564317b36a51 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&)>) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x7269a51)
#13 0x0000564314a92feb (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#14 0x0000564314a8f9af mlir::applyPatternsGreedily(mlir::Region&, mlir::FrozenRewritePatternSet const&, mlir::GreedyRewriteConfig, bool*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41c29af)
#15 0x0000564312a9de28 (anonymous namespace)::LinalgSpecializeGenericOpsPass::runOnOperation() Specialize.cpp:0:0
#16 0x0000564314a1a496 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x414d496)
#17 0x0000564314a1adc0 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x414ddc0)
#18 0x0000564314a1d6c2 mlir::PassManager::run(mlir::Operation*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41506c2)
#19 0x0000564314a15aea performActions(llvm::raw_ostream&, std::shared_ptr<llvm::SourceMgr> const&, mlir::MLIRContext*, mlir::MlirOptMainConfig const&) MlirOptMain.cpp:0:0
#20 0x0000564314a1573d llvm::LogicalResult llvm::function_ref<llvm::LogicalResult (std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, 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::raw_ostream&) MlirOptMain.cpp:0:0
#21 0x0000564314abc785 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::raw_ostream&)>, llvm::raw_ostream&, llvm::StringRef, llvm::StringRef) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41ef785)
#22 0x0000564314a0f755 mlir::MlirOptMain(llvm::raw_ostream&, std::unique_ptr<llvm::MemoryBuffer, std::default_delete<llvm::MemoryBuffer>>, mlir::DialectRegistry&, mlir::MlirOptMainConfig const&) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4142755)
#23 0x0000564314a0f9ff mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41429ff)
#24 0x0000564314a0fd2e mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4142d2e)
#25 0x0000564311a9ea67 main (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11d1a67)
#26 0x00007fd71b29c083 __libc_start_main /build/glibc-LcI20x/glibc-2.31/csu/../csu/libc-start.c:342:3
#27 0x0000564311a9e5de _start (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11d15de)

@llvmbot llvmbot added the mlir label Jan 8, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 8, 2025

@llvm/issue-subscribers-mlir

Author: None (Emilyaxe)

git version: e4372c4

system: Ubuntu 18.04.6 LTS

reproduce with: mlir-opt a.mlir --linalg-specialize-generic-ops

a.mlir:

#map = affine_map&lt;(d0, d1, d2) -&gt; (0, d1, 0)&gt;
#map1 = affine_map&lt;(d0, d1, d2) -&gt; (d0, d1, d2)&gt;
#map2 = affine_map&lt;(d0, d1) -&gt; (d0)&gt;
#map3 = affine_map&lt;(d0, d1) -&gt; (d1)&gt;
#map4 = affine_map&lt;(d0, d1) -&gt; ()&gt;
#map5 = affine_map&lt;(d0, d1) -&gt; (d0, d1)&gt;
#map6 = affine_map&lt;(d0, d1, d2) -&gt; (0, d1, d2)&gt;
#map7 = affine_map&lt;(d0, d1, d2) -&gt; (0, 0, 0)&gt;
#map8 = affine_map&lt;(d0, d1, d2) -&gt; (d0, d2)&gt;
#map9 = affine_map&lt;(d0, d1, d2) -&gt; (d2, d1)&gt;
#map10 = affine_map&lt;(d0, d1, d2) -&gt; (d0, d1)&gt;
module {
  func.func private @<!-- -->printMemrefI32(tensor&lt;*xi32&gt;)
  func.func private @<!-- -->printMemrefF32(tensor&lt;*xf32&gt;)
  func.func private @<!-- -->func1(%arg0: index, %arg1: tensor&lt;1x4x26xi32&gt;, %arg2: tensor&lt;1x4x26xi32&gt;) -&gt; ( tensor&lt;1x4x4xi32&gt;) {
    %true = index.bool.constant true
    %cst = arith.constant dense&lt;1&gt; : tensor&lt;1x4x1xi32&gt;
    %cst_0 = arith.constant dense&lt;0&gt; : tensor&lt;1x4x1xi32&gt;
    %cst_1 = arith.constant dense&lt;1&gt; : tensor&lt;1x4x4xi32&gt;
    %cst_2 = arith.constant dense&lt;6&gt; : tensor&lt;1x4x4xi32&gt;
    %cst_3 = arith.constant dense&lt;0&gt; : tensor&lt;1x4x4xi32&gt;
    %c0_i32 = arith.constant 0 : i32
    %cst_4 = arith.constant dense&lt;1.000000e+00&gt; : tensor&lt;1x4x1xf32&gt;
    %cst_5 = arith.constant dense&lt;0.000000e+00&gt; : tensor&lt;1x4x1xf32&gt;
    %cst_6 = arith.constant dense&lt;9968&gt; : tensor&lt;1x4x26xi32&gt;
    %0 = tensor.empty() : tensor&lt;1x4xi32&gt;
    %c2147483647_i32 = arith.constant 2147483647 : i32
    %1 = linalg.fill ins(%c2147483647_i32 : i32) outs(%0 : tensor&lt;1x4xi32&gt;) -&gt; tensor&lt;1x4xi32&gt;
    %reduced = linalg.reduce ins(%cst_6 : tensor&lt;1x4x26xi32&gt;) outs(%1 : tensor&lt;1x4xi32&gt;) dimensions = [2] 
      (%in: i32, %init: i32) {
        %51 = arith.minsi %in, %init : i32
        linalg.yield %51 : i32
      }
    %expanded = tensor.expand_shape %reduced [[0], [1, 2]] output_shape [1, 4, 1] : tensor&lt;1x4xi32&gt; into tensor&lt;1x4x1xi32&gt;
    %collapsed = tensor.collapse_shape %expanded [[0, 1, 2]] : tensor&lt;1x4x1xi32&gt; into tensor&lt;4xi32&gt;
    %collapsed_7 = tensor.collapse_shape %expanded [[0, 1, 2]] : tensor&lt;1x4x1xi32&gt; into tensor&lt;4xi32&gt;
    %12 = tensor.empty() : tensor&lt;4x4xi32&gt;
    %13 = linalg.generic {indexing_maps = [#map2, #map3, #map4, #map5], iterator_types = ["parallel", "parallel"]} ins(%collapsed_7, %collapsed, %c0_i32 : tensor&lt;4xi32&gt;, tensor&lt;4xi32&gt;, i32) outs(%12 : tensor&lt;4x4xi32&gt;) {
    ^bb0(%in: i32, %in_11: i32, %in_12: i32, %out: i32):
      %51 = arith.muli %in, %in_11 : i32
      %52 = arith.addi %in_12, %51 : i32
      linalg.yield %52 : i32
    } -&gt; tensor&lt;4x4xi32&gt;
    %expanded_8 = tensor.expand_shape %13 [[0, 1], [2]] output_shape [1, 4, 4] : tensor&lt;4x4xi32&gt; into tensor&lt;1x4x4xi32&gt;
    %14 = scf.while (%arg3 = %cst_3) : (tensor&lt;1x4x4xi32&gt;) -&gt; tensor&lt;1x4x4xi32&gt; {
      %51 = tensor.empty() : tensor&lt;1x4x4xi1&gt;
      %52 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%cst_2, %arg3 : tensor&lt;1x4x4xi32&gt;, tensor&lt;1x4x4xi32&gt;) outs(%51 : tensor&lt;1x4x4xi1&gt;) {
      ^bb0(%in: i32, %in_11: i32, %out: i1):
        %53 = arith.cmpi sgt, %in, %in_11 : i32
        linalg.yield %53 : i1
      } -&gt; tensor&lt;1x4x4xi1&gt;
      %extracted = tensor.extract %52[%arg0, %arg0, %arg0] : tensor&lt;1x4x4xi1&gt;
      %from_elements = tensor.from_elements %extracted : tensor&lt;i1&gt;
      scf.condition(%extracted) %arg3 : tensor&lt;1x4x4xi32&gt;
    } do {
    ^bb0(%arg3: tensor&lt;1x4x4xi32&gt;):
      %51 = tensor.empty() : tensor&lt;1x4x4xi32&gt;
      %52 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%arg3, %cst_1 : tensor&lt;1x4x4xi32&gt;, tensor&lt;1x4x4xi32&gt;) outs(%51 : tensor&lt;1x4x4xi32&gt;) {
      ^bb0(%in: i32, %in_11: i32, %out: i32):
        %53 = arith.addi %in, %in_11 : i32
        linalg.yield %53 : i32
      } -&gt; tensor&lt;1x4x4xi32&gt;
      scf.yield %52 : tensor&lt;1x4x4xi32&gt;
    }
    %45 = tensor.empty() : tensor&lt;1x4x4xi32&gt;
    %46 = linalg.generic {indexing_maps = [#map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%14 : tensor&lt;1x4x4xi32&gt;) outs(%45 : tensor&lt;1x4x4xi32&gt;) {
    ^bb0(%in: i32, %out: i32):
      %c0_i32_11 = arith.constant 0 : i32
      %51 = arith.subi %c0_i32_11, %in : i32
      %52 = arith.maxsi %in, %51 : i32
      linalg.yield %52 : i32
    } -&gt; tensor&lt;1x4x4xi32&gt;
    %47 = tensor.empty() : tensor&lt;1x4x4xi32&gt;
    %48 = linalg.generic {indexing_maps = [#map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%46 : tensor&lt;1x4x4xi32&gt;) outs(%47 : tensor&lt;1x4x4xi32&gt;) {
    ^bb0(%in: i32, %out: i32):
      %c0_i32_11 = arith.constant 0 : i32
      %c16_i32 = arith.constant 16 : i32
      %51 = arith.maxsi %c0_i32_11, %in : i32
      %52 = arith.minsi %c16_i32, %51 : i32
      linalg.yield %52 : i32
    } -&gt; tensor&lt;1x4x4xi32&gt;
    %49 = tensor.empty() : tensor&lt;1x4x4xi32&gt;
    %50 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%expanded_8, %48 : tensor&lt;1x4x4xi32&gt;, tensor&lt;1x4x4xi32&gt;) outs(%49 : tensor&lt;1x4x4xi32&gt;) {
    ^bb0(%in: i32, %in_11: i32, %out: i32):
      %51 = arith.shrui %in, %in_11 : i32
      linalg.yield %51 : i32
    } -&gt; tensor&lt;1x4x4xi32&gt;
    return  %50 :  tensor&lt;1x4x4xi32&gt;
  }
}


stack trace:

 mlir-opt: /data/szy/MLIR/llvm-release/llvm-project/llvm/include/llvm/Support/Casting.h:566: decltype(auto) llvm::cast(const From &amp;) [To = mlir::RankedTensorType, From = mlir::Type]: Assertion `isa&lt;To&gt;(Val) &amp;&amp; "cast&lt;Ty&gt;() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt --linalg-specialize-generic-ops a.mlir
 #<!-- -->0 0x0000564311abfce8 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11f2ce8)
 #<!-- -->1 0x0000564311abd7fe llvm::sys::RunSignalHandlers() (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11f07fe)
 #<!-- -->2 0x0000564311ac067d SignalHandler(int) Signals.cpp:0:0
 #<!-- -->3 0x00007fd71bc7e420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #<!-- -->4 0x00007fd71b2bb00b raise /build/glibc-LcI20x/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #<!-- -->5 0x00007fd71b29a859 abort /build/glibc-LcI20x/glibc-2.31/stdlib/abort.c:81:7
 #<!-- -->6 0x00007fd71b29a729 get_sysdep_segment_value /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:509:8
 #<!-- -->7 0x00007fd71b29a729 _nl_load_domain /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:970:34
 #<!-- -->8 0x00007fd71b2abfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #<!-- -->9 0x0000564312cb67f5 (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x23e97f5)
#<!-- -->10 0x0000564312cb5120 (anonymous namespace)::DecomposeProjectedPermutation::matchAndRewrite(mlir::linalg::GenericOp, mlir::PatternRewriter&amp;) const DecomposeGenericByUnfoldingPermutation.cpp:0:0
#<!-- -->11 0x0000564317b39d31 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&amp;, llvm::function_ref&lt;bool (mlir::Pattern const&amp;)&gt;, llvm::function_ref&lt;void (mlir::Pattern const&amp;)&gt;, llvm::function_ref&lt;llvm::LogicalResult (mlir::Pattern const&amp;)&gt;)::$_0&gt;(long) PatternApplicator.cpp:0:0
#<!-- -->12 0x0000564317b36a51 mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&amp;, llvm::function_ref&lt;bool (mlir::Pattern const&amp;)&gt;, llvm::function_ref&lt;void (mlir::Pattern const&amp;)&gt;, llvm::function_ref&lt;llvm::LogicalResult (mlir::Pattern const&amp;)&gt;) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x7269a51)
#<!-- -->13 0x0000564314a92feb (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->14 0x0000564314a8f9af mlir::applyPatternsGreedily(mlir::Region&amp;, mlir::FrozenRewritePatternSet const&amp;, mlir::GreedyRewriteConfig, bool*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41c29af)
#<!-- -->15 0x0000564312a9de28 (anonymous namespace)::LinalgSpecializeGenericOpsPass::runOnOperation() Specialize.cpp:0:0
#<!-- -->16 0x0000564314a1a496 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x414d496)
#<!-- -->17 0x0000564314a1adc0 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&amp;, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x414ddc0)
#<!-- -->18 0x0000564314a1d6c2 mlir::PassManager::run(mlir::Operation*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41506c2)
#<!-- -->19 0x0000564314a15aea performActions(llvm::raw_ostream&amp;, std::shared_ptr&lt;llvm::SourceMgr&gt; const&amp;, mlir::MLIRContext*, mlir::MlirOptMainConfig const&amp;) MlirOptMain.cpp:0:0
#<!-- -->20 0x0000564314a1573d llvm::LogicalResult llvm::function_ref&lt;llvm::LogicalResult (std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, 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::raw_ostream&amp;) MlirOptMain.cpp:0:0
#<!-- -->21 0x0000564314abc785 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::raw_ostream&amp;)&gt;, llvm::raw_ostream&amp;, llvm::StringRef, llvm::StringRef) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41ef785)
#<!-- -->22 0x0000564314a0f755 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;) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4142755)
#<!-- -->23 0x0000564314a0f9ff mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&amp;) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41429ff)
#<!-- -->24 0x0000564314a0fd2e mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&amp;) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4142d2e)
#<!-- -->25 0x0000564311a9ea67 main (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11d1a67)
#<!-- -->26 0x00007fd71b29c083 __libc_start_main /build/glibc-LcI20x/glibc-2.31/csu/../csu/libc-start.c:342:3
#<!-- -->27 0x0000564311a9e5de _start (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11d15de)

@EugeneZelenko EugeneZelenko added the crash Prefer [crash-on-valid] or [crash-on-invalid] label Jan 8, 2025
@EugeneZelenko
Copy link
Contributor

@Emilyaxe: Please do not forget to add crash label and Bug type for crash issues.

@Emilyaxe
Copy link
Author

Emilyaxe commented Jan 9, 2025

@Emilyaxe: Please do not forget to add crash label and Bug type for crash issues.

ok, I will add labels!

@Emilyaxe Emilyaxe added mlir:linalg and removed mlir labels Jan 9, 2025
@llvmbot
Copy link
Member

llvmbot commented Jan 9, 2025

@llvm/issue-subscribers-mlir-linalg

Author: None (Emilyaxe)

git version: e4372c4

system: Ubuntu 18.04.6 LTS

reproduce with: mlir-opt a.mlir --linalg-specialize-generic-ops

a.mlir:

#map = affine_map&lt;(d0, d1, d2) -&gt; (0, d1, 0)&gt;
#map1 = affine_map&lt;(d0, d1, d2) -&gt; (d0, d1, d2)&gt;
#map2 = affine_map&lt;(d0, d1) -&gt; (d0)&gt;
#map3 = affine_map&lt;(d0, d1) -&gt; (d1)&gt;
#map4 = affine_map&lt;(d0, d1) -&gt; ()&gt;
#map5 = affine_map&lt;(d0, d1) -&gt; (d0, d1)&gt;
#map6 = affine_map&lt;(d0, d1, d2) -&gt; (0, d1, d2)&gt;
#map7 = affine_map&lt;(d0, d1, d2) -&gt; (0, 0, 0)&gt;
#map8 = affine_map&lt;(d0, d1, d2) -&gt; (d0, d2)&gt;
#map9 = affine_map&lt;(d0, d1, d2) -&gt; (d2, d1)&gt;
#map10 = affine_map&lt;(d0, d1, d2) -&gt; (d0, d1)&gt;
module {
  func.func private @<!-- -->printMemrefI32(tensor&lt;*xi32&gt;)
  func.func private @<!-- -->printMemrefF32(tensor&lt;*xf32&gt;)
  func.func private @<!-- -->func1(%arg0: index, %arg1: tensor&lt;1x4x26xi32&gt;, %arg2: tensor&lt;1x4x26xi32&gt;) -&gt; ( tensor&lt;1x4x4xi32&gt;) {
    %true = index.bool.constant true
    %cst = arith.constant dense&lt;1&gt; : tensor&lt;1x4x1xi32&gt;
    %cst_0 = arith.constant dense&lt;0&gt; : tensor&lt;1x4x1xi32&gt;
    %cst_1 = arith.constant dense&lt;1&gt; : tensor&lt;1x4x4xi32&gt;
    %cst_2 = arith.constant dense&lt;6&gt; : tensor&lt;1x4x4xi32&gt;
    %cst_3 = arith.constant dense&lt;0&gt; : tensor&lt;1x4x4xi32&gt;
    %c0_i32 = arith.constant 0 : i32
    %cst_4 = arith.constant dense&lt;1.000000e+00&gt; : tensor&lt;1x4x1xf32&gt;
    %cst_5 = arith.constant dense&lt;0.000000e+00&gt; : tensor&lt;1x4x1xf32&gt;
    %cst_6 = arith.constant dense&lt;9968&gt; : tensor&lt;1x4x26xi32&gt;
    %0 = tensor.empty() : tensor&lt;1x4xi32&gt;
    %c2147483647_i32 = arith.constant 2147483647 : i32
    %1 = linalg.fill ins(%c2147483647_i32 : i32) outs(%0 : tensor&lt;1x4xi32&gt;) -&gt; tensor&lt;1x4xi32&gt;
    %reduced = linalg.reduce ins(%cst_6 : tensor&lt;1x4x26xi32&gt;) outs(%1 : tensor&lt;1x4xi32&gt;) dimensions = [2] 
      (%in: i32, %init: i32) {
        %51 = arith.minsi %in, %init : i32
        linalg.yield %51 : i32
      }
    %expanded = tensor.expand_shape %reduced [[0], [1, 2]] output_shape [1, 4, 1] : tensor&lt;1x4xi32&gt; into tensor&lt;1x4x1xi32&gt;
    %collapsed = tensor.collapse_shape %expanded [[0, 1, 2]] : tensor&lt;1x4x1xi32&gt; into tensor&lt;4xi32&gt;
    %collapsed_7 = tensor.collapse_shape %expanded [[0, 1, 2]] : tensor&lt;1x4x1xi32&gt; into tensor&lt;4xi32&gt;
    %12 = tensor.empty() : tensor&lt;4x4xi32&gt;
    %13 = linalg.generic {indexing_maps = [#map2, #map3, #map4, #map5], iterator_types = ["parallel", "parallel"]} ins(%collapsed_7, %collapsed, %c0_i32 : tensor&lt;4xi32&gt;, tensor&lt;4xi32&gt;, i32) outs(%12 : tensor&lt;4x4xi32&gt;) {
    ^bb0(%in: i32, %in_11: i32, %in_12: i32, %out: i32):
      %51 = arith.muli %in, %in_11 : i32
      %52 = arith.addi %in_12, %51 : i32
      linalg.yield %52 : i32
    } -&gt; tensor&lt;4x4xi32&gt;
    %expanded_8 = tensor.expand_shape %13 [[0, 1], [2]] output_shape [1, 4, 4] : tensor&lt;4x4xi32&gt; into tensor&lt;1x4x4xi32&gt;
    %14 = scf.while (%arg3 = %cst_3) : (tensor&lt;1x4x4xi32&gt;) -&gt; tensor&lt;1x4x4xi32&gt; {
      %51 = tensor.empty() : tensor&lt;1x4x4xi1&gt;
      %52 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%cst_2, %arg3 : tensor&lt;1x4x4xi32&gt;, tensor&lt;1x4x4xi32&gt;) outs(%51 : tensor&lt;1x4x4xi1&gt;) {
      ^bb0(%in: i32, %in_11: i32, %out: i1):
        %53 = arith.cmpi sgt, %in, %in_11 : i32
        linalg.yield %53 : i1
      } -&gt; tensor&lt;1x4x4xi1&gt;
      %extracted = tensor.extract %52[%arg0, %arg0, %arg0] : tensor&lt;1x4x4xi1&gt;
      %from_elements = tensor.from_elements %extracted : tensor&lt;i1&gt;
      scf.condition(%extracted) %arg3 : tensor&lt;1x4x4xi32&gt;
    } do {
    ^bb0(%arg3: tensor&lt;1x4x4xi32&gt;):
      %51 = tensor.empty() : tensor&lt;1x4x4xi32&gt;
      %52 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%arg3, %cst_1 : tensor&lt;1x4x4xi32&gt;, tensor&lt;1x4x4xi32&gt;) outs(%51 : tensor&lt;1x4x4xi32&gt;) {
      ^bb0(%in: i32, %in_11: i32, %out: i32):
        %53 = arith.addi %in, %in_11 : i32
        linalg.yield %53 : i32
      } -&gt; tensor&lt;1x4x4xi32&gt;
      scf.yield %52 : tensor&lt;1x4x4xi32&gt;
    }
    %45 = tensor.empty() : tensor&lt;1x4x4xi32&gt;
    %46 = linalg.generic {indexing_maps = [#map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%14 : tensor&lt;1x4x4xi32&gt;) outs(%45 : tensor&lt;1x4x4xi32&gt;) {
    ^bb0(%in: i32, %out: i32):
      %c0_i32_11 = arith.constant 0 : i32
      %51 = arith.subi %c0_i32_11, %in : i32
      %52 = arith.maxsi %in, %51 : i32
      linalg.yield %52 : i32
    } -&gt; tensor&lt;1x4x4xi32&gt;
    %47 = tensor.empty() : tensor&lt;1x4x4xi32&gt;
    %48 = linalg.generic {indexing_maps = [#map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%46 : tensor&lt;1x4x4xi32&gt;) outs(%47 : tensor&lt;1x4x4xi32&gt;) {
    ^bb0(%in: i32, %out: i32):
      %c0_i32_11 = arith.constant 0 : i32
      %c16_i32 = arith.constant 16 : i32
      %51 = arith.maxsi %c0_i32_11, %in : i32
      %52 = arith.minsi %c16_i32, %51 : i32
      linalg.yield %52 : i32
    } -&gt; tensor&lt;1x4x4xi32&gt;
    %49 = tensor.empty() : tensor&lt;1x4x4xi32&gt;
    %50 = linalg.generic {indexing_maps = [#map6, #map6, #map1], iterator_types = ["parallel", "parallel", "parallel"]} ins(%expanded_8, %48 : tensor&lt;1x4x4xi32&gt;, tensor&lt;1x4x4xi32&gt;) outs(%49 : tensor&lt;1x4x4xi32&gt;) {
    ^bb0(%in: i32, %in_11: i32, %out: i32):
      %51 = arith.shrui %in, %in_11 : i32
      linalg.yield %51 : i32
    } -&gt; tensor&lt;1x4x4xi32&gt;
    return  %50 :  tensor&lt;1x4x4xi32&gt;
  }
}


stack trace:

 mlir-opt: /data/szy/MLIR/llvm-release/llvm-project/llvm/include/llvm/Support/Casting.h:566: decltype(auto) llvm::cast(const From &amp;) [To = mlir::RankedTensorType, From = mlir::Type]: Assertion `isa&lt;To&gt;(Val) &amp;&amp; "cast&lt;Ty&gt;() argument of incompatible type!"' failed.
PLEASE submit a bug report to https://github.com/llvm/llvm-project/issues/ and include the crash backtrace.
Stack dump:
0.      Program arguments: /data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt --linalg-specialize-generic-ops a.mlir
 #<!-- -->0 0x0000564311abfce8 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11f2ce8)
 #<!-- -->1 0x0000564311abd7fe llvm::sys::RunSignalHandlers() (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11f07fe)
 #<!-- -->2 0x0000564311ac067d SignalHandler(int) Signals.cpp:0:0
 #<!-- -->3 0x00007fd71bc7e420 __restore_rt (/lib/x86_64-linux-gnu/libpthread.so.0+0x14420)
 #<!-- -->4 0x00007fd71b2bb00b raise /build/glibc-LcI20x/glibc-2.31/signal/../sysdeps/unix/sysv/linux/raise.c:51:1
 #<!-- -->5 0x00007fd71b29a859 abort /build/glibc-LcI20x/glibc-2.31/stdlib/abort.c:81:7
 #<!-- -->6 0x00007fd71b29a729 get_sysdep_segment_value /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:509:8
 #<!-- -->7 0x00007fd71b29a729 _nl_load_domain /build/glibc-LcI20x/glibc-2.31/intl/loadmsgcat.c:970:34
 #<!-- -->8 0x00007fd71b2abfd6 (/lib/x86_64-linux-gnu/libc.so.6+0x33fd6)
 #<!-- -->9 0x0000564312cb67f5 (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x23e97f5)
#<!-- -->10 0x0000564312cb5120 (anonymous namespace)::DecomposeProjectedPermutation::matchAndRewrite(mlir::linalg::GenericOp, mlir::PatternRewriter&amp;) const DecomposeGenericByUnfoldingPermutation.cpp:0:0
#<!-- -->11 0x0000564317b39d31 void llvm::function_ref&lt;void ()&gt;::callback_fn&lt;mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&amp;, llvm::function_ref&lt;bool (mlir::Pattern const&amp;)&gt;, llvm::function_ref&lt;void (mlir::Pattern const&amp;)&gt;, llvm::function_ref&lt;llvm::LogicalResult (mlir::Pattern const&amp;)&gt;)::$_0&gt;(long) PatternApplicator.cpp:0:0
#<!-- -->12 0x0000564317b36a51 mlir::PatternApplicator::matchAndRewrite(mlir::Operation*, mlir::PatternRewriter&amp;, llvm::function_ref&lt;bool (mlir::Pattern const&amp;)&gt;, llvm::function_ref&lt;void (mlir::Pattern const&amp;)&gt;, llvm::function_ref&lt;llvm::LogicalResult (mlir::Pattern const&amp;)&gt;) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x7269a51)
#<!-- -->13 0x0000564314a92feb (anonymous namespace)::GreedyPatternRewriteDriver::processWorklist() GreedyPatternRewriteDriver.cpp:0:0
#<!-- -->14 0x0000564314a8f9af mlir::applyPatternsGreedily(mlir::Region&amp;, mlir::FrozenRewritePatternSet const&amp;, mlir::GreedyRewriteConfig, bool*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41c29af)
#<!-- -->15 0x0000564312a9de28 (anonymous namespace)::LinalgSpecializeGenericOpsPass::runOnOperation() Specialize.cpp:0:0
#<!-- -->16 0x0000564314a1a496 mlir::detail::OpToOpPassAdaptor::run(mlir::Pass*, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x414d496)
#<!-- -->17 0x0000564314a1adc0 mlir::detail::OpToOpPassAdaptor::runPipeline(mlir::OpPassManager&amp;, mlir::Operation*, mlir::AnalysisManager, bool, unsigned int, mlir::PassInstrumentor*, mlir::PassInstrumentation::PipelineParentInfo const*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x414ddc0)
#<!-- -->18 0x0000564314a1d6c2 mlir::PassManager::run(mlir::Operation*) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41506c2)
#<!-- -->19 0x0000564314a15aea performActions(llvm::raw_ostream&amp;, std::shared_ptr&lt;llvm::SourceMgr&gt; const&amp;, mlir::MLIRContext*, mlir::MlirOptMainConfig const&amp;) MlirOptMain.cpp:0:0
#<!-- -->20 0x0000564314a1573d llvm::LogicalResult llvm::function_ref&lt;llvm::LogicalResult (std::unique_ptr&lt;llvm::MemoryBuffer, std::default_delete&lt;llvm::MemoryBuffer&gt;&gt;, 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::raw_ostream&amp;) MlirOptMain.cpp:0:0
#<!-- -->21 0x0000564314abc785 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::raw_ostream&amp;)&gt;, llvm::raw_ostream&amp;, llvm::StringRef, llvm::StringRef) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41ef785)
#<!-- -->22 0x0000564314a0f755 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;) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4142755)
#<!-- -->23 0x0000564314a0f9ff mlir::MlirOptMain(int, char**, llvm::StringRef, llvm::StringRef, mlir::DialectRegistry&amp;) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x41429ff)
#<!-- -->24 0x0000564314a0fd2e mlir::MlirOptMain(int, char**, llvm::StringRef, mlir::DialectRegistry&amp;) (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x4142d2e)
#<!-- -->25 0x0000564311a9ea67 main (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11d1a67)
#<!-- -->26 0x00007fd71b29c083 __libc_start_main /build/glibc-LcI20x/glibc-2.31/csu/../csu/libc-start.c:342:3
#<!-- -->27 0x0000564311a9e5de _start (/data/szy/MLIR/llvm-release/llvm-project/build/bin/mlir-opt+0x11d15de)

@GrumpyPigSkin
Copy link
Contributor

A smaller example is (sorry if this can be made smaller):

#map0 = affine_map<(i, j) -> (i, j)>
#map1 = affine_map<(i, j) -> ()>

module {
  func.func @test() -> tensor<2x2xi32> {
    // Two 2x2 tensor inputs.
    %lhs = arith.constant dense<2> : tensor<2x2xi32>
    %rhs = arith.constant dense<3> : tensor<2x2xi32>
    // A scalar i32 input (the problem input).
    %c0 = arith.constant 42 : i32
    %init = tensor.empty() : tensor<2x2xi32>

    %res =
      linalg.generic
        { indexing_maps = [#map0, #map0, #map1, #map0],
          iterator_types = ["parallel", "parallel"] }
        // pass in scalar here
        ins(%lhs, %rhs, %c0 : tensor<2x2xi32>, tensor<2x2xi32>, i32)
        outs(%init : tensor<2x2xi32>) 
  {
      ^bb0(%x: i32, %y: i32, %scalar: i32, %out: i32):
        %add = arith.addi %x, %y : i32
        %sum = arith.addi %add, %scalar : i32
        linalg.yield %sum : i32
    } -> tensor<2x2xi32>

    // Return the computed tensor.
    return %res : tensor<2x2xi32>
  }
}

The issue here being that linalg.generic expects inputs of some ranked tensor not a scalar i32. So this would pass:

#map0 = affine_map<(i, j) -> (i, j)>
#map1 = affine_map<(i, j) -> ()>

module {
  func.func @test() -> tensor<2x2xi32> {
    // Two 2x2 tensor inputs.
    %lhs = arith.constant dense<2> : tensor<2x2xi32>
    %rhs = arith.constant dense<3> : tensor<2x2xi32>
    // A scalar i32 input.
    %c0 = arith.constant 42 : i32
    // create tensor
    %ct0 = tensor.from_elements %c0 : tensor<i32>
    // ...
        ins(%lhs, %rhs, %ct0 : tensor<2x2xi32>, tensor<2x2xi32>, tensor<i32>)
   // ...
  }
}

I was having a look through the code and this validates the operands:

  // LinalgInterfaces.cpp
  for (OpOperand &opOperand : linalgOp->getOpOperands()) {
    AffineMap indexingMap = linalgOp.getMatchingIndexingMap(&opOperand);

    // Symbols disallowed.
    if (indexingMap.getNumSymbols() != 0)
      return op->emitOpError("unexpected symbols in indexing_map #")
             << opOperand.getOperandNumber();

    // Domain must be consistent.
    unsigned numLoops = linalgOp.getNumLoops();
    if (indexingMap.getNumDims() != numLoops)
      return op->emitOpError("expected indexing_map #")
             << opOperand.getOperandNumber() << " to have " << numLoops
             << " dim(s) to match the number of loops";

    int64_t rank = linalgOp.getRank(&opOperand);

    if (indexingMap.getNumResults() != rank)
      return op->emitOpError("expected operand rank (")
             << rank << ") to match the result rank of indexing_map #"
             << opOperand.getOperandNumber() << " ("
             << indexingMap.getNumResults() << ")";
  }

The issue is here the i32 returns it is rank-0 and the check passes, then further down the pipeline here:

  // DecomposeGenericByUnfoldingPermutation.cpp
  if (llvm::any_of(op->getOpOperands(), [](OpOperand &oper) {
        auto opType = cast<RankedTensorType>(oper.get().getType());
        return ShapedType::isDynamicShape(opType.getShape());
      }))
    return failure();

It attempts to cast i32 to a RankedTensorType which fails.

I am new to MLIR so not sure what the best cause of action would be, but where the checks are done in LinalgInterfaces.cpp would it be worth adding a check where if we have what looks like a rank-0 tensor, we check if it is scalar error if it is. Or do we want to "promote" it to a tensor?

@CoTinker
Copy link
Contributor

CoTinker commented Jan 9, 2025

I think we should add extra check for RankedTensorType operand. CC @javedabsar1

@GrumpyPigSkin
Copy link
Contributor

@CoTinker could you assign this to me and I will add the check?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
crash Prefer [crash-on-valid] or [crash-on-invalid] mlir:linalg
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants