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