llvm.org GIT mirror llvm / ccb80b9
(NFC) Track global summary liveness in GVFlags. Replace GVFlags::LiveRoot with GVFlags::Live and use that instead of all the DeadSymbols sets. This is refactoring in order to make liveness information available in the RegularLTO pipeline. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@304466 91177308-0d34-0410-b5e6-96231b3b80d8 Evgeniy Stepanov 2 years ago
8 changed file(s) with 115 addition(s) and 104 deletion(s). Raw diff Collapse all Expand all
133133 /// be renamed or references something that can't be renamed).
134134 unsigned NotEligibleToImport : 1;
135135
136 /// Indicate that the global value must be considered a live root for
137 /// index-based liveness analysis. Used for special LLVM values such as
138 /// llvm.global_ctors that the linker does not know about.
139 unsigned LiveRoot : 1;
136 /// In per-module summary, indicate that the global value must be considered
137 /// a live root for index-based liveness analysis. Used for special LLVM
138 /// values such as llvm.global_ctors that the linker does not know about.
139 ///
140 /// In combined summary, indicate that the global value is live.
141 unsigned Live : 1;
140142
141143 /// Convenience Constructors
142144 explicit GVFlags(GlobalValue::LinkageTypes Linkage,
143 bool NotEligibleToImport, bool LiveRoot)
145 bool NotEligibleToImport, bool Live)
144146 : Linkage(Linkage), NotEligibleToImport(NotEligibleToImport),
145 LiveRoot(LiveRoot) {}
147 Live(Live) {}
146148 };
147149
148150 private:
171173 /// are listed in the derived FunctionSummary object.
172174 std::vector RefEdgeList;
173175
176 bool isLive() const { return Flags.Live; }
177
174178 protected:
175179 GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector Refs)
176180 : Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {}
212216 /// Return true if this global value can't be imported.
213217 bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
214218
215 /// Return true if this global value must be considered a root for live
216 /// value analysis on the index.
217 bool liveRoot() const { return Flags.LiveRoot; }
218
219 /// Flag that this global value must be considered a root for live
220 /// value analysis on the index.
221 void setLiveRoot() { Flags.LiveRoot = true; }
219 void setLive(bool Live) { Flags.Live = Live; }
222220
223221 /// Flag that this global value cannot be imported.
224222 void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
225223
226224 /// Return the list of values referenced by this global value definition.
227225 ArrayRef refs() const { return RefEdgeList; }
226
227 friend class ModuleSummaryIndex;
228 friend void computeDeadSymbols(class ModuleSummaryIndex &,
229 const DenseSet &);
228230 };
229231
230232 /// \brief Alias summary information.
534536 /// GUIDs, it will be mapped to 0.
535537 std::map OidGuidMap;
536538
539 /// Indicates that summary-based GlobalValue GC has run, and values with
540 /// GVFlags::Live==false are really dead. Otherwise, all values must be
541 /// considered live.
542 bool WithGlobalValueDeadStripping = false;
543
537544 // YAML I/O support.
538545 friend yaml::MappingTraits;
539546
548555 gvsummary_iterator end() { return GlobalValueMap.end(); }
549556 const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
550557 size_t size() const { return GlobalValueMap.size(); }
558
559 bool withGlobalValueDeadStripping() const {
560 return WithGlobalValueDeadStripping;
561 }
562 void setWithGlobalValueDeadStripping() {
563 WithGlobalValueDeadStripping = true;
564 }
565
566 bool isGlobalValueLive(const GlobalValueSummary *GVS) const {
567 return !WithGlobalValueDeadStripping || GVS->isLive();
568 }
551569
552570 /// Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
553571 ValueInfo getValueInfo(GlobalValue::GUID GUID) const {
8080 /// \p ExportLists contains for each Module the set of globals (GUID) that will
8181 /// be imported by another module, or referenced by such a function. I.e. this
8282 /// is the set of globals that need to be promoted/renamed appropriately.
83 ///
84 /// \p DeadSymbols (optional) contains a list of GUID that are deemed "dead" and
85 /// will be ignored for the purpose of importing.
8683 void ComputeCrossModuleImport(
8784 const ModuleSummaryIndex &Index,
8885 const StringMap &ModuleToDefinedGVSummaries,
8986 StringMap &ImportLists,
90 StringMap &ExportLists,
91 const DenseSet *DeadSymbols = nullptr);
87 StringMap &ExportLists);
9288
9389 /// Compute all the imports for the given module using the Index.
9490 ///
10197 /// Compute all the symbols that are "dead": i.e these that can't be reached
10298 /// in the graph from any of the given symbols listed in
10399 /// \p GUIDPreservedSymbols.
104 DenseSet
105 computeDeadSymbols(const ModuleSummaryIndex &Index,
106 const DenseSet &GUIDPreservedSymbols);
100 void computeDeadSymbols(
101 ModuleSummaryIndex &Index,
102 const DenseSet &GUIDPreservedSymbols);
107103
108104 /// Compute the set of summaries needed for a ThinLTO backend compilation of
109105 /// \p ModulePath.
274274 // FIXME: refactor this to use the same code that inliner is using.
275275 F.isVarArg();
276276 GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
277 /* LiveRoot = */ false);
277 /* Live = */ false);
278278 auto FuncSummary = llvm::make_unique(
279279 Flags, NumInsts, RefEdges.takeVector(), CallGraphEdges.takeVector(),
280280 TypeTests.takeVector(), TypeTestAssumeVCalls.takeVector(),
294294 findRefEdges(Index, &V, RefEdges, Visited);
295295 bool NonRenamableLocal = isNonRenamableLocal(V);
296296 GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
297 /* LiveRoot = */ false);
297 /* Live = */ false);
298298 auto GVarSummary =
299299 llvm::make_unique(Flags, RefEdges.takeVector());
300300 if (NonRenamableLocal)
307307 DenseSet &CantBePromoted) {
308308 bool NonRenamableLocal = isNonRenamableLocal(A);
309309 GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
310 /* LiveRoot = */ false);
310 /* Live = */ false);
311311 auto AS = llvm::make_unique(Flags, ArrayRef{});
312312 auto *Aliasee = A.getBaseObject();
313313 auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
322322 static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
323323 if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
324324 for (auto &Summary : VI.getSummaryList())
325 Summary->setLiveRoot();
325 Summary->setLive(true);
326326 }
327327
328328 ModuleSummaryIndex llvm::buildModuleSummaryIndex(
422422 return;
423423 assert(GV->isDeclaration() && "Def in module asm already has definition");
424424 GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage,
425 /* NotEligibleToImport */ true,
426 /* LiveRoot */ true);
425 /* NotEligibleToImport = */ true,
426 /* Live = */ true);
427427 CantBePromoted.insert(GlobalValue::getGUID(Name));
428428 // Create the appropriate summary type.
429429 if (isa(GV)) {
864864 auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
865865 RawFlags = RawFlags >> 4;
866866 bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;
867 // The LiveRoot flag wasn't introduced until version 3. For dead stripping
867 // The Live flag wasn't introduced until version 3. For dead stripping
868868 // to work correctly on earlier versions, we must conservatively treat all
869869 // values as live.
870 bool LiveRoot = (RawFlags & 0x2) || Version < 3;
871 return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, LiveRoot);
870 bool Live = (RawFlags & 0x2) || Version < 3;
871 return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live);
872872 }
873873
874874 static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {
863863 uint64_t RawFlags = 0;
864864
865865 RawFlags |= Flags.NotEligibleToImport; // bool
866 RawFlags |= (Flags.LiveRoot << 1);
866 RawFlags |= (Flags.Live << 1);
867867 // Linkage don't need to be remapped at that time for the summary. Any future
868868 // change to the getEncodedLinkage() function will need to be taken into
869869 // account here as well.
929929 };
930930 }
931931
932 static bool IsLiveByGUID(const ModuleSummaryIndex &Index,
933 GlobalValue::GUID GUID) {
934 auto VI = Index.getValueInfo(GUID);
935 if (!VI)
936 return false;
937 for (auto &I : VI.getSummaryList())
938 if (Index.isGlobalValueLive(I.get()))
939 return true;
940 return false;
941 }
942
932943 Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
933944 bool HasRegularLTO) {
934945 if (ThinLTO.ModuleMap.empty())
972983 GlobalValue::dropLLVMManglingEscape(Res.second.IRName)));
973984 }
974985
975 auto DeadSymbols =
976 computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols);
986 computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols);
977987
978988 ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
979 ImportLists, ExportLists, &DeadSymbols);
989 ImportLists, ExportLists);
980990
981991 std::set ExportedGUIDs;
982992 for (auto &Res : GlobalResolutions) {
9911001 auto GUID = GlobalValue::getGUID(
9921002 GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
9931003 // Mark exported unless index-based analysis determined it to be dead.
994 if (!DeadSymbols.count(GUID))
1004 if (IsLiveByGUID(ThinLTO.CombinedIndex, GUID))
9951005 ExportedGUIDs.insert(GUID);
9961006 }
9971007
627627 PreservedSymbols, Triple(TheModule.getTargetTriple()));
628628
629629 // Compute "dead" symbols, we don't want to import/export these!
630 auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
630 computeDeadSymbols(Index, GUIDPreservedSymbols);
631631
632632 // Generate import/export list
633633 StringMap ImportLists(ModuleCount);
634634 StringMap ExportLists(ModuleCount);
635635 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
636 ExportLists, &DeadSymbols);
636 ExportLists);
637637
638638 // Resolve LinkOnce/Weak symbols.
639639 StringMap> ResolvedODR;
672672 PreservedSymbols, Triple(TheModule.getTargetTriple()));
673673
674674 // Compute "dead" symbols, we don't want to import/export these!
675 auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
675 computeDeadSymbols(Index, GUIDPreservedSymbols);
676676
677677 // Generate import/export list
678678 StringMap ImportLists(ModuleCount);
679679 StringMap ExportLists(ModuleCount);
680680 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
681 ExportLists, &DeadSymbols);
681 ExportLists);
682682 auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
683683
684684 crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
749749 Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
750750
751751 // Compute "dead" symbols, we don't want to import/export these!
752 auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
752 computeDeadSymbols(Index, GUIDPreservedSymbols);
753753
754754 // Generate import/export list
755755 StringMap ImportLists(ModuleCount);
756756 StringMap ExportLists(ModuleCount);
757757 ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
758 ExportLists, &DeadSymbols);
758 ExportLists);
759759 auto &ExportList = ExportLists[ModuleIdentifier];
760760
761761 // Be friendly and don't nuke totally the module when the client didn't
901901 computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
902902
903903 // Compute "dead" symbols, we don't want to import/export these!
904 auto DeadSymbols = computeDeadSymbols(*Index, GUIDPreservedSymbols);
904 computeDeadSymbols(*Index, GUIDPreservedSymbols);
905905
906906 // Collect the import/export lists for all modules from the call-graph in the
907907 // combined index.
908908 StringMap ImportLists(ModuleCount);
909909 StringMap ExportLists(ModuleCount);
910910 ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
911 ExportLists, &DeadSymbols);
911 ExportLists);
912912
913913 // We use a std::map here to be able to have a defined ordering when
914914 // producing a hash for the cache entry.
291291 static void ComputeImportForModule(
292292 const GVSummaryMapTy &DefinedGVSummaries, const ModuleSummaryIndex &Index,
293293 FunctionImporter::ImportMapTy &ImportList,
294 StringMap *ExportLists = nullptr,
295 const DenseSet *DeadSymbols = nullptr) {
294 StringMap *ExportLists = nullptr) {
296295 // Worklist contains the list of function imported in this module, for which
297296 // we will analyse the callees and may import further down the callgraph.
298297 SmallVector Worklist;
300299 // Populate the worklist with the import for the functions in the current
301300 // module
302301 for (auto &GVSummary : DefinedGVSummaries) {
303 if (DeadSymbols && DeadSymbols->count(GVSummary.first)) {
302 if (!Index.isGlobalValueLive(GVSummary.second)) {
304303 DEBUG(dbgs() << "Ignores Dead GUID: " << GVSummary.first << "\n");
305304 continue;
306305 }
343342 const ModuleSummaryIndex &Index,
344343 const StringMap &ModuleToDefinedGVSummaries,
345344 StringMap &ImportLists,
346 StringMap &ExportLists,
347 const DenseSet *DeadSymbols) {
345 StringMap &ExportLists) {
348346 // For each module that has function defined, compute the import/export lists.
349347 for (auto &DefinedGVSummaries : ModuleToDefinedGVSummaries) {
350348 auto &ImportList = ImportLists[DefinedGVSummaries.first()];
351349 DEBUG(dbgs() << "Computing import for Module '"
352350 << DefinedGVSummaries.first() << "'\n");
353351 ComputeImportForModule(DefinedGVSummaries.second, Index, ImportList,
354 &ExportLists, DeadSymbols);
352 &ExportLists);
355353 }
356354
357355 // When computing imports we added all GUIDs referenced by anything
413411 #endif
414412 }
415413
416 DenseSet llvm::computeDeadSymbols(
417 const ModuleSummaryIndex &Index,
414 void llvm::computeDeadSymbols(
415 ModuleSummaryIndex &Index,
418416 const DenseSet &GUIDPreservedSymbols) {
417 assert(!Index.withGlobalValueDeadStripping());
419418 if (!ComputeDead)
420 return DenseSet();
419 return;
421420 if (GUIDPreservedSymbols.empty())
422421 // Don't do anything when nothing is live, this is friendly with tests.
423 return DenseSet();
424 DenseSet LiveSymbols;
422 return;
423 unsigned LiveSymbols = 0;
425424 SmallVector Worklist;
426425 Worklist.reserve(GUIDPreservedSymbols.size() * 2);
427426 for (auto GUID : GUIDPreservedSymbols) {
428427 ValueInfo VI = Index.getValueInfo(GUID);
429428 if (!VI)
430429 continue;
431 DEBUG(dbgs() << "Live root: " << VI.getGUID() << "\n");
432 LiveSymbols.insert(VI);
430 for (auto &S : VI.getSummaryList())
431 S->setLive(true);
432 }
433
434 // Add values flagged in the index as live roots to the worklist.
435 for (const auto &Entry : Index)
436 for (auto &S : Entry.second.SummaryList)
437 if (S->isLive()) {
438 DEBUG(dbgs() << "Live root: " << Entry.first << "\n");
439 Worklist.push_back(ValueInfo(&Entry));
440 ++LiveSymbols;
441 break;
442 }
443
444 // Make value live and add it to the worklist if it was not live before.
445 // FIXME: we should only make the prevailing copy live here
446 auto visit = [&](ValueInfo VI) {
447 for (auto &S : VI.getSummaryList())
448 if (S->isLive())
449 return;
450 for (auto &S : VI.getSummaryList())
451 S->setLive(true);
452 ++LiveSymbols;
433453 Worklist.push_back(VI);
434 }
435 // Add values flagged in the index as live roots to the worklist.
436 for (const auto &Entry : Index) {
437 bool IsLiveRoot = llvm::any_of(
438 Entry.second.SummaryList,
439 [&](const std::unique_ptr &Summary) {
440 return Summary->liveRoot();
441 });
442 if (!IsLiveRoot)
443 continue;
444 DEBUG(dbgs() << "Live root (summary): " << Entry.first << "\n");
445 Worklist.push_back(ValueInfo(&Entry));
446 }
454 };
447455
448456 while (!Worklist.empty()) {
449457 auto VI = Worklist.pop_back_val();
450
451 // FIXME: we should only make the prevailing copy live here
452458 for (auto &Summary : VI.getSummaryList()) {
453 for (auto Ref : Summary->refs()) {
454 if (LiveSymbols.insert(Ref).second) {
455 DEBUG(dbgs() << "Marking live (ref): " << Ref.getGUID() << "\n");
456 Worklist.push_back(Ref);
457 }
458 }
459 if (auto *FS = dyn_cast(Summary.get())) {
460 for (auto Call : FS->calls()) {
461 if (LiveSymbols.insert(Call.first).second) {
462 DEBUG(dbgs() << "Marking live (call): " << Call.first.getGUID()
463 << "\n");
464 Worklist.push_back(Call.first);
465 }
466 }
467 }
459 for (auto Ref : Summary->refs())
460 visit(Ref);
461 if (auto *FS = dyn_cast(Summary.get()))
462 for (auto Call : FS->calls())
463 visit(Call.first);
468464 if (auto *AS = dyn_cast(Summary.get())) {
469465 auto AliaseeGUID = AS->getAliasee().getOriginalName();
470466 ValueInfo AliaseeVI = Index.getValueInfo(AliaseeGUID);
471 if (AliaseeVI && LiveSymbols.insert(AliaseeVI).second) {
472 DEBUG(dbgs() << "Marking live (alias): " << AliaseeGUID << "\n");
473 Worklist.push_back(AliaseeVI);
474 }
467 if (AliaseeVI)
468 visit(AliaseeVI);
475469 }
476470 }
477471 }
478 DenseSet DeadSymbols;
479 DeadSymbols.reserve(
480 std::min(Index.size(), Index.size() - LiveSymbols.size()));
481 for (auto &Entry : Index) {
482 if (!LiveSymbols.count(ValueInfo(&Entry))) {
483 DEBUG(dbgs() << "Marking dead: " << Entry.first << "\n");
484 DeadSymbols.insert(Entry.first);
485 }
486 }
487 DEBUG(dbgs() << LiveSymbols.size() << " symbols Live, and "
488 << DeadSymbols.size() << " symbols Dead \n");
489 NumDeadSymbols += DeadSymbols.size();
490 NumLiveSymbols += LiveSymbols.size();
491 return DeadSymbols;
472 Index.setWithGlobalValueDeadStripping();
473
474 unsigned DeadSymbols = Index.size() - LiveSymbols;
475 DEBUG(dbgs() << LiveSymbols << " symbols Live, and " << DeadSymbols
476 << " symbols Dead \n");
477 NumDeadSymbols += DeadSymbols;
478 NumLiveSymbols += LiveSymbols;
492479 }
493480
494481 /// Compute the set of summaries needed for a ThinLTO backend compilation of