| 1 |
//===- SPIRVLowerMemmove.cpp - Lower llvm.memmove to llvm.memcpys ---------===// |
| 2 |
// |
| 3 |
// The LLVM/SPIRV Translator |
| 4 |
// |
| 5 |
// This file is distributed under the University of Illinois Open Source |
| 6 |
// License. See LICENSE.TXT for details. |
| 7 |
// |
| 8 |
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved. |
| 9 |
// |
| 10 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 11 |
// copy of this software and associated documentation files (the "Software"), |
| 12 |
// to deal with the Software without restriction, including without limitation |
| 13 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 15 |
// Software is furnished to do so, subject to the following conditions: |
| 16 |
// |
| 17 |
// Redistributions of source code must retain the above copyright notice, |
| 18 |
// this list of conditions and the following disclaimers. |
| 19 |
// Redistributions in binary form must reproduce the above copyright notice, |
| 20 |
// this list of conditions and the following disclaimers in the documentation |
| 21 |
// and/or other materials provided with the distribution. |
| 22 |
// Neither the names of Advanced Micro Devices, Inc., nor the names of its |
| 23 |
// contributors may be used to endorse or promote products derived from this |
| 24 |
// Software without specific prior written permission. |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 |
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 31 |
// THE SOFTWARE. |
| 32 |
// |
| 33 |
//===----------------------------------------------------------------------===// |
| 34 |
// |
| 35 |
// This file implements lowering llvm.memmove into several llvm.memcpys. |
| 36 |
// |
| 37 |
//===----------------------------------------------------------------------===// |
| 38 |
#define DEBUG_TYPE "spvmemmove" |
| 39 |
|
| 40 |
#include "SPIRVLowerMemmove.h" |
| 41 |
#include "SPIRVInternal.h" |
| 42 |
#include "libSPIRV/SPIRVDebug.h" |
| 43 |
|
| 44 |
#include "llvm/IR/IRBuilder.h" |
| 45 |
#include "llvm/Transforms/Utils/LowerMemIntrinsics.h" |
| 46 |
|
| 47 |
using namespace llvm; |
| 48 |
using namespace SPIRV; |
| 49 |
|
| 50 |
namespace SPIRV { |
| 51 |
|
| 52 |
void SPIRVLowerMemmoveBase::LowerMemMoveInst(MemMoveInst &I) { |
| 53 |
// There is no direct equivalent of @llvm.memmove in SPIR-V and the closest |
| 54 |
// instructions are 'OpCopyMemory' and 'OpCopyMemorySized'. |
| 55 |
// |
| 56 |
// 'OpCopyMemory' does not accept amount of bytes to copy and infers that |
| 57 |
// from type which is being copied; also it only allows to copy value of a |
| 58 |
// particular type to pointer pointing to the same type. |
| 59 |
// |
| 60 |
// 'OpCopyMemorySized' is closer to @llvm.memmove, because it actually |
| 61 |
// copies bytes, but unlike memove it is not explicitly specified whether it |
| 62 |
// supports overlapping source and destination. Therefore, we replace |
| 63 |
// memmove with two 'OpCopyMemorySized' instructions: the first one copies |
| 64 |
// bytes from source to a temporary location, the second one copies bytes |
| 65 |
// from that temporary location to the destination. |
| 66 |
IRBuilder<> Builder(I.getParent()); |
| 67 |
Builder.SetInsertPoint(&I); |
| 68 |
|
| 69 |
auto *Length = cast<ConstantInt>(I.getLength()); |
| 70 |
auto *AllocaTy = |
| 71 |
ArrayType::get(IntegerType::getInt8Ty(*Context), Length->getZExtValue()); |
| 72 |
MaybeAlign SrcAlign = I.getSourceAlign(); |
| 73 |
|
| 74 |
auto *Alloca = Builder.CreateAlloca(AllocaTy); |
| 75 |
if (SrcAlign.has_value()) |
| 76 |
Alloca->setAlignment(SrcAlign.value()); |
| 77 |
|
| 78 |
// FIXME: Do we need to pass the size of alloca here? From LangRef: |
| 79 |
// > The first argument is a constant integer representing the size of the |
| 80 |
// > object, or -1 if it is variable sized. |
| 81 |
// |
| 82 |
// https://llvm.org/docs/LangRef.html#llvm-lifetime-start-intrinsic |
| 83 |
Builder.CreateLifetimeStart(Alloca); |
| 84 |
Builder.CreateMemCpy(Alloca, SrcAlign, I.getRawSource(), SrcAlign, Length, |
| 85 |
I.isVolatile()); |
| 86 |
|
| 87 |
auto *SecondCpy = |
| 88 |
Builder.CreateMemCpy(I.getRawDest(), I.getDestAlign(), Alloca, SrcAlign, |
| 89 |
Length, I.isVolatile()); |
| 90 |
Builder.CreateLifetimeEnd(Alloca); |
| 91 |
|
| 92 |
SecondCpy->takeName(&I); |
| 93 |
I.replaceAllUsesWith(SecondCpy); |
| 94 |
I.dropAllReferences(); |
| 95 |
I.eraseFromParent(); |
| 96 |
} |
| 97 |
|
| 98 |
bool SPIRVLowerMemmoveBase::expandMemMoveIntrinsicUses(Function &F) { |
| 99 |
bool Changed = false; |
| 100 |
|
| 101 |
for (User *U : make_early_inc_range(F.users())) { |
| 102 |
MemMoveInst *Inst = cast<MemMoveInst>(U); |
| 103 |
if (!isa<ConstantInt>(Inst->getLength())) { |
| 104 |
expandMemMoveAsLoop(Inst); |
| 105 |
Inst->eraseFromParent(); |
| 106 |
} else { |
| 107 |
LowerMemMoveInst(*Inst); |
| 108 |
} |
| 109 |
Changed = true; |
| 110 |
} |
| 111 |
return Changed; |
| 112 |
} |
| 113 |
|
| 114 |
bool SPIRVLowerMemmoveBase::runLowerMemmove(Module &M) { |
| 115 |
Context = &M.getContext(); |
| 116 |
bool Changed = false; |
| 117 |
|
| 118 |
for (Function &F : M) { |
| 119 |
if (!F.isDeclaration()) |
| 120 |
continue; |
| 121 |
|
| 122 |
if (F.getIntrinsicID() == Intrinsic::memmove) |
| 123 |
Changed |= expandMemMoveIntrinsicUses(F); |
| 124 |
} |
| 125 |
|
| 126 |
verifyRegularizationPass(M, "SPIRVLowerMemmove"); |
| 127 |
return Changed; |
| 128 |
} |
| 129 |
|
| 130 |
llvm::PreservedAnalyses |
| 131 |
SPIRVLowerMemmovePass::run(llvm::Module &M, llvm::ModuleAnalysisManager &MAM) { |
| 132 |
return runLowerMemmove(M) ? llvm::PreservedAnalyses::none() |
| 133 |
: llvm::PreservedAnalyses::all(); |
| 134 |
} |
| 135 |
|
| 136 |
SPIRVLowerMemmoveLegacy::SPIRVLowerMemmoveLegacy() : ModulePass(ID) { |
| 137 |
initializeSPIRVLowerMemmoveLegacyPass(*PassRegistry::getPassRegistry()); |
| 138 |
} |
| 139 |
|
| 140 |
bool SPIRVLowerMemmoveLegacy::runOnModule(Module &M) { |
| 141 |
return runLowerMemmove(M); |
| 142 |
} |
| 143 |
|
| 144 |
char SPIRVLowerMemmoveLegacy::ID = 0; |
| 145 |
|
| 146 |
} // namespace SPIRV |
| 147 |
|
| 148 |
INITIALIZE_PASS(SPIRVLowerMemmoveLegacy, "spvmemmove", |
| 149 |
"Lower llvm.memmove into llvm.memcpy", false, false) |
| 150 |
|
| 151 |
ModulePass *llvm::createSPIRVLowerMemmoveLegacy() { |
| 152 |
return new SPIRVLowerMemmoveLegacy(); |
| 153 |
} |