Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[CIR][CIRGen] Create a new block after break and continue #611

Merged
merged 1 commit into from
Jun 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 10 additions & 11 deletions clang/lib/CIR/CodeGen/CIRGenStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -551,20 +551,11 @@ mlir::LogicalResult CIRGenFunction::buildGotoStmt(const GotoStmt &S) {
// info support just yet, look at this again once we have it.
assert(builder.getInsertionBlock() && "not yet implemented");

mlir::Block *currBlock = builder.getBlock();
mlir::Block *gotoBlock = currBlock;
if (!currBlock->empty() &&
currBlock->back().hasTrait<mlir::OpTrait::IsTerminator>()) {
gotoBlock = builder.createBlock(builder.getBlock()->getParent());
builder.setInsertionPointToEnd(gotoBlock);
}
Comment on lines -554 to -560
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part of code was used for test shouldCreateBlkForGoto in clang/test/CIR/CodeGen/goto.cpp. However, it is unrelated to goto, but a workaround for the broken code for break. This patch also moves the test to switch.cpp and renamed it to unreachable_after_break_1.


// A goto marks the end of a block, create a new one for codegen after
// buildGotoStmt can resume building in that block.

builder.create<mlir::cir::GotoOp>(getLoc(S.getSourceRange()),
S.getLabel()->getName());

// A goto marks the end of a block, create a new one for codegen after
// buildGotoStmt can resume building in that block.
// Insert the new block to continue codegen after goto.
builder.createBlock(builder.getBlock()->getParent());

Expand Down Expand Up @@ -597,11 +588,19 @@ mlir::LogicalResult CIRGenFunction::buildLabel(const LabelDecl *D) {
mlir::LogicalResult
CIRGenFunction::buildContinueStmt(const clang::ContinueStmt &S) {
builder.createContinue(getLoc(S.getContinueLoc()));

// Insert the new block to continue codegen after the continue statement.
builder.createBlock(builder.getBlock()->getParent());

return mlir::success();
}

mlir::LogicalResult CIRGenFunction::buildBreakStmt(const clang::BreakStmt &S) {
builder.createBreak(getLoc(S.getBreakLoc()));

// Insert the new block to continue codegen after the break statement.
builder.createBlock(builder.getBlock()->getParent());

return mlir::success();
}

Expand Down
20 changes: 0 additions & 20 deletions clang/test/CIR/CodeGen/goto.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -101,26 +101,6 @@ int shouldGenBranch(int x) {
// NOFLAT: ^bb1:
// NOFLAT: cir.label "err"

int shouldCreateBlkForGoto(int a) {
switch (a) {
case(42):
break;
goto exit;
default:
return 0;
};

exit:
return -1;

}
// NOFLAT: cir.func @_Z22shouldCreateBlkForGotoi
// NOFLAT: case (equal, 42) {
// NOFLAT: cir.break
// NOFLAT: ^bb1: // no predecessors
// NOFLAT: cir.goto "exit"
// NOFLAT: }

void severalLabelsInARow(int a) {
int b = a;
goto end1;
Expand Down
52 changes: 52 additions & 0 deletions clang/test/CIR/CodeGen/loop.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -205,3 +205,55 @@ void l6() {
// CHECK-NEXT: }
// CHECK-NEXT: cir.return
// CHECK-NEXT: }

void unreachable_after_break() {
for (;;) {
break;
int x = 1;
}
}

// CHECK-NEXT: cir.func @_Z23unreachable_after_breakv()
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init] {alignment = 4 : i64}
// CHECK-NEXT: cir.for : cond {
// CHECK-NEXT: %1 = cir.const #true
// CHECK-NEXT: cir.condition(%1)
// CHECK-NEXT: } body {
// CHECK-NEXT: cir.break
// CHECK-NEXT: ^bb1: // no predecessors
// CHECK-NEXT: %1 = cir.const #cir.int<1> : !s32i
// CHECK-NEXT: cir.store %1, %0 : !s32i, !cir.ptr<!s32i>
// CHECK-NEXT: cir.yield
// CHECK-NEXT: } step {
// CHECK-NEXT: cir.yield
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: cir.return
// CHECK-NEXT: }

void unreachable_after_continue() {
for (;;) {
continue;
int x = 1;
}
}

// CHECK-NEXT: cir.func @_Z26unreachable_after_continuev()
// CHECK-NEXT: cir.scope {
// CHECK-NEXT: %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["x", init] {alignment = 4 : i64}
// CHECK-NEXT: cir.for : cond {
// CHECK-NEXT: %1 = cir.const #true
// CHECK-NEXT: cir.condition(%1)
// CHECK-NEXT: } body {
// CHECK-NEXT: cir.continue
// CHECK-NEXT: ^bb1: // no predecessors
// CHECK-NEXT: %1 = cir.const #cir.int<1> : !s32i
// CHECK-NEXT: cir.store %1, %0 : !s32i, !cir.ptr<!s32i>
// CHECK-NEXT: cir.yield
// CHECK-NEXT: } step {
// CHECK-NEXT: cir.yield
// CHECK-NEXT: }
// CHECK-NEXT: }
// CHECK-NEXT: cir.return
// CHECK-NEXT: }
19 changes: 19 additions & 0 deletions clang/test/CIR/CodeGen/return.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,22 @@ int &ret0(int &x) {
// CHECK: cir.store %2, %1 : !cir.ptr<!s32i>, !cir.ptr<!cir.ptr<!s32i>>
// CHECK: %3 = cir.load %1 : !cir.ptr<!cir.ptr<!s32i>>, !cir.ptr<!s32i>
// CHECK: cir.return %3 : !cir.ptr<!s32i>

int unreachable_after_return() {
return 0;
return 1;
}

// CHECK: cir.func @_Z24unreachable_after_returnv
// CHECK-NEXT: %0 = cir.alloca !s32i, !cir.ptr<!s32i>, ["__retval"] {alignment = 4 : i64}
// CHECK-NEXT: %1 = cir.const #cir.int<0> : !s32i
// CHECK-NEXT: cir.store %1, %0 : !s32i, !cir.ptr<!s32i>
// CHECK-NEXT: cir.br ^bb1
// CHECK-NEXT: ^bb1: // 2 preds: ^bb0, ^bb2
// CHECK-NEXT: %2 = cir.load %0 : !cir.ptr<!s32i>, !s32i
// CHECK-NEXT: cir.return %2 : !s32i
// CHECK-NEXT: ^bb2: // no predecessors
// CHECK-NEXT: %3 = cir.const #cir.int<1> : !s32i
// CHECK-NEXT: cir.store %3, %0 : !s32i, !cir.ptr<!s32i>
// CHECK-NEXT: cir.br ^bb1
// CHECK-NEXT: }
22 changes: 22 additions & 0 deletions clang/test/CIR/CodeGen/switch-unreachable-after-break.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -fclangir -emit-cir %s -o %t.cir
// RUN: FileCheck --input-file=%t.cir %s
// XFAIL: *
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These tests are failing because the insertion point is restored by an InsertionGuard in buildSwitchBody(). I suggest revising that part after #528 is merged to avoid conflict.

} else if (lastCaseBlock) {
// This means it's a random stmt following up a case, just
// emit it as part of previous known case.
mlir::OpBuilder::InsertionGuard guardCase(builder);
builder.setInsertionPointToEnd(lastCaseBlock);
res = buildStmt(c, /*useCurrentScope=*/!isa<CompoundStmt>(c));
} else {


void unreachable_after_break(int a) {
switch(a) {
case 0:
break;
break;
int x = 1;
}
}

int unreachable_after_return(int a) {
switch (a) {
case 0:
return 0;
return 1;
int x = 1;
}
return 2;
}
20 changes: 20 additions & 0 deletions clang/test/CIR/CodeGen/switch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -327,3 +327,23 @@ void fallthrough(int x) {
// CHECK-NEXT: }
// CHECK-NEXT: ]
// CHECK-NEXT: }

int unreachable_after_break_1(int a) {
switch (a) {
case(42):
break;
goto exit;
default:
return 0;
};

exit:
return -1;

}
// CHECK: cir.func @_Z25unreachable_after_break_1i
// CHECK: case (equal, 42) {
// CHECK: cir.break
// CHECK: ^bb1: // no predecessors
// CHECK: cir.goto "exit"
// CHECK: }
Loading