|
| 1 | +//===- LowerESIMDVLoadVStore.cpp - lower vload/vstore to load/store -------===// |
| 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 | +// convert vload/vstore to load/store if they are not for genx_volatile. |
| 10 | +// |
| 11 | +// File scope simd variables marked with genx_volatile attribute want |
| 12 | +// guarranteed allocation in register file, therefore we use vload/vstore |
| 13 | +// instead of load/store, so they won't be optimized away by llvm. |
| 14 | +// |
| 15 | +// For ordinary simd variables, we do not need to protect load/store. But |
| 16 | +// there is no good way to do this in clang. So we need this pass in the |
| 17 | +// end of module passes to separate the cases that we need vload/vstore vs. |
| 18 | +// the cases that we do not need vload/vstore |
| 19 | +// |
| 20 | +//===----------------------------------------------------------------------===// |
| 21 | + |
| 22 | +#define DEBUG_TYPE "loweresimdvloadvstore" |
| 23 | + |
| 24 | +#include "llvm/GenXIntrinsics/GenXIntrinsics.h" |
| 25 | +#include "llvm/IR/Function.h" |
| 26 | +#include "llvm/IR/IRBuilder.h" |
| 27 | +#include "llvm/IR/InstIterator.h" |
| 28 | +#include "llvm/IR/Instructions.h" |
| 29 | +#include "llvm/IR/IntrinsicInst.h" |
| 30 | +#include "llvm/IR/Module.h" |
| 31 | +#include "llvm/SYCLLowerIR/LowerESIMD.h" |
| 32 | +#include "llvm/Support/Debug.h" |
| 33 | +#include "llvm/Transforms/Scalar.h" |
| 34 | + |
| 35 | +#include "llvm/Pass.h" |
| 36 | + |
| 37 | +using namespace llvm; |
| 38 | + |
| 39 | +namespace llvm { |
| 40 | +void initializeESIMDLowerLoadStorePass(PassRegistry &); |
| 41 | +} |
| 42 | + |
| 43 | +namespace { |
| 44 | + |
| 45 | +class ESIMDLowerLoadStore : public FunctionPass { |
| 46 | +public: |
| 47 | + static char ID; |
| 48 | + ESIMDLowerLoadStore() : FunctionPass(ID) { |
| 49 | + initializeESIMDLowerLoadStorePass(*PassRegistry::getPassRegistry()); |
| 50 | + } |
| 51 | + virtual void getAnalysisUsage(AnalysisUsage &AU) const override { |
| 52 | + AU.setPreservesCFG(); |
| 53 | + } |
| 54 | + |
| 55 | + virtual bool runOnFunction(Function &F) override { |
| 56 | + FunctionAnalysisManager FAM; |
| 57 | + auto PA = Impl.run(F, FAM); |
| 58 | + return !PA.areAllPreserved(); |
| 59 | + } |
| 60 | + |
| 61 | +private: |
| 62 | + ESIMDLowerLoadStorePass Impl; |
| 63 | +}; |
| 64 | + |
| 65 | +} // namespace |
| 66 | + |
| 67 | +char ESIMDLowerLoadStore::ID = 0; |
| 68 | +INITIALIZE_PASS(ESIMDLowerLoadStore, "ESIMDLowerLoadStore", |
| 69 | + "Lower ESIMD reference loads and stores", false, false) |
| 70 | + |
| 71 | +// Lower non-volatilE vload/vstore intrinsic calls into normal load/store |
| 72 | +// instructions. |
| 73 | +PreservedAnalyses ESIMDLowerLoadStorePass::run(Function &F, |
| 74 | + FunctionAnalysisManager &FAM) { |
| 75 | + std::vector<Instruction *> ToErase; |
| 76 | + for (Instruction &Inst : instructions(F)) { |
| 77 | + if (!GenXIntrinsic::isVLoadStore(&Inst)) |
| 78 | + continue; |
| 79 | + |
| 80 | + auto *Ptr = Inst.getOperand(0); |
| 81 | + if (GenXIntrinsic::isVStore(&Inst)) |
| 82 | + Ptr = Inst.getOperand(1); |
| 83 | + auto AS0 = cast<PointerType>(Ptr->getType())->getAddressSpace(); |
| 84 | + Ptr = Ptr->stripPointerCasts(); |
| 85 | + auto GV = dyn_cast<GlobalVariable>(Ptr); |
| 86 | + if (!GV || !GV->hasAttribute("genx_volatile")) { |
| 87 | + // change to load/store |
| 88 | + IRBuilder<> Builder(&Inst); |
| 89 | + if (GenXIntrinsic::isVStore(&Inst)) |
| 90 | + Builder.CreateStore(Inst.getOperand(0), Inst.getOperand(1)); |
| 91 | + else { |
| 92 | + auto LI = Builder.CreateLoad(Inst.getOperand(0), Inst.getName()); |
| 93 | + LI->setDebugLoc(Inst.getDebugLoc()); |
| 94 | + Inst.replaceAllUsesWith(LI); |
| 95 | + } |
| 96 | + ToErase.push_back(&Inst); |
| 97 | + } else { |
| 98 | + // change to vload/vstore that has the same address space as |
| 99 | + // the global-var in order to clean up unnecessary addr-cast. |
| 100 | + auto AS1 = GV->getType()->getAddressSpace(); |
| 101 | + if (AS0 != AS1) { |
| 102 | + IRBuilder<> Builder(&Inst); |
| 103 | + if (GenXIntrinsic::isVStore(&Inst)) { |
| 104 | + auto PtrTy = cast<PointerType>(Inst.getOperand(1)->getType()); |
| 105 | + PtrTy = PointerType::get(PtrTy->getElementType(), AS1); |
| 106 | + auto PtrCast = Builder.CreateAddrSpaceCast(Inst.getOperand(1), PtrTy); |
| 107 | + Type *Tys[] = {Inst.getOperand(0)->getType(), PtrCast->getType()}; |
| 108 | + Value *Args[] = {Inst.getOperand(0), PtrCast}; |
| 109 | + Function *Fn = GenXIntrinsic::getGenXDeclaration( |
| 110 | + F.getParent(), GenXIntrinsic::genx_vstore, Tys); |
| 111 | + Builder.CreateCall(Fn, Args, Inst.getName()); |
| 112 | + } else { |
| 113 | + auto PtrTy = cast<PointerType>(Inst.getOperand(0)->getType()); |
| 114 | + PtrTy = PointerType::get(PtrTy->getElementType(), AS1); |
| 115 | + auto PtrCast = Builder.CreateAddrSpaceCast(Inst.getOperand(0), PtrTy); |
| 116 | + Type *Tys[] = {Inst.getType(), PtrCast->getType()}; |
| 117 | + Function *Fn = GenXIntrinsic::getGenXDeclaration( |
| 118 | + F.getParent(), GenXIntrinsic::genx_vload, Tys); |
| 119 | + Value *VLoad = Builder.CreateCall(Fn, PtrCast, Inst.getName()); |
| 120 | + Inst.replaceAllUsesWith(VLoad); |
| 121 | + } |
| 122 | + ToErase.push_back(&Inst); |
| 123 | + } |
| 124 | + } |
| 125 | + } |
| 126 | + |
| 127 | + for (auto Inst : ToErase) { |
| 128 | + Inst->eraseFromParent(); |
| 129 | + } |
| 130 | + |
| 131 | + return !ToErase.empty() ? PreservedAnalyses::none() |
| 132 | + : PreservedAnalyses::all(); |
| 133 | +} |
| 134 | + |
| 135 | +namespace llvm { |
| 136 | +FunctionPass *createESIMDLowerLoadStorePass() { |
| 137 | + return new ESIMDLowerLoadStore; |
| 138 | +} |
| 139 | +} // namespace llvm |
0 commit comments