[clang] [Clang][CodeGen] Fix crash in implicit assignment operators after a `noreturn` member copy (PR #240447)

via cfe-commits cfe-commits at lists.llvm.org
Mon Nov 9 19:25:18 PST 2026


llvmorg-github-actions[bot] wrote:


<!--LLVM PR SUMMARY COMMENT-->

@llvm/pr-subscribers-clang-codegen

Author: Akash Manna (akash-manna-sky)

<details>
<summary>Changes</summary>

Fixes #<!-- -->111612

When an implicit assignment operator copies a member or base through a `noreturn` `operator=`, `EmitStmt` emits `unreachable` and clears the insertion point. `AssignmentMemcpyizer` then batches the trivially copyable members that follow into one memcpy and emits it directly, so the field GEPs were built on a null block.

`FieldMemcpyizer::emitMemcpy` now drops the batch when there is no insertion point, as `EmitStmt` does for any other unreachable statement. The operator then ends with the call and `unreachable`, like a hand-written one with the same statements.

AI Assisted!


---
Full diff: https://github.com/llvm/llvm-project/pull/240447.diff


3 Files Affected:

- (modified) clang/docs/ReleaseNotes.md (+1) 
- (modified) clang/lib/CodeGen/CGClass.cpp (+6) 
- (modified) clang/test/CodeGenCXX/pod-member-memcpys.cpp (+18) 


``````````diff
diff --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 6d1dbbbb6eb77b..800aa9a8c9f3da 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -795,6 +795,7 @@ documentation](CIR/index.md) for more information regarding ClangIR's status and
 - Fixed IEEE f128 complex mul/div using the IBM f128 libcalls on powerpc. (#GH216820)
 - Fixed an ICE that occurred when a structured binding pack is expanded outside the lambda where it was declared. (#GH214160)
 - Fixed a crash when a statement expression in the initializer of a new-expression starts with a loop or a label. (#GH175427)
+- Fixed a crash in an implicit assignment operator when a `noreturn` member or base `operator=` is followed by trivially copyable members. (#GH111612)
 - Fixed a bug where a stray closing curley brace in an OpenMP/OpenACC pragma could cause pragma parsing issues when inside of a member function. (#GH214195)
 - Fixed a bug where preprocessor directives following comments were not correctly recognized when using -C. (#GH48361)
 - Fixed a crash when declaring a member template within a local class inside an OpenMP region. (#GH216052)
diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp
index 2901233638ffee..514bed6dddaaad 100644
--- a/clang/lib/CodeGen/CGClass.cpp
+++ b/clang/lib/CodeGen/CGClass.cpp
@@ -910,6 +910,12 @@ class FieldMemcpyizer {
       return;
     }
 
+    // Unreachable, e.g. after a noreturn member copy; drop it like EmitStmt.
+    if (!CGF.HaveInsertPoint()) {
+      reset();
+      return;
+    }
+
     uint64_t FirstByteOffset;
     if (FirstField->isBitField()) {
       const CGRecordLayout &RL =
diff --git a/clang/test/CodeGenCXX/pod-member-memcpys.cpp b/clang/test/CodeGenCXX/pod-member-memcpys.cpp
index 8efec6184a3daf..cc47a90f399e24 100644
--- a/clang/test/CodeGenCXX/pod-member-memcpys.cpp
+++ b/clang/test/CodeGenCXX/pod-member-memcpys.cpp
@@ -122,6 +122,16 @@ struct WithEmptyNUAField {
     int b;
 };
 
+struct NoReturnNonPOD {
+  __attribute__((noreturn)) NoReturnNonPOD& operator=(const NoReturnNonPOD&);
+};
+
+struct NoReturnMember {
+  int a, b, c, d;
+  NoReturnNonPOD np;
+  int w, x, y, z;
+};
+
 // COPY-ASSIGNMENT OPERATORS:
 
 // Assignment operators are output in the order they're encountered.
@@ -139,6 +149,7 @@ CALL_AO(InnerClassMember)
 CALL_AO(PackedMembers)
 CALL_AO(WithEmptyField)
 CALL_AO(WithEmptyNUAField)
+CALL_AO(NoReturnMember)
 
 // Basic copy-assignment:
 // CHECK-LABEL: define linkonce_odr nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZN5BasicaSERKS_(ptr {{[^,]*}} %this, ptr nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %0)
@@ -215,6 +226,13 @@ CALL_AO(WithEmptyNUAField)
 // CHECK: call nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZN6NonPODaSERKS_
 // CHECK: ret ptr
 
+// NoReturnMember copy-assignment: no memcpy after the noreturn call.
+// CHECK-LABEL: define linkonce_odr nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZN14NoReturnMemberaSERKS_(ptr {{[^,]*}} %this, ptr nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) %0)
+// CHECK: call void @llvm.memcpy.p0.p0.i64({{.*}} align 4 {{.*}} align 4 {{.*}}i64 16, i1 {{.*}})
+// CHECK: call nonnull align {{[0-9]+}} dereferenceable({{[0-9]+}}) ptr @_ZN14NoReturnNonPODaSERKS_
+// CHECK-NEXT: unreachable
+// CHECK-NEXT: }
+
 // COPY-CONSTRUCTORS:
 
 // Clang outputs copy-constructors in the reverse of the order that

``````````

</details>


https://github.com/llvm/llvm-project/pull/240447


More information about the cfe-commits mailing list