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

mlir-opt crashes with UNREACHABLE in linalg.elementwise when operands have vector types

Open compilerStudyer opened this issue 10 months ago • 8 comments

mlir-opt crashes with an UNREACHABLE when parsing a linalg.elementwise operation whose operands are vector types instead of tensors or memrefs.

The crash happens during parsing / region construction, before any pass pipeline is executed.

module {
  func.func @main() {
    %0 = spirv.Constant dense<0> : vector<4xi32>
    %1 = gpu.grid_dim  x
    %2:4 = vector.to_elements %0 : vector<4xi32>
    %3 = vector.multi_reduction <add>, %0, %2#0 [0] : vector<4xi32> to i32
    %4 = vector.reduction <add>, %0, %3 : vector<4xi32> into i32
    %5 = linalg.elementwise kind=#linalg.elementwise_kind<add> ins(%0, %0 : vector<4xi32>, vector<4xi32>) outs(%0 : vector<4xi32>) -> tensor<4xi32>
    return
  }
}
mlir-opt test.mlir
unsupported non numeric type
UNREACHABLE executed at mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp:514!
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
1.      MLIR Parser: custom op parser 'builtin.module'
2.      MLIR Parser: custom op parser 'func.func'
3.      MLIR Parser: custom op parser 'linalg.elementwise'
 #0 0x0000565bb1f40e32 llvm::sys::PrintStackTrace(llvm::raw_ostream&, int) (mlir-opt+0x252fe32)
 #1 0x0000565bb1f3d5af llvm::sys::RunSignalHandlers() (mlir-opt+0x252c5af)
 #2 0x0000565bb1f3d6fc SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #3 0x0000796393245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #4 0x000079639329eb2c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #5 0x000079639329eb2c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #6 0x000079639329eb2c pthread_kill ./nptl/pthread_kill.c:89:10
 #7 0x000079639324527e raise ./signal/../sysdeps/posix/raise.c:27:6
 #8 0x00007963932288ff abort ./stdlib/abort.c:81:7
 #9 0x0000565bb1f2012e (mlir-opt+0x250f12e)
#10 0x0000565bb7ee3e74 (anonymous namespace)::RegionBuilderHelper::buildBinaryFn(mlir::linalg::BinaryFn, mlir::Value, mlir::Value, llvm::function_ref<mlir::InFlightDiagnostic ()>) LinalgOps.cpp:0:0
#11 0x0000565bb7ef6280 mlir::linalg::ElementwiseOp::regionBuilder(mlir::ImplicitLocOpBuilder&, mlir::Block&, llvm::ArrayRef<mlir::NamedAttribute>, llvm::function_ref<mlir::InFlightDiagnostic ()>) (mlir-opt+0x84e5280)

compilerStudyer avatar Jan 28 '26 04:01 compilerStudyer

@llvm/issue-subscribers-mlir-linalg

Author: None (compilerStudyer)

mlir-opt crashes with an UNREACHABLE when parsing a linalg.elementwise operation whose operands are vector types instead of tensors or memrefs.

The crash happens during parsing / region construction, before any pass pipeline is executed.

module {
  func.func @<!-- -->main() {
    %0 = spirv.Constant dense&lt;0&gt; : vector&lt;4xi32&gt;
    %1 = gpu.grid_dim  x
    %2:4 = vector.to_elements %0 : vector&lt;4xi32&gt;
    %3 = vector.multi_reduction &lt;add&gt;, %0, %2#<!-- -->0 [0] : vector&lt;4xi32&gt; to i32
    %4 = vector.reduction &lt;add&gt;, %0, %3 : vector&lt;4xi32&gt; into i32
    %5 = linalg.elementwise kind=#linalg.elementwise_kind&lt;add&gt; ins(%0, %0 : vector&lt;4xi32&gt;, vector&lt;4xi32&gt;) outs(%0 : vector&lt;4xi32&gt;) -&gt; tensor&lt;4xi32&gt;
    return
  }
}
mlir-opt test.mlir
unsupported non numeric type
UNREACHABLE executed at mlir/lib/Dialect/Linalg/IR/LinalgOps.cpp:514!
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
1.      MLIR Parser: custom op parser 'builtin.module'
2.      MLIR Parser: custom op parser 'func.func'
3.      MLIR Parser: custom op parser 'linalg.elementwise'
 #<!-- -->0 0x0000565bb1f40e32 llvm::sys::PrintStackTrace(llvm::raw_ostream&amp;, int) (mlir-opt+0x252fe32)
 #<!-- -->1 0x0000565bb1f3d5af llvm::sys::RunSignalHandlers() (mlir-opt+0x252c5af)
 #<!-- -->2 0x0000565bb1f3d6fc SignalHandler(int, siginfo_t*, void*) Signals.cpp:0:0
 #<!-- -->3 0x0000796393245330 (/lib/x86_64-linux-gnu/libc.so.6+0x45330)
 #<!-- -->4 0x000079639329eb2c __pthread_kill_implementation ./nptl/pthread_kill.c:44:76
 #<!-- -->5 0x000079639329eb2c __pthread_kill_internal ./nptl/pthread_kill.c:78:10
 #<!-- -->6 0x000079639329eb2c pthread_kill ./nptl/pthread_kill.c:89:10
 #<!-- -->7 0x000079639324527e raise ./signal/../sysdeps/posix/raise.c:27:6
 #<!-- -->8 0x00007963932288ff abort ./stdlib/abort.c:81:7
 #<!-- -->9 0x0000565bb1f2012e (mlir-opt+0x250f12e)
#<!-- -->10 0x0000565bb7ee3e74 (anonymous namespace)::RegionBuilderHelper::buildBinaryFn(mlir::linalg::BinaryFn, mlir::Value, mlir::Value, llvm::function_ref&lt;mlir::InFlightDiagnostic ()&gt;) LinalgOps.cpp:0:0
#<!-- -->11 0x0000565bb7ef6280 mlir::linalg::ElementwiseOp::regionBuilder(mlir::ImplicitLocOpBuilder&amp;, mlir::Block&amp;, llvm::ArrayRef&lt;mlir::NamedAttribute&gt;, llvm::function_ref&lt;mlir::InFlightDiagnostic ()&gt;) (mlir-opt+0x84e5280)

llvmbot avatar Jan 28 '26 05:01 llvmbot

Is this an issue that should be handled by the verifier or by an explicit condition check? It seems that this case should be diagnosed earlier, rather than reaching an llvm_unreachable. I’d like to try fixing this issue.

IamYJLee avatar Jan 28 '26 06:01 IamYJLee

This happens during parsing. It looks like emitError is not passed when calling RegionBuilderHelper::buildBinaryFn. (See call site in ElementwiseOp::regionBuilder.)

matthias-springer avatar Jan 28 '26 08:01 matthias-springer

@javedabsar1

rengolin avatar Jan 28 '26 08:01 rengolin

This happens during parsing. It looks like emitError is not passed when calling RegionBuilderHelper::buildBinaryFn. (See call site in ElementwiseOp::regionBuilder.)

Thanks for the pointer! Would you mind assigning the issue to me? I’ll take a look.

IamYJLee avatar Jan 28 '26 09:01 IamYJLee

This is interesting. I never imagined vectors being passed to linalg.elementwise. The elementwise op must lower to linalg.generic which only accepts memref/tensors. Probably we need a clean 'check memref or tensor' and bail out nicely on 'linalg.elementwise ... vector<4xi32>, vector<4xi32>) '

javedabsar1 avatar Jan 28 '26 12:01 javedabsar1

This is interesting. I never imagined vectors being passed to linalg.elementwise. The elementwise op must lower to linalg.generic which only accepts memref/tensors. Probably we need a clean 'check memref or tensor' and bail out nicely on 'linalg.elementwise ... vector<4xi32>, vector<4xi32>) '

Just to make sure I’m on the right track, is this the expected outcome for the issue I’m working on?

test.mlir:8:64: error: custom op 'linalg.elementwise' Cannot build binary Linalg operation: expects allComplex, allFloatingPoint, or allInteger, got 'vector<4xi32>' and 'vector<4xi32>'
    %5 = linalg.elementwise kind=#linalg.elementwise_kind<add> ins(%0, %0 : vector<4xi32>, vector<4xi32>) outs(%0 : vector<4xi32>) -> tensor<4xi32>
                                                               ^
test.mlir:9:5: error: custom op 'linalg.elementwise' unable to parse elemwise op
    return
    ^

I still don’t fully understand how the elementwise op should be lowered to linalg.generic.

IamYJLee avatar Jan 29 '26 07:01 IamYJLee

The types must match. tensor -> tensor, memref -> memref and vector -> vector

rengolin avatar Jan 29 '26 12:01 rengolin