2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-11-18 20:43:43 +00:00
|
|
|
//
|
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
|
2015-11-18 20:43:43 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-11-18 20:43:43 +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.
|
2015-11-18 20:43:43 +00:00
|
|
|
|
|
|
|
// Common validation fixtures for unit tests
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#ifndef TEST_VAL_VAL_FIXTURES_H_
|
|
|
|
#define TEST_VAL_VAL_FIXTURES_H_
|
2015-12-14 13:21:08 +00:00
|
|
|
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <memory>
|
|
|
|
#include <string>
|
|
|
|
|
2017-01-11 20:03:53 +00:00
|
|
|
#include "source/val/validation_state.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "test/test_fixture.h"
|
|
|
|
#include "test/unit_spirv.h"
|
2015-11-18 20:43:43 +00:00
|
|
|
|
|
|
|
namespace spvtest {
|
|
|
|
|
2016-02-22 20:25:50 +00:00
|
|
|
template <typename T>
|
2015-11-18 20:43:43 +00:00
|
|
|
class ValidateBase : public ::testing::Test,
|
|
|
|
public ::testing::WithParamInterface<T> {
|
|
|
|
public:
|
|
|
|
ValidateBase();
|
|
|
|
|
|
|
|
virtual void TearDown();
|
|
|
|
|
|
|
|
// Returns the a spv_const_binary struct
|
|
|
|
spv_const_binary get_const_binary();
|
|
|
|
|
2018-06-20 17:29:38 +00:00
|
|
|
// Checks that 'code' is valid SPIR-V text representation and stores the
|
|
|
|
// binary version for further method calls.
|
2016-04-21 19:46:08 +00:00
|
|
|
void CompileSuccessfully(std::string code,
|
|
|
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
|
2015-11-18 20:43:43 +00:00
|
|
|
|
2016-11-24 20:37:22 +00:00
|
|
|
// Overwrites the word at index 'index' with the given word.
|
|
|
|
// For testing purposes, it is often useful to be able to manipulate the
|
|
|
|
// assembled binary before running the validator on it.
|
|
|
|
// This function overwrites the word at the given index with a new word.
|
|
|
|
void OverwriteAssembledBinary(uint32_t index, uint32_t word);
|
|
|
|
|
2018-06-20 17:29:38 +00:00
|
|
|
// Performs validation on the SPIR-V code.
|
2016-04-21 19:46:08 +00:00
|
|
|
spv_result_t ValidateInstructions(spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
|
2015-11-18 20:43:43 +00:00
|
|
|
|
2017-01-11 20:03:53 +00:00
|
|
|
// Performs validation. Returns the status and stores validation state into
|
|
|
|
// the vstate_ member.
|
|
|
|
spv_result_t ValidateAndRetrieveValidationState(
|
|
|
|
spv_target_env env = SPV_ENV_UNIVERSAL_1_0);
|
|
|
|
|
2015-11-18 20:43:43 +00:00
|
|
|
std::string getDiagnosticString();
|
2015-12-16 02:44:21 +00:00
|
|
|
spv_position_t getErrorPosition();
|
2017-02-15 18:29:33 +00:00
|
|
|
spv_validator_options getValidatorOptions();
|
2015-11-18 20:43:43 +00:00
|
|
|
|
|
|
|
spv_binary binary_;
|
|
|
|
spv_diagnostic diagnostic_;
|
2017-02-15 18:29:33 +00:00
|
|
|
spv_validator_options options_;
|
2018-07-10 03:18:44 +00:00
|
|
|
std::unique_ptr<spvtools::val::ValidationState_t> vstate_;
|
2015-11-18 20:43:43 +00:00
|
|
|
};
|
2018-02-07 16:50:26 +00:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
ValidateBase<T>::ValidateBase() : binary_(), diagnostic_() {
|
|
|
|
// Initialize to default command line options. Different tests can then
|
|
|
|
// specialize specific options as necessary.
|
|
|
|
options_ = spvValidatorOptionsCreate();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
spv_const_binary ValidateBase<T>::get_const_binary() {
|
|
|
|
return spv_const_binary(binary_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ValidateBase<T>::TearDown() {
|
|
|
|
if (diagnostic_) {
|
|
|
|
spvDiagnosticPrint(diagnostic_);
|
|
|
|
}
|
|
|
|
spvDiagnosticDestroy(diagnostic_);
|
|
|
|
spvBinaryDestroy(binary_);
|
|
|
|
spvValidatorOptionsDestroy(options_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ValidateBase<T>::CompileSuccessfully(std::string code,
|
|
|
|
spv_target_env env) {
|
|
|
|
spv_diagnostic diagnostic = nullptr;
|
|
|
|
ASSERT_EQ(SPV_SUCCESS,
|
|
|
|
spvTextToBinary(ScopedContext(env).context, code.c_str(),
|
|
|
|
code.size(), &binary_, &diagnostic))
|
|
|
|
<< "ERROR: " << diagnostic->error
|
|
|
|
<< "\nSPIR-V could not be compiled into binary:\n"
|
|
|
|
<< code;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
void ValidateBase<T>::OverwriteAssembledBinary(uint32_t index, uint32_t word) {
|
|
|
|
ASSERT_TRUE(index < binary_->wordCount)
|
|
|
|
<< "OverwriteAssembledBinary: The given index is larger than the binary "
|
|
|
|
"word count.";
|
|
|
|
binary_->code[index] = word;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
spv_result_t ValidateBase<T>::ValidateInstructions(spv_target_env env) {
|
|
|
|
return spvValidateWithOptions(ScopedContext(env).context, options_,
|
|
|
|
get_const_binary(), &diagnostic_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
spv_result_t ValidateBase<T>::ValidateAndRetrieveValidationState(
|
|
|
|
spv_target_env env) {
|
2018-07-10 03:18:44 +00:00
|
|
|
return spvtools::val::ValidateBinaryAndKeepValidationState(
|
2018-02-07 16:50:26 +00:00
|
|
|
ScopedContext(env).context, options_, get_const_binary()->code,
|
|
|
|
get_const_binary()->wordCount, &diagnostic_, &vstate_);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
std::string ValidateBase<T>::getDiagnosticString() {
|
|
|
|
return diagnostic_ == nullptr ? std::string()
|
|
|
|
: std::string(diagnostic_->error);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
spv_validator_options ValidateBase<T>::getValidatorOptions() {
|
|
|
|
return options_;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
spv_position_t ValidateBase<T>::getErrorPosition() {
|
|
|
|
return diagnostic_ == nullptr ? spv_position_t() : diagnostic_->position;
|
|
|
|
}
|
|
|
|
|
2017-11-27 15:16:41 +00:00
|
|
|
} // namespace spvtest
|
2018-08-03 12:05:33 +00:00
|
|
|
|
|
|
|
#endif // TEST_VAL_VAL_FIXTURES_H_
|