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 <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
Reviewed-by: Corentin Wallez <cwallez@chromium.org>
This commit is contained in:
Ben Clayton 2022-11-03 19:16:26 +00:00 committed by Dawn LUCI CQ
parent 02f04d914d
commit 8fa6c25b78
7 changed files with 63 additions and 15 deletions

View File

@ -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_

View File

@ -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

View File

@ -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",

View File

@ -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

View File

@ -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

View File

@ -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;
}

36
src/tint/tint.cc Normal file
View File

@ -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