David Neto | 22f144c | 2017-06-12 14:26:21 -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 | e345e0e | 2018-06-15 11:38:32 -0400 | [diff] [blame] | 15 | #include "llvm/IR/Constants.h" |
| 16 | #include "llvm/IR/IRBuilder.h" |
| 17 | #include "llvm/IR/Instructions.h" |
| 18 | #include "llvm/IR/Module.h" |
| 19 | #include "llvm/Pass.h" |
| 20 | #include "llvm/Support/raw_ostream.h" |
| 21 | #include "llvm/Transforms/Utils/Cloning.h" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 22 | |
David Neto | e345e0e | 2018-06-15 11:38:32 -0400 | [diff] [blame] | 23 | #include "spirv/1.0/spirv.hpp" |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 24 | |
| 25 | using namespace llvm; |
| 26 | |
| 27 | #define DEBUG_TYPE "ReplaceLLVMIntrinsics" |
| 28 | |
| 29 | namespace { |
| 30 | struct ReplaceLLVMIntrinsicsPass final : public ModulePass { |
| 31 | static char ID; |
| 32 | ReplaceLLVMIntrinsicsPass() : ModulePass(ID) {} |
| 33 | |
| 34 | bool runOnModule(Module &M) override; |
| 35 | bool replaceMemset(Module &M); |
| 36 | bool replaceMemcpy(Module &M); |
David Neto | e345e0e | 2018-06-15 11:38:32 -0400 | [diff] [blame] | 37 | bool removeLifetimeDeclarations(Module &M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 38 | }; |
| 39 | } |
| 40 | |
| 41 | char ReplaceLLVMIntrinsicsPass::ID = 0; |
| 42 | static RegisterPass<ReplaceLLVMIntrinsicsPass> |
| 43 | X("ReplaceLLVMIntrinsics", "Replace LLVM intrinsics Pass"); |
| 44 | |
| 45 | namespace clspv { |
| 46 | ModulePass *createReplaceLLVMIntrinsicsPass() { |
| 47 | return new ReplaceLLVMIntrinsicsPass(); |
| 48 | } |
| 49 | } |
| 50 | |
| 51 | bool ReplaceLLVMIntrinsicsPass::runOnModule(Module &M) { |
| 52 | bool Changed = false; |
| 53 | |
David Neto | e345e0e | 2018-06-15 11:38:32 -0400 | [diff] [blame] | 54 | // Remove lifetime annotations first. They coulud be using memset |
| 55 | // and memcpy calls. |
| 56 | Changed |= removeLifetimeDeclarations(M); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 57 | Changed |= replaceMemset(M); |
| 58 | Changed |= replaceMemcpy(M); |
| 59 | |
| 60 | return Changed; |
| 61 | } |
| 62 | |
| 63 | bool ReplaceLLVMIntrinsicsPass::replaceMemset(Module &M) { |
| 64 | bool Changed = false; |
David Neto | d3f5938 | 2017-10-18 18:30:30 -0400 | [diff] [blame] | 65 | auto Layout = M.getDataLayout(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 66 | |
| 67 | for (auto &F : M) { |
| 68 | if (F.getName().startswith("llvm.memset")) { |
| 69 | SmallVector<CallInst *, 8> CallsToReplace; |
| 70 | |
| 71 | for (auto U : F.users()) { |
| 72 | if (auto CI = dyn_cast<CallInst>(U)) { |
| 73 | auto Initializer = dyn_cast<ConstantInt>(CI->getArgOperand(1)); |
| 74 | |
| 75 | // We only handle cases where the initializer is a constant int that |
| 76 | // is 0. |
| 77 | if (!Initializer || (0 != Initializer->getZExtValue())) { |
| 78 | Initializer->print(errs()); |
| 79 | llvm_unreachable("Unhandled llvm.memset.* instruction that had a " |
| 80 | "non-0 initializer!"); |
| 81 | } |
| 82 | |
| 83 | CallsToReplace.push_back(CI); |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | for (auto CI : CallsToReplace) { |
| 88 | auto NewArg = CI->getArgOperand(0); |
| 89 | |
| 90 | if (auto Bitcast = dyn_cast<BitCastInst>(NewArg)) { |
| 91 | NewArg = Bitcast->getOperand(0); |
| 92 | } |
| 93 | |
David Neto | d3f5938 | 2017-10-18 18:30:30 -0400 | [diff] [blame] | 94 | auto NumBytes = cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue(); |
| 95 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 96 | auto Ty = NewArg->getType(); |
| 97 | auto PointeeTy = Ty->getPointerElementType(); |
| 98 | |
| 99 | auto NewFType = |
| 100 | FunctionType::get(F.getReturnType(), {Ty, PointeeTy}, false); |
| 101 | |
| 102 | // Create our fake intrinsic to initialize it to 0. |
| 103 | auto SPIRVIntrinsic = "spirv.store_null"; |
| 104 | |
| 105 | auto NewF = |
| 106 | Function::Create(NewFType, F.getLinkage(), SPIRVIntrinsic, &M); |
| 107 | |
| 108 | auto Zero = Constant::getNullValue(PointeeTy); |
| 109 | |
David Neto | d3f5938 | 2017-10-18 18:30:30 -0400 | [diff] [blame] | 110 | const auto num_stores = NumBytes / Layout.getTypeAllocSize(PointeeTy); |
| 111 | assert((NumBytes == num_stores * Layout.getTypeAllocSize(PointeeTy)) && |
| 112 | "Null memset can't be divided evenly across multiple stores."); |
| 113 | assert((num_stores & 0xFFFFFFFF) == num_stores); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 114 | |
David Neto | d3f5938 | 2017-10-18 18:30:30 -0400 | [diff] [blame] | 115 | // Generate the first store. |
| 116 | CallInst::Create(NewF, {NewArg, Zero}, "", CI); |
| 117 | |
| 118 | // Generate subsequent stores, but only if needed. |
| 119 | if (num_stores) { |
| 120 | auto I32Ty = Type::getInt32Ty(M.getContext()); |
| 121 | auto One = ConstantInt::get(I32Ty, 1); |
| 122 | auto Ptr = NewArg; |
| 123 | for (uint32_t i = 1; i < num_stores; i++) { |
| 124 | Ptr = GetElementPtrInst::Create(PointeeTy, Ptr, {One}, "", CI); |
| 125 | CallInst::Create(NewF, {Ptr, Zero}, "", CI); |
| 126 | } |
| 127 | } |
| 128 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 129 | CI->eraseFromParent(); |
| 130 | |
| 131 | if (auto Bitcast = dyn_cast<BitCastInst>(NewArg)) { |
| 132 | Bitcast->eraseFromParent(); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | } |
| 137 | |
| 138 | return Changed; |
| 139 | } |
| 140 | |
| 141 | bool ReplaceLLVMIntrinsicsPass::replaceMemcpy(Module &M) { |
| 142 | bool Changed = false; |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 143 | auto Layout = M.getDataLayout(); |
| 144 | |
| 145 | // Unpack source and destination types until we find a matching |
| 146 | // element type. Count the number of levels we unpack for the |
| 147 | // source and destination types. So far this only works for |
| 148 | // array types, but could be generalized to other regular types |
| 149 | // like vectors. |
Alan Baker | 7dea884 | 2018-10-22 10:15:41 -0400 | [diff] [blame^] | 150 | auto match_types = [&Layout](CallInst &CI, uint64_t Size, Type **DstElemTy, |
| 151 | Type **SrcElemTy, unsigned *NumDstUnpackings, |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 152 | unsigned *NumSrcUnpackings) { |
Alan Baker | 7dea884 | 2018-10-22 10:15:41 -0400 | [diff] [blame^] | 153 | auto descend_type = [](Type *InType) { |
| 154 | Type *OutType = InType; |
| 155 | if (OutType->isStructTy()) { |
| 156 | OutType = OutType->getStructElementType(0); |
| 157 | } else if (OutType->isArrayTy()) { |
| 158 | OutType = OutType->getArrayElementType(); |
| 159 | } else if (OutType->isVectorTy()) { |
| 160 | OutType = OutType->getVectorElementType(); |
| 161 | } else { |
| 162 | assert(false && "Don't know how to descend into type"); |
| 163 | } |
| 164 | |
| 165 | return OutType; |
| 166 | }; |
| 167 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 168 | unsigned *numSrcUnpackings = 0; |
| 169 | unsigned *numDstUnpackings = 0; |
| 170 | while (*SrcElemTy != *DstElemTy) { |
| 171 | auto SrcElemSize = Layout.getTypeSizeInBits(*SrcElemTy); |
| 172 | auto DstElemSize = Layout.getTypeSizeInBits(*DstElemTy); |
| 173 | if (SrcElemSize >= DstElemSize) { |
Alan Baker | 7dea884 | 2018-10-22 10:15:41 -0400 | [diff] [blame^] | 174 | *SrcElemTy = descend_type(*SrcElemTy); |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 175 | (*NumSrcUnpackings)++; |
| 176 | } else if (DstElemSize >= SrcElemSize) { |
Alan Baker | 7dea884 | 2018-10-22 10:15:41 -0400 | [diff] [blame^] | 177 | *DstElemTy = descend_type(*DstElemTy); |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 178 | (*NumDstUnpackings)++; |
| 179 | } else { |
| 180 | errs() << "Don't know how to unpack types for memcpy: " << CI |
| 181 | << "\ngot to: " << **DstElemTy << " vs " << **SrcElemTy << "\n"; |
| 182 | assert(false && "Don't know how to unpack these types"); |
| 183 | } |
| 184 | } |
Alan Baker | 7dea884 | 2018-10-22 10:15:41 -0400 | [diff] [blame^] | 185 | |
| 186 | auto DstElemSize = Layout.getTypeSizeInBits(*DstElemTy) / 8; |
| 187 | while (Size < DstElemSize) { |
| 188 | *DstElemTy = descend_type(*DstElemTy); |
| 189 | *SrcElemTy = descend_type(*SrcElemTy); |
| 190 | (*NumDstUnpackings)++; |
| 191 | (*NumSrcUnpackings)++; |
| 192 | DstElemSize = Layout.getTypeSizeInBits(*DstElemTy) / 8; |
| 193 | } |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 194 | }; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 195 | |
| 196 | for (auto &F : M) { |
| 197 | if (F.getName().startswith("llvm.memcpy")) { |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 198 | SmallPtrSet<Instruction *, 8> BitCastsToForget; |
| 199 | SmallVector<CallInst *, 8> CallsToReplaceWithSpirvCopyMemory; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 200 | |
| 201 | for (auto U : F.users()) { |
| 202 | if (auto CI = dyn_cast<CallInst>(U)) { |
| 203 | assert(isa<BitCastInst>(CI->getArgOperand(0))); |
| 204 | auto Dst = dyn_cast<BitCastInst>(CI->getArgOperand(0))->getOperand(0); |
| 205 | |
| 206 | assert(isa<BitCastInst>(CI->getArgOperand(1))); |
| 207 | auto Src = dyn_cast<BitCastInst>(CI->getArgOperand(1))->getOperand(0); |
| 208 | |
| 209 | // The original type of Dst we get from the argument to the bitcast |
| 210 | // instruction. |
| 211 | auto DstTy = Dst->getType(); |
| 212 | assert(DstTy->isPointerTy()); |
| 213 | |
| 214 | // The original type of Src we get from the argument to the bitcast |
| 215 | // instruction. |
| 216 | auto SrcTy = Src->getType(); |
| 217 | assert(SrcTy->isPointerTy()); |
| 218 | |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 219 | // Check that the size is a constant integer. |
| 220 | assert(isa<ConstantInt>(CI->getArgOperand(2))); |
| 221 | auto Size = |
| 222 | dyn_cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue(); |
| 223 | |
Alan Baker | 7dea884 | 2018-10-22 10:15:41 -0400 | [diff] [blame^] | 224 | auto DstElemTy = DstTy->getPointerElementType(); |
| 225 | auto SrcElemTy = SrcTy->getPointerElementType(); |
| 226 | unsigned NumDstUnpackings = 0; |
| 227 | unsigned NumSrcUnpackings = 0; |
| 228 | match_types(*CI, Size, &DstElemTy, &SrcElemTy, &NumDstUnpackings, |
| 229 | &NumSrcUnpackings); |
| 230 | |
| 231 | // Check that the pointee types match. |
| 232 | assert(DstElemTy == SrcElemTy); |
| 233 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 234 | auto DstElemSize = Layout.getTypeSizeInBits(DstElemTy) / 8; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 235 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 236 | // Check that the size is a multiple of the size of the pointee type. |
| 237 | assert(Size % DstElemSize == 0); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 238 | |
| 239 | // Check that the alignment is a constant integer. |
| 240 | assert(isa<ConstantInt>(CI->getArgOperand(3))); |
| 241 | auto Alignment = |
| 242 | dyn_cast<ConstantInt>(CI->getArgOperand(3))->getZExtValue(); |
| 243 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 244 | auto TypeAlignment = Layout.getABITypeAlignment(DstElemTy); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 245 | |
| 246 | // Check that the alignment is at least the alignment of the pointee |
| 247 | // type. |
| 248 | assert(Alignment >= TypeAlignment); |
| 249 | |
| 250 | // Check that the alignment is a multiple of the alignment of the |
| 251 | // pointee type. |
| 252 | assert(0 == (Alignment % TypeAlignment)); |
| 253 | |
| 254 | // Check that volatile is a constant. |
| 255 | assert(isa<ConstantInt>(CI->getArgOperand(4))); |
| 256 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 257 | CallsToReplaceWithSpirvCopyMemory.push_back(CI); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 258 | } |
| 259 | } |
| 260 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 261 | for (auto CI : CallsToReplaceWithSpirvCopyMemory) { |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 262 | auto Arg0 = dyn_cast<BitCastInst>(CI->getArgOperand(0)); |
| 263 | auto Arg1 = dyn_cast<BitCastInst>(CI->getArgOperand(1)); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 264 | auto Arg3 = dyn_cast<ConstantInt>(CI->getArgOperand(3)); |
| 265 | auto Arg4 = dyn_cast<ConstantInt>(CI->getArgOperand(4)); |
| 266 | |
| 267 | auto I32Ty = Type::getInt32Ty(M.getContext()); |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 268 | auto Alignment = ConstantInt::get(I32Ty, Arg3->getZExtValue()); |
| 269 | auto Volatile = ConstantInt::get(I32Ty, Arg4->getZExtValue()); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 270 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 271 | auto Dst = dyn_cast<BitCastInst>(Arg0)->getOperand(0); |
| 272 | auto Src = dyn_cast<BitCastInst>(Arg1)->getOperand(0); |
| 273 | |
| 274 | auto DstElemTy = Dst->getType()->getPointerElementType(); |
| 275 | auto SrcElemTy = Src->getType()->getPointerElementType(); |
| 276 | unsigned NumDstUnpackings = 0; |
| 277 | unsigned NumSrcUnpackings = 0; |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 278 | auto Size = dyn_cast<ConstantInt>(CI->getArgOperand(2))->getZExtValue(); |
Alan Baker | 7dea884 | 2018-10-22 10:15:41 -0400 | [diff] [blame^] | 279 | match_types(*CI, Size, &DstElemTy, &SrcElemTy, &NumDstUnpackings, |
| 280 | &NumSrcUnpackings); |
| 281 | auto SPIRVIntrinsic = "spirv.copy_memory"; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 282 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 283 | auto DstElemSize = Layout.getTypeSizeInBits(DstElemTy) / 8; |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 284 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 285 | IRBuilder<> Builder(CI); |
| 286 | |
| 287 | if (NumSrcUnpackings == 0 && NumDstUnpackings == 0) { |
| 288 | auto NewFType = FunctionType::get( |
| 289 | F.getReturnType(), {Dst->getType(), Src->getType(), I32Ty, I32Ty}, |
| 290 | false); |
| 291 | auto NewF = |
| 292 | Function::Create(NewFType, F.getLinkage(), SPIRVIntrinsic, &M); |
| 293 | Builder.CreateCall(NewF, {Dst, Src, Alignment, Volatile}, ""); |
| 294 | } else { |
| 295 | auto Zero = ConstantInt::get(I32Ty, 0); |
| 296 | SmallVector<Value *, 3> SrcIndices; |
| 297 | SmallVector<Value *, 3> DstIndices; |
| 298 | // Make unpacking indices. |
| 299 | for (unsigned unpacking = 0; unpacking < NumSrcUnpackings; |
| 300 | ++unpacking) { |
| 301 | SrcIndices.push_back(Zero); |
| 302 | } |
| 303 | for (unsigned unpacking = 0; unpacking < NumDstUnpackings; |
| 304 | ++unpacking) { |
| 305 | DstIndices.push_back(Zero); |
| 306 | } |
| 307 | // Add a placeholder for the final index. |
| 308 | SrcIndices.push_back(Zero); |
| 309 | DstIndices.push_back(Zero); |
| 310 | |
| 311 | // Build the function and function type only once. |
| 312 | FunctionType* NewFType = nullptr; |
| 313 | Function* NewF = nullptr; |
| 314 | |
| 315 | IRBuilder<> Builder(CI); |
| 316 | for (unsigned i = 0; i < Size / DstElemSize; ++i) { |
| 317 | auto Index = ConstantInt::get(I32Ty, i); |
| 318 | SrcIndices.back() = Index; |
| 319 | DstIndices.back() = Index; |
| 320 | |
| 321 | auto SrcElemPtr = Builder.CreateGEP(Src, SrcIndices); |
| 322 | auto DstElemPtr = Builder.CreateGEP(Dst, DstIndices); |
| 323 | NewFType = |
| 324 | NewFType != nullptr |
| 325 | ? NewFType |
| 326 | : FunctionType::get(F.getReturnType(), |
| 327 | {DstElemPtr->getType(), |
| 328 | SrcElemPtr->getType(), I32Ty, I32Ty}, |
| 329 | false); |
| 330 | NewF = NewF != nullptr ? NewF |
| 331 | : Function::Create(NewFType, F.getLinkage(), |
| 332 | SPIRVIntrinsic, &M); |
| 333 | Builder.CreateCall( |
| 334 | NewF, {DstElemPtr, SrcElemPtr, Alignment, Volatile}, ""); |
| 335 | } |
| 336 | } |
| 337 | |
| 338 | // Erase the call. |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 339 | CI->eraseFromParent(); |
| 340 | |
David Neto | b84ba34 | 2017-06-19 17:55:37 -0400 | [diff] [blame] | 341 | // Erase the bitcasts. A particular bitcast might be used |
| 342 | // in more than one memcpy, so defer actual deleting until later. |
| 343 | BitCastsToForget.insert(Arg0); |
| 344 | BitCastsToForget.insert(Arg1); |
| 345 | } |
| 346 | for (auto* Inst : BitCastsToForget) { |
| 347 | Inst->eraseFromParent(); |
David Neto | 22f144c | 2017-06-12 14:26:21 -0400 | [diff] [blame] | 348 | } |
| 349 | } |
| 350 | } |
| 351 | |
| 352 | return Changed; |
| 353 | } |
David Neto | e345e0e | 2018-06-15 11:38:32 -0400 | [diff] [blame] | 354 | |
| 355 | bool ReplaceLLVMIntrinsicsPass::removeLifetimeDeclarations(Module &M) { |
| 356 | // SPIR-V OpLifetimeStart and OpLifetimeEnd require Kernel capability. |
| 357 | // Vulkan doesn't support that, so remove all lifteime bounds declarations. |
| 358 | |
| 359 | bool Changed = false; |
| 360 | |
| 361 | SmallVector<Function *, 2> WorkList; |
| 362 | for (auto &F : M) { |
| 363 | if (F.getName().startswith("llvm.lifetime.")) { |
| 364 | WorkList.push_back(&F); |
| 365 | } |
| 366 | } |
| 367 | |
| 368 | for (auto *F : WorkList) { |
| 369 | Changed = true; |
| 370 | for (auto U : F->users()) { |
| 371 | if (auto *CI = dyn_cast<CallInst>(U)) { |
| 372 | CI->eraseFromParent(); |
| 373 | } |
| 374 | } |
| 375 | F->eraseFromParent(); |
| 376 | } |
| 377 | |
| 378 | return Changed; |
| 379 | } |