This is an archive of the discontinued LLVM Phabricator instance.

[MSVC compatibility][dllimport/dllexport][PS] Allow dllexport/dllimport for classes with UniqueExternalLinkage
ClosedPublic

Authored by wolfgangp on Mar 17 2023, 4:27 PM.

Details

Summary

This replaces D145271.

Rather than coercing classes with UniqueExternalLInkage to ExternalLinkage as proposed in D145271, this patch suggests to simply allow the dllexport/dllimport attributes for classes that exhibit UniqueExternalLinkage, which includes instantiations of template classes where the template parameters are local classes or classes in anonymous namespaces:

template <typename T>
class __declspec(dllimport) A {};

void func()
{
  class B : public A<B> {};
}

namespace {
  class C : public A<C> {};
}

In D145271 it was suggested that we drop the attribute in such contexts, and this is effectively what happens. The compiler does not produce any exported definitions (or import any symbols) for such classes. The patch is simply to suppress the diagnostic for MSVC mode and Playstation.

I have not changed the behavior of the Windows/Itanium triple since I wasn't sure if this was desirable.

Diff Detail

Event Timeline

wolfgangp created this revision.Mar 17 2023, 4:27 PM
Herald added a project: Restricted Project. · View Herald TranscriptMar 17 2023, 4:27 PM
wolfgangp requested review of this revision.Mar 17 2023, 4:27 PM
hans added a comment.Mar 20 2023, 7:32 AM

In D145271 it was suggested that we drop the attribute in such contexts, and this is effectively what happens. The compiler does not produce any exported definitions (or import any symbols) for such classes. The patch is simply to suppress the diagnostic for MSVC mode and Playstation.

With the current patch, we still end up with the attribute on the base class in the AST. Also, does this make the compiler accept dllexport of classes in anonymous namespaces? I'm not sure we want that.

Is it not possible to check the linkage and bail out in Sema::propagateDLLAttrToBaseClassTemplate instead?

In D145271 it was suggested that we drop the attribute in such contexts, and this is effectively what happens. The compiler does not produce any exported definitions (or import any symbols) for such classes. The patch is simply to suppress the diagnostic for MSVC mode and Playstation.

With the current patch, we still end up with the attribute on the base class in the AST. Also, does this make the compiler accept dllexport of classes in anonymous namespaces? I'm not sure we want that.
Is it not possible to check the linkage and bail out in Sema::propagateDLLAttrToBaseClassTemplate instead?

Actually, the compiler would (still) not accept explicit dll* attributes on the class in either function scopes or anon namespaces. This case is about classes with internal linkage deriving from exported/imported template classes that have been instantiated with a class with internal linkage. In the description, the classes B and C are such classes. They both derive from A, which is imported and instantiated with B and C as template parameters.

However, I think I have found a way to drop the attribute. I think it's equivalent to just suppressing the error message, but it's probably cleaner.

Instead of suppressing the error message we drop the dllimport/dllexport attribute when we see that a class has UniqueExternal linkage. This will suppress exporting or importing any members, which could not be accessed outside the TU anyway.

hans added a comment.Mar 23 2023, 7:20 AM

Thanks, I like this.

Could you please also add tests in CodeGenCXX/dll{ex,im}port.cpp which verify that the IR looks right?

clang/test/SemaCXX/dllexport.cpp
437

Is this one used somewhere?

439

Just to double check: this case (non-template base class) worked before this patch too, right?

wolfgangp marked an inline comment as done.

Added 2 test cases that check that dll{ex,im}ported classes that are instantiated with classes with internal linkage as template arguments are not exported or imported.

Had to place them into separate files, since they are negative tests and did not fit well into dllexport.cpp and dllimport.cpp

wolfgangp added inline comments.Mar 27 2023, 12:03 PM
clang/test/SemaCXX/dllexport.cpp
437

It's not. Thanks for finding it.

439

Right. I just added the case for good measure. Our case is about allowing a dll{ex,im}ported class that is called into existence, so to speak (i.e. instantiated), with template arguments that cause it to have non-external linkage. There is no question about the linkage of the non-template base classes.

hans accepted this revision.Mar 28 2023, 5:49 AM

lgtm

This revision is now accepted and ready to land.Mar 28 2023, 5:49 AM
Herald added a project: Restricted Project. · View Herald TranscriptMar 29 2023, 11:15 AM