llvm.org GIT mirror llvm / d31dc69
AsmParser: Extend the API to make the global value and metadata node slot mappings publicly accessible. This commit creates a new structure called 'SlotMapping' in the AsmParser library. This structure can be passed into the public parsing APIs from the AsmParser library in order to extract the data structures that map from slot numbers to unnamed global values and metadata nodes. This change is useful for MIR Serialization, as the MIR Parser has to lookup the unnamed global values and metadata nodes by their slot numbers. Reviewers: Duncan P. N. Exon Smith Differential Revision: http://reviews.llvm.org/D10551 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@240427 91177308-0d34-0410-b5e6-96231b3b80d8 Alex Lorenz 5 years ago
6 changed file(s) with 114 addition(s) and 32 deletion(s). Raw diff Collapse all Expand all
1717
1818 namespace llvm {
1919
20 class LLVMContext;
2021 class Module;
22 struct SlotMapping;
2123 class SMDiagnostic;
22 class LLVMContext;
2324
2425 /// This function is the main interface to the LLVM Assembly Parser. It parses
2526 /// an ASCII file that (presumably) contains LLVM Assembly code. It returns a
2627 /// Module (intermediate representation) with the corresponding features. Note
2728 /// that this does not verify that the generated Module is valid, so you should
2829 /// run the verifier after parsing the file to check that it is okay.
29 /// @brief Parse LLVM Assembly from a file
30 /// @param Filename The name of the file to parse
31 /// @param Error Error result info.
32 /// @param Context Context in which to allocate globals info.
30 /// \brief Parse LLVM Assembly from a file
31 /// \param Filename The name of the file to parse
32 /// \param Error Error result info.
33 /// \param Context Context in which to allocate globals info.
34 /// \param Slots The optional slot mapping that will be initialized during
35 /// parsing.
3336 std::unique_ptr parseAssemblyFile(StringRef Filename,
3437 SMDiagnostic &Error,
35 LLVMContext &Context);
38 LLVMContext &Context,
39 SlotMapping *Slots = nullptr);
3640
3741 /// The function is a secondary interface to the LLVM Assembly Parser. It parses
3842 /// an ASCII string that (presumably) contains LLVM Assembly code. It returns a
3943 /// Module (intermediate representation) with the corresponding features. Note
4044 /// that this does not verify that the generated Module is valid, so you should
4145 /// run the verifier after parsing the file to check that it is okay.
42 /// @brief Parse LLVM Assembly from a string
43 /// @param AsmString The string containing assembly
44 /// @param Error Error result info.
45 /// @param Context Context in which to allocate globals info.
46 /// \brief Parse LLVM Assembly from a string
47 /// \param AsmString The string containing assembly
48 /// \param Error Error result info.
49 /// \param Context Context in which to allocate globals info.
50 /// \param Slots The optional slot mapping that will be initialized during
51 /// parsing.
4652 std::unique_ptr parseAssemblyString(StringRef AsmString,
4753 SMDiagnostic &Error,
48 LLVMContext &Context);
54 LLVMContext &Context,
55 SlotMapping *Slots = nullptr);
4956
5057 /// parseAssemblyFile and parseAssemblyString are wrappers around this function.
51 /// @brief Parse LLVM Assembly from a MemoryBuffer.
52 /// @param F The MemoryBuffer containing assembly
53 /// @param Err Error result info.
54 /// @param Context Context in which to allocate globals info.
58 /// \brief Parse LLVM Assembly from a MemoryBuffer.
59 /// \param F The MemoryBuffer containing assembly
60 /// \param Err Error result info.
61 /// \param Slots The optional slot mapping that will be initialized during
62 /// parsing.
5563 std::unique_ptr parseAssembly(MemoryBufferRef F, SMDiagnostic &Err,
56 LLVMContext &Context);
64 LLVMContext &Context,
65 SlotMapping *Slots = nullptr);
5766
5867 /// This function is the low-level interface to the LLVM Assembly Parser.
5968 /// This is kept as an independent function instead of being inlined into
6069 /// parseAssembly for the convenience of interactive users that want to add
6170 /// recently parsed bits to an existing module.
6271 ///
63 /// @param F The MemoryBuffer containing assembly
64 /// @param M The module to add data to.
65 /// @param Err Error result info.
66 /// @return true on error.
67 bool parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err);
72 /// \param F The MemoryBuffer containing assembly
73 /// \param M The module to add data to.
74 /// \param Err Error result info.
75 /// \param Slots The optional slot mapping that will be initialized during
76 /// parsing.
77 /// \return true on error.
78 bool parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err,
79 SlotMapping *Slots = nullptr);
6880
6981 } // End llvm namespace
7082
0 //===-- SlotMapping.h - Slot number mapping for unnamed values --*- C++ -*-===//
1 //
2 // The LLVM Compiler Infrastructure
3 //
4 // This file is distributed under the University of Illinois Open Source
5 // License. See LICENSE.TXT for details.
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file contains the declaration of the SlotMapping struct.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef LLVM_ASMPARSER_SLOTMAPPING_H
14 #define LLVM_ASMPARSER_SLOTMAPPING_H
15
16 #include "llvm/IR/TrackingMDRef.h"
17 #include
18 #include
19
20 namespace llvm {
21
22 class GlobalValue;
23
24 /// This struct contains the mapping from the slot numbers to unnamed metadata
25 /// nodes and global values.
26 struct SlotMapping {
27 std::vector GlobalValues;
28 std::map MetadataNodes;
29 };
30
31 } // end namespace llvm
32
33 #endif
1212
1313 #include "LLParser.h"
1414 #include "llvm/ADT/SmallPtrSet.h"
15 #include "llvm/AsmParser/SlotMapping.h"
1516 #include "llvm/IR/AutoUpgrade.h"
1617 #include "llvm/IR/CallingConv.h"
1718 #include "llvm/IR/Constants.h"
159160 UpgradeCallsToIntrinsic(FI++); // must be post-increment, as we remove
160161
161162 UpgradeDebugInfo(*M);
163
164 if (!Slots)
165 return false;
166 // Initialize the slot mapping.
167 // Because by this point we've parsed and validated everything, we can "steal"
168 // the mapping from LLParser as it doesn't need it anymore.
169 Slots->GlobalValues = std::move(NumberedVals);
170 Slots->MetadataNodes = std::move(NumberedMetadata);
162171
163172 return false;
164173 }
3636 class Comdat;
3737 class MDString;
3838 class MDNode;
39 struct SlotMapping;
3940 class StructType;
4041
4142 /// ValID - Represents a reference of a definition of some sort with no type.
8687 LLVMContext &Context;
8788 LLLexer Lex;
8889 Module *M;
90 SlotMapping *Slots;
8991
9092 // Instruction metadata resolution. Each instruction can have a list of
9193 // MDRef info associated with them.
134136 std::map NumberedAttrBuilders;
135137
136138 public:
137 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *m)
138 : Context(m->getContext()), Lex(F, SM, Err, m->getContext()), M(m),
139 BlockAddressPFS(nullptr) {}
139 LLParser(StringRef F, SourceMgr &SM, SMDiagnostic &Err, Module *M,
140 SlotMapping *Slots = nullptr)
141 : Context(M->getContext()), Lex(F, SM, Err, M->getContext()), M(M),
142 Slots(Slots), BlockAddressPFS(nullptr) {}
140143 bool Run();
141144
142145 LLVMContext &getContext() { return Context; }
2121 #include
2222 using namespace llvm;
2323
24 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err) {
24 bool llvm::parseAssemblyInto(MemoryBufferRef F, Module &M, SMDiagnostic &Err,
25 SlotMapping *Slots) {
2526 SourceMgr SM;
2627 std::unique_ptr Buf = MemoryBuffer::getMemBuffer(F);
2728 SM.AddNewSourceBuffer(std::move(Buf), SMLoc());
2829
29 return LLParser(F.getBuffer(), SM, Err, &M).Run();
30 return LLParser(F.getBuffer(), SM, Err, &M, Slots).Run();
3031 }
3132
3233 std::unique_ptr llvm::parseAssembly(MemoryBufferRef F,
3334 SMDiagnostic &Err,
34 LLVMContext &Context) {
35 LLVMContext &Context,
36 SlotMapping *Slots) {
3537 std::unique_ptr M =
3638 make_unique(F.getBufferIdentifier(), Context);
3739
38 if (parseAssemblyInto(F, *M, Err))
40 if (parseAssemblyInto(F, *M, Err, Slots))
3941 return nullptr;
4042
4143 return M;
4345
4446 std::unique_ptr llvm::parseAssemblyFile(StringRef Filename,
4547 SMDiagnostic &Err,
46 LLVMContext &Context) {
48 LLVMContext &Context,
49 SlotMapping *Slots) {
4750 ErrorOr> FileOrErr =
4851 MemoryBuffer::getFileOrSTDIN(Filename);
4952 if (std::error_code EC = FileOrErr.getError()) {
5255 return nullptr;
5356 }
5457
55 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context);
58 return parseAssembly(FileOrErr.get()->getMemBufferRef(), Err, Context, Slots);
5659 }
5760
5861 std::unique_ptr llvm::parseAssemblyString(StringRef AsmString,
5962 SMDiagnostic &Err,
60 LLVMContext &Context) {
63 LLVMContext &Context,
64 SlotMapping *Slots) {
6165 MemoryBufferRef F(AsmString, "");
62 return parseAssembly(F, Err, Context);
66 return parseAssembly(F, Err, Context, Slots);
6367 }
88
99 #include "llvm/ADT/StringRef.h"
1010 #include "llvm/AsmParser/Parser.h"
11 #include "llvm/AsmParser/SlotMapping.h"
1112 #include "llvm/IR/LLVMContext.h"
1213 #include "llvm/IR/Module.h"
1314 #include "llvm/Support/SourceMgr.h"
4344 #endif
4445 #endif
4546
47 TEST(AsmParserTest, SlotMappingTest) {
48 LLVMContext &Ctx = getGlobalContext();
49 StringRef Source = "@0 = global i32 0\n !0 = !{}\n !42 = !{i32 42}";
50 SMDiagnostic Error;
51 SlotMapping Mapping;
52 auto Mod = parseAssemblyString(Source, Error, Ctx, &Mapping);
53
54 EXPECT_TRUE(Mod != nullptr);
55 EXPECT_TRUE(Error.getMessage().empty());
56
57 ASSERT_EQ(Mapping.GlobalValues.size(), 1u);
58 EXPECT_TRUE(isa(Mapping.GlobalValues[0]));
59
60 EXPECT_EQ(Mapping.MetadataNodes.size(), 2u);
61 EXPECT_EQ(Mapping.MetadataNodes.count(0), 1u);
62 EXPECT_EQ(Mapping.MetadataNodes.count(42), 1u);
63 EXPECT_EQ(Mapping.MetadataNodes.count(1), 0u);
64 }
65
4666 } // end anonymous namespace