Skip to content

Commit 2f50a99

Browse files
[CIR] Fix error in cir.label when using cir.br in entry block (llvm#163280)
This PR fixes an error I found while working on cir.indirectbr. The issue occurs when a branching operator points to the entry block LLVM’s verifier does not allow this. This PR is the same as the one I submitted in the incubator:llvm/clangir#1939
1 parent f0b701d commit 2f50a99

File tree

3 files changed

+53
-8
lines changed

3 files changed

+53
-8
lines changed

clang/lib/CIR/CodeGen/CIRGenStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -536,7 +536,7 @@ mlir::LogicalResult CIRGenFunction::emitLabel(const clang::LabelDecl &d) {
536536
mlir::Block *currBlock = builder.getBlock();
537537
mlir::Block *labelBlock = currBlock;
538538

539-
if (!currBlock->empty()) {
539+
if (!currBlock->empty() || currBlock->isEntryBlock()) {
540540
{
541541
mlir::OpBuilder::InsertionGuard guard(builder);
542542
labelBlock = builder.createBlock(builder.getBlock()->getParent());

clang/test/CIR/CodeGen/goto.cpp

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,6 +205,8 @@ extern "C" void case_follow_label(int v) {
205205
// CIR: cir.func dso_local @case_follow_label
206206
// CIR: cir.switch
207207
// CIR: cir.case(equal, [#cir.int<1> : !s32i]) {
208+
// CIR: cir.br ^bb1
209+
// CIR: ^bb1:
208210
// CIR: cir.label "label"
209211
// CIR: cir.case(equal, [#cir.int<2> : !s32i]) {
210212
// CIR: cir.call @action1()
@@ -215,9 +217,11 @@ extern "C" void case_follow_label(int v) {
215217

216218
// LLVM: define dso_local void @case_follow_label
217219
// LLVM: switch i32 {{.*}}, label %[[SWDEFAULT:.*]] [
218-
// LLVM: i32 1, label %[[LABEL:.*]]
220+
// LLVM: i32 1, label %[[CASE1:.*]]
219221
// LLVM: i32 2, label %[[CASE2:.*]]
220222
// LLVM: ]
223+
// LLVM: [[CASE1]]:
224+
// LLVM: br label %[[LABEL:.*]]
221225
// LLVM: [[LABEL]]:
222226
// LLVM: br label %[[CASE2]]
223227
// LLVM: [[CASE2]]:
@@ -303,3 +307,24 @@ extern "C" void default_follow_label(int v) {
303307
// OGCG: br label %label
304308
// OGCG: sw.epilog:
305309
// OGCG: ret void
310+
311+
void g3() {
312+
label:
313+
goto label;
314+
}
315+
316+
// CIR: cir.func dso_local @_Z2g3v
317+
// CIR: cir.br ^bb1
318+
// CIR: ^bb1:
319+
// CIR: cir.label "label"
320+
// CIR: cir.goto "label"
321+
322+
// LLVM: define dso_local void @_Z2g3v()
323+
// LLVM: br label %1
324+
// LLVM: 1:
325+
// LLVM: br label %1
326+
327+
// OGCG: define dso_local void @_Z2g3v()
328+
// OGCG: br label %label
329+
// OGCG: label:
330+
// OGCG: br label %label

clang/test/CIR/CodeGen/label.c

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,14 @@ void label() {
1111
}
1212

1313
// CIR: cir.func no_proto dso_local @label
14+
// CIR: cir.br ^bb1
15+
// CIR: ^bb1:
1416
// CIR: cir.label "labelA"
1517
// CIR: cir.return
1618

1719
// LLVM:define dso_local void @label
20+
// LLVM: br label %1
21+
// LLVM: 1:
1822
// LLVM: ret void
1923

2024
// OGCG: define dso_local void @label
@@ -29,15 +33,19 @@ void multiple_labels() {
2933
}
3034

3135
// CIR: cir.func no_proto dso_local @multiple_labels
32-
// CIR: cir.label "labelB"
3336
// CIR: cir.br ^bb1
34-
// CIR: ^bb1: // pred: ^bb0
37+
// CIR: ^bb1:
38+
// CIR: cir.label "labelB"
39+
// CIR: cir.br ^bb2
40+
// CIR: ^bb2:
3541
// CIR: cir.label "labelC"
3642
// CIR: cir.return
3743

3844
// LLVM: define dso_local void @multiple_labels()
3945
// LLVM: br label %1
4046
// LLVM: 1:
47+
// LLVM: br label %2
48+
// LLVM: 2:
4149
// LLVM: ret void
4250

4351
// OGCG: define dso_local void @multiple_labels
@@ -56,6 +64,8 @@ void label_in_if(int cond) {
5664

5765
// CIR: cir.func dso_local @label_in_if
5866
// CIR: cir.if {{.*}} {
67+
// CIR: cir.br ^bb1
68+
// CIR: ^bb1:
5969
// CIR: cir.label "labelD"
6070
// CIR: [[LOAD:%.*]] = cir.load align(4) [[COND:%.*]] : !cir.ptr<!s32i>, !s32i
6171
// CIR: [[INC:%.*]] = cir.unary(inc, %3) nsw : !s32i, !s32i
@@ -68,15 +78,17 @@ void label_in_if(int cond) {
6878
// LLVM: 3:
6979
// LLVM: [[LOAD:%.*]] = load i32, ptr [[COND:%.*]], align 4
7080
// LLVM: [[CMP:%.*]] = icmp ne i32 [[LOAD]], 0
71-
// LLVM: br i1 [[CMP]], label %6, label %9
81+
// LLVM: br i1 [[CMP]], label %6, label %10
7282
// LLVM: 6:
83+
// LLVM: br label %7
84+
// LLVM: 7:
7385
// LLVM: [[LOAD2:%.*]] = load i32, ptr [[COND]], align 4
7486
// LLVM: [[ADD1:%.*]] = add nsw i32 [[LOAD2]], 1
7587
// LLVM: store i32 [[ADD1]], ptr [[COND]], align 4
76-
// LLVM: br label %9
77-
// LLVM: 9:
7888
// LLVM: br label %10
7989
// LLVM: 10:
90+
// LLVM: br label %11
91+
// LLVM: 11:
8092
// LLVM: ret void
8193

8294
// OGCG: define dso_local void @label_in_if
@@ -142,11 +154,15 @@ void labelWithoutMatch() {
142154
return;
143155
}
144156
// CIR: cir.func no_proto dso_local @labelWithoutMatch
157+
// CIR: cir.br ^bb1
158+
// CIR: ^bb1:
145159
// CIR: cir.label "end"
146160
// CIR: cir.return
147161
// CIR: }
148162

149163
// LLVM: define dso_local void @labelWithoutMatch
164+
// LLVM: br label %1
165+
// LLVM: 1:
150166
// LLVM: ret void
151167

152168
// OGCG: define dso_local void @labelWithoutMatch
@@ -167,13 +183,17 @@ void foo() {
167183

168184
// CIR: cir.func no_proto dso_local @foo
169185
// CIR: cir.scope {
170-
// CIR: cir.label "label"
171186
// CIR: %0 = cir.alloca !rec_S, !cir.ptr<!rec_S>, ["agg.tmp0"]
187+
// CIR: cir.br ^bb1
188+
// CIR: ^bb1:
189+
// CIR: cir.label "label"
172190

173191
// LLVM:define dso_local void @foo() {
174192
// LLVM: [[ALLOC:%.*]] = alloca %struct.S, i64 1, align 1
175193
// LLVM: br label %2
176194
// LLVM:2:
195+
// LLVM: br label %3
196+
// LLVM:3:
177197
// LLVM: [[CALL:%.*]] = call %struct.S @get()
178198
// LLVM: store %struct.S [[CALL]], ptr [[ALLOC]], align 1
179199
// LLVM: [[LOAD:%.*]] = load %struct.S, ptr [[ALLOC]], align 1

0 commit comments

Comments
 (0)