[Mlir-commits] [mlir] [mlir][emitc] Only mark operator with fundamental type have no side effect (PR #144990)
Jianjian Guan
llvmlistbot at llvm.org
Mon Aug 18 19:56:06 PDT 2025
https://github.com/jacquesguan updated https://github.com/llvm/llvm-project/pull/144990
>From 5a8f14e920f7033c9dc3877500bd19b64b1ca68a Mon Sep 17 00:00:00 2001
From: Jianjian GUAN <jacquesguan at me.com>
Date: Fri, 20 Jun 2025 15:23:33 +0800
Subject: [PATCH] [mlir][emitc] Only mark operator with fundamental type have
no side effect
---
mlir/include/mlir/Dialect/EmitC/IR/EmitC.h | 3 +++
mlir/include/mlir/Dialect/EmitC/IR/EmitC.td | 7 +++++--
mlir/lib/Dialect/EmitC/IR/EmitC.cpp | 6 ++++++
mlir/test/Dialect/EmitC/transforms.mlir | 23 +++++++++++++++++++++
4 files changed, 37 insertions(+), 2 deletions(-)
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 937b34a625628..16991a75f2f33 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 e6a3154721faa..c430c75ab7e2c 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -134,6 +134,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/transforms.mlir b/mlir/test/Dialect/EmitC/transforms.mlir
index a38f396dad953..4412db06473bf 100644
--- a/mlir/test/Dialect/EmitC/transforms.mlir
+++ b/mlir/test/Dialect/EmitC/transforms.mlir
@@ -163,3 +163,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