[Mlir-commits] [mlir] [mlir][EmitC] Do not convert illegal types in EmitC (PR #104571)

Longsheng Mou llvmlistbot at llvm.org
Fri Aug 16 02:31:00 PDT 2024


https://github.com/CoTinker created https://github.com/llvm/llvm-project/pull/104571

This patch adds check for unsupported types in emitc, which fixes a crash. Fix #103706.

>From ac5c5c13ccbb51f5bdcacf86a2ea6b6aeba4d22f Mon Sep 17 00:00:00 2001
From: Longsheng Mou <moulongsheng at huawei.com>
Date: Fri, 16 Aug 2024 17:19:15 +0800
Subject: [PATCH] [mlir][EmitC] Do not convert illegal types in EmitC

This patch adds check for unsupported types in emitc, which
fixes a crash.
---
 .../Conversion/MemRefToEmitC/MemRefToEmitC.cpp   |  3 ++-
 .../MemRefToEmitC/memref-to-emitc-failed.mlir    | 16 ++++++++++++++++
 2 files changed, 18 insertions(+), 1 deletion(-)

diff --git a/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp b/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
index e0c421741b3055..e968675a648c22 100644
--- a/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
+++ b/mlir/lib/Conversion/MemRefToEmitC/MemRefToEmitC.cpp
@@ -177,7 +177,8 @@ void mlir::populateMemRefToEmitCTypeConversion(TypeConverter &typeConverter) {
         }
         Type convertedElementType =
             typeConverter.convertType(memRefType.getElementType());
-        if (!convertedElementType)
+        if (!convertedElementType ||
+            !emitc::isSupportedEmitCType(convertedElementType))
           return {};
         return emitc::ArrayType::get(memRefType.getShape(),
                                      convertedElementType);
diff --git a/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-failed.mlir b/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-failed.mlir
index 89dafa7529ed53..836d8aedefc1f0 100644
--- a/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-failed.mlir
+++ b/mlir/test/Conversion/MemRefToEmitC/memref-to-emitc-failed.mlir
@@ -43,3 +43,19 @@ func.func @zero_rank() {
 
 // expected-error at +1 {{failed to legalize operation 'memref.global'}}
 memref.global "nested" constant @nested_global : memref<3x7xf32>
+
+// -----
+
+func.func @unsupported_type_f16() {
+  // expected-error at +1 {{failed to legalize operation 'memref.alloca'}}
+  %0 = memref.alloca() : memref<4xf16>
+  return
+}
+
+// -----
+
+func.func @unsupported_type_i4() {
+  // expected-error at +1 {{failed to legalize operation 'memref.alloca'}}
+  %0 = memref.alloca() : memref<4xi4>
+  return
+}



More information about the Mlir-commits mailing list