From 277d029608ed0f7fdb0823f010d653dd0169c82c Mon Sep 17 00:00:00 2001 From: Stuart Brady Date: Fri, 11 Jun 2021 09:42:20 +0100 Subject: [PATCH] Run spirv-val for SPIR-V offline compilation (#1108) The common --disable-spirv-validation option has been added to disable this functionality. Signed-off-by: Stuart Brady --- test_common/harness/kernelHelpers.cpp | 22 ++++++++++++++++- test_common/harness/parseParameters.cpp | 32 ++++++++++++++++++++++++- test_common/harness/parseParameters.h | 2 ++ 3 files changed, 54 insertions(+), 2 deletions(-) diff --git a/test_common/harness/kernelHelpers.cpp b/test_common/harness/kernelHelpers.cpp index aaf0d689..18f51cbe 100644 --- a/test_common/harness/kernelHelpers.cpp +++ b/test_common/harness/kernelHelpers.cpp @@ -530,7 +530,7 @@ static int get_offline_compiler_output( sourceFilename, outputFilename); if (error != CL_SUCCESS) return error; - // read output file + // open output file for reading ifs.open(outputFilename.c_str(), std::ios::binary); if (!ifs.good()) { @@ -540,6 +540,26 @@ static int get_offline_compiler_output( } } } + + if (compilationMode == kSpir_v && !gDisableSPIRVValidation) + { + std::string runString = gSPIRVValidator + " " + outputFilename; + + int returnCode = system(runString.c_str()); + if (returnCode == -1) + { + log_error("Error: failed to invoke SPIR-V validator\n"); + return CL_COMPILE_PROGRAM_FAILURE; + } + else if (returnCode != 0) + { + log_error( + "Failed to validate SPIR-V file %s: system() returned 0x%x\n", + outputFilename.c_str(), returnCode); + return CL_COMPILE_PROGRAM_FAILURE; + } + } + return CL_SUCCESS; } diff --git a/test_common/harness/parseParameters.cpp b/test_common/harness/parseParameters.cpp index b2ab5b02..e946d744 100644 --- a/test_common/harness/parseParameters.cpp +++ b/test_common/harness/parseParameters.cpp @@ -28,11 +28,14 @@ using namespace std; #define DEFAULT_COMPILATION_PROGRAM "cl_offline_compiler" +#define DEFAULT_SPIRV_VALIDATOR "spirv-val" CompilationMode gCompilationMode = kOnline; CompilationCacheMode gCompilationCacheMode = kCacheModeCompileIfAbsent; std::string gCompilationCachePath = "."; std::string gCompilationProgram = DEFAULT_COMPILATION_PROGRAM; +bool gDisableSPIRVValidation = false; +std::string gSPIRVValidator = DEFAULT_SPIRV_VALIDATOR; void helpInfo() { @@ -62,7 +65,14 @@ For offline compilation (binary and spir-v modes) only: Path for offline compiler output and CL source --compilation-program Program to use for offline compilation, defaults to: - )" DEFAULT_COMPILATION_PROGRAM "\n\n"); + )" DEFAULT_COMPILATION_PROGRAM R"( + +For spir-v mode only: + --disable-spirv-validation + Disable validation of SPIR-V using the SPIR-V validator + --spirv-validator + Path for SPIR-V validator, defaults to )" DEFAULT_SPIRV_VALIDATOR "\n" + "\n"); } int parseCustomParam(int argc, const char *argv[], const char *ignore) @@ -198,6 +208,26 @@ int parseCustomParam(int argc, const char *argv[], const char *ignore) return -1; } } + else if (!strcmp(argv[i], "--disable-spirv-validation")) + { + delArg++; + gDisableSPIRVValidation = true; + } + else if (!strcmp(argv[i], "--spirv-validator")) + { + delArg++; + if ((i + 1) < argc) + { + delArg++; + gSPIRVValidator = argv[i + 1]; + } + else + { + log_error("Program argument for --spirv-validator was not " + "specified.\n"); + return -1; + } + } // cleaning parameters from argv tab for (int j = i; j < argc - delArg; j++) argv[j] = argv[j + delArg]; diff --git a/test_common/harness/parseParameters.h b/test_common/harness/parseParameters.h index b0f8328a..437e12f9 100644 --- a/test_common/harness/parseParameters.h +++ b/test_common/harness/parseParameters.h @@ -38,6 +38,8 @@ extern CompilationMode gCompilationMode; extern CompilationCacheMode gCompilationCacheMode; extern std::string gCompilationCachePath; extern std::string gCompilationProgram; +extern bool gDisableSPIRVValidation; +extern std::string gSPIRVValidator; extern int parseCustomParam(int argc, const char *argv[], const char *ignore = 0);