Skip to content

Commit

Permalink
Reland [clang][AIX] add option mdefault-visibility-export-mapping
Browse files Browse the repository at this point in the history
The option mdefault-visibility-export-mapping is created to allow
mapping default visibility to an explicit shared library export
(e.g. dllexport). Exactly how and if this is manifested is target
dependent (since it depends on how they map dllexport in the IR).

Three values are provided for the option:

* none: the default and behavior without the option, no additional export linkage information is created.
* explicit: add the export for entities with explict default visibility from the source, including RTTI
* all: add the export for all entities with default visibility

This option is useful for targets which do not export symbols as part of
their usual default linkage behaviour (e.g. AIX), such targets
traditionally specified such information in external files (e.g. export
lists), but this mapping allows them to use the visibility information
typically used for this purpose on other (e.g. ELF) platforms.

This relands commit: 8c8a267

with fixes for the compile time and assert problems that were reported
by:

* making shouldMapVisibilityToDLLExport inline and provide an early return
in the case where no mapping is in effect (aka non-AIX platforms)
* don't try to export RTTI types which we will give internal linkage to

Reviewed By: MaskRay

Differential Revision: https://reviews.llvm.org/D126340
  • Loading branch information
daltenty committed Jun 13, 2022
1 parent 166d6ed commit 6a86730
Show file tree
Hide file tree
Showing 13 changed files with 870 additions and 11 deletions.
17 changes: 17 additions & 0 deletions clang/docs/UsersManual.rst
Expand Up @@ -3626,6 +3626,23 @@ Clang expects the GCC executable "gcc.exe" compiled for
`Some tests might fail <https://bugs.llvm.org/show_bug.cgi?id=9072>`_ on
``x86_64-w64-mingw32``.

AIX
^^^

The ``-mdefault-visibility-export-mapping=`` option can be used to control
mapping of default visibility to an explicit shared object export
(i.e. XCOFF exported visibility). Three values are provided for the option:

* ``-mdefault-visibility-export-mapping=none``: no additional export
information is created for entities with default visibility.
* ``-mdefault-visibility-export-mapping=explicit``: mark entities for export
if they have explict (e.g. via an attribute) default visibility from the
source, including RTTI.
* ``-mdefault-visibility-export-mapping=all``: set XCOFF exported visibility
for all entities with default visibility from any source. This gives a
export behavior similar to ELF platforms where all entities with default
visibility are exported.

.. _spir-v:

SPIR-V support
Expand Down
1 change: 1 addition & 0 deletions clang/include/clang/Basic/LangOptions.def
Expand Up @@ -290,6 +290,7 @@ BENIGN_LANGOPT(DumpRecordLayoutsComplete , 1, 0, "dumping the AST layout of all
BENIGN_LANGOPT(DumpVTableLayouts , 1, 0, "dumping the layouts of emitted vtables")
LANGOPT(NoConstantCFStrings , 1, 0, "no constant CoreFoundation strings")
BENIGN_LANGOPT(InlineVisibilityHidden , 1, 0, "hidden visibility for inline C++ methods")
BENIGN_ENUM_LANGOPT(DefaultVisibilityExportMapping, DefaultVisiblityExportMapping, 2, DefaultVisiblityExportMapping::None, "controls mapping of default visibility to dllexport")
BENIGN_LANGOPT(IgnoreXCOFFVisibility, 1, 0, "All the visibility attributes that are specified in the source code are ignored in aix XCOFF.")
BENIGN_LANGOPT(VisibilityInlinesHiddenStaticLocalVar, 1, 0,
"hidden visibility for static local variables in inline C++ "
Expand Down
23 changes: 23 additions & 0 deletions clang/include/clang/Basic/LangOptions.h
Expand Up @@ -350,6 +350,14 @@ class LangOptions : public LangOptionsBase {
PerThread,
};

enum class DefaultVisiblityExportMapping {
None,
/// map only explicit default visibilities to exported
Explicit,
/// map all default visibilities to exported
All,
};

public:
/// The used language standard.
LangStandard::Kind LangStd;
Expand Down Expand Up @@ -581,6 +589,21 @@ class LangOptions : public LangOptionsBase {

bool isSYCL() const { return SYCLIsDevice || SYCLIsHost; }

bool hasDefaultVisibilityExportMapping() const {
return getDefaultVisibilityExportMapping() !=
DefaultVisiblityExportMapping::None;
}

bool isExplicitDefaultVisibilityExportMapping() const {
return getDefaultVisibilityExportMapping() ==
DefaultVisiblityExportMapping::Explicit;
}

bool isAllDefaultVisibilityExportMapping() const {
return getDefaultVisibilityExportMapping() ==
DefaultVisiblityExportMapping::All;
}

/// Remap path prefix according to -fmacro-prefix-path option.
void remapPathPrefix(SmallVectorImpl<char> &Path) const;
};
Expand Down
7 changes: 7 additions & 0 deletions clang/include/clang/Driver/Options.td
Expand Up @@ -2918,6 +2918,13 @@ def fvisibility_ms_compat : Flag<["-"], "fvisibility-ms-compat">, Group<f_Group>
def fvisibility_global_new_delete_hidden : Flag<["-"], "fvisibility-global-new-delete-hidden">, Group<f_Group>,
HelpText<"Give global C++ operator new and delete declarations hidden visibility">, Flags<[CC1Option]>,
MarshallingInfoFlag<LangOpts<"GlobalAllocationFunctionVisibilityHidden">>;
def mdefault_visibility_export_mapping_EQ : Joined<["-"], "mdefault-visibility-export-mapping=">,
Values<"none,explicit,all">,
NormalizedValuesScope<"LangOptions::DefaultVisiblityExportMapping">,
NormalizedValues<["None", "Explicit", "All"]>,
HelpText<"Mapping between default visibility and export">,
Group<m_Group>, Flags<[CC1Option]>,
MarshallingInfoEnum<LangOpts<"DefaultVisibilityExportMapping">,"None">;
defm new_infallible : BoolFOption<"new-infallible",
LangOpts<"NewInfallible">, DefaultFalse,
PosFlag<SetTrue, [], "Enable">, NegFlag<SetFalse, [], "Disable">,
Expand Down
10 changes: 7 additions & 3 deletions clang/lib/CodeGen/CodeGenModule.cpp
Expand Up @@ -1214,7 +1214,9 @@ void CodeGenModule::setDLLImportDLLExport(llvm::GlobalValue *GV,
if (D && D->isExternallyVisible()) {
if (D->hasAttr<DLLImportAttr>())
GV->setDLLStorageClass(llvm::GlobalVariable::DLLImportStorageClass);
else if (D->hasAttr<DLLExportAttr>() && !GV->isDeclarationForLinker())
else if ((D->hasAttr<DLLExportAttr>() ||
shouldMapVisibilityToDLLExport(D)) &&
!GV->isDeclarationForLinker())
GV->setDLLStorageClass(llvm::GlobalVariable::DLLExportStorageClass);
}
}
Expand Down Expand Up @@ -3790,7 +3792,8 @@ llvm::Constant *CodeGenModule::GetOrCreateLLVMFunction(
}

// Handle dropped DLL attributes.
if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>()) {
if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
!shouldMapVisibilityToDLLExport(cast_or_null<NamedDecl>(D))) {
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);
setDSOLocal(Entry);
}
Expand Down Expand Up @@ -4102,7 +4105,8 @@ CodeGenModule::GetOrCreateLLVMGlobal(StringRef MangledName, llvm::Type *Ty,
}

// Handle dropped DLL attributes.
if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>())
if (D && !D->hasAttr<DLLImportAttr>() && !D->hasAttr<DLLExportAttr>() &&
!shouldMapVisibilityToDLLExport(D))
Entry->setDLLStorageClass(llvm::GlobalValue::DefaultStorageClass);

if (LangOpts.OpenMP && !LangOpts.OpenMPSimd && D)
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/CodeGen/CodeGenModule.h
Expand Up @@ -798,6 +798,14 @@ class CodeGenModule : public CodeGenTypeCache {

void setDSOLocal(llvm::GlobalValue *GV) const;

bool shouldMapVisibilityToDLLExport(const NamedDecl *D) const {
return getLangOpts().hasDefaultVisibilityExportMapping() && D &&
(D->getLinkageAndVisibility().getVisibility() ==
DefaultVisibility) &&
(getLangOpts().isAllDefaultVisibilityExportMapping() ||
(getLangOpts().isExplicitDefaultVisibilityExportMapping() &&
D->getLinkageAndVisibility().isVisibilityExplicit()));
}
void setDLLImportDLLExport(llvm::GlobalValue *GV, GlobalDecl D) const;
void setDLLImportDLLExport(llvm::GlobalValue *GV, const NamedDecl *D) const;
/// Set visibility, dllimport/dllexport and dso_local.
Expand Down
16 changes: 9 additions & 7 deletions clang/lib/CodeGen/ItaniumCXXABI.cpp
Expand Up @@ -3680,12 +3680,14 @@ llvm::Constant *ItaniumRTTIBuilder::BuildTypeInfo(QualType Ty) {

llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
llvm::GlobalValue::DefaultStorageClass;
if (CGM.getTriple().isWindowsItaniumEnvironment()) {
auto RD = Ty->getAsCXXRecordDecl();
if (RD && RD->hasAttr<DLLExportAttr>())
if (auto RD = Ty->getAsCXXRecordDecl()) {
if ((CGM.getTriple().isWindowsItaniumEnvironment() &&
RD->hasAttr<DLLExportAttr>()) ||
(CGM.shouldMapVisibilityToDLLExport(RD) &&
!llvm::GlobalValue::isLocalLinkage(Linkage) &&
llvmVisibility == llvm::GlobalValue::DefaultVisibility))
DLLStorageClass = llvm::GlobalValue::DLLExportStorageClass;
}

return BuildTypeInfo(Ty, Linkage, llvmVisibility, DLLStorageClass);
}

Expand Down Expand Up @@ -4168,9 +4170,9 @@ void ItaniumCXXABI::EmitFundamentalRTTIDescriptors(const CXXRecordDecl *RD) {
getContext().Char32Ty
};
llvm::GlobalValue::DLLStorageClassTypes DLLStorageClass =
RD->hasAttr<DLLExportAttr>()
? llvm::GlobalValue::DLLExportStorageClass
: llvm::GlobalValue::DefaultStorageClass;
RD->hasAttr<DLLExportAttr>() || CGM.shouldMapVisibilityToDLLExport(RD)
? llvm::GlobalValue::DLLExportStorageClass
: llvm::GlobalValue::DefaultStorageClass;
llvm::GlobalValue::VisibilityTypes Visibility =
CodeGenModule::GetLLVMVisibility(RD->getVisibility());
for (const QualType &FundamentalType : FundamentalTypes) {
Expand Down
6 changes: 5 additions & 1 deletion clang/lib/CodeGen/MicrosoftCXXABI.cpp
Expand Up @@ -47,7 +47,11 @@ class MicrosoftCXXABI : public CGCXXABI {
: CGCXXABI(CGM), BaseClassDescriptorType(nullptr),
ClassHierarchyDescriptorType(nullptr),
CompleteObjectLocatorType(nullptr), CatchableTypeType(nullptr),
ThrowInfoType(nullptr) {}
ThrowInfoType(nullptr) {
assert(!(CGM.getLangOpts().isExplicitDefaultVisibilityExportMapping() ||
CGM.getLangOpts().isAllDefaultVisibilityExportMapping()) &&
"visibility export mapping option unimplemented in this ABI");
}

bool HasThisReturn(GlobalDecl GD) const override;
bool hasMostDerivedReturn(GlobalDecl GD) const override;
Expand Down
8 changes: 8 additions & 0 deletions clang/lib/Driver/ToolChains/Clang.cpp
Expand Up @@ -5982,6 +5982,14 @@ void Clang::ConstructJob(Compilation &C, const JobAction &JA,
<< A->getAsString(Args) << TripleStr;
}

if (const Arg *A =
Args.getLastArg(options::OPT_mdefault_visibility_export_mapping_EQ)) {
if (Triple.isOSAIX())
A->render(Args, CmdArgs);
else
D.Diag(diag::err_drv_unsupported_opt_for_target)
<< A->getAsString(Args) << TripleStr;
}

if (Args.hasFlag(options::OPT_fvisibility_inlines_hidden,
options::OPT_fno_visibility_inlines_hidden, false))
Expand Down
29 changes: 29 additions & 0 deletions clang/test/CodeGen/mdefault-visibility-export-mapping.c
@@ -0,0 +1,29 @@
// RUN: %clang_cc1 -triple powerpc-ibm-aix %s -S -emit-llvm -o - | \
// RUN: FileCheck -check-prefixes=UNSPECIFIED-DEF,EXPLICIT-DEF %s
// RUN: %clang_cc1 -triple powerpc-ibm-aix %s -mdefault-visibility-export-mapping=none -S -emit-llvm -o - | \
// RUN: FileCheck -check-prefixes=UNSPECIFIED-DEF,EXPLICIT-DEF %s
// RUN: %clang_cc1 -triple powerpc-ibm-aix %s -mdefault-visibility-export-mapping=explicit -S -emit-llvm -o - | \
// RUN: FileCheck -check-prefixes=UNSPECIFIED-DEF,EXPLICIT-EXP %s
// RUN: %clang_cc1 -triple powerpc-ibm-aix %s -mdefault-visibility-export-mapping=all -S -emit-llvm -o - | \
// RUN: FileCheck -check-prefixes=UNSPECIFIED-EXP,EXPLICIT-EXP %s
// RUN: %clang -target powerpc-ibm-aix %s -mdefault-visibility-export-mapping=all -fvisibility=hidden -S -emit-llvm -o - | \
// RUN: FileCheck -check-prefixes=UNSPECIFIED-HID,EXPLICIT-EXP %s

// RUN: not %clang -mdefault-visibility-export-mapping=explicit -target powerpc-unknown-linux %s 2>&1 | \
// RUN: FileCheck -check-prefix=ERROR %s
// ERROR: unsupported option '-mdefault-visibility-export-mapping=explicit' for target 'powerpc-unknown-linux'

// UNSPECIFIED-DEF: define void @func()
// UNSPECIFIED-HID: define hidden void @func()
// UNSPECIFIED-EXP: define dllexport void @func()
void func() {}

#pragma GCC visibility push(default)
// EXPLICIT-DEF: define void @pragmafunc()
// EXPLICIT-EXP: define dllexport void @pragmafunc()
void pragmafunc() {}
#pragma GCC visibility pop

// EXPLICIT-DEF: define void @explicitfunc()
// EXPLICIT-EXP: define dllexport void @explicitfunc()
void __attribute__((visibility("default"))) explicitfunc() {}
41 changes: 41 additions & 0 deletions clang/test/CodeGenCXX/mdefault-visibility-export-mapping-alias.cpp
@@ -0,0 +1,41 @@
// RUN: %clang_cc1 -triple powerpc-ibm-aix %s -mdefault-visibility-export-mapping=none -mconstructor-aliases -S -emit-llvm -o - | \
// RUN: FileCheck -check-prefixes=UNSPECIFIED-DEF,EXPLICIT-DEF %s
// RUN: %clang_cc1 -triple powerpc-ibm-aix %s -mdefault-visibility-export-mapping=explicit -mconstructor-aliases -S -emit-llvm -o - | \
// RUN: FileCheck -check-prefixes=UNSPECIFIED-DEF,EXPLICIT-EXP %s
// RUN: %clang_cc1 -triple powerpc-ibm-aix %s -mdefault-visibility-export-mapping=all -mconstructor-aliases -S -emit-llvm -o - | \
// RUN: FileCheck -check-prefixes=UNSPECIFIED-EXP,EXPLICIT-EXP %s

class A {
public:
~A();
};

A::~A() {}

class __attribute__((visibility("default"))) B {
public:
~B();
};

B::~B() {}

void func() {
A x;
B y;
}

// A::~A() (complete object destructor)
// UNSPECIFIED-DEF: @_ZN1AD1Ev = unnamed_addr alias
// UNSPECIFIED-EXP: @_ZN1AD1Ev = dllexport unnamed_addr alias

// B::~B() (complete object destructor)
// EXPLICIT-DEF: @_ZN1BD1Ev = unnamed_addr alias
// EXPLICIT-EXP: @_ZN1BD1Ev = dllexport unnamed_addr alias

// A::~A() (base object destructor)
// UNSPECIFIED-DEF: define void @_ZN1AD2Ev(
// UNSPECIFIED-EXP: define dllexport void @_ZN1AD2Ev(

// B::~B() (base object destructor)
// EXPLICIT-DEF: define void @_ZN1BD2Ev(
// EXPLICIT-EXP: define dllexport void @_ZN1BD2Ev(

0 comments on commit 6a86730

Please sign in to comment.