NVPTXLowerAlloca.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. //===-- NVPTXLowerAlloca.cpp - Make alloca to use local memory =====--===//
  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. // For all alloca instructions, and add a pair of cast to local address for
  10. // each of them. For example,
  11. //
  12. // %A = alloca i32
  13. // store i32 0, i32* %A ; emits st.u32
  14. //
  15. // will be transformed to
  16. //
  17. // %A = alloca i32
  18. // %Local = addrspacecast i32* %A to i32 addrspace(5)*
  19. // %Generic = addrspacecast i32 addrspace(5)* %A to i32*
  20. // store i32 0, i32 addrspace(5)* %Generic ; emits st.local.u32
  21. //
  22. // And we will rely on NVPTXInferAddressSpaces to combine the last two
  23. // instructions.
  24. //
  25. //===----------------------------------------------------------------------===//
  26. #include "NVPTX.h"
  27. #include "NVPTXUtilities.h"
  28. #include "MCTargetDesc/NVPTXBaseInfo.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/IR/Instructions.h"
  31. #include "llvm/IR/IntrinsicInst.h"
  32. #include "llvm/IR/Module.h"
  33. #include "llvm/IR/Type.h"
  34. #include "llvm/Pass.h"
  35. using namespace llvm;
  36. namespace llvm {
  37. void initializeNVPTXLowerAllocaPass(PassRegistry &);
  38. }
  39. namespace {
  40. class NVPTXLowerAlloca : public FunctionPass {
  41. bool runOnFunction(Function &F) override;
  42. public:
  43. static char ID; // Pass identification, replacement for typeid
  44. NVPTXLowerAlloca() : FunctionPass(ID) {}
  45. StringRef getPassName() const override {
  46. return "convert address space of alloca'ed memory to local";
  47. }
  48. };
  49. } // namespace
  50. char NVPTXLowerAlloca::ID = 1;
  51. INITIALIZE_PASS(NVPTXLowerAlloca, "nvptx-lower-alloca",
  52. "Lower Alloca", false, false)
  53. // =============================================================================
  54. // Main function for this pass.
  55. // =============================================================================
  56. bool NVPTXLowerAlloca::runOnFunction(Function &F) {
  57. if (skipFunction(F))
  58. return false;
  59. bool Changed = false;
  60. for (auto &BB : F)
  61. for (auto &I : BB) {
  62. if (auto allocaInst = dyn_cast<AllocaInst>(&I)) {
  63. Changed = true;
  64. auto ETy = allocaInst->getAllocatedType();
  65. auto LocalAddrTy = PointerType::get(ETy, ADDRESS_SPACE_LOCAL);
  66. auto NewASCToLocal = new AddrSpaceCastInst(allocaInst, LocalAddrTy, "");
  67. auto GenericAddrTy = PointerType::get(ETy, ADDRESS_SPACE_GENERIC);
  68. auto NewASCToGeneric =
  69. new AddrSpaceCastInst(NewASCToLocal, GenericAddrTy, "");
  70. NewASCToLocal->insertAfter(allocaInst);
  71. NewASCToGeneric->insertAfter(NewASCToLocal);
  72. for (Use &AllocaUse : llvm::make_early_inc_range(allocaInst->uses())) {
  73. // Check Load, Store, GEP, and BitCast Uses on alloca and make them
  74. // use the converted generic address, in order to expose non-generic
  75. // addrspacecast to NVPTXInferAddressSpaces. For other types
  76. // of instructions this is unnecessary and may introduce redundant
  77. // address cast.
  78. auto LI = dyn_cast<LoadInst>(AllocaUse.getUser());
  79. if (LI && LI->getPointerOperand() == allocaInst &&
  80. !LI->isVolatile()) {
  81. LI->setOperand(LI->getPointerOperandIndex(), NewASCToGeneric);
  82. continue;
  83. }
  84. auto SI = dyn_cast<StoreInst>(AllocaUse.getUser());
  85. if (SI && SI->getPointerOperand() == allocaInst &&
  86. !SI->isVolatile()) {
  87. SI->setOperand(SI->getPointerOperandIndex(), NewASCToGeneric);
  88. continue;
  89. }
  90. auto GI = dyn_cast<GetElementPtrInst>(AllocaUse.getUser());
  91. if (GI && GI->getPointerOperand() == allocaInst) {
  92. GI->setOperand(GI->getPointerOperandIndex(), NewASCToGeneric);
  93. continue;
  94. }
  95. auto BI = dyn_cast<BitCastInst>(AllocaUse.getUser());
  96. if (BI && BI->getOperand(0) == allocaInst) {
  97. BI->setOperand(0, NewASCToGeneric);
  98. continue;
  99. }
  100. }
  101. }
  102. }
  103. return Changed;
  104. }
  105. FunctionPass *llvm::createNVPTXLowerAllocaPass() {
  106. return new NVPTXLowerAlloca();
  107. }