[tint] Auto-generate a benchmark input header

The Chrome Perf waterfall prefers test binaries to be standalone with
no runtime dependencies. This script collects all WGSL and SPIR-V
shaders in `test/tint/benchmark/` and converts them to string literals
and constant arrays in a single header file that can be included by
the benchmark binary. It also defines the macro that registers each of
these shaders with Google Benchmark.

Bug: 42251293
Change-Id: I778d7395ea64988705bbbb624f7e5b5abb8a4920
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/200515
Reviewed-by: dan sinclair <dsinclair@chromium.org>
Commit-Queue: James Price <jrprice@google.com>
diff --git a/src/tint/BUILD.gn b/src/tint/BUILD.gn
index fd26dbe..aa2a940 100644
--- a/src/tint/BUILD.gn
+++ b/src/tint/BUILD.gn
@@ -258,6 +258,12 @@
     testonly = true
     public_deps = [ "//third_party/google_benchmark" ]
   }
+  action("generate_benchmark_inputs") {
+    output_header = "${target_gen_dir}/cmd/bench/benchmark_inputs.h"
+    outputs = [ output_header ]
+    script = "${tint_src_dir}/cmd/bench/generate_benchmark_inputs.py"
+    args = [ rebase_path(output_header, root_build_dir) ]
+  }
 }
 
 group("abseil") {
diff --git a/src/tint/CMakeLists.txt b/src/tint/CMakeLists.txt
index 7e67586..0e0ff6e 100644
--- a/src/tint/CMakeLists.txt
+++ b/src/tint/CMakeLists.txt
@@ -259,6 +259,7 @@
 
 function(tint_bench_compile_options TARGET)
   tint_core_compile_options(${TARGET})
+  target_include_directories(${TARGET} PUBLIC "${DAWN_BUILD_GEN_DIR}")
   set_target_properties(${TARGET} PROPERTIES FOLDER "Benchmarks")
 endfunction()
 
@@ -320,6 +321,18 @@
 ################################################################################
 # Benchmarks
 ################################################################################
+if(TINT_BUILD_BENCHMARKS)
+  set(SCRIPT "${TINT_ROOT_SOURCE_DIR}/src/tint/cmd/bench/generate_benchmark_inputs.py")
+  set(OUTPUT "${DAWN_BUILD_GEN_DIR}/src/tint/cmd/bench/benchmark_inputs.h")
+  set(ARGS ${Python3_EXECUTABLE} ${SCRIPT} ${OUTPUT})
+  add_custom_command(
+    COMMAND ${ARGS}
+    OUTPUT ${OUTPUT}
+    DEPENDS ${SCRIPT}
+    COMMENT "Tint benchmark: Generating ${OUTPUT}."
+  )
+  add_custom_target(tint_generate_benchmark_inputs DEPENDS ${OUTPUT})
+endif()
 if(TINT_BUILD_BENCHMARKS AND TINT_EXTERNAL_BENCHMARK_CORPUS_DIR)
   # Glob all the files at TINT_EXTERNAL_BENCHMARK_CORPUS_DIR, and create a header
   # that lists these with the macros:
diff --git a/src/tint/cmd/bench/generate_benchmark_inputs.py b/src/tint/cmd/bench/generate_benchmark_inputs.py
new file mode 100644
index 0000000..76a40b6
--- /dev/null
+++ b/src/tint/cmd/bench/generate_benchmark_inputs.py
@@ -0,0 +1,107 @@
+#!/usr/bin/env python3
+
+# 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.
+"""
+Generates a header file that declares all of the Tint benchmark programs as embedded WGSL and
+SPIR-V shaders, and declares macros that will be used to register them all with Google Benchmark.
+
+The SPIR-V shaders are emitted as an array of uint32_t values.
+
+Usage:
+   generate_benchmark_inputs.py <header_output_path>
+"""
+
+import argparse
+import struct
+import sys
+from os import listdir, path
+
+
+def main():
+    parser = argparse.ArgumentParser()
+    parser.add_argument('header_output_path')
+    args = parser.parse_args()
+
+    script_dir = path.dirname(path.realpath(__file__))
+    benchmark_dir = script_dir + '/../../../../test/tint/benchmark'
+    wgsl_files = [f for f in listdir(benchmark_dir) if f.endswith('.wgsl')]
+    spv_files = [f for f in listdir(benchmark_dir) if f.endswith('.spv')]
+
+    with open(args.header_output_path, 'w') as output:
+        print('''// AUTOMATICALLY GENERATED, DO NOT MODIFY.
+
+#ifndef SRC_TINT_CMD_BENCH_BENCHMARK_INPUTS_H_
+#define SRC_TINT_CMD_BENCH_BENCHMARK_INPUTS_H_
+
+#include <cstdint>
+#include <vector>
+
+namespace tint::bench {
+
+struct BenchmarkInput {
+    const char* name = nullptr;
+    const char* wgsl = nullptr;
+    const std::vector<uint32_t> spirv;
+};
+const BenchmarkInput kBenchmarkInputs[] = {''',
+              file=output)
+
+        # Add an entry to the array for each benchmark.
+        for f in wgsl_files:
+            # WGSL shaders are emitted as string literals.
+            with open(benchmark_dir + '/' + f, 'r') as input:
+                print(f'    {{"{f}", R"({input.read()})"}},', file=output)
+        for f in spv_files:
+            # SPIR-V shaders are emitted as uint32_t initializer lists.
+            with open(benchmark_dir + '/' + f, 'rb') as input:
+                print(f'    {{"{f}", nullptr, {{', file=output, end='')
+                content = input.read()
+                for word in struct.unpack("<" + ("I" * ((len(content)) // 4)),
+                                          content):
+                    print(f'{word}', file=output, end=', ')
+                print(f'}}}},', file=output)
+
+        print('};', file=output)
+        print('', file=output)
+
+        # Define the macro that registers each of the inputs with Google Benchmark.
+        print('#define TINT_BENCHMARK_PROGRAMS(FUNC) \\', file=output)
+        for f in sorted(wgsl_files) + sorted(spv_files):
+            print(f'    BENCHMARK_CAPTURE(FUNC, {f}, "{f}"); \\', file=output)
+        print('    TINT_REQUIRE_SEMICOLON', file=output)
+        print('', file=output)
+
+        print('''
+}  // namespace tint::bench
+
+#endif  // SRC_TINT_CMD_BENCH_BENCH_H_''',
+              file=output)
+
+
+if __name__ == "__main__":
+    sys.exit(main())