Skip to content

Commit e481943

Browse files
authoredFeb 28, 2025
[MLIR][EmitC][cf] Bugfix: correctly inline emitc.expression op in the emitted if condition of a cf.cond_br (#128958)
emitc.expression ops are expected to be inlined in the if condition in the lowering of cf.cond_br if this is their only use but they weren't inlined. Instead, a use of the variable corresponding to the expression result was generated but with no declaration/definition.
1 parent 5d89123 commit e481943

File tree

2 files changed

+23
-2
lines changed

2 files changed

+23
-2
lines changed
 

‎mlir/lib/Target/Cpp/TranslateToCpp.cpp

+4-2
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

+19
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)
Please sign in to comment.