inline_opaque_pass.cpp 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  1. // Copyright (c) 2017 The Khronos Group Inc.
  2. // Copyright (c) 2017 Valve Corporation
  3. // Copyright (c) 2017 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #include "source/opt/inline_opaque_pass.h"
  17. #include <utility>
  18. namespace spvtools {
  19. namespace opt {
  20. namespace {
  21. const uint32_t kTypePointerTypeIdInIdx = 1;
  22. } // anonymous namespace
  23. bool InlineOpaquePass::IsOpaqueType(uint32_t typeId) {
  24. const Instruction* typeInst = get_def_use_mgr()->GetDef(typeId);
  25. switch (typeInst->opcode()) {
  26. case SpvOpTypeSampler:
  27. case SpvOpTypeImage:
  28. case SpvOpTypeSampledImage:
  29. return true;
  30. case SpvOpTypePointer:
  31. return IsOpaqueType(
  32. typeInst->GetSingleWordInOperand(kTypePointerTypeIdInIdx));
  33. default:
  34. break;
  35. }
  36. // TODO(greg-lunarg): Handle arrays containing opaque type
  37. if (typeInst->opcode() != SpvOpTypeStruct) return false;
  38. // Return true if any member is opaque
  39. return !typeInst->WhileEachInId([this](const uint32_t* tid) {
  40. if (IsOpaqueType(*tid)) return false;
  41. return true;
  42. });
  43. }
  44. bool InlineOpaquePass::HasOpaqueArgsOrReturn(const Instruction* callInst) {
  45. // Check return type
  46. if (IsOpaqueType(callInst->type_id())) return true;
  47. // Check args
  48. int icnt = 0;
  49. return !callInst->WhileEachInId([&icnt, this](const uint32_t* iid) {
  50. if (icnt > 0) {
  51. const Instruction* argInst = get_def_use_mgr()->GetDef(*iid);
  52. if (IsOpaqueType(argInst->type_id())) return false;
  53. }
  54. ++icnt;
  55. return true;
  56. });
  57. }
  58. bool InlineOpaquePass::InlineOpaque(Function* func) {
  59. bool modified = false;
  60. // Using block iterators here because of block erasures and insertions.
  61. for (auto bi = func->begin(); bi != func->end(); ++bi) {
  62. for (auto ii = bi->begin(); ii != bi->end();) {
  63. if (IsInlinableFunctionCall(&*ii) && HasOpaqueArgsOrReturn(&*ii)) {
  64. // Inline call.
  65. std::vector<std::unique_ptr<BasicBlock>> newBlocks;
  66. std::vector<std::unique_ptr<Instruction>> newVars;
  67. GenInlineCode(&newBlocks, &newVars, ii, bi);
  68. // If call block is replaced with more than one block, point
  69. // succeeding phis at new last block.
  70. if (newBlocks.size() > 1) UpdateSucceedingPhis(newBlocks);
  71. // Replace old calling block with new block(s).
  72. bi = bi.Erase();
  73. bi = bi.InsertBefore(&newBlocks);
  74. // Insert new function variables.
  75. if (newVars.size() > 0)
  76. func->begin()->begin().InsertBefore(std::move(newVars));
  77. // Restart inlining at beginning of calling block.
  78. ii = bi->begin();
  79. modified = true;
  80. } else {
  81. ++ii;
  82. }
  83. }
  84. }
  85. return modified;
  86. }
  87. void InlineOpaquePass::Initialize() { InitializeInline(); }
  88. Pass::Status InlineOpaquePass::ProcessImpl() {
  89. // Do opaque inlining on each function in entry point call tree
  90. ProcessFunction pfn = [this](Function* fp) { return InlineOpaque(fp); };
  91. bool modified = ProcessEntryPointCallTree(pfn, get_module());
  92. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  93. }
  94. InlineOpaquePass::InlineOpaquePass() = default;
  95. Pass::Status InlineOpaquePass::Process() {
  96. Initialize();
  97. return ProcessImpl();
  98. }
  99. } // namespace opt
  100. } // namespace spvtools