reduction_pass.h 3.5 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. #ifndef SOURCE_REDUCE_REDUCTION_PASS_H_
  15. #define SOURCE_REDUCE_REDUCTION_PASS_H_
  16. #include <limits>
  17. #include "source/opt/ir_context.h"
  18. #include "source/reduce/reduction_opportunity_finder.h"
  19. #include "spirv-tools/libspirv.hpp"
  20. namespace spvtools {
  21. namespace reduce {
  22. // Abstract class representing a reduction pass, which can be repeatedly
  23. // invoked to find and apply particular reduction opportunities to a SPIR-V
  24. // binary. In the spirit of delta debugging, a pass initially tries to apply
  25. // large chunks of reduction opportunities, iterating through available
  26. // opportunities at a given granularity. When an iteration over available
  27. // opportunities completes, the granularity is reduced and iteration starts
  28. // again, until the minimum granularity is reached.
  29. class ReductionPass {
  30. public:
  31. // Constructs a reduction pass with a given target environment, |target_env|,
  32. // and a given finder of reduction opportunities, |finder|.
  33. explicit ReductionPass(const spv_target_env target_env,
  34. std::unique_ptr<ReductionOpportunityFinder> finder)
  35. : target_env_(target_env),
  36. finder_(std::move(finder)),
  37. index_(0),
  38. granularity_(std::numeric_limits<uint32_t>::max()) {}
  39. // Applies the reduction pass to the given binary by applying a "chunk" of
  40. // reduction opportunities. Returns the new binary if a chunk was applied; in
  41. // this case, before the next call the caller must invoke
  42. // NotifyInteresting(...) to indicate whether the new binary is interesting.
  43. // Returns an empty vector if there are no more chunks left to apply; in this
  44. // case, the index will be reset and the granularity lowered for the next
  45. // round.
  46. //
  47. // If |target_function| is non-zero, only reduction opportunities that
  48. // simplify the internals of the function with result id |target_function|
  49. // will be applied.
  50. std::vector<uint32_t> TryApplyReduction(const std::vector<uint32_t>& binary,
  51. uint32_t target_function);
  52. // Notifies the reduction pass whether the binary returned from
  53. // TryApplyReduction is interesting, so that the next call to
  54. // TryApplyReduction will avoid applying the same chunk of opportunities.
  55. void NotifyInteresting(bool interesting);
  56. // Sets a consumer to which relevant messages will be directed.
  57. void SetMessageConsumer(MessageConsumer consumer);
  58. // Returns true if the granularity with which reduction opportunities are
  59. // applied has reached a minimum.
  60. bool ReachedMinimumGranularity() const;
  61. // Returns the name associated with this reduction pass (based on its
  62. // associated finder).
  63. std::string GetName() const;
  64. private:
  65. const spv_target_env target_env_;
  66. const std::unique_ptr<ReductionOpportunityFinder> finder_;
  67. MessageConsumer consumer_;
  68. uint32_t index_;
  69. uint32_t granularity_;
  70. };
  71. } // namespace reduce
  72. } // namespace spvtools
  73. #endif // SOURCE_REDUCE_REDUCTION_PASS_H_