[Mlir-commits] [mlir] [mlir][emitc] Fix bug in ApplyOp translation (PR #155171)

llvmlistbot at llvm.org llvmlistbot at llvm.org
Sun Aug 24 07:40:37 PDT 2025


llvmbot wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-mlir-emitc

Author: Gil Rapaport (aniragil)

<details>
<summary>Changes</summary>

The translator emits `emitc.apply` incorrectly when the op is part of an expression, as it prints the name of the operand instead of calling emitOperand() which takes into account the expression being emitted, leaving out the part of the expression feeding this op, e.g.
```mlir
func.func @<!-- -->foo(%a: i32, %p: !emitc.ptr<i32>) -> i32 {
  %c = emitc.expression : i32 {
    %e = emitc.sub %p, %a : (!emitc.ptr<i32>, i32) -> !emitc.ptr<i32>
    %d = emitc.apply "*"(%e) : (!emitc.ptr<i32>) -> i32
    emitc.yield %d : i32
  }
  return %c : i32
}
```
translates to:
```C
int32_t foo(int32_t v1, int32_t* v2) {
  int32_t v3 = *v4;
  return v3;
}
```
instead of:
```C
int32_t foo(int32_t v1, int32_t* v2) {
  int32_t v3 = *(v2 - v1);
  return v3;
}
```

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


2 Files Affected:

- (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (+1-3) 
- (modified) mlir/test/Target/Cpp/expressions.mlir (+19) 


``````````diff
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index a5ee64c7fe38b..704a8c6edf8aa 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -782,9 +782,7 @@ static LogicalResult printOperation(CppEmitter &emitter,
   if (failed(emitter.emitAssignPrefix(op)))
     return failure();
   os << applyOp.getApplicableOperator();
-  os << emitter.getOrCreateName(applyOp.getOperand());
-
-  return success();
+  return emitter.emitOperand(applyOp.getOperand());
 }
 
 static LogicalResult printOperation(CppEmitter &emitter,
diff --git a/mlir/test/Target/Cpp/expressions.mlir b/mlir/test/Target/Cpp/expressions.mlir
index 9316d7b77619b..857b162f0bd8b 100644
--- a/mlir/test/Target/Cpp/expressions.mlir
+++ b/mlir/test/Target/Cpp/expressions.mlir
@@ -295,6 +295,25 @@ func.func @different_expressions(%arg0: i32, %arg1: i32, %arg2: i32, %arg3: i32)
   return %v_load : i32
 }
 
+// CPP-DEFAULT:      int32_t expression_with_dereference(int32_t [[VAL_1:v[0-9]+]], int32_t* [[VAL_2]]) {
+// CPP-DEFAULT-NEXT:   int32_t [[VAL_3:v[0-9]+]] = *([[VAL_2]] - [[VAL_1]]);
+// CPP-DEFAULT-NEXT:   return [[VAL_3]];
+// CPP-DEFAULT-NEXT: }
+
+// CPP-DECLTOP:      int32_t expression_with_dereference(int32_t [[VAL_1:v[0-9]+]], int32_t* [[VAL_2]]) {
+// CPP-DECLTOP-NEXT:   int32_t [[VAL_3:v[0-9]+]];
+// CPP-DECLTOP-NEXT:   [[VAL_3]] = *([[VAL_2]] - [[VAL_1]]);
+// CPP-DECLTOP-NEXT:   return [[VAL_3]];
+// CPP-DECLTOP-NEXT: }
+func.func @expression_with_dereference(%arg1: i32, %arg2: !emitc.ptr<i32>) -> i32 {
+  %c = emitc.expression : i32 {
+    %e = emitc.sub %arg2, %arg1 : (!emitc.ptr<i32>, i32) -> !emitc.ptr<i32>
+    %d = emitc.apply "*"(%e) : (!emitc.ptr<i32>) -> i32
+    emitc.yield %d : i32
+  }
+  return %c : i32
+}
+
 // CPP-DEFAULT:      bool expression_with_address_taken(int32_t [[VAL_1:v[0-9]+]], int32_t [[VAL_2:v[0-9]+]], int32_t* [[VAL_3]]) {
 // CPP-DEFAULT-NEXT:   int32_t [[VAL_4:v[0-9]+]] = 42;
 // CPP-DEFAULT-NEXT:   return &[[VAL_4]] - [[VAL_2]] < [[VAL_3]];

``````````

</details>


https://github.com/llvm/llvm-project/pull/155171


More information about the Mlir-commits mailing list