[clang] [clang] Fix loss of `dllexport` for exported template specialization (PR #94664)

Andrew Ng via cfe-commits cfe-commits at lists.llvm.org
Fri Jun 7 10:24:26 PDT 2024


https://github.com/nga888 updated https://github.com/llvm/llvm-project/pull/94664

>From 05175ee44ce0b796b641beb34b18d7974c8355d2 Mon Sep 17 00:00:00 2001
From: Andrew Ng <andrew.ng at sony.com>
Date: Fri, 31 May 2024 17:17:18 +0100
Subject: [PATCH 1/2] [clang] Fix loss of `dllexport` for exported template
 specialization

When dropping DLL attributes, ensure that the most recent declaration is
being checked.
---
 clang/lib/CodeGen/CodeGenModule.cpp            | 17 +++++++++++++----
 ...tiate-dllexport-template-specialization.cpp | 18 ++++++++++++++++++
 2 files changed, 31 insertions(+), 4 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 be7bf0b72dc0c..dcd80bc19e25c 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4561,10 +4561,19 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
     }
 
     // Handle dropped DLL attributes.
-    if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
-        !shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) {
-      Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
-      setDSOLocal(Entry);
+    if (D) {
+      auto SC = Entry->getDLLStorageClass();
+      if (SC != llvm::GlobalValue::DefaultStorageClass) {
+        const Decl *MRD = D->getMostRecentDecl();
+        if (((SC == llvm::GlobalValue::DLLImportStorageClass &&
+              !MRD->hasAttr<DLLImportAttr>()) ||
+             (SC == llvm::GlobalValue::DLLExportStorageClass &&
+              !MRD->hasAttr<DLLExportAttr>())) &&
+            !shouldMapVisibilityToDLLExport(cast<NamedDecl>(MRD))) {
+          Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
+          setDSOLocal(Entry);
+        }
+      }
     }
 
     // If there are two attempts to define the same mangled name, issue an
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 01e6a04c6ab4fbb07d0497fd85c495d4c98f17fc Mon Sep 17 00:00:00 2001
From: Andrew Ng <andrew.ng at sony.com>
Date: Fri, 7 Jun 2024 17:47:23 +0100
Subject: [PATCH 2/2] Apply new DLL attribute dropping logic to globals too

Moved logic to determine DLL attribute dropping into
`shouldDropDLLAttribute()` and use in `GetOrCreateLLVMFunction()` and
`GetOrCreateLLVMGlobal()`.
---
 clang/lib/CodeGen/CodeGenModule.cpp | 32 +++++++++++++++--------------
 clang/lib/CodeGen/CodeGenModule.h   |  2 ++
 2 files changed, 19 insertions(+), 15 deletions(-)

diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index dcd80bc19e25c..f3f89b829220d 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -4509,6 +4509,19 @@ llvm::Constant *CodeGenModule::GetOrCreateMultiVersionResolver(GlobalDecl GD) {
   return Resolver;
 }
 
+bool CodeGenModule::shouldDropDLLAttribute(const Decl *D,
+                                           const llvm::GlobalValue *GV) const {
+  auto SC = GV->getDLLStorageClass();
+  if (SC == llvm::GlobalValue::DefaultStorageClass)
+    return false;
+  const Decl *MRD = D->getMostRecentDecl();
+  return (((SC == llvm::GlobalValue::DLLImportStorageClass &&
+            !MRD->hasAttr<DLLImportAttr>()) ||
+           (SC == llvm::GlobalValue::DLLExportStorageClass &&
+            !MRD->hasAttr<DLLExportAttr>())) &&
+          !shouldMapVisibilityToDLLExport(cast<NamedDecl>(MRD)));
+}
+
 /// GetOrCreateLLVMFunction - If the specified mangled name is not in the
 /// module, create and return an llvm Function with the specified type. If there
 /// is something in the module with the specified name, return it potentially
@@ -4561,19 +4574,9 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
     }
 
     // Handle dropped DLL attributes.
-    if (D) {
-      auto SC = Entry->getDLLStorageClass();
-      if (SC != llvm::GlobalValue::DefaultStorageClass) {
-        const Decl *MRD = D->getMostRecentDecl();
-        if (((SC == llvm::GlobalValue::DLLImportStorageClass &&
-              !MRD->hasAttr<DLLImportAttr>()) ||
-             (SC == llvm::GlobalValue::DLLExportStorageClass &&
-              !MRD->hasAttr<DLLExportAttr>())) &&
-            !shouldMapVisibilityToDLLExport(cast<NamedDecl>(MRD))) {
-          Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
-          setDSOLocal(Entry);
-        }
-      }
+    if (D && shouldDropDLLAttribute(D, Entry)) {
+      Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
+      setDSOLocal(Entry);
     }
 
     // If there are two attempts to define the same mangled name, issue an
@@ -4865,8 +4868,7 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
     }
 
     // Handle dropped DLL attributes.
-    if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
-        !shouldMapVisibilityToDLLExport(D))
+    if (D && shouldDropDLLAttribute(D, Entry))
       Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
 
     if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index dc24971a3c186..9b63f47ef42cb 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -1594,6 +1594,8 @@ class CodeGenModule : public CodeGenTypeCache {
   }
 
 private:
+  bool shouldDropDLLAttribute(const Decl *D, const llvm::GlobalValue *GV) const;
+
   llvm::Constant *GetOrCreateLLVMFunction(
       StringRef MangledName, llvm::Type *Ty, GlobalDecl D, bool ForVTable,
       bool DontDefer = false, bool IsThunk = false,



More information about the cfe-commits mailing list