Skip to content

Commit 94cd19d

Browse files
committed
[CIR][Transforms] Fix flattening for TernaryOp to properly handle locations
1 parent 4fcb3ac commit 94cd19d

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-3
lines changed

clang/lib/CIR/Dialect/Transforms/FlattenCFG.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -438,9 +438,13 @@ class CIRTernaryOpFlattening
438438
auto *condBlock = rewriter.getInsertionBlock();
439439
auto opPosition = rewriter.getInsertionPoint();
440440
auto *remainingOpsBlock = rewriter.splitBlock(condBlock, opPosition);
441-
auto *continueBlock = rewriter.createBlock(
442-
remainingOpsBlock, op->getResultTypes(),
443-
SmallVector<mlir::Location>(/* result number always 1 */ 1, loc));
441+
SmallVector<mlir::Location, 2> locs;
442+
// Ternary result is optional, make sure to populate the location only
443+
// when relevant.
444+
if (op->getResultTypes().size())
445+
locs.push_back(loc);
446+
auto *continueBlock =
447+
rewriter.createBlock(remainingOpsBlock, op->getResultTypes(), locs);
444448
rewriter.create<mlir::cir::BrOp>(loc, remainingOpsBlock);
445449

446450
auto &trueRegion = op.getTrueRegion();

clang/test/CIR/Transforms/ternary.cir

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,25 @@ module {
4444
// CHECK: cir.return %8 : !s32i
4545
// CHECK: }
4646

47+
cir.func @foo2(%arg0: !cir.bool) {
48+
cir.ternary(%arg0, true {
49+
cir.yield
50+
}, false {
51+
cir.yield
52+
}) : (!cir.bool) -> ()
53+
cir.return
54+
}
55+
56+
// CHECK: cir.func @foo2(%arg0: !cir.bool) {
57+
// CHECK: cir.brcond %arg0 ^bb1, ^bb2
58+
// CHECK: ^bb1: // pred: ^bb0
59+
// CHECK: cir.br ^bb3
60+
// CHECK: ^bb2: // pred: ^bb0
61+
// CHECK: cir.br ^bb3
62+
// CHECK: ^bb3: // 2 preds: ^bb1, ^bb2
63+
// CHECK: cir.br ^bb4
64+
// CHECK: ^bb4: // pred: ^bb3
65+
// CHECK: cir.return
66+
// CHECK: }
67+
4768
}

0 commit comments

Comments
 (0)