[clang] [clang] Fix loss of `dllexport` for exported template specialization (PR #93302)
Andrew Ng via cfe-commits
cfe-commits at lists.llvm.org
Fri May 24 08:22:31 PDT 2024
https://github.com/nga888 updated https://github.com/llvm/llvm-project/pull/93302
>From 0cfd700e604e5ec68856dfa7daca378e71795fb2 Mon Sep 17 00:00:00 2001
From: Andrew Ng <andrew.ng at sony.com>
Date: Mon, 20 May 2024 18:26:32 +0100
Subject: [PATCH 1/3] [clang] Fix loss of `dllexport` for exported template
specialization
In commit 0a20f541, "Better codegen support for DLL attributes being
dropped after the first declaration (PR20792)", code was added to enable
"dropping" of DLL attributes. The specific issue and example given was
related to `dllimport` and this was the test case that was added in that
commit.
However, the code also included the "dropping" of `dllexport` but no
test case for this was added. This "dropping" of `dllexport` can cause a
`dllexport` template specialization to incorrectly lose its `dllexport`
attribute as shown by the test case in this patch.
As there appears to be no need for the "dropping" of `dllexport`, remove
this code to fix this issue.
---
clang/lib/CodeGen/CodeGenModule.cpp | 16 +++++++++++-----
...tiate-dllexport-template-specialization.cpp | 18 ++++++++++++++++++
2 files changed, 29 insertions(+), 5 deletions(-)
create mode 100644 clang/test/CodeGenCXX/windows-instantiate-dllexport-template-specialization.cpp
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index e4774a587707a..ae8104fe1c4cb 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4554,8 +4554,11 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
Entry->setLinkage(llvm::Function::ExternalLinkage);
}
- // Handle dropped DLL attributes.
- if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
+ // Handle dropped dllimport.
+ if (D &&
+ (Entry->getDLLStorageClass() ==
+ llvm::GlobalVariable::DLLImportStorageClass) &&
+ !D->hasAttr<DLLImportAttr>() &&
!shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) {
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
setDSOLocal(Entry);
@@ -4849,9 +4852,12 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
Entry->setLinkage(llvm::Function::ExternalLinkage);
}
- // Handle dropped DLL attributes.
- if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
- !shouldMapVisibilityToDLLExport(D))
+ // Handle dropped dllimport.
+ if (D &&
+ (Entry->getDLLStorageClass() ==
+ llvm::GlobalVariable::DLLImportStorageClass) &&
+ !D->hasAttr<DLLImportAttr>() &&
+ !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D)))
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
diff --git a/clang/test/CodeGenCXX/windows-instantiate-dllexport-template-specialization.cpp b/clang/test/CodeGenCXX/windows-instantiate-dllexport-template-specialization.cpp
new file mode 100644
index 0000000000000..97f341ba1f909
--- /dev/null
+++ b/clang/test/CodeGenCXX/windows-instantiate-dllexport-template-specialization.cpp
@@ -0,0 +1,18 @@
+// RUN: %clang_cc1 -triple i686-windows -fdeclspec -emit-llvm %s -o - | FileCheck %s -check-prefix CHECK-MS
+// RUN: %clang_cc1 -triple i686-windows-itanium -fdeclspec -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-scei-ps4 -fdeclspec -emit-llvm %s -o - | FileCheck %s
+// RUN: %clang_cc1 -triple x86_64-sie-ps5 -fdeclspec -emit-llvm %s -o - | FileCheck %s
+
+struct s {
+ template <bool b = true> static bool f();
+};
+
+template <typename T> bool template_using_f(T) { return s::f(); }
+
+bool use_template_using_f() { return template_using_f(0); }
+
+template<>
+bool __declspec(dllexport) s::f<true>() { return true; }
+
+// CHECK-MS: dllexport {{.*}} @"??$f@$00 at s@@SA_NXZ"
+// CHECK: dllexport {{.*}} @_ZN1s1fILb1EEEbv
>From 5425cfb18b53f767fd9dc18e5ac7e196b5349f23 Mon Sep 17 00:00:00 2001
From: Andrew Ng <andrew.ng at sony.com>
Date: Fri, 24 May 2024 16:11:52 +0100
Subject: [PATCH 2/3] Fix up calls to shouldMapVisibilityToDLLExport()
---
clang/lib/CodeGen/CodeGenModule.cpp | 4 ++--
1 file changed, 2 insertions(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index ae8104fe1c4cb..6c3e7a6ffbb0b 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4559,7 +4559,7 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
(Entry->getDLLStorageClass() ==
llvm::GlobalVariable::DLLImportStorageClass) &&
!D->hasAttr<DLLImportAttr>() &&
- !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) {
+ !shouldMapVisibilityToDLLExport(cast<NamedDecl>(D))) {
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
setDSOLocal(Entry);
}
@@ -4857,7 +4857,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
(Entry->getDLLStorageClass() ==
llvm::GlobalVariable::DLLImportStorageClass) &&
!D->hasAttr<DLLImportAttr>() &&
- !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D)))
+ !shouldMapVisibilityToDLLExport(D))
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
>From 501b2d783e0486479a026cd0b3682acba64215dc Mon Sep 17 00:00:00 2001
From: Andrew Ng <andrew.ng at sony.com>
Date: Fri, 24 May 2024 16:21:37 +0100
Subject: [PATCH 3/3] clang-format the result of the last change
---
clang/lib/CodeGen/CodeGenModule.cpp | 3 +--
1 file changed, 1 insertion(+), 2 deletions(-)
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 6c3e7a6ffbb0b..4327eeb92bfbf 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4856,8 +4856,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
if (D &&
(Entry->getDLLStorageClass() ==
llvm::GlobalVariable::DLLImportStorageClass) &&
- !D->hasAttr<DLLImportAttr>() &&
- !shouldMapVisibilityToDLLExport(D))
+ !D->hasAttr<DLLImportAttr>() && !shouldMapVisibilityToDLLExport(D))
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
More information about the cfe-commits
mailing list