-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkslicer_utils.cpp
More file actions
602 lines (510 loc) · 19.2 KB
/
kslicer_utils.cpp
File metadata and controls
602 lines (510 loc) · 19.2 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
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
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
#include "kslicer.h"
#include "initial_pass.h"
#include "template_rendering.h"
#include <algorithm>
#include <cctype>
#include <string>
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::string kslicer::GetRangeSourceCode(const clang::SourceRange a_range, const clang::CompilerInstance& compiler)
{
const clang::SourceManager& sm = compiler.getSourceManager();
const clang::LangOptions& lopt = compiler.getLangOpts();
clang::SourceLocation b(a_range.getBegin()), _e(a_range.getEnd());
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, sm, lopt));
if(e < b)
return std::string("");
else
return std::string(sm.getCharacterData(b), sm.getCharacterData(e));
}
std::string kslicer::GetRangeSourceCode(const clang::SourceRange a_range, const clang::SourceManager& sm)
{
clang::LangOptions lopt;
clang::SourceLocation b(a_range.getBegin()), _e(a_range.getEnd());
clang::SourceLocation e(clang::Lexer::getLocForEndOfToken(_e, 0, sm, lopt));
return std::string(sm.getCharacterData(b), sm.getCharacterData(e));
}
uint64_t kslicer::GetHashOfSourceRange(const clang::SourceRange& a_range)
{
const uint32_t hash1 = a_range.getBegin().getRawEncoding();
const uint32_t hash2 = a_range.getEnd().getRawEncoding();
return (uint64_t(hash1) << 32) | uint64_t(hash2);
}
void kslicer::PrintError(const std::string& a_msg, const clang::SourceRange& a_range, const clang::SourceManager& a_sm)
{
const auto beginLoc = a_range.getBegin();
const auto inFileLoc = a_sm.getPresumedLoc(beginLoc);
const auto fileName = std::string(a_sm.getFilename(beginLoc));
const auto line = inFileLoc.getLine();
const auto col = inFileLoc.getColumn();
std::string code = GetRangeSourceCode(a_range, a_sm);
std::cout << fileName.c_str() << ":" << line << ":" << col << ": error: " << a_msg << std::endl;
std::cout << "--> " << code.c_str() << std::endl;
}
void kslicer::PrintWarning(const std::string& a_msg, const clang::SourceRange& a_range, const clang::SourceManager& a_sm)
{
const auto beginLoc = a_range.getBegin();
const auto inFileLoc = a_sm.getPresumedLoc(beginLoc);
const auto fileName = std::string(a_sm.getFilename(beginLoc));
const auto line = inFileLoc.getLine();
const auto col = inFileLoc.getColumn();
std::string code = GetRangeSourceCode(a_range, a_sm);
std::cout << fileName.c_str() << ":" << line << ":" << col << ": warning: " << a_msg << " --> " << code.c_str() << std::endl;
}
void kslicer::ExtractTypeAndVarNameFromConstructor(clang::CXXConstructExpr* constructExpr, clang::ASTContext* astContext, std::string& varName, std::string& typeName)
{
// (0) очищаем строки
//
varName = "";
typeName = "";
// (1) Получаем имя типа
//
clang::CXXConstructorDecl* ctor = constructExpr->getConstructor();
typeName = ctor->getNameInfo().getName().getAsString();
// (2) Получаем имя переменной
clang::DynTypedNodeList parents = astContext->getParents(*constructExpr);
for (const clang::DynTypedNode& parent : parents) {
if (const clang::VarDecl* varDecl = parent.get<clang::VarDecl>()) {
varName = varDecl->getNameAsString();
break;
}
}
}
const clang::Expr* kslicer::RemoveImplicitCast(const clang::Expr* nextNode)
{
if(nextNode == nullptr)
return nullptr;
while(clang::isa<clang::ImplicitCastExpr>(nextNode))
{
auto cast = dyn_cast<const clang::ImplicitCastExpr>(nextNode);
nextNode = cast->getSubExpr();
}
return nextNode;
}
clang::Expr* kslicer::RemoveImplicitCast(clang::Expr* nextNode)
{
if(nextNode == nullptr)
return nullptr;
while(clang::isa<clang::ImplicitCastExpr>(nextNode))
{
auto cast = dyn_cast<clang::ImplicitCastExpr>(nextNode);
nextNode = cast->getSubExpr();
}
return nextNode;
}
std::string kslicer::CutOffFileExt(const std::string& a_filePath)
{
const size_t lastindex = a_filePath.find_last_of(".");
if(lastindex != std::string::npos)
return a_filePath.substr(0, lastindex);
else
return a_filePath;
}
std::string kslicer::CutOffStructClass(const std::string& a_typeName)
{
auto spacePos = a_typeName.find_last_of(" ");
if(spacePos != std::string::npos)
return a_typeName.substr(spacePos+1);
return a_typeName;
}
void MakeAbsolutePathRelativeTo(std::filesystem::path& a_filePath, const std::filesystem::path& a_folderPath)
{
a_filePath = std::filesystem::relative(a_filePath, a_folderPath);
}
std::string ToLowerCase(std::string a_str)
{
std::transform(a_str.begin(), a_str.end(), a_str.begin(), [](unsigned char c){ return std::tolower(c); });
return a_str;
}
std::unordered_set<std::string> kslicer::GetAllServiceKernels()
{
std::unordered_set<std::string> names;
names.insert("copyKernelFloat");
names.insert("matMulTranspose");
return names;
}
std::string kslicer::ExtractSizeFromArgExpression(const std::string& a_str)
{
auto posOfPlus = a_str.find("+");
auto posOfEnd = a_str.find(".end()");
if(posOfPlus != std::string::npos)
{
std::string sizeExpr = a_str.substr(posOfPlus+1);
ReplaceFirst(sizeExpr," ", "");
return sizeExpr;
}
else if(posOfEnd != std::string::npos)
{
return a_str.substr(0, posOfEnd) + ".size()";
}
return a_str;
}
std::string kslicer::ClearNameFromBegin(const std::string& a_str)
{
auto posOfBeg = a_str.find(".begin()");
if(posOfBeg != std::string::npos)
return a_str.substr(0, posOfBeg);
return a_str;
}
std::string kslicer::FixLamdbaSourceCode(std::string a_str)
{
while(ReplaceFirst(a_str, "uint32_t ", "uint "));
while(ReplaceFirst(a_str, "unsigned int ", "uint "));
while(ReplaceFirst(a_str, "uint2 ", "uvec2 "));
while(ReplaceFirst(a_str, "uint3 ", "uvec3 "));
while(ReplaceFirst(a_str, "uint4 ", "uvec4 "));
while(ReplaceFirst(a_str, "int2 ", "ivec2 "));
while(ReplaceFirst(a_str, "int3 ", "ivec3 "));
while(ReplaceFirst(a_str, "int4 ", "ivec4 "));
while(ReplaceFirst(a_str, "float2 ", "vec2 "));
while(ReplaceFirst(a_str, "float3 ", "vec3 "));
while(ReplaceFirst(a_str, "float4 ", "vec4 "));
return a_str;
}
std::string kslicer::SubstrBetween(const std::string& a_str, const std::string& first, const std::string& second)
{
auto pos1 = a_str.find(first);
auto pos2 = a_str.find(second);
if(pos1 != std::string::npos && pos2 != std::string::npos)
return a_str.substr(pos1+1, pos2 - pos1 - 1);
return a_str;
}
bool kslicer::IsTexture(clang::QualType a_qt)
{
if(a_qt->isReferenceType())
a_qt = a_qt.getNonReferenceType();
auto typeDecl = a_qt->getAsRecordDecl();
if(typeDecl == nullptr || !clang::isa<clang::ClassTemplateSpecializationDecl>(typeDecl))
return false;
const std::string typeName = a_qt.getAsString();
return (typeName.find("Texture") != std::string::npos || typeName.find("Image") != std::string::npos);
}
bool kslicer::IsAccelStruct(const std::string& a_typeName)
{
if( (a_typeName == "struct ISceneObject") || (a_typeName == "ISceneObject") ||
(a_typeName == "struct ISceneObject2") || (a_typeName == "ISceneObject2") )
return true;
else if(a_typeName.find("ISceneObject_") != std::string::npos)
return true;
else
return false;
}
bool kslicer::IsVectorContainer(const std::string& a_typeName)
{
return (a_typeName == "vector") || (a_typeName == "std::vector") || (a_typeName == "cvex::vector");
}
bool kslicer::IsPointerContainer(const std::string& a_typeName)
{
return (a_typeName == "shared_ptr") || (a_typeName == "unique_ptr") ||
(a_typeName == "std::shared_ptr") || (a_typeName == "std::unique_ptr");
}
std::string kslicer::MakeKernellCallSignature(const std::string& a_mainFuncName, const std::vector<ArgReferenceOnCall>& a_args, const std::map<std::string, UsedContainerInfo>& a_usedContainers)
{
std::stringstream strOut;
for(const auto& arg : a_args)
{
switch(arg.argType)
{
case KERN_CALL_ARG_TYPE::ARG_REFERENCE_LOCAL:
strOut << "[L]";
break;
case KERN_CALL_ARG_TYPE::ARG_REFERENCE_ARG:
strOut << "[A][" << a_mainFuncName.c_str() << "]" ;
break;
case KERN_CALL_ARG_TYPE::ARG_REFERENCE_CLASS_VECTOR:
strOut << "[V]";
break;
case KERN_CALL_ARG_TYPE::ARG_REFERENCE_CLASS_POD:
strOut << "[P]";
break;
case KERN_CALL_ARG_TYPE::ARG_REFERENCE_THREAD_ID:
strOut << "[T]";
break;
default:
strOut << "[U]";
break;
};
strOut << arg.name.c_str();
}
for(const auto& vecName : a_usedContainers)
strOut << "[MV][" << vecName.second.name.c_str() << "]";
return strOut.str();
}
std::vector<kslicer::NameFlagsPair> kslicer::ListAccessedTextures(const std::vector<kslicer::ArgReferenceOnCall>& args, const kslicer::KernelInfo& kernel)
{
std::vector<kslicer::NameFlagsPair> accesedTextures;
accesedTextures.reserve(16);
for(uint32_t i=0;i<uint32_t(args.size());i++)
{
if(args[i].isTexture())
{
std::string argNameInKernel = kernel.args[i].name;
auto pFlags = kernel.texAccessInArgs.find(argNameInKernel);
kslicer::NameFlagsPair tex;
tex.name = args[i].name;
if(pFlags != kernel.texAccessInArgs.end())
tex.flags = pFlags->second;
tex.isArg = true;
tex.argId = i;
accesedTextures.push_back(tex);
}
}
for(const auto& container : kernel.usedContainers)
{
if(container.second.isTexture())
{
auto pFlags = kernel.texAccessInMemb.find(container.second.name);
kslicer::NameFlagsPair tex;
tex.name = container.second.name;
if(pFlags != kernel.texAccessInMemb.end())
tex.flags = pFlags->second;
else
tex.flags = kslicer::TEX_ACCESS::TEX_ACCESS_SAMPLE;
tex.isArg = false;
accesedTextures.push_back(tex);
}
}
return accesedTextures;
}
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::unordered_map<std::string, std::string> ReadCommandLineParams(int argc, const char** argv,
std::unordered_map<std::string, std::string>& defines,
std::filesystem::path& fileName,
std::vector<std::string>& allFiles,
std::vector<std::string>& ignoreFiles,
std::vector<std::string>& processFiles,
std::vector<std::string>& cppIncludes)
{
std::unordered_map<std::string, std::string> cmdLineParams;
for(int i=0; i<argc; i++)
{
std::string key(argv[i]);
const bool isDefine = key.size() > 1 && key.substr(0,2) == "-D";
const bool isKey = key.size() > 0 && key[0] == '-';
if(key == "-ignore")
ignoreFiles.push_back(argv[i+1]);
else if (key == "-process")
processFiles.push_back(argv[i+1]);
else if (key == "-cpp_include")
cppIncludes.push_back(argv[i+1]);
else if(key != "-v" && !isDefine && isKey) // exclude special "-IfoldePath" form, exclude "-v"
{
if(i != argc-1) // not last argument
{
cmdLineParams[key] = argv[i+1];
i++;
}
else
cmdLineParams[key] = "";
}
else if(key.find(".cpp") != std::string::npos)
allFiles.push_back(key);
else if(isDefine)
{
std::string name, value;
size_t spacePos = key.find(' ', 2);
if (spacePos != std::string::npos)
{
// Извлекаем имя определения (после -D и до пробела)
name = key.substr(2, spacePos - 2);
// Извлекаем значение (после пробела)
value = key.substr(spacePos + 1);
}
else
{
// Если пробела нет, вся строка после -D считается именем, значение пустое
name = key.substr(2);
value = "";
}
if(name != "")
defines[name] = value;
}
}
if(allFiles.size() == 0)
{
std::cout << "[main]: no input file is specified " << std::endl;
exit(0);
}
//else if(allFiles.size() == 1)
// fileName = allFiles[0];
else
{
fileName = allFiles[0];
// merge files to a single temporary file
auto folderPath = fileName.parent_path();
auto fileName2 = fileName.filename();
fileName2.replace_extension("");
if (cmdLineParams.find("-temp_suffix") != cmdLineParams.end())
{
std::cout << "[kslicer] Found custom temp file suffix \"" << cmdLineParams["-temp_suffix"] << "\"" << std::endl;
fileName2.concat("_" + cmdLineParams["-temp_suffix"] + "_temp.cpp");
}
else {
fileName2.concat("_temp.cpp");
}
auto fileNameT = folderPath / fileName2;
std::cout << "[kslicer]: merging input files to temporary file " << fileName2 << std::endl;
std::ofstream fout(fileNameT);
for(auto def : defines)
fout << "#define " << def.first << " " << def.second << std::endl;
auto pExpand = cmdLineParams.find("-expand_macro");
if(pExpand != cmdLineParams.end() && (pExpand->second == "1" || pExpand->second == "true")) // merging with macro expansion
{
std::cout << "[kslicer]: expand multiline macro defs; please note kslicer calls separate clang installation!" << std::endl;
std::vector<std::string> linesWithIncludes;
linesWithIncludes.reserve(64);
for(auto file : allFiles)
{
std::ifstream fin(file);
std::string line;
// (1) extract includes
//
int lastInclude = 0;
int lineNumber = 0;
linesWithIncludes.clear();
while (std::getline(fin, line))
{
if(line.find("#include") != std::string::npos)
{
linesWithIncludes.push_back(line);
lastInclude = lineNumber;
}
lineNumber++;
}
// (1) extract code and process it with clang++ to expand macro definitions
//
std::string tempFileName1 = file + "_temp1.cpp";
std::string tempFileName2 = file + "_temp2.cpp";
std::ofstream tempOutForExpand(tempFileName1.c_str());
fin.close();
fin.open(file);
//std::cout << "[kslicer]: seekg is failed! " << std::endl;
lineNumber = 0;
while (std::getline(fin, line))
{
if(lineNumber > lastInclude)
tempOutForExpand << line.c_str() << std::endl;
lineNumber++;
}
std::string commandLine = std::string("clang++ ") + tempFileName1 + " -E -o " + tempFileName2;
tempOutForExpand.close();
std::cout << "[kslicer]: exec '" << commandLine.c_str() << "'" << std::endl;
int result = std::system(commandLine.c_str());
if(result != 0)
std::cout << "[kslicer]: exec FAILED! : " << std::endl;
// (3) join files
//
fout << "////////////////////////////////////////////////////" << std::endl;
fout << "//// input file: " << file << std::endl;
fout << "////////////////////////////////////////////////////" << std::endl;
for(auto line : linesWithIncludes)
fout << line.c_str() << std::endl;
std::ifstream fin2(tempFileName2);
while (std::getline(fin2, line)) {
if(line[0] == '#')
continue;
fout << line.c_str() << std::endl;
}
std::filesystem::remove(tempFileName1);
std::filesystem::remove(tempFileName2);
}
}
else // normal merging
{
for(auto file : allFiles)
{
fout << "////////////////////////////////////////////////////" << std::endl;
fout << "//// input file: " << file << std::endl;
fout << "////////////////////////////////////////////////////" << std::endl;
std::ifstream fin(file);
std::string line;
while (std::getline(fin, line))
fout << line.c_str() << std::endl;
}
}
fout.close();
fileName = std::filesystem::absolute(fileNameT);
std::cout << "[kslicer]: merging finished to '" << fileName.c_str() << "'" << std::endl;
}
return cmdLineParams;
}
std::vector<const char*> ExcludeSlicerParams(int argc, const char** argv, const std::unordered_map<std::string,std::string>& params, const char* a_mainFileName, const std::unordered_map<std::string,std::string>& defines)
{
std::unordered_set<std::string> values;
for(auto p : params)
values.insert(p.second);
bool foundDSlicer = false;
bool foundMainFile = false;
static std::vector<std::string> g_data;
g_data.reserve(128);
std::vector<const char*> argsForClang; // exclude our input from cmdline parameters and pass the rest to clang
argsForClang.reserve(argc);
for(int i=1;i<argc;i++)
{
if(params.find(argv[i]) == params.end() && values.find(argv[i]) == values.end())
argsForClang.push_back(argv[i]);
if(std::string(argv[i]) == "-DKERNEL_SLICER")
foundDSlicer = true;
else if(std::string(argv[i]) == a_mainFileName)
foundMainFile = true;
}
if(!foundMainFile)
{
argsForClang.insert(argsForClang.begin(), a_mainFileName);
}
if(!foundDSlicer)
argsForClang.push_back("-DKERNEL_SLICER");
for(auto def : defines)
{
std::string name = def.first;
if(name.find("-D") != 0)
name = std::string("-D") + name;
g_data.push_back(name);
argsForClang.push_back(g_data.back().c_str());
if(def.second != "")
{
g_data.push_back(def.second);
argsForClang.push_back(g_data.back().c_str());
}
}
return argsForClang;
}
const char* GetClangToolingErrorCodeMessage(int code)
{
if(code == 0)
return "OK";
else if (code == 1)
return "ERROR";
else
return "SKIPPED_FILES";
}
void ReadThreadsOrderFromStr(const std::string& threadsOrderStr, uint32_t threadsOrder[3])
{
auto size = std::min<size_t>(threadsOrderStr.size(), 3);
for(size_t symbId = 0; symbId < size; symbId++)
{
switch(threadsOrderStr[symbId])
{
case 'x':
case 'X':
case '0':
threadsOrder[symbId] = 0;
break;
case 'y':
case 'Y':
case '1':
threadsOrder[symbId] = 1;
break;
case 'z':
case 'Z':
case '2':
threadsOrder[symbId] = 2;
break;
default:
break;
};
}
}