Skip to content
This repository was archived by the owner on Feb 5, 2019. It is now read-only.

Commit 8025a39

Browse files
[PBQP] Tweak spill costs and coalescing benefits
This patch improves how the different costs (register, interference, spill and coalescing) relates together. The assumption is now that: - coalescing (or any other "side effect" of reg alloc) is negative, and instead of being derived from a spill cost, they use the block frequency info. - spill costs are in the [MinSpillCost:+inf( range - register or interference costs are in [0.0:MinSpillCost( or +inf The current MinSpillCost is set to 10.0, which is a random value high enough that the current constraint builders do not need to worry about when settings costs. It would however be worth adding a normalization step for register and interference costs as the last step in the constraint builder chain to ensure they are not greater than SpillMinCost (unless this has some sense for some architectures). This would work well with the current builder pipeline, where all costs are tweaked relatively to each others, but could grow above MinSpillCost if the pipeline is deep enough. The current heuristic is tuned to depend rather on the number of uses of a live interval rather than a density of uses, as used by the greedy allocator. This heuristic provides a few percent improvement on a number of benchmarks (eembc, spec, ...) and will definitely need to change once spill placement is implemented: the current spill placement is really ineficient, so making the cost proportionnal to the number of use is a clear win. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@221292 91177308-0d34-0410-b5e6-96231b3b80d8
1 parent e246328 commit 8025a39

File tree

4 files changed

+29
-12
lines changed

4 files changed

+29
-12
lines changed

include/llvm/CodeGen/CalcSpillWeights.h

+4-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,10 @@ namespace llvm {
3030
/// @param UseDefFreq Expected number of executed use and def instructions
3131
/// per function call. Derived from block frequencies.
3232
/// @param Size Size of live interval as returnexd by getSize()
33+
/// @param NumInstr Number of instructions using this live interval
3334
///
34-
static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size) {
35+
static inline float normalizeSpillWeight(float UseDefFreq, unsigned Size,
36+
unsigned NumInstr) {
3537
// The constant 25 instructions is added to avoid depending too much on
3638
// accidental SlotIndex gaps for small intervals. The effect is that small
3739
// intervals have a spill weight that is mostly proportional to the number
@@ -44,7 +46,7 @@ namespace llvm {
4446
/// spill weight and allocation hint.
4547
class VirtRegAuxInfo {
4648
public:
47-
typedef float (*NormalizingFn)(float, unsigned);
49+
typedef float (*NormalizingFn)(float, unsigned, unsigned);
4850

4951
private:
5052
MachineFunction &MF;

lib/CodeGen/CalcSpillWeights.cpp

+3-1
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
100100
MachineLoop *loop = nullptr;
101101
bool isExiting = false;
102102
float totalWeight = 0;
103+
unsigned numInstr = 0; // Number of instructions using li
103104
SmallPtrSet<MachineInstr*, 8> visited;
104105

105106
// Find the best physreg hint and the best virtreg hint.
@@ -116,6 +117,7 @@ VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
116117
I = mri.reg_instr_begin(li.reg), E = mri.reg_instr_end();
117118
I != E; ) {
118119
MachineInstr *mi = &*(I++);
120+
numInstr++;
119121
if (mi->isIdentityCopy() || mi->isImplicitDef() || mi->isDebugValue())
120122
continue;
121123
if (!visited.insert(mi))
@@ -189,5 +191,5 @@ VirtRegAuxInfo::calculateSpillWeightAndHint(LiveInterval &li) {
189191
if (isRematerializable(li, LIS, *MF.getSubtarget().getInstrInfo()))
190192
totalWeight *= 0.5F;
191193

192-
li.weight = normalize(totalWeight, li.getSize());
194+
li.weight = normalize(totalWeight, li.getSize(), numInstr);
193195
}

lib/CodeGen/RegAllocGreedy.cpp

+5-3
Original file line numberDiff line numberDiff line change
@@ -1789,9 +1789,11 @@ unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order,
17891789
// instructions.
17901790
//
17911791
// Try to guess the size of the new interval.
1792-
const float EstWeight = normalizeSpillWeight(blockFreq * (NewGaps + 1),
1793-
Uses[SplitBefore].distance(Uses[SplitAfter]) +
1794-
(LiveBefore + LiveAfter)*SlotIndex::InstrDist);
1792+
const float EstWeight = normalizeSpillWeight(
1793+
blockFreq * (NewGaps + 1),
1794+
Uses[SplitBefore].distance(Uses[SplitAfter]) +
1795+
(LiveBefore + LiveAfter) * SlotIndex::InstrDist,
1796+
1);
17951797
// Would this split be possible to allocate?
17961798
// Never allocate all gaps, we wouldn't be making progress.
17971799
DEBUG(dbgs() << " w=" << EstWeight);

lib/CodeGen/RegAllocPBQP.cpp

+17-6
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,17 @@ class SpillCosts : public PBQPRAConstraint {
150150
void apply(PBQPRAGraph &G) override {
151151
LiveIntervals &LIS = G.getMetadata().LIS;
152152

153+
// A minimum spill costs, so that register constraints can can be set
154+
// without normalization in the [0.0:MinSpillCost( interval.
155+
const PBQP::PBQPNum MinSpillCost = 10.0;
156+
153157
for (auto NId : G.nodeIds()) {
154158
PBQP::PBQPNum SpillCost =
155159
LIS.getInterval(G.getNodeMetadata(NId).getVReg()).weight;
156160
if (SpillCost == 0.0)
157161
SpillCost = std::numeric_limits<PBQP::PBQPNum>::min();
162+
else
163+
SpillCost += MinSpillCost;
158164
PBQPRAGraph::RawVector NodeCosts(G.getNodeCosts(NId));
159165
NodeCosts[PBQP::RegAlloc::getSpillOptionIdx()] = SpillCost;
160166
G.setNodeCosts(NId, std::move(NodeCosts));
@@ -350,11 +356,8 @@ class Coalescing : public PBQPRAConstraint {
350356
unsigned DstReg = CP.getDstReg();
351357
unsigned SrcReg = CP.getSrcReg();
352358

353-
const float CopyFactor = 0.5; // Cost of copy relative to load. Current
354-
// value plucked randomly out of the air.
355-
356-
PBQP::PBQPNum CBenefit =
357-
CopyFactor * LiveIntervals::getSpillWeight(false, true, &MBFI, &MI);
359+
const float Scale = 1.0f / MBFI.getEntryFreq();
360+
PBQP::PBQPNum CBenefit = MBFI.getBlockFreq(&MBB).getFrequency() * Scale;
358361

359362
if (CP.isPhys()) {
360363
if (!MF.getRegInfo().isAllocatable(DstReg))
@@ -607,12 +610,20 @@ void RegAllocPBQP::finalizeAlloc(MachineFunction &MF,
607610
}
608611
}
609612

613+
static inline float normalizePBQPSpillWeight(float UseDefFreq, unsigned Size,
614+
unsigned NumInstr) {
615+
// All intervals have a spill weight that is mostly proportional to the number
616+
// of uses, with uses in loops having a bigger weight.
617+
return NumInstr * normalizeSpillWeight(UseDefFreq, Size, 1);
618+
}
619+
610620
bool RegAllocPBQP::runOnMachineFunction(MachineFunction &MF) {
611621
LiveIntervals &LIS = getAnalysis<LiveIntervals>();
612622
MachineBlockFrequencyInfo &MBFI =
613623
getAnalysis<MachineBlockFrequencyInfo>();
614624

615-
calculateSpillWeightsAndHints(LIS, MF, getAnalysis<MachineLoopInfo>(), MBFI);
625+
calculateSpillWeightsAndHints(LIS, MF, getAnalysis<MachineLoopInfo>(), MBFI,
626+
normalizePBQPSpillWeight);
616627

617628
VirtRegMap &VRM = getAnalysis<VirtRegMap>();
618629

0 commit comments

Comments
 (0)