Skip to content

Commit c8fc234

Browse files
authored
[BOLT][NFC] Eliminate uses of throwing std::map::at (#92950)
Remove calls to std::unordered_map::at, std::map::at, and std::vector::at.
1 parent 7c5c8b2 commit c8fc234

File tree

9 files changed

+81
-39
lines changed

9 files changed

+81
-39
lines changed

bolt/include/bolt/Profile/BoltAddressTranslation.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -283,7 +283,9 @@ class BoltAddressTranslation {
283283

284284
/// Returns the number of basic blocks in a function.
285285
size_t getNumBasicBlocks(uint64_t OutputAddress) const {
286-
return NumBasicBlocksMap.at(OutputAddress);
286+
auto It = NumBasicBlocksMap.find(OutputAddress);
287+
assert(It != NumBasicBlocksMap.end());
288+
return It->second;
287289
}
288290

289291
private:

bolt/lib/Core/BinaryContext.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -934,10 +934,13 @@ std::string BinaryContext::generateJumpTableName(const BinaryFunction &BF,
934934
uint64_t Offset = 0;
935935
if (const JumpTable *JT = BF.getJumpTableContainingAddress(Address)) {
936936
Offset = Address - JT->getAddress();
937-
auto Itr = JT->Labels.find(Offset);
938-
if (Itr != JT->Labels.end())
939-
return std::string(Itr->second->getName());
940-
Id = JumpTableIds.at(JT->getAddress());
937+
auto JTLabelsIt = JT->Labels.find(Offset);
938+
if (JTLabelsIt != JT->Labels.end())
939+
return std::string(JTLabelsIt->second->getName());
940+
941+
auto JTIdsIt = JumpTableIds.find(JT->getAddress());
942+
assert(JTIdsIt != JumpTableIds.end());
943+
Id = JTIdsIt->second;
941944
} else {
942945
Id = JumpTableIds[Address] = BF.JumpTables.size();
943946
}

bolt/lib/Core/BinaryEmitter.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -813,7 +813,9 @@ void BinaryEmitter::emitJumpTable(const JumpTable &JT, MCSection *HotSection,
813813
// determining its destination.
814814
std::map<MCSymbol *, uint64_t> LabelCounts;
815815
if (opts::JumpTables > JTS_SPLIT && !JT.Counts.empty()) {
816-
MCSymbol *CurrentLabel = JT.Labels.at(0);
816+
auto It = JT.Labels.find(0);
817+
assert(It != JT.Labels.end());
818+
MCSymbol *CurrentLabel = It->second;
817819
uint64_t CurrentLabelCount = 0;
818820
for (unsigned Index = 0; Index < JT.Entries.size(); ++Index) {
819821
auto LI = JT.Labels.find(Index * JT.EntrySize);

bolt/lib/Core/DynoStats.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -114,8 +114,9 @@ void DynoStats::print(raw_ostream &OS, const DynoStats *Other,
114114
for (auto &Stat : llvm::reverse(SortedHistogram)) {
115115
OS << format("%20s,%'18lld", Printer->getOpcodeName(Stat.second).data(),
116116
Stat.first * opts::DynoStatsScale);
117-
118-
MaxOpcodeHistogramTy MaxMultiMap = OpcodeHistogram.at(Stat.second).second;
117+
auto It = OpcodeHistogram.find(Stat.second);
118+
assert(It != OpcodeHistogram.end());
119+
MaxOpcodeHistogramTy MaxMultiMap = It->second.second;
119120
// Start with function name:BB offset with highest execution count.
120121
for (auto &Max : llvm::reverse(MaxMultiMap)) {
121122
OS << format(", %'18lld, ", Max.first * opts::DynoStatsScale)

bolt/lib/Passes/BinaryFunctionCallGraph.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,9 @@ std::deque<BinaryFunction *> BinaryFunctionCallGraph::buildTraversalOrder() {
5656
std::stack<NodeId> Worklist;
5757

5858
for (BinaryFunction *Func : Funcs) {
59-
const NodeId Id = FuncToNodeId.at(Func);
59+
auto It = FuncToNodeId.find(Func);
60+
assert(It != FuncToNodeId.end());
61+
const NodeId Id = It->second;
6062
Worklist.push(Id);
6163
NodeStatus[Id] = NEW;
6264
}

bolt/lib/Passes/BinaryPasses.cpp

Lines changed: 22 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1563,23 +1563,28 @@ Error PrintProgramStats::runOnFunctions(BinaryContext &BC) {
15631563
const bool Ascending =
15641564
opts::DynoStatsSortOrderOpt == opts::DynoStatsSortOrder::Ascending;
15651565

1566-
if (SortAll) {
1567-
llvm::stable_sort(Functions,
1568-
[Ascending, &Stats](const BinaryFunction *A,
1569-
const BinaryFunction *B) {
1570-
return Ascending ? Stats.at(A) < Stats.at(B)
1571-
: Stats.at(B) < Stats.at(A);
1572-
});
1573-
} else {
1574-
llvm::stable_sort(
1575-
Functions, [Ascending, &Stats](const BinaryFunction *A,
1576-
const BinaryFunction *B) {
1577-
const DynoStats &StatsA = Stats.at(A);
1578-
const DynoStats &StatsB = Stats.at(B);
1579-
return Ascending ? StatsA.lessThan(StatsB, opts::PrintSortedBy)
1580-
: StatsB.lessThan(StatsA, opts::PrintSortedBy);
1581-
});
1582-
}
1566+
std::function<bool(const DynoStats &, const DynoStats &)>
1567+
DynoStatsComparator =
1568+
SortAll ? [](const DynoStats &StatsA,
1569+
const DynoStats &StatsB) { return StatsA < StatsB; }
1570+
: [](const DynoStats &StatsA, const DynoStats &StatsB) {
1571+
return StatsA.lessThan(StatsB, opts::PrintSortedBy);
1572+
};
1573+
1574+
llvm::stable_sort(Functions,
1575+
[Ascending, &Stats, DynoStatsComparator](
1576+
const BinaryFunction *A, const BinaryFunction *B) {
1577+
auto StatsItr = Stats.find(A);
1578+
assert(StatsItr != Stats.end());
1579+
const DynoStats &StatsA = StatsItr->second;
1580+
1581+
StatsItr = Stats.find(B);
1582+
assert(StatsItr != Stats.end());
1583+
const DynoStats &StatsB = StatsItr->second;
1584+
1585+
return Ascending ? DynoStatsComparator(StatsA, StatsB)
1586+
: DynoStatsComparator(StatsB, StatsA);
1587+
});
15831588

15841589
BC.outs() << "BOLT-INFO: top functions sorted by ";
15851590
if (SortAll) {

bolt/lib/Passes/CacheMetrics.cpp

Lines changed: 33 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,20 @@ calcTSPScore(const std::vector<BinaryFunction *> &BinaryFunctions,
6767
for (BinaryBasicBlock *DstBB : SrcBB->successors()) {
6868
if (SrcBB != DstBB && BI->Count != BinaryBasicBlock::COUNT_NO_PROFILE) {
6969
JumpCount += BI->Count;
70-
if (BBAddr.at(SrcBB) + BBSize.at(SrcBB) == BBAddr.at(DstBB))
70+
71+
auto BBAddrIt = BBAddr.find(SrcBB);
72+
assert(BBAddrIt != BBAddr.end());
73+
uint64_t SrcBBAddr = BBAddrIt->second;
74+
75+
auto BBSizeIt = BBSize.find(SrcBB);
76+
assert(BBSizeIt != BBSize.end());
77+
uint64_t SrcBBSize = BBSizeIt->second;
78+
79+
BBAddrIt = BBAddr.find(DstBB);
80+
assert(BBAddrIt != BBAddr.end());
81+
uint64_t DstBBAddr = BBAddrIt->second;
82+
83+
if (SrcBBAddr + SrcBBSize == DstBBAddr)
7184
Score += BI->Count;
7285
}
7386
++BI;
@@ -149,29 +162,39 @@ double expectedCacheHitRatio(
149162
for (BinaryFunction *BF : BinaryFunctions) {
150163
if (BF->getLayout().block_empty())
151164
continue;
152-
const uint64_t Page =
153-
BBAddr.at(BF->getLayout().block_front()) / ITLBPageSize;
154-
PageSamples[Page] += FunctionSamples.at(BF);
165+
auto BBAddrIt = BBAddr.find(BF->getLayout().block_front());
166+
assert(BBAddrIt != BBAddr.end());
167+
const uint64_t Page = BBAddrIt->second / ITLBPageSize;
168+
169+
auto FunctionSamplesIt = FunctionSamples.find(BF);
170+
assert(FunctionSamplesIt != FunctionSamples.end());
171+
PageSamples[Page] += FunctionSamplesIt->second;
155172
}
156173

157174
// Computing the expected number of misses for every function
158175
double Misses = 0;
159176
for (BinaryFunction *BF : BinaryFunctions) {
160177
// Skip the function if it has no samples
161-
if (BF->getLayout().block_empty() || FunctionSamples.at(BF) == 0.0)
178+
auto FunctionSamplesIt = FunctionSamples.find(BF);
179+
assert(FunctionSamplesIt != FunctionSamples.end());
180+
double Samples = FunctionSamplesIt->second;
181+
if (BF->getLayout().block_empty() || Samples == 0.0)
162182
continue;
163-
double Samples = FunctionSamples.at(BF);
164-
const uint64_t Page =
165-
BBAddr.at(BF->getLayout().block_front()) / ITLBPageSize;
183+
184+
auto BBAddrIt = BBAddr.find(BF->getLayout().block_front());
185+
assert(BBAddrIt != BBAddr.end());
186+
const uint64_t Page = BBAddrIt->second / ITLBPageSize;
166187
// The probability that the page is not present in the cache
167188
const double MissProb =
168189
pow(1.0 - PageSamples[Page] / TotalSamples, ITLBEntries);
169190

170191
// Processing all callers of the function
171192
for (std::pair<BinaryFunction *, uint64_t> Pair : Calls[BF]) {
172193
BinaryFunction *SrcFunction = Pair.first;
173-
const uint64_t SrcPage =
174-
BBAddr.at(SrcFunction->getLayout().block_front()) / ITLBPageSize;
194+
195+
BBAddrIt = BBAddr.find(SrcFunction->getLayout().block_front());
196+
assert(BBAddrIt != BBAddr.end());
197+
const uint64_t SrcPage = BBAddrIt->second / ITLBPageSize;
175198
// Is this a 'long' or a 'short' call?
176199
if (Page != SrcPage) {
177200
// This is a miss

bolt/lib/Passes/Inliner.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,9 @@ Inliner::inlineCall(BinaryBasicBlock &CallerBB,
355355
std::vector<BinaryBasicBlock *> Successors(BB.succ_size());
356356
llvm::transform(BB.successors(), Successors.begin(),
357357
[&InlinedBBMap](const BinaryBasicBlock *BB) {
358-
return InlinedBBMap.at(BB);
358+
auto It = InlinedBBMap.find(BB);
359+
assert(It != InlinedBBMap.end());
360+
return It->second;
359361
});
360362

361363
if (CallerFunction.hasValidProfile() && Callee.hasValidProfile())

bolt/lib/Profile/StaleProfileMatching.cpp

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -372,8 +372,10 @@ createFlowFunction(const BinaryFunction::BasicBlockOrderType &BlockOrder) {
372372

373373
// Create necessary metadata for the flow function
374374
for (FlowJump &Jump : Func.Jumps) {
375-
Func.Blocks.at(Jump.Source).SuccJumps.push_back(&Jump);
376-
Func.Blocks.at(Jump.Target).PredJumps.push_back(&Jump);
375+
assert(Jump.Source < Func.Blocks.size());
376+
Func.Blocks[Jump.Source].SuccJumps.push_back(&Jump);
377+
assert(Jump.Target < Func.Blocks.size());
378+
Func.Blocks[Jump.Target].PredJumps.push_back(&Jump);
377379
}
378380
return Func;
379381
}

0 commit comments

Comments
 (0)