Skip to content

Commit 81a9c97

Browse files
author
Wael Yehia
committed
[IR] enable attaching metadata on ifuncs
In PR #153049, we have a use case of attaching the !associated metadata to an ifunc. Since an ifunc is similar to a function declaration, it seems natural to allow metadata on ifuncs. Currently, the metadata API allows adding Metadata to llvm::Values, so the in-memory IR allows for metadata on ifuncs, but the IR reader/writer is not aware of that. Teach the IR parser and writer to support metadata on ifuncs, and update documentation.
1 parent ccd35e5 commit 81a9c97

File tree

6 files changed

+30
-3
lines changed

6 files changed

+30
-3
lines changed

llvm/docs/LangRef.rst

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1020,13 +1020,14 @@ On ELF platforms, IFuncs are resolved by the dynamic linker at load time. On
10201020
Mach-O platforms, they are lowered in terms of ``.symbol_resolver`` functions,
10211021
which lazily resolve the callee the first time they are called.
10221022

1023-
IFunc may have an optional :ref:`linkage type <linkage>` and an optional
1024-
:ref:`visibility style <visibility>`.
1023+
IFunc may have an optional :ref:`linkage type <linkage>`, an optional
1024+
:ref:`visibility style <visibility>`, an option partition, and an optional
1025+
list of attached :ref:`metadata <metadata>`.
10251026

10261027
Syntax::
10271028

10281029
@<Name> = [Linkage] [PreemptionSpecifier] [Visibility] ifunc <IFuncTy>, <ResolverTy>* @<Resolver>
1029-
[, partition "name"]
1030+
[, partition "name"] (, !name !N)*
10301031

10311032

10321033
.. _langref_comdats:

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1258,6 +1258,9 @@ bool LLParser::parseAliasOrIFunc(const std::string &Name, unsigned NameID,
12581258
GV->setPartition(Lex.getStrVal());
12591259
if (parseToken(lltok::StringConstant, "expected partition string"))
12601260
return true;
1261+
} else if (!IsAlias && Lex.getKind() == lltok::MetadataVar) {
1262+
if (parseGlobalObjectMetadataAttachment(*GI.get()))
1263+
return true;
12611264
} else {
12621265
return tokError("unknown alias or ifunc property!");
12631266
}

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2630,6 +2630,9 @@ void ModuleBitcodeWriter::writeModuleMetadata() {
26302630
for (const Function &F : M)
26312631
if (F.isDeclaration() && F.hasMetadata())
26322632
AddDeclAttachedMetadata(F);
2633+
for (const GlobalIFunc &GI : M.ifuncs())
2634+
if (GI.hasMetadata())
2635+
AddDeclAttachedMetadata(GI);
26332636
// FIXME: Only store metadata for declarations here, and move data for global
26342637
// variable definitions to a separate block (PR28134).
26352638
for (const GlobalVariable &GV : M.globals())

llvm/lib/Bitcode/Writer/ValueEnumerator.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -495,6 +495,12 @@ ValueEnumerator::ValueEnumerator(const Module &M,
495495
EnumerateMetadata(&F, Op);
496496
}
497497
}
498+
for (const GlobalIFunc &GIF : M.ifuncs()) {
499+
MDs.clear();
500+
GIF.getAllMetadata(MDs);
501+
for (const auto &I : MDs)
502+
EnumerateMetadata(nullptr, I.second);
503+
}
498504

499505
// Optimize constant ordering.
500506
OptimizeConstants(FirstConstant, Values.size());

llvm/lib/IR/AsmWriter.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1078,6 +1078,7 @@ void SlotTracker::processModule() {
10781078
for (const GlobalIFunc &I : TheModule->ifuncs()) {
10791079
if (!I.hasName())
10801080
CreateModuleSlot(&I);
1081+
processGlobalObjectMetadata(I);
10811082
}
10821083

10831084
// Add metadata used by named metadata.
@@ -4077,6 +4078,11 @@ void AssemblyWriter::printIFunc(const GlobalIFunc *GI) {
40774078
printEscapedString(GI->getPartition(), Out);
40784079
Out << '"';
40794080
}
4081+
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
4082+
GI->getAllMetadata(MDs);
4083+
if (!MDs.empty()) {
4084+
printMetadataAttachments(MDs, ", ");
4085+
}
40804086

40814087
printInfoComment(*GI);
40824088
Out << '\n';

llvm/test/Assembler/metadata.ll

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,14 @@
55
; CHECK-UNMAT: @global = global i32 0, !foo [[M2:![0-9]+]], !foo [[M3:![0-9]+]], !baz [[M3]]
66
@global = global i32 0, !foo !2, !foo !3, !baz !3
77

8+
; CHECK-UNMAT: @ifunc_func = ifunc void (...), ptr @resolver, !foo [[M2]]
9+
@ifunc_func = ifunc void (...), ptr @resolver, !foo !2
10+
11+
define internal ptr @resolver() {
12+
entry:
13+
ret ptr @test
14+
}
15+
816
; CHECK-LABEL: @test
917
; CHECK: ret void, !foo [[M0:![0-9]+]], !bar [[M1:![0-9]+]]
1018
define void @test() !dbg !1 {

0 commit comments

Comments
 (0)