Skip to content

Commit 71b21b5

Browse files
authored
[CIR] Move alloca from cir.try to the surrounding cir.scope (#164488)
Upstream moving the allocas from cir.try block to the surrounding cir.scope Issue #154992
1 parent 213b8a9 commit 71b21b5

File tree

2 files changed

+52
-1
lines changed

2 files changed

+52
-1
lines changed

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2065,7 +2065,11 @@ mlir::Value CIRGenFunction::emitAlloca(StringRef name, mlir::Type ty,
20652065
// a surrounding cir.scope, make sure the alloca ends up in the surrounding
20662066
// scope instead. This is necessary in order to guarantee all SSA values are
20672067
// reachable during cleanups.
2068-
assert(!cir::MissingFeatures::tryOp());
2068+
if (auto tryOp =
2069+
llvm::dyn_cast_if_present<cir::TryOp>(entryBlock->getParentOp())) {
2070+
if (auto scopeOp = llvm::dyn_cast<cir::ScopeOp>(tryOp->getParentOp()))
2071+
entryBlock = &scopeOp.getScopeRegion().front();
2072+
}
20692073

20702074
return emitAlloca(name, ty, loc, alignment,
20712075
builder.getBestAllocaInsertPoint(entryBlock), arraySize);

clang/test/CIR/CodeGen/try-catch.cpp

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,3 +117,50 @@ void try_catch_with_empty_catch_all_2() {
117117
// OGCG: %[[RESULT:.*]] = add nsw i32 %[[TMP_A]], 1
118118
// OGCG: store i32 %[[RESULT]], ptr %[[A_ADDR]], align 4
119119
// OGCG: ret void
120+
121+
void try_catch_with_alloca() {
122+
try {
123+
int a;
124+
int b;
125+
int c = a + b;
126+
} catch (...) {
127+
}
128+
}
129+
130+
// CIR: cir.scope {
131+
// CIR: %[[A_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["a"]
132+
// CIR: %[[B_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["b"]
133+
// CIR: %[[C_ADDR:.*]] = cir.alloca !s32i, !cir.ptr<!s32i>, ["c", init]
134+
// CIR: cir.try {
135+
// CIR: %[[TMP_A:.*]] = cir.load{{.*}} %[[A_ADDR]] : !cir.ptr<!s32i>, !s32i
136+
// CIR: %[[TMP_B:.*]] = cir.load{{.*}} %[[B_ADDR]] : !cir.ptr<!s32i>, !s32i
137+
// CIR: %[[RESULT:.*]] = cir.binop(add, %[[TMP_A]], %[[TMP_B]]) nsw : !s32i
138+
// CIR: cir.store{{.*}} %[[RESULT]], %[[C_ADDR]] : !s32i, !cir.ptr<!s32i>
139+
// CIR: cir.yield
140+
// CIR: }
141+
// CIR: }
142+
143+
// LLVM: %[[A_ADDR:.*]] = alloca i32, i64 1, align 4
144+
// LLVM: %[[B_ADDR:.*]] = alloca i32, i64 1, align 4
145+
// LLVM: %[[C_ADDR:.*]] = alloca i32, i64 1, align 4
146+
// LLVM: br label %[[LABEL_1:.*]]
147+
// LLVM: [[LABEL_1]]:
148+
// LLVM: br label %[[LABEL_2:.*]]
149+
// LLVM: [[LABEL_2]]:
150+
// LLVM: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
151+
// LLVM: %[[TMP_B:.*]] = load i32, ptr %[[B_ADDR]], align 4
152+
// LLVM: %[[RESULT:.*]] = add nsw i32 %[[TMP_A]], %[[TMP_B]]
153+
// LLVM: store i32 %[[RESULT]], ptr %[[C_ADDR]], align 4
154+
// LLVM: br label %[[LABEL_3:.*]]
155+
// LLVM: [[LABEL_3]]:
156+
// LLVM: br label %[[LABEL_4:.*]]
157+
// LLVM: [[LABEL_4]]:
158+
// LLVM: ret void
159+
160+
// OGCG: %[[A_ADDR:.*]] = alloca i32, align 4
161+
// OGCG: %[[B_ADDR:.*]] = alloca i32, align 4
162+
// OGCG: %[[C_ADDR:.*]] = alloca i32, align 4
163+
// OGCG: %[[TMP_A:.*]] = load i32, ptr %[[A_ADDR]], align 4
164+
// OGCG: %[[TMP_B:.*]] = load i32, ptr %[[B_ADDR]], align 4
165+
// OGCG: %[[RESULT:.*]] = add nsw i32 %[[TMP_A]], %[[TMP_B]]
166+
// OGCG: store i32 %[[RESULT]], ptr %[[C_ADDR]], align 4

0 commit comments

Comments
 (0)