Skip to content

Commit 0ebf44c

Browse files
committed
[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; } ```
1 parent ff80bdc commit 0ebf44c

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed

mlir/lib/Target/Cpp/TranslateToCpp.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,10 @@ static LogicalResult printOperation(CppEmitter &emitter,
613613
Block &trueSuccessor = *condBranchOp.getTrueDest();
614614
Block &falseSuccessor = *condBranchOp.getFalseDest();
615615

616-
os << "if (" << emitter.getOrCreateName(condBranchOp.getCondition())
617-
<< ") {\n";
616+
os << "if (";
617+
if (failed(emitter.emitOperand(condBranchOp.getCondition())))
618+
return failure();
619+
os << ") {\n";
618620

619621
os.indent();
620622

mlir/test/Target/Cpp/control_flow.mlir

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,22 @@ func.func @block_labels1() {
6868
// CPP-DECLTOP-NEXT: label2:
6969
// CPP-DECLTOP-NEXT: return;
7070
// CPP-DECLTOP-NEXT: }
71+
72+
emitc.func @expression_inlining(%0 : i32, %1 : i32) {
73+
%2 = expression : i1 {
74+
%3 = cmp lt, %0, %1 : (i32, i32) -> i1
75+
yield %3 : i1
76+
}
77+
cf.cond_br %2, ^bb1, ^bb1
78+
^bb1: // 2 preds: ^bb0, ^bb0
79+
return
80+
}
81+
// CPP-DECLTOP: void expression_inlining(int32_t [[v1:v.*]], int32_t [[v2:v.*]]) {
82+
// CPP-DECLTOP-NEXT: if ([[v1]] < [[v2]]) {
83+
// CPP-DECLTOP-NEXT: goto label2;
84+
// CPP-DECLTOP-NEXT: } else {
85+
// CPP-DECLTOP-NEXT: goto label2;
86+
// CPP-DECLTOP-NEXT: }
87+
// CPP-DECLTOP-NEXT: label2:
88+
// CPP-DECLTOP-NEXT: return;
89+
// CPP-DECLTOP-NEXT: }

0 commit comments

Comments
 (0)