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

via cfe-commits cfe-commits at lists.llvm.org
Tue Nov 10 19:17:49 PST 2026


Author: Akash Manna
Date: 2026-11-11T03:17:43Z
New Revision: c8b08b01f9fd0f4cd600ac85be17f221580d0f5a

URL: https://github.com/llvm/llvm-project/commit/c8b08b01f9fd0f4cd600ac85be17f221580d0f5a
DIFF: https://github.com/llvm/llvm-project/commit/c8b08b01f9fd0f4cd600ac85be17f221580d0f5a.diff

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

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!

Added: 
    

Modified: 
    clang/docs/ReleaseNotes.md
    clang/lib/CodeGen/CGClass.cpp
    clang/test/CodeGenCXX/pod-member-memcpys.cpp

Removed: 
    


################################################################################
diff  --git a/clang/docs/ReleaseNotes.md b/clang/docs/ReleaseNotes.md
index 7205fd4ab2968..0f9e7e01636d7 100644
--- a/clang/docs/ReleaseNotes.md
+++ b/clang/docs/ReleaseNotes.md
@@ -845,6 +845,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 2901233638ffe..514bed6dddaaa 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 8efec6184a3da..cc47a90f399e2 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


        


More information about the cfe-commits mailing list