[clang] [clang][CodeGen] Fix crash on if/switch init-statement ending in noreturn (PR #201047)
via cfe-commits
cfe-commits at lists.llvm.org
Thu Jun 11 03:48:08 PDT 2026
https://github.com/lijinpei-amd updated https://github.com/llvm/llvm-project/pull/201047
>From 7b9a14d8c80c4a5fe9f5a49e35af7b6af35a3fb9 Mon Sep 17 00:00:00 2001
From: Li Jinpei <jinpli at amd.com>
Date: Tue, 2 Jun 2026 15:15:08 +0800
Subject: [PATCH] [clang][CodeGen] Fix crash on if/switch init-statement ending
in noreturn
EmitStmt may `ClearInsertionPoint()` to mark dead code; the condition emitted
next assumes a valid insertion point. Restore one with `EnsureInsertPoint()`
after the init statement. The if/switch body may still be reachable through a
label, so it must be emitted rather than skipped.
Fixes #115514.
---
clang/lib/CodeGen/CGStmt.cpp | 14 ++-
clang/test/CodeGenCXX/noreturn-init-stmt.cpp | 98 ++++++++++++++++++++
2 files changed, 110 insertions(+), 2 deletions(-)
create mode 100644 clang/test/CodeGenCXX/noreturn-init-stmt.cpp
diff --git a/clang/lib/CodeGen/CGStmt.cpp b/clang/lib/CodeGen/CGStmt.cpp
index 30756180ebafa..232094777f233 100644
--- a/clang/lib/CodeGen/CGStmt.cpp
+++ b/clang/lib/CodeGen/CGStmt.cpp
@@ -885,9 +885,14 @@ void CodeGenFunction::EmitIfStmt(const IfStmt &S) {
LexicalScope ConditionScope(*this, S.getCond()->getSourceRange());
ApplyDebugLocation DL(*this, S.getCond());
- if (S.getInit())
+ if (S.getInit()) {
EmitStmt(S.getInit());
+ // The init statement may have cleared the insertion point (e.g. it ended in
+ // a 'noreturn' call); the condition emitted below needs a valid one.
+ EnsureInsertPoint();
+ }
+
if (S.getConditionVariable())
EmitDecl(*S.getConditionVariable());
@@ -2374,9 +2379,14 @@ void CodeGenFunction::EmitSwitchStmt(const SwitchStmt &S) {
RunCleanupsScope ConditionScope(*this);
- if (S.getInit())
+ if (S.getInit()) {
EmitStmt(S.getInit());
+ // The init statement may have cleared the insertion point (e.g. it ended in
+ // a 'noreturn' call); the condition emitted below needs a valid one.
+ EnsureInsertPoint();
+ }
+
if (S.getConditionVariable())
EmitDecl(*S.getConditionVariable());
llvm::Value *CondV = EmitScalarExpr(S.getCond());
diff --git a/clang/test/CodeGenCXX/noreturn-init-stmt.cpp b/clang/test/CodeGenCXX/noreturn-init-stmt.cpp
new file mode 100644
index 0000000000000..ab857da974648
--- /dev/null
+++ b/clang/test/CodeGenCXX/noreturn-init-stmt.cpp
@@ -0,0 +1,98 @@
+// NOTE: Assertions have been autogenerated by utils/update_cc_test_checks.py UTC_ARGS: --version 6
+// RUN: %clang_cc1 -std=c++17 -triple x86_64-unknown-linux-gnu -emit-llvm %s -o - | FileCheck %s
+
+struct T {
+ ~T();
+};
+[[noreturn]] void die();
+bool check(const T &);
+int pick(const T &);
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z4testb(
+// CHECK-SAME: i1 noundef zeroext [[COND:%.*]]) #[[ATTR0:[0-9]+]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
+// CHECK-NEXT: [[COND_ADDR:%.*]] = alloca i8, align 1
+// CHECK-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_T:%.*]], align 1
+// CHECK-NEXT: [[STOREDV:%.*]] = zext i1 [[COND]] to i8
+// CHECK-NEXT: store i8 [[STOREDV]], ptr [[COND_ADDR]], align 1
+// CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[COND_ADDR]], align 1
+// CHECK-NEXT: [[LOADEDV:%.*]] = icmp ne i8 [[TMP0]], 0
+// CHECK-NEXT: br i1 [[LOADEDV]], label %[[IF_THEN:.*]], label %[[IF_END:.*]]
+// CHECK: [[IF_THEN]]:
+// CHECK-NEXT: br label %[[LABEL:.*]]
+// CHECK: [[IF_END]]:
+// CHECK-NEXT: call void @_Z3diev() #[[ATTR4:[0-9]+]]
+// CHECK-NEXT: unreachable
+// CHECK: [[BB1:.*:]]
+// CHECK-NEXT: [[CALL:%.*]] = call noundef zeroext i1 @_Z5checkRK1T(ptr noundef nonnull align 1 dereferenceable(1) [[REF_TMP]])
+// CHECK-NEXT: call void @_ZN1TD1Ev(ptr noundef nonnull align 1 dereferenceable(1) [[REF_TMP]]) #[[ATTR5:[0-9]+]]
+// CHECK-NEXT: br i1 [[CALL]], label %[[IF_THEN1:.*]], label %[[IF_END2:.*]]
+// CHECK: [[IF_THEN1]]:
+// CHECK-NEXT: br label %[[LABEL]]
+// CHECK: [[LABEL]]:
+// CHECK-NEXT: store i32 1, ptr [[RETVAL]], align 4
+// CHECK-NEXT: br label %[[RETURN:.*]]
+// CHECK: [[IF_END2]]:
+// CHECK-NEXT: store i32 0, ptr [[RETVAL]], align 4
+// CHECK-NEXT: br label %[[RETURN]]
+// CHECK: [[RETURN]]:
+// CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[RETVAL]], align 4
+// CHECK-NEXT: ret i32 [[TMP2]]
+//
+int test(bool cond) {
+ if (cond)
+ goto label;
+ if (die(); check(T())) {
+ label:
+ return 1;
+ }
+ return 0;
+}
+
+// CHECK-LABEL: define dso_local noundef i32 @_Z11test_switchb(
+// CHECK-SAME: i1 noundef zeroext [[COND:%.*]]) #[[ATTR0]] {
+// CHECK-NEXT: [[ENTRY:.*:]]
+// CHECK-NEXT: [[RETVAL:%.*]] = alloca i32, align 4
+// CHECK-NEXT: [[COND_ADDR:%.*]] = alloca i8, align 1
+// CHECK-NEXT: [[REF_TMP:%.*]] = alloca [[STRUCT_T:%.*]], align 1
+// CHECK-NEXT: [[STOREDV:%.*]] = zext i1 [[COND]] to i8
+// CHECK-NEXT: store i8 [[STOREDV]], ptr [[COND_ADDR]], align 1
+// CHECK-NEXT: [[TMP0:%.*]] = load i8, ptr [[COND_ADDR]], align 1
+// CHECK-NEXT: [[LOADEDV:%.*]] = icmp ne i8 [[TMP0]], 0
+// CHECK-NEXT: br i1 [[LOADEDV]], label %[[IF_THEN:.*]], label %[[IF_END:.*]]
+// CHECK: [[IF_THEN]]:
+// CHECK-NEXT: br label %[[LABEL:.*]]
+// CHECK: [[IF_END]]:
+// CHECK-NEXT: call void @_Z3diev() #[[ATTR4]]
+// CHECK-NEXT: unreachable
+// CHECK: [[BB1:.*:]]
+// CHECK-NEXT: [[CALL:%.*]] = call noundef i32 @_Z4pickRK1T(ptr noundef nonnull align 1 dereferenceable(1) [[REF_TMP]])
+// CHECK-NEXT: call void @_ZN1TD1Ev(ptr noundef nonnull align 1 dereferenceable(1) [[REF_TMP]]) #[[ATTR5]]
+// CHECK-NEXT: switch i32 [[CALL]], label %[[SW_EPILOG:.*]] [
+// CHECK-NEXT: i32 1, label %[[SW_BB:.*]]
+// CHECK-NEXT: ]
+// CHECK: [[SW_BB]]:
+// CHECK-NEXT: store i32 1, ptr [[RETVAL]], align 4
+// CHECK-NEXT: br label %[[RETURN:.*]]
+// CHECK: [[LABEL]]:
+// CHECK-NEXT: store i32 2, ptr [[RETVAL]], align 4
+// CHECK-NEXT: br label %[[RETURN]]
+// CHECK: [[SW_EPILOG]]:
+// CHECK-NEXT: store i32 0, ptr [[RETVAL]], align 4
+// CHECK-NEXT: br label %[[RETURN]]
+// CHECK: [[RETURN]]:
+// CHECK-NEXT: [[TMP2:%.*]] = load i32, ptr [[RETVAL]], align 4
+// CHECK-NEXT: ret i32 [[TMP2]]
+//
+int test_switch(bool cond) {
+ if (cond)
+ goto label;
+ switch (die(); pick(T())) {
+ case 1:
+ return 1;
+ label:
+ return 2;
+ }
+ return 0;
+}
More information about the cfe-commits
mailing list