2018-11-21 19:03:09 +00:00
|
|
|
// Copyright (c) 2018 Google LLC
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2019-04-08 18:37:17 +00:00
|
|
|
#include "source/reduce/reducer.h"
|
|
|
|
|
2018-11-21 19:03:09 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <sstream>
|
|
|
|
|
2019-04-15 18:54:36 +00:00
|
|
|
#include "source/reduce/conditional_branch_to_simple_conditional_branch_opportunity_finder.h"
|
2019-03-26 13:22:31 +00:00
|
|
|
#include "source/reduce/merge_blocks_reduction_opportunity_finder.h"
|
|
|
|
#include "source/reduce/operand_to_const_reduction_opportunity_finder.h"
|
|
|
|
#include "source/reduce/operand_to_dominating_id_reduction_opportunity_finder.h"
|
|
|
|
#include "source/reduce/operand_to_undef_reduction_opportunity_finder.h"
|
2019-04-08 18:37:17 +00:00
|
|
|
#include "source/reduce/remove_block_reduction_opportunity_finder.h"
|
2019-03-26 13:22:31 +00:00
|
|
|
#include "source/reduce/remove_function_reduction_opportunity_finder.h"
|
2019-04-02 15:50:15 +00:00
|
|
|
#include "source/reduce/remove_selection_reduction_opportunity_finder.h"
|
2020-05-13 21:08:40 +00:00
|
|
|
#include "source/reduce/remove_unused_instruction_reduction_opportunity_finder.h"
|
2020-05-14 14:36:38 +00:00
|
|
|
#include "source/reduce/remove_unused_struct_member_reduction_opportunity_finder.h"
|
2019-04-15 18:54:36 +00:00
|
|
|
#include "source/reduce/simple_conditional_branch_to_branch_opportunity_finder.h"
|
2021-07-06 06:14:36 +00:00
|
|
|
#include "source/reduce/structured_construct_to_block_reduction_opportunity_finder.h"
|
2019-03-26 13:22:31 +00:00
|
|
|
#include "source/reduce/structured_loop_to_selection_reduction_opportunity_finder.h"
|
2018-11-21 19:03:09 +00:00
|
|
|
#include "source/spirv_reducer_options.h"
|
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace reduce {
|
|
|
|
|
2019-10-08 12:02:34 +00:00
|
|
|
Reducer::Reducer(spv_target_env target_env) : target_env_(target_env) {}
|
2018-11-21 19:03:09 +00:00
|
|
|
|
|
|
|
Reducer::~Reducer() = default;
|
|
|
|
|
|
|
|
void Reducer::SetMessageConsumer(MessageConsumer c) {
|
2019-10-08 12:02:34 +00:00
|
|
|
for (auto& pass : passes_) {
|
2018-11-21 19:03:09 +00:00
|
|
|
pass->SetMessageConsumer(c);
|
|
|
|
}
|
2019-10-08 12:02:34 +00:00
|
|
|
for (auto& pass : cleanup_passes_) {
|
|
|
|
pass->SetMessageConsumer(c);
|
|
|
|
}
|
|
|
|
consumer_ = std::move(c);
|
2018-11-21 19:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reducer::SetInterestingnessFunction(
|
|
|
|
Reducer::InterestingnessFunction interestingness_function) {
|
2019-10-08 12:02:34 +00:00
|
|
|
interestingness_function_ = std::move(interestingness_function);
|
2018-11-21 19:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
Reducer::ReductionResultStatus Reducer::Run(
|
2021-04-09 12:55:51 +00:00
|
|
|
const std::vector<uint32_t>& binary_in, std::vector<uint32_t>* binary_out,
|
2019-03-21 14:28:06 +00:00
|
|
|
spv_const_reducer_options options,
|
2019-10-08 12:02:34 +00:00
|
|
|
spv_validator_options validator_options) {
|
2021-04-09 12:55:51 +00:00
|
|
|
std::vector<uint32_t> current_binary(binary_in);
|
2019-03-21 14:28:06 +00:00
|
|
|
|
2019-10-08 12:02:34 +00:00
|
|
|
spvtools::SpirvTools tools(target_env_);
|
2019-03-21 14:28:06 +00:00
|
|
|
assert(tools.IsValid() && "Failed to create SPIRV-Tools interface");
|
2018-11-21 19:03:09 +00:00
|
|
|
|
|
|
|
// Keeps track of how many reduction attempts have been tried. Reduction
|
|
|
|
// bails out if this reaches a given limit.
|
|
|
|
uint32_t reductions_applied = 0;
|
|
|
|
|
2019-03-21 14:28:06 +00:00
|
|
|
// Initial state should be valid.
|
|
|
|
if (!tools.Validate(¤t_binary[0], current_binary.size(),
|
|
|
|
validator_options)) {
|
2019-10-08 12:02:34 +00:00
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {},
|
|
|
|
"Initial binary is invalid; stopping.");
|
2019-03-21 14:28:06 +00:00
|
|
|
return Reducer::ReductionResultStatus::kInitialStateInvalid;
|
|
|
|
}
|
|
|
|
|
2018-11-21 19:03:09 +00:00
|
|
|
// Initial state should be interesting.
|
2019-10-08 12:02:34 +00:00
|
|
|
if (!interestingness_function_(current_binary, reductions_applied)) {
|
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {},
|
|
|
|
"Initial state was not interesting; stopping.");
|
2018-11-21 19:03:09 +00:00
|
|
|
return Reducer::ReductionResultStatus::kInitialStateNotInteresting;
|
|
|
|
}
|
|
|
|
|
2019-10-08 12:02:34 +00:00
|
|
|
Reducer::ReductionResultStatus result =
|
|
|
|
RunPasses(&passes_, options, validator_options, tools, ¤t_binary,
|
|
|
|
&reductions_applied);
|
|
|
|
|
|
|
|
if (result == Reducer::ReductionResultStatus::kComplete) {
|
|
|
|
// Cleanup passes.
|
|
|
|
result = RunPasses(&cleanup_passes_, options, validator_options, tools,
|
|
|
|
¤t_binary, &reductions_applied);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (result == Reducer::ReductionResultStatus::kComplete) {
|
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {}, "No more to reduce; stopping.");
|
|
|
|
}
|
|
|
|
|
|
|
|
// Even if the reduction has failed by this point (e.g. due to producing an
|
|
|
|
// invalid binary), we still update the output binary for better debugging.
|
|
|
|
*binary_out = std::move(current_binary);
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Reducer::AddDefaultReductionPasses() {
|
|
|
|
AddReductionPass(
|
2020-05-13 21:08:40 +00:00
|
|
|
spvtools::MakeUnique<RemoveUnusedInstructionReductionOpportunityFinder>(
|
|
|
|
false));
|
2019-10-08 12:02:34 +00:00
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<OperandToUndefReductionOpportunityFinder>());
|
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<OperandToConstReductionOpportunityFinder>());
|
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<OperandToDominatingIdReductionOpportunityFinder>());
|
2021-07-06 06:14:36 +00:00
|
|
|
AddReductionPass(spvtools::MakeUnique<
|
|
|
|
StructuredConstructToBlockReductionOpportunityFinder>());
|
2019-10-08 12:02:34 +00:00
|
|
|
AddReductionPass(spvtools::MakeUnique<
|
|
|
|
StructuredLoopToSelectionReductionOpportunityFinder>());
|
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<MergeBlocksReductionOpportunityFinder>());
|
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<RemoveFunctionReductionOpportunityFinder>());
|
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<RemoveBlockReductionOpportunityFinder>());
|
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<RemoveSelectionReductionOpportunityFinder>());
|
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<
|
|
|
|
ConditionalBranchToSimpleConditionalBranchOpportunityFinder>());
|
|
|
|
AddReductionPass(
|
|
|
|
spvtools::MakeUnique<SimpleConditionalBranchToBranchOpportunityFinder>());
|
2020-05-14 14:36:38 +00:00
|
|
|
AddReductionPass(spvtools::MakeUnique<
|
|
|
|
RemoveUnusedStructMemberReductionOpportunityFinder>());
|
2019-10-08 12:02:34 +00:00
|
|
|
|
|
|
|
// Cleanup passes.
|
|
|
|
|
|
|
|
AddCleanupReductionPass(
|
2020-05-13 21:08:40 +00:00
|
|
|
spvtools::MakeUnique<RemoveUnusedInstructionReductionOpportunityFinder>(
|
|
|
|
true));
|
2019-10-08 12:02:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reducer::AddReductionPass(
|
2021-04-09 12:55:51 +00:00
|
|
|
std::unique_ptr<ReductionOpportunityFinder> finder) {
|
2019-10-08 12:02:34 +00:00
|
|
|
passes_.push_back(
|
|
|
|
spvtools::MakeUnique<ReductionPass>(target_env_, std::move(finder)));
|
2021-07-06 06:14:36 +00:00
|
|
|
}
|
2019-10-08 12:02:34 +00:00
|
|
|
|
|
|
|
void Reducer::AddCleanupReductionPass(
|
2021-04-09 12:55:51 +00:00
|
|
|
std::unique_ptr<ReductionOpportunityFinder> finder) {
|
2019-10-08 12:02:34 +00:00
|
|
|
cleanup_passes_.push_back(
|
|
|
|
spvtools::MakeUnique<ReductionPass>(target_env_, std::move(finder)));
|
|
|
|
}
|
|
|
|
|
|
|
|
bool Reducer::ReachedStepLimit(uint32_t current_step,
|
|
|
|
spv_const_reducer_options options) {
|
|
|
|
return current_step >= options->step_limit;
|
|
|
|
}
|
|
|
|
|
|
|
|
Reducer::ReductionResultStatus Reducer::RunPasses(
|
|
|
|
std::vector<std::unique_ptr<ReductionPass>>* passes,
|
|
|
|
spv_const_reducer_options options, spv_validator_options validator_options,
|
|
|
|
const SpirvTools& tools, std::vector<uint32_t>* current_binary,
|
|
|
|
uint32_t* const reductions_applied) {
|
2018-11-21 19:03:09 +00:00
|
|
|
// Determines whether, on completing one round of reduction passes, it is
|
|
|
|
// worthwhile trying a further round.
|
|
|
|
bool another_round_worthwhile = true;
|
|
|
|
|
|
|
|
// Apply round after round of reduction passes until we hit the reduction
|
|
|
|
// step limit, or deem that another round is not going to be worthwhile.
|
2019-10-08 12:02:34 +00:00
|
|
|
while (!ReachedStepLimit(*reductions_applied, options) &&
|
2018-11-21 19:03:09 +00:00
|
|
|
another_round_worthwhile) {
|
|
|
|
// At the start of a round of reduction passes, assume another round will
|
|
|
|
// not be worthwhile unless we find evidence to the contrary.
|
|
|
|
another_round_worthwhile = false;
|
|
|
|
|
2019-10-08 12:02:34 +00:00
|
|
|
// Iterate through the available passes.
|
|
|
|
for (auto& pass : *passes) {
|
2018-12-12 14:06:13 +00:00
|
|
|
// If this pass hasn't reached its minimum granularity then it's
|
|
|
|
// worth eventually doing another round of reductions, in order to
|
|
|
|
// try this pass at a finer granularity.
|
|
|
|
another_round_worthwhile |= !pass->ReachedMinimumGranularity();
|
|
|
|
|
2018-11-21 19:03:09 +00:00
|
|
|
// Keep applying this pass at its current granularity until it stops
|
|
|
|
// working or we hit the reduction step limit.
|
2019-10-08 12:02:34 +00:00
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {},
|
|
|
|
("Trying pass " + pass->GetName() + ".").c_str());
|
2018-11-21 19:03:09 +00:00
|
|
|
do {
|
2020-09-11 05:29:43 +00:00
|
|
|
auto maybe_result =
|
|
|
|
pass->TryApplyReduction(*current_binary, options->target_function);
|
2018-11-21 19:03:09 +00:00
|
|
|
if (maybe_result.empty()) {
|
2019-03-26 13:22:31 +00:00
|
|
|
// For this round, the pass has no more opportunities (chunks) to
|
|
|
|
// apply, so move on to the next pass.
|
2019-10-08 12:02:34 +00:00
|
|
|
consumer_(
|
2018-11-21 19:03:09 +00:00
|
|
|
SPV_MSG_INFO, nullptr, {},
|
|
|
|
("Pass " + pass->GetName() + " did not make a reduction step.")
|
|
|
|
.c_str());
|
|
|
|
break;
|
|
|
|
}
|
2019-03-26 13:22:31 +00:00
|
|
|
bool interesting = false;
|
2018-11-21 19:03:09 +00:00
|
|
|
std::stringstream stringstream;
|
2019-10-08 12:02:34 +00:00
|
|
|
(*reductions_applied)++;
|
2018-11-21 19:03:09 +00:00
|
|
|
stringstream << "Pass " << pass->GetName() << " made reduction step "
|
2019-10-08 12:02:34 +00:00
|
|
|
<< *reductions_applied << ".";
|
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {}, (stringstream.str().c_str()));
|
2019-03-21 14:28:06 +00:00
|
|
|
if (!tools.Validate(&maybe_result[0], maybe_result.size(),
|
|
|
|
validator_options)) {
|
2018-12-12 14:06:13 +00:00
|
|
|
// The reduction step went wrong and an invalid binary was produced.
|
|
|
|
// By design, this shouldn't happen; this is a safeguard to stop an
|
|
|
|
// invalid binary from being regarded as interesting.
|
2019-10-08 12:02:34 +00:00
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {},
|
|
|
|
"Reduction step produced an invalid binary.");
|
2019-03-26 13:22:31 +00:00
|
|
|
if (options->fail_on_validation_error) {
|
2019-10-08 12:02:34 +00:00
|
|
|
// In this mode, we fail, so we update the current binary so it is
|
|
|
|
// output for debugging.
|
|
|
|
*current_binary = std::move(maybe_result);
|
2019-03-26 13:22:31 +00:00
|
|
|
return Reducer::ReductionResultStatus::kStateInvalid;
|
|
|
|
}
|
2019-10-08 12:02:34 +00:00
|
|
|
} else if (interestingness_function_(maybe_result,
|
|
|
|
*reductions_applied)) {
|
2018-11-21 19:03:09 +00:00
|
|
|
// Success! The binary produced by this reduction step is
|
|
|
|
// interesting, so make it the binary of interest henceforth, and
|
|
|
|
// note that it's worth doing another round of reduction passes.
|
2019-10-08 12:02:34 +00:00
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {}, "Reduction step succeeded.");
|
|
|
|
*current_binary = std::move(maybe_result);
|
2019-03-26 13:22:31 +00:00
|
|
|
interesting = true;
|
2018-11-21 19:03:09 +00:00
|
|
|
another_round_worthwhile = true;
|
|
|
|
}
|
2019-03-26 13:22:31 +00:00
|
|
|
// We must call this before the next call to TryApplyReduction.
|
|
|
|
pass->NotifyInteresting(interesting);
|
2018-11-21 19:03:09 +00:00
|
|
|
// Bail out if the reduction step limit has been reached.
|
2019-10-08 12:02:34 +00:00
|
|
|
} while (!ReachedStepLimit(*reductions_applied, options));
|
2018-11-21 19:03:09 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Report whether reduction completed, or bailed out early due to reaching
|
|
|
|
// the step limit.
|
2019-10-08 12:02:34 +00:00
|
|
|
if (ReachedStepLimit(*reductions_applied, options)) {
|
|
|
|
consumer_(SPV_MSG_INFO, nullptr, {},
|
|
|
|
"Reached reduction step limit; stopping.");
|
2018-11-21 19:03:09 +00:00
|
|
|
return Reducer::ReductionResultStatus::kReachedStepLimit;
|
|
|
|
}
|
|
|
|
|
2019-10-08 12:02:34 +00:00
|
|
|
// The passes completed successfully, although we may still run more passes.
|
|
|
|
return Reducer::ReductionResultStatus::kComplete;
|
2018-11-21 19:03:09 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace reduce
|
2018-11-22 19:35:40 +00:00
|
|
|
} // namespace spvtools
|