@@ -154,6 +154,11 @@ class BitcodeReader : public GVMaterializer {
154
154
uint64_t VSTOffset = 0 ;
155
155
// Contains an arbitrary and optional string identifying the bitcode producer
156
156
std::string ProducerIdentification;
157
+ // Number of module level metadata records specified by the
158
+ // MODULE_CODE_METADATA_VALUES record.
159
+ unsigned NumModuleMDs = 0 ;
160
+ // Support older bitcode without the MODULE_CODE_METADATA_VALUES record.
161
+ bool SeenModuleValuesRecord = false ;
157
162
158
163
std::vector<Type*> TypeList;
159
164
BitcodeReaderValueList ValueList;
@@ -404,7 +409,7 @@ class BitcodeReader : public GVMaterializer {
404
409
std::error_code parseFunctionBody (Function *F);
405
410
std::error_code globalCleanup ();
406
411
std::error_code resolveGlobalAndAliasInits ();
407
- std::error_code parseMetadata ();
412
+ std::error_code parseMetadata (bool ModuleLevel = false );
408
413
std::error_code parseMetadataKinds ();
409
414
std::error_code parseMetadataKindRecord (SmallVectorImpl<uint64_t > &Record);
410
415
std::error_code parseMetadataAttachment (Function &F);
@@ -1911,9 +1916,25 @@ BitcodeReader::parseMetadataKindRecord(SmallVectorImpl<uint64_t> &Record) {
1911
1916
1912
1917
static int64_t unrotateSign (uint64_t U) { return U & 1 ? ~(U >> 1 ) : U >> 1 ; }
1913
1918
1914
- std::error_code BitcodeReader::parseMetadata () {
1919
+ // / Parse a METADATA_BLOCK. If ModuleLevel is true then we are parsing
1920
+ // / module level metadata.
1921
+ std::error_code BitcodeReader::parseMetadata (bool ModuleLevel) {
1915
1922
IsMetadataMaterialized = true ;
1916
1923
unsigned NextMDValueNo = MDValueList.size ();
1924
+ if (ModuleLevel && SeenModuleValuesRecord) {
1925
+ // Now that we are parsing the module level metadata, we want to restart
1926
+ // the numbering of the MD values, and replace temp MD created earlier
1927
+ // with their real values. If we saw a METADATA_VALUE record then we
1928
+ // would have set the MDValueList size to the number specified in that
1929
+ // record, to support parsing function-level metadata first, and we need
1930
+ // to reset back to 0 to fill the MDValueList in with the parsed module
1931
+ // The function-level metadata parsing should have reset the MDValueList
1932
+ // size back to the value reported by the METADATA_VALUE record, saved in
1933
+ // NumModuleMDs.
1934
+ assert (NumModuleMDs == MDValueList.size () &&
1935
+ " Expected MDValueList to only contain module level values" );
1936
+ NextMDValueNo = 0 ;
1937
+ }
1917
1938
1918
1939
if (Stream.EnterSubBlock (bitc::METADATA_BLOCK_ID))
1919
1940
return error (" Invalid record" );
@@ -2375,6 +2396,9 @@ std::error_code BitcodeReader::parseMetadata() {
2375
2396
}
2376
2397
}
2377
2398
}
2399
+ assert ((!(ModuleLevel && SeenModuleValuesRecord) ||
2400
+ NumModuleMDs == MDValueList.size ()) &&
2401
+ " Inconsistent bitcode: METADATA_VALUES mismatch" );
2378
2402
#undef GET_OR_DISTINCT
2379
2403
}
2380
2404
@@ -3062,7 +3086,7 @@ std::error_code BitcodeReader::materializeMetadata() {
3062
3086
for (uint64_t BitPos : DeferredMetadataInfo) {
3063
3087
// Move the bit stream to the saved position.
3064
3088
Stream.JumpToBit (BitPos);
3065
- if (std::error_code EC = parseMetadata ())
3089
+ if (std::error_code EC = parseMetadata (true ))
3066
3090
return EC;
3067
3091
}
3068
3092
DeferredMetadataInfo.clear ();
@@ -3274,7 +3298,7 @@ std::error_code BitcodeReader::parseModule(uint64_t ResumeBit,
3274
3298
break ;
3275
3299
}
3276
3300
assert (DeferredMetadataInfo.empty () && " Unexpected deferred metadata" );
3277
- if (std::error_code EC = parseMetadata ())
3301
+ if (std::error_code EC = parseMetadata (true ))
3278
3302
return EC;
3279
3303
break ;
3280
3304
case bitc::METADATA_KIND_BLOCK_ID:
@@ -3654,6 +3678,28 @@ std::error_code BitcodeReader::parseModule(uint64_t ResumeBit,
3654
3678
return error (" Invalid record" );
3655
3679
VSTOffset = Record[0 ];
3656
3680
break ;
3681
+ // / MODULE_CODE_METADATA_VALUES: [numvals]
3682
+ case bitc::MODULE_CODE_METADATA_VALUES:
3683
+ if (Record.size () < 1 )
3684
+ return error (" Invalid record" );
3685
+ assert (!IsMetadataMaterialized);
3686
+ // This record contains the number of metadata values in the module-level
3687
+ // METADATA_BLOCK. It is used to support lazy parsing of metadata as
3688
+ // a postpass, where we will parse function-level metadata first.
3689
+ // This is needed because the ids of metadata are assigned implicitly
3690
+ // based on their ordering in the bitcode, with the function-level
3691
+ // metadata ids starting after the module-level metadata ids. Otherwise,
3692
+ // we would have to parse the module-level metadata block to prime the
3693
+ // MDValueList when we are lazy loading metadata during function
3694
+ // importing. Initialize the MDValueList size here based on the
3695
+ // record value, regardless of whether we are doing lazy metadata
3696
+ // loading, so that we have consistent handling and assertion
3697
+ // checking in parseMetadata for module-level metadata.
3698
+ NumModuleMDs = Record[0 ];
3699
+ SeenModuleValuesRecord = true ;
3700
+ assert (MDValueList.size () == 0 );
3701
+ MDValueList.resize (NumModuleMDs);
3702
+ break ;
3657
3703
}
3658
3704
Record.clear ();
3659
3705
}
@@ -5212,8 +5258,12 @@ std::error_code BitcodeReader::findFunctionInStream(
5212
5258
void BitcodeReader::releaseBuffer () { Buffer.release (); }
5213
5259
5214
5260
std::error_code BitcodeReader::materialize (GlobalValue *GV) {
5215
- if (std::error_code EC = materializeMetadata ())
5216
- return EC;
5261
+ // In older bitcode we must materialize the metadata before parsing
5262
+ // any functions, in order to set up the MDValueList properly.
5263
+ if (!SeenModuleValuesRecord) {
5264
+ if (std::error_code EC = materializeMetadata ())
5265
+ return EC;
5266
+ }
5217
5267
5218
5268
Function *F = dyn_cast<Function>(GV);
5219
5269
// If it's not a function or is already material, ignore the request.
0 commit comments