[Mlir-commits] [mlir] [MLIR][EmitC][cf] Bugfix: correctly inline emitc.expression op in the emitted if condition of a cf.cond_br (PR #128958)
llvmlistbot at llvm.org
llvmlistbot at llvm.org
Wed Feb 26 15:22:09 PST 2025
llvmbot wrote:
<!--LLVM PR SUMMARY COMMENT-->
@llvm/pr-subscribers-mlir
Author: None (gdehame)
<details>
<summary>Changes</summary>
[MLIR][EmitC][cf] Bugfix: correctly inline emitc.expression op in the emitted if condition of a cf.cond_br
Running `mlir-translate -mlir-to-cpp -declare-variables-at-top input.mlir` with `input.mlir` as
```
module {
emitc.func @<!-- -->f(%0 : i32, %1 : i32) {
%2 = expression : i1 {
%3 = cmp lt, %0, %1 : (i32, i32) -> i1
yield %3 : i1
}
cf.cond_br %2, ^bb1, ^bb1
^bb1: // 2 preds: ^bb0, ^bb0
return
}
}
```
doesn't inline the expression %2 and generates a use of an undeclared variable in the generated if:
```
void f(int32_t v1, int32_t v2) {
if (v3) {
goto label2;
} else {
goto label2;
}
label2:
return;
}
```
---
Full diff: https://github.com/llvm/llvm-project/pull/128958.diff
2 Files Affected:
- (modified) mlir/lib/Target/Cpp/TranslateToCpp.cpp (+4-2)
- (modified) mlir/test/Target/Cpp/control_flow.mlir (+19)
``````````diff
diff --git a/mlir/lib/Target/Cpp/TranslateToCpp.cpp b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
index abff252575eb0..b00820ffc542b 100644
--- a/mlir/lib/Target/Cpp/TranslateToCpp.cpp
+++ b/mlir/lib/Target/Cpp/TranslateToCpp.cpp
@@ -613,8 +613,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
Block &trueSuccessor = *condBranchOp.getTrueDest();
Block &falseSuccessor = *condBranchOp.getFalseDest();
- os << "if (" << emitter.getOrCreateName(condBranchOp.getCondition())
- << ") {\n";
+ os << "if (";
+ if (failed(emitter.emitOperand(condBranchOp.getCondition())))
+ return failure();
+ os << ") {\n";
os.indent();
diff --git a/mlir/test/Target/Cpp/control_flow.mlir b/mlir/test/Target/Cpp/control_flow.mlir
index 436543f7ace95..101b30c2521c9 100644
--- a/mlir/test/Target/Cpp/control_flow.mlir
+++ b/mlir/test/Target/Cpp/control_flow.mlir
@@ -68,3 +68,22 @@ func.func @block_labels1() {
// CPP-DECLTOP-NEXT: label2:
// CPP-DECLTOP-NEXT: return;
// CPP-DECLTOP-NEXT: }
+
+emitc.func @expression_inlining(%0 : i32, %1 : i32) {
+ %2 = expression : i1 {
+ %3 = cmp lt, %0, %1 : (i32, i32) -> i1
+ yield %3 : i1
+ }
+ cf.cond_br %2, ^bb1, ^bb1
+ ^bb1: // 2 preds: ^bb0, ^bb0
+ return
+}
+// CPP-DECLTOP: void expression_inlining(int32_t [[v1:v.*]], int32_t [[v2:v.*]]) {
+// CPP-DECLTOP-NEXT: if ([[v1]] < [[v2]]) {
+// CPP-DECLTOP-NEXT: goto label2;
+// CPP-DECLTOP-NEXT: } else {
+// CPP-DECLTOP-NEXT: goto label2;
+// CPP-DECLTOP-NEXT: }
+// CPP-DECLTOP-NEXT: label2:
+// CPP-DECLTOP-NEXT: return;
+// CPP-DECLTOP-NEXT: }
``````````
</details>
https://github.com/llvm/llvm-project/pull/128958
More information about the Mlir-commits
mailing list