[tint][hlsl] Use tint::Result for Generate()
This is more consistent with the rest of the codebase.
Also renames `writer::Result` to `writer::Output` to avoid clashing
with tint::Result.
Change-Id: Iaf776c1c9dca7a6dab6dec4962b398f815bd7bdd
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/144262
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: James Price <jrprice@google.com>
Auto-Submit: James Price <jrprice@google.com>
Reviewed-by: Dan Sinclair <dsinclair@chromium.org>
diff --git a/src/dawn/native/d3d/ShaderUtils.cpp b/src/dawn/native/d3d/ShaderUtils.cpp
index 6b86aaa..0c1359e 100644
--- a/src/dawn/native/d3d/ShaderUtils.cpp
+++ b/src/dawn/native/d3d/ShaderUtils.cpp
@@ -244,11 +244,11 @@
TRACE_EVENT0(tracePlatform.UnsafeGetValue(), General, "tint::hlsl::writer::Generate");
auto result = tint::hlsl::writer::Generate(&transformedProgram, options);
- DAWN_INVALID_IF(!result.success, "An error occured while generating HLSL: %s", result.error);
+ DAWN_INVALID_IF(!result, "An error occured while generating HLSL: %s", result.Failure());
compiledShader->usesVertexIndex = usesVertexIndex;
compiledShader->usesInstanceIndex = usesInstanceIndex;
- compiledShader->hlslSource = std::move(result.hlsl);
+ compiledShader->hlslSource = std::move(result->hlsl);
return {};
}
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn
index e2d661dd..992d13d 100644
--- a/src/tint/BUILD.gn
+++ b/src/tint/BUILD.gn
@@ -1188,8 +1188,8 @@
"lang/hlsl/writer/ast_printer/ast_printer.h",
"lang/hlsl/writer/common/options.cc",
"lang/hlsl/writer/common/options.h",
- "lang/hlsl/writer/result.cc",
- "lang/hlsl/writer/result.h",
+ "lang/hlsl/writer/output.cc",
+ "lang/hlsl/writer/output.h",
"lang/hlsl/writer/writer.cc",
"lang/hlsl/writer/writer.h",
]
diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt
index 31b3599..9f3e025 100644
--- a/src/tint/CMakeLists.txt
+++ b/src/tint/CMakeLists.txt
@@ -754,8 +754,8 @@
list(APPEND TINT_LIB_SRCS
lang/hlsl/writer/common/options.cc
lang/hlsl/writer/common/options.h
- lang/hlsl/writer/result.cc
- lang/hlsl/writer/result.h
+ lang/hlsl/writer/output.cc
+ lang/hlsl/writer/output.h
lang/hlsl/writer/writer.cc
lang/hlsl/writer/writer.h
lang/hlsl/writer/ast_printer/ast_printer.cc
diff --git a/src/tint/cmd/loopy.cc b/src/tint/cmd/loopy.cc
index c69926c..118bc08 100644
--- a/src/tint/cmd/loopy.cc
+++ b/src/tint/cmd/loopy.cc
@@ -247,9 +247,9 @@
gen_options.external_texture_options.bindings_map =
tint::cmd::GenerateExternalTextureBindings(program);
auto result = tint::hlsl::writer::Generate(program, gen_options);
- if (!result.success) {
+ if (!result) {
tint::cmd::PrintWGSL(std::cerr, *program);
- std::cerr << "Failed to generate: " << result.error << std::endl;
+ std::cerr << "Failed to generate: " << result.Failure() << std::endl;
return false;
}
diff --git a/src/tint/cmd/main.cc b/src/tint/cmd/main.cc
index 92873df..88d88a4 100644
--- a/src/tint/cmd/main.cc
+++ b/src/tint/cmd/main.cc
@@ -692,17 +692,17 @@
tint::cmd::GenerateExternalTextureBindings(program);
gen_options.root_constant_binding_point = options.hlsl_root_constant_binding_point;
auto result = tint::hlsl::writer::Generate(program, gen_options);
- if (!result.success) {
+ if (!result) {
tint::cmd::PrintWGSL(std::cerr, *program);
- std::cerr << "Failed to generate: " << result.error << std::endl;
+ std::cerr << "Failed to generate: " << result.Failure() << std::endl;
return false;
}
- if (!WriteFile(options.output_file, "w", result.hlsl)) {
+ if (!WriteFile(options.output_file, "w", result->hlsl)) {
return false;
}
- const auto hash = tint::CRC32(result.hlsl.c_str());
+ const auto hash = tint::CRC32(result->hlsl.c_str());
if (options.print_hash) {
PrintHash(hash);
}
@@ -731,7 +731,7 @@
}
dxc_res = tint::hlsl::validate::UsingDXC(
- dxc.Path(), result.hlsl, result.entry_points, dxc_require_16bit_types);
+ dxc.Path(), result->hlsl, result->entry_points, dxc_require_16bit_types);
} else if (must_validate_dxc) {
// DXC was explicitly requested. Error if it could not be found.
dxc_res.failed = true;
@@ -751,7 +751,7 @@
if (fxc.Found()) {
fxc_found = true;
fxc_res =
- tint::hlsl::validate::UsingFXC(fxc.Path(), result.hlsl, result.entry_points);
+ tint::hlsl::validate::UsingFXC(fxc.Path(), result->hlsl, result->entry_points);
} else if (must_validate_fxc) {
// FXC was explicitly requested. Error if it could not be found.
fxc_res.failed = true;
diff --git a/src/tint/fuzzers/tint_common_fuzzer.cc b/src/tint/fuzzers/tint_common_fuzzer.cc
index 2e1da16..7f2d93f 100644
--- a/src/tint/fuzzers/tint_common_fuzzer.cc
+++ b/src/tint/fuzzers/tint_common_fuzzer.cc
@@ -313,7 +313,7 @@
}
case OutputFormat::kHLSL: {
#if TINT_BUILD_HLSL_WRITER
- hlsl::writer::Generate(&program, options_hlsl_);
+ (void)hlsl::writer::Generate(&program, options_hlsl_);
#endif // TINT_BUILD_HLSL_WRITER
break;
}
diff --git a/src/tint/fuzzers/tint_concurrency_fuzzer.cc b/src/tint/fuzzers/tint_concurrency_fuzzer.cc
index 9b2d6f2..f09524d 100644
--- a/src/tint/fuzzers/tint_concurrency_fuzzer.cc
+++ b/src/tint/fuzzers/tint_concurrency_fuzzer.cc
@@ -95,7 +95,7 @@
#if TINT_BUILD_HLSL_WRITER
case Writer::kHLSL: {
- tint::hlsl::writer::Generate(&program, {});
+ (void)tint::hlsl::writer::Generate(&program, {});
break;
}
#endif // TINT_BUILD_HLSL_WRITER
diff --git a/src/tint/lang/hlsl/writer/ast_printer/ast_printer_test.cc b/src/tint/lang/hlsl/writer/ast_printer/ast_printer_test.cc
index 5187dd2..c616f72 100644
--- a/src/tint/lang/hlsl/writer/ast_printer/ast_printer_test.cc
+++ b/src/tint/lang/hlsl/writer/ast_printer/ast_printer_test.cc
@@ -27,7 +27,8 @@
auto program = std::make_unique<Program>(resolver::Resolve(*this));
ASSERT_FALSE(program->IsValid());
auto result = Generate(program.get(), Options{});
- EXPECT_EQ(result.error, "input program is not valid");
+ EXPECT_FALSE(result);
+ EXPECT_EQ(result.Failure(), "input program is not valid");
}
TEST_F(HlslASTPrinterTest, UnsupportedExtension) {
diff --git a/src/tint/lang/hlsl/writer/result.cc b/src/tint/lang/hlsl/writer/output.cc
similarity index 82%
rename from src/tint/lang/hlsl/writer/result.cc
rename to src/tint/lang/hlsl/writer/output.cc
index 069545c..cd51ba3 100644
--- a/src/tint/lang/hlsl/writer/result.cc
+++ b/src/tint/lang/hlsl/writer/output.cc
@@ -12,14 +12,14 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#include "src/tint/lang/hlsl/writer/result.h"
+#include "src/tint/lang/hlsl/writer/output.h"
namespace tint::hlsl::writer {
-Result::Result() = default;
+Output::Output() = default;
-Result::~Result() = default;
+Output::~Output() = default;
-Result::Result(const Result&) = default;
+Output::Output(const Output&) = default;
} // namespace tint::hlsl::writer
diff --git a/src/tint/lang/hlsl/writer/result.h b/src/tint/lang/hlsl/writer/output.h
similarity index 73%
rename from src/tint/lang/hlsl/writer/result.h
rename to src/tint/lang/hlsl/writer/output.h
index 6de548d..f3e54f7 100644
--- a/src/tint/lang/hlsl/writer/result.h
+++ b/src/tint/lang/hlsl/writer/output.h
@@ -12,10 +12,9 @@
// See the License for the specific language governing permissions and
// limitations under the License.
-#ifndef SRC_TINT_LANG_HLSL_WRITER_RESULT_H_
-#define SRC_TINT_LANG_HLSL_WRITER_RESULT_H_
+#ifndef SRC_TINT_LANG_HLSL_WRITER_OUTPUT_H_
+#define SRC_TINT_LANG_HLSL_WRITER_OUTPUT_H_
-#include <optional>
#include <string>
#include <unordered_set>
#include <utility>
@@ -25,22 +24,16 @@
namespace tint::hlsl::writer {
-/// The result produced when generating HLSL.
-struct Result {
+/// The output produced when generating HLSL.
+struct Output {
/// Constructor
- Result();
+ Output();
/// Destructor
- ~Result();
+ ~Output();
/// Copy constructor
- Result(const Result&);
-
- /// True if generation was successful.
- bool success = false;
-
- /// The errors generated during code generation, if any.
- std::string error;
+ Output(const Output&);
/// The generated HLSL.
std::string hlsl = "";
@@ -55,4 +48,4 @@
} // namespace tint::hlsl::writer
-#endif // SRC_TINT_LANG_HLSL_WRITER_RESULT_H_
+#endif // SRC_TINT_LANG_HLSL_WRITER_OUTPUT_H_
diff --git a/src/tint/lang/hlsl/writer/writer.cc b/src/tint/lang/hlsl/writer/writer.cc
index d8b7670..fa88e21 100644
--- a/src/tint/lang/hlsl/writer/writer.cc
+++ b/src/tint/lang/hlsl/writer/writer.cc
@@ -21,39 +21,38 @@
namespace tint::hlsl::writer {
-Result Generate(const Program* program, const Options& options) {
- Result result;
+Result<Output, std::string> Generate(const Program* program, const Options& options) {
if (!program->IsValid()) {
- result.error = "input program is not valid";
- return result;
+ return std::string("input program is not valid");
}
// Sanitize the program.
auto sanitized_result = Sanitize(program, options);
if (!sanitized_result.program.IsValid()) {
- result.success = false;
- result.error = sanitized_result.program.Diagnostics().str();
- return result;
+ return sanitized_result.program.Diagnostics().str();
}
// Generate the HLSL code.
auto impl = std::make_unique<ASTPrinter>(&sanitized_result.program);
- result.success = impl->Generate();
- result.error = impl->Diagnostics().str();
- result.hlsl = impl->Result();
+ if (!impl->Generate()) {
+ return impl->Diagnostics().str();
+ }
+
+ Output output;
+ output.hlsl = impl->Result();
// Collect the list of entry points in the sanitized program.
for (auto* func : sanitized_result.program.AST().Functions()) {
if (func->IsEntryPoint()) {
auto name = func->name->symbol.Name();
- result.entry_points.push_back({name, func->PipelineStage()});
+ output.entry_points.push_back({name, func->PipelineStage()});
}
}
- result.used_array_length_from_uniform_indices =
+ output.used_array_length_from_uniform_indices =
std::move(sanitized_result.used_array_length_from_uniform_indices);
- return result;
+ return output;
}
} // namespace tint::hlsl::writer
diff --git a/src/tint/lang/hlsl/writer/writer.h b/src/tint/lang/hlsl/writer/writer.h
index 4c0410b..2d28d7f 100644
--- a/src/tint/lang/hlsl/writer/writer.h
+++ b/src/tint/lang/hlsl/writer/writer.h
@@ -15,8 +15,11 @@
#ifndef SRC_TINT_LANG_HLSL_WRITER_WRITER_H_
#define SRC_TINT_LANG_HLSL_WRITER_WRITER_H_
+#include <string>
+
#include "src/tint/lang/hlsl/writer/common/options.h"
-#include "src/tint/lang/hlsl/writer/result.h"
+#include "src/tint/lang/hlsl/writer/output.h"
+#include "src/tint/utils/result/result.h"
// Forward declarations
namespace tint {
@@ -26,12 +29,11 @@
namespace tint::hlsl::writer {
/// Generate HLSL for a program, according to a set of configuration options.
-/// The result will contain the HLSL, as well as success status and diagnostic
-/// information.
+/// The result will contain the HLSL and supplementary information, or an error string.
/// @param program the program to translate to HLSL
/// @param options the configuration options to use when generating HLSL
-/// @returns the resulting HLSL and supplementary information
-Result Generate(const Program* program, const Options& options);
+/// @returns the resulting HLSL and supplementary information, or an error string
+Result<Output, std::string> Generate(const Program* program, const Options& options);
} // namespace tint::hlsl::writer
diff --git a/src/tint/lang/hlsl/writer/writer_bench.cc b/src/tint/lang/hlsl/writer/writer_bench.cc
index 7203ae9..1831f22 100644
--- a/src/tint/lang/hlsl/writer/writer_bench.cc
+++ b/src/tint/lang/hlsl/writer/writer_bench.cc
@@ -28,8 +28,8 @@
auto& program = std::get<bench::ProgramAndFile>(res).program;
for (auto _ : state) {
auto res = Generate(&program, {});
- if (!res.error.empty()) {
- state.SkipWithError(res.error.c_str());
+ if (!res) {
+ state.SkipWithError(res.Failure().c_str());
}
}
}