[mlir][emitc] Unify API for deferred emission#167532
Merged
Conversation
This patch adds `printOperation()` functions for deferred emission ops in order to unify the API used for emitting operations. No functional change intended.
Member
|
@llvm/pr-subscribers-mlir @llvm/pr-subscribers-mlir-emitc Author: Gil Rapaport (aniragil) ChangesThis patch adds Full diff: https://github.com/llvm/llvm-project/pull/167532.diff 1 Files Affected:
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 12435119b98a1..ae209679ece6c 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -402,6 +402,57 @@ static bool shouldBeInlined(ExpressionOp expressionOp) {
return false;
}
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::GetFieldOp getFieldOp) {
+ emitter.cacheDeferredOpResult(getFieldOp.getResult(),
+ getFieldOp.getFieldName());
+ return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::GetGlobalOp getGlobalOp) {
+ emitter.cacheDeferredOpResult(getGlobalOp.getResult(), getGlobalOp.getName());
+ return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::LiteralOp literalOp) {
+ emitter.cacheDeferredOpResult(literalOp.getResult(), literalOp.getValue());
+ return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::MemberOp memberOp) {
+ std::string out;
+ llvm::raw_string_ostream ss(out);
+ ss << emitter.getOrCreateName(memberOp.getOperand());
+ ss << "." << memberOp.getMember();
+ emitter.cacheDeferredOpResult(memberOp.getResult(), out);
+ return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::MemberOfPtrOp memberOfPtrOp) {
+ std::string out;
+ llvm::raw_string_ostream ss(out);
+ ss << emitter.getOrCreateName(memberOfPtrOp.getOperand());
+ ss << "->" << memberOfPtrOp.getMember();
+ emitter.cacheDeferredOpResult(memberOfPtrOp.getResult(), out);
+ return success();
+}
+
+static LogicalResult printOperation(CppEmitter &emitter,
+ emitc::SubscriptOp subscriptOp) {
+ std::string out;
+ llvm::raw_string_ostream ss(out);
+ ss << emitter.getOrCreateName(subscriptOp.getValue());
+ for (auto index : subscriptOp.getIndices()) {
+ ss << "[" << emitter.getOrCreateName(index) << "]";
+ }
+ emitter.cacheDeferredOpResult(subscriptOp.getResult(), out);
+ return success();
+}
+
static LogicalResult printConstantOp(CppEmitter &emitter, Operation *operation,
Attribute value) {
OpResult result = operation->getResult(0);
@@ -1761,41 +1812,19 @@ LogicalResult CppEmitter::emitOperation(Operation &op, bool trailingSemicolon) {
emitc::CmpOp, emitc::ConditionalOp, emitc::ConstantOp,
emitc::DeclareFuncOp, emitc::DivOp, emitc::DoOp,
emitc::ExpressionOp, emitc::FieldOp, emitc::FileOp,
- emitc::ForOp, emitc::FuncOp, emitc::GlobalOp, emitc::IfOp,
- emitc::IncludeOp, emitc::LoadOp, emitc::LogicalAndOp,
- emitc::LogicalNotOp, emitc::LogicalOrOp, emitc::MulOp,
- emitc::RemOp, emitc::ReturnOp, emitc::SubOp, emitc::SwitchOp,
- emitc::UnaryMinusOp, emitc::UnaryPlusOp, emitc::VariableOp,
- emitc::VerbatimOp>(
+ emitc::ForOp, emitc::FuncOp, emitc::GetFieldOp,
+ emitc::GetGlobalOp, emitc::GlobalOp, emitc::IfOp,
+ emitc::IncludeOp, emitc::LiteralOp, emitc::LoadOp,
+ emitc::LogicalAndOp, emitc::LogicalNotOp, emitc::LogicalOrOp,
+ emitc::MemberOfPtrOp, emitc::MemberOp, emitc::MulOp,
+ emitc::RemOp, emitc::ReturnOp, emitc::SubscriptOp, emitc::SubOp,
+ emitc::SwitchOp, emitc::UnaryMinusOp, emitc::UnaryPlusOp,
+ emitc::VariableOp, emitc::VerbatimOp>(
[&](auto op) { return printOperation(*this, op); })
// Func ops.
.Case<func::CallOp, func::FuncOp, func::ReturnOp>(
[&](auto op) { return printOperation(*this, op); })
- .Case<emitc::GetGlobalOp>([&](auto op) {
- cacheDeferredOpResult(op.getResult(), op.getName());
- return success();
- })
- .Case<emitc::GetFieldOp>([&](auto op) {
- cacheDeferredOpResult(op.getResult(), op.getFieldName());
- return success();
- })
- .Case<emitc::LiteralOp>([&](auto op) {
- cacheDeferredOpResult(op.getResult(), op.getValue());
- return success();
- })
- .Case<emitc::MemberOp>([&](auto op) {
- cacheDeferredOpResult(op.getResult(), createMemberAccess(op));
- return success();
- })
- .Case<emitc::MemberOfPtrOp>([&](auto op) {
- cacheDeferredOpResult(op.getResult(), createMemberAccess(op));
- return success();
- })
- .Case<emitc::SubscriptOp>([&](auto op) {
- cacheDeferredOpResult(op.getResult(), getSubscriptName(op));
- return success();
- })
.Default([&](Operation *) {
return op.emitOpError("unable to find printer for op");
});
|
simon-camp
approved these changes
Nov 12, 2025
Contributor
simon-camp
left a comment
There was a problem hiding this comment.
LGTM, thanks for the cleanup
Contributor
Author
|
Thanks @simon-camp ! |
aniragil
added a commit
to aniragil/llvm-project
that referenced
this pull request
Nov 12, 2025
This patch is a follow up on llvm#167532, which refactored these method's code into the relevant `printOperation()` functions but did not remove them.
aniragil
added a commit
that referenced
this pull request
Nov 13, 2025
This patch is a follow up on #167532, which refactored these method's code into the relevant `printOperation()` functions but did not remove them.
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
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.
This patch adds
printOperation()functions for deferred emission ops in order to unify the API used for emitting operations.No functional change intended.