Skip to content
This repository was archived by the owner on Apr 23, 2020. It is now read-only.

Commit ccb80b9

Browse files
committed
(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
1 parent cbf8bcc commit ccb80b9

File tree

8 files changed

+114
-103
lines changed

8 files changed

+114
-103
lines changed

include/llvm/IR/ModuleSummaryIndex.h

+31-13
Original file line numberDiff line numberDiff line change
@@ -134,16 +134,18 @@ class GlobalValueSummary {
134134
/// be renamed or references something that can't be renamed).
135135
unsigned NotEligibleToImport : 1;
136136

137-
/// Indicate that the global value must be considered a live root for
138-
/// index-based liveness analysis. Used for special LLVM values such as
139-
/// llvm.global_ctors that the linker does not know about.
140-
unsigned LiveRoot : 1;
137+
/// In per-module summary, indicate that the global value must be considered
138+
/// a live root for index-based liveness analysis. Used for special LLVM
139+
/// values such as llvm.global_ctors that the linker does not know about.
140+
///
141+
/// In combined summary, indicate that the global value is live.
142+
unsigned Live : 1;
141143

142144
/// Convenience Constructors
143145
explicit GVFlags(GlobalValue::LinkageTypes Linkage,
144-
bool NotEligibleToImport, bool LiveRoot)
146+
bool NotEligibleToImport, bool Live)
145147
: Linkage(Linkage), NotEligibleToImport(NotEligibleToImport),
146-
LiveRoot(LiveRoot) {}
148+
Live(Live) {}
147149
};
148150

149151
private:
@@ -172,6 +174,8 @@ class GlobalValueSummary {
172174
/// are listed in the derived FunctionSummary object.
173175
std::vector<ValueInfo> RefEdgeList;
174176

177+
bool isLive() const { return Flags.Live; }
178+
175179
protected:
176180
GlobalValueSummary(SummaryKind K, GVFlags Flags, std::vector<ValueInfo> Refs)
177181
: Kind(K), Flags(Flags), RefEdgeList(std::move(Refs)) {}
@@ -213,19 +217,17 @@ class GlobalValueSummary {
213217
/// Return true if this global value can't be imported.
214218
bool notEligibleToImport() const { return Flags.NotEligibleToImport; }
215219

216-
/// Return true if this global value must be considered a root for live
217-
/// value analysis on the index.
218-
bool liveRoot() const { return Flags.LiveRoot; }
219-
220-
/// Flag that this global value must be considered a root for live
221-
/// value analysis on the index.
222-
void setLiveRoot() { Flags.LiveRoot = true; }
220+
void setLive(bool Live) { Flags.Live = Live; }
223221

224222
/// Flag that this global value cannot be imported.
225223
void setNotEligibleToImport() { Flags.NotEligibleToImport = true; }
226224

227225
/// Return the list of values referenced by this global value definition.
228226
ArrayRef<ValueInfo> refs() const { return RefEdgeList; }
227+
228+
friend class ModuleSummaryIndex;
229+
friend void computeDeadSymbols(class ModuleSummaryIndex &,
230+
const DenseSet<GlobalValue::GUID> &);
229231
};
230232

231233
/// \brief Alias summary information.
@@ -535,6 +537,11 @@ class ModuleSummaryIndex {
535537
/// GUIDs, it will be mapped to 0.
536538
std::map<GlobalValue::GUID, GlobalValue::GUID> OidGuidMap;
537539

540+
/// Indicates that summary-based GlobalValue GC has run, and values with
541+
/// GVFlags::Live==false are really dead. Otherwise, all values must be
542+
/// considered live.
543+
bool WithGlobalValueDeadStripping = false;
544+
538545
// YAML I/O support.
539546
friend yaml::MappingTraits<ModuleSummaryIndex>;
540547

@@ -550,6 +557,17 @@ class ModuleSummaryIndex {
550557
const_gvsummary_iterator end() const { return GlobalValueMap.end(); }
551558
size_t size() const { return GlobalValueMap.size(); }
552559

560+
bool withGlobalValueDeadStripping() const {
561+
return WithGlobalValueDeadStripping;
562+
}
563+
void setWithGlobalValueDeadStripping() {
564+
WithGlobalValueDeadStripping = true;
565+
}
566+
567+
bool isGlobalValueLive(const GlobalValueSummary *GVS) const {
568+
return !WithGlobalValueDeadStripping || GVS->isLive();
569+
}
570+
553571
/// Return a ValueInfo for GUID if it exists, otherwise return ValueInfo().
554572
ValueInfo getValueInfo(GlobalValue::GUID GUID) const {
555573
auto I = GlobalValueMap.find(GUID);

include/llvm/Transforms/IPO/FunctionImport.h

+4-8
Original file line numberDiff line numberDiff line change
@@ -81,15 +81,11 @@ class FunctionImportPass : public PassInfoMixin<FunctionImportPass> {
8181
/// \p ExportLists contains for each Module the set of globals (GUID) that will
8282
/// be imported by another module, or referenced by such a function. I.e. this
8383
/// is the set of globals that need to be promoted/renamed appropriately.
84-
///
85-
/// \p DeadSymbols (optional) contains a list of GUID that are deemed "dead" and
86-
/// will be ignored for the purpose of importing.
8784
void ComputeCrossModuleImport(
8885
const ModuleSummaryIndex &Index,
8986
const StringMap<GVSummaryMapTy> &ModuleToDefinedGVSummaries,
9087
StringMap<FunctionImporter::ImportMapTy> &ImportLists,
91-
StringMap<FunctionImporter::ExportSetTy> &ExportLists,
92-
const DenseSet<GlobalValue::GUID> *DeadSymbols = nullptr);
88+
StringMap<FunctionImporter::ExportSetTy> &ExportLists);
9389

9490
/// Compute all the imports for the given module using the Index.
9591
///
@@ -102,9 +98,9 @@ void ComputeCrossModuleImportForModule(
10298
/// Compute all the symbols that are "dead": i.e these that can't be reached
10399
/// in the graph from any of the given symbols listed in
104100
/// \p GUIDPreservedSymbols.
105-
DenseSet<GlobalValue::GUID>
106-
computeDeadSymbols(const ModuleSummaryIndex &Index,
107-
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
101+
void computeDeadSymbols(
102+
ModuleSummaryIndex &Index,
103+
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols);
108104

109105
/// Compute the set of summaries needed for a ThinLTO backend compilation of
110106
/// \p ModulePath.

lib/Analysis/ModuleSummaryAnalysis.cpp

+6-6
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ computeFunctionSummary(ModuleSummaryIndex &Index, const Module &M,
275275
// FIXME: refactor this to use the same code that inliner is using.
276276
F.isVarArg();
277277
GlobalValueSummary::GVFlags Flags(F.getLinkage(), NotEligibleForImport,
278-
/* LiveRoot = */ false);
278+
/* Live = */ false);
279279
auto FuncSummary = llvm::make_unique<FunctionSummary>(
280280
Flags, NumInsts, RefEdges.takeVector(), CallGraphEdges.takeVector(),
281281
TypeTests.takeVector(), TypeTestAssumeVCalls.takeVector(),
@@ -295,7 +295,7 @@ computeVariableSummary(ModuleSummaryIndex &Index, const GlobalVariable &V,
295295
findRefEdges(Index, &V, RefEdges, Visited);
296296
bool NonRenamableLocal = isNonRenamableLocal(V);
297297
GlobalValueSummary::GVFlags Flags(V.getLinkage(), NonRenamableLocal,
298-
/* LiveRoot = */ false);
298+
/* Live = */ false);
299299
auto GVarSummary =
300300
llvm::make_unique<GlobalVarSummary>(Flags, RefEdges.takeVector());
301301
if (NonRenamableLocal)
@@ -308,7 +308,7 @@ computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
308308
DenseSet<GlobalValue::GUID> &CantBePromoted) {
309309
bool NonRenamableLocal = isNonRenamableLocal(A);
310310
GlobalValueSummary::GVFlags Flags(A.getLinkage(), NonRenamableLocal,
311-
/* LiveRoot = */ false);
311+
/* Live = */ false);
312312
auto AS = llvm::make_unique<AliasSummary>(Flags, ArrayRef<ValueInfo>{});
313313
auto *Aliasee = A.getBaseObject();
314314
auto *AliaseeSummary = Index.getGlobalValueSummary(*Aliasee);
@@ -323,7 +323,7 @@ computeAliasSummary(ModuleSummaryIndex &Index, const GlobalAlias &A,
323323
static void setLiveRoot(ModuleSummaryIndex &Index, StringRef Name) {
324324
if (ValueInfo VI = Index.getValueInfo(GlobalValue::getGUID(Name)))
325325
for (auto &Summary : VI.getSummaryList())
326-
Summary->setLiveRoot();
326+
Summary->setLive(true);
327327
}
328328

329329
ModuleSummaryIndex llvm::buildModuleSummaryIndex(
@@ -423,8 +423,8 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
423423
return;
424424
assert(GV->isDeclaration() && "Def in module asm already has definition");
425425
GlobalValueSummary::GVFlags GVFlags(GlobalValue::InternalLinkage,
426-
/* NotEligibleToImport */ true,
427-
/* LiveRoot */ true);
426+
/* NotEligibleToImport = */ true,
427+
/* Live = */ true);
428428
CantBePromoted.insert(GlobalValue::getGUID(Name));
429429
// Create the appropriate summary type.
430430
if (isa<Function>(GV)) {

lib/Bitcode/Reader/BitcodeReader.cpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -865,11 +865,11 @@ static GlobalValueSummary::GVFlags getDecodedGVSummaryFlags(uint64_t RawFlags,
865865
auto Linkage = GlobalValue::LinkageTypes(RawFlags & 0xF); // 4 bits
866866
RawFlags = RawFlags >> 4;
867867
bool NotEligibleToImport = (RawFlags & 0x1) || Version < 3;
868-
// The LiveRoot flag wasn't introduced until version 3. For dead stripping
868+
// The Live flag wasn't introduced until version 3. For dead stripping
869869
// to work correctly on earlier versions, we must conservatively treat all
870870
// values as live.
871-
bool LiveRoot = (RawFlags & 0x2) || Version < 3;
872-
return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, LiveRoot);
871+
bool Live = (RawFlags & 0x2) || Version < 3;
872+
return GlobalValueSummary::GVFlags(Linkage, NotEligibleToImport, Live);
873873
}
874874

875875
static GlobalValue::VisibilityTypes getDecodedVisibility(unsigned Val) {

lib/Bitcode/Writer/BitcodeWriter.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -864,7 +864,7 @@ static uint64_t getEncodedGVSummaryFlags(GlobalValueSummary::GVFlags Flags) {
864864
uint64_t RawFlags = 0;
865865

866866
RawFlags |= Flags.NotEligibleToImport; // bool
867-
RawFlags |= (Flags.LiveRoot << 1);
867+
RawFlags |= (Flags.Live << 1);
868868
// Linkage don't need to be remapped at that time for the summary. Any future
869869
// change to the getEncodedLinkage() function will need to be taken into
870870
// account here as well.

lib/LTO/LTO.cpp

+14-4
Original file line numberDiff line numberDiff line change
@@ -930,6 +930,17 @@ ThinBackend lto::createWriteIndexesThinBackend(std::string OldPrefix,
930930
};
931931
}
932932

933+
static bool IsLiveByGUID(const ModuleSummaryIndex &Index,
934+
GlobalValue::GUID GUID) {
935+
auto VI = Index.getValueInfo(GUID);
936+
if (!VI)
937+
return false;
938+
for (auto &I : VI.getSummaryList())
939+
if (Index.isGlobalValueLive(I.get()))
940+
return true;
941+
return false;
942+
}
943+
933944
Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
934945
bool HasRegularLTO) {
935946
if (ThinLTO.ModuleMap.empty())
@@ -973,11 +984,10 @@ Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
973984
GlobalValue::dropLLVMManglingEscape(Res.second.IRName)));
974985
}
975986

976-
auto DeadSymbols =
977-
computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols);
987+
computeDeadSymbols(ThinLTO.CombinedIndex, GUIDPreservedSymbols);
978988

979989
ComputeCrossModuleImport(ThinLTO.CombinedIndex, ModuleToDefinedGVSummaries,
980-
ImportLists, ExportLists, &DeadSymbols);
990+
ImportLists, ExportLists);
981991

982992
std::set<GlobalValue::GUID> ExportedGUIDs;
983993
for (auto &Res : GlobalResolutions) {
@@ -992,7 +1002,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, NativeObjectCache Cache,
9921002
auto GUID = GlobalValue::getGUID(
9931003
GlobalValue::dropLLVMManglingEscape(Res.second.IRName));
9941004
// Mark exported unless index-based analysis determined it to be dead.
995-
if (!DeadSymbols.count(GUID))
1005+
if (IsLiveByGUID(ThinLTO.CombinedIndex, GUID))
9961006
ExportedGUIDs.insert(GUID);
9971007
}
9981008

lib/LTO/ThinLTOCodeGenerator.cpp

+8-8
Original file line numberDiff line numberDiff line change
@@ -628,13 +628,13 @@ void ThinLTOCodeGenerator::promote(Module &TheModule,
628628
PreservedSymbols, Triple(TheModule.getTargetTriple()));
629629

630630
// Compute "dead" symbols, we don't want to import/export these!
631-
auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
631+
computeDeadSymbols(Index, GUIDPreservedSymbols);
632632

633633
// Generate import/export list
634634
StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
635635
StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
636636
ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
637-
ExportLists, &DeadSymbols);
637+
ExportLists);
638638

639639
// Resolve LinkOnce/Weak symbols.
640640
StringMap<std::map<GlobalValue::GUID, GlobalValue::LinkageTypes>> ResolvedODR;
@@ -673,13 +673,13 @@ void ThinLTOCodeGenerator::crossModuleImport(Module &TheModule,
673673
PreservedSymbols, Triple(TheModule.getTargetTriple()));
674674

675675
// Compute "dead" symbols, we don't want to import/export these!
676-
auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
676+
computeDeadSymbols(Index, GUIDPreservedSymbols);
677677

678678
// Generate import/export list
679679
StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
680680
StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
681681
ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
682-
ExportLists, &DeadSymbols);
682+
ExportLists);
683683
auto &ImportList = ImportLists[TheModule.getModuleIdentifier()];
684684

685685
crossImportIntoModule(TheModule, Index, ModuleMap, ImportList);
@@ -750,13 +750,13 @@ void ThinLTOCodeGenerator::internalize(Module &TheModule,
750750
Index.collectDefinedGVSummariesPerModule(ModuleToDefinedGVSummaries);
751751

752752
// Compute "dead" symbols, we don't want to import/export these!
753-
auto DeadSymbols = computeDeadSymbols(Index, GUIDPreservedSymbols);
753+
computeDeadSymbols(Index, GUIDPreservedSymbols);
754754

755755
// Generate import/export list
756756
StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
757757
StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
758758
ComputeCrossModuleImport(Index, ModuleToDefinedGVSummaries, ImportLists,
759-
ExportLists, &DeadSymbols);
759+
ExportLists);
760760
auto &ExportList = ExportLists[ModuleIdentifier];
761761

762762
// Be friendly and don't nuke totally the module when the client didn't
@@ -902,14 +902,14 @@ void ThinLTOCodeGenerator::run() {
902902
computeGUIDPreservedSymbols(PreservedSymbols, TMBuilder.TheTriple);
903903

904904
// Compute "dead" symbols, we don't want to import/export these!
905-
auto DeadSymbols = computeDeadSymbols(*Index, GUIDPreservedSymbols);
905+
computeDeadSymbols(*Index, GUIDPreservedSymbols);
906906

907907
// Collect the import/export lists for all modules from the call-graph in the
908908
// combined index.
909909
StringMap<FunctionImporter::ImportMapTy> ImportLists(ModuleCount);
910910
StringMap<FunctionImporter::ExportSetTy> ExportLists(ModuleCount);
911911
ComputeCrossModuleImport(*Index, ModuleToDefinedGVSummaries, ImportLists,
912-
ExportLists, &DeadSymbols);
912+
ExportLists);
913913

914914
// We use a std::map here to be able to have a defined ordering when
915915
// producing a hash for the cache entry.

0 commit comments

Comments
 (0)