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