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 15 #ifndef SOURCE_REDUCE_REDUCTION_PASS_H_ 16 #define SOURCE_REDUCE_REDUCTION_PASS_H_ 17 18 #include "spirv-tools/libspirv.hpp" 19 20 #include "reduction_opportunity.h" 21 #include "source/opt/ir_context.h" 22 23 namespace spvtools { 24 namespace reduce { 25 26 // Abstract class representing a reduction pass, which can be repeatedly 27 // invoked to find and apply particular reduction opportunities to a SPIR-V 28 // binary. In the spirit of delta debugging, a pass initially tries to apply 29 // large chunks of reduction opportunities, iterating through available 30 // opportunities at a given granularity. When an iteration over available 31 // opportunities completes, the granularity is reduced and iteration starts 32 // again, until the minimum granularity is reached. 33 class ReductionPass { 34 public: 35 // Constructs a reduction pass with a given target environment, |target_env|. 36 // Initially the pass is uninitialized. ReductionPass(const spv_target_env target_env)37 explicit ReductionPass(const spv_target_env target_env) 38 : target_env_(target_env), is_initialized_(false) {} 39 40 virtual ~ReductionPass() = default; 41 42 // Apply the reduction pass to the given binary. 43 std::vector<uint32_t> TryApplyReduction(const std::vector<uint32_t>& binary); 44 45 // Set a consumer to which relevant messages will be directed. 46 void SetMessageConsumer(MessageConsumer consumer); 47 48 // Determines whether the granularity with which reduction opportunities are 49 // applied has reached a minimum. 50 bool ReachedMinimumGranularity() const; 51 52 // Returns the name of the reduction pass (useful for monitoring reduction 53 // progress). 54 virtual std::string GetName() const = 0; 55 56 protected: 57 // Finds the reduction opportunities relevant to this pass that could be 58 // applied to a given SPIR-V module. 59 virtual std::vector<std::unique_ptr<ReductionOpportunity>> 60 GetAvailableOpportunities(opt::IRContext* context) const = 0; 61 62 private: 63 const spv_target_env target_env_; 64 MessageConsumer consumer_; 65 bool is_initialized_; 66 uint32_t index_; 67 uint32_t granularity_; 68 }; 69 70 } // namespace reduce 71 } // namespace spvtools 72 73 #endif // SOURCE_REDUCE_REDUCTION_PASS_H_ 74