Debian Codegraph @

IGC/Compiler/Optimizer/OpenCLPasses/KernelFunctionCloning/KernelFunctionCloning.cpp

raw symbols SCIP
1 /*========================== begin_copyright_notice ============================
2
3 Copyright (C) 2017-2021 Intel Corporation
4
5 SPDX-License-Identifier: MIT
6
7 ============================= end_copyright_notice ===========================*/
8
9 #include "common/LLVMWarningsPush.hpp"
10 #include <llvm/Pass.h>
11 #include <llvm/ADT/SmallVector.h>
12 #include <llvmWrapper/IR/CallSite.h>
13 #include <llvm/Transforms/Utils/Cloning.h>
14 #include "common/LLVMWarningsPop.hpp"
15 #include "Compiler/CISACodeGen/ShaderCodeGen.hpp"
16 #include "Compiler/IGCPassSupport.h"
17 #include "Compiler/MetaDataUtilsWrapper.h"
18
19 using namespace llvm;
20 using namespace IGC;
21 using namespace IGC::IGCMD;
22
23 // OpenCL C specification states that a kernel function is just a regular
24 // function call if a __kernel function is called by another kernel function.
25 // But, it doesn't clarify what's the behavior exactly. Under certain
26 // conditions, such a call may be ambiguous without separating kernel function
27 // and user functions, e.g.
28 //
29 // __kernel __attribute__((reqd_work_group_size(1, 1, 1)))
30 // void bar(...) {
31 // ...
32 // }
33 //
34 // __kernel __attribute__((reqd_work_group_size(32, 1, 1)))
35 // void foo(...) {
36 // ...
37 // bar(...);
38 // ...
39 // }
40 //
41 // Such ambiguity also exists if function call is enabled as well as if there
42 // are optimizations which may treat kernel function differently from user
43 // functions, such as inline buffer resolution, which will resolve the first
44 // local pointer argument at compilation time. If a function is used as both a
45 // kernel function and a user function. The first local pointer argument is
46 // ambiguous for optimization to resolve it statically.
47 //
48 // OpenCL C++ specification already clarifies the issue and would not allow a
49 // kernel function to called from another kernel function. However, we still
50 // need to handle that for OCL 1.2 and OCL 2.0.
51 //
52 // This pass is added to clone a kernel function to a user function if it's
53 // called.
54 //
55
56 namespace {
57 class KernelFunctionCloning : public ModulePass {
58 public:
59 static char ID;
60
61 KernelFunctionCloning() : ModulePass(ID) {}
62
63 bool runOnModule(Module &) override;
64
65 llvm::StringRef getPassName() const override { return "KernelFunctionCloning"; }
66
67 private:
68 void getAnalysisUsage(AnalysisUsage &AU) const override {
69 AU.setPreservesCFG();
70 AU.addRequired<CodeGenContextWrapper>();
71 AU.addRequired<MetaDataUtilsWrapper>();
72 }
73 };
74
75 } // End anonymous namespace
76
77 namespace IGC {
78
79 ModulePass *createKernelFunctionCloningPass() { return new KernelFunctionCloning(); }
80
81 #define PASS_FLAG "igc-kernel-function-cloning"
82 #define PASS_DESC "Clone kernel functions if it's called."
83 #define PASS_CFG_ONLY false
84 #define PASS_ANALYSIS false
85 IGC_INITIALIZE_PASS_BEGIN(KernelFunctionCloning, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
86 IGC_INITIALIZE_PASS_DEPENDENCY(CodeGenContextWrapper)
87 IGC_INITIALIZE_PASS_DEPENDENCY(MetaDataUtilsWrapper)
88 IGC_INITIALIZE_PASS_END(KernelFunctionCloning, PASS_FLAG, PASS_DESC, PASS_CFG_ONLY, PASS_ANALYSIS)
89
90 } // namespace IGC
91
92 char KernelFunctionCloning::ID = 0;
93
94 template <typename PatternTypeFirst, typename... PatternTypeRest> struct PatternChecker {
95 template <typename Checker> static bool run(User *user, Checker check) {
96 auto casted = dyn_cast<PatternTypeFirst>(user);
97 if (!casted)
98 return false;
99
100 if constexpr (sizeof...(PatternTypeRest) > 0) {
101 for (auto user : casted->users()) {
102 if (!PatternChecker<PatternTypeRest...>::run(user, check)) {
103 return false;
104 }
105 }
106 }
107 return check(casted);
108 }
109 };
110
111 bool KernelFunctionCloning::runOnModule(Module &M) {
112 MetaDataUtils *MDU = getAnalysis<MetaDataUtilsWrapper>().getMetaDataUtils();
113
114 // Collect kernel functions being called.
115 SmallVector<Function *, 8> KernelsToClone;
116 for (auto &F : M) {
117 auto FII = MDU->findFunctionsInfoItem(&F);
118 if (FII == MDU->end_FunctionsInfo())
119 continue;
120 // Check this kernell function is called.
121 for (auto *U : F.users()) {
122 //
123 // Ignore if it's a user semantic decoration on function.
124 //
125 // GlobalVariable("llvm.global.annotations"):
126 // ConstantArray:
127 // ConstantStruct:
128 // BitCastOperator:
129 // Function = [ANNOTATED_FUNCTION]
130 // GetElementPtr:
131 // GlobalVariable = [ANNOTATION]
132 // ConstantStruct:
133 // ...
134 //
135 bool user_semantic =
136 PatternChecker<BitCastOperator, ConstantStruct, ConstantArray, GlobalVariable>::run(U, [](User *user) {
137 if (auto casted = dyn_cast<GlobalVariable>(user)) {
138 return casted->getName().compare("llvm.global.annotations") == 0;
139 }
140 return true;
141 });
142
143 if (user_semantic) {
144 continue;
145 }
146 IGCLLVM::CallSite *call = nullptr;
147 call = dyn_cast<IGCLLVM::CallSite>(U);
148 if (!call)
149 continue;
150 KernelsToClone.push_back(&F);
151 break;
152 }
153 }
154
155 // Clone it
156 bool Changed = false;
157 for (auto *F : KernelsToClone) {
158 ValueToValueMapTy VMap;
159 auto *NewF = CloneFunction(F, VMap);
160 NewF->setLinkage(GlobalValue::InternalLinkage);
161 if (!F->getParent()->getFunction(NewF->getName()))
162 F->getParent()->getFunctionList().push_back(NewF);
163
164 // Collect pointers to users (callsites) of the original kernel
165 // function and loop through the collection. Otherwise when looping
166 // through F->users(), calling call->setCalledFunction(NewF) modifies
167 // the F->users() by removing the very first element (second element
168 // becomes first) and the loop skips every second element.
169 SmallVector<User *, 8> originalKernelFunctionUsers;
170 for (auto *U : F->users()) {
171 originalKernelFunctionUsers.push_back(U);
172 }
173
174 // Replace the original calls to kernel function with calls to user
175 // function clone at the callsites.
176 for (auto &U : originalKernelFunctionUsers) {
177 IGCLLVM::CallSite *call = nullptr;
178 call = dyn_cast<IGCLLVM::CallSite>(U);
179 if (!call)
180 continue;
181
182 if (call->getCalledFunction()->getType() == NewF->getType())
183 call->setCalledFunction(NewF);
184 }
185
186 Changed = true;
187 }
188
189 return Changed;
190 }