Skip to content

Commit 0daf303

Browse files
authoredJan 12, 2024
[BOLT] Fix double conversion in CacheMetrics (#75253)
The change (i) fixes an issue with double-int conversion in CacheMetrics and (ii) removes command-line options for computing metrics (which aren't modified anyway). This change might break some tests verifying the exact output of CacheMetrics.
1 parent 97a9dbb commit 0daf303

File tree

1 file changed

+15
-18
lines changed

1 file changed

+15
-18
lines changed
 

‎bolt/lib/Passes/CacheMetrics.cpp

+15-18
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,20 @@
1414
#include "bolt/Passes/CacheMetrics.h"
1515
#include "bolt/Core/BinaryBasicBlock.h"
1616
#include "bolt/Core/BinaryFunction.h"
17-
#include "llvm/Support/CommandLine.h"
1817
#include <unordered_map>
1918

2019
using namespace llvm;
2120
using namespace bolt;
2221

23-
namespace opts {
24-
25-
extern cl::OptionCategory BoltOptCategory;
26-
27-
extern cl::opt<unsigned> ITLBPageSize;
28-
extern cl::opt<unsigned> ITLBEntries;
29-
30-
} // namespace opts
31-
3222
namespace {
3323

24+
/// The following constants are used to estimate the number of i-TLB cache
25+
/// misses for a given code layout. Empirically the values result in high
26+
/// correlations between the estimations and the perf measurements.
27+
/// The constants do not affect the code layout algorithms.
28+
constexpr unsigned ITLBPageSize = 4096;
29+
constexpr unsigned ITLBEntries = 16;
30+
3431
/// Initialize and return a position map for binary basic blocks
3532
void extractBasicBlockInfo(
3633
const std::vector<BinaryFunction *> &BinaryFunctions,
@@ -133,9 +130,6 @@ double expectedCacheHitRatio(
133130
const std::vector<BinaryFunction *> &BinaryFunctions,
134131
const std::unordered_map<BinaryBasicBlock *, uint64_t> &BBAddr,
135132
const std::unordered_map<BinaryBasicBlock *, uint64_t> &BBSize) {
136-
137-
const double PageSize = opts::ITLBPageSize;
138-
const uint64_t CacheEntries = opts::ITLBEntries;
139133
std::unordered_map<const BinaryFunction *, Predecessors> Calls =
140134
extractFunctionCalls(BinaryFunctions);
141135
// Compute 'hotness' of the functions
@@ -155,7 +149,8 @@ double expectedCacheHitRatio(
155149
for (BinaryFunction *BF : BinaryFunctions) {
156150
if (BF->getLayout().block_empty())
157151
continue;
158-
double Page = BBAddr.at(BF->getLayout().block_front()) / PageSize;
152+
const uint64_t Page =
153+
BBAddr.at(BF->getLayout().block_front()) / ITLBPageSize;
159154
PageSamples[Page] += FunctionSamples.at(BF);
160155
}
161156

@@ -166,15 +161,17 @@ double expectedCacheHitRatio(
166161
if (BF->getLayout().block_empty() || FunctionSamples.at(BF) == 0.0)
167162
continue;
168163
double Samples = FunctionSamples.at(BF);
169-
double Page = BBAddr.at(BF->getLayout().block_front()) / PageSize;
164+
const uint64_t Page =
165+
BBAddr.at(BF->getLayout().block_front()) / ITLBPageSize;
170166
// The probability that the page is not present in the cache
171-
double MissProb = pow(1.0 - PageSamples[Page] / TotalSamples, CacheEntries);
167+
const double MissProb =
168+
pow(1.0 - PageSamples[Page] / TotalSamples, ITLBEntries);
172169

173170
// Processing all callers of the function
174171
for (std::pair<BinaryFunction *, uint64_t> Pair : Calls[BF]) {
175172
BinaryFunction *SrcFunction = Pair.first;
176-
double SrcPage =
177-
BBAddr.at(SrcFunction->getLayout().block_front()) / PageSize;
173+
const uint64_t SrcPage =
174+
BBAddr.at(SrcFunction->getLayout().block_front()) / ITLBPageSize;
178175
// Is this a 'long' or a 'short' call?
179176
if (Page != SrcPage) {
180177
// This is a miss

0 commit comments

Comments
 (0)
Please sign in to comment.