inline_opaque_pass.cpp 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118
  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. constexpr uint32_t kTypePointerTypeIdInIdx = 1;
  22. } // namespace
  23. bool InlineOpaquePass::IsOpaqueType(uint32_t typeId) {
  24. const Instruction* typeInst = get_def_use_mgr()->GetDef(typeId);
  25. switch (typeInst->opcode()) {
  26. case spv::Op::OpTypeSampler:
  27. case spv::Op::OpTypeImage:
  28. case spv::Op::OpTypeSampledImage:
  29. return true;
  30. case spv::Op::OpTypePointer:
  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() != spv::Op::OpTypeStruct) 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. Pass::Status 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. if (!GenInlineCode(&newBlocks, &newVars, ii, bi)) {
  68. return Status::Failure;
  69. }
  70. // If call block is replaced with more than one block, point
  71. // succeeding phis at new last block.
  72. if (newBlocks.size() > 1) UpdateSucceedingPhis(newBlocks);
  73. // Replace old calling block with new block(s).
  74. bi = bi.Erase();
  75. bi = bi.InsertBefore(&newBlocks);
  76. // Insert new function variables.
  77. if (newVars.size() > 0)
  78. func->begin()->begin().InsertBefore(std::move(newVars));
  79. // Restart inlining at beginning of calling block.
  80. ii = bi->begin();
  81. modified = true;
  82. } else {
  83. ++ii;
  84. }
  85. }
  86. }
  87. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  88. }
  89. void InlineOpaquePass::Initialize() { InitializeInline(); }
  90. Pass::Status InlineOpaquePass::ProcessImpl() {
  91. Status status = Status::SuccessWithoutChange;
  92. // Do opaque inlining on each function in entry point call tree
  93. ProcessFunction pfn = [&status, this](Function* fp) {
  94. status = CombineStatus(status, InlineOpaque(fp));
  95. return false;
  96. };
  97. context()->ProcessReachableCallTree(pfn);
  98. return status;
  99. }
  100. InlineOpaquePass::InlineOpaquePass() = default;
  101. Pass::Status InlineOpaquePass::Process() {
  102. Initialize();
  103. return ProcessImpl();
  104. }
  105. } // namespace opt
  106. } // namespace spvtools