blob: 83ee1bc2b3c698feb50507b303cde76853026365 [file] [log] [blame]
David Netoc5fb5242018-07-30 13:28:31 -04001// Copyright 2018 The Clspv Authors. All rights reserved.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#include <climits>
16#include <map>
17#include <set>
18#include <utility>
19#include <vector>
20
21#include "llvm/ADT/DenseMap.h"
22#include "llvm/ADT/SmallVector.h"
23#include "llvm/ADT/UniqueVector.h"
24#include "llvm/IR/Constants.h"
25#include "llvm/IR/DerivedTypes.h"
26#include "llvm/IR/Function.h"
27#include "llvm/IR/IRBuilder.h"
28#include "llvm/IR/Instructions.h"
29#include "llvm/IR/Module.h"
30#include "llvm/Pass.h"
31#include "llvm/Support/raw_ostream.h"
32
33#include "clspv/Option.h"
34#include "clspv/Passes.h"
35
36#include "ArgKind.h"
Alan Baker202c8c72018-08-13 13:47:44 -040037#include "Constants.h"
David Netoc5fb5242018-07-30 13:28:31 -040038
39using namespace llvm;
40
41#define DEBUG_TYPE "directresourceaccess"
42
43namespace {
44
45cl::opt<bool> ShowDRA("show-dra", cl::init(false), cl::Hidden,
46 cl::desc("Show direct resource access details"));
47
48using SamplerMapType = llvm::ArrayRef<std::pair<unsigned, std::string>>;
49
50class DirectResourceAccessPass final : public ModulePass {
51public:
52 static char ID;
53 DirectResourceAccessPass() : ModulePass(ID) {}
54 bool runOnModule(Module &M) override;
55
56private:
57 // Return the functions reachable from entry point functions, where
58 // callers appear before callees. OpenCL C does not permit recursion
59 // or function or pointers, so this is always well defined. The ordering
60 // should be reproducible from one run to the next.
61 UniqueVector<Function *> CallGraphOrderedFunctions(Module &);
62
63 // For each kernel argument that will map to a resource variable (descriptor),
64 // try to rewrite the uses of the argument as a direct access of the resource.
65 // We can only do this if all the callees of the function use the same
66 // resource access value for that argument. Returns true if the module
67 // changed.
68 bool RewriteResourceAccesses(Function *fn);
69
70 // Rewrite uses of this resrouce-based arg if all the callers pass in the
71 // same resource access. Returns true if the module changed.
72 bool RewriteAccessesForArg(Function *fn, int arg_index, Argument &arg);
73};
74} // namespace
75
76char DirectResourceAccessPass::ID = 0;
77static RegisterPass<DirectResourceAccessPass> X("DirectResourceAccessPass",
78 "Direct resource access");
79
80namespace clspv {
81ModulePass *createDirectResourceAccessPass() {
82 return new DirectResourceAccessPass();
83}
84} // namespace clspv
85
86namespace {
87bool DirectResourceAccessPass::runOnModule(Module &M) {
88 bool Changed = false;
89
90 if (clspv::Option::DirectResourceAccess()) {
91 auto ordered_functions = CallGraphOrderedFunctions(M);
92 for (auto *fn : ordered_functions) {
93 Changed |= RewriteResourceAccesses(fn);
94 }
95 }
96
97 return Changed;
98}
99
100UniqueVector<Function *>
101DirectResourceAccessPass::CallGraphOrderedFunctions(Module &M) {
102 // Use a topological sort.
103
104 // Make an ordered list of all functions having bodies, with kernel entry
105 // points listed first.
106 UniqueVector<Function *> functions;
107 SmallVector<Function *, 10> entry_points;
108 for (Function &F : M) {
109 if (F.isDeclaration()) {
110 continue;
111 }
112 if (F.getCallingConv() == CallingConv::SPIR_KERNEL) {
113 functions.insert(&F);
114 entry_points.push_back(&F);
115 }
116 }
117 // Add the remaining functions.
118 for (Function &F : M) {
119 if (F.isDeclaration()) {
120 continue;
121 }
122 if (F.getCallingConv() != CallingConv::SPIR_KERNEL) {
123 functions.insert(&F);
124 }
125 }
126
127 // This will be a complete set of reveresed edges, i.e. with all pairs
128 // of (callee, caller).
129 using Edge = std::pair<unsigned, unsigned>;
130 auto make_edge = [&functions](Function *callee, Function *caller) {
131 return std::pair<unsigned, unsigned>{functions.idFor(callee),
132 functions.idFor(caller)};
133 };
134 std::set<Edge> reverse_edges;
135 // Map each function to the functions it calls, and populate |reverse_edges|.
136 std::map<Function *, SmallVector<Function *, 3>> calls_functions;
137 for (Function *callee : functions) {
138 for (auto &use : callee->uses()) {
139 if (auto *call = dyn_cast<CallInst>(use.getUser())) {
140 Function *caller = call->getParent()->getParent();
141 calls_functions[caller].push_back(callee);
142 reverse_edges.insert(make_edge(callee, caller));
143 }
144 }
145 }
146 // Sort the callees in module-order. This helps us produce a deterministic
147 // result.
148 for (auto &pair : calls_functions) {
149 auto &callees = pair.second;
150 std::sort(callees.begin(), callees.end(),
151 [&functions](Function *lhs, Function *rhs) {
152 return functions.idFor(lhs) < functions.idFor(rhs);
153 });
154 }
155
156 // Use Kahn's algorithm for topoological sort.
157 UniqueVector<Function *> result;
158 SmallVector<Function *, 10> work_list(entry_points.begin(),
159 entry_points.end());
160 while (!work_list.empty()) {
161 Function *caller = work_list.back();
162 work_list.pop_back();
163 result.insert(caller);
164 auto &callees = calls_functions[caller];
165 for (auto *callee : callees) {
166 reverse_edges.erase(make_edge(callee, caller));
167 auto lower_bound = reverse_edges.lower_bound(make_edge(callee, nullptr));
168 if (lower_bound == reverse_edges.end() ||
169 lower_bound->first != functions.idFor(callee)) {
170 // Callee has no other unvisited callers.
171 work_list.push_back(callee);
172 }
173 }
174 }
175 // If reverse_edges is not empty then there was a cycle. But we don't care
176 // about that erroneous case.
177
178 if (ShowDRA) {
179 outs() << "DRA: Ordered functions:\n";
180 for (Function *fun : result) {
181 outs() << "DRA: " << fun->getName() << "\n";
182 }
183 }
184 return result;
185}
186
187bool DirectResourceAccessPass::RewriteResourceAccesses(Function *fn) {
188 bool Changed = false;
189 int arg_index = 0;
190 for (Argument &arg : fn->args()) {
191 switch (clspv::GetArgKindForType(arg.getType())) {
192 case clspv::ArgKind::Buffer:
Alan Bakerfcda9482018-10-02 17:09:59 -0400193 case clspv::ArgKind::BufferUBO:
David Netoc5fb5242018-07-30 13:28:31 -0400194 case clspv::ArgKind::ReadOnlyImage:
195 case clspv::ArgKind::WriteOnlyImage:
196 case clspv::ArgKind::Sampler:
Alan Baker202c8c72018-08-13 13:47:44 -0400197 case clspv::ArgKind::Local:
David Netoc5fb5242018-07-30 13:28:31 -0400198 Changed |= RewriteAccessesForArg(fn, arg_index, arg);
199 break;
David Neto3a0df832018-08-03 14:35:42 -0400200 default:
201 // Should not happen
202 break;
David Netoc5fb5242018-07-30 13:28:31 -0400203 }
204 arg_index++;
205 }
206 return Changed;
207}
208
209bool DirectResourceAccessPass::RewriteAccessesForArg(Function *fn,
210 int arg_index,
211 Argument &arg) {
212 bool Changed = false;
213 if (fn->use_empty()) {
214 return false;
215 }
216
217 // We can convert a parameter to a direct resource access if it is
218 // either a direct call to a clspv.resource.var.* or if it a GEP of
219 // such a thing (where the GEP can only have zero indices).
220 struct ParamInfo {
Alan Baker202c8c72018-08-13 13:47:44 -0400221 // The base value. It is either a global variable or a resource-access
222 // builtin function. (@clspv.resource.var.* or @clspv.local.var.*)
223 Value *base;
David Netoc5fb5242018-07-30 13:28:31 -0400224 // The descriptor set.
225 uint32_t set;
226 // The binding.
227 uint32_t binding;
228 // If the parameter is a GEP, then this is the number of zero-indices
229 // the GEP used.
230 unsigned num_gep_zeroes;
231 // An example call fitting
232 CallInst *sample_call;
233 };
234 // The common valid parameter info across all the callers seen soo far.
235
236 bool seen_one = false;
237 ParamInfo common;
238 // Tries to merge the given parameter info into |common|. If it is the first
239 // time we've tried, then save it. Returns true if there is no conflict.
240 auto merge_param_info = [&seen_one, &common](const ParamInfo &pi) {
241 if (!seen_one) {
242 common = pi;
243 seen_one = true;
244 return true;
245 }
Alan Baker202c8c72018-08-13 13:47:44 -0400246 return pi.base == common.base && pi.set == common.set &&
David Netoc5fb5242018-07-30 13:28:31 -0400247 pi.binding == common.binding &&
248 pi.num_gep_zeroes == common.num_gep_zeroes;
249 };
250
251 for (auto &use : fn->uses()) {
252 if (auto *caller = dyn_cast<CallInst>(use.getUser())) {
253 Value *value = caller->getArgOperand(arg_index);
254 // We care about two cases:
255 // - a direct call to clspv.resource.var.*
256 // - a GEP with only zero indices, where the base pointer is
257
258 // Unpack GEPs with zeros, if we can. Rewrite |value| as we go along.
259 unsigned num_gep_zeroes = 0;
David Neto2f450002018-08-01 16:13:03 -0400260 bool first_gep = true;
David Netoc5fb5242018-07-30 13:28:31 -0400261 for (auto *gep = dyn_cast<GetElementPtrInst>(value); gep;
262 gep = dyn_cast<GetElementPtrInst>(value)) {
263 if (!gep->hasAllZeroIndices()) {
264 return false;
265 }
David Neto2f450002018-08-01 16:13:03 -0400266 // If not the first GEP, then ignore the "element" index (which I call
267 // "slide") since that will be combined with the last index of the
268 // previous GEP.
269 num_gep_zeroes += gep->getNumIndices() + (first_gep ? 0 : -1);
David Netoc5fb5242018-07-30 13:28:31 -0400270 value = gep->getPointerOperand();
David Neto2f450002018-08-01 16:13:03 -0400271 first_gep = false;
David Netoc5fb5242018-07-30 13:28:31 -0400272 }
273 if (auto *call = dyn_cast<CallInst>(value)) {
274 // If the call is a call to a @clspv.resource.var.* function, then try
275 // to merge it, assuming the given number of GEP zero-indices so far.
276 if (call->getCalledFunction()->getName().startswith(
Alan Baker202c8c72018-08-13 13:47:44 -0400277 clspv::ResourceAccessorFunction())) {
David Netoc5fb5242018-07-30 13:28:31 -0400278 const auto set = uint32_t(
279 dyn_cast<ConstantInt>(call->getOperand(0))->getZExtValue());
280 const auto binding = uint32_t(
281 dyn_cast<ConstantInt>(call->getOperand(1))->getZExtValue());
282 if (!merge_param_info({call->getCalledFunction(), set, binding,
Alan Baker202c8c72018-08-13 13:47:44 -0400283 num_gep_zeroes, call}))
David Netoc5fb5242018-07-30 13:28:31 -0400284 return false;
Alan Baker202c8c72018-08-13 13:47:44 -0400285 } else if (call->getCalledFunction()->getName().startswith(
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400286 clspv::WorkgroupAccessorFunction())) {
Alan Baker202c8c72018-08-13 13:47:44 -0400287 const uint32_t spec_id = uint32_t(
288 dyn_cast<ConstantInt>(call->getOperand(0))->getZExtValue());
289 if (!merge_param_info({call->getCalledFunction(), spec_id, 0,
290 num_gep_zeroes, call}))
291 return false;
David Netoc5fb5242018-07-30 13:28:31 -0400292 } else {
293 // A call but not to a resource access builtin function.
294 return false;
295 }
Alan Baker202c8c72018-08-13 13:47:44 -0400296 } else if (isa<GlobalValue>(value)) {
297 if (!merge_param_info({value, 0, 0, num_gep_zeroes, nullptr}))
298 return false;
David Netoc5fb5242018-07-30 13:28:31 -0400299 } else {
300 // Not a call.
301 return false;
302 }
303 } else {
304 // There isn't enough commonality. Bail out without changing anything.
305 return false;
306 }
307 }
308 if (ShowDRA) {
309 if (seen_one) {
310 outs() << "DRA: Rewrite " << fn->getName() << " arg " << arg_index << " "
Alan Baker202c8c72018-08-13 13:47:44 -0400311 << arg.getName() << ": " << common.base->getName() << " ("
David Netoc5fb5242018-07-30 13:28:31 -0400312 << common.set << "," << common.binding
Alan Bakerfc6888e2018-08-20 20:54:33 -0400313 << ") zeroes: " << common.num_gep_zeroes << " sample-call ";
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400314 if (common.sample_call)
315 outs() << *common.sample_call << "\n";
316 else
317 outs() << "nullptr\n";
David Netoc5fb5242018-07-30 13:28:31 -0400318 }
319 }
320
321 // Now rewrite the argument, using the info in |common|.
322
323 Changed = true;
324 IRBuilder<> Builder(fn->getParent()->getContext());
325 auto *zero = Builder.getInt32(0);
326 Builder.SetInsertPoint(fn->getEntryBlock().getFirstNonPHI());
327
Alan Baker202c8c72018-08-13 13:47:44 -0400328 Value *replacement = common.base;
Diego Novillo3cc8d7a2019-04-10 13:30:34 -0400329 if (Function *function = dyn_cast<Function>(replacement)) {
Alan Baker202c8c72018-08-13 13:47:44 -0400330 // Create the call.
331 SmallVector<Value *, 8> args(common.sample_call->arg_begin(),
332 common.sample_call->arg_end());
333 replacement = Builder.CreateCall(function, args);
334 if (ShowDRA) {
335 outs() << "DRA: Replace: call " << *replacement << "\n";
336 }
David Netoc5fb5242018-07-30 13:28:31 -0400337 }
338 if (common.num_gep_zeroes) {
339 SmallVector<Value *, 3> zeroes;
340 for (unsigned i = 0; i < common.num_gep_zeroes; i++) {
341 zeroes.push_back(zero);
342 }
Alan Baker202c8c72018-08-13 13:47:44 -0400343 // Builder.CreateGEP is not used to avoid creating a GEPConstantExpr in the
344 // case of global variables.
345 replacement = GetElementPtrInst::Create(nullptr, replacement, zeroes);
346 Builder.Insert(cast<Instruction>(replacement));
David Netoc5fb5242018-07-30 13:28:31 -0400347 if (ShowDRA) {
348 outs() << "DRA: Replace: gep " << *replacement << "\n";
349 }
350 }
351 arg.replaceAllUsesWith(replacement);
352
353 return Changed;
354}
355
356} // namespace