-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclass_gen_ipv.cpp
351 lines (296 loc) · 16.2 KB
/
class_gen_ipv.cpp
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
#include "kslicer.h"
#include "class_gen.h"
#include "extractor.h"
#include "ast_matchers.h"
#include <sstream>
#include <algorithm>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
void kslicer::IPV_Pattern::ProcessKernelArg(KernelInfo::ArgInfo& arg, const KernelInfo& a_kernel) const
{
auto found = std::find_if(a_kernel.loopIters.begin(), a_kernel.loopIters.end(),
[&](const auto& val){ return arg.name == val.sizeText; });
arg.isLoopSize = (found != a_kernel.loopIters.end());
}
std::vector<kslicer::ArgFinal> kslicer::IPV_Pattern::GetKernelTIDArgs(const KernelInfo& a_kernel) const
{
std::vector<kslicer::ArgFinal> args;
for (uint32_t i = 0; i < a_kernel.loopIters.size(); i++)
{
const auto& arg = a_kernel.loopIters[i];
ArgFinal arg2;
arg2.name = arg.name;
arg2.type = pShaderFuncRewriter->RewriteStdVectorTypeStr(arg.type);
arg2.loopIter = arg;
arg2.loopIter.id = i;
args.push_back(arg2);
}
std::sort(args.begin(), args.end(), [](const auto& a, const auto & b) { return a.loopIter.id < b.loopIter.id; });
return args;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kslicer::IPV_Pattern::MList kslicer::IPV_Pattern::ListMatchers_CF(const std::string& mainFuncName)
{
std::vector<clang::ast_matchers::StatementMatcher> list;
list.push_back(kslicer::MakeMatch_LocalVarOfMethod(mainFuncName));
list.push_back(kslicer::MakeMatch_MemberVarOfMethod(mainFuncName));
list.push_back(kslicer::MakeMatch_MethodCallFromMethod(mainFuncName));
list.push_back(kslicer::MakeMatch_SingleForLoopInsideFunction(mainFuncName));
list.push_back(kslicer::MakeMatch_IfInsideForLoopInsideFunction(mainFuncName));
list.push_back(kslicer::MakeMatch_FunctionCallInsideForLoopInsideFunction(mainFuncName));
list.push_back(kslicer::MakeMatch_IfReturnFromFunction(mainFuncName));
return list;
}
kslicer::IPV_Pattern::MHandlerCFPtr kslicer::IPV_Pattern::MatcherHandler_CF(kslicer::MainFuncInfo& a_mainFuncRef, const clang::CompilerInstance& a_compiler)
{
return std::move(std::make_unique<kslicer::MainFuncAnalyzerRT>(std::cout, *this, a_compiler.getASTContext(), a_mainFuncRef));
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kslicer::IPV_Pattern::MList kslicer::IPV_Pattern::ListMatchers_KF(const std::string& a_kernelName)
{
std::vector<clang::ast_matchers::StatementMatcher> list;
list.push_back(kslicer::MakeMatch_MemberVarOfMethod(a_kernelName));
list.push_back(kslicer::MakeMatch_FunctionCallFromFunction(a_kernelName));
list.push_back(kslicer::MakeMatch_ForLoopInsideFunction(a_kernelName));
list.push_back(kslicer::MakeMatch_BeforeForLoopInsideFunction(a_kernelName));
return list;
}
static bool areSameVariable(const clang::ValueDecl *First, const clang::ValueDecl *Second)
{
return First && Second && First->getCanonicalDecl() == Second->getCanonicalDecl();
}
class LoopHandlerInsideKernelsIPV : public kslicer::UsedCodeFilter
{
public:
explicit LoopHandlerInsideKernelsIPV(std::ostream& s, kslicer::MainClassInfo& a_allInfo, kslicer::KernelInfo* a_currKernel, const clang::CompilerInstance& a_compiler) :
UsedCodeFilter(s, a_allInfo, a_currKernel, a_compiler)
{
m_maxNesting = a_allInfo.GetKernelDim(*a_currKernel);
a_currKernel->loopIters.clear();
}
std::string GetStrideText(const clang::Expr* a_expr)
{
if(clang::isa<clang::UnaryOperator>(a_expr))
{
auto uop = clang::dyn_cast<clang::UnaryOperator>(a_expr);
std::string opStr = std::string(uop->getOpcodeStr(uop->getOpcode()));
if(opStr == "--")
return "(-1)";
else
return "1";
}
//#TODO: process += and -= expressinons, but first we must fix ast matchers for that
return "1";
}
bool FromThisClass(const clang::FunctionDecl* func_decl, int& classOrder)
{
bool fromThisClass = true;
if(func_decl->getNameAsString() == currKernel->name && clang::isa<clang::CXXMethodDecl>(func_decl))
{
const clang::CXXMethodDecl* method = clang::dyn_cast<clang::CXXMethodDecl>(func_decl);
const clang::CXXRecordDecl* parentClass = method->getParent();
std::string className = parentClass->getNameAsString();
auto pFound = m_allInfo.mainClassNames.find(className);
fromThisClass = (pFound != m_allInfo.mainClassNames.end()); // (className == m_mainClassName);
if(fromThisClass)
classOrder = pFound->second;
}
return fromThisClass;
}
void run(clang::ast_matchers::MatchFinder::MatchResult const & result) override
{
using namespace clang;
const FunctionDecl* func_decl = result.Nodes.getNodeAs<FunctionDecl>("targetFunction");
const ForStmt* forLoop = result.Nodes.getNodeAs<ForStmt>("loop");
const CompoundStmt* loopOutsidesInit = result.Nodes.getNodeAs<CompoundStmt>("loopInitCode");
//clang::SourceManager& srcMgr(const_cast<clang::SourceManager &>(result.Context->getSourceManager()));
if(forLoop && func_decl)
{
const clang::VarDecl* initVar = result.Nodes.getNodeAs<clang::VarDecl>("initVar");
const clang::VarDecl* condVar = result.Nodes.getNodeAs<clang::VarDecl>("condVar");
const clang::VarDecl* incVar = result.Nodes.getNodeAs<clang::VarDecl>("incVar");
const clang::Expr* loopSZ = result.Nodes.getNodeAs<clang::Expr> ("loopSize");
const bool sameInitAndCond = areSameVariable(initVar,condVar);
const bool sameInitAndInc = areSameVariable(initVar, incVar);
//std::string debugInitVar = kslicer::GetRangeSourceCode(initVar->getSourceRange(), m_compiler);
//std::string debugCondVar = kslicer::GetRangeSourceCode(condVar->getSourceRange(), m_compiler);
//std::string debugIncVar = kslicer::GetRangeSourceCode(incVar->getSourceRange(), m_compiler);
//std::string debugForExp = kslicer::GetRangeSourceCode(forLoop->getSourceRange(), m_compiler);
if(sameInitAndCond && sameInitAndInc && loopSZ)
{
std::string name = initVar->getNameAsString();
//std::string debugText = kslicer::GetRangeSourceCode(forLoop->getBody()->getSourceRange(), m_compiler);
int classOrder = 0;
bool fromThisClass = FromThisClass(func_decl, classOrder);
if(classOrder > currKernel->loopInsidesOrder) // found loop in derived already, skip base
fromThisClass = false;
else if(classOrder < currKernel->loopInsidesOrder) // override all loops by this (derived)
currKernel->loopIters.clear();
//std::cout << " [LoopHandlerIPV]: Variable name is: " << name.c_str() << std::endl;
if(fromThisClass && currKernel->loopIters.size() < m_maxNesting)
{
const clang::QualType qt = initVar->getType();
kslicer::KernelInfo::LoopIter tidArg;
tidArg.name = initVar->getNameAsString();
tidArg.type = qt.getAsString();
tidArg.sizeText = kslicer::GetRangeSourceCode(loopSZ->getSourceRange(), m_compiler);
tidArg.startText = kslicer::GetRangeSourceCode(initVar->getAnyInitializer()->getSourceRange(), m_compiler);
tidArg.strideText = GetStrideText(forLoop->getInc()); //kslicer::GetRangeSourceCode(forLoop->getInc()->getSourceRange(), m_compiler);
tidArg.condTextOriginal = kslicer::GetRangeSourceCode(forLoop->getCond()->getSourceRange(), m_compiler);
tidArg.iterTextOriginal = kslicer::GetRangeSourceCode(forLoop->getInc()->getSourceRange(), m_compiler);
//tidArg.startRange = initVar->getAnyInitializer()->getSourceRange(); // seems does not works
//tidArg.sizeRange = loopSZ->getSourceRange(); // seems does not works
//tidArg.strideRange = forLoop->getInc()->getSourceRange(); // seems does not works
//tidArg.startNode = initVar->getAnyInitializer(); // seems does not works
//tidArg.sizeNode = loopSZ; // seems does not works
//tidArg.strideNode = forLoop->getInc(); // seems does not works
tidArg.loopNesting = uint32_t(currKernel->loopIters.size());
currKernel->loopIters.push_back(tidArg);
currKernel->loopInsides = forLoop->getBody()->getSourceRange();
currKernel->loopInsidesOrder = classOrder;
}
}
}
else if(loopOutsidesInit && func_decl)
{
int classOrder = 0;
bool fromThisClass = FromThisClass(func_decl, classOrder);
if(classOrder > currKernel->loopOutsidesInitOrder)
fromThisClass = false;
if(fromThisClass)
{
currKernel->loopOutsidesInit = loopOutsidesInit->getSourceRange();
currKernel->loopOutsidesInitOrder = classOrder;
auto debugText = kslicer::GetRangeSourceCode(loopOutsidesInit->getSourceRange(), m_compiler);
//std::cout << "debugText = " << debugText.c_str() << std::endl;
clang::SourceLocation endOfInit = currKernel->loopOutsidesInit.getEnd();
auto p = loopOutsidesInit->body_begin();
for(; p != loopOutsidesInit->body_end(); ++p)
{
const Stmt* expr = *p;
if(isa<ForStmt>(expr)) // TODO: CHECK THIS IS EXACTLY THE 'for' WE NEED! HOW?
break; // TODO: REMEMBER 'for' LOCATION IN SOME VARIABLE AND IGNORE OTHER 'fors' INSIDE 'if(forLoop && func_decl)'
endOfInit = expr->getSourceRange().getEnd(); // TODO: WHICH ARE NOT INSIDE THIS FOR AND NOT THIS FOR ITSELF.
}
currKernel->hasInitPass = (currKernel->loopOutsidesInit.getEnd() != endOfInit);
currKernel->loopOutsidesInit.setEnd(endOfInit);
++p;
if(p != loopOutsidesInit->body_end())
{
const Stmt* beginOfEnd = *p;
const auto range = beginOfEnd->getSourceRange();
const auto begin = range.getBegin();
currKernel->loopOutsidesFinish.setBegin(begin);
p = loopOutsidesInit->body_end(); --p;
const Stmt* endOfFinish = *p;
currKernel->loopOutsidesFinish.setEnd(endOfFinish->getSourceRange().getEnd());
currKernel->hasFinishPassSelf = (currKernel->loopOutsidesFinish.getBegin() != currKernel->loopOutsidesFinish.getEnd());
//auto debugMeTail = kslicer::GetRangeSourceCode(currKernel->loopOutsidesFinish, m_compiler);
//std::cout << "debugMeTail = " << debugMeTail.c_str() << std::endl;
}
}
}
else
UsedCodeFilter::run(result);
return;
} // run
uint32_t m_maxNesting = 0;
};
kslicer::IPV_Pattern::MHandlerKFPtr kslicer::IPV_Pattern::MatcherHandler_KF(KernelInfo& kernel, const clang::CompilerInstance& a_compile)
{
return std::move(std::make_unique<LoopHandlerInsideKernelsIPV>(std::cout, *this, &kernel, a_compile));
}
std::string kslicer::IPV_Pattern::VisitAndRewrite_KF(KernelInfo& a_funcInfo, const clang::CompilerInstance& compiler,
std::string& a_outLoopInitCode, std::string& a_outLoopFinishCode)
{
//if(a_funcInfo.name == "kernel2D_ExtractBrightPixels")
// a_funcInfo.astNode->dump();
Rewriter rewrite2;
rewrite2.setSourceMgr(compiler.getSourceManager(), compiler.getLangOpts());
auto pVisitor = pShaderCC->MakeKernRewriter(rewrite2, compiler, this, a_funcInfo, "");
pVisitor->SetCurrKernelInfo(&a_funcInfo);
auto kernelNodes = kslicer::ExtractKernelForLoops(a_funcInfo.astNode->getBody(), int(a_funcInfo.loopIters.size()), compiler);
std::string funcBodyText = ""; // SOME REPLACEMENT DOES NOT WORKS
//{
//if(kernelNodes.loopBody != nullptr)
//{
// funcBodyText = pVisitor->RecursiveRewrite(kernelNodes.loopBody);
// if(!clang::isa<clang::CompoundStmt>(kernelNodes.loopBody))
// funcBodyText += ";";
//}
//if(kernelNodes.beforeLoop != nullptr) // beforeLoop does not works, removed
// a_outLoopInitCode = pVisitor->RecursiveRewrite(kernelNodes.beforeLoop);
//else
// a_outLoopInitCode = "";
//
//if(kernelNodes.afterLoop != nullptr)
// a_outLoopFinishCode = pVisitor->RecursiveRewrite(kernelNodes.afterLoop); // afterLoop does not works in this way, removed
//else
// a_outLoopFinishCode = "";
//
//if(kernelNodes.loopBody != nullptr)
// return funcBodyText;
//else
// return "//empty kernel body is found";
//}
// old way
//
pVisitor->TraverseDecl(const_cast<clang::CXXMethodDecl*>(a_funcInfo.astNode));
//pVisitor->TraverseStmt(const_cast<clang::Stmt*>(a_funcInfo.astNode->getBody()));
pVisitor->ApplyDefferedWorkArounds();
pVisitor->ResetCurrKernelInfo();
a_funcInfo.shaderFeatures = a_funcInfo.shaderFeatures || pVisitor->GetKernelShaderFeatures(); // TODO: don't work !!!
if(a_funcInfo.loopOutsidesInit.isValid())
{
auto brokenEnd = a_funcInfo.loopOutsidesInit.getEnd().getRawEncoding();
auto nextBegin = a_funcInfo.loopInsides.getBegin().getRawEncoding();
if(brokenEnd + 1 < nextBegin)
{
auto repairedEnd = clang::SourceLocation::getFromRawEncoding(brokenEnd+1);
a_funcInfo.loopOutsidesInit.setEnd(repairedEnd);
a_outLoopInitCode = rewrite2.getRewrittenText(a_funcInfo.loopOutsidesInit) + ";";
}
}
if(a_funcInfo.loopOutsidesFinish.isValid())
a_outLoopFinishCode = rewrite2.getRewrittenText(a_funcInfo.loopOutsidesFinish) + ";";
// new way
//
if(funcBodyText != "")
return funcBodyText;
else
return rewrite2.getRewrittenText(a_funcInfo.loopInsides) + ";"; // old way works
}
void kslicer::MainClassInfo::VisitAndPrepare_KF(KernelInfo& a_funcInfo, const clang::CompilerInstance& compiler)
{
//a_funcInfo.astNode->dump();
// (1) run visitor for basic kernel info
//
Rewriter rewrite2;
rewrite2.setSourceMgr(compiler.getSourceManager(), compiler.getLangOpts());
auto pVisitor = std::make_shared<KernelInfoVisitor>(rewrite2, compiler, this, a_funcInfo);
pVisitor->TraverseDecl(const_cast<clang::CXXMethodDecl*>(a_funcInfo.astNode));
// (2) analize all buffers and iput containers for shader features and e.t.c
//
ShaderFeatures accessShaderFeatures;
for(const auto& arg : a_funcInfo.args) {
if(arg.isContainer || arg.IsPointer()) {
//std::cout << "arg.type = " << arg.type << std::endl;
accessShaderFeatures = accessShaderFeatures || kslicer::GetUsedShaderFeaturesFromTypeName(arg.type);
}
}
for(const auto& cont : a_funcInfo.usedContainers) {
//std::cout << "cont.second.type = " << cont.second.type.c_str() << std::endl;
accessShaderFeatures = accessShaderFeatures || kslicer::GetUsedShaderFeaturesFromTypeName(cont.second.type);
}
a_funcInfo.shaderFeatures = a_funcInfo.shaderFeatures || accessShaderFeatures;
if(accessShaderFeatures.useByteType)
a_funcInfo.shaderFeatures.use8BitStorage = true;
}