[Mlir-commits] [mlir] a50b096 - [mlir][emitc] Only mark operator with fundamental type have no side effect (#144990)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Aug 31 23:38:37 PDT 2025


Author: Jianjian Guan
Date: 2025-09-01T14:38:33+08:00
New Revision: a50b096798ca0e186439b427390424c26add5713

URL: https://github.com/llvm/llvm-project/commit/a50b096798ca0e186439b427390424c26add5713
DIFF: https://github.com/llvm/llvm-project/commit/a50b096798ca0e186439b427390424c26add5713.diff

LOG: [mlir][emitc] Only mark operator with fundamental type have no side effect (#144990)

Added: 
    

Modified: 
    mlir/include/mlir/Dialect/EmitC/IR/EmitC.h
    mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
    mlir/lib/Dialect/EmitC/IR/EmitC.cpp
    mlir/test/Dialect/EmitC/form-expressions.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.h b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.h
index 1984ed8a7f068..eb7ddeb3bfc54 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.h
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.h
@@ -53,6 +53,9 @@ bool isPointerWideType(mlir::Type type);
 struct Placeholder {};
 using ReplacementItem = std::variant<StringRef, Placeholder>;
 
+/// Determines whether \p type is a valid fundamental C++ type in EmitC.
+bool isFundamentalType(mlir::Type type);
+
 } // namespace emitc
 } // namespace mlir
 

diff  --git a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
index 9798015400a81..44265e4c12be2 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -43,7 +43,8 @@ class EmitC_UnaryOp<string mnemonic, list<Trait> traits = []> :
 
   let extraClassDeclaration = [{
     bool hasSideEffects() {
-      return false;
+      // If operand is fundamental type, the operation is pure.
+      return !isFundamentalType(getOperand().getType());
     }
   }];
 }
@@ -57,7 +58,9 @@ class EmitC_BinaryOp<string mnemonic, list<Trait> traits = []> :
 
   let extraClassDeclaration = [{
     bool hasSideEffects() {
-      return false;
+      // If both operands are fundamental types, the operation is pure.
+      return !isFundamentalType(getOperand(0).getType()) ||
+        !isFundamentalType(getOperand(1).getType());
     }
   }];
 }

diff  --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index 5359826eed0fd..58aba59278f2a 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -131,6 +131,12 @@ bool mlir::emitc::isPointerWideType(Type type) {
       type);
 }
 
+bool mlir::emitc::isFundamentalType(Type type) {
+  return llvm::isa<IndexType>(type) || isPointerWideType(type) ||
+         isSupportedIntegerType(type) || isSupportedFloatType(type) ||
+         isa<emitc::PointerType>(type);
+}
+
 /// Check that the type of the initial value is compatible with the operations
 /// result type.
 static LogicalResult verifyInitializationAttribute(Operation *op,

diff  --git a/mlir/test/Dialect/EmitC/form-expressions.mlir b/mlir/test/Dialect/EmitC/form-expressions.mlir
index 1ac053bf1215f..c8dbf0416837e 100644
--- a/mlir/test/Dialect/EmitC/form-expressions.mlir
+++ b/mlir/test/Dialect/EmitC/form-expressions.mlir
@@ -160,3 +160,26 @@ func.func @expression_with_load(%arg0: i32, %arg1: !emitc.ptr<i32>) -> i1 {
   %c = emitc.cmp lt, %b, %arg0 :(i32, i32) -> i1
   return %c : i1
 }
+
+// CHECK-LABEL:   func.func @opaque_type_expression(%arg0: i32, %arg1: !emitc.opaque<"T0">, %arg2: i32) -> i1 {
+// CHECK:           %0 = "emitc.constant"() <{value = 42 : i32}> : () -> i32
+// CHECK:           %1 = emitc.expression : i32 {
+// CHECK:             %3 = mul %arg0, %0 : (i32, i32) -> i32
+// CHECK:             %4 = sub %3, %arg1 : (i32, !emitc.opaque<"T0">) -> i32
+// CHECK:             yield %4 : i32
+// CHECK:           }
+// CHECK:           %2 = emitc.expression : i1 {
+// CHECK:             %3 = cmp lt, %1, %arg2 : (i32, i32) -> i1
+// CHECK:             yield %3 : i1
+// CHECK:           }
+// CHECK:           return %2 : i1
+// CHECK:         }
+
+
+func.func @opaque_type_expression(%arg0: i32,  %arg1: !emitc.opaque<"T0">, %arg2: i32) -> i1 {
+  %c42 = "emitc.constant"(){value = 42 : i32} : () -> i32
+  %a = emitc.mul %arg0, %c42 : (i32, i32) -> i32
+  %b = emitc.sub %a, %arg1 : (i32, !emitc.opaque<"T0">) -> i32
+  %c = emitc.cmp lt, %b, %arg2 :(i32, i32) -> i1
+  return %c : i1
+}


        


More information about the Mlir-commits mailing list