From c45d5b6a203aca4f732fb66db8df18c007ade11f Mon Sep 17 00:00:00 2001 From: Stuart Brady Date: Thu, 27 Jun 2019 16:16:25 +0100 Subject: [PATCH] Update command line parameters for offline compilation This change simplifies the command line interface for controlling offline compilation. It replaces special command line syntax for controlling the compilation cache for SPIR-V compilation, with generic command line parameters that can now also be used for binary offline compilation. --- test_common/harness/kernelHelpers.c | 26 ++++- test_common/harness/parseParameters.cpp | 137 +++++++++++++++++------- 2 files changed, 120 insertions(+), 43 deletions(-) diff --git a/test_common/harness/kernelHelpers.c b/test_common/harness/kernelHelpers.c index 0f3b9172..62889662 100644 --- a/test_common/harness/kernelHelpers.c +++ b/test_common/harness/kernelHelpers.c @@ -21,6 +21,7 @@ #include "testHarness.h" #include "parseParameters.h" +#include #include #include #include @@ -200,6 +201,20 @@ std::string add_build_options(const std::string &baseName, const char *options) return get_file_name(baseName, i, ""); } +static std::string get_offline_compilation_file_type_str(const CompilationMode compilationMode) +{ + switch (compilationMode) + { + default: + assert(0 && "Invalid compilation mode for offline compilation"); + abort(); + case kBinary: + return "binary"; + case kSpir_v: + return "SPIR-V"; + } +} + static cl_int get_device_address_bits(cl_context context, cl_uint &device_address_space_size) { cl_uint numDevices = 0; @@ -287,16 +302,20 @@ static int create_single_kernel_helper_create_program(cl_context context, if (gCompilationCacheMode == kCacheModeOverwrite || !ifs.good()) { + std::string file_type = get_offline_compilation_file_type_str(compilationMode); + if (gCompilationCacheMode == kCacheModeForceRead) { - log_info("OfflineCompiler: can't open cached SpirV file: %s\n", outputFilename.c_str()); + log_info("OfflineCompiler: can't open cached %s file: %s\n", + file_type.c_str(), outputFilename.c_str()); return -1; } ifs.close(); if (gCompilationCacheMode != kCacheModeOverwrite) - log_info("OfflineCompiler: can't find cached SpirV file: %s\n", outputFilename.c_str()); + log_info("OfflineCompiler: can't find cached %s file: %s\n", + file_type.c_str(), outputFilename.c_str()); std::ofstream ofs(sourceFilename.c_str(), std::ios::binary); if (!ofs.good()) @@ -361,7 +380,8 @@ static int create_single_kernel_helper_create_program(cl_context context, ifs.open(outputFilename.c_str(), std::ios::binary); if (!ifs.good()) { - log_info("OfflineCompiler: can't read output file: %s\n", outputFilename.c_str()); + log_info("OfflineCompiler: can't read generated %s file: %s\n", + file_type.c_str(), outputFilename.c_str()); return -1; } } diff --git a/test_common/harness/parseParameters.cpp b/test_common/harness/parseParameters.cpp index 2c54bb6b..2051d0b3 100644 --- a/test_common/harness/parseParameters.cpp +++ b/test_common/harness/parseParameters.cpp @@ -33,12 +33,21 @@ std::string gCompilationCachePath = "."; void helpInfo () { - log_info(" '-offlineCompiler ': use offline compiler\n"); - log_info(" ' output_type binary - \"../build_script_binary.py\" is invoked\n"); - log_info(" ' output_type spir_v - \"../cl_build_script_spir_v.py\" is invoked, optional modes: generate, cache\n"); - log_info(" ' mode generate - force binary generation\n"); - log_info(" ' mode cache - force reading binary files from cache\n"); - log_info("\n"); + log_info("Common options:\n" + " -h, --help This help\n" + " --compilation-mode Specify a compilation mode. Mode can be:\n" + " online Use online compilation (default)\n" + " binary Use binary offline compilation\n" + " spir-v Use SPIR-V offline compilation\n" + "\n" + " For offline compilation (binary and spir-v modes) only:\n" + " --compilation-cache-mode Specify a compilation caching mode:\n" + " compile-if-absent Read from cache if already populated, or\n" + " else perform offline compilation (default)\n" + " force-read Force reading from the cache\n" + " overwrite Disable reading from the cache\n" + " --compilation-cache-path Path for offline compiler output and CL source\n" + "\n"); } int parseCustomParam (int argc, const char *argv[], const char *ignore) @@ -57,64 +66,112 @@ int parseCustomParam (int argc, const char *argv[], const char *ignore) (ptr[strlen(argv[i])] == 0 || ptr[strlen(argv[i])] == ' ')) // last on list or ' ' after continue; } - if (i < 0) i = 0; - delArg = 0; - if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) - helpInfo (); - else if (!strcmp(argv[i], "-offlineCompiler")) + delArg = 0; + + if (strcmp(argv[i], "-h") == 0 || strcmp(argv[i], "--help") == 0) { - log_info(" Offline Compiler enabled\n"); - delArg = 1; + // Note: we don't increment delArg to delete this argument, + // to allow the caller's argument parsing routine to see the + // option and print its own help. + helpInfo (); + } + else if (!strcmp(argv[i], "--compilation-mode")) + { + delArg++; if ((i + 1) < argc) { - if (!strcmp(argv[i + 1], "binary")) + delArg++; + const char *mode = argv[i + 1]; + + if (!strcmp(mode, "online")) + { + gCompilationMode = kOnline; + } + else if (!strcmp(mode, "binary")) { gCompilationMode = kBinary; - delArg++; } - else if (!strcmp(argv[i + 1], "spir_v")) + else if (!strcmp(mode, "spir-v")) { gCompilationMode = kSpir_v; - delArg++; - if ((i + 3) < argc) - { - if (!strcmp(argv[i + 2], "cache")) - { - gCompilationCacheMode = kCacheModeForceRead; - gCompilationCachePath = argv[i + 3]; - log_info(" SpirV reading from cache enabled.\n"); - delArg += 2; - } - else if (!strcmp(argv[i + 2], "generate")) - { - gCompilationCacheMode = kCacheModeOverwrite; - gCompilationCachePath = argv[i + 3]; - log_info(" SpirV force generate binaries enabled.\n"); - delArg += 2; - } - } } else { - log_error(" Offline Compiler output type not supported: %s\n", argv[i + 1]); + log_error("Compilation mode not recognized: %s\n", mode); return -1; } + log_info("Compilation mode specified: %s\n", mode); } else { - log_error(" Offline Compiler parameters are incorrect. Usage:\n"); - log_error(" -offlineCompiler \n"); + log_error("Compilation mode parameters are incorrect. Usage:\n" + " --compilation-mode \n"); + return -1; + } + } + else if (!strcmp(argv[i], "--compilation-cache-mode")) + { + delArg++; + if ((i + 1) < argc) + { + delArg++; + const char *mode = argv[i + 1]; + + if (!strcmp(mode, "compile-if-absent")) + { + gCompilationCacheMode = kCacheModeCompileIfAbsent; + } + else if (!strcmp(mode, "force-read")) + { + gCompilationCacheMode = kCacheModeForceRead; + } + else if (!strcmp(mode, "overwrite")) + { + gCompilationCacheMode = kCacheModeOverwrite; + } + else + { + log_error("Compilation cache mode not recognized: %s\n", mode); + return -1; + } + log_info("Compilation cache mode specified: %s\n", mode); + } + else + { + log_error("Compilation cache mode parameters are incorrect. Usage:\n" + " --compilation-cache-mode \n"); + return -1; + } + } + else if (!strcmp(argv[i], "--compilation-cache-path")) + { + delArg++; + if ((i + 1) < argc) + { + delArg++; + gCompilationCachePath = argv[i + 1]; + } + else + { + log_error("Path argument for --compilation-cache-path was not specified.\n"); return -1; } } //cleaning parameters from argv tab - for (int j=i; j