Skip to content

Commit 2ea15db

Browse files
gitolegbcardosolopes
authored andcommitted
[CIR][CodeGen][BugFix] don't place alloca before the label (#875)
This PR fixes the case, when a temporary var is used, and `alloca` operation is inserted in the block start before the `label` operation. Implementation: when we search for the `alloca` place in a block, we take label operations into account as well. Fix #870 --------- Co-authored-by: Bruno Cardoso Lopes <[email protected]>
1 parent f713df2 commit 2ea15db

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

clang/include/clang/CIR/Dialect/Builder/CIRBaseBuilder.h

+4-4
Original file line numberDiff line numberDiff line change
@@ -538,14 +538,14 @@ class CIRBaseBuilderTy : public mlir::OpBuilder {
538538
// ----------------------
539539
//
540540
OpBuilder::InsertPoint getBestAllocaInsertPoint(mlir::Block *block) {
541-
auto lastAlloca =
541+
auto last =
542542
std::find_if(block->rbegin(), block->rend(), [](mlir::Operation &op) {
543-
return mlir::isa<mlir::cir::AllocaOp>(&op);
543+
return mlir::isa<mlir::cir::AllocaOp, mlir::cir::LabelOp>(&op);
544544
});
545545

546-
if (lastAlloca != block->rend())
546+
if (last != block->rend())
547547
return OpBuilder::InsertPoint(block,
548-
++mlir::Block::iterator(&*lastAlloca));
548+
++mlir::Block::iterator(&*last));
549549
return OpBuilder::InsertPoint(block, block->begin());
550550
};
551551

clang/test/CIR/CodeGen/goto.cpp

+17-1
Original file line numberDiff line numberDiff line change
@@ -271,4 +271,20 @@ void flatLoopWithNoTerminatorInFront(int* ptr) {
271271
// CHECK: ^bb[[#RETURN]]:
272272
// CHECK: cir.return
273273
// CHECK: }
274-
// CHECK:}
274+
// CHECK:}
275+
276+
struct S {};
277+
struct S get();
278+
void bar(struct S);
279+
280+
void foo() {
281+
{
282+
label:
283+
bar(get());
284+
}
285+
}
286+
287+
// NOFLAT: cir.func @_Z3foov()
288+
// NOFLAT: cir.scope {
289+
// NOFLAT: cir.label "label"
290+
// NOFLAT: %0 = cir.alloca !ty_S, !cir.ptr<!ty_S>, ["agg.tmp0"]

0 commit comments

Comments
 (0)