Skip to content

Commit ecf915e

Browse files
committed
Add a new metadata node rather then piggybacking on associated.
1 parent 0b4808c commit ecf915e

File tree

7 files changed

+117
-76
lines changed

7 files changed

+117
-76
lines changed

llvm/docs/LangRef.rst

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8337,13 +8337,6 @@ See :doc:`CalleeTypeMetadata`.
83378337

83388338
The ``associated`` metadata may be attached to a global variable definition with
83398339
a single argument that references a global object (optionally through an alias).
8340-
The metadata is often used with an explicit section consisting of valid C
8341-
identifiers so that the runtime can find the metadata section with
8342-
linker-defined encapsulation symbols ``__start_<section_name>`` and
8343-
``__stop_<section_name>``.
8344-
8345-
ELF Targets
8346-
"""""""""""
83478340

83488341
This metadata lowers to the ELF section flag ``SHF_LINK_ORDER`` which prevents
83498342
discarding of the global variable in linker GC unless the referenced object is
@@ -8361,6 +8354,12 @@ alive, but this many-to-one relationship is not representable. Moreover, if the
83618354
metadata is retained while the function is discarded, the linker will report an
83628355
error of a relocation referencing a discarded section.
83638356

8357+
The metadata is often used with an explicit section consisting of valid C
8358+
identifiers so that the runtime can find the metadata section with
8359+
linker-defined encapsulation symbols ``__start_<section_name>`` and
8360+
``__stop_<section_name>``.
8361+
8362+
It does not have any effect on non-ELF targets.
83648363

83658364
Example:
83668365

@@ -8371,23 +8370,6 @@ Example:
83718370
@b = internal global i32 2, comdat $a, section "abc", !associated !0
83728371
!0 = !{ptr @a}
83738372

8374-
XCOFF Targets
8375-
"""""""""""""
8376-
8377-
This metadata lowers to the .ref assembly directive which will add a relocation
8378-
representing an implicit reference from the section the global belongs to, to
8379-
the associated symbol. This link will keep the associated symbol alive if the
8380-
section is not garbage collected. More than one associated node can be attached
8381-
to the same global variable.
8382-
8383-
Example:
8384-
.. code-block:: text
8385-
8386-
@a = global i32 1
8387-
@b = global i32 2
8388-
@c = global i32 3, section "abc", !associated !0, !associated !1
8389-
!0 = !{ptr @a}
8390-
!1 = !{ptr @b}
83918373

83928374
'``prof``' Metadata
83938375
^^^^^^^^^^^^^^^^^^^
@@ -8688,6 +8670,36 @@ denoting if the type contains a pointer.
86888670

86898671
!0 = !{!"<type-name>", i1 <contains-pointer>}
86908672

8673+
'``ref``' Metadata
8674+
^^^^^^^^^^^^^^^^^^
8675+
8676+
The ``ref`` metadata may be attached to a global variable definition with a
8677+
single argument that references a global object. The metadata is lowered to a
8678+
.ref directive which will emit a relocation introducing an explicit dependence
8679+
to the referenced symbol. This is typically used when there is some implicit
8680+
dependence between the symbols that is otherwise opaque to the linker. One such
8681+
example is metadata which is accessed by a runtime with associated
8682+
``__start_<section_name>`` and ``__stop_<section_name>`` symbols.
8683+
8684+
This metadata lowers to the .ref assembly directive which will add a relocation
8685+
representing an implicit reference from the section the global belongs to, to
8686+
the associated symbol. This link will keep the associated symbol alive if the
8687+
section is not garbage collected. More than one associated node can be attached
8688+
to the same global variable.
8689+
8690+
It does not have any effect on non-XCOFF targets.
8691+
8692+
Example:
8693+
8694+
.. code-block:: text
8695+
8696+
@a = global i32 1
8697+
@b = global i32 2
8698+
@c = global i32 3, section "abc", !ref !0, !ref !1
8699+
!0 = !{ptr @a}
8700+
!1 = !{ptr @b}
8701+
8702+
86918703
Module Flags Metadata
86928704
=====================
86938705

llvm/include/llvm/IR/FixedMetadataKinds.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,3 +57,4 @@ LLVM_FIXED_MD_KIND(MD_callee_type, "callee_type", 42)
5757
LLVM_FIXED_MD_KIND(MD_nofree, "nofree", 43)
5858
LLVM_FIXED_MD_KIND(MD_captures, "captures", 44)
5959
LLVM_FIXED_MD_KIND(MD_alloc_token, "alloc_token", 45)
60+
LLVM_FIXED_MD_KIND(MD_ref, "ref", 46)

llvm/lib/IR/Verifier.cpp

Lines changed: 42 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -740,36 +740,25 @@ void Verifier::visitGlobalValue(const GlobalValue &GV) {
740740
"Global is external, but doesn't have external or weak linkage!", &GV);
741741

742742
if (const GlobalObject *GO = dyn_cast<GlobalObject>(&GV)) {
743-
if (GO->hasMetadata(LLVMContext::MD_associated)) {
744-
SmallVector<MDNode *, 1> MDs;
745-
GO->getMetadata(LLVMContext::MD_associated, MDs);
746-
747-
if (TT.isOSBinFormatELF())
748-
Check(MDs.size() == 1, "only a single associated metadata is supported",
749-
&GV);
750-
751-
for (const MDNode *Associated : MDs) {
752-
Check(Associated->getNumOperands() == 1,
753-
"associated metadata must have one operand", &GV, Associated);
754-
const Metadata *Op = Associated->getOperand(0).get();
755-
Check(Op, "associated metadata must have a global value", GO,
743+
if (const MDNode *Associated =
744+
GO->getMetadata(LLVMContext::MD_associated)) {
745+
Check(Associated->getNumOperands() == 1,
746+
"associated metadata must have one operand", &GV, Associated);
747+
const Metadata *Op = Associated->getOperand(0).get();
748+
Check(Op, "associated metadata must have a global value", GO, Associated);
749+
750+
const auto *VM = dyn_cast_or_null<ValueAsMetadata>(Op);
751+
Check(VM, "associated metadata must be ValueAsMetadata", GO, Associated);
752+
if (VM) {
753+
Check(isa<PointerType>(VM->getValue()->getType()),
754+
"associated value must be pointer typed", GV, Associated);
755+
756+
const Value *Stripped = VM->getValue()->stripPointerCastsAndAliases();
757+
Check(isa<GlobalObject>(Stripped) || isa<Constant>(Stripped),
758+
"associated metadata must point to a GlobalObject", GO, Stripped);
759+
Check(Stripped != GO,
760+
"global values should not associate to themselves", GO,
756761
Associated);
757-
758-
const auto *VM = dyn_cast_or_null<ValueAsMetadata>(Op);
759-
Check(VM, "associated metadata must be ValueAsMetadata", GO,
760-
Associated);
761-
if (VM) {
762-
Check(isa<PointerType>(VM->getValue()->getType()),
763-
"associated value must be pointer typed", GV, Associated);
764-
765-
const Value *Stripped = VM->getValue()->stripPointerCastsAndAliases();
766-
Check(isa<GlobalObject>(Stripped) || isa<Constant>(Stripped),
767-
"associated metadata must point to a GlobalObject", GO,
768-
Stripped);
769-
Check(Stripped != GO,
770-
"global values should not associate to themselves", GO,
771-
Associated);
772-
}
773762
}
774763
}
775764

@@ -780,6 +769,30 @@ void Verifier::visitGlobalValue(const GlobalValue &GV) {
780769
DL.getIntPtrType(GO->getType()),
781770
RangeLikeMetadataKind::AbsoluteSymbol);
782771
}
772+
773+
if (GO->hasMetadata(LLVMContext::MD_ref)) {
774+
SmallVector<MDNode *> MDs;
775+
GO->getMetadata(LLVMContext::MD_ref, MDs);
776+
for (const MDNode *MD : MDs) {
777+
Check(MD->getNumOperands() == 1, "ref metadata must have one operand",
778+
&GV, MD);
779+
const Metadata *Op = MD->getOperand(0).get();
780+
Check(Op, "ref metadata must have a global value", GO, MD);
781+
782+
const auto *VM = dyn_cast_or_null<ValueAsMetadata>(Op);
783+
Check(VM, "ref metadata must be ValueAsMetadata", GO, MD);
784+
if (VM) {
785+
Check(isa<PointerType>(VM->getValue()->getType()),
786+
"ref value must be pointer typed", GV, MD);
787+
788+
const Value *Stripped = VM->getValue()->stripPointerCastsAndAliases();
789+
Check(isa<GlobalObject>(Stripped) || isa<Constant>(Stripped),
790+
"ref metadata must point to a GlobalObject", GO, Stripped);
791+
Check(Stripped != GO, "values should not reference themselves", GO,
792+
MD);
793+
}
794+
}
795+
}
783796
}
784797

785798
Check(!GV.hasAppendingLinkage() || isa<GlobalVariable>(GV),

llvm/lib/Target/PowerPC/PPCAsmPrinter.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ class PPCAIXAsmPrinter : public PPCAsmPrinter {
306306

307307
void emitModuleCommandLines(Module &M) override;
308308

309-
void emitAssociatedMetadata(const GlobalObject *);
309+
void emitRefMetadata(const GlobalObject *);
310310
};
311311

312312
} // end anonymous namespace
@@ -2803,8 +2803,8 @@ void PPCAIXAsmPrinter::emitGlobalVariableHelper(const GlobalVariable *GV) {
28032803
// Switch to the containing csect.
28042804
OutStreamer->switchSection(Csect);
28052805

2806-
if (GV->hasMetadata(LLVMContext::MD_associated)) {
2807-
emitAssociatedMetadata(GV);
2806+
if (GV->hasMetadata(LLVMContext::MD_ref)) {
2807+
emitRefMetadata(GV);
28082808
}
28092809

28102810
const DataLayout &DL = GV->getDataLayout();
@@ -3342,15 +3342,15 @@ void PPCAIXAsmPrinter::emitTTypeReference(const GlobalValue *GV,
33423342
OutStreamer->emitIntValue(0, GetSizeOfEncodedValue(Encoding));
33433343
}
33443344

3345-
void PPCAIXAsmPrinter::emitAssociatedMetadata(const GlobalObject *GO) {
3345+
void PPCAIXAsmPrinter::emitRefMetadata(const GlobalObject *GO) {
33463346
SmallVector<MDNode *> MDs;
3347-
GO->getMetadata(LLVMContext::MD_associated, MDs);
3347+
GO->getMetadata(LLVMContext::MD_ref, MDs);
33483348
assert(MDs.size() && "Expected asscoiated metadata nodes");
33493349

33503350
for (const MDNode *MD : MDs) {
33513351
const ValueAsMetadata *VAM = cast<ValueAsMetadata>(MD->getOperand(0).get());
3352-
const GlobalValue *Associated = cast<GlobalValue>(VAM->getValue());
3353-
MCSymbol *Referenced = TM.getSymbol(Associated);
3352+
const GlobalValue *GV = cast<GlobalValue>(VAM->getValue());
3353+
MCSymbol *Referenced = TM.getSymbol(GV);
33543354
OutStreamer->emitXCOFFRefDirective(Referenced);
33553355
}
33563356
}

llvm/test/CodeGen/PowerPC/aix-associated-metadata.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,9 @@
1010
@a = global i32 1
1111
@b = global i32 2
1212
@c = global i32 3, section "custom_section_c"
13-
@d = global i32 4, !associated !0
14-
@e = constant i32 5, !associated !1, !associated !2
15-
@f = global i32 6, section "custom_section_f", !associated !1
13+
@d = global i32 4, !ref !0
14+
@e = constant i32 5, !ref !1, !ref!2
15+
@f = global i32 6, section "custom_section_f", !ref !1
1616

1717

1818
!0 = !{ptr @a}

llvm/test/Verifier/associated-multiple.ll

Lines changed: 0 additions & 13 deletions
This file was deleted.

llvm/test/Verifier/ref.ll

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
; RUN: not llvm-as -disable-output < %s -o /dev/null 2>&1 | FileCheck %s
2+
3+
@a = global i32 1, !ref !0
4+
@b = global i32 2, !ref !1
5+
@c = global i32 3, !ref !1, !ref !2
6+
@d = global i32 4, !ref !3
7+
8+
!0 = !{i32 1}
9+
!1 = !{ptr @b}
10+
!2 = !{!"Hello World!"}
11+
!3 = !{ptr @c, ptr @a}
12+
13+
; CHECK: ref value must be pointer typed
14+
; CHECK: ptr @a
15+
; CHECK: !0 = !{i32 1}
16+
17+
; CHECK: values should not reference themselves
18+
; CHECK: ptr @b
19+
; CHECK: !1 = !{ptr @b}
20+
21+
; CHECK: ref metadata must be ValueAsMetadata
22+
; CHECK: ptr @c
23+
; CHECK: !2 = !{!"Hello World!"}
24+
25+
; CHECK: ref metadata must have one operand
26+
; CHECK: ptr @d
27+
; CHECK: !3 = !{ptr @c, ptr @a}
28+

0 commit comments

Comments
 (0)