diff --git a/include/spirv-tools/libspirv.h b/include/spirv-tools/libspirv.h index 5dcf4ff26..75de45d95 100644 --- a/include/spirv-tools/libspirv.h +++ b/include/spirv-tools/libspirv.h @@ -351,6 +351,10 @@ typedef enum { // Returns a string describing the given SPIR-V target environment. const char* spvTargetEnvDescription(spv_target_env env); +// Parses s into *env and returns true if successful. If unparsable, returns +// false and sets *env to SPV_ENV_UNIVERSAL_1_0. +bool spvParseTargetEnv(const char* s, spv_target_env* env); + // Creates a context object. Returns null if env is invalid. spv_context spvContextCreate(spv_target_env env); diff --git a/source/spirv_target_env.cpp b/source/spirv_target_env.cpp index 77137718c..116d6b5cd 100644 --- a/source/spirv_target_env.cpp +++ b/source/spirv_target_env.cpp @@ -24,7 +24,8 @@ // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. -#include +#include +#include #include "spirv-tools/libspirv.h" #include "spirv_constant.h" @@ -57,3 +58,19 @@ uint32_t spvVersionForTargetEnv(spv_target_env env) { assert(0 && "Unhandled SPIR-V target environment"); return SPV_SPIRV_VERSION_WORD(0, 0); } + +bool spvParseTargetEnv(const char* s, spv_target_env* env) { + if (!strncmp(s, "vulkan1.0", strlen("vulkan1.0"))) { + if (env) *env = SPV_ENV_VULKAN_1_0; + return true; + } else if (!strncmp(s, "spv1.0", strlen("spv1.0"))) { + if (env) *env = SPV_ENV_UNIVERSAL_1_0; + return true; + } else if (!strncmp(s, "spv1.1", strlen("spv1.1"))) { + if (env) *env = SPV_ENV_UNIVERSAL_1_1; + return true; + } else { + if (env) *env = SPV_ENV_UNIVERSAL_1_0; + return false; + } +} diff --git a/tools/val/val.cpp b/tools/val/val.cpp index d7e998a74..1ee1a0e0e 100644 --- a/tools/val/val.cpp +++ b/tools/val/val.cpp @@ -24,10 +24,9 @@ // TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE // MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS. -#include -#include -#include - +#include +#include +#include #include #include "spirv-tools/libspirv.h" @@ -46,6 +45,8 @@ NOTE: The validator is a work in progress. Options: -h, --help Print this help. --version Display validator version information. + --target-env {vulkan1.0|spv1.0|spv1.1} + Use Vulkan/SPIR-V1.0/SPIR-V1.1 validation rules. )", argv0, argv0); } @@ -66,8 +67,17 @@ int main(int argc, char** argv) { } else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) { print_usage(argv[0]); return 0; - } else if (0 == strcmp(cur_arg, "--vulkan")) { - target_env = SPV_ENV_VULKAN_1_0; + } else if (0 == strcmp(cur_arg, "--target-env")) { + if (argi + 1 < argc) { + const auto env_str = argv[++argi]; + if (!spvParseTargetEnv(env_str, &target_env)) { + fprintf(stderr, "error: Unrecognized target env: %s\n", env_str); + return 1; + } + } else { + fprintf(stderr, "error: Missing argument to --target-env\n"); + return 1; + } } else if (0 == cur_arg[1]) { // Setting a filename of "-" to indicate stdin. if (!inFile) {