| 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|. |
| 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 |
// |
| 53 |
// If |target_function| is non-zero, only reduction opportunities that |
| 54 |
// simplify the internals of the function with result id |target_function| |
| 55 |
// will be applied. |
| 56 |
std::vector<uint32_t> TryApplyReduction(const std::vector<uint32_t>& binary, |
| 57 |
uint32_t target_function); |
| 58 |
|
| 59 |
// Notifies the reduction pass whether the binary returned from |
| 60 |
// TryApplyReduction is interesting, so that the next call to |
| 61 |
// TryApplyReduction will avoid applying the same chunk of opportunities. |
| 62 |
void NotifyInteresting(bool interesting); |
| 63 |
|
| 64 |
// Sets a consumer to which relevant messages will be directed. |
| 65 |
void SetMessageConsumer(MessageConsumer consumer); |
| 66 |
|
| 67 |
// Returns true if the granularity with which reduction opportunities are |
| 68 |
// applied has reached a minimum. |
| 69 |
bool ReachedMinimumGranularity() const; |
| 70 |
|
| 71 |
// Returns the name associated with this reduction pass (based on its |
| 72 |
// associated finder). |
| 73 |
std::string GetName() const; |
| 74 |
|
| 75 |
private: |
| 76 |
const spv_target_env target_env_; |
| 77 |
const std::unique_ptr<ReductionOpportunityFinder> finder_; |
| 78 |
MessageConsumer consumer_; |
| 79 |
uint32_t index_; |
| 80 |
uint32_t granularity_; |
| 81 |
}; |
| 82 |
|
| 83 |
} // namespace reduce |
| 84 |
} // namespace spvtools |
| 85 |
|
| 86 |
#endif // SOURCE_REDUCE_REDUCTION_PASS_H_ |