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
|
/*========================== begin_copyright_notice ============================
Copyright (C) 2017-2021 Intel Corporation
SPDX-License-Identifier: MIT
============================= end_copyright_notice ===========================*/
#include "GenXPressureTracker.h"
#include "FunctionGroup.h"
#include "GenX.h"
#include "GenXBaling.h"
#include "GenXLiveness.h"
#include "GenXUtil.h"
#include "vc/Utils/GenX/RegCategory.h"
#include "vc/Utils/General/Types.h"
#include "Probe/Assertion.h"
using namespace llvm;
using namespace genx;
namespace {
struct LiveRangeAndLength {
LiveRange *LR;
unsigned Length;
LiveRangeAndLength(LiveRange *LR, unsigned Length) : LR(LR), Length(Length) {}
bool operator<(const LiveRangeAndLength &Rhs) const {
return Length > Rhs.Length;
}
};
} // namespace
unsigned PressureTracker::getSizeInBytes(LiveRange *LR, bool AllowWidening) {
SimpleValue SV = *LR->value_begin();
Value *V = SV.getValue();
Type *Ty = IndexFlattener::getElementType(V->getType(), SV.getIndex());
unsigned Bytes = vc::getTypeSize(Ty, &DL).inWordsCeil() * WordBytes;
if (!AllowWidening)
return Bytes;
// Check if this will be a live range to be promoted to a word vector:
// - this is a byte vector
// - non-of values will be used in indirect regions
// - all uses are in the same block (local variables only)
//
auto toWiden = [=]() -> bool {
if (!Ty->isVectorTy() ||
!cast<VectorType>(Ty)->getElementType()->isIntegerTy(8))
return false;
BasicBlock *DefBB = nullptr;
for (auto I = LR->value_begin(), E = LR->value_end(); I != E; ++I) {
auto Inst = dyn_cast<Instruction>((*I).getValue());
if (!Inst)
return false;
if (!DefBB)
DefBB = Inst->getParent();
if (DefBB != Inst->getParent() || Inst->isUsedOutsideOfBlock(DefBB))
return false;
for (auto UI : Inst->users()) {
if (GenXIntrinsic::isRdRegion(UI) || GenXIntrinsic::isWrRegion(UI)) {
Region R = makeRegionFromBaleInfo(cast<Instruction>(UI), BaleInfo());
if (R.Indirect)
return false;
}
}
}
// OK, this is a candidate for widening.
return true;
};
if (toWiden()) {
WidenCandidates.push_back(LR);
Bytes *= 2;
}
return Bytes;
}
// Decrease pressure assuming no widening on variable for LR.
void PressureTracker::decreasePressure(LiveRange *LR) {
if (!LR || LR->getCategory() != vc::RegCategory::General)
return;
#if _DEBUG
auto I = std::find(WidenCandidates.begin(), WidenCandidates.end(), LR);
IGC_ASSERT(I != WidenCandidates.end());
#endif
unsigned Bytes = getSizeInBytes(LR, /*AllowWidening*/ false);
for (auto SI = LR->begin(), SE = LR->end(); SI != SE; ++SI) {
for (unsigned i = SI->getStart(); i != SI->getEnd(); ++i) {
IGC_ASSERT(i < Pressure.size());
IGC_ASSERT(Pressure[i] >= Bytes);
Pressure[i] -= Bytes;
}
}
calculateRedSegments();
}
void PressureTracker::calculate() {
std::vector<LiveRange *> LRs;
getLiveRanges(LRs);
std::vector<LiveRangeAndLength> LRLs;
for (auto LR : LRs)
LRLs.emplace_back(LR, LR->getLength(/*WithWeak*/ false));
LRs.clear();
std::sort(LRLs.begin(), LRLs.end());
// Keep count of the rp at each instruction number.
Pressure.clear();
for (auto &I : LRLs) {
LiveRange *LR = I.LR;
unsigned Bytes = getSizeInBytes(LR, WithByteWidening);
for (auto SI = LR->begin(), SE = LR->end(); SI != SE; ++SI) {
if (SI->getEnd() >= Pressure.size())
Pressure.resize(SI->getEnd() + 1, 0);
for (unsigned i = SI->getStart(); i != SI->getEnd(); ++i)
Pressure[i] += Bytes;
}
}
}
// Calculate high pressure segments.
void PressureTracker::calculateRedSegments() {
HighPressureSegments.clear();
unsigned UNDEF = std::numeric_limits<unsigned>::max();
unsigned B = UNDEF;
unsigned E = UNDEF;
for (unsigned i = 0; i < Pressure.size(); ++i) {
if (Pressure[i] >= THRESHOLD) {
if (B == UNDEF)
B = i;
else
E = i;
} else {
if (B != UNDEF && E != UNDEF)
HighPressureSegments.emplace_back(B, E);
else if (B != UNDEF)
HighPressureSegments.emplace_back(B, B);
B = E = UNDEF;
}
}
}
// Check if segment [B, E] intersects with a high pressure region or not.
bool PressureTracker::intersectWithRedRegion(unsigned B, unsigned E) const {
for (auto S : HighPressureSegments) {
unsigned B1 = S.Begin;
unsigned E1 = S.End;
if (B > E1)
continue;
return E >= B1;
}
return false;
}
bool PressureTracker::intersectWithRedRegion(LiveRange *LR) const {
if (!LR || LR->getCategory() == vc::RegCategory::None)
return false;
for (auto I = LR->begin(), E = LR->end(); I != E; ++I)
if (intersectWithRedRegion(I->getStart(), I->getEnd()))
return true;
return false;
}
void PressureTracker::getLiveRanges(std::vector<LiveRange *> &LRs) {
for (auto I = FG.begin(), E = FG.end(); I != E; ++I) {
Function *F = *I;
for (auto &Arg : F->args())
getLiveRangesForValue(&Arg, LRs);
if (I != FG.begin() && !F->getReturnType()->isVoidTy())
getLiveRangesForValue(Liveness->getUnifiedRet(F), LRs);
for (auto &BB : F->getBasicBlockList())
for (auto &Inst : BB.getInstList())
getLiveRangesForValue(&Inst, LRs);
}
}
void PressureTracker::getLiveRangesForValue(
Value *V, std::vector<LiveRange *> &LRs) const {
auto Ty = V->getType();
for (unsigned i = 0, e = IndexFlattener::getNumElements(Ty); i != e; ++i) {
SimpleValue SV(V, i);
LiveRange *LR = Liveness->getLiveRangeOrNull(SV);
if (!LR || LR->getCategory() == vc::RegCategory::None)
continue;
// Only process an LR if the map iterator is on the value that appears
// first in the LR. That avoids processing the same LR multiple times.
if (SV != *LR->value_begin())
continue;
LRs.push_back(LR);
}
}
|