@@ -428,6 +428,11 @@ class IndexBitcodeWriter : public BitcodeWriterBase {
428428 // / The combined index to write to bitcode.
429429 const ModuleSummaryIndex &Index;
430430
431+ // / When writing combined summaries, provides the set of global value
432+ // / summaries for which the value (function, function alias, etc) should be
433+ // / imported as a declaration.
434+ const GVSummaryPtrSet *DecSummaries = nullptr ;
435+
431436 // / When writing a subset of the index for distributed backends, client
432437 // / provides a map of modules to the corresponding GUIDs/summaries to write.
433438 const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex;
@@ -452,11 +457,16 @@ class IndexBitcodeWriter : public BitcodeWriterBase {
452457 // / Constructs a IndexBitcodeWriter object for the given combined index,
453458 // / writing to the provided \p Buffer. When writing a subset of the index
454459 // / for a distributed backend, provide a \p ModuleToSummariesForIndex map.
460+ // / If provided, \p ModuleToDecSummaries specifies the set of summaries for
461+ // / which the corresponding functions or aliased functions should be imported
462+ // / as a declaration (but not definition) for each module.
455463 IndexBitcodeWriter (BitstreamWriter &Stream, StringTableBuilder &StrtabBuilder,
456464 const ModuleSummaryIndex &Index,
465+ const GVSummaryPtrSet *DecSummaries = nullptr ,
457466 const std::map<std::string, GVSummaryMapTy>
458467 *ModuleToSummariesForIndex = nullptr )
459468 : BitcodeWriterBase(Stream, StrtabBuilder), Index(Index),
469+ DecSummaries (DecSummaries),
460470 ModuleToSummariesForIndex(ModuleToSummariesForIndex) {
461471 // Assign unique value ids to all summaries to be written, for use
462472 // in writing out the call graph edges. Save the mapping from GUID
@@ -1202,7 +1212,8 @@ static uint64_t getEncodedFFlags(FunctionSummary::FFlags Flags) {
12021212
12031213// Decode the flags for GlobalValue in the summary. See getDecodedGVSummaryFlags
12041214// in BitcodeReader.cpp.
1205- static uint64_t getEncodedGVSummaryFlags (GlobalValueSummary::GVFlags Flags) {
1215+ static uint64_t getEncodedGVSummaryFlags (GlobalValueSummary::GVFlags Flags,
1216+ bool ImportAsDecl = false ) {
12061217 uint64_t RawFlags = 0 ;
12071218
12081219 RawFlags |= Flags.NotEligibleToImport ; // bool
@@ -1217,7 +1228,8 @@ static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
12171228
12181229 RawFlags |= (Flags.Visibility << 8 ); // 2 bits
12191230
1220- RawFlags |= (Flags.ImportType << 10 ); // 1 bit
1231+ unsigned ImportType = Flags.ImportType | ImportAsDecl;
1232+ RawFlags |= (ImportType << 10 ); // 1 bit
12211233
12221234 return RawFlags;
12231235}
@@ -4543,6 +4555,12 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
45434555 Abbv->Add (BitCodeAbbrevOp (BitCodeAbbrevOp::VBR, 8 ));
45444556 unsigned AllocAbbrev = Stream.EmitAbbrev (std::move (Abbv));
45454557
4558+ auto shouldImportValueAsDecl = [&](GlobalValueSummary *GVS) -> bool {
4559+ if (DecSummaries == nullptr )
4560+ return false ;
4561+ return DecSummaries->contains (GVS);
4562+ };
4563+
45464564 // The aliases are emitted as a post-pass, and will point to the value
45474565 // id of the aliasee. Save them in a vector for post-processing.
45484566 SmallVector<AliasSummary *, 64 > Aliases;
@@ -4653,7 +4671,8 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
46534671 NameVals.push_back (*ValueId);
46544672 assert (ModuleIdMap.count (FS->modulePath ()));
46554673 NameVals.push_back (ModuleIdMap[FS->modulePath ()]);
4656- NameVals.push_back (getEncodedGVSummaryFlags (FS->flags ()));
4674+ NameVals.push_back (
4675+ getEncodedGVSummaryFlags (FS->flags (), shouldImportValueAsDecl (FS)));
46574676 NameVals.push_back (FS->instCount ());
46584677 NameVals.push_back (getEncodedFFlags (FS->fflags ()));
46594678 NameVals.push_back (FS->entryCount ());
@@ -4702,7 +4721,8 @@ void IndexBitcodeWriter::writeCombinedGlobalValueSummary() {
47024721 NameVals.push_back (AliasValueId);
47034722 assert (ModuleIdMap.count (AS->modulePath ()));
47044723 NameVals.push_back (ModuleIdMap[AS->modulePath ()]);
4705- NameVals.push_back (getEncodedGVSummaryFlags (AS->flags ()));
4724+ NameVals.push_back (
4725+ getEncodedGVSummaryFlags (AS->flags (), shouldImportValueAsDecl (AS)));
47064726 auto AliaseeValueId = SummaryToValueIdMap[&AS->getAliasee ()];
47074727 assert (AliaseeValueId);
47084728 NameVals.push_back (AliaseeValueId);
@@ -5036,8 +5056,9 @@ void BitcodeWriter::writeModule(const Module &M,
50365056
50375057void BitcodeWriter::writeIndex (
50385058 const ModuleSummaryIndex *Index,
5039- const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
5040- IndexBitcodeWriter IndexWriter (*Stream, StrtabBuilder, *Index,
5059+ const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
5060+ const GVSummaryPtrSet *DecSummaries) {
5061+ IndexBitcodeWriter IndexWriter (*Stream, StrtabBuilder, *Index, DecSummaries,
50415062 ModuleToSummariesForIndex);
50425063 IndexWriter.write ();
50435064}
@@ -5090,12 +5111,13 @@ void IndexBitcodeWriter::write() {
50905111// index for a distributed backend, provide a \p ModuleToSummariesForIndex map.
50915112void llvm::writeIndexToFile (
50925113 const ModuleSummaryIndex &Index, raw_ostream &Out,
5093- const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex) {
5114+ const std::map<std::string, GVSummaryMapTy> *ModuleToSummariesForIndex,
5115+ const GVSummaryPtrSet *DecSummaries) {
50945116 SmallVector<char , 0 > Buffer;
50955117 Buffer.reserve (256 * 1024 );
50965118
50975119 BitcodeWriter Writer (Buffer);
5098- Writer.writeIndex (&Index, ModuleToSummariesForIndex);
5120+ Writer.writeIndex (&Index, ModuleToSummariesForIndex, DecSummaries );
50995121 Writer.writeStrtab ();
51005122
51015123 Out.write ((char *)&Buffer.front (), Buffer.size ());
0 commit comments