Skip to content

Commit 51d3829

Browse files
[ThinLTO] Shrink FunctionSummary by 8 bytes (llvm#107706)
During the ThinLTO indexing step for one of our large applications, we create 4 million instances of FunctionSummary. Changing: std::vector<EdgeTy> CallGraphEdgeList; to: SmallVector<EdgeTy, 0> CallGraphEdgeList; in FunctionSummary reduces the size of each instance by 8 bytes. The rest of the patch makes the same change to other places so that the types stay compatible across function boundaries.
1 parent a1e06f7 commit 51d3829

File tree

7 files changed

+30
-24
lines changed

7 files changed

+30
-24
lines changed

llvm/include/llvm/AsmParser/LLParser.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -394,7 +394,7 @@ namespace llvm {
394394
bool parseGVFlags(GlobalValueSummary::GVFlags &GVFlags);
395395
bool parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags);
396396
bool parseOptionalFFlags(FunctionSummary::FFlags &FFlags);
397-
bool parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls);
397+
bool parseOptionalCalls(SmallVectorImpl<FunctionSummary::EdgeTy> &Calls);
398398
bool parseHotness(CalleeInfo::HotnessType &Hotness);
399399
bool parseOptionalTypeIdInfo(FunctionSummary::TypeIdInfo &TypeIdInfo);
400400
bool parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests);

llvm/include/llvm/IR/ModuleSummaryIndex.h

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -851,7 +851,7 @@ class FunctionSummary : public GlobalValueSummary {
851851
/// Create an empty FunctionSummary (with specified call edges).
852852
/// Used to represent external nodes and the dummy root node.
853853
static FunctionSummary
854-
makeDummyFunctionSummary(std::vector<FunctionSummary::EdgeTy> Edges) {
854+
makeDummyFunctionSummary(SmallVectorImpl<FunctionSummary::EdgeTy> &&Edges) {
855855
return FunctionSummary(
856856
FunctionSummary::GVFlags(
857857
GlobalValue::LinkageTypes::AvailableExternallyLinkage,
@@ -880,7 +880,9 @@ class FunctionSummary : public GlobalValueSummary {
880880
FFlags FunFlags;
881881

882882
/// List of <CalleeValueInfo, CalleeInfo> call edge pairs from this function.
883-
std::vector<EdgeTy> CallGraphEdgeList;
883+
/// We use SmallVector<ValueInfo, 0> instead of std::vector<ValueInfo> for its
884+
/// smaller memory footprint.
885+
SmallVector<EdgeTy, 0> CallGraphEdgeList;
884886

885887
std::unique_ptr<TypeIdInfo> TIdInfo;
886888

@@ -910,7 +912,7 @@ class FunctionSummary : public GlobalValueSummary {
910912
public:
911913
FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags,
912914
SmallVectorImpl<ValueInfo> &&Refs,
913-
std::vector<EdgeTy> CGEdges,
915+
SmallVectorImpl<EdgeTy> &&CGEdges,
914916
std::vector<GlobalValue::GUID> TypeTests,
915917
std::vector<VFuncId> TypeTestAssumeVCalls,
916918
std::vector<VFuncId> TypeCheckedLoadVCalls,
@@ -957,7 +959,7 @@ class FunctionSummary : public GlobalValueSummary {
957959
/// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
958960
ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }
959961

960-
std::vector<EdgeTy> &mutableCalls() { return CallGraphEdgeList; }
962+
SmallVector<EdgeTy, 0> &mutableCalls() { return CallGraphEdgeList; }
961963

962964
void addCall(EdgeTy E) { CallGraphEdgeList.push_back(E); }
963965

@@ -1535,7 +1537,7 @@ class ModuleSummaryIndex {
15351537
discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent);
15361538
}
15371539

1538-
std::vector<FunctionSummary::EdgeTy> Edges;
1540+
SmallVector<FunctionSummary::EdgeTy, 0> Edges;
15391541
// create edges to all roots in the Index
15401542
for (auto &P : FunctionHasParent) {
15411543
if (P.second)
@@ -1544,9 +1546,11 @@ class ModuleSummaryIndex {
15441546
}
15451547
if (Edges.empty()) {
15461548
// Failed to find root - return an empty node
1547-
return FunctionSummary::makeDummyFunctionSummary({});
1549+
return FunctionSummary::makeDummyFunctionSummary(
1550+
SmallVector<FunctionSummary::EdgeTy, 0>());
15481551
}
1549-
auto CallGraphRoot = FunctionSummary::makeDummyFunctionSummary(Edges);
1552+
auto CallGraphRoot =
1553+
FunctionSummary::makeDummyFunctionSummary(std::move(Edges));
15501554
return CallGraphRoot;
15511555
}
15521556

@@ -1894,10 +1898,11 @@ template <> struct GraphTraits<ValueInfo> {
18941898
return P.first;
18951899
}
18961900
using ChildIteratorType =
1897-
mapped_iterator<std::vector<FunctionSummary::EdgeTy>::iterator,
1901+
mapped_iterator<SmallVector<FunctionSummary::EdgeTy, 0>::iterator,
18981902
decltype(&valueInfoFromEdge)>;
18991903

1900-
using ChildEdgeIteratorType = std::vector<FunctionSummary::EdgeTy>::iterator;
1904+
using ChildEdgeIteratorType =
1905+
SmallVector<FunctionSummary::EdgeTy, 0>::iterator;
19011906

19021907
static NodeRef getEntryNode(ValueInfo V) { return V; }
19031908

llvm/include/llvm/IR/ModuleSummaryIndexYAML.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -230,7 +230,7 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
230230
FSum.CanAutoHide,
231231
static_cast<GlobalValueSummary::ImportKind>(FSum.ImportType)),
232232
/*NumInsts=*/0, FunctionSummary::FFlags{}, std::move(Refs),
233-
ArrayRef<FunctionSummary::EdgeTy>{}, std::move(FSum.TypeTests),
233+
SmallVector<FunctionSummary::EdgeTy, 0>{}, std::move(FSum.TypeTests),
234234
std::move(FSum.TypeTestAssumeVCalls),
235235
std::move(FSum.TypeCheckedLoadVCalls),
236236
std::move(FSum.TypeTestAssumeConstVCalls),

llvm/lib/Analysis/ModuleSummaryAnalysis.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -309,7 +309,7 @@ static void computeFunctionSummary(
309309
// Map from callee ValueId to profile count. Used to accumulate profile
310310
// counts for all static calls to a given callee.
311311
MapVector<ValueInfo, CalleeInfo, DenseMap<ValueInfo, unsigned>,
312-
std::vector<std::pair<ValueInfo, CalleeInfo>>>
312+
SmallVector<FunctionSummary::EdgeTy, 0>>
313313
CallGraphEdges;
314314
SetVector<ValueInfo, SmallVector<ValueInfo, 0>> RefEdges, LoadRefEdges,
315315
StoreRefEdges;
@@ -964,7 +964,7 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
964964
/* HasUnknownCall */ true,
965965
/* MustBeUnreachable */ false},
966966
SmallVector<ValueInfo, 0>{},
967-
ArrayRef<FunctionSummary::EdgeTy>{},
967+
SmallVector<FunctionSummary::EdgeTy, 0>{},
968968
ArrayRef<GlobalValue::GUID>{},
969969
ArrayRef<FunctionSummary::VFuncId>{},
970970
ArrayRef<FunctionSummary::VFuncId>{},

llvm/lib/AsmParser/LLParser.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9396,7 +9396,7 @@ bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
93969396
/*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
93979397
GlobalValueSummary::Definition);
93989398
unsigned InstCount;
9399-
std::vector<FunctionSummary::EdgeTy> Calls;
9399+
SmallVector<FunctionSummary::EdgeTy, 0> Calls;
94009400
FunctionSummary::TypeIdInfo TypeIdInfo;
94019401
std::vector<FunctionSummary::ParamAccess> ParamAccesses;
94029402
SmallVector<ValueInfo, 0> Refs;
@@ -9685,7 +9685,8 @@ bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
96859685
/// Call ::= '(' 'callee' ':' GVReference
96869686
/// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]?
96879687
/// [ ',' 'tail' ]? ')'
9688-
bool LLParser::parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls) {
9688+
bool LLParser::parseOptionalCalls(
9689+
SmallVectorImpl<FunctionSummary::EdgeTy> &Calls) {
96899690
assert(Lex.getKind() == lltok::kw_calls);
96909691
Lex.Lex();
96919692

llvm/lib/Bitcode/Reader/BitcodeReader.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -986,10 +986,9 @@ class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
986986
uint64_t Offset,
987987
DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
988988
SmallVector<ValueInfo, 0> makeRefList(ArrayRef<uint64_t> Record);
989-
std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record,
990-
bool IsOldProfileFormat,
991-
bool HasProfile,
992-
bool HasRelBF);
989+
SmallVector<FunctionSummary::EdgeTy, 0>
990+
makeCallList(ArrayRef<uint64_t> Record, bool IsOldProfileFormat,
991+
bool HasProfile, bool HasRelBF);
993992
Error parseEntireSummary(unsigned ID);
994993
Error parseModuleStringTable();
995994
void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record);
@@ -7378,11 +7377,11 @@ ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) {
73787377
return Ret;
73797378
}
73807379

7381-
std::vector<FunctionSummary::EdgeTy>
7380+
SmallVector<FunctionSummary::EdgeTy, 0>
73827381
ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
73837382
bool IsOldProfileFormat,
73847383
bool HasProfile, bool HasRelBF) {
7385-
std::vector<FunctionSummary::EdgeTy> Ret;
7384+
SmallVector<FunctionSummary::EdgeTy, 0> Ret;
73867385
// In the case of new profile formats, there are two Record entries per
73877386
// Edge. Otherwise, conservatively reserve up to Record.size.
73887387
if (!IsOldProfileFormat && (HasProfile || HasRelBF))
@@ -7670,7 +7669,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
76707669
ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));
76717670
bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE);
76727671
bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF);
7673-
std::vector<FunctionSummary::EdgeTy> Calls = makeCallList(
7672+
SmallVector<FunctionSummary::EdgeTy, 0> Calls = makeCallList(
76747673
ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex),
76757674
IsOldProfileFormat, HasProfile, HasRelBF);
76767675
setSpecialRefs(Refs, NumRORefs, NumWORefs);
@@ -7824,7 +7823,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
78247823
SmallVector<ValueInfo, 0> Refs = makeRefList(
78257824
ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));
78267825
bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE);
7827-
std::vector<FunctionSummary::EdgeTy> Edges = makeCallList(
7826+
SmallVector<FunctionSummary::EdgeTy, 0> Edges = makeCallList(
78287827
ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex),
78297828
IsOldProfileFormat, HasProfile, false);
78307829
ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID));

llvm/lib/IR/ModuleSummaryIndex.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,8 @@ static cl::opt<bool> ImportConstantsWithRefs(
3737
constexpr uint32_t FunctionSummary::ParamAccess::RangeWidth;
3838

3939
FunctionSummary FunctionSummary::ExternalNode =
40-
FunctionSummary::makeDummyFunctionSummary({});
40+
FunctionSummary::makeDummyFunctionSummary(
41+
SmallVector<FunctionSummary::EdgeTy, 0>());
4142

4243
GlobalValue::VisibilityTypes ValueInfo::getELFVisibility() const {
4344
bool HasProtected = false;

0 commit comments

Comments
 (0)