split_invalid_unreachable_pass.cpp 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. // Copyright (c) 2019 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/split_invalid_unreachable_pass.h"
  15. #include "source/opt/ir_builder.h"
  16. #include "source/opt/ir_context.h"
  17. namespace spvtools {
  18. namespace opt {
  19. Pass::Status SplitInvalidUnreachablePass::Process() {
  20. bool changed = false;
  21. std::unordered_set<uint32_t> entry_points;
  22. for (auto entry_point : context()->module()->entry_points()) {
  23. entry_points.insert(entry_point.GetSingleWordOperand(1));
  24. }
  25. for (auto func = context()->module()->begin();
  26. func != context()->module()->end(); ++func) {
  27. if (entry_points.find(func->result_id()) == entry_points.end()) continue;
  28. std::unordered_set<uint32_t> continue_targets;
  29. std::unordered_set<uint32_t> merge_blocks;
  30. std::unordered_set<BasicBlock*> unreachable_blocks;
  31. for (auto block = func->begin(); block != func->end(); ++block) {
  32. unreachable_blocks.insert(&*block);
  33. uint32_t continue_target = block->ContinueBlockIdIfAny();
  34. if (continue_target != 0) continue_targets.insert(continue_target);
  35. uint32_t merge_block = block->MergeBlockIdIfAny();
  36. if (merge_block != 0) merge_blocks.insert(merge_block);
  37. }
  38. cfg()->ForEachBlockInPostOrder(
  39. func->entry().get(), [&unreachable_blocks](BasicBlock* inner_block) {
  40. unreachable_blocks.erase(inner_block);
  41. });
  42. for (auto unreachable : unreachable_blocks) {
  43. uint32_t block_id = unreachable->id();
  44. if (continue_targets.find(block_id) == continue_targets.end() ||
  45. merge_blocks.find(block_id) == merge_blocks.end()) {
  46. continue;
  47. }
  48. std::vector<std::tuple<Instruction*, uint32_t>> usages;
  49. context()->get_def_use_mgr()->ForEachUse(
  50. unreachable->GetLabelInst(),
  51. [&usages](Instruction* use, uint32_t idx) {
  52. if ((use->opcode() == SpvOpLoopMerge && idx == 0) ||
  53. use->opcode() == SpvOpSelectionMerge) {
  54. usages.push_back(std::make_pair(use, idx));
  55. }
  56. });
  57. for (auto usage : usages) {
  58. Instruction* use;
  59. uint32_t idx;
  60. std::tie(use, idx) = usage;
  61. uint32_t new_id = context()->TakeNextId();
  62. std::unique_ptr<Instruction> new_label(
  63. new Instruction(context(), SpvOpLabel, 0, new_id, {}));
  64. get_def_use_mgr()->AnalyzeInstDefUse(new_label.get());
  65. std::unique_ptr<BasicBlock> new_block(
  66. new BasicBlock(std::move(new_label)));
  67. auto* block_ptr = new_block.get();
  68. InstructionBuilder builder(context(), new_block.get(),
  69. IRContext::kAnalysisDefUse |
  70. IRContext::kAnalysisInstrToBlockMapping);
  71. builder.AddUnreachable();
  72. cfg()->RegisterBlock(block_ptr);
  73. (&*func)->InsertBasicBlockBefore(std::move(new_block), unreachable);
  74. use->SetInOperand(0, {new_id});
  75. get_def_use_mgr()->UpdateDefUse(use);
  76. cfg()->AddEdges(block_ptr);
  77. changed = true;
  78. }
  79. }
  80. }
  81. return changed ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  82. }
  83. } // namespace opt
  84. } // namespace spvtools