[Mlir-commits] [mlir] [mlir][emitc] Fix literal translation (PR #71296)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Sat Nov 4 13:33:00 PDT 2023
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
@llvm/pr-subscribers-mlir-emitc
Author: Gil Rapaport (aniragil)
<details>
<summary>Changes</summary>
- Do not emit variables-at-top for literals
- Do not emit an error for a missing name for literals used as call operands.
---
Full diff: https://github.com/llvm/llvm-project/pull/71296.diff
3 Files Affected:
- (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (+4-1)
- (modified) mlir/test/Target/Cpp/literal.mlir (-1)
- (modified) mlir/test/Target/Cpp/literal_call_operand.mlir (-2)
``````````diff
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 8ffea4d5b7b3248..6c95eb3d20dacde 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -637,6 +637,8 @@ static LogicalResult printOperation(CppEmitter &emitter,
// regions.
WalkResult result =
functionOp.walk<WalkOrder::PreOrder>([&](Operation *op) -> WalkResult {
+ if (isa<emitc::LiteralOp>(op))
+ return WalkResult::skip();
for (OpResult result : op->getResults()) {
if (failed(emitter.emitVariableDeclaration(
result, /*trailingSemicolon=*/true))) {
@@ -839,7 +841,8 @@ LogicalResult CppEmitter::emitAttribute(Location loc, Attribute attr) {
LogicalResult CppEmitter::emitOperands(Operation &op) {
auto emitOperandName = [&](Value result) -> LogicalResult {
- if (!hasValueInScope(result))
+ auto literalDef = dyn_cast_if_present<LiteralOp>(result.getDefiningOp());
+ if (!literalDef && !hasValueInScope(result))
return op.emitOpError() << "operand value not in scope";
os << getOrCreateName(result);
return success();
diff --git a/mlir/test/Target/Cpp/literal.mlir b/mlir/test/Target/Cpp/literal.mlir
index 19be1e83b5d0236..eff787611197738 100644
--- a/mlir/test/Target/Cpp/literal.mlir
+++ b/mlir/test/Target/Cpp/literal.mlir
@@ -10,6 +10,5 @@ func.func @emitc_literal(%arg0: f32) {
// CPP-DEFAULT: float [[V2:[^ ]*]] = [[V0:[^ ]*]] + M_PI
// CPP-DECLTOP: void emitc_literal(float [[V0:[^ ]*]]) {
-// CPP-DECLTOP: float M_PI;
// CPP-DECLTOP: float [[V1:[^ ]*]];
// CPP-DECLTOP: [[V1]] = [[V0:[^ ]*]] + M_PI
diff --git a/mlir/test/Target/Cpp/literal_call_operand.mlir b/mlir/test/Target/Cpp/literal_call_operand.mlir
index a41ee52449cf814..017b4d53c43e3d8 100644
--- a/mlir/test/Target/Cpp/literal_call_operand.mlir
+++ b/mlir/test/Target/Cpp/literal_call_operand.mlir
@@ -1,6 +1,5 @@
// RUN: mlir-translate -mlir-to-cpp %s | FileCheck %s -check-prefix=CPP-DEFAULT
// RUN: mlir-translate -mlir-to-cpp -declare-variables-at-top %s | FileCheck %s -check-prefix=CPP-DECLTOP
-// XFAIL: *
func.func @emitc_call_operand() {
%p0 = emitc.literal "M_PI" : f32
@@ -11,6 +10,5 @@ func.func @emitc_call_operand() {
// CPP-DEFAULT-NEXT: float v1 = foo(M_PI);
// CPP-DECLTOP: void emitc_call_operand() {
-// CPP-DECLTOP-NEXT: float M_PI;
// CPP-DECLTOP-NEXT: float v1;
// CPP-DECLTOP-NEXT: v1 = foo(M_PI);
``````````
</details>
https://github.com/llvm/llvm-project/pull/71296
More information about the Mlir-commits
mailing list