Skip to content

Commit 8042699

Browse files
committed
[LLVM] Add exported visibility style for XCOFF
For the AIX linker, under default options, global or weak symbols which have no visibility bits set to zero (i.e. no visibility, similar to ELF default) are only exported if specified on an export list provided to the linker. So AIX has an additional visibility style called "exported" which indicates to the linker that the symbol should be explicitly globally exported. This change maps "dllexport" in the LLVM IR to correspond to XCOFF exported as we feel this best models the intended semantic (discussion on the discourse RFC thread: https://discourse.llvm.org/t/rfc-adding-exported-visibility-style-to-the-ir-to-model-xcoff-exported-visibility/61853) and allows us to enable writing this visibility for the AIX target in the assembly path. Reviewed By: DiggerLin Differential Revision: https://reviews.llvm.org/D123951
1 parent ded8187 commit 8042699

File tree

11 files changed

+74
-8
lines changed

11 files changed

+74
-8
lines changed

llvm/docs/LangRef.rst

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -481,7 +481,10 @@ styles:
481481
means that the declaration is visible to other modules and, in
482482
shared libraries, means that the declared entity may be overridden.
483483
On Darwin, default visibility means that the declaration is visible
484-
to other modules. Default visibility corresponds to "external
484+
to other modules. On XCOFF, default visibility means no explicit
485+
visibility bit will be set and whether the symbol is visible
486+
(i.e "exported") to other modules depends primarily on export lists
487+
provided to the linker. Default visibility corresponds to "external
485488
linkage" in the language.
486489
"``hidden``" - Hidden style
487490
Two declarations of an object with hidden visibility refer to the
@@ -512,12 +515,15 @@ DLL storage class:
512515
symbol. On Microsoft Windows targets, the pointer name is formed by
513516
combining ``__imp_`` and the function or variable name.
514517
``dllexport``
515-
"``dllexport``" causes the compiler to provide a global pointer to a pointer
516-
in a DLL, so that it can be referenced with the ``dllimport`` attribute. On
517-
Microsoft Windows targets, the pointer name is formed by combining
518-
``__imp_`` and the function or variable name. Since this storage class
519-
exists for defining a dll interface, the compiler, assembler and linker know
520-
it is externally referenced and must refrain from deleting the symbol.
518+
On Microsoft Windows targets, "``dllexport``" causes the compiler to provide
519+
a global pointer to a pointer in a DLL, so that it can be referenced with the
520+
``dllimport`` attribute. the pointer name is formed by combining ``__imp_``
521+
and the function or variable name. On XCOFF targets, ``dllexport`` indicates
522+
that the symbol will be made visible to other modules using "exported"
523+
visibility and thus placed by the linker in the loader section symbol table.
524+
Since this storage class exists for defining a dll interface, the compiler,
525+
assembler and linker know it is externally referenced and must refrain from
526+
deleting the symbol.
521527

522528
.. _tls_model:
523529

llvm/include/llvm/MC/MCAsmInfo.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,10 @@ class MCAsmInfo {
430430
/// hidden visibility. Defaults to MCSA_Hidden.
431431
MCSymbolAttr HiddenVisibilityAttr = MCSA_Hidden;
432432

433+
/// This attribute, if not MCSA_Invalid, is used to declare a symbol as having
434+
/// exported visibility. Defaults to MCSA_Exported.
435+
MCSymbolAttr ExportedVisibilityAttr = MCSA_Exported;
436+
433437
/// This attribute, if not MCSA_Invalid, is used to declare an undefined
434438
/// symbol as having hidden visibility. Defaults to MCSA_Hidden.
435439
MCSymbolAttr HiddenDeclarationVisibilityAttr = MCSA_Hidden;
@@ -758,6 +762,8 @@ class MCAsmInfo {
758762

759763
MCSymbolAttr getHiddenVisibilityAttr() const { return HiddenVisibilityAttr; }
760764

765+
MCSymbolAttr getExportedVisibilityAttr() const { return ExportedVisibilityAttr; }
766+
761767
MCSymbolAttr getHiddenDeclarationVisibilityAttr() const {
762768
return HiddenDeclarationVisibilityAttr;
763769
}

llvm/include/llvm/MC/MCDirectives.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ enum MCSymbolAttr {
3131
MCSA_LGlobal, ///< .lglobl (XCOFF)
3232
MCSA_Extern, ///< .extern (XCOFF)
3333
MCSA_Hidden, ///< .hidden (ELF)
34+
MCSA_Exported, ///< .globl _foo, exported (XCOFF)
3435
MCSA_IndirectSymbol, ///< .indirect_symbol (MachO)
3536
MCSA_Internal, ///< .internal (ELF)
3637
MCSA_LazyReference, ///< .lazy_reference (MachO)

llvm/lib/MC/MCAsmStreamer.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -762,6 +762,8 @@ bool MCAsmStreamer::emitSymbolAttribute(MCSymbol *Symbol,
762762
case MCSA_WeakDefAutoPrivate: OS << "\t.weak_def_can_be_hidden\t"; break;
763763
case MCSA_Cold:
764764
// Assemblers currently do not support a .cold directive.
765+
case MCSA_Exported:
766+
// Non-AIX assemblers currently do not support exported visibility.
765767
return false;
766768
}
767769

@@ -904,6 +906,9 @@ void MCAsmStreamer::emitXCOFFSymbolLinkageWithVisibility(
904906
case MCSA_Protected:
905907
OS << ",protected";
906908
break;
909+
case MCSA_Exported:
910+
OS << ",exported";
911+
break;
907912
default:
908913
report_fatal_error("unexpected value for Visibility type");
909914
}

llvm/lib/MC/MCELFStreamer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,7 @@ bool MCELFStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
215215
case MCSA_WeakDefAutoPrivate:
216216
case MCSA_Invalid:
217217
case MCSA_IndirectSymbol:
218+
case MCSA_Exported:
218219
return false;
219220

220221
case MCSA_NoDeadStrip:

llvm/lib/MC/MCMachOStreamer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -358,6 +358,7 @@ bool MCMachOStreamer::emitSymbolAttribute(MCSymbol *Sym,
358358
case MCSA_Weak:
359359
case MCSA_Local:
360360
case MCSA_LGlobal:
361+
case MCSA_Exported:
361362
return false;
362363

363364
case MCSA_Global:

llvm/lib/MC/MCWasmStreamer.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,7 @@ bool MCWasmStreamer::emitSymbolAttribute(MCSymbol *S, MCSymbolAttr Attribute) {
121121
case MCSA_Invalid:
122122
case MCSA_IndirectSymbol:
123123
case MCSA_Protected:
124+
case MCSA_Exported:
124125
return false;
125126

126127
case MCSA_Hidden:

llvm/lib/MC/MCXCOFFStreamer.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,9 @@ bool MCXCOFFStreamer::emitSymbolAttribute(MCSymbol *Sym,
5656
case llvm::MCSA_Protected:
5757
Symbol->setVisibilityType(XCOFF::SYM_V_PROTECTED);
5858
break;
59+
case llvm::MCSA_Exported:
60+
Symbol->setVisibilityType(XCOFF::SYM_V_EXPORTED);
61+
break;
5962
default:
6063
report_fatal_error("Not implemented yet.");
6164
}

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1898,10 +1898,15 @@ void PPCAIXAsmPrinter::emitLinkage(const GlobalValue *GV,
18981898

18991899
MCSymbolAttr VisibilityAttr = MCSA_Invalid;
19001900
if (!TM.getIgnoreXCOFFVisibility()) {
1901+
if (GV->hasDLLExportStorageClass() && !GV->hasDefaultVisibility())
1902+
report_fatal_error(
1903+
"Cannot not be both dllexport and non-default visibility");
19011904
switch (GV->getVisibility()) {
19021905

1903-
// TODO: "exported" and "internal" Visibility needs to go here.
1906+
// TODO: "internal" Visibility needs to go here.
19041907
case GlobalValue::DefaultVisibility:
1908+
if (GV->hasDLLExportStorageClass())
1909+
VisibilityAttr = MAI->getExportedVisibilityAttr();
19051910
break;
19061911
case GlobalValue::HiddenVisibility:
19071912
VisibilityAttr = MAI->getHiddenVisibilityAttr();
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
; RUN: not --crash llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff 2>&1 < %s | \
2+
; RUN: FileCheck %s
3+
; RUN: not --crash llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff 2>&1 < %s |\
4+
; RUN: FileCheck %s
5+
6+
; RUN: not --crash llc -verify-machineinstrs -mtriple powerpc-ibm-aix-xcoff \
7+
; RUN: -filetype=obj -o %t.o < %s 2>&1 | \
8+
; RUN: FileCheck %s
9+
10+
; RUN: not --crash llc -verify-machineinstrs -mtriple powerpc64-ibm-aix-xcoff \
11+
; RUN: -filetype=obj -o %t.o 2>&1 < %s 2>&1 | \
12+
; RUN: FileCheck %s
13+
14+
; CHECK: LLVM ERROR: Cannot not be both dllexport and non-default visibility
15+
@b_e = hidden dllexport global i32 0, align 4

0 commit comments

Comments
 (0)