[clang] b4bae3f - [clang][CodeGen] Fix global variables initialized with an inheriting constructor.

Eli Friedman via cfe-commits cfe-commits at lists.llvm.org
Sun Jul 2 14:25:35 PDT 2023


Author: Eli Friedman
Date: 2023-07-02T14:25:25-07:00
New Revision: b4bae3fd8ede10026a6f6e1caaec476125f5ac67

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

LOG: [clang][CodeGen] Fix global variables initialized with an inheriting constructor.

For inheriting constructors which have to be emitted inline, we use
CodeGenFunction::EmitInlinedInheritingCXXConstructorCall to emit the
required code.  This code uses EmitParmDecl to emit the "this" argument
of the call.  EmitParmDecl then helpfully calls llvm::Value::setName to
name the parameter... which renames the global variable to "this".

To fix the issue, skip the setName call on globals.

The renaming still has slightly weird results in other cases (it renames
all local variables initialized with an inlined inheriting constructor
to "this"), but the result isn't actually wrong in those cases, so I'm
just going to leave it for now.

Fixes https://github.com/llvm/llvm-project/issues/63618

Differential Revision: https://reviews.llvm.org/D154270

Added: 
    

Modified: 
    clang/lib/CodeGen/CGDecl.cpp
    clang/test/CodeGenCXX/inheriting-constructor.cpp

Removed: 
    


################################################################################
diff  --git a/clang/lib/CodeGen/CGDecl.cpp b/clang/lib/CodeGen/CGDecl.cpp
index fc16fb370dbaa4..e143687479ee3e 100644
--- a/clang/lib/CodeGen/CGDecl.cpp
+++ b/clang/lib/CodeGen/CGDecl.cpp
@@ -2453,7 +2453,10 @@ void CodeGenFunction::EmitParmDecl(const VarDecl &D, ParamValue Arg,
   assert((isa<ParmVarDecl>(D) || isa<ImplicitParamDecl>(D)) &&
          "Invalid argument to EmitParmDecl");
 
-  Arg.getAnyValue()->setName(D.getName());
+  // Set the name of the parameter's initial value to make IR easier to
+  // read. Don't modify the names of globals.
+  if (!isa<llvm::GlobalValue>(Arg.getAnyValue()))
+    Arg.getAnyValue()->setName(D.getName());
 
   QualType Ty = D.getType();
 

diff  --git a/clang/test/CodeGenCXX/inheriting-constructor.cpp b/clang/test/CodeGenCXX/inheriting-constructor.cpp
index b984ea177f211f..37e9166becb4fd 100644
--- a/clang/test/CodeGenCXX/inheriting-constructor.cpp
+++ b/clang/test/CodeGenCXX/inheriting-constructor.cpp
@@ -4,6 +4,24 @@
 // RUN: %clang_cc1 -no-enable-noundef-analysis -std=c++11 -triple i386-windows -emit-llvm -o - %s | FileCheck %s --check-prefix=MSABI --check-prefix=WIN32
 // RUN: %clang_cc1 -no-enable-noundef-analysis -std=c++11 -triple x86_64-windows -emit-llvm -o - %s | FileCheck %s --check-prefix=MSABI --check-prefix=WIN64
 
+// PR63618: make sure we generate definitions for all the globals defined in the test
+// MSABI: @"?b@@3UB@@A" = {{.*}} zeroinitializer
+// MSABI: @"?d@@3UD@@A" = {{.*}} zeroinitializer
+// MSABI: @"?b at noninline_nonvirt@@3UB at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?c at noninline_nonvirt@@3UC at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?b at noninline_virt@@3UB at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?c at noninline_virt@@3UC at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?b at inalloca_nonvirt@@3UB at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?c at inalloca_nonvirt@@3UC at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?b at inalloca_virt@@3UB at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?c at inalloca_virt@@3UC at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?b at inline_nonvirt@@3UB at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?c at inline_nonvirt@@3UC at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?b at inline_virt@@3UB at 1@A" = {{.*}} zeroinitializer
+// MSABI: @"?c at inline_virt@@3UC at 1@A" = {{.*}} zeroinitializer
+
+// MSABI-NOT: @this
+
 // PR12219
 struct A { A(int); virtual ~A(); };
 struct B : A { using A::A; ~B(); };


        


More information about the cfe-commits mailing list