David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 1 | // Copyright 2017 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 | |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 15 | // Cluster POD kernel arguments. |
| 16 | // |
| 17 | // Collect plain-old-data kernel arguments and place them into a single |
| 18 | // struct argument, at the end. Other arguments are pointers, and retain |
| 19 | // their relative order. |
| 20 | // |
| 21 | // We will create a kernel function as the new entry point, and change |
| 22 | // the original kernel function into a regular SPIR function. Key |
| 23 | // kernel metadata is moved from the old function to the wrapper. |
| 24 | // We also attach a "kernel_arg_map" metadata node to the function to |
| 25 | // encode the mapping from old kernel argument to new kernel argument. |
| 26 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 27 | #include <cassert> |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 28 | #include <cstring> |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 29 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 30 | #include "llvm/IR/Constants.h" |
| 31 | #include "llvm/IR/DerivedTypes.h" |
| 32 | #include "llvm/IR/Function.h" |
| 33 | #include "llvm/IR/Instructions.h" |
| 34 | #include "llvm/IR/IRBuilder.h" |
| 35 | #include "llvm/IR/Metadata.h" |
| 36 | #include "llvm/IR/Module.h" |
| 37 | #include "llvm/Pass.h" |
| 38 | #include "llvm/Support/CommandLine.h" |
| 39 | #include "llvm/Support/raw_ostream.h" |
| 40 | #include "llvm/Transforms/Utils/Cloning.h" |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 41 | |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 42 | #include "ArgKind.h" |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 43 | |
| 44 | using namespace llvm; |
| 45 | |
| 46 | #define DEBUG_TYPE "clusterpodkernelargs" |
| 47 | |
| 48 | namespace { |
| 49 | struct ClusterPodKernelArgumentsPass : public ModulePass { |
| 50 | static char ID; |
| 51 | ClusterPodKernelArgumentsPass() : ModulePass(ID) {} |
| 52 | |
| 53 | bool runOnModule(Module &M) override; |
| 54 | }; |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 55 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 56 | } // namespace |
| 57 | |
| 58 | char ClusterPodKernelArgumentsPass::ID = 0; |
| 59 | static RegisterPass<ClusterPodKernelArgumentsPass> |
| 60 | X("ClusterPodKernelArgumentsPass", |
| 61 | "Cluster POD Kernel Arguments Pass"); |
| 62 | |
| 63 | namespace clspv { |
| 64 | llvm::ModulePass *createClusterPodKernelArgumentsPass() { |
| 65 | return new ClusterPodKernelArgumentsPass(); |
| 66 | } |
| 67 | } // namespace clspv |
| 68 | |
| 69 | bool ClusterPodKernelArgumentsPass::runOnModule(Module &M) { |
| 70 | bool Changed = false; |
| 71 | LLVMContext &Context = M.getContext(); |
| 72 | |
| 73 | SmallVector<Function *, 8> WorkList; |
| 74 | |
| 75 | for (Function &F : M) { |
| 76 | if (F.isDeclaration() || F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 77 | continue; |
| 78 | } |
| 79 | for (Argument &Arg : F.args()) { |
| 80 | if (!isa<PointerType>(Arg.getType())) { |
| 81 | WorkList.push_back(&F); |
| 82 | break; |
| 83 | } |
| 84 | } |
| 85 | } |
| 86 | |
David Neto | d5b3f98 | 2017-09-28 14:49:49 -0400 | [diff] [blame] | 87 | SmallVector<CallInst*, 8> CallList; |
| 88 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 89 | // Note: The transformation done in this pass preserves the pointer-to-local |
| 90 | // arg to spec-id mapping. |
| 91 | clspv::ArgIdMapType arg_spec_id_map = clspv::AllocateArgSpecIds(M); |
| 92 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 93 | for (Function* F : WorkList) { |
| 94 | Changed = true; |
| 95 | |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 96 | // An ArgMapping describes how a kernel argument is remapped. |
| 97 | struct ArgMapping { |
| 98 | std::string name; |
| 99 | // 0-based argument index in the old kernel function. |
| 100 | unsigned old_index; |
| 101 | // 0-based argument index in the new kernel function. |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 102 | int new_index; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 103 | // Offset of the argument value within the new kernel argument. |
| 104 | // This is always zero for non-POD arguments. For a POD argument, |
| 105 | // this is the byte offset within the POD arguments struct. |
| 106 | unsigned offset; |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 107 | // Argument type. Same range of values as the result of |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 108 | // clspv::GetArgKindForType. |
| 109 | const char* arg_kind; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 110 | // If non-negative, this argument is a pointer-to-local, and the value |
| 111 | // here is the specialization constant id for the array size. |
| 112 | int spec_id; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 113 | }; |
| 114 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 115 | // In OpenCL, kernel arguments are either pointers or POD. A composite with |
| 116 | // an element or memeber that is a pointer is not allowed. So we'll use POD |
| 117 | // as a shorthand for non-pointer. |
| 118 | |
| 119 | SmallVector<Type *, 8> PtrArgTys; |
| 120 | SmallVector<Type *, 8> PodArgTys; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 121 | SmallVector<ArgMapping, 8> RemapInfo; |
| 122 | unsigned arg_index = 0; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 123 | int new_index = 0; |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 124 | for (Argument &Arg : F->args()) { |
| 125 | Type *ArgTy = Arg.getType(); |
| 126 | if (isa<PointerType>(ArgTy)) { |
| 127 | PtrArgTys.push_back(ArgTy); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 128 | auto kind = clspv::GetArgKindForType(ArgTy); |
| 129 | int spec_id = -1; |
| 130 | if (0 == std::strcmp("local", kind)) { |
| 131 | spec_id = arg_spec_id_map[&Arg]; |
| 132 | assert(spec_id > 0); |
| 133 | } |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 134 | RemapInfo.push_back({std::string(Arg.getName()), arg_index, |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 135 | new_index++, 0u, kind, spec_id}); |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 136 | } else { |
| 137 | PodArgTys.push_back(ArgTy); |
| 138 | } |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 139 | arg_index++; |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 140 | } |
| 141 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 142 | // Put the pointer arguments first, and then POD arguments struct last. |
David Neto | 2ded02e | 2017-10-23 15:30:59 -0400 | [diff] [blame] | 143 | // Use StructType::get so we reuse types where possible. |
| 144 | auto PodArgsStructTy = StructType::get(Context, PodArgTys); |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 145 | SmallVector<Type *, 8> NewFuncParamTys(PtrArgTys); |
| 146 | NewFuncParamTys.push_back(PodArgsStructTy); |
| 147 | |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 148 | // We've recorded the remapping for pointer arguments. Now record the |
| 149 | // remapping for POD arguments. |
| 150 | { |
| 151 | const auto StructLayout = |
| 152 | M.getDataLayout().getStructLayout(PodArgsStructTy); |
| 153 | arg_index = 0; |
| 154 | int pod_index = 0; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 155 | for (Argument &Arg : F->args()) { |
| 156 | Type *ArgTy = Arg.getType(); |
| 157 | if (!isa<PointerType>(ArgTy)) { |
| 158 | RemapInfo.push_back( |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 159 | {std::string(Arg.getName()), arg_index, new_index, |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 160 | unsigned(StructLayout->getElementOffset(pod_index++)), |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 161 | clspv::GetArgKindForType(ArgTy), -1}); |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 162 | } |
| 163 | arg_index++; |
| 164 | } |
| 165 | } |
| 166 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 167 | FunctionType *NewFuncTy = |
| 168 | FunctionType::get(F->getReturnType(), NewFuncParamTys, false); |
| 169 | |
| 170 | // Create the new function and set key properties. |
| 171 | auto NewFunc = Function::Create(NewFuncTy, F->getLinkage()); |
| 172 | // The new function adopts the real name so that linkage to the outside |
| 173 | // world remains the same. |
| 174 | NewFunc->setName(F->getName()); |
| 175 | F->setName(NewFunc->getName().str() + ".inner"); |
| 176 | |
| 177 | NewFunc->setCallingConv(F->getCallingConv()); |
| 178 | F->setCallingConv(CallingConv::SPIR_FUNC); |
| 179 | |
| 180 | NewFunc->setAttributes(F->getAttributes()); |
| 181 | // Move OpenCL kernel named attributes. |
| 182 | // TODO(dneto): Attributes starting with kernel_arg_* should be rewritten |
| 183 | // to reflect change in the argument shape. |
| 184 | std::vector<const char *> Metadatas{ |
| 185 | "reqd_work_group_size", "kernel_arg_addr_space", |
| 186 | "kernel_arg_access_qual", "kernel_arg_type", |
| 187 | "kernel_arg_base_type", "kernel_arg_type_qual"}; |
| 188 | for (auto name : Metadatas) { |
| 189 | NewFunc->setMetadata(name, F->getMetadata(name)); |
| 190 | F->setMetadata(name, nullptr); |
| 191 | } |
| 192 | |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 193 | IRBuilder<> Builder(BasicBlock::Create(Context, "entry", NewFunc)); |
| 194 | |
| 195 | // Set kernel argument mapping metadata. |
| 196 | { |
| 197 | // Attach a metadata node named "kernel_arg_map" to the new kernel |
| 198 | // function. It is a tuple of nodes, each of which is a tuple for |
| 199 | // each argument, with members: |
| 200 | // - Argument name |
| 201 | // - Ordinal index in the original kernel function |
| 202 | // - Ordinal index in the new kernel function |
| 203 | // - Byte offset within the argument. This is always 0 for pointer |
| 204 | // arguments. For POD arguments this is the offest within the POD |
| 205 | // argument struct. |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 206 | // - Argument type |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 207 | LLVMContext& Context = M.getContext(); |
| 208 | SmallVector<Metadata*, 8> mappings; |
| 209 | for (auto &arg_mapping : RemapInfo) { |
| 210 | auto *name_md = MDString::get(Context, arg_mapping.name); |
| 211 | auto *old_index_md = |
| 212 | ConstantAsMetadata::get(Builder.getInt32(arg_mapping.old_index)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 213 | auto *new_index_md = ConstantAsMetadata::get( |
| 214 | Builder.getInt32(arg_mapping.new_index)); |
| 215 | auto *offset_md = |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 216 | ConstantAsMetadata::get(Builder.getInt32(arg_mapping.offset)); |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 217 | auto *argtype_md = MDString::get(Context, arg_mapping.arg_kind); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame^] | 218 | auto *spec_id_md = ConstantAsMetadata::get( |
| 219 | Builder.getInt32(arg_mapping.spec_id)); |
| 220 | auto *arg_md = |
| 221 | MDNode::get(Context, {name_md, old_index_md, new_index_md, |
| 222 | offset_md, argtype_md, spec_id_md}); |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 223 | mappings.push_back(arg_md); |
| 224 | } |
| 225 | |
| 226 | NewFunc->setMetadata("kernel_arg_map", MDNode::get(Context, mappings)); |
| 227 | } |
| 228 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 229 | // Insert the function after the original, to preserve ordering |
| 230 | // in the module as much as possible. |
| 231 | auto &FunctionList = M.getFunctionList(); |
| 232 | for (auto Iter = FunctionList.begin(), IterEnd = FunctionList.end(); |
| 233 | Iter != IterEnd; ++Iter) { |
| 234 | if (&*Iter == F) { |
| 235 | FunctionList.insertAfter(Iter, NewFunc); |
| 236 | break; |
| 237 | } |
| 238 | } |
| 239 | |
| 240 | // The body of the wrapper is essentially a call to the original function, |
| 241 | // but we have to unwrap the non-pointer arguments from the struct. |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 242 | |
| 243 | // Map the wrapper's arguments to the callee's arguments. |
| 244 | SmallVector<Argument *, 8> CallerArgs; |
| 245 | for (Argument &Arg : NewFunc->args()) { |
| 246 | CallerArgs.push_back(&Arg); |
| 247 | } |
| 248 | Argument *PodArg = CallerArgs.back(); |
| 249 | PodArg->setName("podargs"); |
| 250 | |
| 251 | SmallVector<Value *, 8> CalleeArgs; |
| 252 | unsigned podIndex = 0; |
| 253 | unsigned ptrIndex = 0; |
| 254 | for (const Argument &Arg : F->args()) { |
| 255 | if (isa<PointerType>(Arg.getType())) { |
| 256 | CalleeArgs.push_back(CallerArgs[ptrIndex++]); |
| 257 | } else { |
| 258 | CalleeArgs.push_back(Builder.CreateExtractValue(PodArg, {podIndex++})); |
| 259 | } |
| 260 | CalleeArgs.back()->setName(Arg.getName()); |
| 261 | } |
| 262 | assert(ptrIndex + podIndex == F->arg_size()); |
| 263 | assert(ptrIndex = PtrArgTys.size()); |
| 264 | assert(podIndex = PodArgTys.size()); |
| 265 | |
| 266 | auto Call = Builder.CreateCall(F, CalleeArgs); |
| 267 | Call->setCallingConv(F->getCallingConv()); |
David Neto | d5b3f98 | 2017-09-28 14:49:49 -0400 | [diff] [blame] | 268 | CallList.push_back(Call); |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 269 | |
| 270 | Builder.CreateRetVoid(); |
| 271 | } |
| 272 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 273 | // Inline the inner function. It's cleaner to do this. |
| 274 | for (CallInst *C : CallList) { |
| 275 | InlineFunctionInfo info; |
| 276 | Changed |= InlineFunction(C, info); |
David Neto | d5b3f98 | 2017-09-28 14:49:49 -0400 | [diff] [blame] | 277 | } |
| 278 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 279 | return Changed; |
| 280 | } |