systemc-clang 2.0.0
Parsing SystemC constructs
Loading...
Searching...
No Matches
FindGPUMacro.cpp
Go to the documentation of this file.
1#include "FindGPUMacro.h"
2#include "clang/AST/ASTContext.h"
3using namespace systemc_clang;
4
5GPUMacro::GPUMacro(int blockIdx, int blockIdy, int blockIdz, int threadIdx,
6 int threadIdy, int threadIdz, int gpuTime, int cpuTime)
7 : _blockIdx(blockIdx), _blockIdy(blockIdy), _blockIdz(blockIdz),
8 _threadIdx(threadIdx), _threadIdy(threadIdy), _threadIdz(threadIdz),
9 _gpuTime(gpuTime), _cpuTime(cpuTime), _gpuFit(false) {}
10
12 : _blockIdx(1), _blockIdy(1), _blockIdz(1), _threadIdx(1), _threadIdy(1),
13 _threadIdz(1), _gpuTime(0), _cpuTime(0), _gpuFit(false) {}
14
16
17void GPUMacro::addGPUFit() { _gpuFit = true; }
18
19void GPUMacro::denyGPUFit() { _gpuFit = false; }
20
21bool GPUMacro::isGPUFit() { return _gpuFit; }
22
24
26
28
30
32
34
36
38
39void GPUMacro::dump(raw_ostream &os) {
40 os << "\n Block Ids : " << _blockIdx << " " << _blockIdy << " " << _blockIdz;
41
42 os << "\n Thread Ids : " << _threadIdx << " " << _threadIdy << " "
43 << _threadIdz;
44
45 os << "\n GPU time : " << _gpuTime << " CPU time : " << _cpuTime;
46}
47
49FindGPUMacro::FindGPUMacro(CXXMethodDecl *entryFunction, int instanceNum,
50 llvm::raw_ostream &os)
51 : _entryFunction(entryFunction), _instanceNum(instanceNum), _os(os) {
52 TraverseDecl(_entryFunction);
53}
54
56
57bool FindGPUMacro::VisitForStmt(ForStmt *fstmt) {
58
59 Stmt *body = fstmt->getBody();
60
62
63 int tx = 1, ty = 1, tz = 1, bx = 1, by = 1, bz = 1, gpu_time = 0,
64 cpu_time = 0, instanceNum = 0;
65 for (Stmt::child_iterator it = body->child_begin(), eit = body->child_end();
66 it != eit; it++) {
67
68 Stmt *s = *it;
69
70 if (DeclStmt *ds = dyn_cast<DeclStmt>(s)) {
71 if (VarDecl *vd = dyn_cast<VarDecl>(ds->getSingleDecl())) {
72 string className = vd->getTypeSourceInfo()
73 ->getType()
74 .getBaseTypeIdentifier()
75 ->getName();
76 if (className == "profile_time") {
77 if (CXXConstructExpr *ce =
78 dyn_cast<CXXConstructExpr>(vd->getInit()->IgnoreImpCasts())) {
79 if (MaterializeTemporaryExpr *me =
80 dyn_cast<MaterializeTemporaryExpr>(
81 ce->getArg(0)->IgnoreImpCasts())) {
82 if (CXXTemporaryObjectExpr *co = dyn_cast<CXXTemporaryObjectExpr>(
83 me->GetTemporaryExpr()->IgnoreImpCasts())) {
84
85 IntegerLiteral *x = dyn_cast<IntegerLiteral>(co->getArg(0));
86 IntegerLiteral *y = dyn_cast<IntegerLiteral>(co->getArg(1));
87 IntegerLiteral *z = dyn_cast<IntegerLiteral>(co->getArg(2));
88
89 instanceNum = x->getValue().getSExtValue();
90 gpu_time = y->getValue().getSExtValue();
91 cpu_time = z->getValue().getSExtValue();
92 }
93 }
94 }
95 }
96
97 if (className == "sc_gpu_thread_hierarchy") {
98 if (CXXConstructExpr *ce =
99 dyn_cast<CXXConstructExpr>(vd->getInit()->IgnoreImpCasts())) {
100 if (MaterializeTemporaryExpr *me =
101 dyn_cast<MaterializeTemporaryExpr>(
102 ce->getArg(0)->IgnoreImpCasts())) {
103 if (CXXTemporaryObjectExpr *co = dyn_cast<CXXTemporaryObjectExpr>(
104 me->GetTemporaryExpr()->IgnoreImpCasts())) {
105 IntegerLiteral *x = dyn_cast<IntegerLiteral>(co->getArg(1));
106 IntegerLiteral *y = dyn_cast<IntegerLiteral>(co->getArg(2));
107 IntegerLiteral *z = dyn_cast<IntegerLiteral>(co->getArg(3));
108 IntegerLiteral *w = dyn_cast<IntegerLiteral>(co->getArg(4));
109 instanceNum = x->getValue().getSExtValue();
110 tx = x->getValue().getSExtValue();
111 ty = y->getValue().getSExtValue();
112 tz = z->getValue().getSExtValue();
113 }
114 }
115 }
116 }
117 if (className == "sc_gpu_block_hierarchy") {
118 if (CXXConstructExpr *ce =
119 dyn_cast<CXXConstructExpr>(vd->getInit()->IgnoreImpCasts())) {
120 if (MaterializeTemporaryExpr *me =
121 dyn_cast<MaterializeTemporaryExpr>(
122 ce->getArg(0)->IgnoreImpCasts())) {
123 if (CXXTemporaryObjectExpr *co = dyn_cast<CXXTemporaryObjectExpr>(
124 me->GetTemporaryExpr()->IgnoreImpCasts())) {
125 IntegerLiteral *x = dyn_cast<IntegerLiteral>(co->getArg(1));
126 IntegerLiteral *y = dyn_cast<IntegerLiteral>(co->getArg(2));
127 IntegerLiteral *z = dyn_cast<IntegerLiteral>(co->getArg(3));
128 IntegerLiteral *w = dyn_cast<IntegerLiteral>(co->getArg(4));
129
130 instanceNum = x->getValue().getSExtValue();
131 bx = y->getValue().getSExtValue();
132 by = z->getValue().getSExtValue();
133 bz = w->getValue().getSExtValue();
134 }
135 }
136 }
137 }
138 }
139 }
140
141 //_os <<"\n gpu_time : " <<gpu_time<<" cpu_time : " <<cpu_time<<"
142 //instanceNum : " <<_instanceNum<<" " <<instanceNum;
143 if (tx && ty && tz && bx && by && bz && gpu_time && cpu_time &&
144 (_instanceNum == instanceNum)) {
145 //_os <<"\n instance num : " <<_instanceNum<<" " <<instanceNum;
146 GPUMacro *gm = new GPUMacro(bx, by, bz, tx, ty, tz, gpu_time, cpu_time);
147 //_os <<"\n for stmt : " <<fstmt;
148 forStmtInstanceIdPairType forStmtInstanceId =
149 make_pair(_instanceNum, fstmt);
150 _forStmtGPUMacroMap.insert(
151 forStmtGPUMacroPairType(forStmtInstanceId, gm));
152 break;
153 }
154 }
155 return true;
156}
157
162
164 _os << "\n For Stmt GPU Macro";
165 for (forStmtGPUMacroMapType::iterator it = _forStmtGPUMacroMap.begin(),
166 eit = _forStmtGPUMacroMap.end();
167 it != eit; it++) {
168
169 _os << "\n For Stmt : " << it->first.second;
170 _os << "\n GPU kernel parameters \n";
171 it->second->dump(_os);
172 }
173}
174
176 QualType type = val->getType();
177
178 /*
179 std::pair<uint64_t, unsigned> fieldInfo =
180 val->getASTContext().getTypeInfo(val->getType());
181 uint64_t typeSize = fieldInfo.first;
182 unsigned fieldAlign = fieldInfo.second;
183
184 _os << "base type: " << type.getCanonicalType().getAsString()
185 << ", size (bits): " << typeSize
186 << ", align (bits): " << fieldAlign
187 << "\n";
188 */
189}
190
191void FindGPUMacro::analyze_array_base(Expr *base, bool isLHS) {
192 if (CastExpr *cast = dyn_cast<CastExpr>(base)) {
193 Expr *subExpr = cast->getSubExpr();
194
195 if (DeclRefExpr *declRef = dyn_cast<DeclRefExpr>(subExpr)) {
196 if (isLHS) {
197 lhs_decls.insert(declRef->getDecl());
198 _os << "LHS: ";
199 } else {
200 rhs_decls.insert(declRef->getDecl());
201 _os << "RHS: ";
202 }
203 analyze_value_decl(declRef->getDecl());
204 } else if (MemberExpr *member = dyn_cast<MemberExpr>(subExpr)) {
205 if (isLHS) {
206 lhs_decls.insert(member->getMemberDecl());
207 _os << "LHS: ";
208 } else {
209 rhs_decls.insert(member->getMemberDecl());
210 _os << "RHS: ";
211 }
212 analyze_value_decl(member->getMemberDecl());
213 } else {
214 _os << "Type not a DeclRefExpr" << cast->getStmtClassName() << "\n";
215 }
216 } else {
217 _os << "Type not a CastExpr" << cast->getStmtClassName() << "\n";
218 }
219}
220
222 if (ArraySubscriptExpr *array = dyn_cast<ArraySubscriptExpr>(expr)) {
223 Expr *base = array->getBase();
224 Expr *idx = array->getIdx();
225
226 analyze_array_base(base, true /* isLHS */);
227 } else if (CastExpr *cast = dyn_cast<CastExpr>(expr)) {
228 analyze_lhs(cast->getSubExpr());
229 } else if (ParenExpr *paren = dyn_cast<ParenExpr>(expr)) {
230 analyze_lhs(paren->getSubExpr());
231 } else if (BinaryOperator *binOp = dyn_cast<BinaryOperator>(expr)) {
232 if (binOp->isAssignmentOp()) {
233 analyze_lhs(binOp->getLHS());
234 analyze_rhs(binOp->getRHS());
235 } else if (binOp->isCompoundAssignmentOp()) {
236 analyze_lhs(binOp->getLHS());
237 analyze_rhs(binOp->getRHS());
238 } else {
239 _os << "Operation without effects on LHS: " << binOp->getStmtClassName()
240 << "\n";
241 }
242 }
243}
244
246 if (ArraySubscriptExpr *array = dyn_cast<ArraySubscriptExpr>(expr)) {
247 Expr *base = array->getBase();
248 Expr *idx = array->getIdx();
249
250 analyze_array_base(base, false /* isLHS */);
251 } else if (CastExpr *cast = dyn_cast<CastExpr>(expr)) {
252 analyze_rhs(cast->getSubExpr());
253 } else if (ParenExpr *paren = dyn_cast<ParenExpr>(expr)) {
254 analyze_rhs(paren->getSubExpr());
255 } else if (BinaryOperator *binOp = dyn_cast<BinaryOperator>(expr)) {
256 if (binOp->isAssignmentOp()) {
257 analyze_lhs(binOp->getLHS());
258 analyze_rhs(binOp->getRHS());
259 } else if (binOp->isCompoundAssignmentOp()) {
260 analyze_lhs(binOp->getLHS());
261 analyze_rhs(binOp->getRHS());
262 } else {
263 analyze_rhs(binOp->getLHS());
264 analyze_rhs(binOp->getRHS());
265 }
266 }
267}
268
270 if (BinaryOperator *binOp = dyn_cast<BinaryOperator>(expr)) {
271 if (binOp->isAssignmentOp()) {
272 analyze_lhs(binOp->getLHS());
273 analyze_rhs(binOp->getRHS());
274 } else if (binOp->isCompoundAssignmentOp()) {
275 analyze_lhs(binOp->getLHS());
276 analyze_rhs(binOp->getRHS());
277 } else {
278 _os << "Operation without effects: " << binOp->getStmtClassName() << "\n";
279 }
280 }
281}
282
284 if (!stmtList)
285 return;
286
287 _os << "\nNEW FORLOOP BEGIN\n";
288
289 for (Stmt::child_iterator itr = stmtList->child_begin();
290 itr != stmtList->child_end(); ++itr) {
291 Stmt *stmt = *itr;
292 if (!stmt)
293 continue;
294
295 Expr *expr = dyn_cast<Expr>(stmt);
296 if (!expr)
297 continue;
298
299 // Dump out the AST tree
300 expr->dump();
301
302 analyze_expr(expr);
303 }
304
305 for (std::set<ValueDecl *>::iterator itr = lhs_decls.begin();
306 itr != lhs_decls.end(); ++itr) {
307 _os << "LHS ValueDecl AST Node: " << *itr << ", Name: '"
308 << (*itr)->getNameAsString() << "', ";
309 analyze_value_decl(*itr);
310 }
311
312 for (std::set<ValueDecl *>::iterator itr = rhs_decls.begin();
313 itr != rhs_decls.end(); ++itr) {
314 _os << "RHS ValueDecl AST Node: " << *itr << ", Name: '"
315 << (*itr)->getNameAsString() << "', ";
316 analyze_value_decl(*itr);
317 }
318
319 _os << "NEW FORLOOP END\n";
320}
forStmtGPUMacroMapType _forStmtGPUMacroMap
void analyze_data_struct(Stmt *stmtList)
std::set< ValueDecl * > rhs_decls
forStmtGPUMacroMapType getForStmtGPUMacroMap()
void analyze_value_decl(ValueDecl *val)
CXXMethodDecl * _entryFunction
std::set< ValueDecl * > lhs_decls
void analyze_array_base(Expr *base, bool isLHS)
virtual bool VisitForStmt(ForStmt *)
pair< forStmtInstanceIdPairType, GPUMacro * > forStmtGPUMacroPairType
map< forStmtInstanceIdPairType, GPUMacro * > forStmtGPUMacroMapType
pair< int, ForStmt * > forStmtInstanceIdPairType
FindGPUMacro(CXXMethodDecl *, int, raw_ostream &)
void dump(raw_ostream &)