[ThinLTO] Only emit referenced type id records in index files

Summary:
Currently all type ids are emitted into the index file when it is
written. For distributed ThinLTO, that meant that all type ids were
being duplicated into every single distributed index file, regardless of
whether they were referenced, leading to huge amounts of unnecessary
duplication and size bloat.

Keep track of the type id GUIDs actually referenced by the GV summary
records being emitted, and only emit those type IDs.

Add a new test, and fix test/Assembler/thinlto-summary.ll so that all
type ids are referenced to prevent deletion in that test.

Reviewers: pcc

Subscribers: mehdi_amini, inglorion, eraman, steven_wu, dexonsmith, vitalybuka, llvm-commits

Differential Revision: https://reviews.llvm.org/D49565

llvm-svn: 337503
This commit is contained in:
Teresa Johnson
2018-07-19 22:25:56 +00:00
parent c12c5d421f
commit 0c432b1a70
4 changed files with 109 additions and 7 deletions

View File

@@ -3353,10 +3353,14 @@ void IndexBitcodeWriter::writeModStrings() {
/// Write the function type metadata related records that need to appear before
/// a function summary entry (whether per-module or combined).
static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
FunctionSummary *FS) {
if (!FS->type_tests().empty())
static void writeFunctionTypeMetadataRecords(
BitstreamWriter &Stream, FunctionSummary *FS,
std::set<GlobalValue::GUID> &ReferencedTypeIds) {
if (!FS->type_tests().empty()) {
Stream.EmitRecord(bitc::FS_TYPE_TESTS, FS->type_tests());
for (auto &TT : FS->type_tests())
ReferencedTypeIds.insert(TT);
}
SmallVector<uint64_t, 64> Record;
@@ -3368,6 +3372,7 @@ static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
for (auto &VF : VFs) {
Record.push_back(VF.GUID);
Record.push_back(VF.Offset);
ReferencedTypeIds.insert(VF.GUID);
}
Stream.EmitRecord(Ty, Record);
};
@@ -3382,6 +3387,7 @@ static void writeFunctionTypeMetadataRecords(BitstreamWriter &Stream,
for (auto &VC : VCs) {
Record.clear();
Record.push_back(VC.VFunc.GUID);
ReferencedTypeIds.insert(VC.VFunc.GUID);
Record.push_back(VC.VFunc.Offset);
Record.insert(Record.end(), VC.Args.begin(), VC.Args.end());
Stream.EmitRecord(Ty, Record);
@@ -3447,7 +3453,8 @@ void ModuleBitcodeWriterBase::writePerModuleFunctionSummaryRecord(
NameVals.push_back(ValueID);
FunctionSummary *FS = cast<FunctionSummary>(Summary);
writeFunctionTypeMetadataRecords(Stream, FS);
std::set<GlobalValue::GUID> ReferencedTypeIds;
writeFunctionTypeMetadataRecords(Stream, FS, ReferencedTypeIds);
NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
NameVals.push_back(FS->instCount());
@@ -3702,6 +3709,10 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
SmallVector<uint64_t, 64> NameVals;
// Set that will be populated during call to writeFunctionTypeMetadataRecords
// with the type ids referenced by this index file.
std::set<GlobalValue::GUID> ReferencedTypeIds;
// For local linkage, we also emit the original name separately
// immediately after the record.
auto MaybeEmitOriginalName = [&](GlobalValueSummary &S) {
@@ -3753,7 +3764,7 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
}
auto *FS = cast<FunctionSummary>(S);
writeFunctionTypeMetadataRecords(Stream, FS);
writeFunctionTypeMetadataRecords(Stream, FS, ReferencedTypeIds);
NameVals.push_back(*ValueId);
NameVals.push_back(Index.getModuleId(FS->modulePath()));
@@ -3862,6 +3873,9 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
if (!Index.typeIds().empty()) {
for (auto &S : Index.typeIds()) {
// Skip if not referenced in any GV summary within this index file.
if (!ReferencedTypeIds.count(GlobalValue::getGUID(S.first)))
continue;
writeTypeIdSummaryRecord(NameVals, StrtabBuilder, S.first, S.second);
Stream.EmitRecord(bitc::FS_TYPE_ID, NameVals);
NameVals.clear();