1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
// This file contains implementation of Register Pressure Estimator.
#include "GraphColor.h"
#include "PointsToAnalysis.h"
#include "RPE.h"
#include "Timer.h"
namespace vISA {
RPE::RPE(const GlobalRA &g, const LivenessAnalysis *l, DECLARE_LIST *spills)
: gra(g), fg(g.kernel.fg), liveAnalysis(l), live(), vars(l->vars)
{
options = g.kernel.getOptions();
if (spills) {
std::for_each(spills->begin(), spills->end(),
[&](const G4_Declare *dcl) { spilledVars.insert(dcl); });
}
}
void RPE::run() {
TIME_SCOPE(RPE);
if (!vars.empty()) {
for (auto &bb : gra.kernel.fg) {
runBB(bb);
}
}
}
void RPE::runBB(G4_BB *bb) {
G4_Declare *topdcl = nullptr;
unsigned int id = 0;
// Compute reg pressure at BB exit
regPressureBBExit(bb);
auto updateLivenessForLLR = [this](LocalLiveRange *LLR, bool val) {
int numRows = LLR->getTopDcl()->getNumRows();
int sreg;
G4_VarBase *preg = LLR->getPhyReg(sreg);
int startGRF = preg->asGreg()->getRegNum();
for (int i = startGRF; i < startGRF + numRows; ++i) {
G4_Declare *GRFDcl = gra.getGRFDclForHRA(i);
updateLiveness(live, GRFDcl->getRegVar()->getId(), val);
}
};
// Iterate in bottom-up order to analyze register usage (similar to intf graph
// construction)
for (auto rInst = bb->rbegin(), rEnd = bb->rend(); rInst != rEnd; rInst++) {
auto inst = (*rInst);
auto dst = inst->getDst();
rp[inst] = (uint32_t)regPressure;
LocalLiveRange *LLR = nullptr;
if (dst && (topdcl = dst->getTopDcl())) {
if (topdcl->getRegVar()->isRegAllocPartaker()) {
// Check if dst is killed
if (liveAnalysis->writeWholeRegion(bb, inst, dst) ||
inst->isPseudoKill()) {
id = topdcl->getRegVar()->getId();
updateLiveness(live, id, false);
}
} else if ((LLR = gra.getLocalLR(topdcl)) && LLR->getAssigned()) {
uint32_t firstRefIdx;
if (LLR->getFirstRef(firstRefIdx) == inst ||
liveAnalysis->writeWholeRegion(bb, inst, dst)) {
updateLivenessForLLR(LLR, false);
}
}
}
for (unsigned int i = 0, numSrc = inst->getNumSrc(); i < numSrc; i++) {
auto src = inst->getSrc(i);
G4_RegVar *regVar = nullptr;
if (!src)
continue;
if (!src->isSrcRegRegion() || !src->getTopDcl())
continue;
if (!src->asSrcRegRegion()->isIndirect()) {
if ((regVar = src->getTopDcl()->getRegVar()) &&
regVar->isRegAllocPartaker()) {
unsigned int id = regVar->getId();
updateLiveness(live, id, true);
} else if ((LLR = gra.getLocalLR(src->getTopDcl())) &&
LLR->getAssigned()) {
updateLivenessForLLR(LLR, true);
}
} else if (src->asSrcRegRegion()->isIndirect()) {
// make every var in points-to set live
const REGVAR_VECTOR &pointsToSet =
liveAnalysis->getPointsToAnalysis().getAllInPointsToOrIndrUse(src,
bb);
for (const auto &pt : pointsToSet) {
if (pt.var->isRegAllocPartaker()) {
updateLiveness(live, pt.var->getId(), true);
}
}
}
}
}
}
void RPE::regPressureBBExit(G4_BB *bb) {
live.clear();
live = liveAnalysis->use_out[bb->getId()];
live &= liveAnalysis->def_out[bb->getId()];
// Iterate over all live variables and add up numRows required
// for each. For scalar variables, add them up separately.
regPressure = 0;
unsigned int numScalarBytes = 0;
for (auto LI = live.begin(), LE = live.end(); LI != LE; ++LI) {
unsigned i = *LI;
{
auto range = vars[i];
G4_Declare *rootDcl = range->getDeclare()->getRootDeclare();
if (isSpilled(rootDcl))
continue;
if (isStackPseudoVar(rootDcl))
continue;
if (rootDcl->getNumElems() > 1) {
regPressure += rootDcl->getNumRows();
} else {
auto dclSize = rootDcl->getByteSize();
auto alignBytes = static_cast<uint32_t>(rootDcl->getSubRegAlign()) * 2;
if (dclSize < gra.builder.getGRFSize() && dclSize < alignBytes) {
dclSize = std::min(dclSize * 2, alignBytes);
}
numScalarBytes += dclSize;
}
}
}
regPressure += (double)numScalarBytes / gra.builder.getGRFSize();
}
void RPE::updateLiveness(SparseBitVector &live, uint32_t id, bool val) {
bool change = false;
bool clean = false;
if (val) { //true
if (!live.test(id)) { //used to be false
change = true;
live.set(id);
}
} else {
if (live.test(id)) { //
change = true;
clean = true;
live.reset(id);
}
}
updateRegisterPressure(change, clean, id);
}
void RPE::updateRegisterPressure(bool change, bool clean,
unsigned int id) {
if (change) {
auto dcl = vars[id]->getDeclare();
if (isSpilled(dcl))
return;
if (isStackPseudoVar(dcl))
return;
// For <1 GRF variable we have to take alignment into consideration as well
// when computing register pressure. For now we double each <1GRF variable's
// size if its alignment also exceeds its size. Alternative is to simply
// take the alignment as the size, but it might cause performance
// regressions due to being too conservative (i.e., a GRF-aligned variable
// may share physical GRF with several other
auto dclSize = dcl->getByteSize();
auto alignBytes = static_cast<uint32_t>(dcl->getSubRegAlign()) * 2;
if (dclSize < gra.builder.getGRFSize() && dclSize < alignBytes) {
dclSize = std::min(dclSize * 2, alignBytes);
}
double delta = dclSize < gra.builder.getGRFSize()
? dclSize / (double)gra.builder.getGRFSize()
: (double)dcl->getNumRows();
if (clean) {
if (regPressure < delta) {
regPressure = 0;
} else {
regPressure -= delta;
}
} else {
regPressure += delta;
}
}
maxRP = std::max(maxRP, (uint32_t)regPressure);
}
void RPE::recomputeMaxRP() {
maxRP = 0;
// Find max register pressure over all entries in map
for (const auto &item : rp) {
maxRP = std::max(maxRP, item.second);
}
}
void RPE::dump() const {
std::cerr << "Max pressure = " << maxRP << "\n";
for (auto &bb : gra.kernel.fg) {
for (auto inst : *bb) {
std::cerr << "[";
if (rp.count(inst)) {
std::cerr << rp.at(inst);
} else {
std::cerr << "??";
}
std::cerr << "]";
inst->dump();
}
std::cerr << "\n";
}
}
} // namespace vISA
|