Skip to content

[mlir][emitc] Fix literal translation #71296

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
merged 1 commit into from
Nov 6, 2023

Conversation

aniragil
Copy link
Contributor

@aniragil aniragil commented Nov 4, 2023

  • Do not emit variables-at-top for literals
  • Do not emit an error for a missing name for literals used as call operands.

- Do not emit variables-at-top for literals
- Do not emit an error for a missing name for literals used as call operands.
@llvmbot
Copy link
Member

llvmbot commented Nov 4, 2023

@llvm/pr-subscribers-mlir

@llvm/pr-subscribers-mlir-emitc

Author: Gil Rapaport (aniragil)

Changes
  • 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 --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);

@aniragil aniragil requested a review from marbre November 4, 2023 20:33
@aniragil
Copy link
Contributor Author

aniragil commented Nov 4, 2023

@jpienaar jpienaar merged commit 6c59f0e into llvm:main Nov 6, 2023
@marbre
Copy link
Member

marbre commented Nov 6, 2023

Thanks for reviewing and landing @jpienaar.

@aniragil aniragil deleted the emitc-fix-literal-emission branch November 6, 2023 08:16
@aniragil
Copy link
Contributor Author

aniragil commented Nov 6, 2023

Thanks @jpienaar !

marbre pushed a commit that referenced this pull request Nov 6, 2023
Fix a corner case missed in #71296 when operands generated by literals
are mixed with the args attribute of a call op.

Additionally remove a range check that is already handled by the CallOp
verifier.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants