Skip to content

Commit c94bd96

Browse files
authored
[Clang][CodeGen] Don't emit assumptions if current block is unreachable. (#106936)
Fixes #106898. When emitting an infinite loop, clang codegen will delete the whole block and leave builder's current block as nullptr: https://github.com/llvm/llvm-project/blob/837ee5b46a5f7f898f0de7e46a19600b896a0a1f/clang/lib/CodeGen/CGStmt.cpp#L597-L600 Then clang will create `zext (icmp slt %a, %b)` without parent block for `a < b`. It will crash here: https://github.com/llvm/llvm-project/blob/837ee5b46a5f7f898f0de7e46a19600b896a0a1f/clang/lib/CodeGen/CGExprScalar.cpp#L416-L420 Even if we disabled this optimization, it still crashes in `Builder.CreateAssumption`: https://github.com/llvm/llvm-project/blob/837ee5b46a5f7f898f0de7e46a19600b896a0a1f/llvm/lib/IR/IRBuilder.cpp#L551-L561 This patch disables assumptions emission if current block is null.
1 parent 6c607cf commit c94bd96

File tree

2 files changed

+10
-1
lines changed

2 files changed

+10
-1
lines changed

clang/lib/CodeGen/CGStmt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -752,7 +752,7 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
752752
} break;
753753
case attr::CXXAssume: {
754754
const Expr *Assumption = cast<CXXAssumeAttr>(A)->getAssumption();
755-
if (getLangOpts().CXXAssumptions &&
755+
if (getLangOpts().CXXAssumptions && Builder.GetInsertBlock() &&
756756
!Assumption->HasSideEffects(getContext())) {
757757
llvm::Value *AssumptionVal = EvaluateExprAsBool(Assumption);
758758
Builder.CreateAssumption(AssumptionVal);

clang/test/SemaCXX/cxx23-assume.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -158,3 +158,12 @@ foo (int x, int y)
158158
return x + y;
159159
}
160160
}
161+
162+
// Do not crash when assumptions are unreachable.
163+
namespace gh106898 {
164+
int foo () {
165+
while(1);
166+
int a = 0, b = 1;
167+
__attribute__((assume (a < b)));
168+
}
169+
}

0 commit comments

Comments
 (0)