Skip to content

Commit

Permalink
[Clang][CodeGen] Don't emit assumptions if current block is unreachab…
Browse files Browse the repository at this point in the history
…le. (#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.
  • Loading branch information
dtcxzyw committed Sep 4, 2024
1 parent 6c607cf commit c94bd96
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 1 deletion.
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGStmt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -752,7 +752,7 @@ void CodeGenFunction::EmitAttributedStmt(const AttributedStmt &S) {
} break;
case attr::CXXAssume: {
const Expr *Assumption = cast<CXXAssumeAttr>(A)->getAssumption();
if (getLangOpts().CXXAssumptions &&
if (getLangOpts().CXXAssumptions && Builder.GetInsertBlock() &&
!Assumption->HasSideEffects(getContext())) {
llvm::Value *AssumptionVal = EvaluateExprAsBool(Assumption);
Builder.CreateAssumption(AssumptionVal);
Expand Down
9 changes: 9 additions & 0 deletions clang/test/SemaCXX/cxx23-assume.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -158,3 +158,12 @@ foo (int x, int y)
return x + y;
}
}

// Do not crash when assumptions are unreachable.
namespace gh106898 {
int foo () {
while(1);
int a = 0, b = 1;
__attribute__((assume (a < b)));
}
}

0 comments on commit c94bd96

Please sign in to comment.