diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn index 28fe25d4dc..d8a75b30f2 100644 --- a/src/tint/BUILD.gn +++ b/src/tint/BUILD.gn @@ -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") { sources = [ "ir/binary.cc", @@ -1114,8 +1132,6 @@ libtint_source_set("libtint_ir_src") { "ir/block.h", "ir/builder.cc", "ir/builder.h", - "ir/builder_impl.cc", - "ir/builder_impl.h", "ir/builtin.cc", "ir/builtin.h", "ir/call.cc", @@ -1161,11 +1177,8 @@ libtint_source_set("libtint_ir_src") { ] deps = [ - ":libtint_ast_src", ":libtint_builtins_src", ":libtint_constant_src", - ":libtint_program_src", - ":libtint_sem_src", ":libtint_symbols_src", ":libtint_type_src", ":libtint_utils_src", @@ -1222,7 +1235,10 @@ source_set("libtint") { if (tint_build_ir) { assert(!build_with_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" ] @@ -2112,7 +2128,10 @@ if (tint_build_unittests) { "ir/unary_test.cc", ] - deps = [ ":libtint_ir_src" ] + deps = [ + ":libtint_ir_builder_src", + ":libtint_ir_src", + ] } if (build_with_chromium) { diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt index 64e0939a8e..ac1a0b5f37 100644 --- a/src/tint/CMakeLists.txt +++ b/src/tint/CMakeLists.txt @@ -718,6 +718,8 @@ if(${TINT_BUILD_IR}) ir/construct.h ir/convert.cc ir/convert.h + ir/converter.cc + ir/converter.h ir/debug.cc ir/debug.h ir/disassembler.cc diff --git a/src/tint/cmd/loopy.cc b/src/tint/cmd/loopy.cc index 1714e3bbdf..75102ebe1a 100644 --- a/src/tint/cmd/loopy.cc +++ b/src/tint/cmd/loopy.cc @@ -19,6 +19,7 @@ #include "tint/tint.h" #if TINT_BUILD_IR +#include "src/tint/ir/converter.h" #include "src/tint/ir/module.h" #endif // TINT_BUILD_IR @@ -377,7 +378,7 @@ int main(int argc, const char** argv) { loop_count = options.loop_count; } 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) { std::cerr << "Failed to build IR from program: " << result.Failure() << std::endl; } diff --git a/src/tint/cmd/main.cc b/src/tint/cmd/main.cc index d61ae247d1..c8260f1ea0 100644 --- a/src/tint/cmd/main.cc +++ b/src/tint/cmd/main.cc @@ -49,10 +49,11 @@ #include "tint/tint.h" #if TINT_BUILD_IR -#include "src/tint/ir/debug.h" -#include "src/tint/ir/disassembler.h" -#include "src/tint/ir/module.h" -#endif // TINT_BUILD_IR +#include "src/tint/ir/converter.h" // nogncheck +#include "src/tint/ir/debug.h" // nogncheck +#include "src/tint/ir/disassembler.h" // nogncheck +#include "src/tint/ir/module.h" // nogncheck +#endif // TINT_BUILD_IR namespace { @@ -1078,7 +1079,7 @@ int main(int argc, const char** argv) { #if TINT_BUILD_IR 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) { std::cerr << "Failed to build IR from program: " << result.Failure() << std::endl; } else { diff --git a/src/tint/ir/builder.cc b/src/tint/ir/builder.cc index 13ef345da2..64066fd3fc 100644 --- a/src/tint/ir/builder.cc +++ b/src/tint/ir/builder.cc @@ -16,8 +16,6 @@ #include -#include "src/tint/ir/builder_impl.h" - namespace tint::ir { Builder::Builder() {} diff --git a/src/tint/ir/converter.cc b/src/tint/ir/converter.cc new file mode 100644 index 0000000000..e89be3a42e --- /dev/null +++ b/src/tint/ir/converter.cc @@ -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 diff --git a/src/tint/ir/converter.h b/src/tint/ir/converter.h new file mode 100644 index 0000000000..8defd83a34 --- /dev/null +++ b/src/tint/ir/converter.h @@ -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 + +#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; + + /// 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_ diff --git a/src/tint/ir/module.cc b/src/tint/ir/module.cc index e434888653..133d364727 100644 --- a/src/tint/ir/module.cc +++ b/src/tint/ir/module.cc @@ -14,26 +14,8 @@ #include "src/tint/ir/module.h" -#include "src/tint/ir/builder_impl.h" -#include "src/tint/program.h" - 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(Module&&) = default; @@ -42,8 +24,4 @@ Module::~Module() = default; Module& Module::operator=(Module&&) = default; -const Program* Module::ToProgram() const { - return nullptr; -} - } // namespace tint::ir diff --git a/src/tint/ir/module.h b/src/tint/ir/module.h index 5cfc883182..a7172eaca2 100644 --- a/src/tint/ir/module.h +++ b/src/tint/ir/module.h @@ -28,29 +28,11 @@ #include "src/tint/utils/result.h" #include "src/tint/utils/vector.h" -// Forward Declarations -namespace tint { -class Program; -} // namespace tint - namespace tint::ir { /// Main module class for the IR. class Module { public: - /// The result type for the FromProgram method. - using Result = utils::Result; - - /// 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 Module(); /// Move constructor @@ -64,11 +46,6 @@ class Module { /// @returns a reference to this module 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: /// Program Id required to create other components ProgramID prog_id_;