Skip to content

Commit cdc9378

Browse files
committed
[LoopInterchange] Add normalize function from DependenceInfo
The motivation is to introduce the custom functions for LoopInterchange.
1 parent 4924203 commit cdc9378

File tree

1 file changed

+66
-3
lines changed

1 file changed

+66
-3
lines changed

llvm/lib/Transforms/Scalar/LoopInterchange.cpp

Lines changed: 66 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,65 @@ static void printDepMatrix(CharMatrix &DepMatrix) {
8282
}
8383
#endif
8484

85+
static bool isDirectionNegative(std::vector<Dependence::DVEntry> &DV,
86+
unsigned Levels) {
87+
for (unsigned Level = 1; Level <= Levels; ++Level) {
88+
unsigned char Direction = DV[Level - 1].Direction;
89+
if (Direction == Dependence::DVEntry::EQ)
90+
continue;
91+
if (Direction == Dependence::DVEntry::GT ||
92+
Direction == Dependence::DVEntry::GE)
93+
return true;
94+
return false;
95+
}
96+
return false;
97+
}
98+
99+
static void dumpDirection(raw_ostream &OS, std::vector<Dependence::DVEntry> &DV,
100+
unsigned Levels) {
101+
OS << " [";
102+
for (unsigned II = 1; II <= Levels; ++II) {
103+
unsigned Direction = DV[II - 1].Direction;
104+
if (Direction == Dependence::DVEntry::ALL)
105+
OS << "*";
106+
else {
107+
if (Direction & Dependence::DVEntry::LT)
108+
OS << "<";
109+
if (Direction & Dependence::DVEntry::EQ)
110+
OS << "=";
111+
if (Direction & Dependence::DVEntry::GT)
112+
OS << ">";
113+
}
114+
if (II < Levels)
115+
OS << " ";
116+
}
117+
OS << "]\n";
118+
}
119+
120+
static bool normalize(std::vector<Dependence::DVEntry> &DV, unsigned Levels,
121+
ScalarEvolution *SE) {
122+
if (!isDirectionNegative(DV, Levels))
123+
return false;
124+
125+
LLVM_DEBUG(dbgs() << "Before normalizing negative direction vectors:\n";
126+
dumpDirection(dbgs(), DV, Levels););
127+
for (unsigned Level = 1; Level <= Levels; ++Level) {
128+
unsigned char Direction = DV[Level - 1].Direction;
129+
// Reverse the direction vector, this means LT becomes GT
130+
// and GT becomes LT.
131+
unsigned char RevDirection = Direction & Dependence::DVEntry::EQ;
132+
if (Direction & Dependence::DVEntry::LT)
133+
RevDirection |= Dependence::DVEntry::GT;
134+
if (Direction & Dependence::DVEntry::GT)
135+
RevDirection |= Dependence::DVEntry::LT;
136+
DV[Level - 1].Direction = RevDirection;
137+
}
138+
139+
LLVM_DEBUG(dbgs() << "After normalizing negative direction vectors:\n";
140+
dumpDirection(dbgs(), DV, Levels););
141+
return true;
142+
}
143+
85144
static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
86145
Loop *L, DependenceInfo *DI,
87146
ScalarEvolution *SE) {
@@ -123,23 +182,27 @@ static bool populateDependencyMatrix(CharMatrix &DepMatrix, unsigned Level,
123182
// Track Output, Flow, and Anti dependencies.
124183
if (auto D = DI->depends(Src, Dst, true)) {
125184
assert(D->isOrdered() && "Expected an output, flow or anti dep.");
185+
186+
unsigned Levels = D->getLevels();
187+
std::vector<Dependence::DVEntry> DV(Levels);
188+
for (unsigned II = 1; II <= Levels; ++II)
189+
DV[II - 1].Direction = D->getDirection(II);
126190
// If the direction vector is negative, normalize it to
127191
// make it non-negative.
128-
if (D->normalize(SE))
192+
if (normalize(DV, Levels, SE))
129193
LLVM_DEBUG(dbgs() << "Negative dependence vector normalized.\n");
130194
LLVM_DEBUG(StringRef DepType =
131195
D->isFlow() ? "flow" : D->isAnti() ? "anti" : "output";
132196
dbgs() << "Found " << DepType
133197
<< " dependency between Src and Dst\n"
134198
<< " Src:" << *Src << "\n Dst:" << *Dst << '\n');
135-
unsigned Levels = D->getLevels();
136199
char Direction;
137200
for (unsigned II = 1; II <= Levels; ++II) {
138201
if (D->isScalar(II)) {
139202
Direction = 'S';
140203
Dep.push_back(Direction);
141204
} else {
142-
unsigned Dir = D->getDirection(II);
205+
unsigned Dir = DV[II - 1].Direction;
143206
if (Dir == Dependence::DVEntry::LT ||
144207
Dir == Dependence::DVEntry::LE)
145208
Direction = '<';

0 commit comments

Comments
 (0)