[tint][fuzz][ast] Add FirstIndexOffset fuzzer
Bug: tint:2223
Change-Id: I1ef8c17adaa269f8058c748d722ce6ad72c21c1d
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/185642
Reviewed-by: James Price <jrprice@google.com>
Reviewed-by: Ryan Harrison <rharrison@chromium.org>
diff --git a/src/tint/lang/wgsl/ast/transform/BUILD.cmake b/src/tint/lang/wgsl/ast/transform/BUILD.cmake
index ebdb5fe..8f5b676 100644
--- a/src/tint/lang/wgsl/ast/transform/BUILD.cmake
+++ b/src/tint/lang/wgsl/ast/transform/BUILD.cmake
@@ -259,6 +259,7 @@
lang/wgsl/ast/transform/direct_variable_access_fuzz.cc
lang/wgsl/ast/transform/disable_uniformity_analysis_fuzz.cc
lang/wgsl/ast/transform/expand_compound_assignment_fuzz.cc
+ lang/wgsl/ast/transform/first_index_offset_fuzz.cc
lang/wgsl/ast/transform/zero_init_workgroup_memory_fuzz.cc
)
diff --git a/src/tint/lang/wgsl/ast/transform/BUILD.gn b/src/tint/lang/wgsl/ast/transform/BUILD.gn
index f708ef4..9839001 100644
--- a/src/tint/lang/wgsl/ast/transform/BUILD.gn
+++ b/src/tint/lang/wgsl/ast/transform/BUILD.gn
@@ -249,6 +249,7 @@
"direct_variable_access_fuzz.cc",
"disable_uniformity_analysis_fuzz.cc",
"expand_compound_assignment_fuzz.cc",
+ "first_index_offset_fuzz.cc",
"zero_init_workgroup_memory_fuzz.cc",
]
deps = [
diff --git a/src/tint/lang/wgsl/ast/transform/first_index_offset.cc b/src/tint/lang/wgsl/ast/transform/first_index_offset.cc
index 399bb2a..a7d912c 100644
--- a/src/tint/lang/wgsl/ast/transform/first_index_offset.cc
+++ b/src/tint/lang/wgsl/ast/transform/first_index_offset.cc
@@ -69,6 +69,7 @@
FirstIndexOffset::BindingPoint::BindingPoint(uint32_t b, uint32_t g) : binding(b), group(g) {}
FirstIndexOffset::BindingPoint::~BindingPoint() = default;
+FirstIndexOffset::Data::Data() = default;
FirstIndexOffset::Data::Data(bool has_vtx_index, bool has_inst_index)
: has_vertex_index(has_vtx_index), has_instance_index(has_inst_index) {}
FirstIndexOffset::Data::Data(const Data&) = default;
diff --git a/src/tint/lang/wgsl/ast/transform/first_index_offset.h b/src/tint/lang/wgsl/ast/transform/first_index_offset.h
index c943d8b..c414485 100644
--- a/src/tint/lang/wgsl/ast/transform/first_index_offset.h
+++ b/src/tint/lang/wgsl/ast/transform/first_index_offset.h
@@ -28,7 +28,9 @@
#ifndef SRC_TINT_LANG_WGSL_AST_TRANSFORM_FIRST_INDEX_OFFSET_H_
#define SRC_TINT_LANG_WGSL_AST_TRANSFORM_FIRST_INDEX_OFFSET_H_
+#include "src/tint/lang/wgsl/ast/transform/binding_remapper.h"
#include "src/tint/lang/wgsl/ast/transform/transform.h"
+#include "src/tint/utils/reflection/reflection.h"
namespace tint::ast::transform {
@@ -91,12 +93,18 @@
uint32_t binding = 0;
/// `@group()` for the first vertex / first instance uniform buffer
uint32_t group = 0;
+
+ /// Reflection for this struct
+ TINT_REFLECT(BindingPoint, binding, group);
};
/// Data is outputted by the FirstIndexOffset transform.
/// Data holds information about shader usage and constant buffer offsets.
struct Data final : public Castable<Data, transform::Data> {
/// Constructor
+ Data();
+
+ /// Constructor
/// @param has_vtx_index True if the shader uses vertex_index
/// @param has_inst_index True if the shader uses instance_index
Data(bool has_vtx_index, bool has_inst_index);
@@ -108,9 +116,12 @@
~Data() override;
/// True if the shader uses vertex_index
- const bool has_vertex_index;
+ bool has_vertex_index = false;
/// True if the shader uses instance_index
- const bool has_instance_index;
+ bool has_instance_index = false;
+
+ /// Reflection for this struct
+ TINT_REFLECT(Data, has_vertex_index, has_instance_index);
};
/// Constructor
diff --git a/src/tint/lang/wgsl/ast/transform/first_index_offset_fuzz.cc b/src/tint/lang/wgsl/ast/transform/first_index_offset_fuzz.cc
new file mode 100644
index 0000000..b7ed12e
--- /dev/null
+++ b/src/tint/lang/wgsl/ast/transform/first_index_offset_fuzz.cc
@@ -0,0 +1,70 @@
+// Copyright 2024 The Dawn & Tint Authors
+//
+// Redistribution and use in source and binary forms, with or without
+// modification, are permitted provided that the following conditions are met:
+//
+// 1. Redistributions of source code must retain the above copyright notice, this
+// list of conditions and the following disclaimer.
+//
+// 2. Redistributions in binary form must reproduce the above copyright notice,
+// this list of conditions and the following disclaimer in the documentation
+// and/or other materials provided with the distribution.
+//
+// 3. Neither the name of the copyright holder nor the names of its
+// contributors may be used to endorse or promote products derived from
+// this software without specific prior written permission.
+//
+// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
+// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
+// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
+// DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
+// FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
+// DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
+// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
+// OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
+// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+
+#include "src/tint/api/common/binding_point.h"
+#include "src/tint/cmd/fuzz/wgsl/fuzz.h"
+#include "src/tint/lang/wgsl/ast/module.h"
+#include "src/tint/lang/wgsl/ast/transform/first_index_offset.h"
+#include "src/tint/lang/wgsl/sem/variable.h"
+
+namespace tint::ast::transform {
+namespace {
+
+bool CanRun(const Program& program, const FirstIndexOffset::BindingPoint& binding_point) {
+ for (auto& global : program.AST().GlobalVariables()) {
+ if (auto* sem = program.Sem().Get<sem::GlobalVariable>(global)) {
+ if (sem->Attributes().binding_point ==
+ BindingPoint{binding_point.group, binding_point.binding}) {
+ // Might cause binding point collision
+ return false;
+ }
+ }
+ }
+ return true;
+}
+
+void FirstIndexOffsetFuzzer(const Program& program,
+ const FirstIndexOffset::BindingPoint& binding_point) {
+ if (!CanRun(program, binding_point)) {
+ return;
+ }
+
+ DataMap inputs;
+ inputs.Add<FirstIndexOffset::BindingPoint>(std::move(binding_point));
+
+ DataMap outputs;
+ if (auto result = FirstIndexOffset{}.Apply(program, inputs, outputs)) {
+ if (!result->IsValid()) {
+ TINT_ICE() << "FirstIndexOffset returned invalid program:\n" << result->Diagnostics();
+ }
+ }
+}
+
+} // namespace
+} // namespace tint::ast::transform
+
+TINT_WGSL_PROGRAM_FUZZER(tint::ast::transform::FirstIndexOffsetFuzzer);