[Mlir-commits] [mlir] [MLIR][Linalg] Fix crash when parsing linalg.elementwise with vector inputs (#178363) (PR #179170)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Feb 2 18:51:57 PST 2026


================
@@ -4847,12 +4847,57 @@ ParseResult ElementwiseOp::parse(OpAsmParser &parser, OperationState &result) {
   auto arityGroupAndKind = getArityGroupAndKind(elemwiseKindVal);
   int numRegionArgs =
       getArityGroupAsUInt(arityGroupAndKind.arityGroup) + 1 /*output*/;
-  if (parseNamedStructuredOp(parser, result, numRegionArgs,
-                             ElementwiseOp::getRegionBuilder())) {
----------------
IamYJLee wrote:

@matthias-springer 
Thank you for your feedback.

Initially, I planned this patch based on the advice in the issue.
However, while testing cases where the input and output types differ (for example, memref as input and tensor as output), I noticed that this situation is already handled with the following error: (in verifier)

```mlir
../testing.mlir:20:15: error: 'linalg.elementwise' op expected to have pure tensor or buffer semantics
    %result = linalg.elementwise kind=#linalg.elementwise_kind<add> ins(%A, %B: memref<16x8xf32>, memref<16x8xf32>) outs(%C: tensor<16x8xf32>) -> tensor<16x8xf32>
              ^
../testing.mlir:20:15: note: see current operation: 
%0 = "linalg.elementwise"(%arg0, %arg1, %arg2) <{indexing_maps = [affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>, affine_map<(d0, d1) -> (d0, d1)>], kind = #linalg.elementwise_kind<add>, operandSegmentSizes = array<i32: 2, 1>}> ({
^bb0(%arg3: f32, %arg4: f32, %arg5: f32):
  %1 = "arith.addf"(%arg3, %arg4) <{fastmath = #arith.fastmath<none>}> : (f32, f32) -> f32
  "linalg.yield"(%1) : (f32) -> ()
}) : (memref<16x8xf32>, memref<16x8xf32>, tensor<16x8xf32>) -> tensor<16x8xf32>
```

If this type-mismatch error handling is considered sufficient, then this extra code may not be necessary.

In that case, would it be acceptable to handle the vector case by just propagating `emitError`, so that it produces an error like the following?

```mlir
../testing.mlir:4:69: error: custom op 'linalg.elementwise' Cannot build binary Linalg operation: expects allComplex, allFloatingPoint, or allInteger, got 'vector<16x8xf32>' and 'vector<16x8xf32>'
    %result = linalg.elementwise kind=#linalg.elementwise_kind<add> ins(%A, %B: vector<16x8xf32>, vector<16x8xf32>) outs(%C: vector<16x8xf32>) -> vector<16x8xf32>
                                                                    ^
../testing.mlir:5:5: error: custom op 'linalg.elementwise' unable to parse elemwise op
    return %result : vector<16x8xf32>
    ^
```

https://github.com/llvm/llvm-project/pull/179170


More information about the Mlir-commits mailing list