[Mlir-commits] [mlir] 68b071d - [mlir][emitc] Fix corner case in translation of literal ops (#71375)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Mon Nov 6 07:17:25 PST 2023


Author: Simon Camphausen
Date: 2023-11-06T16:17:20+01:00
New Revision: 68b071d9a22796d2037eee74e49bbb9fe707e19b

URL: https://github.com/llvm/llvm-project/commit/68b071d9a22796d2037eee74e49bbb9fe707e19b
DIFF: https://github.com/llvm/llvm-project/commit/68b071d9a22796d2037eee74e49bbb9fe707e19b.diff

LOG: [mlir][emitc] Fix corner case in translation of literal ops (#71375)

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.

Added: 
    

Modified: 
    mlir/lib/Target/Cpp/TranslateToCpp.cpp
    mlir/test/Target/Cpp/literal_call_operand.mlir

Removed: 
    


################################################################################
diff  --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index 6c95eb3d20dacde..291624c5480318d 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -425,12 +425,13 @@ 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)))
+        Value operand = op.getOperand(idx);
+        auto literalDef =
+            dyn_cast_if_present<LiteralOp>(operand.getDefiningOp());
+        if (!literalDef && !emitter.hasValueInScope(operand))
           return op.emitOpError("operand ")
                  << idx << "'s value not defined in scope";
-        os << emitter.getOrCreateName(op.getOperand(idx));
+        os << emitter.getOrCreateName(operand);
         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);


        


More information about the Mlir-commits mailing list