2016-06-28 18:58:17 +00:00
|
|
|
// Copyright (c) 2016 Google Inc.
|
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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
|
2016-06-28 18:58:17 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2016-06-28 18:58:17 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// 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.
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef TEST_OPT_PASS_FIXTURE_H_
|
|
|
|
#define TEST_OPT_PASS_FIXTURE_H_
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2016-08-20 03:17:44 +00:00
|
|
|
#include <iostream>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
2016-06-28 18:58:17 +00:00
|
|
|
#include <string>
|
2016-07-26 16:11:03 +00:00
|
|
|
#include <tuple>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <utility>
|
2016-06-28 18:58:17 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2018-10-04 14:00:11 +00:00
|
|
|
#include "effcee/effcee.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "gtest/gtest.h"
|
|
|
|
#include "source/opt/build_module.h"
|
|
|
|
#include "source/opt/pass_manager.h"
|
|
|
|
#include "source/opt/passes.h"
|
2019-07-10 18:12:19 +00:00
|
|
|
#include "source/spirv_optimizer_options.h"
|
2018-11-27 21:47:10 +00:00
|
|
|
#include "source/spirv_validator_options.h"
|
2018-08-14 16:44:54 +00:00
|
|
|
#include "source/util/make_unique.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "spirv-tools/libspirv.hpp"
|
2016-06-28 18:58:17 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
2018-07-11 13:24:49 +00:00
|
|
|
namespace opt {
|
2016-06-28 18:58:17 +00:00
|
|
|
|
|
|
|
// Template class for testing passes. It contains some handy utility methods for
|
|
|
|
// running passes and checking results.
|
|
|
|
//
|
|
|
|
// To write value-Parameterized tests:
|
|
|
|
// using ValueParamTest = PassTest<::testing::TestWithParam<std::string>>;
|
|
|
|
// To use as normal fixture:
|
|
|
|
// using FixtureTest = PassTest<::testing::Test>;
|
|
|
|
template <typename TestT>
|
|
|
|
class PassTest : public TestT {
|
|
|
|
public:
|
|
|
|
PassTest()
|
2018-11-30 19:15:51 +00:00
|
|
|
: consumer_(
|
|
|
|
[](spv_message_level_t, const char*, const spv_position_t&,
|
|
|
|
const char* message) { std::cerr << message << std::endl; }),
|
2017-11-14 19:11:50 +00:00
|
|
|
context_(nullptr),
|
2018-07-11 13:24:49 +00:00
|
|
|
manager_(new PassManager()),
|
2017-04-11 19:11:04 +00:00
|
|
|
assemble_options_(SpirvTools::kDefaultAssembleOption),
|
2019-05-07 18:52:22 +00:00
|
|
|
disassemble_options_(SpirvTools::kDefaultDisassembleOption),
|
|
|
|
env_(SPV_ENV_UNIVERSAL_1_3) {}
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2016-11-10 17:11:50 +00:00
|
|
|
// Runs the given |pass| on the binary assembled from the |original|.
|
2017-11-14 19:11:50 +00:00
|
|
|
// Returns a tuple of the optimized binary and the boolean value returned
|
2016-11-10 17:11:50 +00:00
|
|
|
// from pass Process() function.
|
2018-07-11 13:24:49 +00:00
|
|
|
std::tuple<std::vector<uint32_t>, Pass::Status> OptimizeToBinary(
|
|
|
|
Pass* pass, const std::string& original, bool skip_nop) {
|
2019-05-07 18:52:22 +00:00
|
|
|
context_ =
|
|
|
|
std::move(BuildModule(env_, consumer_, original, assemble_options_));
|
2017-11-14 19:11:50 +00:00
|
|
|
EXPECT_NE(nullptr, context()) << "Assembling failed for shader:\n"
|
|
|
|
<< original << std::endl;
|
|
|
|
if (!context()) {
|
2018-07-11 13:24:49 +00:00
|
|
|
return std::make_tuple(std::vector<uint32_t>(), Pass::Status::Failure);
|
2016-07-26 16:11:03 +00:00
|
|
|
}
|
|
|
|
|
2019-07-10 18:12:19 +00:00
|
|
|
context()->set_preserve_bindings(OptimizerOptions()->preserve_bindings_);
|
|
|
|
context()->set_preserve_spec_constants(
|
|
|
|
OptimizerOptions()->preserve_spec_constants_);
|
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
const auto status = pass->Run(context());
|
2016-07-26 16:11:03 +00:00
|
|
|
|
|
|
|
std::vector<uint32_t> binary;
|
2019-08-16 17:15:17 +00:00
|
|
|
if (status != Pass::Status::Failure) {
|
|
|
|
context()->module()->ToBinary(&binary, skip_nop);
|
|
|
|
}
|
2016-11-10 17:11:50 +00:00
|
|
|
return std::make_tuple(binary, status);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs a single pass of class |PassT| on the binary assembled from the
|
2017-11-14 19:11:50 +00:00
|
|
|
// |assembly|. Returns a tuple of the optimized binary and the boolean value
|
2016-11-10 17:11:50 +00:00
|
|
|
// from the pass Process() function.
|
|
|
|
template <typename PassT, typename... Args>
|
2018-07-11 13:24:49 +00:00
|
|
|
std::tuple<std::vector<uint32_t>, Pass::Status> SinglePassRunToBinary(
|
2016-11-10 17:11:50 +00:00
|
|
|
const std::string& assembly, bool skip_nop, Args&&... args) {
|
|
|
|
auto pass = MakeUnique<PassT>(std::forward<Args>(args)...);
|
|
|
|
pass->SetMessageConsumer(consumer_);
|
|
|
|
return OptimizeToBinary(pass.get(), assembly, skip_nop);
|
2016-07-26 16:11:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Runs a single pass of class |PassT| on the binary assembled from the
|
|
|
|
// |assembly|, disassembles the optimized binary. Returns a tuple of
|
|
|
|
// disassembly string and the boolean value from the pass Process() function.
|
2016-08-31 16:44:49 +00:00
|
|
|
template <typename PassT, typename... Args>
|
2018-07-11 13:24:49 +00:00
|
|
|
std::tuple<std::string, Pass::Status> SinglePassRunAndDisassemble(
|
2017-11-30 22:03:06 +00:00
|
|
|
const std::string& assembly, bool skip_nop, bool do_validation,
|
|
|
|
Args&&... args) {
|
2016-11-10 17:11:50 +00:00
|
|
|
std::vector<uint32_t> optimized_bin;
|
2018-07-11 13:24:49 +00:00
|
|
|
auto status = Pass::Status::SuccessWithoutChange;
|
2016-11-10 17:11:50 +00:00
|
|
|
std::tie(optimized_bin, status) = SinglePassRunToBinary<PassT>(
|
|
|
|
assembly, skip_nop, std::forward<Args>(args)...);
|
2017-11-30 22:03:06 +00:00
|
|
|
if (do_validation) {
|
2019-05-07 18:52:22 +00:00
|
|
|
spv_context spvContext = spvContextCreate(env_);
|
2017-11-30 22:03:06 +00:00
|
|
|
spv_diagnostic diagnostic = nullptr;
|
|
|
|
spv_const_binary_t binary = {optimized_bin.data(), optimized_bin.size()};
|
2018-11-27 21:47:10 +00:00
|
|
|
spv_result_t error = spvValidateWithOptions(
|
|
|
|
spvContext, ValidatorOptions(), &binary, &diagnostic);
|
2017-11-30 22:03:06 +00:00
|
|
|
EXPECT_EQ(error, 0);
|
|
|
|
if (error != 0) spvDiagnosticPrint(diagnostic);
|
|
|
|
spvDiagnosticDestroy(diagnostic);
|
|
|
|
spvContextDestroy(spvContext);
|
|
|
|
}
|
2016-11-10 17:11:50 +00:00
|
|
|
std::string optimized_asm;
|
2019-05-07 18:52:22 +00:00
|
|
|
SpirvTools tools(env_);
|
2017-11-27 15:16:41 +00:00
|
|
|
EXPECT_TRUE(
|
2019-05-07 18:52:22 +00:00
|
|
|
tools.Disassemble(optimized_bin, &optimized_asm, disassemble_options_))
|
2016-11-10 17:11:50 +00:00
|
|
|
<< "Disassembling failed for shader:\n"
|
|
|
|
<< assembly << std::endl;
|
|
|
|
return std::make_tuple(optimized_asm, status);
|
2016-07-26 16:11:03 +00:00
|
|
|
}
|
|
|
|
|
2016-06-28 18:58:17 +00:00
|
|
|
// Runs a single pass of class |PassT| on the binary assembled from the
|
|
|
|
// |original| assembly, and checks whether the optimized binary can be
|
2016-11-10 17:11:50 +00:00
|
|
|
// disassembled to the |expected| assembly. Optionally will also validate
|
2017-11-14 19:11:50 +00:00
|
|
|
// the optimized binary. This does *not* involve pass manager. Callers
|
2016-11-10 17:11:50 +00:00
|
|
|
// are suggested to use SCOPED_TRACE() for better messages.
|
2016-08-31 16:44:49 +00:00
|
|
|
template <typename PassT, typename... Args>
|
2016-06-28 18:58:17 +00:00
|
|
|
void SinglePassRunAndCheck(const std::string& original,
|
2016-08-31 16:44:49 +00:00
|
|
|
const std::string& expected, bool skip_nop,
|
2016-11-10 17:11:50 +00:00
|
|
|
bool do_validation, Args&&... args) {
|
|
|
|
std::vector<uint32_t> optimized_bin;
|
2018-07-11 13:24:49 +00:00
|
|
|
auto status = Pass::Status::SuccessWithoutChange;
|
2016-11-10 17:11:50 +00:00
|
|
|
std::tie(optimized_bin, status) = SinglePassRunToBinary<PassT>(
|
2016-08-31 16:44:49 +00:00
|
|
|
original, skip_nop, std::forward<Args>(args)...);
|
2016-06-28 18:58:17 +00:00
|
|
|
// Check whether the pass returns the correct modification indication.
|
2018-07-11 13:24:49 +00:00
|
|
|
EXPECT_NE(Pass::Status::Failure, status);
|
2016-09-12 16:39:44 +00:00
|
|
|
EXPECT_EQ(original == expected,
|
2018-07-11 13:24:49 +00:00
|
|
|
status == Pass::Status::SuccessWithoutChange);
|
2016-11-10 17:11:50 +00:00
|
|
|
if (do_validation) {
|
2019-05-07 18:52:22 +00:00
|
|
|
spv_context spvContext = spvContextCreate(env_);
|
2016-11-10 17:11:50 +00:00
|
|
|
spv_diagnostic diagnostic = nullptr;
|
2017-11-27 15:16:41 +00:00
|
|
|
spv_const_binary_t binary = {optimized_bin.data(), optimized_bin.size()};
|
2018-11-27 21:47:10 +00:00
|
|
|
spv_result_t error = spvValidateWithOptions(
|
|
|
|
spvContext, ValidatorOptions(), &binary, &diagnostic);
|
2016-11-10 17:11:50 +00:00
|
|
|
EXPECT_EQ(error, 0);
|
2017-11-27 15:16:41 +00:00
|
|
|
if (error != 0) spvDiagnosticPrint(diagnostic);
|
2016-11-10 17:11:50 +00:00
|
|
|
spvDiagnosticDestroy(diagnostic);
|
2017-11-14 19:11:50 +00:00
|
|
|
spvContextDestroy(spvContext);
|
2016-11-10 17:11:50 +00:00
|
|
|
}
|
|
|
|
std::string optimized_asm;
|
2019-05-07 18:52:22 +00:00
|
|
|
SpirvTools tools(env_);
|
2017-11-27 15:16:41 +00:00
|
|
|
EXPECT_TRUE(
|
2019-05-07 18:52:22 +00:00
|
|
|
tools.Disassemble(optimized_bin, &optimized_asm, disassemble_options_))
|
2016-11-10 17:11:50 +00:00
|
|
|
<< "Disassembling failed for shader:\n"
|
|
|
|
<< original << std::endl;
|
|
|
|
EXPECT_EQ(expected, optimized_asm);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Runs a single pass of class |PassT| on the binary assembled from the
|
|
|
|
// |original| assembly, and checks whether the optimized binary can be
|
|
|
|
// disassembled to the |expected| assembly. This does *not* involve pass
|
|
|
|
// manager. Callers are suggested to use SCOPED_TRACE() for better messages.
|
|
|
|
template <typename PassT, typename... Args>
|
|
|
|
void SinglePassRunAndCheck(const std::string& original,
|
|
|
|
const std::string& expected, bool skip_nop,
|
|
|
|
Args&&... args) {
|
|
|
|
SinglePassRunAndCheck<PassT>(original, expected, skip_nop, false,
|
2017-11-27 15:16:41 +00:00
|
|
|
std::forward<Args>(args)...);
|
2016-06-28 18:58:17 +00:00
|
|
|
}
|
|
|
|
|
2017-11-08 05:23:42 +00:00
|
|
|
// Runs a single pass of class |PassT| on the binary assembled from the
|
|
|
|
// |original| assembly, then runs an Effcee matcher over the disassembled
|
|
|
|
// result, using checks parsed from |original|. Always skips OpNop.
|
|
|
|
// This does *not* involve pass manager. Callers are suggested to use
|
|
|
|
// SCOPED_TRACE() for better messages.
|
|
|
|
template <typename PassT, typename... Args>
|
2017-11-30 22:03:06 +00:00
|
|
|
void SinglePassRunAndMatch(const std::string& original, bool do_validation,
|
|
|
|
Args&&... args) {
|
2017-11-08 05:23:42 +00:00
|
|
|
const bool skip_nop = true;
|
|
|
|
auto pass_result = SinglePassRunAndDisassemble<PassT>(
|
2017-11-30 22:03:06 +00:00
|
|
|
original, skip_nop, do_validation, std::forward<Args>(args)...);
|
2017-11-08 05:23:42 +00:00
|
|
|
auto disassembly = std::get<0>(pass_result);
|
|
|
|
auto match_result = effcee::Match(disassembly, original);
|
|
|
|
EXPECT_EQ(effcee::Result::Status::Ok, match_result.status())
|
|
|
|
<< match_result.message() << "\nChecking result:\n"
|
|
|
|
<< disassembly;
|
|
|
|
}
|
|
|
|
|
2019-07-30 14:38:46 +00:00
|
|
|
// Runs a single pass of class |PassT| on the binary assembled from the
|
|
|
|
// |original| assembly. Check for failure and expect an Effcee matcher
|
|
|
|
// to pass when run on the diagnostic messages. This does *not* involve
|
|
|
|
// pass manager. Callers are suggested to use SCOPED_TRACE() for better
|
|
|
|
// messages.
|
|
|
|
template <typename PassT, typename... Args>
|
|
|
|
void SinglePassRunAndFail(const std::string& original, Args&&... args) {
|
|
|
|
context_ =
|
|
|
|
std::move(BuildModule(env_, consumer_, original, assemble_options_));
|
|
|
|
EXPECT_NE(nullptr, context()) << "Assembling failed for shader:\n"
|
|
|
|
<< original << std::endl;
|
|
|
|
std::ostringstream errs;
|
|
|
|
auto error_consumer = [&errs](spv_message_level_t, const char*,
|
|
|
|
const spv_position_t&, const char* message) {
|
|
|
|
errs << message << std::endl;
|
|
|
|
};
|
|
|
|
auto pass = MakeUnique<PassT>(std::forward<Args>(args)...);
|
|
|
|
pass->SetMessageConsumer(error_consumer);
|
|
|
|
const auto status = pass->Run(context());
|
|
|
|
EXPECT_EQ(Pass::Status::Failure, status);
|
|
|
|
auto match_result = effcee::Match(errs.str(), original);
|
|
|
|
EXPECT_EQ(effcee::Result::Status::Ok, match_result.status())
|
|
|
|
<< match_result.message() << "\nChecking messages:\n"
|
|
|
|
<< errs.str();
|
|
|
|
}
|
|
|
|
|
2016-06-28 18:58:17 +00:00
|
|
|
// Adds a pass to be run.
|
2016-08-31 16:44:49 +00:00
|
|
|
template <typename PassT, typename... Args>
|
|
|
|
void AddPass(Args&&... args) {
|
|
|
|
manager_->AddPass<PassT>(std::forward<Args>(args)...);
|
2016-06-28 18:58:17 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Renews the pass manager, including clearing all previously added passes.
|
2016-09-20 20:48:00 +00:00
|
|
|
void RenewPassManger() {
|
2018-08-14 19:01:50 +00:00
|
|
|
manager_ = MakeUnique<PassManager>();
|
2016-09-20 20:48:00 +00:00
|
|
|
manager_->SetMessageConsumer(consumer_);
|
|
|
|
}
|
2016-06-28 18:58:17 +00:00
|
|
|
|
|
|
|
// Runs the passes added thus far using a pass manager on the binary assembled
|
|
|
|
// from the |original| assembly, and checks whether the optimized binary can
|
|
|
|
// be disassembled to the |expected| assembly. Callers are suggested to use
|
|
|
|
// SCOPED_TRACE() for better messages.
|
|
|
|
void RunAndCheck(const std::string& original, const std::string& expected) {
|
|
|
|
assert(manager_->NumPasses());
|
|
|
|
|
2019-05-07 18:52:22 +00:00
|
|
|
context_ =
|
|
|
|
std::move(BuildModule(env_, nullptr, original, assemble_options_));
|
2017-11-14 19:11:50 +00:00
|
|
|
ASSERT_NE(nullptr, context());
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2019-07-10 18:12:19 +00:00
|
|
|
context()->set_preserve_bindings(OptimizerOptions()->preserve_bindings_);
|
|
|
|
context()->set_preserve_spec_constants(
|
|
|
|
OptimizerOptions()->preserve_spec_constants_);
|
|
|
|
|
2019-08-16 17:15:17 +00:00
|
|
|
auto status = manager_->Run(context());
|
|
|
|
EXPECT_NE(status, Pass::Status::Failure);
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2019-08-16 17:15:17 +00:00
|
|
|
if (status != Pass::Status::Failure) {
|
|
|
|
std::vector<uint32_t> binary;
|
|
|
|
context()->module()->ToBinary(&binary, /* skip_nop = */ false);
|
2016-06-28 18:58:17 +00:00
|
|
|
|
2019-08-16 17:15:17 +00:00
|
|
|
std::string optimized;
|
|
|
|
SpirvTools tools(env_);
|
|
|
|
EXPECT_TRUE(tools.Disassemble(binary, &optimized, disassemble_options_));
|
|
|
|
EXPECT_EQ(expected, optimized);
|
|
|
|
}
|
2016-06-28 18:58:17 +00:00
|
|
|
}
|
|
|
|
|
2017-04-11 19:11:04 +00:00
|
|
|
void SetAssembleOptions(uint32_t assemble_options) {
|
|
|
|
assemble_options_ = assemble_options;
|
|
|
|
}
|
|
|
|
|
|
|
|
void SetDisassembleOptions(uint32_t disassemble_options) {
|
|
|
|
disassemble_options_ = disassemble_options;
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
MessageConsumer consumer() { return consumer_; }
|
2018-07-11 13:24:49 +00:00
|
|
|
IRContext* context() { return context_.get(); }
|
2017-11-27 15:16:41 +00:00
|
|
|
|
2018-01-30 16:24:03 +00:00
|
|
|
void SetMessageConsumer(MessageConsumer msg_consumer) {
|
|
|
|
consumer_ = msg_consumer;
|
|
|
|
}
|
|
|
|
|
2019-07-10 18:12:19 +00:00
|
|
|
spv_optimizer_options OptimizerOptions() { return &optimizer_options_; }
|
|
|
|
|
2018-11-27 21:47:10 +00:00
|
|
|
spv_validator_options ValidatorOptions() { return &validator_options_; }
|
|
|
|
|
2019-05-07 18:52:22 +00:00
|
|
|
void SetTargetEnv(spv_target_env env) { env_ = env; }
|
|
|
|
|
2016-06-28 18:58:17 +00:00
|
|
|
private:
|
2019-05-07 18:52:22 +00:00
|
|
|
MessageConsumer consumer_; // Message consumer.
|
|
|
|
std::unique_ptr<IRContext> context_; // IR context
|
2018-07-11 13:24:49 +00:00
|
|
|
std::unique_ptr<PassManager> manager_; // The pass manager.
|
2017-04-11 19:11:04 +00:00
|
|
|
uint32_t assemble_options_;
|
|
|
|
uint32_t disassemble_options_;
|
2019-07-10 18:12:19 +00:00
|
|
|
spv_optimizer_options_t optimizer_options_;
|
2018-11-27 21:47:10 +00:00
|
|
|
spv_validator_options_t validator_options_;
|
2019-05-07 18:52:22 +00:00
|
|
|
spv_target_env env_;
|
2016-06-28 18:58:17 +00:00
|
|
|
};
|
|
|
|
|
2018-07-11 13:24:49 +00:00
|
|
|
} // namespace opt
|
2016-06-28 18:58:17 +00:00
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // TEST_OPT_PASS_FIXTURE_H_
|