Skip to content

[clang] Fix loss of dllexport for exported template specialization #94664

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged

Conversation

nga888
Copy link
Collaborator

@nga888 nga888 commented Jun 6, 2024

When dropping DLL attributes, ensure that the most recent declaration is being checked.

@nga888 nga888 requested review from zmodem, wjristow and pogo59 June 6, 2024 19:27
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:codegen IR generation bugs: mangling, exceptions, etc. labels Jun 6, 2024
@llvmbot
Copy link
Member

llvmbot commented Jun 6, 2024

@llvm/pr-subscribers-clang-codegen

@llvm/pr-subscribers-clang

Author: Andrew Ng (nga888)

Changes

When dropping DLL attributes, ensure that the most recent declaration is being checked.


Full diff: https://github.com/llvm/llvm-project/pull/94664.diff

2 Files Affected:

  • (modified) clang/lib/CodeGen/CodeGenModule.cpp (+13-4)
  • (added) clang/test/CodeGenCXX/windows-instantiate-dllexport-template-specialization.cpp (+18)
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@s@@SA_NXZ"
+// CHECK: dllexport {{.*}} @_ZN1s1fILb1EEEbv

Copy link
Collaborator

@zmodem zmodem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! This basically looks good to me.

It would be nice to keep GetOrCreateLLVMGlobal and GetOrCreateLLVMFunction in sync. Would it be possible to extract the "Handle dropped DLL attributes" logic into a small utility function that could be called from both?

@nga888
Copy link
Collaborator Author

nga888 commented Jun 7, 2024

It would be nice to keep GetOrCreateLLVMGlobal and GetOrCreateLLVMFunction in sync. Would it be possible to extract the "Handle dropped DLL attributes" logic into a small utility function that could be called from both?

I did briefly think about this, but does this change really apply to GetOrCreateLLVMGlobal? Is there any test case to exercise it? Perhaps it would be better to leave GetOrCreateLLVMGlobal as is?

@zmodem
Copy link
Collaborator

zmodem commented Jun 7, 2024

It would be nice to keep GetOrCreateLLVMGlobal and GetOrCreateLLVMFunction in sync. Would it be possible to extract the "Handle dropped DLL attributes" logic into a small utility function that could be called from both?

I did briefly think about this, but does this change really apply to GetOrCreateLLVMGlobal? Is there any test case to exercise it? Perhaps it would be better to leave GetOrCreateLLVMGlobal as is?

I think we should look at it the other way around: unless there's a reason that the functions should behave differently (and I'm not aware of any), it seems better for them to keep using the same logic.

@nga888
Copy link
Collaborator Author

nga888 commented Jun 7, 2024

I did briefly think about this, but does this change really apply to GetOrCreateLLVMGlobal? Is there any test case to exercise it? Perhaps it would be better to leave GetOrCreateLLVMGlobal as is?

I think we should look at it the other way around: unless there's a reason that the functions should behave differently (and I'm not aware of any), it seems better for them to keep using the same logic.

One function handles "functions" and the other "globals", so behaviour is not necessarily the same. However, if you feel it would be better that both use the same logic, then I will go ahead with adding this.

@zmodem
Copy link
Collaborator

zmodem commented Jun 7, 2024

One function handles "functions" and the other "globals", so behaviour is not necessarily the same. However, if you feel it would be better that both use the same logic, then I will go ahead with adding this.

Yes, I think that since the dll attribute logic has been the same between GetOrCreateLLVMGlobal and GetOrCreateLLVMFunction so far, we should keep it the same until we find an explicit reason to make them different.

Copy link
Collaborator

@zmodem zmodem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you, I think this is much better.

Copy link
Collaborator

@zmodem zmodem left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

When dropping DLL attributes, ensure that the most recent declaration is
being checked.
@nga888 nga888 force-pushed the fix-dllexport-for-template-specialization-mrd branch from 01e6a04 to d230adc Compare June 10, 2024 16:57
@nga888 nga888 merged commit baba78d into llvm:main Jun 10, 2024
5 of 6 checks passed
Lukacma pushed a commit to Lukacma/llvm-project that referenced this pull request Jun 12, 2024
…lvm#94664)

When dropping DLL attributes, ensure that the most recent declaration is
being checked.
@HerrCai0907 HerrCai0907 mentioned this pull request Jun 13, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:codegen IR generation bugs: mangling, exceptions, etc. clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants