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" |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 33 | #include "llvm/IR/IRBuilder.h" |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 34 | #include "llvm/IR/Instructions.h" |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 35 | #include "llvm/IR/Metadata.h" |
| 36 | #include "llvm/IR/Module.h" |
| 37 | #include "llvm/Pass.h" |
| 38 | #include "llvm/Support/CommandLine.h" |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 39 | #include "llvm/Support/MathExtras.h" |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 40 | #include "llvm/Support/raw_ostream.h" |
| 41 | #include "llvm/Transforms/Utils/Cloning.h" |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 42 | |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 43 | #include "clspv/Option.h" |
| 44 | |
David Neto | 4feb7a4 | 2017-10-06 17:29:42 -0400 | [diff] [blame] | 45 | #include "ArgKind.h" |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 46 | #include "Passes.h" |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 47 | |
| 48 | using namespace llvm; |
| 49 | |
| 50 | #define DEBUG_TYPE "clusterpodkernelargs" |
| 51 | |
| 52 | namespace { |
| 53 | struct ClusterPodKernelArgumentsPass : public ModulePass { |
| 54 | static char ID; |
| 55 | ClusterPodKernelArgumentsPass() : ModulePass(ID) {} |
| 56 | |
| 57 | bool runOnModule(Module &M) override; |
| 58 | }; |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 59 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 60 | } // namespace |
| 61 | |
| 62 | char ClusterPodKernelArgumentsPass::ID = 0; |
Diego Novillo | a4c44fa | 2019-04-11 10:56:15 -0400 | [diff] [blame] | 63 | INITIALIZE_PASS(ClusterPodKernelArgumentsPass, "ClusterPodKernelArgumentsPass", |
| 64 | "Cluster POD Kernel Arguments Pass", false, false) |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 65 | |
| 66 | namespace clspv { |
| 67 | llvm::ModulePass *createClusterPodKernelArgumentsPass() { |
| 68 | return new ClusterPodKernelArgumentsPass(); |
| 69 | } |
| 70 | } // namespace clspv |
| 71 | |
| 72 | bool ClusterPodKernelArgumentsPass::runOnModule(Module &M) { |
| 73 | bool Changed = false; |
| 74 | LLVMContext &Context = M.getContext(); |
| 75 | |
| 76 | SmallVector<Function *, 8> WorkList; |
| 77 | |
| 78 | for (Function &F : M) { |
| 79 | if (F.isDeclaration() || F.getCallingConv() != CallingConv::SPIR_KERNEL) { |
| 80 | continue; |
| 81 | } |
| 82 | for (Argument &Arg : F.args()) { |
| 83 | if (!isa<PointerType>(Arg.getType())) { |
| 84 | WorkList.push_back(&F); |
| 85 | break; |
| 86 | } |
| 87 | } |
| 88 | } |
| 89 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 90 | SmallVector<CallInst *, 8> CallList; |
David Neto | d5b3f98 | 2017-09-28 14:49:49 -0400 | [diff] [blame] | 91 | |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 92 | // Note: The transformation done in this pass preserves the pointer-to-local |
| 93 | // arg to spec-id mapping. |
| 94 | clspv::ArgIdMapType arg_spec_id_map = clspv::AllocateArgSpecIds(M); |
| 95 | |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 96 | for (Function *F : WorkList) { |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 97 | Changed = true; |
| 98 | |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 99 | // An ArgMapping describes how a kernel argument is remapped. |
| 100 | struct ArgMapping { |
| 101 | std::string name; |
| 102 | // 0-based argument index in the old kernel function. |
| 103 | unsigned old_index; |
| 104 | // 0-based argument index in the new kernel function. |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 105 | int new_index; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 106 | // Offset of the argument value within the new kernel argument. |
| 107 | // This is always zero for non-POD arguments. For a POD argument, |
| 108 | // this is the byte offset within the POD arguments struct. |
| 109 | unsigned offset; |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 110 | // Size of the argument |
| 111 | unsigned arg_size; |
Kévin Petit | 8bea15e | 2019-04-09 14:05:17 +0100 | [diff] [blame] | 112 | // Argument type. |
| 113 | clspv::ArgKind arg_kind; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 114 | // If non-negative, this argument is a pointer-to-local, and the value |
| 115 | // here is the specialization constant id for the array size. |
| 116 | int spec_id; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 117 | }; |
| 118 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 119 | // In OpenCL, kernel arguments are either pointers or POD. A composite with |
Kévin Petit | 921c1ab | 2019-03-19 21:25:44 +0000 | [diff] [blame] | 120 | // an element or member that is a pointer is not allowed. So we'll use POD |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 121 | // as a shorthand for non-pointer. |
| 122 | |
| 123 | SmallVector<Type *, 8> PtrArgTys; |
| 124 | SmallVector<Type *, 8> PodArgTys; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 125 | SmallVector<ArgMapping, 8> RemapInfo; |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 126 | DenseMap<Argument *, unsigned> PodIndexMap; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 127 | unsigned arg_index = 0; |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 128 | int new_index = 0; |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 129 | unsigned pod_index = 0; |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 130 | for (Argument &Arg : F->args()) { |
| 131 | Type *ArgTy = Arg.getType(); |
| 132 | if (isa<PointerType>(ArgTy)) { |
| 133 | PtrArgTys.push_back(ArgTy); |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 134 | const auto kind = clspv::GetArgKindForType(ArgTy); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 135 | int spec_id = -1; |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 136 | if (kind == clspv::ArgKind::Local) { |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 137 | spec_id = arg_spec_id_map[&Arg]; |
| 138 | assert(spec_id > 0); |
| 139 | } |
David Neto | 862b7d8 | 2018-06-14 18:48:37 -0400 | [diff] [blame] | 140 | RemapInfo.push_back({std::string(Arg.getName()), arg_index, new_index++, |
Kévin Petit | 8bea15e | 2019-04-09 14:05:17 +0100 | [diff] [blame] | 141 | 0u, 0u, kind, spec_id}); |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 142 | } else { |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 143 | PodIndexMap[&Arg] = pod_index++; |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 144 | PodArgTys.push_back(ArgTy); |
| 145 | } |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 146 | arg_index++; |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 147 | } |
| 148 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 149 | // Put the pointer arguments first, and then POD arguments struct last. |
David Neto | 2ded02e | 2017-10-23 15:30:59 -0400 | [diff] [blame] | 150 | // Use StructType::get so we reuse types where possible. |
| 151 | auto PodArgsStructTy = StructType::get(Context, PodArgTys); |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 152 | SmallVector<Type *, 8> NewFuncParamTys(PtrArgTys); |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 153 | |
| 154 | if (clspv::Option::PodArgsInUniformBuffer() && |
| 155 | !clspv::Option::Std430UniformBufferLayout()) { |
| 156 | SmallVector<Type *, 16> PaddedPodArgTys; |
| 157 | const DataLayout DL(&M); |
| 158 | const auto StructLayout = DL.getStructLayout(PodArgsStructTy); |
| 159 | unsigned pod_index = 0; |
| 160 | for (auto &Arg : F->args()) { |
| 161 | auto arg_type = Arg.getType(); |
| 162 | if (arg_type->isPointerTy()) |
| 163 | continue; |
| 164 | |
| 165 | // The frontend has validated individual POD arguments. When the |
| 166 | // unified struct is constructed, pad struct and array elements as |
| 167 | // necessary to achieve a 16-byte alignment. |
| 168 | if (arg_type->isStructTy() || arg_type->isArrayTy()) { |
| 169 | auto offset = StructLayout->getElementOffset(pod_index); |
| 170 | auto aligned = alignTo(offset, 16); |
| 171 | if (offset < aligned) { |
| 172 | auto int_ty = IntegerType::get(Context, 32); |
| 173 | auto char_ty = IntegerType::get(Context, 8); |
| 174 | size_t num_ints = (aligned - offset) / 4; |
| 175 | size_t num_chars = (aligned - offset) - (num_ints * 4); |
| 176 | assert((num_chars == 0 || clspv::Option::Int8Support()) && |
| 177 | "Char in UBO struct without char support"); |
| 178 | // Fix the index for the offset of the argument. |
| 179 | // Add char padding first. |
| 180 | PodIndexMap[&Arg] += num_ints + num_chars; |
| 181 | for (size_t i = 0; i < num_chars; ++i) { |
| 182 | PaddedPodArgTys.push_back(char_ty); |
| 183 | } |
| 184 | for (size_t i = 0; i < num_ints; ++i) { |
| 185 | PaddedPodArgTys.push_back(int_ty); |
| 186 | } |
| 187 | } |
| 188 | } |
| 189 | ++pod_index; |
| 190 | PaddedPodArgTys.push_back(arg_type); |
| 191 | } |
| 192 | PodArgsStructTy = StructType::get(Context, PaddedPodArgTys); |
| 193 | } |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 194 | NewFuncParamTys.push_back(PodArgsStructTy); |
| 195 | |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 196 | // We've recorded the remapping for pointer arguments. Now record the |
| 197 | // remapping for POD arguments. |
| 198 | { |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 199 | const DataLayout DL(&M); |
| 200 | const auto StructLayout = DL.getStructLayout(PodArgsStructTy); |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 201 | arg_index = 0; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 202 | for (Argument &Arg : F->args()) { |
| 203 | Type *ArgTy = Arg.getType(); |
| 204 | if (!isa<PointerType>(ArgTy)) { |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 205 | unsigned arg_size = DL.getTypeStoreSize(ArgTy); |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 206 | RemapInfo.push_back( |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 207 | {std::string(Arg.getName()), arg_index, new_index, |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 208 | unsigned(StructLayout->getElementOffset(PodIndexMap[&Arg])), |
| 209 | arg_size, clspv::GetArgKindForType(ArgTy), -1}); |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 210 | } |
| 211 | arg_index++; |
| 212 | } |
| 213 | } |
| 214 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 215 | FunctionType *NewFuncTy = |
| 216 | FunctionType::get(F->getReturnType(), NewFuncParamTys, false); |
| 217 | |
| 218 | // Create the new function and set key properties. |
| 219 | auto NewFunc = Function::Create(NewFuncTy, F->getLinkage()); |
| 220 | // The new function adopts the real name so that linkage to the outside |
| 221 | // world remains the same. |
| 222 | NewFunc->setName(F->getName()); |
| 223 | F->setName(NewFunc->getName().str() + ".inner"); |
| 224 | |
| 225 | NewFunc->setCallingConv(F->getCallingConv()); |
| 226 | F->setCallingConv(CallingConv::SPIR_FUNC); |
| 227 | |
Kévin Petit | 921c1ab | 2019-03-19 21:25:44 +0000 | [diff] [blame] | 228 | // Transfer attributes that don't apply to the POD arguments |
| 229 | // to the new functions. |
| 230 | auto Attributes = F->getAttributes(); |
| 231 | SmallVector<std::pair<unsigned, AttributeSet>, 8> AttrBuildInfo; |
| 232 | |
| 233 | // Return attributes have to come first |
| 234 | if (Attributes.hasAttributes(AttributeList::ReturnIndex)) { |
| 235 | auto idx = AttributeList::ReturnIndex; |
| 236 | auto attrs = Attributes.getRetAttributes(); |
| 237 | AttrBuildInfo.push_back(std::make_pair(idx, attrs)); |
| 238 | } |
| 239 | |
Kévin Petit | 8bea15e | 2019-04-09 14:05:17 +0100 | [diff] [blame] | 240 | // Then attributes for non-POD parameters |
Kévin Petit | 921c1ab | 2019-03-19 21:25:44 +0000 | [diff] [blame] | 241 | for (auto &rinfo : RemapInfo) { |
Kévin Petit | 8bea15e | 2019-04-09 14:05:17 +0100 | [diff] [blame] | 242 | bool argIsPod = rinfo.arg_kind == clspv::ArgKind::Pod || |
alan-baker | 9b0ec3c | 2020-04-06 14:45:34 -0400 | [diff] [blame] | 243 | rinfo.arg_kind == clspv::ArgKind::PodUBO || |
| 244 | rinfo.arg_kind == clspv::ArgKind::PodPushConstant; |
Kévin Petit | 8bea15e | 2019-04-09 14:05:17 +0100 | [diff] [blame] | 245 | if (!argIsPod && Attributes.hasParamAttrs(rinfo.old_index)) { |
Kévin Petit | 921c1ab | 2019-03-19 21:25:44 +0000 | [diff] [blame] | 246 | auto idx = rinfo.new_index + AttributeList::FirstArgIndex; |
| 247 | auto attrs = Attributes.getParamAttributes(rinfo.old_index); |
| 248 | AttrBuildInfo.push_back(std::make_pair(idx, attrs)); |
| 249 | } |
| 250 | } |
| 251 | |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 252 | // And finally function attributes. |
Kévin Petit | 921c1ab | 2019-03-19 21:25:44 +0000 | [diff] [blame] | 253 | if (Attributes.hasAttributes(AttributeList::FunctionIndex)) { |
Kévin Petit | 921c1ab | 2019-03-19 21:25:44 +0000 | [diff] [blame] | 254 | auto idx = AttributeList::FunctionIndex; |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 255 | auto attrs = Attributes.getFnAttributes(); |
| 256 | AttrBuildInfo.push_back(std::make_pair(idx, attrs)); |
Kévin Petit | 921c1ab | 2019-03-19 21:25:44 +0000 | [diff] [blame] | 257 | } |
alan-baker | bccf62c | 2019-03-29 10:32:41 -0400 | [diff] [blame] | 258 | auto newAttributes = AttributeList::get(M.getContext(), AttrBuildInfo); |
Kévin Petit | 921c1ab | 2019-03-19 21:25:44 +0000 | [diff] [blame] | 259 | NewFunc->setAttributes(newAttributes); |
| 260 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 261 | // Move OpenCL kernel named attributes. |
| 262 | // TODO(dneto): Attributes starting with kernel_arg_* should be rewritten |
| 263 | // to reflect change in the argument shape. |
| 264 | std::vector<const char *> Metadatas{ |
| 265 | "reqd_work_group_size", "kernel_arg_addr_space", |
| 266 | "kernel_arg_access_qual", "kernel_arg_type", |
| 267 | "kernel_arg_base_type", "kernel_arg_type_qual"}; |
| 268 | for (auto name : Metadatas) { |
| 269 | NewFunc->setMetadata(name, F->getMetadata(name)); |
| 270 | F->setMetadata(name, nullptr); |
| 271 | } |
| 272 | |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 273 | IRBuilder<> Builder(BasicBlock::Create(Context, "entry", NewFunc)); |
| 274 | |
| 275 | // Set kernel argument mapping metadata. |
| 276 | { |
| 277 | // Attach a metadata node named "kernel_arg_map" to the new kernel |
| 278 | // function. It is a tuple of nodes, each of which is a tuple for |
| 279 | // each argument, with members: |
| 280 | // - Argument name |
| 281 | // - Ordinal index in the original kernel function |
| 282 | // - Ordinal index in the new kernel function |
| 283 | // - Byte offset within the argument. This is always 0 for pointer |
| 284 | // arguments. For POD arguments this is the offest within the POD |
| 285 | // argument struct. |
David Neto | 48f56a4 | 2017-10-06 16:44:25 -0400 | [diff] [blame] | 286 | // - Argument type |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 287 | LLVMContext &Context = M.getContext(); |
| 288 | SmallVector<Metadata *, 8> mappings; |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 289 | for (auto &arg_mapping : RemapInfo) { |
| 290 | auto *name_md = MDString::get(Context, arg_mapping.name); |
| 291 | auto *old_index_md = |
| 292 | ConstantAsMetadata::get(Builder.getInt32(arg_mapping.old_index)); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 293 | auto *new_index_md = |
| 294 | ConstantAsMetadata::get(Builder.getInt32(arg_mapping.new_index)); |
David Neto | c6f3ab2 | 2018-04-06 18:02:31 -0400 | [diff] [blame] | 295 | auto *offset_md = |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 296 | ConstantAsMetadata::get(Builder.getInt32(arg_mapping.offset)); |
Kévin PETIT | a353c83 | 2018-03-20 23:21:21 +0000 | [diff] [blame] | 297 | auto *arg_size_md = |
| 298 | ConstantAsMetadata::get(Builder.getInt32(arg_mapping.arg_size)); |
Kévin Petit | 8bea15e | 2019-04-09 14:05:17 +0100 | [diff] [blame] | 299 | auto argKindName = GetArgKindName(arg_mapping.arg_kind); |
| 300 | auto *argtype_md = MDString::get(Context, argKindName); |
Diego Novillo | 3cc8d7a | 2019-04-10 13:30:34 -0400 | [diff] [blame] | 301 | auto *spec_id_md = |
| 302 | ConstantAsMetadata::get(Builder.getInt32(arg_mapping.spec_id)); |
| 303 | auto *arg_md = MDNode::get( |
| 304 | Context, {name_md, old_index_md, new_index_md, offset_md, |
| 305 | arg_size_md, argtype_md, spec_id_md}); |
David Neto | 156783e | 2017-07-05 15:39:41 -0400 | [diff] [blame] | 306 | mappings.push_back(arg_md); |
| 307 | } |
| 308 | |
| 309 | NewFunc->setMetadata("kernel_arg_map", MDNode::get(Context, mappings)); |
| 310 | } |
| 311 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 312 | // Insert the function after the original, to preserve ordering |
| 313 | // in the module as much as possible. |
| 314 | auto &FunctionList = M.getFunctionList(); |
| 315 | for (auto Iter = FunctionList.begin(), IterEnd = FunctionList.end(); |
| 316 | Iter != IterEnd; ++Iter) { |
| 317 | if (&*Iter == F) { |
| 318 | FunctionList.insertAfter(Iter, NewFunc); |
| 319 | break; |
| 320 | } |
| 321 | } |
| 322 | |
| 323 | // The body of the wrapper is essentially a call to the original function, |
| 324 | // but we have to unwrap the non-pointer arguments from the struct. |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 325 | |
| 326 | // Map the wrapper's arguments to the callee's arguments. |
| 327 | SmallVector<Argument *, 8> CallerArgs; |
| 328 | for (Argument &Arg : NewFunc->args()) { |
| 329 | CallerArgs.push_back(&Arg); |
| 330 | } |
| 331 | Argument *PodArg = CallerArgs.back(); |
| 332 | PodArg->setName("podargs"); |
| 333 | |
| 334 | SmallVector<Value *, 8> CalleeArgs; |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 335 | unsigned podCount = 0; |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 336 | unsigned ptrIndex = 0; |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 337 | for (Argument &Arg : F->args()) { |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 338 | if (isa<PointerType>(Arg.getType())) { |
| 339 | CalleeArgs.push_back(CallerArgs[ptrIndex++]); |
| 340 | } else { |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 341 | podCount++; |
| 342 | unsigned podIndex = PodIndexMap[&Arg]; |
| 343 | CalleeArgs.push_back(Builder.CreateExtractValue(PodArg, {podIndex})); |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 344 | } |
| 345 | CalleeArgs.back()->setName(Arg.getName()); |
| 346 | } |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 347 | assert(ptrIndex + podCount == F->arg_size()); |
Kévin Petit | 98d9c33 | 2019-03-13 15:03:40 +0000 | [diff] [blame] | 348 | assert(ptrIndex == PtrArgTys.size()); |
alan-baker | 038e924 | 2019-04-19 22:14:41 -0400 | [diff] [blame] | 349 | assert(podCount != 0); |
| 350 | assert(podCount == PodArgTys.size()); |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 351 | |
| 352 | auto Call = Builder.CreateCall(F, CalleeArgs); |
| 353 | Call->setCallingConv(F->getCallingConv()); |
David Neto | d5b3f98 | 2017-09-28 14:49:49 -0400 | [diff] [blame] | 354 | CallList.push_back(Call); |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 355 | |
| 356 | Builder.CreateRetVoid(); |
| 357 | } |
| 358 | |
David Neto | 482550a | 2018-03-24 05:21:07 -0700 | [diff] [blame] | 359 | // Inline the inner function. It's cleaner to do this. |
| 360 | for (CallInst *C : CallList) { |
| 361 | InlineFunctionInfo info; |
alan-baker | 741fd1f | 2020-04-14 17:38:15 -0400 | [diff] [blame] | 362 | Changed |= InlineFunction(*C, info).isSuccess(); |
David Neto | d5b3f98 | 2017-09-28 14:49:49 -0400 | [diff] [blame] | 363 | } |
| 364 | |
David Neto | dd99221 | 2017-06-23 17:47:55 -0400 | [diff] [blame] | 365 | return Changed; |
| 366 | } |