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

Eli Friedman via Phabricator via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 30 15:46:55 PDT 2023


efriedma created this revision.
efriedma added reviewers: rsmith, rnk, rjmccall, akhuang.
Herald added a subscriber: kristof.beyls.
Herald added a project: All.
efriedma requested review of this revision.
Herald added a project: clang.

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


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D154270

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


Index: clang/test/CodeGenCXX/inheriting-constructor.cpp
===================================================================
--- clang/test/CodeGenCXX/inheriting-constructor.cpp
+++ 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(); };
Index: clang/lib/CodeGen/CGDecl.cpp
===================================================================
--- clang/lib/CodeGen/CGDecl.cpp
+++ clang/lib/CodeGen/CGDecl.cpp
@@ -2453,7 +2453,10 @@
   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();
 


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D154270.536467.patch
Type: text/x-patch
Size: 2256 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/cfe-commits/attachments/20230630/7b1b72f0/attachment.bin>


More information about the cfe-commits mailing list