-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathclass_gen_ipv.cpp
More file actions
259 lines (220 loc) · 12.6 KB
/
class_gen_ipv.cpp
File metadata and controls
259 lines (220 loc) · 12.6 KB
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
#include "kslicer.h"
#include "class_gen.h"
#include "extractor.h"
#include "ast_matchers.h"
#include <sstream>
#include <algorithm>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
kslicer::MainClassInfo::MList kslicer::MainClassInfo::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::MainClassInfo::MHandlerCFPtr kslicer::MainClassInfo::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::MainClassInfo::MList kslicer::MainClassInfo::ListMatchers_KF(const KernelInfo& a_kernel)
{
const std::string& a_kernelName = a_kernel.name;
std::vector<clang::ast_matchers::StatementMatcher> list;
list.push_back(kslicer::MakeMatch_MemberVarOfMethod(a_kernelName));
list.push_back(kslicer::MakeMatch_FunctionCallFromFunction(a_kernelName));
if(a_kernel.pattern == kslicer::PATTERN_TP::PATTERN_IPV)
{
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::MainClassInfo::MHandlerKFPtr kslicer::MainClassInfo::MatcherHandler_KF(KernelInfo& kernel, const clang::CompilerInstance& a_compile)
{
if(kernel.pattern == kslicer::PATTERN_TP::PATTERN_IPV)
return std::move(std::make_unique<LoopHandlerInsideKernelsIPV>(std::cout, *this, &kernel, a_compile));
else if(kernel.pattern == kslicer::PATTERN_TP::PATTERN_RTV)
return std::move(std::make_unique<kslicer::UsedCodeFilter>(std::cout, *this, &kernel, a_compile));
else
return nullptr;
}
void kslicer::MainClassInfo::VisitAndPrepare_KF(KernelInfo& a_funcInfo, const clang::CompilerInstance& compiler)
{
// (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 input 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;
}