Skip to content

Commit

Permalink
[ThinLTO] Shrink FunctionSummary by 8 bytes (#107706)
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kazutakahirata authored Sep 7, 2024
1 parent a1e06f7 commit 51d3829
Show file tree
Hide file tree
Showing 7 changed files with 30 additions and 24 deletions.
2 changes: 1 addition & 1 deletion llvm/include/llvm/AsmParser/LLParser.h
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ namespace llvm {
bool parseGVFlags(GlobalValueSummary::GVFlags &GVFlags);
bool parseGVarFlags(GlobalVarSummary::GVarFlags &GVarFlags);
bool parseOptionalFFlags(FunctionSummary::FFlags &FFlags);
bool parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls);
bool parseOptionalCalls(SmallVectorImpl<FunctionSummary::EdgeTy> &Calls);
bool parseHotness(CalleeInfo::HotnessType &Hotness);
bool parseOptionalTypeIdInfo(FunctionSummary::TypeIdInfo &TypeIdInfo);
bool parseTypeTests(std::vector<GlobalValue::GUID> &TypeTests);
Expand Down
23 changes: 14 additions & 9 deletions llvm/include/llvm/IR/ModuleSummaryIndex.h
Original file line number Diff line number Diff line change
Expand Up @@ -851,7 +851,7 @@ class FunctionSummary : public GlobalValueSummary {
/// Create an empty FunctionSummary (with specified call edges).
/// Used to represent external nodes and the dummy root node.
static FunctionSummary
makeDummyFunctionSummary(std::vector<FunctionSummary::EdgeTy> Edges) {
makeDummyFunctionSummary(SmallVectorImpl<FunctionSummary::EdgeTy> &&Edges) {
return FunctionSummary(
FunctionSummary::GVFlags(
GlobalValue::LinkageTypes::AvailableExternallyLinkage,
Expand Down Expand Up @@ -880,7 +880,9 @@ class FunctionSummary : public GlobalValueSummary {
FFlags FunFlags;

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

std::unique_ptr<TypeIdInfo> TIdInfo;

Expand Down Expand Up @@ -910,7 +912,7 @@ class FunctionSummary : public GlobalValueSummary {
public:
FunctionSummary(GVFlags Flags, unsigned NumInsts, FFlags FunFlags,
SmallVectorImpl<ValueInfo> &&Refs,
std::vector<EdgeTy> CGEdges,
SmallVectorImpl<EdgeTy> &&CGEdges,
std::vector<GlobalValue::GUID> TypeTests,
std::vector<VFuncId> TypeTestAssumeVCalls,
std::vector<VFuncId> TypeCheckedLoadVCalls,
Expand Down Expand Up @@ -957,7 +959,7 @@ class FunctionSummary : public GlobalValueSummary {
/// Return the list of <CalleeValueInfo, CalleeInfo> pairs.
ArrayRef<EdgeTy> calls() const { return CallGraphEdgeList; }

std::vector<EdgeTy> &mutableCalls() { return CallGraphEdgeList; }
SmallVector<EdgeTy, 0> &mutableCalls() { return CallGraphEdgeList; }

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

Expand Down Expand Up @@ -1535,7 +1537,7 @@ class ModuleSummaryIndex {
discoverNodes(ValueInfo(HaveGVs, &S), FunctionHasParent);
}

std::vector<FunctionSummary::EdgeTy> Edges;
SmallVector<FunctionSummary::EdgeTy, 0> Edges;
// create edges to all roots in the Index
for (auto &P : FunctionHasParent) {
if (P.second)
Expand All @@ -1544,9 +1546,11 @@ class ModuleSummaryIndex {
}
if (Edges.empty()) {
// Failed to find root - return an empty node
return FunctionSummary::makeDummyFunctionSummary({});
return FunctionSummary::makeDummyFunctionSummary(
SmallVector<FunctionSummary::EdgeTy, 0>());
}
auto CallGraphRoot = FunctionSummary::makeDummyFunctionSummary(Edges);
auto CallGraphRoot =
FunctionSummary::makeDummyFunctionSummary(std::move(Edges));
return CallGraphRoot;
}

Expand Down Expand Up @@ -1894,10 +1898,11 @@ template <> struct GraphTraits<ValueInfo> {
return P.first;
}
using ChildIteratorType =
mapped_iterator<std::vector<FunctionSummary::EdgeTy>::iterator,
mapped_iterator<SmallVector<FunctionSummary::EdgeTy, 0>::iterator,
decltype(&valueInfoFromEdge)>;

using ChildEdgeIteratorType = std::vector<FunctionSummary::EdgeTy>::iterator;
using ChildEdgeIteratorType =
SmallVector<FunctionSummary::EdgeTy, 0>::iterator;

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

Expand Down
2 changes: 1 addition & 1 deletion llvm/include/llvm/IR/ModuleSummaryIndexYAML.h
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ template <> struct CustomMappingTraits<GlobalValueSummaryMapTy> {
FSum.CanAutoHide,
static_cast<GlobalValueSummary::ImportKind>(FSum.ImportType)),
/*NumInsts=*/0, FunctionSummary::FFlags{}, std::move(Refs),
ArrayRef<FunctionSummary::EdgeTy>{}, std::move(FSum.TypeTests),
SmallVector<FunctionSummary::EdgeTy, 0>{}, std::move(FSum.TypeTests),
std::move(FSum.TypeTestAssumeVCalls),
std::move(FSum.TypeCheckedLoadVCalls),
std::move(FSum.TypeTestAssumeConstVCalls),
Expand Down
4 changes: 2 additions & 2 deletions llvm/lib/Analysis/ModuleSummaryAnalysis.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -309,7 +309,7 @@ static void computeFunctionSummary(
// Map from callee ValueId to profile count. Used to accumulate profile
// counts for all static calls to a given callee.
MapVector<ValueInfo, CalleeInfo, DenseMap<ValueInfo, unsigned>,
std::vector<std::pair<ValueInfo, CalleeInfo>>>
SmallVector<FunctionSummary::EdgeTy, 0>>
CallGraphEdges;
SetVector<ValueInfo, SmallVector<ValueInfo, 0>> RefEdges, LoadRefEdges,
StoreRefEdges;
Expand Down Expand Up @@ -964,7 +964,7 @@ ModuleSummaryIndex llvm::buildModuleSummaryIndex(
/* HasUnknownCall */ true,
/* MustBeUnreachable */ false},
SmallVector<ValueInfo, 0>{},
ArrayRef<FunctionSummary::EdgeTy>{},
SmallVector<FunctionSummary::EdgeTy, 0>{},
ArrayRef<GlobalValue::GUID>{},
ArrayRef<FunctionSummary::VFuncId>{},
ArrayRef<FunctionSummary::VFuncId>{},
Expand Down
5 changes: 3 additions & 2 deletions llvm/lib/AsmParser/LLParser.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9396,7 +9396,7 @@ bool LLParser::parseFunctionSummary(std::string Name, GlobalValue::GUID GUID,
/*Live=*/false, /*IsLocal=*/false, /*CanAutoHide=*/false,
GlobalValueSummary::Definition);
unsigned InstCount;
std::vector<FunctionSummary::EdgeTy> Calls;
SmallVector<FunctionSummary::EdgeTy, 0> Calls;
FunctionSummary::TypeIdInfo TypeIdInfo;
std::vector<FunctionSummary::ParamAccess> ParamAccesses;
SmallVector<ValueInfo, 0> Refs;
Expand Down Expand Up @@ -9685,7 +9685,8 @@ bool LLParser::parseOptionalFFlags(FunctionSummary::FFlags &FFlags) {
/// Call ::= '(' 'callee' ':' GVReference
/// [( ',' 'hotness' ':' Hotness | ',' 'relbf' ':' UInt32 )]?
/// [ ',' 'tail' ]? ')'
bool LLParser::parseOptionalCalls(std::vector<FunctionSummary::EdgeTy> &Calls) {
bool LLParser::parseOptionalCalls(
SmallVectorImpl<FunctionSummary::EdgeTy> &Calls) {
assert(Lex.getKind() == lltok::kw_calls);
Lex.Lex();

Expand Down
15 changes: 7 additions & 8 deletions llvm/lib/Bitcode/Reader/BitcodeReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -986,10 +986,9 @@ class ModuleSummaryIndexBitcodeReader : public BitcodeReaderBase {
uint64_t Offset,
DenseMap<unsigned, GlobalValue::LinkageTypes> &ValueIdToLinkageMap);
SmallVector<ValueInfo, 0> makeRefList(ArrayRef<uint64_t> Record);
std::vector<FunctionSummary::EdgeTy> makeCallList(ArrayRef<uint64_t> Record,
bool IsOldProfileFormat,
bool HasProfile,
bool HasRelBF);
SmallVector<FunctionSummary::EdgeTy, 0>
makeCallList(ArrayRef<uint64_t> Record, bool IsOldProfileFormat,
bool HasProfile, bool HasRelBF);
Error parseEntireSummary(unsigned ID);
Error parseModuleStringTable();
void parseTypeIdCompatibleVtableSummaryRecord(ArrayRef<uint64_t> Record);
Expand Down Expand Up @@ -7378,11 +7377,11 @@ ModuleSummaryIndexBitcodeReader::makeRefList(ArrayRef<uint64_t> Record) {
return Ret;
}

std::vector<FunctionSummary::EdgeTy>
SmallVector<FunctionSummary::EdgeTy, 0>
ModuleSummaryIndexBitcodeReader::makeCallList(ArrayRef<uint64_t> Record,
bool IsOldProfileFormat,
bool HasProfile, bool HasRelBF) {
std::vector<FunctionSummary::EdgeTy> Ret;
SmallVector<FunctionSummary::EdgeTy, 0> Ret;
// In the case of new profile formats, there are two Record entries per
// Edge. Otherwise, conservatively reserve up to Record.size.
if (!IsOldProfileFormat && (HasProfile || HasRelBF))
Expand Down Expand Up @@ -7670,7 +7669,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));
bool HasProfile = (BitCode == bitc::FS_PERMODULE_PROFILE);
bool HasRelBF = (BitCode == bitc::FS_PERMODULE_RELBF);
std::vector<FunctionSummary::EdgeTy> Calls = makeCallList(
SmallVector<FunctionSummary::EdgeTy, 0> Calls = makeCallList(
ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex),
IsOldProfileFormat, HasProfile, HasRelBF);
setSpecialRefs(Refs, NumRORefs, NumWORefs);
Expand Down Expand Up @@ -7824,7 +7823,7 @@ Error ModuleSummaryIndexBitcodeReader::parseEntireSummary(unsigned ID) {
SmallVector<ValueInfo, 0> Refs = makeRefList(
ArrayRef<uint64_t>(Record).slice(RefListStartIndex, NumRefs));
bool HasProfile = (BitCode == bitc::FS_COMBINED_PROFILE);
std::vector<FunctionSummary::EdgeTy> Edges = makeCallList(
SmallVector<FunctionSummary::EdgeTy, 0> Edges = makeCallList(
ArrayRef<uint64_t>(Record).slice(CallGraphEdgeStartIndex),
IsOldProfileFormat, HasProfile, false);
ValueInfo VI = std::get<0>(getValueInfoFromValueId(ValueID));
Expand Down
3 changes: 2 additions & 1 deletion llvm/lib/IR/ModuleSummaryIndex.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ static cl::opt<bool> ImportConstantsWithRefs(
constexpr uint32_t FunctionSummary::ParamAccess::RangeWidth;

FunctionSummary FunctionSummary::ExternalNode =
FunctionSummary::makeDummyFunctionSummary({});
FunctionSummary::makeDummyFunctionSummary(
SmallVector<FunctionSummary::EdgeTy, 0>());

GlobalValue::VisibilityTypes ValueInfo::getELFVisibility() const {
bool HasProtected = false;
Expand Down

0 comments on commit 51d3829

Please sign in to comment.