/
AddImplicitArgs.cpp
760 lines (655 loc) · 27.7 KB
/
AddImplicitArgs.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
/*===================== begin_copyright_notice ==================================
Copyright (c) 2017 Intel Corporation
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
======================= end_copyright_notice ==================================*/
#include "AddImplicitArgs.hpp"
#include "AdaptorCommon/ImplicitArgs.hpp"
#include "Compiler/IGCPassSupport.h"
#include "Compiler/CISACodeGen/CISACodeGen.h"
#include "Compiler/Optimizer/OCLBIUtils.h"
#include "LLVM3DBuilder/MetadataBuilder.h"
#include "common/LLVMWarningsPush.hpp"
#include "llvm/ADT/SCCIterator.h"
#include <llvm/IR/Module.h>
#include <llvmWrapper/IR/Function.h>
#include <llvm/IR/Instructions.h>
#include <llvm/IR/DerivedTypes.h>
#include "llvm/IR/DIBuilder.h"
#include "common/LLVMWarningsPop.hpp"
#include "Compiler/DebugInfo/VISADebugEmitter.hpp"
#include "common/debug/Debug.hpp"
#include <map>
#include <utility>
using namespace llvm;
using namespace IGC;
using namespace IGC::IGCMD;
// Register pass to igc-opt
#define PASS_FLAG "igc-add-implicit-args"
#define PASS_DESCRIPTION "Add implicit args to all functions in the module and adjusts call to these functions"
#define PASS_CFG_ONLY false
#define PASS_ANALYSIS false
IGC_INITIALIZE_PASS_BEGIN(AddImplicitArgs, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(AddImplicitArgs, PASS_FLAG, PASS_DESCRIPTION, PASS_CFG_ONLY, PASS_ANALYSIS)
char AddImplicitArgs::ID = 0;
AddImplicitArgs::AddImplicitArgs() : ModulePass(ID)
{
initializeAddImplicitArgsPass(*PassRegistry::getPassRegistry());
}
bool AddImplicitArgs::runOnModule(Module &M)
{
MapList<Function*, Function*> funcsMapping;
MapList<Function*, Function*> funcsMappingForReplacement;
m_pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
CodeGenContext* ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
// Update function signatures
// Create new functions with implicit args
for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I)
{
// Only handle functions defined in this module
Function* pFunc = &(*I);
if (pFunc->isDeclaration()) continue;
// skip non-entry functions
if (m_pMdUtils->findFunctionsInfoItem(pFunc) == m_pMdUtils->end_FunctionsInfo()) continue;
// see the detail in StatelessToStatefull.cpp.
// If SToSProducesPositivePointer is true, do not generate implicit arguments.
if (IGC_IS_FLAG_DISABLED(SToSProducesPositivePointer) &&
(ctx->getModuleMetaData()->compOpt.HasBufferOffsetArg ||
IGC_IS_FLAG_ENABLED(EnableSupportBufferOffset)))
{
ImplicitArgs::addBufferOffsetArgs(*pFunc, m_pMdUtils);
}
// Create the new function body and insert it into the module
ImplicitArgs implicitArgs(*pFunc, m_pMdUtils);
FunctionType *pNewFTy = getNewFuncType(pFunc, &implicitArgs);
Function* pNewFunc = Function::Create(pNewFTy, pFunc->getLinkage());
pNewFunc->copyAttributesFrom(pFunc);
pNewFunc->setSubprogram(pFunc->getSubprogram());
M.getFunctionList().insert(pFunc->getIterator(), pNewFunc);
pNewFunc->takeName(pFunc);
// Since we have now created the new function, splice the body of the old
// function right into the new function, leaving the old body of the function empty.
pNewFunc->getBasicBlockList().splice(pNewFunc->begin(), pFunc->getBasicBlockList());
// Loop over the argument list, transferring uses of the old arguments over to
// the new arguments
updateNewFuncArgs(pFunc, pNewFunc, &implicitArgs);
// Map old func to new func
funcsMapping[pFunc] = pNewFunc;
if (!pFunc->use_empty())
{
funcsMappingForReplacement[pFunc] = pNewFunc;
}
}
if (IGC_GET_FLAG_VALUE(FunctionControl) != FLAG_FCALL_FORCE_INLINE)
{
for (auto I : funcsMappingForReplacement)
{
replaceAllUsesWithNewOCLBuiltinFunction(ctx, I.first, I.second);
}
}
// Update IGC Metadata
// Function declarations are changing, this needs to be reflected in the metadata.
MetadataBuilder mbuilder(&M);
auto &FuncMD = ctx->getModuleMetaData()->FuncMD;
for (auto i : funcsMapping)
{
auto oldFuncIter = m_pMdUtils->findFunctionsInfoItem(i.first);
m_pMdUtils->setFunctionsInfoItem(i.second, oldFuncIter->second);
m_pMdUtils->eraseFunctionsInfoItem(oldFuncIter);
mbuilder.UpdateShadingRate(i.first, i.second);
auto loc = FuncMD.find(i.first);
if (loc != FuncMD.end())
{
auto funcInfo = loc->second;
FuncMD.erase(i.first);
FuncMD[i.second] = funcInfo;
}
}
m_pMdUtils->save(M.getContext());
//Return if any error
if (!(getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->oclErrorMessage.empty()))
{
return false;
}
// Go over all changed functions
for (MapList<Function*, Function*>::const_iterator I = funcsMapping.begin(), E = funcsMapping.end(); I != E; ++I)
{
Function* pFunc = I->first;
assert(pFunc->use_empty() && "Assume all user function are inlined at this point");
// Now, after changing funciton signature,
// and validate there are no calls to the old function we can erase it.
pFunc->eraseFromParent();
}
return true;
}
FunctionType* AddImplicitArgs::getNewFuncType(Function* pFunc, const ImplicitArgs* pImplicitArgs)
{
// Add all explicit parameters
FunctionType* pFuncType = pFunc->getFunctionType();
std::vector<Type *> newParamTypes(pFuncType->param_begin(), pFuncType->param_end());
// Add implicit arguments parameter types
for(unsigned int i = 0; i < pImplicitArgs->size(); ++i)
{
newParamTypes.push_back((*pImplicitArgs)[i].getLLVMType(pFunc->getContext()));
}
// Create new function type with explicit and implicit parameter types
return FunctionType::get( pFunc->getReturnType(),newParamTypes, pFunc->isVarArg());
}
void AddImplicitArgs::updateNewFuncArgs(llvm::Function* pFunc, llvm::Function* pNewFunc, const ImplicitArgs* pImplicitArgs)
{
// Loop over the argument list, transferring uses of the old arguments over to
// the new arguments, also transferring over the names as well.
Function::arg_iterator currArg = pNewFunc->arg_begin();
std::map<void*, unsigned int> argMap;
std::vector<std::pair<llvm::Instruction*, unsigned int>> newAddr;
bool fullDebugInfo = false;
IF_DEBUG_INFO(bool lineNumbersOnly = false;)
IF_DEBUG_INFO(CodeGenContext* ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();)
IF_DEBUG_INFO(DebugMetadataInfo::hasAnyDebugInfo(ctx, fullDebugInfo, lineNumbersOnly);)
if (fullDebugInfo)
{
unsigned int i = 0;
// Create a map storing function arguments of pFunc with their position
// of occurrence.
for (auto arg = pFunc->arg_begin(); arg != pFunc->arg_end(); ++arg, ++i)
{
argMap.insert(std::make_pair(&(*arg), i));
}
// Iterate over each dbg.declare intrinsic call. If the address operand
// matches with any argument from old function, pFunc, store it in a
// data structure so we can fix it later.
for (auto bb = pNewFunc->begin(); bb != pNewFunc->end(); ++bb)
{
for (auto inst = bb->begin(); inst != bb->end();)
{
auto DIInst = dyn_cast_or_null<DbgDeclareInst>(&(*inst));
if (DIInst)
{
{
auto addr = dyn_cast_or_null<Value>(DIInst->getAddress());
if (addr)
{
if (argMap.find(addr) != argMap.end())
{
newAddr.push_back(std::make_pair(DIInst, argMap.find(addr)->second));
}
}
}
}
++inst;
}
}
}
for (Function::arg_iterator I = pFunc->arg_begin(), E = pFunc->arg_end(); I != E; ++I, ++currArg)
{
llvm::Value* newArg = &(*currArg);
if ((*I).getType() != currArg->getType())
{
// fix opague type mismatch on %opencl.image...
std::string str0;
llvm::raw_string_ostream s(str0);
currArg->getType()->print(s);
StringRef argTypeName = s.str();
BasicBlock &entry = pNewFunc->getEntryBlock();
newArg = new llvm::BitCastInst(&(*currArg), (*I).getType(), "", &entry.front());
}
// Move the name and users over to the new version.
I->replaceAllUsesWith(newArg);
currArg->takeName(&(*I));
}
// In following loop, fix dbg.declare nodes that reference function arguments.
// This occurs for example when a struct type is passed as a kernel parameter
// byval. Bug#GD-429 had this exact issue. If we dont do this then we lose
// mapping of argument to dbg.declare and elf file comes up with empty
// storage location for the variable.
for (auto toReplace : newAddr)
{
unsigned int i = 0;
for (auto pNewFuncArg = pNewFunc->arg_begin(); pNewFuncArg != pNewFunc->arg_end(); ++pNewFuncArg, ++i)
{
if (i != toReplace.second)
continue;
IF_DEBUG_INFO(auto d = dyn_cast<DbgDeclareInst>(toReplace.first);)
llvm::DIBuilder Builder(*pNewFunc->getParent());
IF_DEBUG_INFO(auto DIVar = d->getVariable();)
Value* v = dyn_cast_or_null<Value>(&(*pNewFuncArg));
if (v)
{
IF_DEBUG_INFO(Builder.insertDeclare(v, DIVar, nullptr, d->getDebugLoc().get(), d);)
IF_DEBUG_INFO(auto oldInst = d;)
IF_DEBUG_INFO(oldInst->eraseFromParent();)
}
break;
}
}
// Set implict argument names
InfoToImpArgMap &infoToArg = m_FuncInfoToImpArgMap[pNewFunc];
ImpArgToExpNum &argImpToExpNum = m_FuncImpToExpNumMap[pNewFunc];
for (unsigned int i = 0; i < pImplicitArgs->size(); ++i, ++currArg)
{
ImplicitStructArgument info;
currArg->setName((*pImplicitArgs)[i].getName());
ImplicitArg::ArgType argId = (*pImplicitArgs)[i].getArgType();
info.DW0.All.argId = argId;
info.DW0.All.argExplictNum = 0;
info.DW0.All.argOffset = 0;
if (argId < ImplicitArg::ArgType::STRUCT_START)
{
infoToArg[info.DW0.Value] = &(*currArg);
}
else if (argId <= ImplicitArg::IMAGES_END || argId == ImplicitArg::ArgType::GET_OBJECT_ID || argId == ImplicitArg::ArgType::GET_BLOCK_SIMD_SIZE)
{
// struct, image
FunctionInfoMetaDataHandle funcInfo = m_pMdUtils->getFunctionsInfoItem(pFunc);
ArgInfoMetaDataHandle argInfo = funcInfo->getImplicitArgInfoListItem(i);
assert(argInfo->isExplicitArgNumHasValue() && "wrong data in MetaData");
info.DW0.All.argExplictNum = argInfo->getExplicitArgNum();
if (argId <= ImplicitArg::ArgType::STRUCT_END)
{
assert(argInfo->isStructArgOffsetHasValue() && "wrong data in MetaData");
info.DW0.All.argOffset = argInfo->getStructArgOffset();
}
infoToArg[info.DW0.Value] = &(*currArg);
argImpToExpNum[&(*currArg)] = info.DW0.All.argExplictNum;
}
else
{
infoToArg[info.DW0.Value] = &(*currArg);
}
}
}
void AddImplicitArgs::replaceAllUsesWithNewOCLBuiltinFunction(CodeGenContext* ctx, llvm::Function* old_func, llvm::Function* new_func)
{
assert(!old_func->use_empty());
FunctionInfoMetaDataHandle subFuncInfo = m_pMdUtils->getFunctionsInfoItem(old_func);
std::vector<Instruction*> list_delete;
for (auto U = old_func->user_begin(), UE = old_func->user_end(); U != UE; ++U)
{
std::vector<Value*> new_args;
CallInst *cInst = dyn_cast<CallInst>(*U);
auto BC = dyn_cast<BitCastInst>(*U);
if (BC && BC->hasOneUse())
cInst = dyn_cast<CallInst>(BC->user_back());
if (!cInst)
{
//assert(0 && " Not supported");
getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitError(" undefined reference to `jmp()' ");
return;
}
//Return if any error
if (!(getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->oclErrorMessage.empty()))
{
return;
}
Function *parent_func = cInst->getParent()->getParent();
size_t numArgOperands = cInst->getNumArgOperands();
// let 's prepare argument list on new call function
llvm::Function::arg_iterator new_arg_iter = new_func->arg_begin();
llvm::Function::arg_iterator new_arg_end = new_func->arg_end();
assert(IGCLLVM::GetFuncArgSize(new_func) >= numArgOperands);
// basic arguments
for (unsigned int i = 0; i < numArgOperands; ++i, ++new_arg_iter)
{
llvm::Value* arg = cInst->getOperand(i);
if (arg->getType() != new_arg_iter->getType())
{
// fix opague type mismatch on %opencl...
std::string str0;
llvm::raw_string_ostream s(str0);
arg->getType()->print(s);
StringRef argTypeName = s.str();
arg = new llvm::BitCastInst(arg, new_arg_iter->getType(), "", cInst);
}
new_args.push_back(arg);
}
// implicit arguments
int cImpCount = 0;
InfoToImpArgMap &infoToArg = m_FuncInfoToImpArgMap[parent_func];
ImpArgToExpNum &argImpToExpNum = m_FuncImpToExpNumMap[new_func];
while (new_arg_iter != new_arg_end)
{
ArgInfoMetaDataHandle argInfo = subFuncInfo->getImplicitArgInfoListItem(cImpCount);
ImplicitArg::ArgType argId = (ImplicitArg::ArgType)argInfo->getArgId();
ImplicitStructArgument info;
info.DW0.All.argId = argId;
info.DW0.All.argExplictNum = 0;
info.DW0.All.argOffset = 0;
if (argId < ImplicitArg::ArgType::STRUCT_START)
{
assert(infoToArg.find(info.DW0.Value) != infoToArg.end() &&
"Can't find the implicit argument on parent function");
new_args.push_back(infoToArg[info.DW0.Value]);
}
else if (argId <= ImplicitArg::ArgType::STRUCT_END)
{
assert(0 && "wrong argument type in user function");
}
else if (argId <= ImplicitArg::IMAGES_END || argId == ImplicitArg::ArgType::GET_OBJECT_ID || argId == ImplicitArg::ArgType::GET_BLOCK_SIMD_SIZE)
{
// special handling for image info types, such as ImageWidth, ImageHeight, ...
// and struct type
assert(argImpToExpNum.find(&(*new_arg_iter)) != argImpToExpNum.end() &&
"Can't find explicit argument number");
// tracing it on parent function argument list
Value* callArg = CImagesBI::CImagesUtils::traceImageOrSamplerArgument(cInst, argImpToExpNum[&(*new_arg_iter)]);
Argument* arg = dyn_cast<Argument>(callArg);
assert(arg && " Not supported");
// build info
info.DW0.All.argExplictNum = arg->getArgNo();
assert(infoToArg.find(info.DW0.Value) != infoToArg.end() &&
"Can't find the implicit argument on parent function");
new_args.push_back(infoToArg[info.DW0.Value]);
}
else
{
assert(infoToArg.find(info.DW0.Value) != infoToArg.end() &&
"Can't find the implicit argument on parent function");
new_args.push_back(infoToArg[info.DW0.Value]);
}
++new_arg_iter;
++cImpCount;
}
// insert new call instruction before old one
llvm::CallInst *inst;
if (new_func->getReturnType()->isVoidTy())
{
inst = CallInst::Create(new_func, new_args, "", cInst);
}
else
{
inst = CallInst::Create(new_func, new_args, new_func->getName(), cInst);
}
inst->setCallingConv(new_func->getCallingConv());
inst->setDebugLoc(cInst->getDebugLoc());
cInst->replaceAllUsesWith(inst);
list_delete.push_back(cInst);
}
for (auto i : list_delete)
{
i->eraseFromParent();
}
}
// Builtin CallGraph Analysis
#define PASS_FLAG2 "igc-callgraphscc-analysis"
#define PASS_DESCRIPTION2 "Analyzes CallGraphSCC"
#define PASS_CFG_ONLY2 false
#define PASS_ANALYSIS2 false
IGC_INITIALIZE_PASS_BEGIN(BuiltinCallGraphAnalysis, PASS_FLAG2, PASS_DESCRIPTION2, PASS_CFG_ONLY2, PASS_ANALYSIS2)
IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
IGC_INITIALIZE_PASS_END(BuiltinCallGraphAnalysis, PASS_FLAG2, PASS_DESCRIPTION2, PASS_CFG_ONLY2, PASS_ANALYSIS2)
char BuiltinCallGraphAnalysis::ID = 0;
BuiltinCallGraphAnalysis::BuiltinCallGraphAnalysis() : ModulePass(ID)
{
initializeBuiltinCallGraphAnalysisPass(*PassRegistry::getPassRegistry());
}
/// Check whether there are recursions.
static bool hasRecursion(CallGraph &CG)
{
// Use Tarjan's algorithm to detect recursions.
for (auto I = scc_begin(&CG), E = scc_end(&CG); I != E; ++I)
{
const std::vector<CallGraphNode *> &SCCNodes = *I;
if (SCCNodes.size() >= 2)
{
return true;
}
// Check self-recursion.
auto Node = SCCNodes.back();
for (auto Callee : *Node)
{
if (Callee.second == Node)
{
return true;
}
}
}
// No recursion.
return false;
}
bool BuiltinCallGraphAnalysis::runOnModule(Module &M)
{
if (IGC_GET_FLAG_VALUE(FunctionControl) == FLAG_FCALL_FORCE_INLINE)
{
return false;
}
m_pMdUtils = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
CallGraph &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
if (IGC_IS_FLAG_DISABLED(EnableRecursionOpenCL) && hasRecursion(CG))
{
getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitError(" undefined reference to `jmp()' ");
return false;
}
//Return if any error
CodeGenContext* ctx = getAnalysis<CodeGenContextWrapper>().getCodeGenContext();
if (!(ctx->oclErrorMessage.empty()))
{
return false;
}
for (auto I = scc_begin(&CG), IE = scc_end(&CG); I != IE; ++I)
{
const std::vector<CallGraphNode *> &SCCNodes = *I;
traveseCallGraphSCC(SCCNodes);
}
return false;
}
void BuiltinCallGraphAnalysis::traveseCallGraphSCC(const std::vector<CallGraphNode *> &SCCNodes)
{
// all functions in one scc should end up with the same result
ImplicitArgmentDetail *argData = nullptr;
for (auto CGN : SCCNodes)
{
Function *f = CGN->getFunction();
if (!f || f->isDeclaration())
continue;
// Fail on variadic functions.
if (f->isVarArg())
{
std::string Msg = "Invalid user defined function being processed: ";
Msg += f->getName();
Msg += "()\n";
getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitError(Msg.c_str());
return;
}
if (argData == nullptr)
{
argData = new ImplicitArgmentDetail;
}
// calculate args from sub-routine.
// This function have not beeen processed yet, therefore no map-entry for it yet
assert(argMap.count(f) == 0);
for (auto N : (*CGN))
{
Function *sub = N.second->getFunction();
// if callee has not been visited
auto argMapIter = argMap.find(sub);
// if we have processed the arguments for callee
if (argMapIter != argMap.end())
{
assert(argMapIter->second);
combineTwoArgDetail(*argData, *(argMapIter->second), N.first);
}
}
}
for (auto CGN : SCCNodes)
{
Function *f = CGN->getFunction();
if (!f || f->isDeclaration())
continue;
FunctionInfoMetaDataHandle funcInfo = m_pMdUtils->getFunctionsInfoItem(f);
// calculate implicit args from function metadata
auto I = funcInfo->begin_ImplicitArgInfoList();
auto E = funcInfo->end_ImplicitArgInfoList();
// build everything
for (; I != E; I++)
{
ArgInfoMetaDataHandle argInfo = *I;
ImplicitArg::ArgType argId = (ImplicitArg::ArgType)argInfo->getArgId();
if (argId < ImplicitArg::ArgType::STRUCT_START)
{
// unique implicit argument
// if not exit, the following line will add one.
ImplicitArg::ArgValSet * setx = &(argData->ArgsMaps[argId]);
setx->insert(0);
}
else if (argId <= ImplicitArg::ArgType::STRUCT_END)
{
// aggregate implicity argument
ImplicitStructArgument info;
info.DW0.All.argExplictNum = argInfo->getExplicitArgNum();
info.DW0.All.argOffset = argInfo->getStructArgOffset();
info.DW0.All.argId = argId;
(*argData).StructArgSet.insert(info.DW0.Value);
}
else if (argId <= ImplicitArg::ArgType::IMAGES_END || argId == ImplicitArg::ArgType::GET_OBJECT_ID || argId == ImplicitArg::ArgType::GET_BLOCK_SIMD_SIZE)
{
// image index, project id
int argNum = argInfo->getExplicitArgNum();
ImplicitArg::ArgValSet * setx = &(argData->ArgsMaps[argId]);
setx->insert(argNum);
}
else
{
// unique implicit argument
// if not exit, the following line will add one.
ImplicitArg::ArgValSet * setx = &(argData->ArgsMaps[argId]);
setx->insert(0);
}
}
}
for (auto CGN : SCCNodes)
{
Function *f = CGN->getFunction();
if (!f || f->isDeclaration())
continue;
// write everything back into metaData
writeBackAllIntoMetaData(*argData, f);
argMap.insert(std::make_pair(f, argData));
}
}
void BuiltinCallGraphAnalysis::combineTwoArgDetail(
ImplicitArgmentDetail &retD,
ImplicitArgmentDetail &argD,
llvm::Value* v)
{
for (const auto& argPair : argD.ArgsMaps)
{
ImplicitArg::ArgType argId = argPair.first;
if (argId < ImplicitArg::ArgType::STRUCT_START)
{
// unique implicit argument
// if not exit, the following line will add one.
ImplicitArg::ArgValSet * setx = &retD.ArgsMaps[argId];
setx->insert(0);
}
else if (argId <= ImplicitArg::ArgType::STRUCT_END)
{
// aggregate implicity argument
assert(0 && "wrong location for this kind of argument type");
}
else if (argId <= ImplicitArg::ArgType::IMAGES_END || argId == ImplicitArg::ArgType::GET_OBJECT_ID || argId == ImplicitArg::ArgType::GET_BLOCK_SIMD_SIZE)
{
// image index
CallInst *cInst = dyn_cast<CallInst>(v);
if (!cInst)
{
assert(0 && " Not supported");
getAnalysis<CodeGenContextWrapper>().getCodeGenContext()->EmitError(" undefined reference to `jmp()' ");
return;
}
ImplicitArg::ArgValSet argSet = argPair.second;
ImplicitArg::ArgValSet * setx = &retD.ArgsMaps[argId];
// loop all image arguments on the sub-funtion.
for (const auto& argI : argSet)
{
// find it from calling instruction, and trace it back to parent function argument
Value* callArg = CImagesBI::CImagesUtils::traceImageOrSamplerArgument(cInst, argI);
Argument* arg = dyn_cast<Argument>(callArg);
if (!arg)
{
assert(0 && " Not supported");
}
setx->insert(arg->getArgNo());
}
}
else
{
// unique implicit argument
// if not exit, the following line will add one.
ImplicitArg::ArgValSet * setx = &retD.ArgsMaps[argId];
setx->insert(0);
}
}
#if defined(_DEBUG)
// aggregate structure
for (unsigned i = 0; i < argD.StructArgSet.size(); i++)
{
assert(0 && "wrong argument type in user function");
}
#endif
}
void BuiltinCallGraphAnalysis::writeBackAllIntoMetaData(ImplicitArgmentDetail& data, Function * f)
{
FunctionInfoMetaDataHandle funcInfo = m_pMdUtils->getFunctionsInfoItem(f);
funcInfo->clearImplicitArgInfoList();
for (const auto& A : data.ArgsMaps)
{
ImplicitArg::ArgType argId = A.first;
if (argId < ImplicitArg::ArgType::STRUCT_START)
{
// unique implicit argument, add it on metadata
ArgInfoMetaDataHandle argMD = ArgInfoMetaDataHandle(ArgInfoMetaData::get());
argMD->setArgId(argId);
funcInfo->addImplicitArgInfoListItem(argMD);
}
else if (argId <= ImplicitArg::ArgType::STRUCT_END)
{
// aggregate implicity argument
assert(0 && "wrong location for this kind of argument type");
}
else if (argId <= ImplicitArg::ArgType::IMAGES_END || argId == ImplicitArg::ArgType::GET_OBJECT_ID || argId == ImplicitArg::ArgType::GET_BLOCK_SIMD_SIZE)
{
// image index
for (const auto& vOnSet: A.second)
{
ArgInfoMetaDataHandle argMD = ArgInfoMetaDataHandle(ArgInfoMetaData::get());
argMD->setArgId(argId);
argMD->setExplicitArgNum(vOnSet);
funcInfo->addImplicitArgInfoListItem(argMD);
}
}
else
{
// unique implicit argument
ArgInfoMetaDataHandle argMD = ArgInfoMetaDataHandle(ArgInfoMetaData::get());
argMD->setArgId(argId);
funcInfo->addImplicitArgInfoListItem(argMD);
}
}
for (const auto N : data.StructArgSet) // argument number
{
ArgInfoMetaDataHandle argMD = ArgInfoMetaDataHandle(ArgInfoMetaData::get());
ImplicitStructArgument info;
info.DW0.Value = N;
argMD->setExplicitArgNum(info.DW0.All.argExplictNum);
argMD->setStructArgOffset(info.DW0.All.argOffset); // offset
argMD->setArgId(info.DW0.All.argId); // type
funcInfo->addImplicitArgInfoListItem(argMD);
}
m_pMdUtils->save(f->getParent()->getContext());
}