[Mlir-commits] [mlir] [mlir][Arith][EmitC] Bail-out on arith.constant conversions to EmitC (PR #201565)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Thu Jun 4 05:25:47 PDT 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-emitc

Author: ioana ghiban (ioghiban)

<details>
<summary>Changes</summary>

Update ArithToEmitC to bail-out before creating invalid EmitC ops for unsupported cases.

ArithToEmitC now avoids rewriting memref constants to `emitc.constant`, since the type conversion changes the result type but the attribute cannot be converted into a valid EmitC initializer.

This does not add support for converting these memrefs. It only makes the existing limitation explicit at the conversion boundary.

## Tests

Added negative tests for the standalone conversion pass. This pass marks its source ops illegal, so when a pattern bails-out the pass reports a legalization failure. This is the expected behavior and documents the unsupported cases directly.

Assisted-by: Codex (refine description). I reviewed all text before submission.

---
Full diff: https://github.com/llvm/llvm-project/pull/201565.diff


2 Files Affected:

- (modified) mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp (+3) 
- (modified) mlir/test/Conversion/ArithToEmitC/arith-to-emitc-failed.mlir (+24) 


``````````diff
diff --git a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
index d003dc7a6dff3..cfce60b693f13 100644
--- a/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
+++ b/mlir/lib/Conversion/ArithToEmitC/ArithToEmitC.cpp
@@ -62,6 +62,9 @@ class ArithConstantOpConversionPattern
     Type newTy = this->getTypeConverter()->convertType(arithConst.getType());
     if (!newTy)
       return rewriter.notifyMatchFailure(arithConst, "type conversion failed");
+    if (isa<MemRefType>(arithConst.getType()))
+      return rewriter.notifyMatchFailure(arithConst,
+                                         "memref constants are not supported");
     rewriter.replaceOpWithNewOp<emitc::ConstantOp>(arithConst, newTy,
                                                    adaptor.getValue());
     return success();
diff --git a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-failed.mlir b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-failed.mlir
index fba4483d316f4..02dc388d3da86 100644
--- a/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-failed.mlir
+++ b/mlir/test/Conversion/ArithToEmitC/arith-to-emitc-failed.mlir
@@ -21,3 +21,27 @@ func.func @unsuppoted_emitc_type(%arg0: i4, %arg1: i4) {
   %0 = arith.addi %arg0, %arg1 : i4
   return
 }
+
+// -----
+
+func.func private @rank0_constant() -> memref<i64> {
+  // expected-error at +1 {{failed to legalize operation 'arith.constant'}}
+  %0 = arith.constant dense<-1> : memref<i64>
+  return %0 : memref<i64>
+}
+
+// -----
+
+func.func private @rank1_constant() -> memref<1xi64> {
+  // expected-error at +1 {{failed to legalize operation 'arith.constant'}}
+  %0 = arith.constant dense<[-1]> : memref<1xi64>
+  return %0 : memref<1xi64>
+}
+
+// -----
+
+func.func private @return_rank2_constant() -> memref<1x1xi64> {
+  // expected-error at +1 {{failed to legalize operation 'arith.constant'}}
+  %0 = arith.constant dense<[[-1]]> : memref<1x1xi64>
+  return %0 : memref<1x1xi64>
+}

``````````

</details>


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


More information about the Mlir-commits mailing list