GlobalAllocPass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. /*
  2. * Copyright 2017, The Android Open Source Project
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #include "GlobalAllocPass.h"
  17. #include "Context.h"
  18. #include "RSAllocationUtils.h"
  19. #include "llvm/IR/GlobalVariable.h"
  20. #include "llvm/IR/Module.h"
  21. #include "llvm/Pass.h"
  22. #include "llvm/Support/Debug.h"
  23. #define DEBUG_TYPE "rs2spirv-global-alloc"
  24. using namespace llvm;
  25. namespace rs2spirv {
  26. namespace {
  27. bool collectGlobalAllocs(Module &M,
  28. SmallVectorImpl<GlobalVariable *> &GlobalAllocs) {
  29. for (auto &GV : M.globals()) {
  30. if (!isRSAllocation(GV))
  31. continue;
  32. DEBUG(GV.dump());
  33. GlobalAllocs.push_back(&GV);
  34. }
  35. return !GlobalAllocs.empty();
  36. }
  37. //
  38. // This pass would enumerate used global rs_allocations (TBD) and
  39. // lowers calls to accessors of the following type:
  40. //
  41. // rsGetAllocationDimX(g)
  42. //
  43. // to
  44. //
  45. // __rsov_rsGetAllocationDimX(some uninque constant identifying g) */
  46. //
  47. // Note the __rsov_* variant is used as a marker for another SPIRIT
  48. // transformations (see GlobalAllocSPIRITPass.cpp) to expand them into
  49. // SPIR-V instructions that loads the metadata.
  50. //
  51. class GlobalAllocPass : public ModulePass {
  52. public:
  53. static char ID;
  54. GlobalAllocPass()
  55. : ModulePass(ID), Allocs(Context::getInstance().getGlobalAllocs()) {}
  56. const char *getPassName() const override { return "GlobalAllocPass"; }
  57. bool runOnModule(Module &M) override {
  58. DEBUG(dbgs() << "RS2SPIRVGlobalAllocPass\n");
  59. DEBUG(M.dump());
  60. SmallVector<GlobalVariable *, 8> GlobalAllocs;
  61. const bool CollectRes = collectGlobalAllocs(M, GlobalAllocs);
  62. if (!CollectRes)
  63. return false; // Module not modified.
  64. SmallVector<RSAllocationCallInfo, 8> Calls;
  65. getRSAllocationInfo(M, Allocs);
  66. getRSAllocAccesses(Allocs, Calls);
  67. // Lower the found accessors
  68. for (auto &C : Calls) {
  69. assert(C.Kind == RSAllocAccessKind::DIMX &&
  70. "Unsupported type of accessor call types");
  71. solidifyRSAllocAccess(M, C);
  72. }
  73. // Return true, as the pass modifies module.
  74. DEBUG(dbgs() << "RS2SPIRVGlobalAllocPass end\n");
  75. return true;
  76. }
  77. private:
  78. SmallVectorImpl<RSAllocationInfo> &Allocs;
  79. };
  80. // A simple pass to remove all global allocations forcibly
  81. class RemoveAllGlobalAllocPass : public ModulePass {
  82. public:
  83. static char ID;
  84. RemoveAllGlobalAllocPass() : ModulePass(ID) {}
  85. const char *getPassName() const override {
  86. return "RemoveAllGlobalAllocPass";
  87. }
  88. bool runOnModule(Module &M) override {
  89. DEBUG(dbgs() << "RemoveAllGlobalAllocPass\n");
  90. DEBUG(M.dump());
  91. SmallVector<GlobalVariable *, 8> GlobalAllocs;
  92. const bool CollectRes = collectGlobalAllocs(M, GlobalAllocs);
  93. if (!CollectRes)
  94. return false; // Module not modified.
  95. // Remove global allocations
  96. for (auto *G : GlobalAllocs) {
  97. G->eraseFromParent();
  98. }
  99. DEBUG(dbgs() << "RemoveAllGlobalAllocPass end\n");
  100. DEBUG(M.dump());
  101. // Return true, as the pass modifies module.
  102. return true;
  103. }
  104. };
  105. } // namespace
  106. char GlobalAllocPass::ID = 0;
  107. char RemoveAllGlobalAllocPass::ID = 0;
  108. ModulePass *createRemoveAllGlobalAllocPass() {
  109. return new RemoveAllGlobalAllocPass();
  110. }
  111. ModulePass *createGlobalAllocPass() { return new GlobalAllocPass(); }
  112. } // namespace rs2spirv