From 1c7eea60d3cbada2bae54c42fa178078a3ace9b3 Mon Sep 17 00:00:00 2001 From: Devang Patel Date: Wed, 8 Jul 2009 19:23:54 +0000 Subject: [PATCH] Support MDNode forward reference. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@75031 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/AsmParser/LLParser.cpp | 35 ++++++++++++++++++++++++++++++++--- lib/AsmParser/LLParser.h | 2 ++ test/Feature/mdnode4.ll | 5 +++++ 3 files changed, 39 insertions(+), 3 deletions(-) create mode 100644 test/Feature/mdnode4.ll diff --git a/lib/AsmParser/LLParser.cpp b/lib/AsmParser/LLParser.cpp index 3927cf1e9e9..44eb5b81cfb 100644 --- a/lib/AsmParser/LLParser.cpp +++ b/lib/AsmParser/LLParser.cpp @@ -84,6 +84,12 @@ bool LLParser::ValidateEndOfModule() { "use of undefined value '@" + utostr(ForwardRefValIDs.begin()->first) + "'"); + if (!ForwardRefMDNodes.empty()) + return Error(ForwardRefMDNodes.begin()->second.second, + "use of undefined metadata '!" + + utostr(ForwardRefMDNodes.begin()->first) + "'"); + + // Look for intrinsic functions and CallInst that need to be upgraded for (Module::iterator FI = M->begin(), FE = M->end(); FI != FE; ) UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove @@ -382,6 +388,14 @@ bool LLParser::ParseStandaloneMetadata() { return true; MetadataCache[MetadataID] = Init; + std::map >::iterator + FI = ForwardRefMDNodes.find(MetadataID); + if (FI != ForwardRefMDNodes.end()) { + Constant *FwdNode = FI->second.first; + FwdNode->replaceAllUsesWith(Init); + ForwardRefMDNodes.erase(FI); + } + return false; } @@ -1632,9 +1646,24 @@ bool LLParser::ParseValID(ValID &ID) { unsigned MID = 0; if (!ParseUInt32(MID)) { std::map::iterator I = MetadataCache.find(MID); - if (I == MetadataCache.end()) - return TokError("Unknown metadata reference"); - ID.ConstantVal = I->second; + if (I != MetadataCache.end()) + ID.ConstantVal = I->second; + else { + std::map >::iterator + FI = ForwardRefMDNodes.find(MID); + if (FI != ForwardRefMDNodes.end()) + ID.ConstantVal = FI->second.first; + else { + // Create MDNode forward reference + SmallVector Elts; + std::string FwdRefName = "llvm.mdnode.fwdref." + MID; + Elts.push_back(Context.getMDString(FwdRefName)); + MDNode *FwdNode = Context.getMDNode(Elts.data(), Elts.size()); + ForwardRefMDNodes[MID] = std::make_pair(FwdNode, Lex.getLoc()); + ID.ConstantVal = FwdNode; + } + } + return false; } diff --git a/lib/AsmParser/LLParser.h b/lib/AsmParser/LLParser.h index 1f053c29082..30a49cd8848 100644 --- a/lib/AsmParser/LLParser.h +++ b/lib/AsmParser/LLParser.h @@ -46,6 +46,8 @@ namespace llvm { std::vector NumberedTypes; /// MetadataCache - This map keeps track of parsed metadata constants. std::map MetadataCache; + std::map > ForwardRefMDNodes; + struct UpRefRecord { /// Loc - This is the location of the upref. LocTy Loc; diff --git a/test/Feature/mdnode4.ll b/test/Feature/mdnode4.ll new file mode 100644 index 00000000000..c0549491003 --- /dev/null +++ b/test/Feature/mdnode4.ll @@ -0,0 +1,5 @@ +; Test forward MDNode reference +; RUN: llvm-as < %s | llvm-dis -f -o /dev/null + +@llvm.blah = constant metadata !{metadata !1} +!1 = constant metadata !{i32 23, i32 24} -- 2.34.1