https://github.com/Nikunj-Pokharel created 
https://github.com/llvm/llvm-project/pull/235002

## Summary

Fixes #234795.

When a delegating constructor is generated for a base subobject, Clang should 
not zero-initialize the virtual base as part of that subobject's initialization.

This change distinguishes base-object constructors from complete-object 
constructors when handling delegating construction. It also adds a regression 
test to check the generated initialization for both cases.

## Testing

- `ninja -C build clang`
- `./build/bin/llvm-lit -v clang/test/CodeGenCXX/issue-234795.cpp`

The regression test passes locally.

## AI assistance

AI assistance was used to help investigate the issue, understand the relevant 
code, and develop and review the fix. I reviewed the changes and ran the 
regression test.

>From b5d01429b0bcc039f1e4bf3e517a2212bbe421a7 Mon Sep 17 00:00:00 2001
From: Xen00n <[email protected]>
Date: Fri, 23 Oct 2026 18:22:04 +0530
Subject: [PATCH 1/2] [Clang] Fix preferred function alignment truncation

---
 clang/include/clang/Basic/LangOptions.def | 3 ++-
 clang/test/CodeGen/prefalign.c            | 7 +++++--
 2 files changed, 7 insertions(+), 3 deletions(-)

diff --git a/clang/include/clang/Basic/LangOptions.def 
b/clang/include/clang/Basic/LangOptions.def
index e0c29f7b0d791f..49e14dde6c84cf 100644
--- a/clang/include/clang/Basic/LangOptions.def
+++ b/clang/include/clang/Basic/LangOptions.def
@@ -432,7 +432,8 @@ ENUM_LANGOPT(ClangABICompat, ClangABI, 5, ClangABI::Latest, 
NotCompatible,
              "with")
 
 VALUE_LANGOPT(FunctionAlignment, 5, 0, Compatible, "Default alignment for 
functions")
-VALUE_LANGOPT(PreferredFunctionAlignment, 5, 0, Compatible, "Preferred 
alignment for functions")
+VALUE_LANGOPT(PreferredFunctionAlignment, 17, 0, Compatible,
+              "Preferred alignment for functions")
 VALUE_LANGOPT(LoopAlignment, 32, 0, Compatible, "Default alignment for loops")
 
 LANGOPT(FixedPoint, 1, 0, NotCompatible, "fixed point types")
diff --git a/clang/test/CodeGen/prefalign.c b/clang/test/CodeGen/prefalign.c
index 4e3f2cbda970de..3251c8551cc096 100644
--- a/clang/test/CodeGen/prefalign.c
+++ b/clang/test/CodeGen/prefalign.c
@@ -1,4 +1,7 @@
-// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux 
-fpreferred-function-alignment=16 %s -o - | FileCheck %s
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux 
-fpreferred-function-alignment=16 %s -o - | FileCheck %s --check-prefix=CHECK-16
+// RUN: %clang_cc1 -emit-llvm -triple x86_64-unknown-linux 
-fpreferred-function-alignment=32 %s -o - | FileCheck %s --check-prefix=CHECK-32
+
+// CHECK-16: define {{.*}} void @f() {{.*}} prefalign(16)
+// CHECK-32: define {{.*}} void @f() {{.*}} prefalign(32)
 
-// CHECK: define {{.*}} void @f() {{.*}} prefalign(16)
 void f() {}

>From b3214b980d9b6e36d4a0bfcc35312a2a4664c6cb Mon Sep 17 00:00:00 2001
From: Xen00n <[email protected]>
Date: Fri, 23 Oct 2026 19:59:25 +0530
Subject: [PATCH 2/2] Fix zero initialization of virtual bases in delegating
 constructors

---
 clang/lib/CodeGen/CGExprCXX.cpp        |  6 ++++++
 clang/test/CodeGenCXX/issue-234795.cpp | 30 ++++++++++++++++++++++++++
 2 files changed, 36 insertions(+)
 create mode 100644 clang/test/CodeGenCXX/issue-234795.cpp

diff --git a/clang/lib/CodeGen/CGExprCXX.cpp b/clang/lib/CodeGen/CGExprCXX.cpp
index 8279611accba33..46caf76c158b4b 100644
--- a/clang/lib/CodeGen/CGExprCXX.cpp
+++ b/clang/lib/CodeGen/CGExprCXX.cpp
@@ -621,6 +621,12 @@ void CodeGenFunction::EmitCXXConstructExpr(const 
CXXConstructExpr *E,
   if (E->requiresZeroInitialization() && !Dest.isZeroed()) {
     switch (E->getConstructionKind()) {
     case CXXConstructionKind::Delegating:
+      if (CurGD.getCtorType() == Ctor_Base)
+        EmitNullBaseClassInitialization(*this, Dest.getAddress(),
+                                        CD->getParent());
+      else
+        EmitNullInitialization(Dest.getAddress(), E->getType());
+      break;
     case CXXConstructionKind::Complete:
       EmitNullInitialization(Dest.getAddress(), E->getType());
       break;
diff --git a/clang/test/CodeGenCXX/issue-234795.cpp 
b/clang/test/CodeGenCXX/issue-234795.cpp
new file mode 100644
index 00000000000000..c1ec6db6b54025
--- /dev/null
+++ b/clang/test/CodeGenCXX/issue-234795.cpp
@@ -0,0 +1,30 @@
+// RUN: %clang_cc1 -std=c++17 -O0 -emit-llvm -o - %s | FileCheck %s
+
+struct A {
+  int x = 42;
+  virtual ~A() {}
+};
+
+struct B : virtual A {
+  explicit B(int) : B() {}
+  B() = default;
+};
+
+struct D : B {
+  D() : B(0) {}
+};
+
+int main() {
+  D d;
+  B b(0);
+  return d.x + b.x;
+}
+
+// The complete-object constructor initializes the full object.
+// CHECK-LABEL: define{{.*}} @_ZN1BC1Ei
+// CHECK: call void @llvm.memset{{.*}}i64 24
+
+// The base-object constructor must not zero-initialize its virtual base.
+// CHECK-LABEL: define{{.*}} @_ZN1BC2Ei
+// CHECK: call void @llvm.memset{{.*}}i64 8
+// CHECK: call{{.*}} @_ZN1BC2Ev
\ No newline at end of file

_______________________________________________
cfe-commits mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits

Reply via email to