Skip to content

Commit 76bb7da

Browse files
committed
[TSAR, Parallel, DVMH] Add the 'on' clause to the 'parallel' directive if necessary.
1 parent b1f45ca commit 76bb7da

File tree

2 files changed

+44
-21
lines changed

2 files changed

+44
-21
lines changed

lib/Transform/Clang/DVMHSMAutoPar.cpp

Lines changed: 41 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -110,21 +110,43 @@ void addVarList(const ClangDependenceAnalyzer::ReductionVarListT &VarInfoList,
110110
}
111111
}
112112

113-
unsigned getPerfectNestSize(const DFLoop &DFL,
114-
const PerfectLoopInfo &PerfectInfo,
115-
const CanonicalLoopSet &CanonicalLoops) {
113+
void addOnClause(const DFLoop &DFL, const PerfectLoopInfo &PerfectInfo,
114+
const CanonicalLoopSet &CanonicalLoops,
115+
const MemoryMatchInfo &MemoryMatcher,
116+
SmallVectorImpl<char> &Out) {
117+
Out.append({' ', 'o', 'n', '('});
116118
auto *CurrDFL = &DFL;
117-
unsigned PerfectSize = 1;
118-
for (; PerfectInfo.count(CurrDFL) && CurrDFL->getNumRegions() > 0;
119-
++PerfectSize) {
119+
auto CanonicalItr = CanonicalLoops.find_as(const_cast<DFLoop *>(CurrDFL));
120+
assert(CanonicalItr != CanonicalLoops.end() &&
121+
(**CanonicalItr).isCanonical() &&
122+
"Parallel loop must have canonical loop form!");
123+
auto *Induction = (**CanonicalItr).getInduction();
124+
assert(Induction && "Induction variable must not be null in canonical loop!");
125+
auto MatchItr = MemoryMatcher.Matcher.find<IR>(Induction);
126+
assert(MatchItr != MemoryMatcher.Matcher.end() &&
127+
"AST-level variable representation must be available!");
128+
Out.append({'['});
129+
Out.append(MatchItr->get<AST>()->getName().begin(),
130+
MatchItr->get<AST>()->getName().end());
131+
Out.append({']'});
132+
for (; PerfectInfo.count(CurrDFL) && CurrDFL->getNumRegions() > 0;) {
120133
CurrDFL = dyn_cast<DFLoop>(*CurrDFL->region_begin());
121134
if (!CurrDFL)
122-
return PerfectSize;
135+
break;
123136
auto CanonicalItr = CanonicalLoops.find_as(const_cast<DFLoop *>(CurrDFL));
124137
if (CanonicalItr == CanonicalLoops.end() || !(**CanonicalItr).isCanonical())
125-
return PerfectSize;
138+
break;
139+
auto *Induction = (**CanonicalItr).getInduction();
140+
assert(Induction && "Induction variable must not be null in canonical loop!");
141+
auto MatchItr = MemoryMatcher.Matcher.find<IR>(Induction);
142+
assert(MatchItr != MemoryMatcher.Matcher.end() &&
143+
"AST-level variable representation must be available!");
144+
Out.append({'['});
145+
Out.append(MatchItr->get<AST>()->getName().begin(),
146+
MatchItr->get<AST>()->getName().end());
147+
Out.append({']'});
126148
}
127-
return PerfectSize;
149+
Out.append({')'});
128150
}
129151
} // namespace
130152

@@ -166,13 +188,13 @@ bool ClangDVMHSMParallelization::exploitParallelism(
166188
}
167189
auto &PerfectInfo = Provider.get<ClangPerfectLoopPass>().getPerfectLoopInfo();
168190
auto &CanonicalInfo = Provider.get<CanonicalLoopPass>().getCanonicalLoopInfo();
169-
SmallString<128> ParallelFor("#pragma dvm parallel (");
191+
auto &MemoryMatcher = Provider.get<MemoryMatcherImmutableWrapper>().get();
192+
193+
SmallString<128> ParallelFor("#pragma dvm parallel");
170194
if (HostOnly)
171-
ParallelFor += "1";
195+
ParallelFor += "(1)";
172196
else
173-
Twine(getPerfectNestSize(IR, PerfectInfo, CanonicalInfo))
174-
.toStringRef(ParallelFor);
175-
ParallelFor += ")";
197+
addOnClause(IR, PerfectInfo, CanonicalInfo, MemoryMatcher, ParallelFor);
176198
if (!ASTDepInfo.get<trait::Private>().empty()) {
177199
ParallelFor += " private";
178200
addVarList(ASTDepInfo.get<trait::Private>(), ParallelFor);
@@ -182,16 +204,16 @@ bool ClangDVMHSMParallelization::exploitParallelism(
182204
DVMHRegion += "\n{\n";
183205
// Add directives to the source code.
184206
auto &Rewriter = TfmCtx.getRewriter();
185-
Rewriter.InsertTextBefore(AST.getBeginLoc(), ParallelFor);
186-
Rewriter.InsertTextBefore(AST.getBeginLoc(), DVMHRegion);
207+
Rewriter.InsertTextBefore(AST.getBeginLoc(), ParallelFor);
208+
Rewriter.InsertTextBefore(AST.getBeginLoc(), DVMHRegion);
187209
if (!DVMHActual.empty())
188-
Rewriter.InsertTextBefore(AST.getBeginLoc(), DVMHActual);
210+
Rewriter.InsertTextBefore(AST.getBeginLoc(), DVMHActual);
189211
auto &ASTCtx = TfmCtx.getContext();
190212
Token SemiTok;
191-
auto InsertLoc = (!getRawTokenAfter(AST.getEndLoc(),
213+
auto InsertLoc = (!getRawTokenAfter(AST.getEndLoc(),
192214
ASTCtx.getSourceManager(), ASTCtx.getLangOpts(), SemiTok)
193215
&& SemiTok.is(tok::semi))
194-
? SemiTok.getLocation() : AST.getEndLoc();
216+
? SemiTok.getLocation() : AST.getEndLoc();
195217
Rewriter.InsertTextAfterToken(InsertLoc, "}");
196218
if (!DVMHGetActual.empty()) {
197219
Rewriter.InsertTextAfterToken(InsertLoc, "\n");

lib/Transform/Clang/SharedMemoryAutoPar.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,14 @@
2828

2929
#include "tsar/ADT/DenseMapTraits.h"
3030
#include "tsar/Analysis/AnalysisSocket.h"
31+
#include "tsar/Analysis/Clang/MemoryMatcher.h"
3132
#include "tsar/Support/PassAAProvider.h"
3233
#include "tsar/Support/PassGroupRegistry.h"
3334
#include <bcl/tagged.h>
3435
#include <bcl/utility.h>
3536
#include <llvm/ADT/SmallVector.h>
3637
#include <llvm/ADT/DenseMap.h>
37-
#include <llvm/InitializePasses.h>
38+
#include <llvm/InitializePasses.h>
3839
#include <llvm/Pass.h>
3940

4041
namespace clang {
@@ -70,7 +71,7 @@ using ClangSMParallelProvider =
7071
FunctionPassAAProvider<AnalysisSocketImmutableWrapper, LoopInfoWrapperPass,
7172
ParallelLoopPass, CanonicalLoopPass, LoopMatcherPass,
7273
DFRegionInfoPass, ClangDIMemoryMatcherPass,
73-
ClangPerfectLoopPass>;
74+
MemoryMatcherImmutableWrapper, ClangPerfectLoopPass>;
7475

7576
/// This pass try to insert directives into a source code to obtain
7677
/// a parallel program for a shared memory.

0 commit comments

Comments
 (0)