Skip to content

Commit 5cf7845

Browse files
dtcxzywtru
authored andcommitted
[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. (cherry picked from commit c94bd96)
1 parent 82a11e4 commit 5cf7845

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
@@ -745,7 +745,7 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
745745
} break;
746746
case attr::CXXAssume: {
747747
const Expr *Assumption = cast<CXXAssumeAttr>(A)->getAssumption();
748-
if (getLangOpts().CXXAssumptions &&
748+
if (getLangOpts().CXXAssumptions && Builder.GetInsertBlock() &&
749749
!Assumption->HasSideEffects(getContext())) {
750750
llvm::Value *AssumptionVal = EvaluateExprAsBool(Assumption);
751751
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)