[ir] Split AST and SEM sources out of core ir.

This CL moves the need to pull in SEM and AST into a specific
`libtint_ir_builder_src` library. This will make it a GN error if we
accidentally try to use the SEM or AST inside the IR after the initial
construction.

This required move the `ToProgram`/`FromProgram` methods out of
`ir::Module` and into an `ir::Converter` class.

Bug: tint:1921
Change-Id: I2e6ae195f9a100030b43f35a2c5dad634433147f
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/129661
Reviewed-by: Ben Clayton <bclayton@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
dan sinclair 2023-04-27 11:56:58 +00:00 committed by Dawn LUCI CQ
parent 664e1e0702
commit 5e344a338f
9 changed files with 132 additions and 60 deletions

View File

@ -1104,6 +1104,24 @@ libtint_source_set("libtint_syntax_tree_writer_src") {
] ]
} }
libtint_source_set("libtint_ir_builder_src") {
sources = [
"ir/builder_impl.cc",
"ir/builder_impl.h",
"ir/converter.cc",
"ir/converter.h",
]
deps = [
":libtint_ast_src",
":libtint_constant_src",
":libtint_ir_src",
":libtint_program_src",
":libtint_sem_src",
":libtint_type_src",
":libtint_utils_src",
]
}
libtint_source_set("libtint_ir_src") { libtint_source_set("libtint_ir_src") {
sources = [ sources = [
"ir/binary.cc", "ir/binary.cc",
@ -1114,8 +1132,6 @@ libtint_source_set("libtint_ir_src") {
"ir/block.h", "ir/block.h",
"ir/builder.cc", "ir/builder.cc",
"ir/builder.h", "ir/builder.h",
"ir/builder_impl.cc",
"ir/builder_impl.h",
"ir/builtin.cc", "ir/builtin.cc",
"ir/builtin.h", "ir/builtin.h",
"ir/call.cc", "ir/call.cc",
@ -1161,11 +1177,8 @@ libtint_source_set("libtint_ir_src") {
] ]
deps = [ deps = [
":libtint_ast_src",
":libtint_builtins_src", ":libtint_builtins_src",
":libtint_constant_src", ":libtint_constant_src",
":libtint_program_src",
":libtint_sem_src",
":libtint_symbols_src", ":libtint_symbols_src",
":libtint_type_src", ":libtint_type_src",
":libtint_utils_src", ":libtint_utils_src",
@ -1222,7 +1235,10 @@ source_set("libtint") {
if (tint_build_ir) { if (tint_build_ir) {
assert(!build_with_chromium, assert(!build_with_chromium,
"tint_build_ir cannot be used when building Chromium") "tint_build_ir cannot be used when building Chromium")
public_deps += [ ":libtint_ir_src" ] public_deps += [
":libtint_ir_builder_src",
":libtint_ir_src",
]
} }
configs += [ ":tint_common_config" ] configs += [ ":tint_common_config" ]
@ -2112,7 +2128,10 @@ if (tint_build_unittests) {
"ir/unary_test.cc", "ir/unary_test.cc",
] ]
deps = [ ":libtint_ir_src" ] deps = [
":libtint_ir_builder_src",
":libtint_ir_src",
]
} }
if (build_with_chromium) { if (build_with_chromium) {

View File

@ -718,6 +718,8 @@ if(${TINT_BUILD_IR})
ir/construct.h ir/construct.h
ir/convert.cc ir/convert.cc
ir/convert.h ir/convert.h
ir/converter.cc
ir/converter.h
ir/debug.cc ir/debug.cc
ir/debug.h ir/debug.h
ir/disassembler.cc ir/disassembler.cc

View File

@ -19,6 +19,7 @@
#include "tint/tint.h" #include "tint/tint.h"
#if TINT_BUILD_IR #if TINT_BUILD_IR
#include "src/tint/ir/converter.h"
#include "src/tint/ir/module.h" #include "src/tint/ir/module.h"
#endif // TINT_BUILD_IR #endif // TINT_BUILD_IR
@ -377,7 +378,7 @@ int main(int argc, const char** argv) {
loop_count = options.loop_count; loop_count = options.loop_count;
} }
for (uint32_t i = 0; i < loop_count; ++i) { for (uint32_t i = 0; i < loop_count; ++i) {
auto result = tint::ir::Module::FromProgram(program.get()); auto result = tint::ir::Converter::FromProgram(program.get());
if (!result) { if (!result) {
std::cerr << "Failed to build IR from program: " << result.Failure() << std::endl; std::cerr << "Failed to build IR from program: " << result.Failure() << std::endl;
} }

View File

@ -49,9 +49,10 @@
#include "tint/tint.h" #include "tint/tint.h"
#if TINT_BUILD_IR #if TINT_BUILD_IR
#include "src/tint/ir/debug.h" #include "src/tint/ir/converter.h" // nogncheck
#include "src/tint/ir/disassembler.h" #include "src/tint/ir/debug.h" // nogncheck
#include "src/tint/ir/module.h" #include "src/tint/ir/disassembler.h" // nogncheck
#include "src/tint/ir/module.h" // nogncheck
#endif // TINT_BUILD_IR #endif // TINT_BUILD_IR
namespace { namespace {
@ -1078,7 +1079,7 @@ int main(int argc, const char** argv) {
#if TINT_BUILD_IR #if TINT_BUILD_IR
if (options.dump_ir || options.dump_ir_graph) { if (options.dump_ir || options.dump_ir_graph) {
auto result = tint::ir::Module::FromProgram(program.get()); auto result = tint::ir::Converter::FromProgram(program.get());
if (!result) { if (!result) {
std::cerr << "Failed to build IR from program: " << result.Failure() << std::endl; std::cerr << "Failed to build IR from program: " << result.Failure() << std::endl;
} else { } else {

View File

@ -16,8 +16,6 @@
#include <utility> #include <utility>
#include "src/tint/ir/builder_impl.h"
namespace tint::ir { namespace tint::ir {
Builder::Builder() {} Builder::Builder() {}

42
src/tint/ir/converter.cc Normal file
View File

@ -0,0 +1,42 @@
// Copyright 2023 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/tint/ir/converter.h"
#include "src/tint/ir/builder_impl.h"
#include "src/tint/program.h"
namespace tint::ir {
// static
Converter::Result Converter::FromProgram(const Program* program) {
if (!program->IsValid()) {
return Result{std::string("input program is not valid")};
}
BuilderImpl b(program);
auto r = b.Build();
if (!r) {
return b.Diagnostics().str();
}
return Result{r.Move()};
}
// static
const Program* Converter::ToProgram() {
return nullptr;
}
} // namespace tint::ir

54
src/tint/ir/converter.h Normal file
View File

@ -0,0 +1,54 @@
// Copyright 2023 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.
#ifndef SRC_TINT_IR_CONVERTER_H_
#define SRC_TINT_IR_CONVERTER_H_
#include <string>
#include "src/tint/ir/module.h"
#include "src/tint/utils/result.h"
// Forward Declarations
namespace tint {
class Program;
} // namespace tint
namespace tint::ir {
/// Class for converting into and out of IR.
class Converter {
public:
/// The result type for the FromProgram method.
using Result = utils::Result<Module, std::string>;
/// Builds an ir::Module from the given Program
/// @param program the Program to use.
/// @returns the `utiils::Result` of generating the IR. The result will contain the `ir::Module`
/// on success, otherwise the `std::string` error.
///
/// @note this assumes the program |IsValid|, and has had const-eval done so
/// any abstract values have been calculated and converted into the relevant
/// concrete types.
static Result FromProgram(const Program* program);
/// Converts the module back to a Program
/// @returns the resulting program, or nullptr on error
/// (Note, this will probably turn into a utils::Result, just stubbing for now)
static const Program* ToProgram();
};
} // namespace tint::ir
#endif // SRC_TINT_IR_CONVERTER_H_

View File

@ -14,26 +14,8 @@
#include "src/tint/ir/module.h" #include "src/tint/ir/module.h"
#include "src/tint/ir/builder_impl.h"
#include "src/tint/program.h"
namespace tint::ir { namespace tint::ir {
// static
Module::Result Module::FromProgram(const Program* program) {
if (!program->IsValid()) {
return Result{std::string("input program is not valid")};
}
BuilderImpl b(program);
auto r = b.Build();
if (!r) {
return b.Diagnostics().str();
}
return Result{r.Move()};
}
Module::Module() = default; Module::Module() = default;
Module::Module(Module&&) = default; Module::Module(Module&&) = default;
@ -42,8 +24,4 @@ Module::~Module() = default;
Module& Module::operator=(Module&&) = default; Module& Module::operator=(Module&&) = default;
const Program* Module::ToProgram() const {
return nullptr;
}
} // namespace tint::ir } // namespace tint::ir

View File

@ -28,29 +28,11 @@
#include "src/tint/utils/result.h" #include "src/tint/utils/result.h"
#include "src/tint/utils/vector.h" #include "src/tint/utils/vector.h"
// Forward Declarations
namespace tint {
class Program;
} // namespace tint
namespace tint::ir { namespace tint::ir {
/// Main module class for the IR. /// Main module class for the IR.
class Module { class Module {
public: public:
/// The result type for the FromProgram method.
using Result = utils::Result<Module, std::string>;
/// Builds an ir::Module from the given Program
/// @param program the Program to use.
/// @returns the `utiils::Result` of generating the IR. The result will contain the `ir::Module`
/// on success, otherwise the `std::string` error.
///
/// @note this assumes the program |IsValid|, and has had const-eval done so
/// any abstract values have been calculated and converted into the relevant
/// concrete types.
static Result FromProgram(const Program* program);
/// Constructor /// Constructor
Module(); Module();
/// Move constructor /// Move constructor
@ -64,11 +46,6 @@ class Module {
/// @returns a reference to this module /// @returns a reference to this module
Module& operator=(Module&& o); Module& operator=(Module&& o);
/// Converts the module back to a Program
/// @returns the resulting program, or nullptr on error
/// (Note, this will probably turn into a utils::Result, just stubbing for now)
const Program* ToProgram() const;
private: private:
/// Program Id required to create other components /// Program Id required to create other components
ProgramID prog_id_; ProgramID prog_id_;