-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkslicer_main.cpp
1458 lines (1243 loc) · 60.2 KB
/
kslicer_main.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
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#include <stdio.h>
#include <vector>
#include <system_error>
#include <iostream>
#include <fstream>
#include <filesystem>
#include <unordered_map>
#include <iomanip>
#include <cctype>
#include <queue>
#include "llvm/TargetParser/Host.h"
#include "llvm/Support/raw_ostream.h"
#include "llvm/ADT/IntrusiveRefCntPtr.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Support/FileSystem.h"
#include "clang/Basic/DiagnosticOptions.h"
#include "clang/Frontend/TextDiagnosticPrinter.h"
#include "clang/Frontend/CompilerInstance.h"
#include "clang/Basic/TargetOptions.h"
#include "clang/Basic/TargetInfo.h"
#include "clang/Basic/FileManager.h"
#include "clang/Basic/SourceManager.h"
#include "clang/Lex/Preprocessor.h"
#include "clang/Lex/Lexer.h"
#include "clang/Lex/PreprocessorOptions.h"
#include "clang/Basic/Diagnostic.h"
#include "clang/AST/RecursiveASTVisitor.h"
#include "clang/AST/ASTConsumer.h"
#include "clang/AST/ASTTypeTraits.h"
#include "clang/AST/DeclTemplate.h"
#include "clang/Parse/ParseAST.h"
#include "clang/Rewrite/Frontend/Rewriters.h"
#include "clang/Rewrite/Core/Rewriter.h"
#include "clang/Tooling/CommonOptionsParser.h"
#include "kslicer.h"
#include "kslicer_warnings.h"
#include "initial_pass.h"
#include "ast_matchers.h"
#include "class_gen.h"
#include "extractor.h"
//using namespace clang;
#include "template_rendering.h"
using kslicer::KernelInfo;
using kslicer::DataMemberInfo;
std::vector<std::string> ListProcessedFiles(nlohmann::json a_filesArray, const std::string& a_optionsPath)
{
std::vector<std::string> allFiles;
if(!a_filesArray.is_array())
return allFiles;
std::filesystem::path optionsPath(a_optionsPath);
std::filesystem::path optionsFolder = optionsPath.parent_path();
for(const auto& param : a_filesArray)
{
std::filesystem::path path = param;
if(path.is_absolute())
allFiles.push_back(path.string());
else
{
std::filesystem::path fullPath = optionsFolder / path;
allFiles.push_back(fullPath.string());
}
}
return allFiles;
}
int main(int argc, const char **argv) //
{
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::cout << "[main]: work_dir = " << std::filesystem::current_path() << std::endl;
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
if (argc < 2)
{
std::cout << "Normal usage: kslicer <config.json> or kslicer -config <config.json> " << std::endl;
return 1;
}
std::cout << "CMD LINE: " << std::endl;
for(int i=0;i<argc;i++)
std::cout << i << ": " << argv[i] << std::endl;
std::cout << std::endl;
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
// try to find config file
//
std::string optionsPath = "";
{
for(int argId = 1; argId < argc; argId++ )
{
if((std::string(argv[argId]) == "-options" || std::string(argv[argId]) == "-config") && argId+1 < argc)
{
optionsPath = argv[argId+1];
break;
}
}
if(std::string(argv[1]).find(".json") != std::string::npos)
optionsPath = argv[1];
}
nlohmann::json inputOptions;
std::ifstream ifs(optionsPath);
if(optionsPath == "")
std::cout << "[main]: config is missing, which is ok in general" << std::endl;
else if(!ifs.is_open())
std::cout << "[main]: warning, config is not found at '" << optionsPath.c_str() << "', is it ok?" << std::endl;
else
inputOptions = nlohmann::json::parse(ifs, nullptr, true, true);
auto paramsFromConfig = inputOptions["options"];
std::vector<std::string> allFiles = ListProcessedFiles(inputOptions["source"], optionsPath);
std::vector<std::string> ignoreFiles;
std::vector<std::string> processFiles;
std::vector<std::string> cppIncludesAdditional;
std::filesystem::path fileName;
auto paramsFromCmdLine = ReadCommandLineParams(argc, argv, fileName, // ==>
allFiles, ignoreFiles, processFiles, cppIncludesAdditional); // <==
std::unordered_map<std::string, std::string> params;
{
for(const auto& param : paramsFromConfig.items()) // take params initially from config
params[param.key()] = param.value().is_string() ? param.value().get<std::string>() : param.value().dump();
for(const auto& param : paramsFromCmdLine) // and overide them from command line
params[param.first] = param.second;
}
auto mainClassNode = inputOptions["mainClass"];
if(mainClassNode.is_string())
params["-mainClass"] = mainClassNode.get<std::string>();
std::string composeAPIName = "";
std::string composeImplName = "";
std::vector<std::string> composeIntersections;
{
auto composClassNodes = inputOptions["composClasses"];
for(auto composNode : composClassNodes) {
composeAPIName = composNode["interface"];
composeImplName = composNode["implementation"];
break; // currently support only single composition
}
}
std::filesystem::path mainFolderPath = fileName.parent_path();
std::string mainClassName = "TestClass";
std::string stdlibFolder = "";
std::string patternName = "rtv";
std::string shaderCCName = "clspv";
std::string suffix = "_Generated";
std::string shaderFolderPrefix = "";
uint32_t threadsOrder[3] = {0,1,2};
uint32_t warpSize = 32;
bool useCppInKernels = false;
bool halfFloatTextures = false;
bool useMegakernel = false;
bool usePersistentThreads = false;
auto defaultVkernelType = kslicer::VKERNEL_IMPL_TYPE::VKERNEL_SWITCH;
bool enableSubGroupOps = false;
int ispcThreadModel = 0;
bool ispcExplicitIndices = false;
bool genGPUAPI = false;
if(params.find("-mainClass") != params.end())
mainClassName = params["-mainClass"];
if(params.find("-stdlibfolder") != params.end())
stdlibFolder = params["-stdlibfolder"];
if(params.find("-pattern") != params.end())
patternName = params["-pattern"];
if(params.find("-reorderLoops") != params.end())
ReadThreadsOrderFromStr(params["-reorderLoops"], threadsOrder);
if(params.find("-shaderCC") != params.end())
shaderCCName = params["-shaderCC"];
if(params.find("-shaderFolderPrefix") != params.end())
shaderFolderPrefix = params["-shaderFolderPrefix"];
if(suffix == "_Generated" && (shaderCCName == "ispc") || (shaderCCName == "ISPC"))
suffix = "_ISPC";
if(params.find("-warpSize") != params.end())
warpSize = atoi(params["-warpSize"].c_str());
if(params.find("-enableSubgroup") != params.end())
enableSubGroupOps = atoi(params["-enableSubgroup"].c_str());
if(params.find("-halfTex") != params.end())
halfFloatTextures = (params["-halfTex"] == "1");
if(params.find("-megakernel") != params.end())
useMegakernel = (params["-megakernel"] == "1");
if(params.find("-persistent") != params.end())
usePersistentThreads = (params["-persistent"] == "1");
if(params.find("-cl-std=") != params.end())
useCppInKernels = params["-cl-std="].find("++") != std::string::npos;
else if(params.find("-cl-std") != params.end())
useCppInKernels = params["-cl-std"].find("++") != std::string::npos;
if(params.find("-vkernel_t=") != params.end())
{
if(params["-vkernel_t="] == "switch")
defaultVkernelType = kslicer::VKERNEL_IMPL_TYPE::VKERNEL_SWITCH;
else if(params["-vkernel_t="] == "indirect_dispatch")
defaultVkernelType = kslicer::VKERNEL_IMPL_TYPE::VKERNEL_INDIRECT_DISPATCH;
}
if(params.find("-ispc_threads") != params.end())
ispcThreadModel = atoi(params["-ispc_threads"].c_str());
if(params.find("-ispc_explicit_id") != params.end())
ispcExplicitIndices = (atoi(params["-ispc_explicit_id"].c_str()) == 1);
if(params.find("-composInterface") != params.end())
composeAPIName = params["-composInterface"];
if(params.find("-composImplementation") != params.end())
composeImplName = params["-composImplementation"];
if(params.find("-suffix") != params.end())
suffix = params["-suffix"];
if(params.find("-gen_gpu_api") != params.end())
genGPUAPI = atoi(params["-gen_gpu_api"].c_str());
kslicer::ShaderFeatures forcedFeatures;
{
if(params.find("-forceEnableHalf") != params.end())
forcedFeatures.useHalfType = (atoi(params["-forceEnableHalf"].c_str()) == 1);
if(params.find("-forceEnableInt8") != params.end())
forcedFeatures.useByteType = (atoi(params["-forceEnableInt8"].c_str()) == 1);
if(params.find("-forceEnableInt16") != params.end())
forcedFeatures.useShortType = (atoi(params["-forceEnableInt16"].c_str()) == 1);
if(params.find("-forceEnableInt64") != params.end())
forcedFeatures.useInt64Type = (atoi(params["-forceEnableInt64"].c_str()) == 1);
}
kslicer::TextGenSettings textGenSettings;
{
if(params.find("-enable_motion_blur") != params.end())
textGenSettings.enableMotionBlur = atoi(params["-enable_motion_blur"].c_str()) != 0;
if(params.find("-force_ray_tracing_pipeline") != params.end())
{
bool isEnabled = (atoi(params["-force_ray_tracing_pipeline"].c_str()) != 0);
textGenSettings.enableRayGen = isEnabled;
textGenSettings.enableRayGenForce = isEnabled;
}
else if(params.find("-enable_ray_tracing_pipeline") != params.end())
textGenSettings.enableRayGen = (atoi(params["-enable_ray_tracing_pipeline"].c_str()) != 0) || textGenSettings.enableMotionBlur;
if(params.find("-enable_callable_shaders") != params.end()) // enable_callable_shaders
textGenSettings.enableCallable = (atoi(params["-enable_callable_shaders"].c_str()) != 0);
if(params.find("-timestamps") != params.end())
textGenSettings.enableTimeStamps = (atoi(params["-timestamps"].c_str()) != 0);
textGenSettings.genSeparateGPUAPI = genGPUAPI;
}
// include and process folders
//
std::vector<std::filesystem::path> ignoreFolders;
std::vector<std::filesystem::path> processFolders;
for(auto p : params)
{
std::string folderT = p.second;
std::transform(folderT.begin(), folderT.end(), folderT.begin(), [](unsigned char c){ return std::tolower(c); });
if(p.first.size() > 1 && p.first[0] == '-' && p.first[1] == 'I' && folderT == "ignore")
ignoreFolders.push_back(p.first.substr(2));
else if(p.first.size() > 1 && p.first[0] == '-' && p.first[1] == 'I' && folderT == "process")
processFolders.push_back(p.first.substr(2));
}
for(auto folder : inputOptions["includeProcess"])
{
if(!folder.is_string())
continue;
std::filesystem::path path(folder.get<std::string>());
if(!path.is_absolute())
path = std::filesystem::absolute(std::filesystem::path(optionsPath).parent_path() / path);
if(std::filesystem::exists(path) && std::filesystem::is_directory(path))
processFolders.push_back(path);
else
std::cout << "[main]: bad folder from 'includeProcess' list: " << path.c_str() << std::endl;
}
for(auto folder : inputOptions["includeIgnore"])
{
if(!folder.is_string())
continue;
std::filesystem::path path(folder.get<std::string>());
if(!path.is_absolute())
path = std::filesystem::absolute(std::filesystem::path(optionsPath).parent_path() / path);
if(std::filesystem::exists(path) && std::filesystem::is_directory(path))
ignoreFolders.push_back(path);
else
std::cout << "[main]: bad folder from 'includeIgnore' list: " << path.c_str() << std::endl;
}
// make specific checks to be sure user don't include these files to hit project as normal files
//
{
auto processFolders2 = processFolders;
processFolders2.push_back(mainFolderPath);
kslicer::CheckInterlanIncInExcludedFolders(processFolders2);
}
std::vector<const char*> argsForClang = ExcludeSlicerParams(argc, argv, params, fileName.c_str());
llvm::ArrayRef<const char*> args(argsForClang.data(), argsForClang.data() + argsForClang.size());
// Make sure it exists
std::ifstream fin(fileName.c_str());
if(!fin.is_open())
{
std::cout << "[main]: error, input file " << fileName.c_str() << " not found!" << std::endl;
return 0;
}
fin.close();
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::shared_ptr<kslicer::MainClassInfo> pImplPattern = nullptr;
if(patternName == "rtv")
pImplPattern = std::make_shared<kslicer::RTV_Pattern>();
else if(patternName == "ipv")
pImplPattern = std::make_shared<kslicer::IPV_Pattern>();
else
{
std::cout << "[main]: wrong pattern name '" << patternName.c_str() << "' " << std::endl;
exit(0);
}
kslicer::MainClassInfo& inputCodeInfo = *pImplPattern;
inputCodeInfo.ignoreFolders = ignoreFolders; // set shader folders
inputCodeInfo.processFolders = processFolders; // set common C/C++ folders
inputCodeInfo.ignoreFiles = ignoreFiles; // set exceptions for common C/C++ folders (i.e. processFolders)
inputCodeInfo.processFiles = processFiles; // set exceptions for shader folders (i.e. ignoreFolders)
inputCodeInfo.cppIncudes = cppIncludesAdditional;
inputCodeInfo.genGPUAPI = genGPUAPI;
inputCodeInfo.m_timestampPoolSize = textGenSettings.enableTimeStamps ? 0 : uint32_t(-1);
if(shaderCCName == "glsl" || shaderCCName == "GLSL")
{
inputCodeInfo.pShaderCC = std::make_shared<kslicer::GLSLCompiler>(inputCodeInfo.mainClassSuffix);
inputCodeInfo.pHostCC = std::make_shared<kslicer::VulkanCodeGen>();
inputCodeInfo.processFolders.push_back("include/");
}
else if(shaderCCName == "slang" || shaderCCName == "SLANG" || shaderCCName == "Slang" || shaderCCName == "cuda_slang")
{
inputCodeInfo.pShaderCC = std::make_shared<kslicer::SlangCompiler>(inputCodeInfo.mainClassSuffix);
if(shaderCCName == "cuda_slang")
inputCodeInfo.pHostCC = std::make_shared<kslicer::CudaCodeGen>();
else
inputCodeInfo.pHostCC = std::make_shared<kslicer::VulkanCodeGen>();
inputCodeInfo.processFolders.push_back("include/");
}
else if(shaderCCName == "cuda" || shaderCCName == "CUDA")
{
inputCodeInfo.pShaderCC = std::make_shared<kslicer::CudaCompiler>(inputCodeInfo.mainClassSuffix);
inputCodeInfo.pHostCC = std::make_shared<kslicer::CudaCodeGen>();
inputCodeInfo.ignoreFolders.push_back("include/");
}
else if(shaderCCName == "ispc" || shaderCCName == "ISPC")
{
inputCodeInfo.pShaderCC = std::make_shared<kslicer::ISPCCompiler>(useCppInKernels, inputCodeInfo.mainClassSuffix);
inputCodeInfo.pHostCC = std::make_shared<kslicer::ISPCCodeGen>();
inputCodeInfo.ignoreFolders.push_back("include/");
}
else
{
inputCodeInfo.pShaderCC = std::make_shared<kslicer::ClspvCompiler>(useCppInKernels, inputCodeInfo.mainClassSuffix);
inputCodeInfo.pHostCC = std::make_shared<kslicer::VulkanCodeGen>();
inputCodeInfo.ignoreFolders.push_back("include/");
}
inputCodeInfo.defaultVkernelType = defaultVkernelType;
inputCodeInfo.halfFloatTextures = halfFloatTextures;
inputCodeInfo.megakernelRTV = useMegakernel;
inputCodeInfo.persistentRTV = usePersistentThreads;
inputCodeInfo.mainClassSuffix = suffix;
inputCodeInfo.shaderFolderPrefix = shaderFolderPrefix;
inputCodeInfo.globalShaderFeatures = forcedFeatures;
// analize multiple definitions of args which can not be processed via hash-table params
//
std::vector<std::string> baseClases;
for(int argId = 1; argId < argc; argId++ )
{
if(std::string(argv[argId]) == "-intersectionShader" && argId+1 < argc) {
std::string shaderClassAndFunc = argv[argId+1];
auto splitPos = shaderClassAndFunc.find("::");
std::string className = shaderClassAndFunc.substr(0, splitPos);
std::string funcName = shaderClassAndFunc.substr(splitPos + 2);
inputCodeInfo.intersectionShaders.push_back( std::make_pair(className, funcName) );
}
else if(std::string(argv[argId]) == "-intersectionTriangle" && argId+1 < argc) {
const std::string className = argv[argId+1];
inputCodeInfo.intersectionTriangle.push_back( std::make_pair(className, className) );
}
else if(std::string(argv[argId]) == "-intersectionWhiteList" && argId+1 < argc) {
const std::string className = argv[argId+1];
inputCodeInfo.intersectionWhiteList.insert(className);
}
else if(std::string(argv[argId]) == "-intersectionBlackList" && argId+1 < argc) {
const std::string className = argv[argId+1];
inputCodeInfo.intersectionBlackList.insert(className);
}
else if (std::string(argv[argId]) == "-baseClass" && argId+1 < argc) {
baseClases.push_back(argv[argId+1]);
}
else if(std::string(argv[argId]) == "-with_buffer_reference" && argId+1 < argc)
{
const std::string buffName = argv[argId+1];
if(buffName == "all")
inputCodeInfo.withBufferReferenceAll = true;
else if(buffName != "")
inputCodeInfo.withBufferReference.insert(buffName);
}
else if(std::string(argv[argId]) == "-without_buffer_reference" && argId+1 < argc)
{
const std::string buffName = argv[argId+1];
if(buffName != "")
inputCodeInfo.withoutBufferReference.insert(buffName);
}
else if(std::string(argv[argId]) == "-typedef" && argId+1 < argc)
{
const std::string buffName = argv[argId+1];
std::stringstream strIn(buffName);
char original[64];
char replace[64];
strIn >> original >> replace;
inputCodeInfo.userTypedefs.push_back(std::make_pair(std::string(original), std::string(replace)));
}
else if(std::string(argv[argId]) == "-intersectionShaderPlace" && argId+1 < argc)
{
composeIntersections.push_back(argv[argId+1]);
}
}
for(auto base : inputOptions["baseClasses"]) {
if(base.is_string())
baseClases.push_back(base.get<std::string>());
}
// read compos classes, intersection shaders and e.t.c
{
auto composClassNodes = inputOptions["composClasses"];
for(auto composNode : composClassNodes) {
composeAPIName = composNode["interface"];
composeImplName = composNode["implementation"];
auto intersection = composNode["intersection"];
if(intersection != nullptr)
{
std::string className = intersection["interface"];
std::string funcName = intersection["shader"];
inputCodeInfo.intersectionShaders.push_back( std::make_pair(className, funcName) );
composeIntersections.push_back(composeAPIName);
composeIntersections.push_back(composeImplName);
if(intersection["triangle"] != nullptr) {
const std::string triClassName = intersection["triangle"];
inputCodeInfo.intersectionTriangle.push_back(std::make_pair(className, className));
}
if(intersection["whiteList"] != nullptr) {
for(auto node : intersection["whiteList"])
inputCodeInfo.intersectionWhiteList.insert(std::string(node));
}
if(intersection["blackList"] != nullptr) {
for(auto node : intersection["blackList"])
inputCodeInfo.intersectionBlackList.insert(std::string(node));
}
}
break; // currently support only single composition
}
}
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
/////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
std::unique_ptr<kslicer::MainClassInfo> pInputCodeInfoImpl = nullptr;
clang::CompilerInstance compiler;
clang::DiagnosticOptions diagnosticOptions;
compiler.createDiagnostics(); //compiler.createDiagnostics(argc, argv);
// Create an invocation that passes any flags to preprocessor
std::shared_ptr<clang::CompilerInvocation> Invocation = std::make_shared<clang::CompilerInvocation>();
clang::CompilerInvocation::CreateFromArgs(*Invocation, args, compiler.getDiagnostics());
compiler.setInvocation(Invocation);
// Set default target triple
std::shared_ptr<clang::TargetOptions> pto = std::make_shared<clang::TargetOptions>();
pto->Triple = llvm::sys::getDefaultTargetTriple();
clang::TargetInfo *pti = clang::TargetInfo::CreateTargetInfo(compiler.getDiagnostics(), pto);
compiler.setTarget(pti);
compiler.getLangOpts().GNUMode = 1;
compiler.getLangOpts().CXXExceptions = 1;
compiler.getLangOpts().RTTI = 1;
compiler.getLangOpts().Bool = 1;
compiler.getLangOpts().CPlusPlus = 1;
compiler.getLangOpts().CPlusPlus14 = 1;
compiler.getLangOpts().CPlusPlus17 = 1;
compiler.createFileManager();
compiler.createSourceManager(compiler.getFileManager());
/////////////////////////////////////////////////////////////////////////////////////////////////// -stdlibFolder
if(stdlibFolder == "")
{
std::filesystem::path currentPath = std::filesystem::current_path();
std::filesystem::path tinystl1Path = currentPath / std::filesystem::path("TINYSTL");
std::filesystem::path tinystl2Path = currentPath.parent_path() / std::filesystem::path("TINYSTL");
if(std::filesystem::exists(tinystl1Path) && std::filesystem::is_directory(tinystl1Path))
stdlibFolder = tinystl1Path.string();
else if(std::filesystem::exists(tinystl2Path) && std::filesystem::is_directory(tinystl2Path))
stdlibFolder = tinystl2Path.string();
}
auto alreadyFound = std::find(inputCodeInfo.ignoreFolders.begin(), inputCodeInfo.ignoreFolders.end(), stdlibFolder);
if(stdlibFolder != "" && alreadyFound == inputCodeInfo.ignoreFolders.end())
inputCodeInfo.ignoreFolders.push_back(stdlibFolder);
/////////////////////////////////////////////////////////////////////////////////////////////////// -stdlibFolder
// (0) add path dummy include files for STL and e.t.c. (we don't want to parse actually std library)
//
auto& headerSearchOptions = compiler.getHeaderSearchOpts();
headerSearchOptions.AddPath(stdlibFolder.c_str(), clang::frontend::Angled, false, false);
for(const auto& includePath : processFolders)
headerSearchOptions.AddPath(includePath.c_str(), clang::frontend::Angled, false, false);
for(const auto& includePath : ignoreFolders)
headerSearchOptions.AddPath(includePath.c_str(), clang::frontend::Angled, false, false);
//headerSearchOptions.Verbose = 1;
compiler.createPreprocessor(clang::TU_Complete);
compiler.getPreprocessorOpts().UsePredefines = true;
// register our header lister
HeaderLister headerLister(&inputCodeInfo);
compiler.getPreprocessor().addPPCallbacks(std::make_unique<HeaderLister>(headerLister));
compiler.createASTContext();
const clang::FileEntry *pFile = compiler.getFileManager().getFile(fileName.u8string()).get();
compiler.getSourceManager().setMainFileID( compiler.getSourceManager().createFileID( pFile, clang::SourceLocation(), clang::SrcMgr::C_User));
compiler.getDiagnosticClient().BeginSourceFile(compiler.getLangOpts(), &compiler.getPreprocessor());
// init clang tooling
//
const std::string filenameString = fileName.u8string();
std::vector<const char*> argv2 = {argv[0], filenameString.c_str()};
std::vector<std::string> extraArgs;
extraArgs.reserve(256);
for(auto p : params) {
if(p.first.size() > 1 && p.first[0] == '-' && p.first[1] == 'I') {
extraArgs.push_back(std::string("-extra-arg=") + p.first);
argv2.push_back(extraArgs.back().c_str());
}
}
for(const auto& includePath : processFolders) {
extraArgs.push_back(std::string("-extra-arg=") + std::string("-I") + includePath.string());
argv2.push_back(extraArgs.back().c_str());
}
for(const auto& includePath : ignoreFolders) {
extraArgs.push_back(std::string("-extra-arg=") + std::string("-I") + includePath.string());
argv2.push_back(extraArgs.back().c_str());
}
//if(optionsPath != "")
{
extraArgs.push_back(std::string("-extra-arg=") + std::string("-I") + stdlibFolder);
argv2.push_back(extraArgs.back().c_str());
}
argv2.push_back("--");
int argSize = argv2.size();
llvm::cl::OptionCategory GDOpts("global-detect options");
auto OptionsParser = clang::tooling::CommonOptionsParser::create(argSize, argv2.data(), GDOpts); // clang 14
clang::tooling::ClangTool Tool(OptionsParser->getCompilations(), OptionsParser->getSourcePathList()); // clang 14
// (0) find all "Main" functions, a functions which call kernels. Kernels are also listed for each mainFunc;
//
std::vector<std::string> cfNames;
cfNames.reserve(20);
std::cout << "(0) Listing main functions of " << mainClassName.c_str() << std::endl;
auto cfList = kslicer::ListAllMainRTFunctions(Tool, mainClassName, compiler.getASTContext(), inputCodeInfo);
std::cout << "{" << std::endl;
for(const auto& f : cfList)
{
std::cout << " found " << f.first.c_str() << std::endl;
cfNames.push_back(f.first);
}
std::cout << "}" << std::endl;
std::cout << std::endl;
inputCodeInfo.mainFunc.resize(cfList.size());
inputCodeInfo.mainClassName = mainClassName;
inputCodeInfo.mainClassFileName = fileName;
// create default Shader Rewriter, don't delete it please!
//
clang::Rewriter rewrite2;
rewrite2.setSourceMgr(compiler.getSourceManager(), compiler.getLangOpts());
inputCodeInfo.pShaderFuncRewriter = inputCodeInfo.pShaderCC->MakeFuncRewriter(rewrite2, compiler, &inputCodeInfo);
// Parse code, initial pass
//
std::cout << "(1) Processing class '" << mainClassName.c_str() << "' with initial pass" << std::endl;
std::cout << "{" << std::endl;
std::vector<std::string> composClassNames;
{
if(composeAPIName != "")
composClassNames.push_back(composeAPIName);
if(composeImplName != "")
composClassNames.push_back(composeImplName);
if(composeAPIName != "ISceneObject" && composeAPIName.find("ISceneObject") != std::string::npos) // need to add 'ISceneObject' if ISceneObject2 or ISceneObject_LiteRT or sms like that is used for API
composClassNames.push_back("ISceneObject");
}
kslicer::InitialPassASTConsumer firstPassData(cfNames, mainClassName, composClassNames, compiler, inputCodeInfo);
// TODO: move this inside constructor ...
{
for(size_t i=0;i<baseClases.size();i++)
{
std::string className = baseClases[i];
firstPassData.rv.m_baseClassInfo[className] = kslicer::ClassInfo(className);
firstPassData.rv.m_baseClassInfo[className].baseClassOrder = int(i);
}
}
ParseAST(compiler.getPreprocessor(), &firstPassData, compiler.getASTContext());
if(firstPassData.rv.mci.astNode == nullptr)
{
std::cout << " [main]: critical error, main class '" << mainClassName.c_str() << "' not found" << std::endl;
return 0;
}
// вызов compiler.getDiagnosticClient().EndSourceFile()
// обеспечивает корректное завершение обработки диагностических сообщений
// для текущего исходного файла в процессе компиляции с использованием Clang.
compiler.getDiagnosticClient().EndSourceFile();
auto pComposAPI = firstPassData.rv.m_composedClassInfo.find(composeAPIName);
auto pComposImpl = firstPassData.rv.m_composedClassInfo.find(composeImplName);
if(pComposAPI != firstPassData.rv.m_composedClassInfo.end() && pComposImpl != firstPassData.rv.m_composedClassInfo.end()) // if compos classes are found
{
std::string composMemberName = kslicer::PerformClassComposition(firstPassData.rv.mci, pComposAPI->second, pComposImpl->second); // perform class composition
for(const auto& name : composClassNames)
inputCodeInfo.composPrefix[name] = composMemberName;
inputCodeInfo.composClassNames.insert(composeImplName);
for(auto intersectionPlace : composeIntersections)
inputCodeInfo.composIntersection.insert(intersectionPlace);
}
else if(baseClases.size() != 0)
{
std::vector<const clang::CXXRecordDecl*> aux_classes;
aux_classes.reserve(firstPassData.rv.m_composedClassInfo.size());
// Преобразование unordered_map в vector
std::transform(firstPassData.rv.m_baseClassInfo.begin(), firstPassData.rv.m_baseClassInfo.end(),
std::back_inserter(aux_classes), [](const auto& pair) { return pair.second.astNode; });
auto sorted = kslicer::ExtractAndSortBaseClasses(aux_classes, firstPassData.rv.mci.astNode);
for(size_t classOrder = 0; classOrder < sorted.size(); classOrder++) {
const auto& baseClass = sorted[classOrder];
auto typeName = baseClass->getQualifiedNameAsString();
const auto& classInfo = firstPassData.rv.m_baseClassInfo[typeName]; // !!!!!
kslicer::PerformInheritanceMerge(firstPassData.rv.mci, classInfo);
inputCodeInfo.mainClassNames[typeName] = int(classOrder) + 1;
}
}
inputCodeInfo.mainClassNames[inputCodeInfo.mainClassName] = 0; // put main (derived) class name in this hash-set, use 'mainClassNames' instead of 'mainClassName' later
// merge mainClassNames and composClassNames in single array, add 'const Type' names to it; TODO: merge to single function
{
inputCodeInfo.dataClassNames.clear();
//inputCodeInfo.dataClassNames.insert(inputCodeInfo.mainClassNames.begin(), inputCodeInfo.mainClassNames.end());
for(auto c : inputCodeInfo.mainClassNames)
inputCodeInfo.dataClassNames.insert(c.first);
inputCodeInfo.dataClassNames.insert(inputCodeInfo.composClassNames.begin(), inputCodeInfo.composClassNames.end());
inputCodeInfo.dataClassNames.insert("ISceneObject"); // TODO: list all base classes for compose classes
inputCodeInfo.dataClassNames.insert("ISceneObject2"); // TODO: list all base classes for compose classes
std::vector<std::string> constVarianst;
constVarianst.reserve(inputCodeInfo.dataClassNames.size());
for(auto name : inputCodeInfo.dataClassNames) {
constVarianst.push_back(name + "*");
constVarianst.push_back(name + " *");
constVarianst.push_back(name + "&");
constVarianst.push_back(name + " &");
constVarianst.push_back(std::string("const ") + name);
constVarianst.push_back(std::string("const ") + name + "*");
constVarianst.push_back(std::string("const ") + name + " *");
constVarianst.push_back(std::string("const ") + name + "&");
constVarianst.push_back(std::string("const ") + name + " &");
}
for(auto name : constVarianst)
inputCodeInfo.dataClassNames.insert(name);
}
inputCodeInfo.mainClassFileInclude = firstPassData.rv.MAIN_FILE_INCLUDE;
inputCodeInfo.mainClassASTNode = firstPassData.rv.mci.astNode;
inputCodeInfo.allKernels = firstPassData.rv.mci.funKernels;
inputCodeInfo.allDataMembers = firstPassData.rv.mci.dataMembers;
inputCodeInfo.ctors = firstPassData.rv.mci.ctors;
inputCodeInfo.allMemberFunctions = firstPassData.rv.mci.funMembers;
inputCodeInfo.ProcessAllSetters(firstPassData.rv.mci.m_setters, compiler);
std::vector<kslicer::DeclInClass> generalDecls = firstPassData.rv.GetExtractedDecls();
if(inputCodeInfo.mainClassASTNode == nullptr)
{
std::cout << "[main]: ERROR, main class " << mainClassName.c_str() << " not found" << std::endl;
return 0;
}
std::cout << "}" << std::endl;
std::cout << std::endl;
std::cout << "(2) Process control functions; extract local variables, known calls like memcpy, sort, std::fill and other " << std::endl;
std::cout << "{" << std::endl;
size_t mainFuncId = 0;
for(const auto f : cfList)
{
const std::string& mainFuncName = f.first;
auto& mainFuncRef = inputCodeInfo.mainFunc[mainFuncId];
mainFuncRef.Name = mainFuncName;
mainFuncRef.Node = firstPassData.rv.mci.funControls[mainFuncName].astNode;
// Now process each main function: variables and kernel calls, if()->break and if()->return statements.
//
{
auto allMatchers = inputCodeInfo.ListMatchers_CF(mainFuncName);
auto pMatcherPrc = inputCodeInfo.MatcherHandler_CF(mainFuncRef, compiler);
clang::ast_matchers::MatchFinder finder;
for(auto& matcher : allMatchers)
finder.addMatcher(clang::ast_matchers::traverse(clang::TK_IgnoreUnlessSpelledInSource,matcher), pMatcherPrc.get());
std::cout << " process control function: " << mainFuncName.c_str() << "(...)" << std::endl;
auto res = Tool.run(clang::tooling::newFrontendActionFactory(&finder).get());
std::cout << " process control function: " << mainFuncName.c_str() << "(...) --> " << GetClangToolingErrorCodeMessage(res) << std::endl;
// filter out unused kernels
//
inputCodeInfo.kernels.reserve(inputCodeInfo.allKernels.size());
inputCodeInfo.kernels.clear();
for (auto& k : inputCodeInfo.allKernels)
{
if(k.second.usedInMainFunc && inputCodeInfo.kernels.find(k.first) == inputCodeInfo.kernels.end())
inputCodeInfo.kernels[k.first] = k.second;
}
// filter out excluded local variables
//
for(const auto& var : mainFuncRef.ExcludeList)
{
auto ex = mainFuncRef.Locals.find(var);
if(ex != mainFuncRef.Locals.end())
mainFuncRef.Locals.erase(ex);
}
}
mainFuncId++;
}
std::cout << "}" << std::endl;
std::cout << std::endl;
std::cout << "(3) Mark data members, methods and functions which are actually used in kernels." << std::endl;
std::cout << "{" << std::endl;
for(auto& nk : inputCodeInfo.kernels)
{
auto& kernel = nk.second;
kernel.warpSize = warpSize;
kernel.enableSubGroups = enableSubGroupOps;
kernel.singleThreadISPC = (ispcThreadModel == 1);
kernel.openMpAndISPC = (ispcThreadModel == 2);
kernel.explicitIdISPC = ispcExplicitIndices;
auto kernelMatchers = inputCodeInfo.ListMatchers_KF(kernel.name);
auto pFilter = inputCodeInfo.MatcherHandler_KF(kernel, compiler);
clang::ast_matchers::MatchFinder finder;
for(auto& matcher : kernelMatchers)
finder.addMatcher(clang::ast_matchers::traverse(clang::TK_IgnoreUnlessSpelledInSource, matcher), pFilter.get());
auto res = Tool.run(clang::tooling::newFrontendActionFactory(&finder).get());
std::cout << " process " << kernel.name.c_str() << ":\t" << GetClangToolingErrorCodeMessage(res) << std::endl;
for(auto& arg : kernel.args) // it is important to run this loop after second stage at which kernel matchers are applied!
inputCodeInfo.ProcessKernelArg(arg, kernel);
kernel.isIndirect = inputCodeInfo.IsIndirect(kernel);
if(kernel.isIndirect)
{
kernel.indirectBlockOffset = inputCodeInfo.m_indirectBufferSize;
inputCodeInfo.m_indirectBufferSize++;
}
inputCodeInfo.VisitAndPrepare_KF(kernel, compiler);
if(kernel.name.find("kernelBE") != std::string::npos)
inputCodeInfo.ProcessBlockExpansionKernel(kernel, compiler);
if(kernel.hasFinishPass) // add additional buffers for reduction
{
uint32_t buffNumber = 0;
for(auto& redVar : kernel.subjectedToReduction)
{
if(inputCodeInfo.pShaderCC->SupportAtomicGlobal(redVar.second))
continue;
std::stringstream strOut;
strOut << "tmpred" << buffNumber << redVar.second.GetSizeOfDataType();
inputCodeInfo.AddTempBufferToKernel(strOut.str(), redVar.second.dataType, kernel);
redVar.second.tmpVarName = strOut.str();
buffNumber++;
}
}
}
std::cout << "}" << std::endl;
std::cout << std::endl;
std::cout << "(4) Extract functions, constants and structs from 'MainClass' " << std::endl;
std::cout << "{" << std::endl;
std::vector<kslicer::FuncData> usedFunctions = kslicer::ExtractUsedFunctions(inputCodeInfo, compiler); // recursive processing of functions used by kernel, extracting all needed functions
//std::vector<kslicer::DeclInClass> usedDecls = kslicer::ExtractTCFromClass(inputCodeInfo.mainClassName, inputCodeInfo.mainClassASTNode, compiler, Tool);
std::vector<kslicer::DeclInClass> usedDecls;
for(auto name : inputCodeInfo.mainClassNames)
{
auto astNode = inputCodeInfo.allASTNodes.find(name.first);
if(astNode != inputCodeInfo.allASTNodes.end())
{
auto declsPerClass = kslicer::ExtractTCFromClass(name.first, astNode->second, compiler, Tool);
usedDecls.insert(usedDecls.end(), declsPerClass.begin(), declsPerClass.end());
}
}
for(const auto& usedDecl : usedDecls) // merge usedDecls with generalDecls
{
bool found = false;
for(const auto& currDecl : generalDecls)
{
if(currDecl.name == usedDecl.name)
{
found = true;
break;
}
}
if(!found)
generalDecls.push_back(usedDecl);
}
// process virtual functions
//
std::cout << " (4.0) Process Virtual-Functions-Hierarchies:" << std::endl;
std::unordered_map<const clang::FunctionDecl*, kslicer::FuncData> procesedFunctions;
for(auto f : usedFunctions)
procesedFunctions[f.astNode] = f;
size_t oldSizeOfFunctions = usedFunctions.size();
for(auto& k : inputCodeInfo.kernels)
{
bool hasVirtual = false;
for(const auto& f : k.second.usedMemberFunctions) {
if(f.second.isVirtual) {
hasVirtual = true;
break;
}
}
if(hasVirtual)
{
inputCodeInfo.ProcessVFH(firstPassData.rv.m_classList, compiler);
inputCodeInfo.ExtractVFHConstants(compiler, Tool);
auto sortedFunctions = kslicer::ExtractUsedFromVFH(inputCodeInfo, compiler, k.second.usedMemberFunctions);
for(auto f : sortedFunctions)
{
if(f.isMember)
continue;
if(procesedFunctions.find(f.astNode) == procesedFunctions.end())
{
procesedFunctions[f.astNode] = f;
usedFunctions.push_back(f);
}
}
inputCodeInfo.VisitAndPrepare_KF(k.second, compiler); // move data from usedContainersProbably to usedContainers if a kernel actually uses it
std::cout << std::endl;
}
}
if(usedFunctions.size() != oldSizeOfFunctions)
std::sort(usedFunctions.begin(), usedFunctions.end(), [](const auto& a, const auto& b) { return a.depthUse > b.depthUse; });
std::vector<std::string> usedDefines = kslicer::ExtractDefines(compiler);
std::cout << "}" << std::endl;
std::cout << std::endl;
inputCodeInfo.AddSpecVars_CF(inputCodeInfo.mainFunc, inputCodeInfo.kernels);
if(inputCodeInfo.pShaderCC->MemberFunctionsAreSupported()) // We don't implement this for OpenCL kernels yet ... or at all.
{
std::cout << "(4.1) Process Member function calls, extract data accesed in member functions " << std::endl;
std::cout << "{" << std::endl;
for(auto& k : inputCodeInfo.kernels)
{
std::vector<kslicer::FuncData> usedFunctionsCopy;
for(auto memberF : k.second.usedMemberFunctions) // (1) process member functions
usedFunctionsCopy.push_back(memberF.second);
usedFunctionsCopy.push_back(kslicer::FuncDataFromKernel(k.second)); // (2) process kernel in the same way as used member functions by this kernel
for(const auto& f : usedFunctionsCopy)
{
if(!f.isMember) // and if is called from this kernel.It it is called, list all input parameters for each call!
continue;
// list all input parameters for each call of member function inside kernel; in this way we know which textures, vectors and samplers were actually used by these functions
//
std::unordered_map<std::string, kslicer::UsedContainerInfo> auxContainers;
auto machedParams = kslicer::ArgMatchTraversal (&k.second, f, usedFunctions, inputCodeInfo, compiler);
auto usedMembers = kslicer::ExtractUsedMemberData(&k.second, f, usedFunctions, auxContainers, inputCodeInfo, compiler);
// TODO: process bindedParams correctly
//
std::vector<kslicer::DataMemberInfo> samplerMembers;
for(auto x : usedMembers)
{
if(kslicer::IsSamplerTypeName(x.second.type))
{
auto y = x.second;
y.kind = kslicer::DATA_KIND::KIND_SAMPLER;
samplerMembers.push_back(y);
}
}
for(auto& member : usedMembers)
{
k.second.usedMembers.insert(member.first);
member.second.usedInKernel = true;
if(kslicer::IsSamplerTypeName(member.second.type)) // actually this is not correct!!!
{
for(auto sampler : samplerMembers)
{
for(auto map : machedParams)
{
for(auto par : map)
{
std::string actualTextureName = par.second;
k.second.texAccessSampler[actualTextureName] = sampler.name;
}
}
}
}
else if(member.second.isContainer)
{
kslicer::UsedContainerInfo info;
info.type = member.second.type;