Skip to content

Commit

Permalink
[BOLT][NFC] Make YamlProfileToFunction a DenseMap (#108712)
Browse files Browse the repository at this point in the history
YAML function profiles have sparse function IDs, assigned from
sequential function IDs from profiled binary. For example, for one large
binary, YAML profile has 15K functions, but the highest ID is ~600K,
close to number of functions in the profiled binary.

In `matchProfileToFunction`, `YamlProfileToFunction` vector was resized
to match function ID, which entails a 40X overcommit. Change the type of
`YamlProfileToFunction` to DenseMap to reduce memory utilization.

#99891 makes use of it for profile lookup associated with a given binary
function.
  • Loading branch information
aaupov authored Nov 8, 2024
1 parent 7382509 commit d936924
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 13 deletions.
4 changes: 1 addition & 3 deletions bolt/include/bolt/Profile/YAMLProfileReader.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ class YAMLProfileReader : public ProfileReaderBase {
yaml::bolt::BinaryProfile YamlBP;

/// Map a function ID from a YAML profile to a BinaryFunction object.
std::vector<BinaryFunction *> YamlProfileToFunction;
DenseMap<uint32_t, BinaryFunction *> YamlProfileToFunction;

using FunctionSet = std::unordered_set<const BinaryFunction *>;
/// To keep track of functions that have a matched profile before the profile
Expand Down Expand Up @@ -162,8 +162,6 @@ class YAMLProfileReader : public ProfileReaderBase {
/// Update matched YAML -> BinaryFunction pair.
void matchProfileToFunction(yaml::bolt::BinaryFunctionProfile &YamlBF,
BinaryFunction &BF) {
if (YamlBF.Id >= YamlProfileToFunction.size())
YamlProfileToFunction.resize(YamlBF.Id + 1);
YamlProfileToFunction[YamlBF.Id] = &BF;
YamlBF.Used = true;

Expand Down
13 changes: 3 additions & 10 deletions bolt/lib/Profile/YAMLProfileReader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -238,9 +238,7 @@ bool YAMLProfileReader::parseFunctionProfile(
BB.setExecutionCount(YamlBB.ExecCount);

for (const yaml::bolt::CallSiteInfo &YamlCSI : YamlBB.CallSites) {
BinaryFunction *Callee = YamlCSI.DestId < YamlProfileToFunction.size()
? YamlProfileToFunction[YamlCSI.DestId]
: nullptr;
BinaryFunction *Callee = YamlProfileToFunction.lookup(YamlCSI.DestId);
bool IsFunction = Callee ? true : false;
MCSymbol *CalleeSymbol = nullptr;
if (IsFunction)
Expand Down Expand Up @@ -703,7 +701,7 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
break;
}
}
YamlProfileToFunction.resize(YamlBP.Functions.size() + 1);
YamlProfileToFunction.reserve(YamlBP.Functions.size());

// Computes hash for binary functions.
if (opts::MatchProfileWithFunctionHash) {
Expand Down Expand Up @@ -756,12 +754,7 @@ Error YAMLProfileReader::readProfile(BinaryContext &BC) {
NormalizeByCalls = usesEvent("branches");
uint64_t NumUnused = 0;
for (yaml::bolt::BinaryFunctionProfile &YamlBF : YamlBP.Functions) {
if (YamlBF.Id >= YamlProfileToFunction.size()) {
// Such profile was ignored.
++NumUnused;
continue;
}
if (BinaryFunction *BF = YamlProfileToFunction[YamlBF.Id])
if (BinaryFunction *BF = YamlProfileToFunction.lookup(YamlBF.Id))
parseFunctionProfile(*BF, YamlBF);
else
++NumUnused;
Expand Down

0 comments on commit d936924

Please sign in to comment.