https://github.com/akash-manna-sky updated https://github.com/llvm/llvm-project/pull/240447
>From 71d515e275dedf4484fbc6fefdc7fed8454f76dc Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Sat, 7 Nov 2026 18:49:04 +0530 Subject: [PATCH 1/2] [Clang][CodeGen] Fix crash in implicit assignment operators after a `noreturn` member copy 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. Assisted-by: LLMs. --- clang/docs/ReleaseNotes.md | 1 + clang/lib/CodeGen/CGClass.cpp | 6 ++ clang/test/CodeGenCXX/GH111612.cpp | 116 +++++++++++++++++++++++++++++ 3 files changed, 123 insertions(+) create mode 100644 clang/test/CodeGenCXX/GH111612.cpp 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/GH111612.cpp b/clang/test/CodeGenCXX/GH111612.cpp new file mode 100644 index 00000000000000..9a054f7070f494 --- /dev/null +++ b/clang/test/CodeGenCXX/GH111612.cpp @@ -0,0 +1,116 @@ +// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-linux-gnu -fexceptions -fcxx-exceptions -emit-llvm -o - %s | FileCheck %s +// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -emit-llvm-only %s + +// A memcpy batch of trivially copyable members that follows a noreturn member +// or base operator= is unreachable and must be dropped, not emitted. + +struct S { + __attribute__((noreturn)) void operator=(S); +}; + +struct T { + void operator=(T); +}; + +struct C { + S b; + float c; + _Complex float e; +}; +void f1(C &x, C &y) { x = y; } +// CHECK-LABEL: define {{.*}} ptr @_ZN1CaSERKS_( +// CHECK: call void @_ZN1SaSES_( +// CHECK-NEXT: unreachable +// CHECK-NEXT: } + +// The batch before the call is still emitted. +struct C2 { + float c; + _Complex float e; + S b; +}; +void f2(C2 &x, C2 &y) { x = y; } +// CHECK-LABEL: define {{.*}} ptr @_ZN2C2aSERKS_( +// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %{{.*}}, i64 12, i1 false) +// CHECK: call void @_ZN1SaSES_( +// CHECK-NEXT: unreachable +// CHECK-NEXT: } + +// The batch is flushed by a later member copy instead of the return. +struct C3 { + S b; + float c; + float d; + T t; +}; +void f3(C3 &x, C3 &y) { x = y; } +// CHECK-LABEL: define {{.*}} ptr @_ZN2C3aSERKS_( +// CHECK: call void @_ZN1SaSES_( +// CHECK-NEXT: unreachable +// CHECK-NEXT: } + +struct D : S { + float c; + _Complex float e; +}; +void f4(D &x, D &y) { x = y; } +// CHECK-LABEL: define {{.*}} ptr @_ZN1DaSERKS_( +// CHECK: call void @_ZN1SaSES_( +// CHECK-NEXT: unreachable +// CHECK-NEXT: } + +struct M { + M &operator=(const M &); + __attribute__((noreturn)) M &operator=(M &&); +}; +struct CM { + M b; + float c; + _Complex float e; +}; +void f5(CM &x, CM &y) { x = static_cast<CM &&>(y); } +// CHECK-LABEL: define {{.*}} ptr @_ZN2CMaSEOS_( +// CHECK: call {{.*}} @_ZN1MaSEOS_( +// CHECK-NEXT: unreachable +// CHECK-NEXT: } + +// The loop exit is reachable, so the batch after an array member is emitted. +struct CA { + S arr[2]; + float c; + _Complex float e; +}; +void f6(CA &x, CA &y) { x = y; } +// CHECK-LABEL: define {{.*}} ptr @_ZN2CAaSERKS_( +// CHECK: call void @_ZN1SaSES_( +// CHECK-NEXT: unreachable +// CHECK: for.end: +// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %{{.*}}, i64 12, i1 false) +// CHECK-NEXT: ret ptr %this1 + +struct N { + T b; + float c; + _Complex float e; +}; +void f7(N &x, N &y) { x = y; } +// CHECK-LABEL: define {{.*}} ptr @_ZN1NaSERKS_( +// CHECK: call void @_ZN1TaSES_( +// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %{{.*}}, i64 12, i1 false) +// CHECK-NEXT: ret ptr %this1 + +// Constructors keep an insertion point after a noreturn call. +struct SC { + SC(); + __attribute__((noreturn)) SC(const SC &); +}; +struct CC { + SC b; + float c; + _Complex float e; +}; +void f8(CC &x) { CC y(x); } +// CHECK-LABEL: define {{.*}} void @_ZN2CCC2ERKS_( +// CHECK: call void @_ZN2SCC1ERKS_( +// CHECK-NEXT: unreachable >From a4cd93158f673c9a0b9c27bc3d503ec32561f48a Mon Sep 17 00:00:00 2001 From: Akash Manna <[email protected]> Date: Tue, 10 Nov 2026 08:53:54 +0530 Subject: [PATCH 2/2] [Clang][CodeGen] Move the GH111612 test into pod-member-memcpys.cpp --- clang/test/CodeGenCXX/GH111612.cpp | 116 ------------------- clang/test/CodeGenCXX/pod-member-memcpys.cpp | 18 +++ 2 files changed, 18 insertions(+), 116 deletions(-) delete mode 100644 clang/test/CodeGenCXX/GH111612.cpp diff --git a/clang/test/CodeGenCXX/GH111612.cpp b/clang/test/CodeGenCXX/GH111612.cpp deleted file mode 100644 index 9a054f7070f494..00000000000000 --- a/clang/test/CodeGenCXX/GH111612.cpp +++ /dev/null @@ -1,116 +0,0 @@ -// RUN: %clang_cc1 -triple x86_64-linux-gnu -emit-llvm -o - %s | FileCheck %s -// RUN: %clang_cc1 -triple x86_64-linux-gnu -fexceptions -fcxx-exceptions -emit-llvm -o - %s | FileCheck %s -// RUN: %clang_cc1 -triple x86_64-pc-windows-msvc -emit-llvm-only %s - -// A memcpy batch of trivially copyable members that follows a noreturn member -// or base operator= is unreachable and must be dropped, not emitted. - -struct S { - __attribute__((noreturn)) void operator=(S); -}; - -struct T { - void operator=(T); -}; - -struct C { - S b; - float c; - _Complex float e; -}; -void f1(C &x, C &y) { x = y; } -// CHECK-LABEL: define {{.*}} ptr @_ZN1CaSERKS_( -// CHECK: call void @_ZN1SaSES_( -// CHECK-NEXT: unreachable -// CHECK-NEXT: } - -// The batch before the call is still emitted. -struct C2 { - float c; - _Complex float e; - S b; -}; -void f2(C2 &x, C2 &y) { x = y; } -// CHECK-LABEL: define {{.*}} ptr @_ZN2C2aSERKS_( -// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %{{.*}}, i64 12, i1 false) -// CHECK: call void @_ZN1SaSES_( -// CHECK-NEXT: unreachable -// CHECK-NEXT: } - -// The batch is flushed by a later member copy instead of the return. -struct C3 { - S b; - float c; - float d; - T t; -}; -void f3(C3 &x, C3 &y) { x = y; } -// CHECK-LABEL: define {{.*}} ptr @_ZN2C3aSERKS_( -// CHECK: call void @_ZN1SaSES_( -// CHECK-NEXT: unreachable -// CHECK-NEXT: } - -struct D : S { - float c; - _Complex float e; -}; -void f4(D &x, D &y) { x = y; } -// CHECK-LABEL: define {{.*}} ptr @_ZN1DaSERKS_( -// CHECK: call void @_ZN1SaSES_( -// CHECK-NEXT: unreachable -// CHECK-NEXT: } - -struct M { - M &operator=(const M &); - __attribute__((noreturn)) M &operator=(M &&); -}; -struct CM { - M b; - float c; - _Complex float e; -}; -void f5(CM &x, CM &y) { x = static_cast<CM &&>(y); } -// CHECK-LABEL: define {{.*}} ptr @_ZN2CMaSEOS_( -// CHECK: call {{.*}} @_ZN1MaSEOS_( -// CHECK-NEXT: unreachable -// CHECK-NEXT: } - -// The loop exit is reachable, so the batch after an array member is emitted. -struct CA { - S arr[2]; - float c; - _Complex float e; -}; -void f6(CA &x, CA &y) { x = y; } -// CHECK-LABEL: define {{.*}} ptr @_ZN2CAaSERKS_( -// CHECK: call void @_ZN1SaSES_( -// CHECK-NEXT: unreachable -// CHECK: for.end: -// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %{{.*}}, i64 12, i1 false) -// CHECK-NEXT: ret ptr %this1 - -struct N { - T b; - float c; - _Complex float e; -}; -void f7(N &x, N &y) { x = y; } -// CHECK-LABEL: define {{.*}} ptr @_ZN1NaSERKS_( -// CHECK: call void @_ZN1TaSES_( -// CHECK: call void @llvm.memcpy.p0.p0.i64(ptr align 4 %{{.*}}, ptr align 4 %{{.*}}, i64 12, i1 false) -// CHECK-NEXT: ret ptr %this1 - -// Constructors keep an insertion point after a noreturn call. -struct SC { - SC(); - __attribute__((noreturn)) SC(const SC &); -}; -struct CC { - SC b; - float c; - _Complex float e; -}; -void f8(CC &x) { CC y(x); } -// CHECK-LABEL: define {{.*}} void @_ZN2CCC2ERKS_( -// CHECK: call void @_ZN2SCC1ERKS_( -// CHECK-NEXT: unreachable 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 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
