[tint] Add support for benchmarking .spv files

Change-Id: Id687d5208e9f39ae7b40db4d90c194283ebac91b
Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/132642
Reviewed-by: James Price <jrprice@google.com>
Kokoro: Ben Clayton <bclayton@google.com>
Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
Ben Clayton 2023-05-14 01:36:30 +00:00 committed by Dawn LUCI CQ
parent 516a4fd13a
commit 1a8bc1df7f
9 changed files with 75 additions and 29 deletions

View File

@ -1546,24 +1546,42 @@ if(TINT_BUILD_BENCHMARKS)
if (TINT_EXTERNAL_BENCHMARK_CORPUS_DIR)
# Glob all the files at TINT_EXTERNAL_BENCHMARK_CORPUS_DIR, and create a header
# that lists these with the TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS() macro
# that lists these with the macros:
# TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS()
# TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS()
set(TINT_BENCHMARK_GEN_DIR "${DAWN_BUILD_GEN_DIR}/src/tint/benchmark/")
set(TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAM_HEADER "${TINT_BENCHMARK_GEN_DIR}/external_wgsl_programs.h")
set(TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER "${TINT_BENCHMARK_GEN_DIR}/external_wgsl_programs.h")
message("Globbing ${TINT_EXTERNAL_BENCHMARK_CORPUS_DIR}...")
file(GLOB_RECURSE
TINT_EXTERNAL_BENCHMARK_FILES
TINT_EXTERNAL_WGSL_BENCHMARK_FILES
RELATIVE "${TINT_EXTERNAL_BENCHMARK_CORPUS_DIR}"
"${TINT_EXTERNAL_BENCHMARK_CORPUS_DIR}/**.wgsl")
list(TRANSFORM TINT_EXTERNAL_BENCHMARK_FILES REPLACE
list(TRANSFORM TINT_EXTERNAL_WGSL_BENCHMARK_FILES REPLACE
"(.+)"
" BENCHMARK_CAPTURE\(FUNC, \"\\1\", \"${TINT_EXTERNAL_BENCHMARK_CORPUS_DIR}/\\1\")")
list(JOIN TINT_EXTERNAL_BENCHMARK_FILES "; \\\n" TINT_EXTERNAL_BENCHMARK_FILES)
list(JOIN TINT_EXTERNAL_WGSL_BENCHMARK_FILES "; \\\n" TINT_EXTERNAL_WGSL_BENCHMARK_FILES)
file(GLOB_RECURSE
TINT_EXTERNAL_SPV_BENCHMARK_FILES
RELATIVE "${TINT_EXTERNAL_BENCHMARK_CORPUS_DIR}"
"${TINT_EXTERNAL_BENCHMARK_CORPUS_DIR}/**.spv")
list(TRANSFORM TINT_EXTERNAL_SPV_BENCHMARK_FILES REPLACE
"(.+)"
" BENCHMARK_CAPTURE\(FUNC, \"\\1\", \"${TINT_EXTERNAL_BENCHMARK_CORPUS_DIR}/\\1\")")
list(JOIN TINT_EXTERNAL_SPV_BENCHMARK_FILES "; \\\n" TINT_EXTERNAL_SPV_BENCHMARK_FILES)
file(CONFIGURE
OUTPUT "${TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAM_HEADER}"
CONTENT "#define TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS(FUNC) \\
${TINT_EXTERNAL_BENCHMARK_FILES};")
# Define TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAM_HEADER to the generated header path
OUTPUT "${TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER}"
CONTENT "
#define TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS(FUNC) \\
${TINT_EXTERNAL_WGSL_BENCHMARK_FILES};
#define TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS(FUNC) \\
${TINT_EXTERNAL_SPV_BENCHMARK_FILES};")
# Define TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER to the generated header path
target_compile_definitions(tint-benchmark PRIVATE
"TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAM_HEADER=\"${TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAM_HEADER}\"")
"TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER=\"${TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER}\"")
endif()
endif(TINT_BUILD_BENCHMARKS)

View File

@ -89,12 +89,30 @@ bool FindBenchmarkInputDir() {
} // namespace
std::variant<tint::Source::File, Error> LoadInputFile(std::string name) {
auto path = (kInputFileDir / name).string();
auto path = std::filesystem::path(name).is_absolute() ? name : (kInputFileDir / name).string();
if (utils::HasSuffix(path, ".wgsl")) {
auto data = ReadFile<uint8_t>(path);
if (auto* buf = std::get_if<std::vector<uint8_t>>(&data)) {
return tint::Source::File(path, std::string(buf->begin(), buf->end()));
}
return std::get<Error>(data);
}
if (utils::HasSuffix(path, ".spv")) {
auto spirv = ReadFile<uint32_t>(path);
if (auto* buf = std::get_if<std::vector<uint32_t>>(&spirv)) {
auto program = tint::reader::spirv::Parse(*buf, {});
if (!program.IsValid()) {
return Error{program.Diagnostics().str()};
}
auto result = tint::writer::wgsl::Generate(&program, {});
if (!result.success) {
return Error{result.error};
}
return tint::Source::File(path, result.wgsl);
}
return std::get<Error>(spirv);
}
return Error{"unsupported file extension: '" + name + "'"};
}
std::variant<ProgramAndFile, Error> LoadProgram(std::string name) {

View File

@ -40,7 +40,8 @@ struct ProgramAndFile {
};
/// LoadInputFile attempts to load a benchmark input file with the given file
/// name.
/// name. Accepts files with the .wgsl and .spv extension.
/// SPIR-V files are automatically converted to WGSL.
/// @param name the file name
/// @returns either the loaded Source::File or an Error
std::variant<Source::File, Error> LoadInputFile(std::string name);
@ -51,20 +52,21 @@ std::variant<Source::File, Error> LoadInputFile(std::string name);
/// @returns either the loaded Program or an Error
std::variant<ProgramAndFile, Error> LoadProgram(std::string name);
// If TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAM_HEADER is defined, include that to
// declare the TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS() macro, which appends
// external programs to the TINT_BENCHMARK_WGSL_PROGRAMS() list.
#ifdef TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAM_HEADER
#include TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAM_HEADER
// If TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER is defined, include that to
// declare the TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS() and TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS()
// macros, which appends external programs to the TINT_BENCHMARK_WGSL_PROGRAMS() and
// TINT_BENCHMARK_SPV_PROGRAMS() list.
#ifdef TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER
#include TINT_BENCHMARK_EXTERNAL_SHADERS_HEADER
#else
#define TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS(x)
#define TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS(x)
#endif
/// Declares a benchmark with the given function and WGSL file name
#define TINT_BENCHMARK_WGSL_PROGRAM(FUNC, WGSL_NAME) BENCHMARK_CAPTURE(FUNC, WGSL_NAME, WGSL_NAME);
/// Declares a set of benchmarks for the given function using a list of WGSL
/// files in `<tint>/test/benchmark`.
/// Declares a set of benchmarks for the given function using a list of WGSL files.
#define TINT_BENCHMARK_WGSL_PROGRAMS(FUNC) \
TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "animometer.wgsl"); \
TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "atan2-const-eval.wgsl"); \
@ -81,6 +83,14 @@ std::variant<ProgramAndFile, Error> LoadProgram(std::string name);
TINT_BENCHMARK_WGSL_PROGRAM(FUNC, "skinned-shadowed-pbr-vertex.wgsl"); \
TINT_BENCHMARK_EXTERNAL_WGSL_PROGRAMS(FUNC)
/// Declares a set of benchmarks for the given function using a list of SPIR-V files.
#define TINT_BENCHMARK_SPV_PROGRAMS(FUNC) TINT_BENCHMARK_EXTERNAL_SPV_PROGRAMS(FUNC)
/// Declares a set of benchmarks for the given function using a list of WGSL and SPIR-V files.
#define TINT_BENCHMARK_PROGRAMS(FUNC) \
TINT_BENCHMARK_WGSL_PROGRAMS(FUNC) \
TINT_BENCHMARK_SPV_PROGRAMS(FUNC)
} // namespace tint::bench
#endif // SRC_TINT_BENCH_BENCHMARK_H_

View File

@ -34,7 +34,7 @@ void ParseWGSL(benchmark::State& state, std::string input_name) {
}
}
TINT_BENCHMARK_WGSL_PROGRAMS(ParseWGSL);
TINT_BENCHMARK_PROGRAMS(ParseWGSL);
} // namespace
} // namespace tint::reader::wgsl

View File

@ -45,7 +45,7 @@ void GenerateGLSL(benchmark::State& state, std::string input_name) {
}
}
TINT_BENCHMARK_WGSL_PROGRAMS(GenerateGLSL);
TINT_BENCHMARK_PROGRAMS(GenerateGLSL);
} // namespace
} // namespace tint::writer::glsl

View File

@ -34,7 +34,7 @@ void GenerateHLSL(benchmark::State& state, std::string input_name) {
}
}
TINT_BENCHMARK_WGSL_PROGRAMS(GenerateHLSL);
TINT_BENCHMARK_PROGRAMS(GenerateHLSL);
} // namespace
} // namespace tint::writer::hlsl

View File

@ -53,7 +53,7 @@ void GenerateMSL(benchmark::State& state, std::string input_name) {
}
}
TINT_BENCHMARK_WGSL_PROGRAMS(GenerateMSL);
TINT_BENCHMARK_PROGRAMS(GenerateMSL);
} // namespace
} // namespace tint::writer::msl

View File

@ -34,7 +34,7 @@ void GenerateSPIRV(benchmark::State& state, std::string input_name) {
}
}
TINT_BENCHMARK_WGSL_PROGRAMS(GenerateSPIRV);
TINT_BENCHMARK_PROGRAMS(GenerateSPIRV);
} // namespace
} // namespace tint::writer::spirv

View File

@ -34,7 +34,7 @@ void GenerateWGSL(benchmark::State& state, std::string input_name) {
}
}
TINT_BENCHMARK_WGSL_PROGRAMS(GenerateWGSL);
TINT_BENCHMARK_PROGRAMS(GenerateWGSL);
} // namespace
} // namespace tint::writer::wgsl