[Mlir-commits] [mlir] [mlir][emitc] Fix literal translation (PR #71296)
Gil Rapaport
llvmlistbot at llvm.org
Sat Nov 4 13:32:30 PDT 2023
https://github.com/aniragil created https://github.com/llvm/llvm-project/pull/71296
- Do not emit variables-at-top for literals
- Do not emit an error for a missing name for literals used as call operands.
>From 7b275c41d61fa263de20578c26e276376b834635 Mon Sep 17 00:00:00 2001
From: Gil Rapaport <gil.rapaport at mobileye.com>
Date: Sat, 4 Nov 2023 20:46:15 +0200
Subject: [PATCH] [mlir][emitc] Fix literal translation
- Do not emit variables-at-top for literals
- Do not emit an error for a missing name for literals used as call operands.
---
mlir/lib/Target/Cpp/TranslateToCpp.cpp | 5 ++++-
mlir/test/Target/Cpp/literal.mlir | 1 -
mlir/test/Target/Cpp/literal_call_operand.mlir | 2 --
3 files changed, 4 insertions(+), 4 deletions(-)
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);
More information about the Mlir-commits
mailing list