-
Notifications
You must be signed in to change notification settings - Fork 15k
[mlir][emitc] Only mark operator with fundamental type have no side effect #144990
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
jacquesguan
merged 1 commit into
llvm:main
from
jacquesguan:mlir-emitc-operator-sideeffect
Sep 1, 2025
Merged
[mlir][emitc] Only mark operator with fundamental type have no side effect #144990
jacquesguan
merged 1 commit into
llvm:main
from
jacquesguan:mlir-emitc-operator-sideeffect
Sep 1, 2025
Conversation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@llvm/pr-subscribers-mlir-emitc @llvm/pr-subscribers-mlir Author: Jianjian Guan (jacquesguan) ChangesFull diff: https://github.com/llvm/llvm-project/pull/144990.diff 3 Files Affected:
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,
|
Hey, thanks for the PR. Can you please add a test case that shows the new behavior? |
aniragil
requested changes
Jul 29, 2025
e27317b
to
69809fa
Compare
aniragil
requested changes
Aug 14, 2025
69809fa
to
5a8f14e
Compare
aniragil
approved these changes
Aug 29, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, thanks!
5a8f14e
to
e5ce700
Compare
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
No description provided.