Skip to content

Commit 905c7f6

Browse files
committed
[clang] Do not emit template parameter objects as COMDATs when they have internal linkage. (#125448)
Per the ELF spec, section groups may only contain local symbols if those symbols are only referenced from within the section group. [1] In the case of template parameter objects, they can be referenced from outside the group when the type of the object was declared in an anonymous namespace. In that case, we can't place the object in a COMDAT. This matches GCC's linkage behavior on the test input. [1]: https://www.sco.com/developers/gabi/latest/ch4.sheader.html#section_groups Cherry-picked from llvm/llvm-project@8f025f2
1 parent 72ed786 commit 905c7f6

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

Diff for: clang/lib/CodeGen/CodeGenModule.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -3565,7 +3565,7 @@ ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject(
35653565
auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
35663566
/*isConstant=*/true, Linkage, Init, Name);
35673567
setGVProperties(GV, TPO);
3568-
if (supportsCOMDAT())
3568+
if (supportsCOMDAT() && Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
35693569
GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
35703570
Emitter.finalize(GV);
35713571

Diff for: clang/test/CodeGenCXX/template-param-objects.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ struct S { char buf[32]; };
55
template<S s> constexpr const char *begin() { return s.buf; }
66
template<S s> constexpr const char *end() { return s.buf + __builtin_strlen(s.buf); }
77

8+
namespace { struct T { char buf[32]; }; }
9+
template<T t> constexpr const char* begin_anon() { return t.buf; }
10+
811
// ITANIUM: [[HELLO:@_ZTAXtl1StlA32_cLc104ELc101ELc108ELc108ELc111ELc32ELc119ELc111ELc114ELc108ELc100EEEE]]
912
// MSABI: [[HELLO:@"[?][?]__N2US@@3D0GI@@0GF@@0GM@@0GM@@0GP@@0CA@@0HH@@0GP@@0HC@@0GM@@0GE@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@0A@@@@@"]]
1013
// ITANIUM-SAME: = linkonce_odr constant { <{ [11 x i8], [21 x i8] }> } { <{ [11 x i8], [21 x i8] }> <{ [11 x i8] c"hello world", [21 x i8] zeroinitializer }> }, comdat
@@ -19,3 +22,10 @@ const char *p = begin<S{"hello world"}>();
1922
// MSABI: @"?q@@3PEBDEB"
2023
// CHECK-SAME: global ptr getelementptr (i8, ptr [[HELLO]], i64 11)
2124
const char *q = end<S{"hello world"}>();
25+
26+
27+
// CHECK: internal constant { <{ [10 x i8], [22 x i8] }> } { <{ [10 x i8], [22 x i8] }> <{ [10 x i8] c"hello anon", [22 x i8] zeroinitializer }> }
28+
// CHECK-NOT: comdat
29+
// ITANIUM: @r
30+
// MSABI: @"?r@@3PEBDEB"
31+
const char *r = begin_anon<T{"hello anon"}>();

0 commit comments

Comments
 (0)