[msl-writer] Support MSL compiler validation in unit tests
The MSL writer GTest harness (TestHelper) now provides a function to invoke the XCode SDK Metal compiler for the MSL output of a given tint::Program. The tint_unittests binary now provides the `--validate-msl` and `--xcrun-path` command-line flags to optionally enable MSL validation and to configure its path. The MSL validation logic itself is conditionally compiled based on the TINT_BUILD_MSL_WRITER define. The TINT_BUILD_* flags were previously not propagated to the GTest binary which this CL addresses by linking the common/public tint configs when building the tint_unittests_main target. Bug: tint:535 Fixed: tint:696 Change-Id: I08b1c36ba59c606ef6cffa5fa5454fd8cf8b035d Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/45800 Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: Ben Clayton <bclayton@google.com> Commit-Queue: Arman Uguray <armansito@chromium.org>
This commit is contained in:
parent
f8313e5a6e
commit
d13982ff2e
|
@ -774,6 +774,7 @@ if(${TINT_BUILD_TESTS})
|
||||||
writer/msl/generator_impl_type_test.cc
|
writer/msl/generator_impl_type_test.cc
|
||||||
writer/msl/generator_impl_unary_op_test.cc
|
writer/msl/generator_impl_unary_op_test.cc
|
||||||
writer/msl/generator_impl_variable_decl_statement_test.cc
|
writer/msl/generator_impl_variable_decl_statement_test.cc
|
||||||
|
writer/msl/test_helper.cc
|
||||||
writer/msl/test_helper.h
|
writer/msl/test_helper.h
|
||||||
)
|
)
|
||||||
endif()
|
endif()
|
||||||
|
|
|
@ -15,6 +15,7 @@
|
||||||
#include "gmock/gmock.h"
|
#include "gmock/gmock.h"
|
||||||
#include "src/utils/command.h"
|
#include "src/utils/command.h"
|
||||||
#include "src/writer/hlsl/test_helper.h"
|
#include "src/writer/hlsl/test_helper.h"
|
||||||
|
#include "src/writer/msl/test_helper.h"
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
|
@ -25,6 +26,8 @@ void TintInternalCompilerErrorReporter(const tint::diag::List& diagnostics) {
|
||||||
struct Flags {
|
struct Flags {
|
||||||
bool validate_hlsl = false;
|
bool validate_hlsl = false;
|
||||||
std::string dxc_path;
|
std::string dxc_path;
|
||||||
|
bool validate_msl = false;
|
||||||
|
std::string xcrun_path;
|
||||||
|
|
||||||
bool parse(int argc, char** argv) {
|
bool parse(int argc, char** argv) {
|
||||||
bool errored = false;
|
bool errored = false;
|
||||||
|
@ -47,6 +50,9 @@ struct Flags {
|
||||||
|
|
||||||
if (match("--validate-hlsl") || parse_value("--dxc-path", dxc_path)) {
|
if (match("--validate-hlsl") || parse_value("--dxc-path", dxc_path)) {
|
||||||
validate_hlsl = true;
|
validate_hlsl = true;
|
||||||
|
} else if (match("--validate-msl") ||
|
||||||
|
parse_value("--xcrun-path", xcrun_path)) {
|
||||||
|
validate_msl = true;
|
||||||
} else {
|
} else {
|
||||||
std::cout << "Unknown flag '" << argv[i] << "'" << std::endl;
|
std::cout << "Unknown flag '" << argv[i] << "'" << std::endl;
|
||||||
return false;
|
return false;
|
||||||
|
@ -91,6 +97,31 @@ int main(int argc, char** argv) {
|
||||||
}
|
}
|
||||||
#endif // TINT_BUILD_HLSL_WRITER
|
#endif // TINT_BUILD_HLSL_WRITER
|
||||||
|
|
||||||
|
#if TINT_BUILD_MSL_WRITER
|
||||||
|
// This must be kept alive for the duration of RUN_ALL_TESTS() as the c_str()
|
||||||
|
// is passed into tint::writer::msl::EnableMSLValidation(), which does not
|
||||||
|
// make a copy. This is to work around Chromium's strict rules on globals
|
||||||
|
// having no constructors / destructors.
|
||||||
|
std::string xcrun_path;
|
||||||
|
if (flags.validate_msl) {
|
||||||
|
auto xcrun = flags.xcrun_path.empty()
|
||||||
|
? tint::utils::Command::LookPath("xcrun")
|
||||||
|
: tint::utils::Command(flags.xcrun_path);
|
||||||
|
|
||||||
|
if (!xcrun.Found()) {
|
||||||
|
std::cout << "xcrun executable not found" << std::endl;
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "MSL validation with XCode SDK enabled" << std::endl;
|
||||||
|
|
||||||
|
xcrun_path = xcrun.Path();
|
||||||
|
tint::writer::msl::EnableMSLValidation(xcrun_path.c_str());
|
||||||
|
} else {
|
||||||
|
std::cout << "MSL validation with XCode SDK is not enabled" << std::endl;
|
||||||
|
}
|
||||||
|
#endif // TINT_BUILD_MSL_WRITER
|
||||||
|
|
||||||
tint::SetInternalCompilerErrorReporter(&TintInternalCompilerErrorReporter);
|
tint::SetInternalCompilerErrorReporter(&TintInternalCompilerErrorReporter);
|
||||||
|
|
||||||
auto res = RUN_ALL_TESTS();
|
auto res = RUN_ALL_TESTS();
|
||||||
|
|
|
@ -0,0 +1,82 @@
|
||||||
|
// Copyright 2021 The Tint Authors.
|
||||||
|
//
|
||||||
|
// 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.
|
||||||
|
|
||||||
|
#include "src/writer/msl/test_helper.h"
|
||||||
|
|
||||||
|
#include "src/utils/command.h"
|
||||||
|
#include "src/utils/tmpfile.h"
|
||||||
|
|
||||||
|
namespace tint {
|
||||||
|
namespace writer {
|
||||||
|
namespace msl {
|
||||||
|
|
||||||
|
namespace {
|
||||||
|
|
||||||
|
const char* xcrun_path = nullptr;
|
||||||
|
|
||||||
|
} // namespace
|
||||||
|
|
||||||
|
void EnableMSLValidation(const char* xcrun) {
|
||||||
|
xcrun_path = xcrun;
|
||||||
|
}
|
||||||
|
|
||||||
|
CompileResult Compile(Program* program) {
|
||||||
|
CompileResult result;
|
||||||
|
|
||||||
|
if (!xcrun_path) {
|
||||||
|
result.status = CompileResult::Status::kVerificationNotEnabled;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto xcrun = utils::Command(xcrun_path);
|
||||||
|
if (!xcrun.Found()) {
|
||||||
|
result.output = "xcrun not found at '" + std::string(xcrun_path) + "'";
|
||||||
|
result.status = CompileResult::Status::kFailed;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
auto gen = std::make_unique<GeneratorImpl>(program);
|
||||||
|
if (!gen->Generate()) {
|
||||||
|
result.output = gen->error();
|
||||||
|
result.status = CompileResult::Status::kFailed;
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
result.msl = gen->result();
|
||||||
|
|
||||||
|
utils::TmpFile file(".metal");
|
||||||
|
file << result.msl;
|
||||||
|
|
||||||
|
auto xcrun_res =
|
||||||
|
xcrun("-sdk", "macosx", "metal", "-o", "/dev/null", "-c", file.Path());
|
||||||
|
if (!xcrun_res.out.empty()) {
|
||||||
|
if (!result.output.empty()) {
|
||||||
|
result.output += "\n";
|
||||||
|
}
|
||||||
|
result.output += xcrun_res.out;
|
||||||
|
}
|
||||||
|
if (!xcrun_res.err.empty()) {
|
||||||
|
if (!result.output.empty()) {
|
||||||
|
result.output += "\n";
|
||||||
|
}
|
||||||
|
result.output += xcrun_res.err;
|
||||||
|
}
|
||||||
|
result.status = (xcrun_res.error_code == 0) ? CompileResult::Status::kSuccess
|
||||||
|
: CompileResult::Status::kFailed;
|
||||||
|
|
||||||
|
return result;
|
||||||
|
}
|
||||||
|
|
||||||
|
} // namespace msl
|
||||||
|
} // namespace writer
|
||||||
|
} // namespace tint
|
|
@ -16,6 +16,7 @@
|
||||||
#define SRC_WRITER_MSL_TEST_HELPER_H_
|
#define SRC_WRITER_MSL_TEST_HELPER_H_
|
||||||
|
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <string>
|
||||||
#include <utility>
|
#include <utility>
|
||||||
|
|
||||||
#include "gtest/gtest.h"
|
#include "gtest/gtest.h"
|
||||||
|
@ -27,6 +28,28 @@ namespace tint {
|
||||||
namespace writer {
|
namespace writer {
|
||||||
namespace msl {
|
namespace msl {
|
||||||
|
|
||||||
|
/// Enables verification of MSL shaders by running the Metal compiler and
|
||||||
|
/// checking no errors are reported.
|
||||||
|
/// @param xcrun_path the path to the `xcrun` executable
|
||||||
|
void EnableMSLValidation(const char* xcrun_path);
|
||||||
|
|
||||||
|
/// The return structure of Compile()
|
||||||
|
struct CompileResult {
|
||||||
|
/// Status is an enumerator of status codes from Compile()
|
||||||
|
enum class Status { kSuccess, kFailed, kVerificationNotEnabled };
|
||||||
|
/// The resulting status of the compilation
|
||||||
|
Status status;
|
||||||
|
/// Output of the Metal compiler
|
||||||
|
std::string output;
|
||||||
|
/// The MSL source that was compiled
|
||||||
|
std::string msl;
|
||||||
|
};
|
||||||
|
|
||||||
|
/// Compile attempts to compile the shader with xcrun if found on PATH.
|
||||||
|
/// @param program the MSL program
|
||||||
|
/// @return the result of the compile
|
||||||
|
CompileResult Compile(Program* program);
|
||||||
|
|
||||||
/// Helper class for testing
|
/// Helper class for testing
|
||||||
template <typename BASE>
|
template <typename BASE>
|
||||||
class TestHelperBase : public BASE, public ProgramBuilder {
|
class TestHelperBase : public BASE, public ProgramBuilder {
|
||||||
|
@ -84,6 +107,20 @@ class TestHelperBase : public BASE, public ProgramBuilder {
|
||||||
return *gen_;
|
return *gen_;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Validate generates MSL code for the current contents of `program` and
|
||||||
|
/// passes the output of the generator to the XCode SDK Metal compiler.
|
||||||
|
///
|
||||||
|
/// If the Metal compiler finds problems, then any GTest test case that
|
||||||
|
/// invokes this function test will fail.
|
||||||
|
/// This function does nothing, if the Metal compiler path has not been
|
||||||
|
/// configured by calling `EnableMSLValidation()`.
|
||||||
|
void Validate() {
|
||||||
|
auto res = Compile(program.get());
|
||||||
|
if (res.status == CompileResult::Status::kFailed) {
|
||||||
|
FAIL() << "MSL Validation failed.\n\n" << res.msl << "\n\n" << res.output;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
/// The program built with a call to Build()
|
/// The program built with a call to Build()
|
||||||
std::unique_ptr<Program> program;
|
std::unique_ptr<Program> program;
|
||||||
|
|
||||||
|
|
|
@ -88,6 +88,7 @@ source_set("tint_unittests_main") {
|
||||||
sources = [ "//gpu/tint_unittests_main.cc" ]
|
sources = [ "//gpu/tint_unittests_main.cc" ]
|
||||||
} else {
|
} else {
|
||||||
sources = [ "../src/test_main.cc" ]
|
sources = [ "../src/test_main.cc" ]
|
||||||
|
deps += [ "${tint_root_dir}/src:libtint" ]
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -538,6 +539,7 @@ source_set("tint_unittests_msl_writer_src") {
|
||||||
"../src/writer/msl/generator_impl_type_test.cc",
|
"../src/writer/msl/generator_impl_type_test.cc",
|
||||||
"../src/writer/msl/generator_impl_unary_op_test.cc",
|
"../src/writer/msl/generator_impl_unary_op_test.cc",
|
||||||
"../src/writer/msl/generator_impl_variable_decl_statement_test.cc",
|
"../src/writer/msl/generator_impl_variable_decl_statement_test.cc",
|
||||||
|
"../src/writer/msl/test_helper.cc",
|
||||||
"../src/writer/msl/test_helper.h",
|
"../src/writer/msl/test_helper.h",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue