[clang] 8403ccd - [Clang][CodeGen] Fix linkage and visibility of template parameter objects
Alexander Shaposhnikov via cfe-commits
cfe-commits at lists.llvm.org
Tue Mar 14 11:23:41 PDT 2023
Author: Alexander Shaposhnikov
Date: 2023-03-14T18:23:06Z
New Revision: 8403ccdcd28c2ff30c1e980ce4a8c851fae01a9d
URL: https://github.com/llvm/llvm-project/commit/8403ccdcd28c2ff30c1e980ce4a8c851fae01a9d
DIFF: https://github.com/llvm/llvm-project/commit/8403ccdcd28c2ff30c1e980ce4a8c851fae01a9d.diff
LOG: [Clang][CodeGen] Fix linkage and visibility of template parameter objects
This diff fixes linkage and visibility of template parameter objects.
The associated GitHub issue: https://github.com/llvm/llvm-project/issues/51571#
Test plan:
1/ ninja check-all
2/ bootstrapped Clang passes tests
Differential revision: https://reviews.llvm.org/D145859
Added:
clang/test/CodeGenCXX/template-param-objects-linkage.cpp
clang/test/CodeGenCXX/template-param-objects-visibility.cpp
Modified:
clang/lib/CodeGen/CodeGenModule.cpp
clang/test/CodeGenCXX/template-param-objects.cpp
Removed:
################################################################################
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index b512fb576a2c0..7055205862ded 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -3238,9 +3238,13 @@ ConstantAddress CodeGenModule::GetAddrOfTemplateParamObject(
return ConstantAddress::invalid();
}
- auto *GV = new llvm::GlobalVariable(
- getModule(), Init->getType(),
- /*isConstant=*/true, llvm::GlobalValue::LinkOnceODRLinkage, Init, Name);
+ llvm::GlobalValue::LinkageTypes Linkage =
+ isExternallyVisible(TPO->getLinkageAndVisibility().getLinkage())
+ ? llvm::GlobalValue::LinkOnceODRLinkage
+ : llvm::GlobalValue::InternalLinkage;
+ auto *GV = new llvm::GlobalVariable(getModule(), Init->getType(),
+ /*isConstant=*/true, Linkage, Init, Name);
+ setGVProperties(GV, TPO);
if (supportsCOMDAT())
GV->setComdat(TheModule.getOrInsertComdat(GV->getName()));
Emitter.finalize(GV);
diff --git a/clang/test/CodeGenCXX/template-param-objects-linkage.cpp b/clang/test/CodeGenCXX/template-param-objects-linkage.cpp
new file mode 100644
index 0000000000000..63e7d8c646869
--- /dev/null
+++ b/clang/test/CodeGenCXX/template-param-objects-linkage.cpp
@@ -0,0 +1,26 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++20 %s -emit-llvm -o - | FileCheck %s
+
+struct S { char buf[32]; };
+template<S s> constexpr const char* f() { return s.buf; }
+const char* fbuf = f<S{"a"}>();
+// CHECK: @_ZTAXtl1StlA32_cLc97EEEE = linkonce_odr constant { <{ i8, [31 x i8] }> }
+
+namespace {
+ struct UN { char buf[64]; };
+}
+template <UN un> constexpr const char* g() { return un.buf; }
+const char* gbuf = g<UN{"b"}>();
+// CHECK: @_ZTAXtlN12_GLOBAL__N_12UNEtlA64_cLc98EEEE = internal constant { <{ i8, [63 x i8] }> }
+
+struct Foo { int *i; };
+int m = 0;
+namespace { int n; }
+
+template <Foo foo>
+const int* h() { return foo.i; }
+
+const int* hm = h<Foo{&m}>();
+// CHECK: @_ZTAXtl3FooadL_Z1mEEE = linkonce_odr constant %struct.Foo { ptr @m }
+
+const int* hn = h<Foo{&n}>();
+// CHECK: @_ZTAXtl3FooadL_ZN12_GLOBAL__N_11nEEEE = internal constant %struct.Foo { ptr @_ZN12_GLOBAL__N_11nE }
diff --git a/clang/test/CodeGenCXX/template-param-objects-visibility.cpp b/clang/test/CodeGenCXX/template-param-objects-visibility.cpp
new file mode 100644
index 0000000000000..bf85016b0aff6
--- /dev/null
+++ b/clang/test/CodeGenCXX/template-param-objects-visibility.cpp
@@ -0,0 +1,11 @@
+// RUN: %clang_cc1 -triple x86_64-linux-gnu -std=c++20 %s -emit-llvm -o - | FileCheck %s
+
+struct S { char buf[32]; };
+template<S s> constexpr const char* f() { return s.buf; }
+const char* fbuf = f<S{"a"}>();
+// CHECK: @_ZTAXtl1StlA32_cLc97EEEE = linkonce_odr constant { <{ i8, [31 x i8] }> }
+
+struct __attribute__ ((visibility ("hidden"))) HN { char buf[64]; };
+template <HN hn> constexpr const char* g() { return hn.buf; }
+const char* gbuf = g<HN{"b"}>();
+// CHECK: @_ZTAXtl2HNtlA64_cLc98EEEE = linkonce_odr hidden constant { <{ i8, [63 x i8] }> }
diff --git a/clang/test/CodeGenCXX/template-param-objects.cpp b/clang/test/CodeGenCXX/template-param-objects.cpp
index e36b27185b526..11ebd21521e83 100644
--- a/clang/test/CodeGenCXX/template-param-objects.cpp
+++ b/clang/test/CodeGenCXX/template-param-objects.cpp
@@ -7,7 +7,9 @@ template<S s> constexpr const char *end() { return s.buf + __builtin_strlen(s.bu
// ITANIUM: [[HELLO:@_ZTAXtl1StlA32_cLc104ELc101ELc108ELc108ELc111ELc32ELc119ELc111ELc114ELc108ELc100EEEE]]
// 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@@@@@"]]
-// CHECK-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
+// 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
+// MSABI-SAME: = linkonce_odr dso_local constant { <{ [11 x i8], [21 x i8] }> } { <{ [11 x i8], [21 x i8] }> <{ [11 x i8] c"hello world", [21 x i8] zeroinitializer }> }, comdat
+
// ITANIUM: @p
// MSABI: @"?p@@3PEBDEB"
More information about the cfe-commits
mailing list