From 8fa6c25b7868d2445413e20b3deaf117a02446ad Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 3 Nov 2022 19:16:26 +0000 Subject: [PATCH] tint: Add Initialize() / Shutdown() public APIs Have Initialize() bind the Program printer - which is helpful for debugging. Call these from dawn/node. This allows dawn/node to print programs when things go wrong. Change-Id: I32d8805381d2939e82dc6ea383b9860fbb5fb69e Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/107684 Kokoro: Kokoro Commit-Queue: Ben Clayton Reviewed-by: Corentin Wallez --- include/tint/tint.h | 15 +++++++++++++++ src/dawn/node/Module.cpp | 6 +++++- src/tint/BUILD.gn | 1 + src/tint/CMakeLists.txt | 1 + src/tint/program_builder.h | 2 +- src/tint/test_main.cc | 17 ++++------------- src/tint/tint.cc | 36 ++++++++++++++++++++++++++++++++++++ 7 files changed, 63 insertions(+), 15 deletions(-) create mode 100644 src/tint/tint.cc diff --git a/include/tint/tint.h b/include/tint/tint.h index c7f8a8c598..57a3c67ead 100644 --- a/include/tint/tint.h +++ b/include/tint/tint.h @@ -15,6 +15,9 @@ #ifndef INCLUDE_TINT_TINT_H_ #define INCLUDE_TINT_TINT_H_ +// Guard for accidental includes to private headers +#define CURRENTLY_IN_TINT_PUBLIC_HEADER + // TODO(tint:88): When implementing support for an install target, all of these // headers will need to be moved to include/tint/. @@ -64,4 +67,16 @@ #include "src/tint/writer/glsl/generator.h" #endif // TINT_BUILD_GLSL_WRITER +namespace tint { + +/// Initialize initializes the Tint library. Call before using the Tint API. +void Initialize(); + +/// Shutdown uninitializes the Tint library. Call after using the Tint API. +void Shutdown(); + +} // namespace tint + +#undef CURRENTLY_IN_TINT_PUBLIC_HEADER + #endif // INCLUDE_TINT_TINT_H_ diff --git a/src/dawn/node/Module.cpp b/src/dawn/node/Module.cpp index ba2ad04cb3..d8acf00b2c 100644 --- a/src/dawn/node/Module.cpp +++ b/src/dawn/node/Module.cpp @@ -20,6 +20,7 @@ #include "dawn/dawn_proc.h" #include "src/dawn/node/binding/Flags.h" #include "src/dawn/node/binding/GPU.h" +#include "tint/tint.h" namespace { Napi::Value CreateGPU(const Napi::CallbackInfo& info) { @@ -55,7 +56,10 @@ Napi::Value CreateGPU(const Napi::CallbackInfo& info) { // types into the global object, and adding the 'create' function on the exported // object. Napi::Object Initialize(Napi::Env env, Napi::Object exports) { - // Begin by setting the Dawn procedure function pointers. + // Initialize Tint + tint::Initialize(); + + // Set all the Dawn procedure function pointers. dawnProcSetProcs(&dawn::native::GetProcs()); // Register all the interop types diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index 97bffef50e..337b6a1ff3 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn @@ -474,6 +474,7 @@ libtint_source_set("libtint_core_all_src") { "symbol_table.h", "text/unicode.cc", "text/unicode.h", + "tint.cc", "traits.h", "transform/add_block_attribute.cc", "transform/add_block_attribute.h", diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt index 8b5fb6b0c6..3ba43452e6 100644 --- a/src/tint/CMakeLists.txt +++ b/src/tint/CMakeLists.txt @@ -385,6 +385,7 @@ set(TINT_LIB_SRCS symbol_table.h symbol.cc symbol.h + tint.cc text/unicode.cc text/unicode.h traits.h diff --git a/src/tint/program_builder.h b/src/tint/program_builder.h index 25e6214fff..2f5b5e0200 100644 --- a/src/tint/program_builder.h +++ b/src/tint/program_builder.h @@ -109,7 +109,7 @@ #include "src/tint/sem/vector.h" #include "src/tint/sem/void.h" -#ifdef INCLUDE_TINT_TINT_H_ +#ifdef CURRENTLY_IN_TINT_PUBLIC_HEADER #error "internal tint header being #included from tint.h" #endif diff --git a/src/tint/test_main.cc b/src/tint/test_main.cc index ca68271587..918bd20d8a 100644 --- a/src/tint/test_main.cc +++ b/src/tint/test_main.cc @@ -14,15 +14,12 @@ #include "gmock/gmock.h" #include "src/tint/program.h" +#include "tint/tint.h" #if TINT_BUILD_SPV_READER #include "src/tint/reader/spirv/parser_impl_test_helper.h" #endif -#if TINT_BUILD_WGSL_WRITER -#include "src/tint/writer/wgsl/generator.h" -#endif - namespace { void TintInternalCompilerErrorReporter(const tint::diag::List& diagnostics) { @@ -54,15 +51,7 @@ struct Flags { int main(int argc, char** argv) { testing::InitGoogleMock(&argc, argv); -#if TINT_BUILD_WGSL_WRITER - tint::Program::printer = [](const tint::Program* program) { - auto result = tint::writer::wgsl::Generate(program, {}); - if (!result.error.empty()) { - return "error: " + result.error; - } - return result.wgsl; - }; -#endif // TINT_BUILD_WGSL_WRITER + tint::Initialize(); Flags flags; if (!flags.parse(argc, argv)) { @@ -79,5 +68,7 @@ int main(int argc, char** argv) { auto res = RUN_ALL_TESTS(); + tint::Shutdown(); + return res; } diff --git a/src/tint/tint.cc b/src/tint/tint.cc new file mode 100644 index 0000000000..cea69cb007 --- /dev/null +++ b/src/tint/tint.cc @@ -0,0 +1,36 @@ +// Copyright 2022 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 "tint/tint.h" + +namespace tint { + +void Initialize() { +#if TINT_BUILD_WGSL_WRITER + // Register the Program printer. This is used for debugging purposes. + tint::Program::printer = [](const tint::Program* program) { + auto result = writer::wgsl::Generate(program, {}); + if (!result.error.empty()) { + return "error: " + result.error; + } + return result.wgsl; + }; +#endif +} + +void Shutdown() { + // Currently no-op, but may release tint resources in the future. +} + +} // namespace tint