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