remove_block_reduction_opportunity_finder.cpp 3.2 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/reduce/remove_block_reduction_opportunity_finder.h"
  15. #include "source/reduce/remove_block_reduction_opportunity.h"
  16. namespace spvtools {
  17. namespace reduce {
  18. std::string RemoveBlockReductionOpportunityFinder::GetName() const {
  19. return "RemoveBlockReductionOpportunityFinder";
  20. }
  21. std::vector<std::unique_ptr<ReductionOpportunity>>
  22. RemoveBlockReductionOpportunityFinder::GetAvailableOpportunities(
  23. opt::IRContext* context, uint32_t target_function) const {
  24. std::vector<std::unique_ptr<ReductionOpportunity>> result;
  25. // Consider every block in every relevant function.
  26. for (auto* function : GetTargetFunctions(context, target_function)) {
  27. for (auto bi = function->begin(); bi != function->end(); ++bi) {
  28. if (IsBlockValidOpportunity(context, function, &bi)) {
  29. result.push_back(MakeUnique<RemoveBlockReductionOpportunity>(
  30. context, function, &*bi));
  31. }
  32. }
  33. }
  34. return result;
  35. }
  36. bool RemoveBlockReductionOpportunityFinder::IsBlockValidOpportunity(
  37. opt::IRContext* context, opt::Function* function,
  38. opt::Function::iterator* bi) {
  39. assert(*bi != function->end() && "Block iterator was out of bounds");
  40. // Don't remove first block; we don't want to end up with no blocks.
  41. if (*bi == function->begin()) {
  42. return false;
  43. }
  44. // Don't remove blocks with references.
  45. if (context->get_def_use_mgr()->NumUsers((*bi)->id()) > 0) {
  46. return false;
  47. }
  48. // Don't remove blocks whose instructions have outside references.
  49. if (!BlockInstructionsHaveNoOutsideReferences(context, *bi)) {
  50. return false;
  51. }
  52. return true;
  53. }
  54. bool RemoveBlockReductionOpportunityFinder::
  55. BlockInstructionsHaveNoOutsideReferences(
  56. opt::IRContext* context, const opt::Function::iterator& bi) {
  57. // Get all instructions in block.
  58. std::unordered_set<uint32_t> instructions_in_block;
  59. for (const opt::Instruction& instruction : *bi) {
  60. instructions_in_block.insert(instruction.unique_id());
  61. }
  62. // For each instruction...
  63. for (const opt::Instruction& instruction : *bi) {
  64. // For each use of the instruction...
  65. bool no_uses_outside_block = context->get_def_use_mgr()->WhileEachUser(
  66. &instruction, [&instructions_in_block](opt::Instruction* user) -> bool {
  67. // If the use is in this block, continue (return true). Otherwise, we
  68. // found an outside use; return false (and stop).
  69. return instructions_in_block.find(user->unique_id()) !=
  70. instructions_in_block.end();
  71. });
  72. if (!no_uses_outside_block) {
  73. return false;
  74. }
  75. }
  76. return true;
  77. }
  78. } // namespace reduce
  79. } // namespace spvtools