@@ -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
50695089void 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.
51255146void 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 ());
0 commit comments