|
| 1 | +//===-- LowerWGLocalMemory.cpp - SYCL kernel local memory allocation pass -===// |
| 2 | +// |
| 3 | +// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. |
| 4 | +// See https://llvm.org/LICENSE.txt for license information. |
| 5 | +// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception |
| 6 | +// |
| 7 | +//===----------------------------------------------------------------------===// |
| 8 | +// |
| 9 | +// This pass replaces calls to __sycl_allocateLocalMemory(Size, Alignment) |
| 10 | +// function with allocation of memory in local address space at the kernel |
| 11 | +// scope. |
| 12 | +// |
| 13 | +//===----------------------------------------------------------------------===// |
| 14 | + |
| 15 | +#include "llvm/SYCLLowerIR/LowerWGLocalMemory.h" |
| 16 | +#include "llvm/IR/Function.h" |
| 17 | +#include "llvm/IR/IRBuilder.h" |
| 18 | +#include "llvm/IR/InstIterator.h" |
| 19 | +#include "llvm/InitializePasses.h" |
| 20 | +#include "llvm/Pass.h" |
| 21 | + |
| 22 | +using namespace llvm; |
| 23 | + |
| 24 | +#define DEBUG_TYPE "LowerWGLocalMemory" |
| 25 | + |
| 26 | +static constexpr char SYCL_ALLOCLOCALMEM_CALL[] = "__sycl_allocateLocalMemory"; |
| 27 | +static constexpr char LOCALMEMORY_GV_PREF[] = "WGLocalMem"; |
| 28 | + |
| 29 | +namespace { |
| 30 | +class SYCLLowerWGLocalMemoryLegacy : public ModulePass { |
| 31 | +public: |
| 32 | + static char ID; |
| 33 | + |
| 34 | + SYCLLowerWGLocalMemoryLegacy() : ModulePass(ID) { |
| 35 | + initializeSYCLLowerWGLocalMemoryLegacyPass( |
| 36 | + *PassRegistry::getPassRegistry()); |
| 37 | + } |
| 38 | + |
| 39 | + bool runOnModule(Module &M) override { |
| 40 | + ModuleAnalysisManager DummyMAM; |
| 41 | + auto PA = Impl.run(M, DummyMAM); |
| 42 | + return !PA.areAllPreserved(); |
| 43 | + } |
| 44 | + |
| 45 | +private: |
| 46 | + SYCLLowerWGLocalMemoryPass Impl; |
| 47 | +}; |
| 48 | +} // namespace |
| 49 | + |
| 50 | +char SYCLLowerWGLocalMemoryLegacy::ID = 0; |
| 51 | +INITIALIZE_PASS(SYCLLowerWGLocalMemoryLegacy, "sycllowerwglocalmemory", |
| 52 | + "Replace __sycl_allocateLocalMemory with allocation of memory " |
| 53 | + "in local address space", |
| 54 | + false, false) |
| 55 | + |
| 56 | +ModulePass *llvm::createSYCLLowerWGLocalMemoryPass() { |
| 57 | + return new SYCLLowerWGLocalMemoryLegacy(); |
| 58 | +} |
| 59 | + |
| 60 | +static bool lowerAllocaLocalMem(Module &M) { |
| 61 | + SmallVector<CallInst *, 8> ToReplace; |
| 62 | + for (Function &F : M) { |
| 63 | + CallingConv::ID CC = F.getCallingConv(); |
| 64 | + |
| 65 | + for (auto &I : instructions(F)) { |
| 66 | + auto *CI = dyn_cast<CallInst>(&I); |
| 67 | + Function *Callee = nullptr; |
| 68 | + if (!CI || !(Callee = CI->getCalledFunction())) |
| 69 | + continue; |
| 70 | + StringRef Name = Callee->getName(); |
| 71 | + if (Name != SYCL_ALLOCLOCALMEM_CALL) |
| 72 | + continue; |
| 73 | + |
| 74 | + // TODO: Static local memory allocation should be requested only in |
| 75 | + // spir kernel scope. |
| 76 | + assert((CC == llvm::CallingConv::SPIR_FUNC || |
| 77 | + CC == llvm::CallingConv::SPIR_KERNEL) && |
| 78 | + "WG static local memery can be allocated only in kernel scope"); |
| 79 | + |
| 80 | + ToReplace.push_back(CI); |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + if (ToReplace.empty()) |
| 85 | + return false; |
| 86 | + |
| 87 | + for (auto *CI : ToReplace) { |
| 88 | + Value *ArgSize = CI->getArgOperand(0); |
| 89 | + uint64_t Size = cast<llvm::ConstantInt>(ArgSize)->getZExtValue(); |
| 90 | + Value *ArgAlign = CI->getArgOperand(1); |
| 91 | + uint64_t Alignment = cast<llvm::ConstantInt>(ArgAlign)->getZExtValue(); |
| 92 | + |
| 93 | + IRBuilder<> Builder(CI); |
| 94 | + Type *LocalMemArrayTy = ArrayType::get(Builder.getInt8Ty(), Size); |
| 95 | + unsigned LocalAS = |
| 96 | + CI->getFunctionType()->getReturnType()->getPointerAddressSpace(); |
| 97 | + auto *LocalMemArrayGV = |
| 98 | + new GlobalVariable(M, // module |
| 99 | + LocalMemArrayTy, // type |
| 100 | + false, // isConstant |
| 101 | + GlobalValue::InternalLinkage, // Linkage |
| 102 | + UndefValue::get(LocalMemArrayTy), // Initializer |
| 103 | + LOCALMEMORY_GV_PREF, // Name prefix |
| 104 | + nullptr, // InsertBefore |
| 105 | + GlobalVariable::NotThreadLocal, // ThreadLocalMode |
| 106 | + LocalAS // AddressSpace |
| 107 | + ); |
| 108 | + LocalMemArrayGV->setAlignment(Align(Alignment)); |
| 109 | + |
| 110 | + Value *LocalMemArrayGVPtr = Builder.CreatePointerCast( |
| 111 | + LocalMemArrayGV, |
| 112 | + Builder.getInt8PtrTy(LocalMemArrayGV->getAddressSpace())); |
| 113 | + CI->replaceAllUsesWith(LocalMemArrayGVPtr); |
| 114 | + CI->eraseFromParent(); |
| 115 | + } |
| 116 | + return true; |
| 117 | +} |
| 118 | + |
| 119 | +PreservedAnalyses SYCLLowerWGLocalMemoryPass::run(Module &M, |
| 120 | + ModuleAnalysisManager &) { |
| 121 | + if (lowerAllocaLocalMem(M)) |
| 122 | + return PreservedAnalyses::none(); |
| 123 | + return PreservedAnalyses::all(); |
| 124 | +} |
0 commit comments