Skip to content

Commit 4811668

Browse files
committed
[TSAR, Transform] Limit statements in pragma
1 parent f9155da commit 4811668

File tree

2 files changed

+16
-28
lines changed

2 files changed

+16
-28
lines changed

include/tsar/Support/DiagnosticKinds.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,4 +185,4 @@ def warn_loop_swapping_no_loop_id: Warning<"cannot find loop ID to perform swapp
185185

186186
def error_loop_swapping_lost_loop: Error<"cannot match ForStmt with its IR">;
187187
def error_loop_swapping_expect_compound: Error<"expected compound statement after pragma">;
188-
def error_loop_swapping_diff_scope: Error<"loops within a pragma must have the same scope">;
188+
def error_loop_swapping_redundant_stmt: Error<"pragma should only contain loops or other pragma">;

lib/Transform/Clang/LoopSwapping.cpp

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -54,19 +54,11 @@ using namespace tsar;
5454

5555
namespace {
5656

57-
struct LoopRangeInfo {
58-
Loop *LoopPtr;
59-
SourceRange Range;
60-
CompoundStmt *CompStmtPtr;
61-
LoopRangeInfo() : LoopPtr(nullptr), CompStmtPtr(nullptr) {}
62-
LoopRangeInfo(llvm::Loop *L, const SourceRange &R, CompoundStmt *S):
63-
LoopPtr(L), Range(R), CompStmtPtr(S) {}
64-
};
65-
6657
/// This provides access to function-level analysis results on server.
6758
using ClangLoopSwappingProvider =
6859
FunctionPassAAProvider<DIEstimateMemoryPass, DIDependencyAnalysisPass>;
6960
using DIAliasTraitVector = std::vector<const DIAliasTrait *>;
61+
using LoopRangeInfo = std::pair<Loop *, SourceRange>;
7062
using LoopRangeList = SmallVector<LoopRangeInfo, 2>;
7163
using PragmaInfoList = SmallVector<std::pair<Stmt *, LoopRangeList>, 2>;
7264

@@ -123,19 +115,22 @@ class LoopVisitor : public RecursiveASTVisitor<LoopVisitor> {
123115
diag::error_loop_swapping_expect_compound);
124116
return false;
125117
}
118+
if (mState == TraverseState::OUTERFOR && !dyn_cast<ForStmt>(S)) {
119+
toDiag(mSrcMgr.getDiagnostics(), S->getBeginLoc(),
120+
diag::error_loop_swapping_redundant_stmt);
121+
return false;
122+
}
126123
return RecursiveASTVisitor::TraverseStmt(S);
127124
}
128125

129126
bool TraverseCompoundStmt(CompoundStmt *S) {
130-
mCompStmtStack.push(S);
131127
if (mState == TraverseState::PRAGMA) {
132128
mState = TraverseState::OUTERFOR;
133129
auto Res = RecursiveASTVisitor::TraverseCompoundStmt(S);
134130
mState = TraverseState::NONE;
135131
return Res;
136132
}
137133
auto Res = RecursiveASTVisitor::TraverseCompoundStmt(S);
138-
mCompStmtStack.pop();
139134
return Res;
140135
}
141136

@@ -144,13 +139,7 @@ class LoopVisitor : public RecursiveASTVisitor<LoopVisitor> {
144139
auto Match = mLoopInfo.find<AST>(S);
145140
if (Match != mLoopInfo.end()) {
146141
auto &LRL = mPragmaLoopsInfo.back().second;
147-
if (!LRL.empty() && LRL.back().CompStmtPtr != mCompStmtStack.top()) {
148-
toDiag(mSrcMgr.getDiagnostics(), S->getBeginLoc(),
149-
diag::error_loop_swapping_diff_scope);
150-
return false;
151-
}
152-
LRL.push_back(LoopRangeInfo(Match->get<IR>(), S->getSourceRange(),
153-
mCompStmtStack.top()));
142+
LRL.push_back(std::make_pair(Match->get<IR>(), S->getSourceRange()));
154143
} else {
155144
toDiag(mSrcMgr.getDiagnostics(), S->getBeginLoc(),
156145
diag::error_loop_swapping_lost_loop);
@@ -178,8 +167,8 @@ class LoopVisitor : public RecursiveASTVisitor<LoopVisitor> {
178167
++It, ++N) {
179168
dbgs() << "\tPragma " << N << " (" << It->first <<"):\n";
180169
for (const auto &Info : It->second) {
181-
const auto LoopPtr = Info.LoopPtr;
182-
const auto &Range = Info.Range;
170+
const auto LoopPtr = Info.first;
171+
const auto &Range = Info.second;
183172
dbgs() << "\t\t[Range]\n";
184173
dbgs() << "\t\tBegin:" << Range.getBegin().printToString(mSrcMgr)
185174
<< "\n";
@@ -200,7 +189,6 @@ class LoopVisitor : public RecursiveASTVisitor<LoopVisitor> {
200189
const LoopMatcherPass::LoopMatcher &mLoopInfo;
201190
TraverseState mState;
202191
SmallVector<Stmt *, 1> mClauses;
203-
std::stack<CompoundStmt *> mCompStmtStack;
204192
PragmaInfoList mPragmaLoopsInfo;
205193
};
206194

@@ -327,15 +315,15 @@ bool ClangLoopSwapping::hasTrueOrAntiDependence(
327315

328316
bool ClangLoopSwapping::isSwappingAvailable(
329317
const LoopRangeList &LRL, const Stmt *Pragma) const {
330-
auto *LoopID0 = mGetLoopID(LRL[0].LoopPtr->getLoopID());
331-
auto *LoopID1 = mGetLoopID(LRL[1].LoopPtr->getLoopID());
318+
auto *LoopID0 = mGetLoopID(LRL[0].first->getLoopID());
319+
auto *LoopID1 = mGetLoopID(LRL[1].first->getLoopID());
332320
if (!LoopID0) {
333-
toDiag(mSrcMgr->getDiagnostics(), LRL[0].Range.getBegin(),
321+
toDiag(mSrcMgr->getDiagnostics(), LRL[0].second.getBegin(),
334322
diag::warn_loop_swapping_no_loop_id);
335323
return false;
336324
}
337325
if (!LoopID1) {
338-
toDiag(mSrcMgr->getDiagnostics(), LRL[1].Range.getBegin(),
326+
toDiag(mSrcMgr->getDiagnostics(), LRL[1].second.getBegin(),
339327
diag::warn_loop_swapping_no_loop_id);
340328
return false;
341329
}
@@ -376,8 +364,8 @@ void ClangLoopSwapping::swapLoops(const LoopVisitor &Visitor) {
376364
diag::warn_loop_swapping_redundant_loop);
377365
}
378366
if (isSwappingAvailable(Loops, Pragma)) {
379-
auto Range0 = Loops[0].Range;
380-
auto Range1 = Loops[1].Range;
367+
auto Range0 = Loops[0].second;
368+
auto Range1 = Loops[1].second;
381369
Range0.setEnd(GetLoopEnd(Range0));
382370
Range1.setEnd(GetLoopEnd(Range1));
383371
auto Range0End = Range0.getEnd();

0 commit comments

Comments
 (0)