Skip to content

Commit a95397a

Browse files
Revert "Revert "[ThinLTO][Bitcode] Generate import type in bitcode (#87600)" …"
This reverts commit 53061ee.
1 parent 038bc1c commit a95397a

File tree

9 files changed

+119
-41
lines changed

9 files changed

+119
-41
lines changed

llvm/include/llvm/Bitcode/BitcodeWriter.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,8 @@ class BitcodeWriter {
102102

103103
void writeIndex(
104104
const ModuleSummaryIndex *Index,
105-
const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex);
105+
const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
106+
const GVSummaryPtrSet *DecSummaries);
106107
};
107108

108109
/// Write the specified module to the specified raw output stream.
@@ -147,10 +148,12 @@ void writeThinLinkBitcodeToFile(const Module &M, raw_ostream &Out,
147148
/// where it will be written in a new bitcode block. This is used when
148149
/// writing the combined index file for ThinLTO. When writing a subset of the
149150
/// index for a distributed backend, provide the \p ModuleToSummariesForIndex
150-
/// map.
151+
/// map. \p DecSummaries specifies the set of summaries for which the
152+
/// corresponding value should be imported as a declaration (prototype).
151153
void writeIndexToFile(const ModuleSummaryIndex &Index, raw_ostream &Out,
152154
const std::map<std::string, GVSummaryMapTy>
153-
*ModuleToSummariesForIndex = nullptr);
155+
*ModuleToSummariesForIndex = nullptr,
156+
const GVSummaryPtrSet *DecSummaries = nullptr);
154157

155158
/// If EmbedBitcode is set, save a copy of the llvm IR as data in the
156159
/// __LLVM,__bitcode section (.llvmbc on non-MacOS).

llvm/include/llvm/LTO/legacy/ThinLTOCodeGenerator.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -271,12 +271,13 @@ class ThinLTOCodeGenerator {
271271
const lto::InputFile &File);
272272

273273
/**
274-
* Compute the list of summaries needed for importing into module.
274+
* Compute the list of summaries and the subset of declaration summaries
275+
* needed for importing into module.
275276
*/
276277
void gatherImportedSummariesForModule(
277278
Module &Module, ModuleSummaryIndex &Index,
278279
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
279-
const lto::InputFile &File);
280+
GVSummaryPtrSet &DecSummaries, const lto::InputFile &File);
280281

281282
/**
282283
* Perform internalization. Index is updated to reflect linkage changes.

llvm/include/llvm/Transforms/IPO/FunctionImport.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -217,11 +217,15 @@ bool convertToDeclaration(GlobalValue &GV);
217217
/// \p ModuleToSummariesForIndex will be populated with the needed summaries
218218
/// from each required module path. Use a std::map instead of StringMap to get
219219
/// stable order for bitcode emission.
220+
///
221+
/// \p DecSummaries will be popluated with the subset of of summary pointers
222+
/// that have 'declaration' import type among all summaries the module need.
220223
void gatherImportedSummariesForModule(
221224
StringRef ModulePath,
222225
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
223226
const FunctionImporter::ImportMapTy &ImportList,
224-
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex);
227+
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
228+
GVSummaryPtrSet &DecSummaries);
225229

226230
/// Emit into \p OutputFilename the files module \p ModulePath will import from.
227231
std::error_code EmitImportsFiles(

llvm/lib/Bitcode/Writer/BitcodeWriter.cpp

Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,11 @@ class IndexBitcodeWriter : public BitcodeWriterBase {
425425
/// The combined index to write to bitcode.
426426
const ModuleSummaryIndex &Index;
427427

428+
/// When writing combined summaries, provides the set of global value
429+
/// summaries for which the value (function, function alias, etc) should be
430+
/// imported as a declaration.
431+
const GVSummaryPtrSet *DecSummaries = nullptr;
432+
428433
/// When writing a subset of the index for distributed backends, client
429434
/// provides a map of modules to the corresponding GUIDs/summaries to write.
430435
const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
@@ -453,11 +458,16 @@ class IndexBitcodeWriter : public BitcodeWriterBase {
453458
/// Constructs a IndexBitcodeWriter object for the given combined index,
454459
/// writing to the provided \p Buffer. When writing a subset of the index
455460
/// for a distributed backend, provide a \p ModuleToSummariesForIndex map.
461+
/// If provided, \p ModuleToDecSummaries specifies the set of summaries for
462+
/// which the corresponding functions or aliased functions should be imported
463+
/// as a declaration (but not definition) for each module.
456464
IndexBitcodeWriter(BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
457465
const ModuleSummaryIndex &Index,
466+
const GVSummaryPtrSet *DecSummaries = nullptr,
458467
const std::map<std::string, GVSummaryMapTy>
459468
*ModuleToSummariesForIndex = nullptr)
460469
: BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
470+
DecSummaries(DecSummaries),
461471
ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
462472

463473
// See if the StackIdIndex was already added to the StackId map and
@@ -1226,7 +1236,8 @@ static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) {
12261236

12271237
// Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
12281238
// in BitcodeReader.cpp.
1229-
static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
1239+
static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags,
1240+
bool ImportAsDecl = false) {
12301241
uint64_t RawFlags = 0;
12311242

12321243
RawFlags |= Flags.NotEligibleToImport; // bool
@@ -1241,7 +1252,8 @@ static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
12411252

12421253
RawFlags |= (Flags.Visibility << 8); // 2 bits
12431254

1244-
RawFlags |= (Flags.ImportType << 10); // 1 bit
1255+
unsigned ImportType = Flags.ImportType | ImportAsDecl;
1256+
RawFlags |= (ImportType << 10); // 1 bit
12451257

12461258
return RawFlags;
12471259
}
@@ -4568,6 +4580,12 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
45684580
Abbv->Add(BitCodeAbbrevOp(BitCodeAbbrevOp::VBR, 8));
45694581
unsigned AllocAbbrev = Stream.EmitAbbrev(std::move(Abbv));
45704582

4583+
auto shouldImportValueAsDecl = [&](GlobalValueSummary *GVS) -> bool {
4584+
if (DecSummaries == nullptr)
4585+
return false;
4586+
return DecSummaries->contains(GVS);
4587+
};
4588+
45714589
// The aliases are emitted as a post-pass, and will point to the value
45724590
// id of the aliasee. Save them in a vector for post-processing.
45734591
SmallVector<AliasSummary *, 64> Aliases;
@@ -4678,7 +4696,8 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
46784696
NameVals.push_back(*ValueId);
46794697
assert(ModuleIdMap.count(FS->modulePath()));
46804698
NameVals.push_back(ModuleIdMap[FS->modulePath()]);
4681-
NameVals.push_back(getEncodedGVSummaryFlags(FS->flags()));
4699+
NameVals.push_back(
4700+
getEncodedGVSummaryFlags(FS->flags(), shouldImportValueAsDecl(FS)));
46824701
NameVals.push_back(FS->instCount());
46834702
NameVals.push_back(getEncodedFFlags(FS->fflags()));
46844703
NameVals.push_back(FS->entryCount());
@@ -4727,7 +4746,8 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
47274746
NameVals.push_back(AliasValueId);
47284747
assert(ModuleIdMap.count(AS->modulePath()));
47294748
NameVals.push_back(ModuleIdMap[AS->modulePath()]);
4730-
NameVals.push_back(getEncodedGVSummaryFlags(AS->flags()));
4749+
NameVals.push_back(
4750+
getEncodedGVSummaryFlags(AS->flags(), shouldImportValueAsDecl(AS)));
47314751
auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee()];
47324752
assert(AliaseeValueId);
47334753
NameVals.push_back(AliaseeValueId);
@@ -5068,8 +5088,9 @@ void BitcodeWriter::writeModule(const Module &M,
50685088

50695089
void BitcodeWriter::writeIndex(
50705090
const ModuleSummaryIndex *Index,
5071-
const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
5072-
IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index,
5091+
const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
5092+
const GVSummaryPtrSet *DecSummaries) {
5093+
IndexBitcodeWriter IndexWriter(*Stream, StrtabBuilder, *Index, DecSummaries,
50735094
ModuleToSummariesForIndex);
50745095
IndexWriter.write();
50755096
}
@@ -5124,12 +5145,13 @@ void IndexBitcodeWriter::write() {
51245145
// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
51255146
void llvm::writeIndexToFile(
51265147
const ModuleSummaryIndex &Index, raw_ostream &Out,
5127-
const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
5148+
const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
5149+
const GVSummaryPtrSet *DecSummaries) {
51285150
SmallVector<char, 0> Buffer;
51295151
Buffer.reserve(256 * 1024);
51305152

51315153
BitcodeWriter Writer(Buffer);
5132-
Writer.writeIndex(&Index, ModuleToSummariesForIndex);
5154+
Writer.writeIndex(&Index, ModuleToSummariesForIndex, DecSummaries);
51335155
Writer.writeStrtab();
51345156

51355157
Out.write((char *)&Buffer.front(), Buffer.size());

llvm/lib/LTO/LTO.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1402,18 +1402,20 @@ class lto::ThinBackendProc {
14021402
llvm::StringRef ModulePath,
14031403
const std::string &NewModulePath) {
14041404
std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
1405+
GVSummaryPtrSet DeclarationSummaries;
14051406

14061407
std::error_code EC;
14071408
gatherImportedSummariesForModule(ModulePath, ModuleToDefinedGVSummaries,
1408-
ImportList, ModuleToSummariesForIndex);
1409+
ImportList, ModuleToSummariesForIndex,
1410+
DeclarationSummaries);
14091411

14101412
raw_fd_ostream OS(NewModulePath + ".thinlto.bc", EC,
14111413
sys::fs::OpenFlags::OF_None);
14121414
if (EC)
14131415
return errorCodeToError(EC);
14141416

1415-
// TODO: Serialize declaration bits to bitcode.
1416-
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex);
1417+
writeIndexToFile(CombinedIndex, OS, &ModuleToSummariesForIndex,
1418+
&DeclarationSummaries);
14171419

14181420
if (ShouldEmitImportsFiles) {
14191421
EC = EmitImportsFiles(ModulePath, NewModulePath + ".imports",

llvm/lib/LTO/ThinLTOCodeGenerator.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -767,7 +767,7 @@ void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
767767
void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
768768
Module &TheModule, ModuleSummaryIndex &Index,
769769
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
770-
const lto::InputFile &File) {
770+
GVSummaryPtrSet &DecSummaries, const lto::InputFile &File) {
771771
auto ModuleCount = Index.modulePaths().size();
772772
auto ModuleIdentifier = TheModule.getModuleIdentifier();
773773

@@ -797,7 +797,7 @@ void ThinLTOCodeGenerator::gatherImportedSummariesForModule(
797797

798798
llvm::gatherImportedSummariesForModule(
799799
ModuleIdentifier, ModuleToDefinedGVSummaries,
800-
ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
800+
ImportLists[ModuleIdentifier], ModuleToSummariesForIndex, DecSummaries);
801801
}
802802

803803
/**
@@ -833,10 +833,14 @@ void ThinLTOCodeGenerator::emitImports(Module &TheModule, StringRef OutputName,
833833
IsPrevailing(PrevailingCopy), ImportLists,
834834
ExportLists);
835835

836+
// 'EmitImportsFiles' emits the list of modules from which to import from, and
837+
// the set of keys in `ModuleToSummariesForIndex` should be a superset of keys
838+
// in `DecSummaries`, so no need to use `DecSummaries` in `EmitImportFiles`.
839+
GVSummaryPtrSet DecSummaries;
836840
std::map<std::string, GVSummaryMapTy> ModuleToSummariesForIndex;
837841
llvm::gatherImportedSummariesForModule(
838842
ModuleIdentifier, ModuleToDefinedGVSummaries,
839-
ImportLists[ModuleIdentifier], ModuleToSummariesForIndex);
843+
ImportLists[ModuleIdentifier], ModuleToSummariesForIndex, DecSummaries);
840844

841845
std::error_code EC;
842846
if ((EC = EmitImportsFiles(ModuleIdentifier, OutputName,

llvm/lib/Transforms/IPO/FunctionImport.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1435,7 +1435,8 @@ void llvm::gatherImportedSummariesForModule(
14351435
StringRef ModulePath,
14361436
const DenseMap<StringRef, GVSummaryMapTy> &ModuleToDefinedGVSummaries,
14371437
const FunctionImporter::ImportMapTy &ImportList,
1438-
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex) {
1438+
std::map<std::string, GVSummaryMapTy> &ModuleToSummariesForIndex,
1439+
GVSummaryPtrSet &DecSummaries) {
14391440
// Include all summaries from the importing module.
14401441
ModuleToSummariesForIndex[std::string(ModulePath)] =
14411442
ModuleToDefinedGVSummaries.lookup(ModulePath);
@@ -1450,7 +1451,7 @@ void llvm::gatherImportedSummariesForModule(
14501451
assert(DS != DefinedGVSummaries.end() &&
14511452
"Expected a defined summary for imported global value");
14521453
if (Type == GlobalValueSummary::Declaration)
1453-
continue;
1454+
DecSummaries.insert(DS->second);
14541455

14551456
SummariesForIndex[GUID] = DS->second;
14561457
}

0 commit comments

Comments
 (0)