[Mlir-commits] [mlir] [mlir][emitc] Only mark operator with fundamental type have no side effect (PR #144990)
Jianjian Guan
llvmlistbot at llvm.org
Fri Jun 20 00:28:07 PDT 2025
https://github.com/jacquesguan created https://github.com/llvm/llvm-project/pull/144990
None
>From e27317b2b48d86fa9a622a46d857a4699673e4ea 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 | 19 +++++++++++++++++--
mlir/lib/Dialect/EmitC/IR/EmitC.cpp | 5 +++++
3 files changed, 25 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 9ecdb74f4d82e..c66c3824bcacd 100644
--- a/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
+++ b/mlir/include/mlir/Dialect/EmitC/IR/EmitC.td
@@ -43,7 +43,11 @@ class EmitC_UnaryOp<string mnemonic, list<Trait> traits = []> :
let extraClassDeclaration = [{
bool hasSideEffects() {
- return false;
+ // If operand is fundamental type, the operation is pure.
+ if (isFundamentalType(getOperand().getType())) {
+ return false;
+ }
+ return true;
}
}];
}
@@ -57,7 +61,18 @@ class EmitC_BinaryOp<string mnemonic, list<Trait> traits = []> :
let extraClassDeclaration = [{
bool hasSideEffects() {
- return false;
+ // If both operands are fundamental types, the operation is pure.
+ if (isFundamentalType(getOperand(0).getType())
+ && isFundamentalType(getOperand(1).getType())) {
+ return false;
+ }
+ // Pointer arithmetic operations is pure.
+ if (isa<emitc::PointerType>(getOperand(0).getType())
+ && (isFundamentalType(getOperand(1).getType()) ||
+ isa<emitc::PointerType>(getOperand(1).getType()))) {
+ return false;
+ }
+ return true;
}
}];
}
diff --git a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
index e602210c2dc6c..bb176d5fad808 100644
--- a/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
+++ b/mlir/lib/Dialect/EmitC/IR/EmitC.cpp
@@ -137,6 +137,11 @@ bool mlir::emitc::isPointerWideType(Type type) {
type);
}
+bool mlir::emitc::isFundamentalType(Type type) {
+ return llvm::isa<IndexType>(type) || isPointerWideType(type) ||
+ isSupportedIntegerType(type) || isSupportedFloatType(type);
+}
+
/// Check that the type of the initial value is compatible with the operations
/// result type.
static LogicalResult verifyInitializationAttribute(Operation *op,
More information about the Mlir-commits
mailing list