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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276 | //===-- sotoc/src/TargetCodeFragment.cpp ---------------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file is distributed under the University of Illinois Open Source
// License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//===----------------------------------------------------------------------===//
#include <sstream>
#include "clang/AST/Decl.h"
#include "clang/AST/DeclOpenMP.h"
#include "clang/AST/PrettyPrinter.h"
#include "clang/AST/Stmt.h"
#include "clang/AST/StmtOpenMP.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Basic/LangOptions.h"
#include "clang/Basic/SourceLocation.h"
#include "clang/Basic/TokenKinds.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/Token.h"
#include "Debug.h"
#include "OmpPragma.h"
#include "TargetCodeFragment.h"
void TargetCodeRegion::addCapture(const clang::CapturedStmt::Capture *Capture) {
CapturedVars.push_back(TargetRegionVariable(Capture, CapturedLowerBounds));
}
void TargetCodeRegion::addOMPClauseParam(clang::VarDecl *Param) {
for (auto &CV : capturedVars()) {
if (CV.getDecl() == Param) {
return;
}
}
if (std::find(OMPClausesParams.begin(), OMPClausesParams.end(), Param) != OMPClausesParams.end()) {
return;
}
DEBUGP("Adding variable " << Param->getName() << "as OpenMP clause parameter");
OMPClausesParams.push_back(Param);
}
void TargetCodeRegion::addOMPClause(clang::OMPClause *Clause) {
OMPClauses.push_back(Clause);
}
static bool hasRegionCompoundStmt(const clang::Stmt *S) {
if (const auto *SS = llvm::dyn_cast<clang::CapturedStmt>(S)) {
if (llvm::isa<clang::CompoundStmt>(SS->getCapturedStmt())) {
return true;
} else if (llvm::isa<clang::CapturedStmt>(SS->getCapturedStmt())) {
return hasRegionCompoundStmt(SS->getCapturedStmt());
}
}
return false;
}
static bool hasRegionOMPStmt(const clang::Stmt *S) {
if (const auto *SS = llvm::dyn_cast<clang::CapturedStmt>(S)) {
if (llvm::isa<clang::OMPExecutableDirective>(SS->getCapturedStmt())) {
return true;
} else if (llvm::isa<clang::CapturedStmt>(SS->getCapturedStmt())) {
return hasRegionOMPStmt(SS->getCapturedStmt());
}
}
return false;
}
static clang::SourceLocation getOMPStmtSourceLocEnd(const clang::Stmt *S) {
while (auto *CS = llvm::dyn_cast<clang::CapturedStmt>(S)) {
S = CS->getCapturedStmt();
}
while (auto *OmpExecS = llvm::dyn_cast<clang::OMPExecutableDirective>(S)) {
S = OmpExecS->getInnermostCapturedStmt();
if (auto *CS = llvm::dyn_cast<clang::CapturedStmt>(S)) {
S = CS->getCapturedStmt();
}
}
return S->getEndLoc();
}
// TODO: Implement recursive for an arbitrary depth?
static clang::SourceLocation findPreviousToken(clang::SourceLocation Loc,
clang::SourceManager &SM,
const clang::LangOptions &LO) {
clang::Token token;
Loc = clang::Lexer::GetBeginningOfToken(Loc, SM, LO);
// Search until we find a valid token before Loc
// TODO: Error handling if no token can be found
do {
Loc = clang::Lexer::GetBeginningOfToken(Loc.getLocWithOffset(-1), SM, LO);
} while ((clang::Lexer::getRawToken(Loc, token, SM, LO)));
return token.getLocation();
}
TargetCodeFragment::~TargetCodeFragment() {}
clang::SourceLocation TargetCodeRegion::getStartLoc() {
clang::SourceManager &SM = Context.getSourceManager();
const clang::LangOptions &LO = Context.getLangOpts();
auto TokenBegin = clang::Lexer::GetBeginningOfToken(
CapturedStmtNode->getBeginLoc(), SM, LO);
if (hasRegionCompoundStmt(CapturedStmtNode)) {
#if 0
// This piece of code could be used to check if we start with a new scope.
// However, the pretty printer destroys this again somehow...
// Since the extra scope does not really hurt, i will leave it as it is for now.
clang::Token token;
if(!(clang::Lexer::getRawToken(TokenBegin, token, SM, LO))) {
if (token.is(clang::tok::l_brace)) {
auto possibleNextToken = clang::Lexer::findNextToken(
TokenBegin, SM, LO);
if (possibleNextToken.hasValue()) {
return possibleNextToken.getValue().getLocation();
} else {
llvm::outs()<< "OUCH\n";
}
return TokenBegin.getLocWithOffset(1);
}
}
else llvm::outs() << "NOTOK\n";
#endif
return TokenBegin;
} else if (hasRegionOMPStmt(CapturedStmtNode)) {
// We have to go backwards 2 tokens in case of an OMP statement
// (the '#' and the 'pragma').
return findPreviousToken(findPreviousToken(TokenBegin, SM, LO), SM, LO);
} else {
return CapturedStmtNode->getBeginLoc();
}
}
clang::SourceLocation TargetCodeRegion::getEndLoc() {
clang::SourceManager &SM = Context.getSourceManager();
const clang::LangOptions &LO = Context.getLangOpts();
auto N = CapturedStmtNode;
if (hasRegionCompoundStmt(N)) {
return clang::Lexer::GetBeginningOfToken(N->getEndLoc(), SM, LO)
.getLocWithOffset(-1); // TODO: If I set this to "1" it works too. I
// think it was here to remove addition scope
// which i get with "printPretty". Does this
// need some fixing?
} else if (hasRegionOMPStmt(N)) {
return getOMPStmtSourceLocEnd(N);
} else {
return N->getEndLoc();
}
}
const std::string TargetCodeRegion::getParentFuncName() {
return ParentFunctionDecl->getNameInfo().getAsString();
}
clang::SourceLocation TargetCodeRegion::getTargetDirectiveLocation() {
return TargetDirective->getBeginLoc();
}
clang::SourceRange TargetCodeRegion::getRealRange() {
return CapturedStmtNode->getSourceRange();
}
clang::SourceRange TargetCodeRegion::getSpellingRange() {
auto &SM =
CapturedStmtNode->getCapturedDecl()->getASTContext().getSourceManager();
auto InnerRange = getInnerRange();
return clang::SourceRange(SM.getSpellingLoc(InnerRange.getBegin()),
SM.getSpellingLoc(InnerRange.getEnd()));
}
clang::SourceRange TargetCodeRegion::getInnerRange() {
auto InnerLocStart = getStartLoc();
auto InnerLocEnd = getEndLoc();
return clang::SourceRange(InnerLocStart, InnerLocEnd);
}
class TargetRegionPrinterHelper : public clang::PrinterHelper {
clang::PrintingPolicy PP;
public:
TargetRegionPrinterHelper(clang::PrintingPolicy PP) : PP(PP){};
bool handledStmt(clang::Stmt *E, llvm::raw_ostream &OS) {
if (auto *Directive = llvm::dyn_cast<clang::OMPExecutableDirective>(E)) {
if (OmpPragma::isReplaceable(Directive)) {
OmpPragma(Directive, PP).printReplacement(OS);
OS << "\n";
Directive->child_begin()->printPretty(OS, this, PP);
return true;
}
if (OmpPragma::needsAdditionalPragma(Directive)) {
OmpPragma(Directive, PP).printAddition(OS);
OS << "\n";
return false;
}
}
return false;
}
};
std::string TargetCodeRegion::PrintPretty() {
// TODO: Is there a better approach (e.g., token or preprocessor based?)
// One issue here: Addition braces (i.e., scope) in some cases.
std::string PrettyStr = "";
llvm::raw_string_ostream PrettyOS(PrettyStr);
TargetRegionPrinterHelper Helper(PP);
if (CapturedStmtNode != NULL)
CapturedStmtNode->printPretty(PrettyOS, &Helper, PP);
return PrettyOS.str();
}
clang::SourceRange TargetCodeDecl::getRealRange() {
return DeclNode->getSourceRange();
}
clang::SourceRange TargetCodeDecl::getSpellingRange() {
auto &SM = DeclNode->getASTContext().getSourceManager();
auto InnerRange = getInnerRange();
return clang::SourceRange(SM.getSpellingLoc(InnerRange.getBegin()),
SM.getSpellingLoc(InnerRange.getEnd()));
}
std::string TargetCodeDecl::PrintPretty() {
std::string PrettyStr = "";
llvm::raw_string_ostream PrettyOS(PrettyStr);
// This hack solves our problem with structs and enums being autoexpanded#
// sometimes (See comment in Issue #20.
clang::PrintingPolicy LocalPP(PP);
if (llvm::isa<clang::TypedefDecl>(DeclNode)) {
LocalPP.IncludeTagDefinition = 1;
}
TargetRegionPrinterHelper Helper(PP);
// This hack removes the 'static' keyword from globalVarDecls, because we
// cannot find variables from the host if they are static.
bool HasStaticKeyword = false;
if (auto *VarDeclNode = llvm::dyn_cast<clang::VarDecl>(DeclNode)) {
if (VarDeclNode->getStorageClass() == clang::SC_Static) {
HasStaticKeyword = true;
VarDeclNode->setStorageClass(clang::SC_None);
}
}
DeclNode->print(PrettyOS, LocalPP, 0, false, &Helper);
// Add static storage class back so (hopefully) this doesnt break anyting
// (but it totally will).
if (auto *VarDeclNode = llvm::dyn_cast<clang::VarDecl>(DeclNode)) {
if (HasStaticKeyword) {
VarDeclNode->setStorageClass(clang::SC_Static);
}
}
// This hack removes '#pragma omp declare target' from the output
std::string outString = PrettyOS.str();
const char *declareTargetPragma = "#pragma omp declare target";
if (outString.compare(0, strlen(declareTargetPragma), declareTargetPragma) ==
0) {
outString = outString.substr(strlen(declareTargetPragma));
}
return outString;
}
|