llvm.org GIT mirror llvm / 086ed79
[ThinLTO] Add MODULE_CODE_METADATA_VALUES record Summary: This is split out from the ThinLTO metadata mapping patch http://reviews.llvm.org/D14752. To avoid needing to parse the module level metadata during function importing, a new module-level record is added which holds the number of module-level metadata values. This is required because metadata value ids are assigned implicitly during parsing, and the function-level metadata ids start after the module-level metadata ids. I made a change to this version of the code compared to D14752 in order to add more consistent and thorough assertion checking of the new record value. We now unconditionally use the record value to initialize the MDValueList size, and handle it the same in parseMetadata for all module level metadata cases (lazy loading or not). Reviewers: dexonsmith, joker.eph Subscribers: davidxl, llvm-commits, joker.eph Differential Revision: http://reviews.llvm.org/D14825 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253668 91177308-0d34-0410-b5e6-96231b3b80d8 Teresa Johnson 4 years ago
5 changed file(s) with 76 addition(s) and 6 deletion(s). Raw diff Collapse all Expand all
101101
102102 // ALIAS: [alias value type, addrspace, aliasee val#, linkage, visibility]
103103 MODULE_CODE_ALIAS = 14,
104
105 // METADATA_VALUES: [numvals]
106 MODULE_CODE_METADATA_VALUES = 15,
104107 };
105108
106109 /// PARAMATTR blocks have code for defining a parameter attribute set.
153153 uint64_t VSTOffset = 0;
154154 // Contains an arbitrary and optional string identifying the bitcode producer
155155 std::string ProducerIdentification;
156 // Number of module level metadata records specified by the
157 // MODULE_CODE_METADATA_VALUES record.
158 unsigned NumModuleMDs = 0;
159 // Support older bitcode without the MODULE_CODE_METADATA_VALUES record.
160 bool SeenModuleValuesRecord = false;
156161
157162 std::vector TypeList;
158163 BitcodeReaderValueList ValueList;
403408 std::error_code parseFunctionBody(Function *F);
404409 std::error_code globalCleanup();
405410 std::error_code resolveGlobalAndAliasInits();
406 std::error_code parseMetadata();
411 std::error_code parseMetadata(bool ModuleLevel = false);
407412 std::error_code parseMetadataKinds();
408413 std::error_code parseMetadataKindRecord(SmallVectorImpl &Record);
409414 std::error_code parseMetadataAttachment(Function &F);
19101915
19111916 static int64_t unrotateSign(uint64_t U) { return U & 1 ? ~(U >> 1) : U >> 1; }
19121917
1913 std::error_code BitcodeReader::parseMetadata() {
1918 /// Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
1919 /// module level metadata.
1920 std::error_code BitcodeReader::parseMetadata(bool ModuleLevel) {
19141921 IsMetadataMaterialized = true;
19151922 unsigned NextMDValueNo = MDValueList.size();
1923 if (ModuleLevel && SeenModuleValuesRecord) {
1924 // Now that we are parsing the module level metadata, we want to restart
1925 // the numbering of the MD values, and replace temp MD created earlier
1926 // with their real values. If we saw a METADATA_VALUE record then we
1927 // would have set the MDValueList size to the number specified in that
1928 // record, to support parsing function-level metadata first, and we need
1929 // to reset back to 0 to fill the MDValueList in with the parsed module
1930 // The function-level metadata parsing should have reset the MDValueList
1931 // size back to the value reported by the METADATA_VALUE record, saved in
1932 // NumModuleMDs.
1933 assert(NumModuleMDs == MDValueList.size() &&
1934 "Expected MDValueList to only contain module level values");
1935 NextMDValueNo = 0;
1936 }
19161937
19171938 if (Stream.EnterSubBlock(bitc::METADATA_BLOCK_ID))
19181939 return error("Invalid record");
23742395 }
23752396 }
23762397 }
2398 assert((!(ModuleLevel && SeenModuleValuesRecord) ||
2399 NumModuleMDs == MDValueList.size()) &&
2400 "Inconsistent bitcode: METADATA_VALUES mismatch");
23772401 #undef GET_OR_DISTINCT
23782402 }
23792403
30613085 for (uint64_t BitPos : DeferredMetadataInfo) {
30623086 // Move the bit stream to the saved position.
30633087 Stream.JumpToBit(BitPos);
3064 if (std::error_code EC = parseMetadata())
3088 if (std::error_code EC = parseMetadata(true))
30653089 return EC;
30663090 }
30673091 DeferredMetadataInfo.clear();
32733297 break;
32743298 }
32753299 assert(DeferredMetadataInfo.empty() && "Unexpected deferred metadata");
3276 if (std::error_code EC = parseMetadata())
3300 if (std::error_code EC = parseMetadata(true))
32773301 return EC;
32783302 break;
32793303 case bitc::METADATA_KIND_BLOCK_ID:
36523676 if (Record.size() < 1)
36533677 return error("Invalid record");
36543678 VSTOffset = Record[0];
3679 break;
3680 /// MODULE_CODE_METADATA_VALUES: [numvals]
3681 case bitc::MODULE_CODE_METADATA_VALUES:
3682 if (Record.size() < 1)
3683 return error("Invalid record");
3684 assert(!IsMetadataMaterialized);
3685 // This record contains the number of metadata values in the module-level
3686 // METADATA_BLOCK. It is used to support lazy parsing of metadata as
3687 // a postpass, where we will parse function-level metadata first.
3688 // This is needed because the ids of metadata are assigned implicitly
3689 // based on their ordering in the bitcode, with the function-level
3690 // metadata ids starting after the module-level metadata ids. Otherwise,
3691 // we would have to parse the module-level metadata block to prime the
3692 // MDValueList when we are lazy loading metadata during function
3693 // importing. Initialize the MDValueList size here based on the
3694 // record value, regardless of whether we are doing lazy metadata
3695 // loading, so that we have consistent handling and assertion
3696 // checking in parseMetadata for module-level metadata.
3697 NumModuleMDs = Record[0];
3698 SeenModuleValuesRecord = true;
3699 assert(MDValueList.size() == 0);
3700 MDValueList.resize(NumModuleMDs);
36553701 break;
36563702 }
36573703 Record.clear();
52115257 void BitcodeReader::releaseBuffer() { Buffer.release(); }
52125258
52135259 std::error_code BitcodeReader::materialize(GlobalValue *GV) {
5214 if (std::error_code EC = materializeMetadata())
5215 return EC;
5260 // In older bitcode we must materialize the metadata before parsing
5261 // any functions, in order to set up the MDValueList properly.
5262 if (!SeenModuleValuesRecord) {
5263 if (std::error_code EC = materializeMetadata())
5264 return EC;
5265 }
52165266
52175267 Function *F = dyn_cast(GV);
52185268 // If it's not a function or is already material, ignore the request.
774774 Stream.EmitRecord(bitc::MODULE_CODE_ALIAS, Vals, AbbrevToUse);
775775 Vals.clear();
776776 }
777
778 // Write a record indicating the number of module-level metadata IDs
779 // This is needed because the ids of metadata are assigned implicitly
780 // based on their ordering in the bitcode, with the function-level
781 // metadata ids starting after the module-level metadata ids. For
782 // function importing where we lazy load the metadata as a postpass,
783 // we want to avoid parsing the module-level metadata before parsing
784 // the imported functions.
785 BitCodeAbbrev *Abbv = new BitCodeAbbrev();
786 Abbv->Add(BitCodeAbbrevOp(bitc::MODULE_CODE_METADATA_VALUES));
787 Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 6));
788 unsigned MDValsAbbrev = Stream.EmitAbbrev(Abbv);
789 Vals.push_back(VE.numMDs());
790 Stream.EmitRecord(bitc::MODULE_CODE_METADATA_VALUES, Vals, MDValsAbbrev);
791 Vals.clear();
777792
778793 uint64_t VSTOffsetPlaceholder =
779794 WriteValueSymbolTableForwardDecl(M->getValueSymbolTable(), Stream);
118118 unsigned getMetadataOrNullID(const Metadata *MD) const {
119119 return MDValueMap.lookup(MD);
120120 }
121 unsigned numMDs() const { return MDs.size(); }
121122
122123 bool hasMDString() const { return HasMDString; }
123124 bool hasDILocation() const { return HasDILocation; }
171171 STRINGIFY_CODE(MODULE_CODE, PURGEVALS)
172172 STRINGIFY_CODE(MODULE_CODE, GCNAME)
173173 STRINGIFY_CODE(MODULE_CODE, VSTOFFSET)
174 STRINGIFY_CODE(MODULE_CODE, METADATA_VALUES)
174175 }
175176 case bitc::IDENTIFICATION_BLOCK_ID:
176177 switch (CodeID) {