Skip to content
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

[clang] Don't inherit dllimport/dllexport to exclude_from_explicit_in… #65961

Merged

Conversation

zmodem
Copy link
Collaborator

@zmodem zmodem commented Sep 11, 2023

…stantiation members during explicit instantiation.

This is a continuation of https://reviews.llvm.org/D155713

Fixes #40363

…stantiation members

during explicit instantiation.

Fixes llvm#40363
@zmodem zmodem requested a review from a team as a code owner September 11, 2023 13:18
@zmodem zmodem requested a review from rnk September 11, 2023 13:18
@llvmbot llvmbot added clang Clang issues not falling into any other category clang:frontend Language frontend issues, e.g. anything involving "Sema" labels Sep 11, 2023
@llvmbot
Copy link
Collaborator

llvmbot commented Sep 11, 2023

@llvm/pr-subscribers-clang

Changes

…stantiation members during explicit instantiation.

This is a continuation of https://reviews.llvm.org/D155713

Fixes #40363

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

3 Files Affected:

  • (modified) clang/lib/Sema/SemaDeclCXX.cpp (+7)
  • (added) clang/test/CodeGenCXX/attr-exclude_from_explicit_instantiation.dllimport.cpp (+48)
  • (added) clang/test/SemaCXX/attr-exclude_from_explicit_instantiation.dllimport.cpp (+27)
diff --git a/clang/lib/Sema/SemaDeclCXX.cpp b/clang/lib/Sema/SemaDeclCXX.cpp
index 2b2b7391f88fd54..1f2bb1edd3b7be5 100644
--- a/clang/lib/Sema/SemaDeclCXX.cpp
+++ b/clang/lib/Sema/SemaDeclCXX.cpp
@@ -6602,6 +6602,13 @@ void Sema::checkClassLevelDLLAttribute(CXXRecordDecl *Class) {
     if (!VD && !MD)
       continue;
 
+    if ((TSK == TSK_ExplicitInstantiationDeclaration ||
+         TSK == TSK_ExplicitInstantiationDefinition) &&
+        Member->hasAttr()) {
+      // Skip members excluded from instantiation.
+      continue;
+    }
+
     if (MD) {
       // Don't process deleted methods.
       if (MD->isDeleted())
diff --git a/clang/test/CodeGenCXX/attr-exclude_from_explicit_instantiation.dllimport.cpp b/clang/test/CodeGenCXX/attr-exclude_from_explicit_instantiation.dllimport.cpp
new file mode 100644
index 000000000000000..b3c804839be1ee4
--- /dev/null
+++ b/clang/test/CodeGenCXX/attr-exclude_from_explicit_instantiation.dllimport.cpp
@@ -0,0 +1,48 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-windows -fms-extensions -emit-llvm -O0 -o - %s | FileCheck %s
+
+// Test that dllimport and exclude_from_explicit_instantiation work properly
+// together. Specifically, we check that when exclude_from_explicit_instantiation
+// is used on a method, the compiler doesn't expect it to be provided externally
+// even if it is marked with dllimport.
+//
+// https://github.com/llvm/llvm-project/issues/40363
+
+#define DLLIMPORT __declspec(dllimport)
+#define DLLEXPORT __declspec(dllexport)
+#define EXCLUDE_FROM_EXPLICIT_INSTANTIATION __attribute__((exclude_from_explicit_instantiation))
+
+template 
+struct DLLIMPORT Foo {
+  EXCLUDE_FROM_EXPLICIT_INSTANTIATION void x() {}
+};
+
+template 
+struct Bar {
+  EXCLUDE_FROM_EXPLICIT_INSTANTIATION void x() {}
+};
+
+extern template struct Foo;
+extern template struct DLLIMPORT Bar;
+
+
+template 
+struct Baz {
+  EXCLUDE_FROM_EXPLICIT_INSTANTIATION void f() {}
+};
+
+template struct DLLEXPORT Baz;
+
+
+void test(Foo& foo, Bar& bar, Baz& baz) {
+  // Not imported.
+  // CHECK-DAG: define linkonce_odr dso_local void @"?x@?$Foo@H@@QEAAXXZ"
+  foo.x();
+
+  // Not imported.
+  // CHECK-DAG: define linkonce_odr dso_local void @"?x@?$Bar@H@@QEAAXXZ"
+  bar.x();
+
+  // Not exported.
+  // CHECK-DAG: define linkonce_odr dso_local void @"?f@?$Baz@H@@QEAAXXZ"
+  baz.f();
+}
diff --git a/clang/test/SemaCXX/attr-exclude_from_explicit_instantiation.dllimport.cpp b/clang/test/SemaCXX/attr-exclude_from_explicit_instantiation.dllimport.cpp
new file mode 100644
index 000000000000000..bdca2d8895f516c
--- /dev/null
+++ b/clang/test/SemaCXX/attr-exclude_from_explicit_instantiation.dllimport.cpp
@@ -0,0 +1,27 @@
+// RUN: %clang_cc1 -triple x86_64-unknown-windows -fms-extensions -verify %s
+
+// Test that an entity marked as both dllimport and exclude_from_explicit_instantiation
+// isn't instantiated.
+
+#define DLLIMPORT __declspec(dllimport)
+#define EXCLUDE_FROM_EXPLICIT_INSTANTIATION __attribute__((exclude_from_explicit_instantiation))
+
+template 
+struct DLLIMPORT Foo {
+  EXCLUDE_FROM_EXPLICIT_INSTANTIATION void x();
+};
+
+template 
+struct Bar {
+  DLLIMPORT EXCLUDE_FROM_EXPLICIT_INSTANTIATION inline void x();
+};
+
+template 
+void Foo::x() { using Fail = typename T::fail; }
+
+template 
+DLLIMPORT inline void Bar::x() { using Fail = typename T::fail; }
+
+// expected-no-diagnostics
+template struct Foo;
+template struct Bar;

Copy link
Collaborator

@rnk rnk left a comment

Choose a reason for hiding this comment

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

Thanks!

@zmodem zmodem merged commit 84216d1 into llvm:main Sep 14, 2023
3 of 4 checks passed
@zmodem zmodem deleted the exclude_from_explicit_instantiation_vs_dllimport branch September 14, 2023 08:20
kstoimenov pushed a commit to kstoimenov/llvm-project that referenced this pull request Sep 14, 2023
…stantiation members during explicit instantiation (llvm#65961)

This is a continuation of https://reviews.llvm.org/D155713

Fixes llvm#40363
ZijunZhaoCCK pushed a commit to ZijunZhaoCCK/llvm-project that referenced this pull request Sep 19, 2023
…stantiation members during explicit instantiation (llvm#65961)

This is a continuation of https://reviews.llvm.org/D155713

Fixes llvm#40363
zmodem added a commit that referenced this pull request Sep 20, 2023
…licit_instantiation members during explicit instantiation (#65961)"

This uncovered a problem with virtual methods and
exclude_from_explicit_instantiation, see
#66909

Reverting until that's fixed.

> This is a continuation of https://reviews.llvm.org/D155713
>
> Fixes #40363

This reverts commit 84216d1.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
clang:frontend Language frontend issues, e.g. anything involving "Sema" clang Clang issues not falling into any other category
Projects
None yet
Development

Successfully merging this pull request may close these issues.

dllimport conflicts with exclude_from_explicit_instantiation.
3 participants