Skip to content

Commit 14647fd

Browse files
pratikasharigcbot
authored andcommitted
Minor fixes.
1 parent ca40f81 commit 14647fd

File tree

5 files changed

+289
-6
lines changed

5 files changed

+289
-6
lines changed

visa/LoopAnalysis.cpp

Lines changed: 148 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -426,8 +426,8 @@ bool Dominator::dominates(G4_BB* bb1, G4_BB* bb2)
426426
{
427427
recomputeIfStale();
428428

429-
auto& dom = getDom(bb1);
430-
if (dom.find(bb2) != dom.end())
429+
auto& dom = getDom(bb2);
430+
if (dom.find(bb1) != dom.end())
431431
return true;
432432

433433
return false;
@@ -663,6 +663,32 @@ std::vector<Loop*> vISA::LoopDetection::getTopLoops()
663663
return topLoops;
664664
}
665665

666+
Loop* Loop::getInnerMostLoop(const G4_BB* bb)
667+
{
668+
// if current loop contains bb, recurse loop tree and return
669+
// most nested loop containing it.
670+
// if current loop doesnt contain bb then return nullptr.
671+
if (!contains(bb))
672+
return nullptr;
673+
674+
for (auto& nested : immNested)
675+
if(auto innerMost = nested->getInnerMostLoop(bb))
676+
return innerMost;
677+
678+
return this;
679+
}
680+
681+
Loop* LoopDetection::getInnerMostLoop(const G4_BB* bb)
682+
{
683+
recomputeIfStale();
684+
685+
for (auto& loop : topLoops)
686+
if (auto innerMost = loop->getInnerMostLoop(bb))
687+
return innerMost;
688+
689+
return nullptr;
690+
}
691+
666692
void LoopDetection::reset()
667693
{
668694
allLoops.clear();
@@ -1016,7 +1042,126 @@ void Loop::dump(std::ostream& os)
10161042
}
10171043
}
10181044

1019-
bool vISA::Loop::contains(const G4_BB* bb)
1045+
bool Loop::contains(const G4_BB* bb)
10201046
{
10211047
return BBsLookup.find(bb) != BBsLookup.end();
10221048
}
1049+
1050+
bool VarReferences::isUniqueDef(G4_DstRegRegion* dst)
1051+
{
1052+
recomputeIfStale();
1053+
1054+
auto dcl = dst->getTopDcl();
1055+
1056+
// return true if spilled variable has a single static def
1057+
// and it is not live-in to current bb (eg, loop, sub).
1058+
if (getDefCount(dcl) != 1)
1059+
{
1060+
// check whether multiple defs exist in program for current
1061+
// lb, rb
1062+
auto lb = dst->getLeftBound();
1063+
auto rb = dst->getRightBound();
1064+
1065+
unsigned int count = 0;
1066+
const auto& defs = getDefs(dcl);
1067+
for (auto& def : *defs)
1068+
{
1069+
if (std::get<2>(def) <= rb &&
1070+
std::get<3>(def) >= lb)
1071+
++count;
1072+
}
1073+
1074+
if (count > 1)
1075+
return false;
1076+
}
1077+
1078+
return true;
1079+
}
1080+
1081+
unsigned int VarReferences::getDefCount(G4_Declare* dcl)
1082+
{
1083+
auto defs = getDefs(dcl);
1084+
if (defs)
1085+
return defs->size();
1086+
return 0;
1087+
}
1088+
1089+
const VarReferences::Defs* VarReferences::getDefs(G4_Declare* dcl)
1090+
{
1091+
recomputeIfStale();
1092+
1093+
auto it = VarRefs.find(dcl);
1094+
if (it != VarRefs.end())
1095+
return &(it->second.first);
1096+
1097+
return nullptr;
1098+
}
1099+
1100+
const VarReferences::Uses* VarReferences::getUses(G4_Declare* dcl)
1101+
{
1102+
recomputeIfStale();
1103+
1104+
auto it = VarRefs.find(dcl);
1105+
if (it != VarRefs.end())
1106+
return &(it->second.second);
1107+
1108+
return nullptr;
1109+
}
1110+
1111+
void VarReferences::reset()
1112+
{
1113+
VarRefs.clear();
1114+
1115+
setStale();
1116+
}
1117+
1118+
void VarReferences::run()
1119+
{
1120+
for (auto bb : kernel.fg)
1121+
{
1122+
for (auto inst : *bb)
1123+
{
1124+
if (inst->isPseudoKill())
1125+
continue;
1126+
1127+
auto dst = inst->getDst();
1128+
1129+
if (dst && !dst->isNullReg())
1130+
{
1131+
auto topdcl = dst->getTopDcl();
1132+
1133+
if (topdcl)
1134+
{
1135+
auto lb = dst->getLeftBound();
1136+
auto rb = dst->getRightBound();
1137+
auto& Defs = VarRefs[topdcl].first;
1138+
Defs.push_back(std::make_tuple(inst, bb, lb, rb));
1139+
}
1140+
}
1141+
1142+
for (unsigned int i = 0; i != inst->getNumSrc(); ++i)
1143+
{
1144+
auto src = inst->getSrc(i);
1145+
if (src && src->isSrcRegRegion())
1146+
{
1147+
auto topdcl = src->asSrcRegRegion()->getTopDcl();
1148+
if (topdcl)
1149+
{
1150+
auto& Uses = VarRefs[topdcl].second;
1151+
Uses.push_back(std::make_tuple(inst, bb));
1152+
}
1153+
}
1154+
}
1155+
}
1156+
}
1157+
1158+
setValid();
1159+
}
1160+
1161+
void VarReferences::dump(std::ostream& os)
1162+
{
1163+
if (isStale())
1164+
os << "Data is stale.\n";
1165+
1166+
os << "#Dcls with defs/uses: " << VarRefs.size();
1167+
}

visa/LoopAnalysis.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,36 @@ namespace vISA
100100
void dump(std::ostream& os = std::cerr) override { dumpImmDom(os); }
101101
};
102102

103+
// Computes and stores direct references of variables.
104+
// Indirects references are not computed here.
105+
// Doesnt include predicates/condition modifiers.
106+
class VarReferences : public Analysis
107+
{
108+
public:
109+
VarReferences(G4_Kernel& k) : kernel(k) {}
110+
111+
// Defs -> vector[tuple<inst, bb, lb, rb>]
112+
// Uses -> vector[tuple<inst, bb>]
113+
using Defs = std::vector<std::tuple<G4_INST*, G4_BB*, unsigned int, unsigned int>>;
114+
using Uses = std::vector<std::tuple<G4_INST*, G4_BB*>>;
115+
116+
bool isUniqueDef(G4_DstRegRegion* dst);
117+
unsigned int getDefCount(G4_Declare* dcl);
118+
const Defs* getDefs(G4_Declare* dcl);
119+
const Uses* getUses(G4_Declare* dcl);
120+
121+
private:
122+
// Dcl -> vector[<inst, bb, lb, rb>]
123+
// this data structure helps check whether a definition or part of it
124+
// has multiple definitions in the program.
125+
std::unordered_map<G4_Declare*, std::pair<Defs, Uses>> VarRefs;
126+
G4_Kernel& kernel;
127+
128+
void reset() override;
129+
void run() override;
130+
void dump(std::ostream& os = std::cerr) override;
131+
};
132+
103133
using BackEdge = std::pair<G4_BB*, G4_BB*>;
104134
using BackEdges = std::vector<BackEdge>;
105135

@@ -134,6 +164,8 @@ namespace vISA
134164
bool fullSubset(Loop* other);
135165
bool fullSuperset(Loop* other);
136166

167+
Loop* getInnerMostLoop(const G4_BB* bb);
168+
137169
private:
138170
std::vector<G4_BB*> BBs;
139171
std::unordered_set<const G4_BB*> BBsLookup;
@@ -148,6 +180,7 @@ namespace vISA
148180
LoopDetection(G4_Kernel&);
149181

150182
std::vector<Loop*> getTopLoops();
183+
Loop* getInnerMostLoop(const G4_BB*);
151184

152185
private:
153186
std::vector<Loop*> topLoops;

0 commit comments

Comments
 (0)