Skip to content

Commit d2d0bea

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 d0043d9 commit d2d0bea

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

clang/lib/CodeGen/CodeGenModule.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3720,7 +3720,7 @@ ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject(
37203720
auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
37213721
/*isConstant=*/true, Linkage, Init, Name);
37223722
setGVProperties(GV, TPO);
3723-
if (supportsCOMDAT())
3723+
if (supportsCOMDAT() && Linkage == llvm::GlobalValue::LinkOnceODRLinkage)
37243724
GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
37253725
Emitter.finalize(GV);
37263726

clang/test/CodeGenCXX/template-param-objects.cpp

Lines changed: 10 additions & 0 deletions
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)