Skip to content

Commit bb62e19

Browse files
committed
ColdBlockInfo: overhaul the analysis
1 parent a57e246 commit bb62e19

File tree

5 files changed

+194
-236
lines changed

5 files changed

+194
-236
lines changed

include/swift/SILOptimizer/Analysis/ColdBlockInfo.h

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
#include "llvm/ADT/DenseMap.h"
1717
#include "swift/SIL/SILValue.h"
18+
#include <bitset>
1819

1920
namespace swift {
2021
class DominanceAnalysis;
@@ -29,47 +30,45 @@ class ColdBlockInfo {
2930
DominanceAnalysis *DA;
3031
PostDominanceAnalysis *PDA;
3132

32-
/// Each block in this map has been determined to be either cold or hot.
33-
llvm::DenseMap<const SILBasicBlock*, bool> ColdBlockMap;
34-
35-
// This is a cache and shouldn't be copied around.
36-
ColdBlockInfo(const ColdBlockInfo &) = delete;
37-
ColdBlockInfo &operator=(const ColdBlockInfo &) = delete;
38-
39-
/// Tri-value return code for checking branch hints.
40-
enum BranchHint : unsigned {
41-
None,
42-
LikelyTrue,
43-
LikelyFalse
33+
// Represents the temperatures of edges flowing into a block.
34+
//
35+
// T = "top" -- both warm and cold edges
36+
// / \
37+
// Warm Cold
38+
// \ /
39+
// B = "bottom" -- neither warm or cold edges
40+
struct State {
41+
// Each state kind, as an integer, is its position in any bit vectors.
42+
enum Temperature {
43+
Warm = 0,
44+
Cold = 1
45+
};
46+
47+
// Number of states, excluding Top or Bottom, in this flow problem.
48+
static constexpr unsigned NumStates = 2;
4449
};
4550

46-
enum {
47-
RecursionDepthLimit = 3
48-
};
51+
using Energy = std::bitset<State::NumStates>;
52+
static std::string toString(Energy e);
4953

50-
// \returns none if there is no known temperature for the block.
51-
std::optional<bool> queryIsCold(const SILBasicBlock *BB);
54+
/// Each block in this map has been determined to be cold and/or warm.
55+
llvm::DenseMap<const SILBasicBlock*, Energy> EnergyMap;
56+
bool changedMap = false;
5257

53-
BranchHint getBranchHint(SILValue Cond, int recursionDepth);
54-
55-
bool isSlowPath(const SILBasicBlock *FromBB, const SILBasicBlock *ToBB,
56-
int recursionDepth);
58+
// This is a cache and shouldn't be copied around.
59+
ColdBlockInfo(const ColdBlockInfo &) = delete;
60+
ColdBlockInfo &operator=(const ColdBlockInfo &) = delete;
61+
LLVM_DUMP_METHOD void dump() const;
5762

58-
bool isCold(const SILBasicBlock *BB,
59-
int recursionDepth);
63+
void resetToCold(const SILBasicBlock *BB);
64+
void set(const SILBasicBlock *BB, State::Temperature temp);
6065

6166
public:
6267
ColdBlockInfo(DominanceAnalysis *DA, PostDominanceAnalysis *PDA);
6368

64-
/// Pre-seed the info with information starting at the exits of the function.
65-
/// Recommended prior to querying if blocks are cold in a function.
66-
void analyzeExits(SILFunction *Fn);
67-
68-
bool isSlowPath(const SILBasicBlock *FromBB, const SILBasicBlock *ToBB) {
69-
return isSlowPath(FromBB, ToBB, 0);
70-
}
69+
void analyze(SILFunction *F);
7170

72-
bool isCold(const SILBasicBlock *BB) { return isCold(BB, 0); }
71+
bool isCold(const SILBasicBlock *BB) const;
7372
};
7473
} // end namespace swift
7574

include/swift/SILOptimizer/Utils/PerformanceInlinerUtils.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ class ShortestPathAnalysis {
429429
return;
430430

431431
BlockInfoStorage.resize(numBlocks);
432-
CBI.analyzeExits(F);
432+
CBI.analyze(F);
433433

434434
// First step: compute the length of the blocks.
435435
unsigned BlockIdx = 0;

0 commit comments

Comments
 (0)