diff --git a/include/llvm/Bitcode/LLVMBitCodes.h b/include/llvm/Bitcode/LLVMBitCodes.h index 71f3cc80dd7..1b25ff3768c 100644 --- a/include/llvm/Bitcode/LLVMBitCodes.h +++ b/include/llvm/Bitcode/LLVMBitCodes.h @@ -219,6 +219,8 @@ enum GlobalValueSummarySymtabCodes { FS_COMBINED_ALIAS = 8, // COMBINED_ORIGINAL_NAME: [original_name_hash] FS_COMBINED_ORIGINAL_NAME = 9, + // VERSION of the summary, bumped when adding flags for instance. + FS_VERSION = 10, }; enum MetadataCodes { diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index cbacf8127d4..8b583fe4995 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -5995,8 +5995,21 @@ std::error_code ModuleSummaryIndexBitcodeReader::parseModule() { std::error_code ModuleSummaryIndexBitcodeReader::parseEntireSummary() { if (Stream.EnterSubBlock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID)) return error("Invalid record"); - SmallVector Record; + + // Parse version + { + BitstreamEntry Entry = Stream.advanceSkippingSubblocks(); + if (Entry.Kind != BitstreamEntry::Record) + return error("Invalid Summary Block: record for version expected"); + if (Stream.readRecord(Entry.ID, Record) != bitc::FS_VERSION) + return error("Invalid Summary Block: version expected"); + } + const uint64_t Version = Record[0]; + if (Version != 1) + return error("Invalid summary version " + Twine(Version) + ", 1 expected"); + Record.clear(); + // Keep around the last seen summary to be used when we see an optional // "OriginalName" attachement. GlobalValueSummary *LastSeenSummary = nullptr; diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 302629b9176..d0f87e7d9d0 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -3067,6 +3067,11 @@ void ModuleBitcodeWriter::writeModuleLevelReferences( NameVals.clear(); } +// Current version for the summary. +// This is bumped whenever we introduce changes in the way some record are +// interpreted, like flags for instance. +static const uint64_t INDEX_VERSION = 1; + /// Emit the per-module summary section alongside the rest of /// the module's bitcode. void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() { @@ -3078,6 +3083,8 @@ void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() { Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3); + Stream.EmitRecord(bitc::FS_VERSION, ArrayRef{INDEX_VERSION}); + // Abbrev for FS_PERMODULE. BitCodeAbbrev *Abbv = new BitCodeAbbrev(); Abbv->Add(BitCodeAbbrevOp(bitc::FS_PERMODULE)); @@ -3162,6 +3169,7 @@ void ModuleBitcodeWriter::writePerModuleGlobalValueSummary() { /// Emit the combined summary section into the combined index file. void IndexBitcodeWriter::writeCombinedGlobalValueSummary() { Stream.EnterSubblock(bitc::GLOBALVAL_SUMMARY_BLOCK_ID, 3); + Stream.EmitRecord(bitc::FS_VERSION, ArrayRef{INDEX_VERSION}); // Abbrev for FS_COMBINED. BitCodeAbbrev *Abbv = new BitCodeAbbrev(); diff --git a/test/Bitcode/summary_version.ll b/test/Bitcode/summary_version.ll new file mode 100644 index 00000000000..718a0ab9bd7 --- /dev/null +++ b/test/Bitcode/summary_version.ll @@ -0,0 +1,12 @@ +; Check summary versioning +; RUN: opt -module-summary %s -o - | llvm-bcanalyzer -dump | FileCheck %s + +; CHECK: + + + +; Need a function for the summary to be populated. +define void @foo() { + ret void +} diff --git a/test/Bitcode/thinlto-alias.ll b/test/Bitcode/thinlto-alias.ll index 7fe6d43ca48..56a01ec0553 100644 --- a/test/Bitcode/thinlto-alias.ll +++ b/test/Bitcode/thinlto-alias.ll @@ -6,6 +6,7 @@ ; RUN: llvm-bcanalyzer -dump %t3.thinlto.bc | FileCheck %s --check-prefix=COMBINED ; CHECK: @@ -17,6 +18,7 @@ ; CHECK-NEXT: ; COMBINED: diff --git a/test/Bitcode/thinlto-function-summary-callgraph-pgo.ll b/test/Bitcode/thinlto-function-summary-callgraph-pgo.ll index dc9460849e7..7bf91a541a7 100644 --- a/test/Bitcode/thinlto-function-summary-callgraph-pgo.ll +++ b/test/Bitcode/thinlto-function-summary-callgraph-pgo.ll @@ -6,6 +6,7 @@ ; RUN: llvm-bcanalyzer -dump %t3.thinlto.bc | FileCheck %s --check-prefix=COMBINED ; CHECK: @@ -17,6 +18,7 @@ ; CHECK-NEXT: ; COMBINED: @@ -17,6 +18,7 @@ ; CHECK-NEXT: ; COMBINED: ; COMBINED-DAG: