reduce: miscellaneous fixes (#2494)

* Fix .gitignore 
* Add missing reduction pass: RemoveBlockReductionOpportunityFinder 
* Add DumpShader functions in test_reduce for debugging 
* Add DumpShader functions in spirv-reduce for debugging 
* Fix include style 
* Don't use "using namespace"
This commit is contained in:
Paul Thomson 2019-04-08 19:37:17 +01:00 committed by GitHub
parent 7ce37d66a8
commit d90aae9a5a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
48 changed files with 201 additions and 130 deletions

4
.gitignore vendored
View File

@ -21,5 +21,5 @@ compile_commands.json
*~
# C-Lion
.idea
cmake-build-debug
/.idea/
/cmake-build-*/

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "change_operand_reduction_opportunity.h"
#include "source/reduce/change_operand_reduction_opportunity.h"
namespace spvtools {
namespace reduce {

View File

@ -15,22 +15,20 @@
#ifndef SOURCE_REDUCE_CHANGE_OPERAND_REDUCTION_OPPORTUNITY_H_
#define SOURCE_REDUCE_CHANGE_OPERAND_REDUCTION_OPPORTUNITY_H_
#include "reduction_opportunity.h"
#include "source/opt/instruction.h"
#include "source/reduce/reduction_opportunity.h"
#include "spirv-tools/libspirv.h"
namespace spvtools {
namespace reduce {
using namespace opt;
// An opportunity to replace an id operand of an instruction with some other id.
class ChangeOperandReductionOpportunity : public ReductionOpportunity {
public:
// Constructs the opportunity to replace operand |operand_index| of |inst|
// with |new_id|.
ChangeOperandReductionOpportunity(Instruction* inst, uint32_t operand_index,
uint32_t new_id)
ChangeOperandReductionOpportunity(opt::Instruction* inst,
uint32_t operand_index, uint32_t new_id)
: inst_(inst),
operand_index_(operand_index),
original_id_(inst->GetOperand(operand_index).words[0]),
@ -43,7 +41,7 @@ class ChangeOperandReductionOpportunity : public ReductionOpportunity {
void Apply() override;
private:
Instruction* const inst_;
opt::Instruction* const inst_;
const uint32_t operand_index_;
const uint32_t original_id_;
const spv_operand_type_t original_type_;

View File

@ -12,15 +12,17 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "merge_blocks_reduction_opportunity.h"
#include "source/opt/block_merge_util.h"
#include "source/reduce/merge_blocks_reduction_opportunity.h"
#include "source/opt/block_merge_util.h"
#include "source/opt/ir_context.h"
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::BasicBlock;
using opt::Function;
using opt::IRContext;
MergeBlocksReductionOpportunity::MergeBlocksReductionOpportunity(
IRContext* context, Function* function, BasicBlock* block) {
@ -48,7 +50,8 @@ bool MergeBlocksReductionOpportunity::PreconditionHolds() {
"predecessor must be present.");
const uint32_t predecessor_id = predecessors[0];
BasicBlock* predecessor_block = context_->get_instr_block(predecessor_id);
return blockmergeutil::CanMergeWithSuccessor(context_, predecessor_block);
return opt::blockmergeutil::CanMergeWithSuccessor(context_,
predecessor_block);
}
void MergeBlocksReductionOpportunity::Apply() {
@ -65,7 +68,7 @@ void MergeBlocksReductionOpportunity::Apply() {
// We need an iterator pointing to the predecessor, hence the loop.
for (auto bi = function_->begin(); bi != function_->end(); ++bi) {
if (bi->id() == predecessor_id) {
blockmergeutil::MergeWithSuccessor(context_, function_, bi);
opt::blockmergeutil::MergeWithSuccessor(context_, function_, bi);
// Block merging changes the control flow graph, so invalidate it.
context_->InvalidateAnalysesExceptFor(IRContext::Analysis::kAnalysisNone);
return;

View File

@ -15,9 +15,9 @@
#ifndef SOURCE_REDUCE_MERGE_BLOCKS_REDUCTION_OPPORTUNITY_H_
#define SOURCE_REDUCE_MERGE_BLOCKS_REDUCTION_OPPORTUNITY_H_
#include "reduction_opportunity.h"
#include "source/opt/basic_block.h"
#include "source/opt/function.h"
#include "source/reduce/reduction_opportunity.h"
namespace spvtools {
namespace reduce {

View File

@ -19,7 +19,7 @@
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::IRContext;
std::string MergeBlocksReductionOpportunityFinder::GetName() const {
return "MergeBlocksReductionOpportunityFinder";
@ -27,14 +27,14 @@ std::string MergeBlocksReductionOpportunityFinder::GetName() const {
std::vector<std::unique_ptr<ReductionOpportunity>>
MergeBlocksReductionOpportunityFinder::GetAvailableOpportunities(
opt::IRContext* context) const {
IRContext* context) const {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
// Consider every block in every function.
for (auto& function : *context->module()) {
for (auto& block : function) {
// See whether it is possible to merge this block with its successor.
if (blockmergeutil::CanMergeWithSuccessor(context, &block)) {
if (opt::blockmergeutil::CanMergeWithSuccessor(context, &block)) {
// It is, so record an opportunity to do this.
result.push_back(spvtools::MakeUnique<MergeBlocksReductionOpportunity>(
context, &function, &block));

View File

@ -20,11 +20,11 @@
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::IRContext;
std::vector<std::unique_ptr<ReductionOpportunity>>
OperandToConstReductionOpportunityFinder::GetAvailableOpportunities(
opt::IRContext* context) const {
IRContext* context) const {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
assert(result.empty());

View File

@ -12,18 +12,21 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "operand_to_dominating_id_reduction_opportunity_finder.h"
#include "change_operand_reduction_opportunity.h"
#include "source/reduce/operand_to_dominating_id_reduction_opportunity_finder.h"
#include "source/opt/instruction.h"
#include "source/reduce/change_operand_reduction_opportunity.h"
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::Function;
using opt::IRContext;
using opt::Instruction;
std::vector<std::unique_ptr<ReductionOpportunity>>
OperandToDominatingIdReductionOpportunityFinder::GetAvailableOpportunities(
opt::IRContext* context) const {
IRContext* context) const {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
// Go through every instruction in every block, considering it as a potential
@ -58,9 +61,9 @@ OperandToDominatingIdReductionOpportunityFinder::GetAvailableOpportunities(
void OperandToDominatingIdReductionOpportunityFinder::
GetOpportunitiesForDominatingInst(
std::vector<std::unique_ptr<ReductionOpportunity>>* opportunities,
opt::Instruction* candidate_dominator,
opt::Function::iterator candidate_dominator_block,
opt::Function* function, opt::IRContext* context) const {
Instruction* candidate_dominator,
Function::iterator candidate_dominator_block, Function* function,
IRContext* context) const {
assert(candidate_dominator->HasResultId());
assert(candidate_dominator->type_id());
auto dominator_analysis = context->GetDominatorAnalysis(function);

View File

@ -15,7 +15,7 @@
#ifndef SOURCE_REDUCE_OPERAND_TO_DOMINATING_ID_REDUCTION_OPPORTUNITY_FINDER_H_
#define SOURCE_REDUCE_OPERAND_TO_DOMINATING_ID_REDUCTION_OPPORTUNITY_FINDER_H_
#include "reduction_opportunity_finder.h"
#include "source/reduce/reduction_opportunity_finder.h"
namespace spvtools {
namespace reduce {

View File

@ -20,7 +20,7 @@
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::IRContext;
std::vector<std::unique_ptr<ReductionOpportunity>>
OperandToUndefReductionOpportunityFinder::GetAvailableOpportunities(

View File

@ -12,6 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "source/reduce/reducer.h"
#include <cassert>
#include <sstream>
@ -19,6 +21,7 @@
#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"
#include "source/reduce/remove_block_reduction_opportunity_finder.h"
#include "source/reduce/remove_function_reduction_opportunity_finder.h"
#include "source/reduce/remove_opname_instruction_reduction_opportunity_finder.h"
#include "source/reduce/remove_selection_reduction_opportunity_finder.h"
@ -26,8 +29,6 @@
#include "source/reduce/structured_loop_to_selection_reduction_opportunity_finder.h"
#include "source/spirv_reducer_options.h"
#include "reducer.h"
namespace spvtools {
namespace reduce {
@ -186,6 +187,8 @@ void Reducer::AddDefaultReductionPasses() {
spvtools::MakeUnique<MergeBlocksReductionOpportunityFinder>());
AddReductionPass(
spvtools::MakeUnique<RemoveFunctionReductionOpportunityFinder>());
AddReductionPass(
spvtools::MakeUnique<RemoveBlockReductionOpportunityFinder>());
AddReductionPass(
spvtools::MakeUnique<RemoveSelectionReductionOpportunityFinder>());
}

View File

@ -18,10 +18,9 @@
#include <functional>
#include <string>
#include "source/reduce/reduction_pass.h"
#include "spirv-tools/libspirv.hpp"
#include "reduction_pass.h"
namespace spvtools {
namespace reduce {

View File

@ -12,7 +12,7 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduction_opportunity.h"
#include "source/reduce/reduction_opportunity.h"
namespace spvtools {
namespace reduce {

View File

@ -15,8 +15,8 @@
#ifndef SOURCE_REDUCE_REDUCTION_OPPORTUNITY_FINDER_H_
#define SOURCE_REDUCE_REDUCTION_OPPORTUNITY_FINDER_H_
#include "reduction_opportunity.h"
#include "source/opt/ir_context.h"
#include "source/reduce/reduction_opportunity.h"
namespace spvtools {
namespace reduce {

View File

@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include <algorithm>
#include "source/reduce/reduction_pass.h"
#include "reduction_pass.h"
#include <algorithm>
#include "source/opt/build_module.h"

View File

@ -19,7 +19,8 @@
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::IRContext;
using opt::Instruction;
uint32_t FindOrCreateGlobalUndef(IRContext* context, uint32_t type_id) {
for (auto& inst : context->module()->types_values()) {

View File

@ -12,14 +12,15 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "remove_block_reduction_opportunity.h"
#include "source/reduce/remove_block_reduction_opportunity.h"
#include "source/opt/ir_context.h"
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::BasicBlock;
using opt::Function;
RemoveBlockReductionOpportunity::RemoveBlockReductionOpportunity(
Function* function, BasicBlock* block)

View File

@ -15,9 +15,9 @@
#ifndef SOURCE_REDUCE_REMOVE_BLOCK_REDUCTION_OPPORTUNITY_H_
#define SOURCE_REDUCE_REMOVE_BLOCK_REDUCTION_OPPORTUNITY_H_
#include "reduction_opportunity.h"
#include "source/opt/basic_block.h"
#include "source/opt/function.h"
#include "source/reduce/reduction_opportunity.h"
namespace spvtools {
namespace reduce {

View File

@ -13,12 +13,15 @@
// limitations under the License.
#include "source/reduce/remove_block_reduction_opportunity_finder.h"
#include "source/reduce/remove_block_reduction_opportunity.h"
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::Function;
using opt::IRContext;
using opt::Instruction;
std::string RemoveBlockReductionOpportunityFinder::GetName() const {
return "RemoveBlockReductionOpportunityFinder";
@ -26,7 +29,7 @@ std::string RemoveBlockReductionOpportunityFinder::GetName() const {
std::vector<std::unique_ptr<ReductionOpportunity>>
RemoveBlockReductionOpportunityFinder::GetAvailableOpportunities(
opt::IRContext* context) const {
IRContext* context) const {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
// Consider every block in every function.
@ -42,8 +45,7 @@ RemoveBlockReductionOpportunityFinder::GetAvailableOpportunities(
}
bool RemoveBlockReductionOpportunityFinder::IsBlockValidOpportunity(
opt::IRContext* context, opt::Function& function,
opt::Function::iterator& bi) {
IRContext* context, Function& function, Function::iterator& bi) {
assert(bi != function.end() && "Block iterator was out of bounds");
// Don't remove first block; we don't want to end up with no blocks.
@ -65,7 +67,7 @@ bool RemoveBlockReductionOpportunityFinder::IsBlockValidOpportunity(
}
bool RemoveBlockReductionOpportunityFinder::
BlockInstructionsHaveNoOutsideReferences(opt::IRContext* context,
BlockInstructionsHaveNoOutsideReferences(IRContext* context,
const Function::iterator& bi) {
// Get all instructions in block.
std::unordered_set<uint32_t> instructions_in_block;

View File

@ -12,7 +12,8 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "remove_function_reduction_opportunity.h"
#include "source/reduce/remove_function_reduction_opportunity.h"
#include "source/opt/eliminate_dead_functions_util.h"
namespace spvtools {

View File

@ -15,8 +15,8 @@
#ifndef SOURCE_REDUCE_REMOVE_FUNCTION_REDUCTION_OPPORTUNITY_H_
#define SOURCE_REDUCE_REMOVE_FUNCTION_REDUCTION_OPPORTUNITY_H_
#include "reduction_opportunity.h"
#include "source/opt/function.h"
#include "source/reduce/reduction_opportunity.h"
namespace spvtools {
namespace reduce {

View File

@ -12,8 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "remove_function_reduction_opportunity_finder.h"
#include "remove_function_reduction_opportunity.h"
#include "source/reduce/remove_function_reduction_opportunity_finder.h"
#include "source/reduce/remove_function_reduction_opportunity.h"
namespace spvtools {
namespace reduce {

View File

@ -12,9 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "source/opt/ir_context.h"
#include "source/reduce/remove_instruction_reduction_opportunity.h"
#include "remove_instruction_reduction_opportunity.h"
#include "source/opt/ir_context.h"
namespace spvtools {
namespace reduce {

View File

@ -15,19 +15,17 @@
#ifndef SOURCE_REDUCE_REMOVE_INSTRUCTION_REDUCTION_OPPORTUNITY_H_
#define SOURCE_REDUCE_REMOVE_INSTRUCTION_REDUCTION_OPPORTUNITY_H_
#include "reduction_opportunity.h"
#include "source/opt/instruction.h"
#include "source/reduce/reduction_opportunity.h"
namespace spvtools {
namespace reduce {
using namespace opt;
// An opportunity to remove an instruction from the SPIR-V module.
class RemoveInstructionReductionOpportunity : public ReductionOpportunity {
public:
// Constructs the opportunity to remove |inst|.
explicit RemoveInstructionReductionOpportunity(Instruction* inst)
explicit RemoveInstructionReductionOpportunity(opt::Instruction* inst)
: inst_(inst) {}
// Always returns true, as this opportunity can always be applied.
@ -37,7 +35,7 @@ class RemoveInstructionReductionOpportunity : public ReductionOpportunity {
void Apply() override;
private:
Instruction* inst_;
opt::Instruction* inst_;
};
} // namespace reduce

View File

@ -12,19 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "remove_opname_instruction_reduction_opportunity_finder.h"
#include "remove_instruction_reduction_opportunity.h"
#include "source/reduce/remove_opname_instruction_reduction_opportunity_finder.h"
#include "source/opcode.h"
#include "source/opt/instruction.h"
#include "source/reduce/remove_instruction_reduction_opportunity.h"
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::IRContext;
std::vector<std::unique_ptr<ReductionOpportunity>>
RemoveOpNameInstructionReductionOpportunityFinder::GetAvailableOpportunities(
opt::IRContext* context) const {
IRContext* context) const {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
for (auto& inst : context->module()->debugs2()) {

View File

@ -15,7 +15,7 @@
#ifndef SOURCE_REDUCE_REMOVE_OPNAME_INSTRUCTION_REDUCTION_OPPORTUNITY_FINDER_H_
#define SOURCE_REDUCE_REMOVE_OPNAME_INSTRUCTION_REDUCTION_OPPORTUNITY_FINDER_H_
#include "reduction_opportunity_finder.h"
#include "source/reduce/reduction_opportunity_finder.h"
namespace spvtools {
namespace reduce {

View File

@ -19,7 +19,9 @@
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::BasicBlock;
using opt::IRContext;
using opt::Instruction;
namespace {
const uint32_t kMergeNodeIndex = 0;
@ -32,7 +34,7 @@ std::string RemoveSelectionReductionOpportunityFinder::GetName() const {
std::vector<std::unique_ptr<ReductionOpportunity>>
RemoveSelectionReductionOpportunityFinder::GetAvailableOpportunities(
opt::IRContext* context) const {
IRContext* context) const {
// Get all loop merge and continue blocks so we can check for these later.
std::unordered_set<uint32_t> merge_and_continue_blocks_from_loops;
for (auto& function : *context->module()) {
@ -71,8 +73,8 @@ RemoveSelectionReductionOpportunityFinder::GetAvailableOpportunities(
}
bool RemoveSelectionReductionOpportunityFinder::CanOpSelectionMergeBeRemoved(
opt::IRContext* context, const opt::BasicBlock& header_block,
opt::Instruction* merge_instruction,
IRContext* context, const BasicBlock& header_block,
Instruction* merge_instruction,
std::unordered_set<uint32_t> merge_and_continue_blocks_from_loops) {
assert(header_block.GetMergeInst() == merge_instruction &&
"CanOpSelectionMergeBeRemoved(...): header block and merge "

View File

@ -12,19 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "remove_unreferenced_instruction_reduction_opportunity_finder.h"
#include "remove_instruction_reduction_opportunity.h"
#include "source/reduce/remove_unreferenced_instruction_reduction_opportunity_finder.h"
#include "source/opcode.h"
#include "source/opt/instruction.h"
#include "source/reduce/remove_instruction_reduction_opportunity.h"
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::IRContext;
std::vector<std::unique_ptr<ReductionOpportunity>>
RemoveUnreferencedInstructionReductionOpportunityFinder::
GetAvailableOpportunities(opt::IRContext* context) const {
GetAvailableOpportunities(IRContext* context) const {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
for (auto& function : *context->module()) {

View File

@ -15,7 +15,7 @@
#ifndef SOURCE_REDUCE_REMOVE_UNREFERENCED_INSTRUCTION_REDUCTION_OPPORTUNITY_FINDER_H_
#define SOURCE_REDUCE_REMOVE_UNREFERENCED_INSTRUCTION_REDUCTION_OPPORTUNITY_FINDER_H_
#include "reduction_opportunity_finder.h"
#include "source/reduce/reduction_opportunity_finder.h"
namespace spvtools {
namespace reduce {

View File

@ -21,6 +21,11 @@
namespace spvtools {
namespace reduce {
using opt::BasicBlock;
using opt::IRContext;
using opt::Instruction;
using opt::Operand;
namespace {
const uint32_t kMergeNodeIndex = 0;
} // namespace
@ -208,8 +213,8 @@ void StructuredLoopToSelectionReductionOpportunity::ChangeLoopToSelection() {
// the "else" branch be the merge block.
auto terminator = loop_construct_header_->terminator();
if (terminator->opcode() == SpvOpBranch) {
analysis::Bool temp;
const analysis::Bool* bool_type =
opt::analysis::Bool temp;
const opt::analysis::Bool* bool_type =
context_->get_type_mgr()->GetRegisteredType(&temp)->AsBool();
auto const_mgr = context_->get_constant_mgr();
auto true_const = const_mgr->GetConstant(bool_type, {1});

View File

@ -23,8 +23,6 @@
namespace spvtools {
namespace reduce {
using namespace opt;
// An opportunity to replace a structured loop with a selection.
class StructuredLoopToSelectionReductionOpportunity
: public ReductionOpportunity {
@ -32,8 +30,8 @@ class StructuredLoopToSelectionReductionOpportunity
// Constructs an opportunity from a loop header block and the function that
// encloses it.
explicit StructuredLoopToSelectionReductionOpportunity(
IRContext* context, BasicBlock* loop_construct_header,
Function* enclosing_function)
opt::IRContext* context, opt::BasicBlock* loop_construct_header,
opt::Function* enclosing_function)
: context_(context),
loop_construct_header_(loop_construct_header),
enclosing_function_(enclosing_function) {}
@ -67,11 +65,12 @@ class StructuredLoopToSelectionReductionOpportunity
// Removes any components of |to_block|'s phi instructions relating to
// |from_id|.
void AdaptPhiInstructionsForRemovedEdge(uint32_t from_id,
BasicBlock* to_block);
opt::BasicBlock* to_block);
// Adds components to |to_block|'s phi instructions to account for a new
// incoming edge from |from_id|.
void AdaptPhiInstructionsForAddedEdge(uint32_t from_id, BasicBlock* to_block);
void AdaptPhiInstructionsForAddedEdge(uint32_t from_id,
opt::BasicBlock* to_block);
// Turns the OpLoopMerge for the loop into OpSelectionMerge, and adapts the
// following branch instruction accordingly.
@ -87,9 +86,10 @@ class StructuredLoopToSelectionReductionOpportunity
// 2) |def| is an OpVariable
// 3) |use| is part of an OpPhi, with associated incoming block b, and |def|
// dominates b.
bool DefinitionSufficientlyDominatesUse(Instruction* def, Instruction* use,
bool DefinitionSufficientlyDominatesUse(opt::Instruction* def,
opt::Instruction* use,
uint32_t use_index,
BasicBlock& def_block);
opt::BasicBlock& def_block);
// Checks whether the global value list has an OpVariable of the given pointer
// type, adding one if not, and returns the id of such an OpVariable.
@ -105,9 +105,9 @@ class StructuredLoopToSelectionReductionOpportunity
// be factored out in due course.
uint32_t FindOrCreateFunctionVariable(uint32_t pointer_type_id);
IRContext* context_;
BasicBlock* loop_construct_header_;
Function* enclosing_function_;
opt::IRContext* context_;
opt::BasicBlock* loop_construct_header_;
opt::Function* enclosing_function_;
};
} // namespace reduce

View File

@ -12,13 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "structured_loop_to_selection_reduction_opportunity_finder.h"
#include "structured_loop_to_selection_reduction_opportunity.h"
#include "source/reduce/structured_loop_to_selection_reduction_opportunity_finder.h"
#include "source/reduce/structured_loop_to_selection_reduction_opportunity.h"
namespace spvtools {
namespace reduce {
using namespace opt;
using opt::IRContext;
namespace {
const uint32_t kMergeNodeIndex = 0;
@ -27,7 +28,7 @@ const uint32_t kContinueNodeIndex = 1;
std::vector<std::unique_ptr<ReductionOpportunity>>
StructuredLoopToSelectionReductionOpportunityFinder::GetAvailableOpportunities(
opt::IRContext* context) const {
IRContext* context) const {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
std::set<uint32_t> merge_block_ids;

View File

@ -15,7 +15,7 @@
#ifndef SOURCE_REDUCE_STRUCTURED_LOOP_TO_SELECTION_REDUCTION_OPPORTUNITY_FINDER_H
#define SOURCE_REDUCE_STRUCTURED_LOOP_TO_SELECTION_REDUCTION_OPPORTUNITY_FINDER_H
#include "reduction_opportunity_finder.h"
#include "source/reduce/reduction_opportunity_finder.h"
namespace spvtools {
namespace reduce {

View File

@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/opt/build_module.h"
#include "source/reduce/merge_blocks_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {

View File

@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/opt/build_module.h"
#include "source/reduce/operand_to_const_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {
namespace {

View File

@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/opt/build_module.h"
#include "source/reduce/operand_to_dominating_id_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {
namespace {

View File

@ -12,8 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "source/opt/build_module.h"
#include "source/reduce/operand_to_undef_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {

View File

@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "test/reduce/reduce_test_util.h"
#include <iostream>
#include "tools/io.h"
namespace spvtools {
namespace reduce {
@ -92,5 +94,19 @@ void CLIMessageConsumer(spv_message_level_t level, const char*,
}
}
void DumpShader(opt::IRContext* context, const char* filename) {
std::vector<uint32_t> binary;
context->module()->ToBinary(&binary, false);
DumpShader(binary, filename);
}
void DumpShader(const std::vector<uint32_t>& binary, const char* filename) {
auto write_file_succeeded =
WriteFile(filename, "wb", &binary[0], binary.size());
if (!write_file_succeeded) {
std::cerr << "Failed to dump shader" << std::endl;
}
}
} // namespace reduce
} // namespace spvtools

View File

@ -16,7 +16,6 @@
#define TEST_REDUCE_REDUCE_TEST_UTIL_H_
#include "gtest/gtest.h"
#include "source/opt/ir_context.h"
#include "source/reduce/reduction_opportunity.h"
#include "spirv-tools/libspirv.h"
@ -63,6 +62,13 @@ void NopDiagnostic(spv_message_level_t /*level*/, const char* /*source*/,
void CLIMessageConsumer(spv_message_level_t level, const char*,
const spv_position_t& position, const char* message);
// Dumps the SPIRV-V module in |context| to file |filename|. Useful for
// interactive debugging.
void DumpShader(opt::IRContext* context, const char* filename);
// Dumps |binary| to file |filename|. Useful for interactive debugging.
void DumpShader(const std::vector<uint32_t>& binary, const char* filename);
} // namespace reduce
} // namespace spvtools

View File

@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/reduce/reducer.h"
#include "source/reduce/operand_to_const_reduction_opportunity_finder.h"
#include "source/reduce/reducer.h"
#include "source/reduce/remove_opname_instruction_reduction_opportunity_finder.h"
#include "source/reduce/remove_unreferenced_instruction_reduction_opportunity_finder.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {

View File

@ -12,11 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/reduce/remove_block_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "source/reduce/remove_block_reduction_opportunity.h"
#include "source/reduce/remove_block_reduction_opportunity_finder.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {

View File

@ -12,10 +12,11 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/reduce/remove_function_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "source/reduce/remove_function_reduction_opportunity_finder.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {

View File

@ -12,12 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/reduce/remove_opname_instruction_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "source/reduce/reduction_pass.h"
#include "source/reduce/remove_opname_instruction_reduction_opportunity_finder.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {

View File

@ -12,10 +12,10 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "source/reduce/remove_selection_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "source/reduce/reduction_pass.h"
#include "source/reduce/remove_selection_reduction_opportunity_finder.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {

View File

@ -12,13 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/reduce/remove_unreferenced_instruction_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "source/reduce/reduction_pass.h"
#include "source/reduce/remove_unreferenced_instruction_reduction_opportunity_finder.h"
#include "source/util/make_unique.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {

View File

@ -12,10 +12,12 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/opt/build_module.h"
#include "source/reduce/structured_loop_to_selection_reduction_opportunity_finder.h"
#include "source/opt/build_module.h"
#include "source/reduce/reduction_opportunity.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {
namespace {

View File

@ -12,16 +12,20 @@
// See the License for the specific language governing permissions and
// limitations under the License.
#include "reduce_test_util.h"
#include "source/reduce/reducer.h"
#include "source/reduce/reduction_pass.h"
#include "source/reduce/reduction_opportunity.h"
#include "source/reduce/remove_instruction_reduction_opportunity.h"
#include "test/reduce/reduce_test_util.h"
namespace spvtools {
namespace reduce {
namespace {
using opt::Function;
using opt::IRContext;
using opt::Instruction;
// A dumb reduction opportunity finder that finds opportunities to remove global
// values regardless of whether they are referenced. This is very likely to make
// the resulting module invalid. We use this to test the reducer's behavior in
@ -40,7 +44,7 @@ class BlindlyRemoveGlobalValuesReductionOpportunityFinder
// referenced (directly or indirectly) from elsewhere in the module, each such
// opportunity will make the module invalid.
std::vector<std::unique_ptr<ReductionOpportunity>> GetAvailableOpportunities(
opt::IRContext* context) const final {
IRContext* context) const final {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
for (auto& inst : context->module()->types_values()) {
if (inst.HasResultId()) {
@ -59,8 +63,8 @@ class BlindlyRemoveGlobalValuesReductionOpportunityFinder
// limits are enforced.
class OpVariableDuplicatorReductionOpportunity : public ReductionOpportunity {
public:
OpVariableDuplicatorReductionOpportunity(Function* function_)
: function_(function_) {}
OpVariableDuplicatorReductionOpportunity(Function* function)
: function_(function) {}
bool PreconditionHolds() override {
Instruction* first_instruction = &*function_->begin()[0].begin();
@ -98,7 +102,7 @@ class OpVariableDuplicatorReductionOpportunityFinder
};
std::vector<std::unique_ptr<ReductionOpportunity>> GetAvailableOpportunities(
opt::IRContext* context) const final {
IRContext* context) const final {
std::vector<std::unique_ptr<ReductionOpportunity>> result;
for (auto& function : *context->module()) {
Instruction* first_instruction = &*function.begin()[0].begin();
@ -446,7 +450,7 @@ TEST(ValidationDuringReductionTest, CheckNotAlwaysInvalidCanMakeProgress) {
// Sets up a Reducer for use in the CheckValidationOptions test; avoids
// repetition.
void setupReducerForCheckValidationOptions(Reducer* reducer) {
void SetupReducerForCheckValidationOptions(Reducer* reducer) {
reducer->SetMessageConsumer(NopDiagnostic);
// Say that every module is interesting.
@ -531,7 +535,7 @@ TEST(ValidationDuringReductionTest, CheckValidationOptions) {
// always returns true.
{
Reducer reducer(env);
setupReducerForCheckValidationOptions(&reducer);
SetupReducerForCheckValidationOptions(&reducer);
Reducer::ReductionResultStatus status =
reducer.Run(std::vector<uint32_t>(binary_in), &binary_out,
@ -547,7 +551,7 @@ TEST(ValidationDuringReductionTest, CheckValidationOptions) {
// test always succeeds, and the finder yields infinite opportunities.
{
Reducer reducer(env);
setupReducerForCheckValidationOptions(&reducer);
SetupReducerForCheckValidationOptions(&reducer);
Reducer::ReductionResultStatus status =
reducer.Run(std::vector<uint32_t>(binary_in), &binary_out,
@ -565,7 +569,7 @@ TEST(ValidationDuringReductionTest, CheckValidationOptions) {
// validator limits.
{
Reducer reducer(env);
setupReducerForCheckValidationOptions(&reducer);
SetupReducerForCheckValidationOptions(&reducer);
Reducer::ReductionResultStatus status =
reducer.Run(std::vector<uint32_t>(binary_in), &binary_out,

View File

@ -18,6 +18,7 @@
#include <functional>
#include "source/opt/build_module.h"
#include "source/opt/ir_context.h"
#include "source/opt/log.h"
#include "source/reduce/reducer.h"
#include "source/spirv_reducer_options.h"
@ -25,8 +26,6 @@
#include "tools/io.h"
#include "tools/util/cli_consumer.h"
using namespace spvtools::reduce;
namespace {
using ErrorOrInt = std::pair<std::string, int>;
@ -200,6 +199,23 @@ ReduceStatus ParseFlags(int argc, const char** argv, const char** in_file,
} // namespace
// Dumps |binary| to file |filename|. Useful for interactive debugging.
void DumpShader(const std::vector<uint32_t>& binary, const char* filename) {
auto write_file_succeeded =
WriteFile(filename, "wb", &binary[0], binary.size());
if (!write_file_succeeded) {
std::cerr << "Failed to dump shader" << std::endl;
}
}
// Dumps the SPIRV-V module in |context| to file |filename|. Useful for
// interactive debugging.
void DumpShader(spvtools::opt::IRContext* context, const char* filename) {
std::vector<uint32_t> binary;
context->module()->ToBinary(&binary, false);
DumpShader(binary, filename);
}
const auto kDefaultEnvironment = SPV_ENV_UNIVERSAL_1_3;
int main(int argc, const char** argv) {
@ -223,7 +239,7 @@ int main(int argc, const char** argv) {
return 2;
}
Reducer reducer(target_env);
spvtools::reduce::Reducer reducer(target_env);
reducer.SetInterestingnessFunction(
[interestingness_test](std::vector<uint32_t> binary,
@ -254,8 +270,8 @@ int main(int argc, const char** argv) {
const auto reduction_status = reducer.Run(std::move(binary_in), &binary_out,
reducer_options, validator_options);
if (reduction_status ==
Reducer::ReductionResultStatus::kInitialStateNotInteresting ||
if (reduction_status == spvtools::reduce::Reducer::ReductionResultStatus::
kInitialStateNotInteresting ||
!WriteFile<uint32_t>("_reduced_final.spv", "wb", binary_out.data(),
binary_out.size())) {
return 1;