IR: Add assembly/bitcode support for function metadata attachments

Add serialization support for function metadata attachments (added in
r235783).  The syntax is:

    define @foo() !attach !0 {

Metadata attachments are only allowed on functions with bodies.  Since
they come before the `{`, they're not really part of the body; since
they require a body, they're not really part of the header.  In
`LLParser` I gave them a separate function called from `ParseDefine()`,
`ParseOptionalFunctionMetadata()`.

In bitcode, I'm using the same `METADATA_ATTACHMENT` record used by
instructions.  Instruction metadata attachments are included in a
special "attachment" block at the end of a `Function`.  The attachment
records are laid out like this:

    InstID (KindID MetadataID)+

Note that these records always have an odd number of fields.  The new
code takes advantage of this to recognize function attachments (which
don't need an instruction ID):

    (KindID MetadataID)+

This means we can use the same attachment block already used for
instructions.

This is part of PR23340.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@235785 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Duncan P. N. Exon Smith
2015-04-24 22:04:41 +00:00
parent ef477f3580
commit ae3211466a
7 changed files with 76 additions and 9 deletions

View File

@@ -776,12 +776,12 @@ void SlotTracker::processFunction() {
if (!BB.hasName())
CreateFunctionSlot(&BB);
processFunctionMetadata(*TheFunction);
for (auto &I : BB) {
if (!I.getType()->isVoidTy() && !I.hasName())
CreateFunctionSlot(&I);
processInstructionMetadata(I);
// We allow direct calls to any llvm.foo function here, because the
// target may not be linked into the optimizer.
if (const CallInst *CI = dyn_cast<CallInst>(&I)) {
@@ -804,9 +804,15 @@ void SlotTracker::processFunction() {
}
void SlotTracker::processFunctionMetadata(const Function &F) {
for (auto &BB : F)
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
for (auto &BB : F) {
F.getAllMetadata(MDs);
for (auto &MD : MDs)
CreateMetadataSlot(MD.second);
for (auto &I : BB)
processInstructionMetadata(I);
}
}
void SlotTracker::processInstructionMetadata(const Instruction &I) {
@@ -2541,6 +2547,10 @@ void AssemblyWriter::printFunction(const Function *F) {
writeOperand(F->getPrologueData(), true);
}
SmallVector<std::pair<unsigned, MDNode *>, 4> MDs;
F->getAllMetadata(MDs);
printMetadataAttachments(MDs, " ");
if (F->isDeclaration()) {
Out << '\n';
} else {