reduction_pass.cpp 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  1. // Copyright (c) 2018 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/reduction_pass.h"
  15. #include <algorithm>
  16. #include "source/opt/build_module.h"
  17. namespace spvtools {
  18. namespace reduce {
  19. std::vector<uint32_t> ReductionPass::TryApplyReduction(
  20. const std::vector<uint32_t>& binary, uint32_t target_function) {
  21. // We represent modules as binaries because (a) attempts at reduction need to
  22. // end up in binary form to be passed on to SPIR-V-consuming tools, and (b)
  23. // when we apply a reduction step we need to do it on a fresh version of the
  24. // module as if the reduction step proves to be uninteresting we need to
  25. // backtrack; re-parsing from binary provides a very clean way of cloning the
  26. // module.
  27. std::unique_ptr<opt::IRContext> context =
  28. BuildModule(target_env_, consumer_, binary.data(), binary.size());
  29. assert(context);
  30. std::vector<std::unique_ptr<ReductionOpportunity>> opportunities =
  31. finder_->GetAvailableOpportunities(context.get(), target_function);
  32. // There is no point in having a granularity larger than the number of
  33. // opportunities, so reduce the granularity in this case.
  34. if (granularity_ > opportunities.size()) {
  35. granularity_ = std::max((uint32_t)1, (uint32_t)opportunities.size());
  36. }
  37. assert(granularity_ > 0);
  38. if (index_ >= opportunities.size()) {
  39. // We have reached the end of the available opportunities and, therefore,
  40. // the end of the round for this pass, so reset the index and decrease the
  41. // granularity for the next round. Return an empty vector to signal the end
  42. // of the round.
  43. index_ = 0;
  44. granularity_ = std::max((uint32_t)1, granularity_ / 2);
  45. return std::vector<uint32_t>();
  46. }
  47. for (uint32_t i = index_;
  48. i < std::min(index_ + granularity_, (uint32_t)opportunities.size());
  49. ++i) {
  50. opportunities[i]->TryToApply();
  51. }
  52. std::vector<uint32_t> result;
  53. context->module()->ToBinary(&result, false);
  54. return result;
  55. }
  56. void ReductionPass::SetMessageConsumer(MessageConsumer consumer) {
  57. consumer_ = std::move(consumer);
  58. }
  59. bool ReductionPass::ReachedMinimumGranularity() const {
  60. assert(granularity_ != 0);
  61. return granularity_ == 1;
  62. }
  63. std::string ReductionPass::GetName() const { return finder_->GetName(); }
  64. void ReductionPass::NotifyInteresting(bool interesting) {
  65. if (!interesting) {
  66. index_ += granularity_;
  67. }
  68. }
  69. } // namespace reduce
  70. } // namespace spvtools