Skip to content

[mlir][emitc] Fix corner case in translation of literal ops #71375

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

simon-camp
Copy link
Contributor

@simon-camp simon-camp commented 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.

@llvmbot
Copy link
Member

llvmbot commented Nov 6, 2023

@llvm/pr-subscribers-mlir

Author: Simon Camphausen (simon-camp)

Changes

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 done by the CallOp verifier.


Full diff: https://github.com/llvm/llvm-project/pull/71375.diff

2 Files Affected:

  • (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (-5)
  • (modified) mlir/test/Target/Cpp/literal_call_operand.mlir (+12)
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 6c95eb3d20dacde..eeec9b11af7a0d0 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -425,11 +425,6 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::CallOp callOp) {
       // Index attributes are treated specially as operand index.
       if (t.getType().isIndex()) {
         int64_t idx = t.getInt();
-        if ((idx < 0) || (idx >= op.getNumOperands()))
-          return op.emitOpError("invalid operand index");
-        if (!emitter.hasValueInScope(op.getOperand(idx)))
-          return op.emitOpError("operand ")
-                 << idx << "'s value not defined in scope";
         os << emitter.getOrCreateName(op.getOperand(idx));
         return success();
       }
diff --git a/mlir/test/Target/Cpp/literal_call_operand.mlir b/mlir/test/Target/Cpp/literal_call_operand.mlir
index 017b4d53c43e3d8..428b66bb2519d9c 100644
--- a/mlir/test/Target/Cpp/literal_call_operand.mlir
+++ b/mlir/test/Target/Cpp/literal_call_operand.mlir
@@ -12,3 +12,15 @@ func.func @emitc_call_operand() {
 // CPP-DECLTOP: void emitc_call_operand() {
 // CPP-DECLTOP-NEXT: float v1;
 // CPP-DECLTOP-NEXT: v1 = foo(M_PI);
+
+func.func @emitc_call_operand_arg() {
+  %p0 = emitc.literal "M_PI" : f32
+  %1 = emitc.call "bar"(%p0) {args = [42 : i32, 0 : index]} : (f32) -> f32
+  return
+}
+// CPP-DEFAULT: void emitc_call_operand_arg() {
+// CPP-DEFAULT-NEXT: float v1 = bar(42, M_PI);
+
+// CPP-DECLTOP: void emitc_call_operand_arg() {
+// CPP-DECLTOP-NEXT: float v1;
+// CPP-DECLTOP-NEXT: v1 = bar(42, M_PI);

@llvmbot
Copy link
Member

llvmbot commented Nov 6, 2023

@llvm/pr-subscribers-mlir-emitc

Author: Simon Camphausen (simon-camp)

Changes

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 done by the CallOp verifier.


Full diff: https://github.com/llvm/llvm-project/pull/71375.diff

2 Files Affected:

  • (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (-5)
  • (modified) mlir/test/Target/Cpp/literal_call_operand.mlir (+12)
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 6c95eb3d20dacde..eeec9b11af7a0d0 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -425,11 +425,6 @@ static LogicalResult printOperation(CppEmitter &emitter, emitc::CallOp callOp) {
       // Index attributes are treated specially as operand index.
       if (t.getType().isIndex()) {
         int64_t idx = t.getInt();
-        if ((idx < 0) || (idx >= op.getNumOperands()))
-          return op.emitOpError("invalid operand index");
-        if (!emitter.hasValueInScope(op.getOperand(idx)))
-          return op.emitOpError("operand ")
-                 << idx << "'s value not defined in scope";
         os << emitter.getOrCreateName(op.getOperand(idx));
         return success();
       }
diff --git a/mlir/test/Target/Cpp/literal_call_operand.mlir b/mlir/test/Target/Cpp/literal_call_operand.mlir
index 017b4d53c43e3d8..428b66bb2519d9c 100644
--- a/mlir/test/Target/Cpp/literal_call_operand.mlir
+++ b/mlir/test/Target/Cpp/literal_call_operand.mlir
@@ -12,3 +12,15 @@ func.func @emitc_call_operand() {
 // CPP-DECLTOP: void emitc_call_operand() {
 // CPP-DECLTOP-NEXT: float v1;
 // CPP-DECLTOP-NEXT: v1 = foo(M_PI);
+
+func.func @emitc_call_operand_arg() {
+  %p0 = emitc.literal "M_PI" : f32
+  %1 = emitc.call "bar"(%p0) {args = [42 : i32, 0 : index]} : (f32) -> f32
+  return
+}
+// CPP-DEFAULT: void emitc_call_operand_arg() {
+// CPP-DEFAULT-NEXT: float v1 = bar(42, M_PI);
+
+// CPP-DECLTOP: void emitc_call_operand_arg() {
+// CPP-DECLTOP-NEXT: float v1;
+// CPP-DECLTOP-NEXT: v1 = bar(42, M_PI);

@marbre marbre self-requested a review November 6, 2023 15:15
@marbre marbre merged commit 68b071d into llvm:main Nov 6, 2023
@simon-camp simon-camp deleted the emitc.literal.fix branch November 6, 2023 15:17
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