2016-01-07 18:44:22 +00:00
|
|
|
// Copyright (c) 2015-2016 The Khronos Group Inc.
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
2015-05-22 17:26:19 +00:00
|
|
|
//
|
2016-09-01 19:33:59 +00:00
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2016-05-11 13:48:52 +00:00
|
|
|
#include <cassert>
|
|
|
|
#include <cstdio>
|
|
|
|
#include <cstring>
|
2017-03-16 19:19:51 +00:00
|
|
|
#include <iostream>
|
2015-05-22 17:26:19 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2016-06-30 18:24:04 +00:00
|
|
|
#include "source/spirv_target_env.h"
|
2017-02-15 18:29:33 +00:00
|
|
|
#include "source/spirv_validator_options.h"
|
2017-03-16 19:19:51 +00:00
|
|
|
#include "spirv-tools/libspirv.hpp"
|
2016-06-30 18:24:04 +00:00
|
|
|
#include "tools/io.h"
|
2018-08-08 15:16:19 +00:00
|
|
|
#include "tools/util/cli_consumer.h"
|
2015-11-11 17:45:23 +00:00
|
|
|
|
2015-11-02 14:41:20 +00:00
|
|
|
void print_usage(char* argv0) {
|
2019-06-20 13:41:28 +00:00
|
|
|
std::string target_env_list = spvTargetEnvList(36, 105);
|
2015-05-22 17:26:19 +00:00
|
|
|
printf(
|
2016-02-18 18:40:56 +00:00
|
|
|
R"(%s - Validate a SPIR-V binary file.
|
|
|
|
|
|
|
|
USAGE: %s [options] [<filename>]
|
|
|
|
|
|
|
|
The SPIR-V binary is read from <filename>. If no file is specified,
|
|
|
|
or if the filename is "-", then the binary is read from standard input.
|
|
|
|
|
2016-02-22 21:43:53 +00:00
|
|
|
NOTE: The validator is a work in progress.
|
|
|
|
|
2016-02-18 18:40:56 +00:00
|
|
|
Options:
|
2017-02-15 18:29:33 +00:00
|
|
|
-h, --help Print this help.
|
|
|
|
--max-struct-members <maximum number of structure members allowed>
|
|
|
|
--max-struct-depth <maximum allowed nesting depth of structures>
|
|
|
|
--max-local-variables <maximum number of local variables allowed>
|
|
|
|
--max-global-variables <maximum number of global variables allowed>
|
|
|
|
--max-switch-branches <maximum number of branches allowed in switch statements>
|
|
|
|
--max-function-args <maximum number arguments allowed per function>
|
|
|
|
--max-control-flow-nesting-depth <maximum Control Flow nesting depth allowed>
|
|
|
|
--max-access-chain-indexes <maximum number of indexes allowed to use for Access Chain instructions>
|
2018-11-06 16:30:19 +00:00
|
|
|
--max-id-bound <maximum value for the id bound>
|
2018-05-17 07:49:19 +00:00
|
|
|
--relax-logical-pointer Allow allocating an object of a pointer type and returning
|
2018-01-07 15:50:01 +00:00
|
|
|
a pointer value from a function in logical addressing mode
|
2018-10-10 00:43:09 +00:00
|
|
|
--relax-block-layout Enable VK_KHR_relaxed_block_layout when checking standard
|
|
|
|
uniform, storage buffer, and push constant layouts.
|
|
|
|
This is the default when targeting Vulkan 1.1 or later.
|
2019-05-08 22:01:10 +00:00
|
|
|
--uniform-buffer-standard-layout Enable VK_KHR_uniform_buffer_standard_layout when checking standard
|
|
|
|
uniform buffer layouts.
|
2018-10-10 00:43:09 +00:00
|
|
|
--scalar-block-layout Enable VK_EXT_scalar_block_layout when checking standard
|
|
|
|
uniform, storage buffer, and push constant layouts. Scalar layout
|
|
|
|
rules are more permissive than relaxed block layout so in effect
|
|
|
|
this will override the --relax-block-layout option.
|
2021-01-28 00:38:38 +00:00
|
|
|
--workgroup-scalar-block-layout Enable scalar block layout when checking Workgroup block layouts.
|
2018-10-10 00:43:09 +00:00
|
|
|
--skip-block-layout Skip checking standard uniform/storage buffer layout.
|
|
|
|
Overrides any --relax-block-layout or --scalar-block-layout option.
|
2017-10-24 19:13:13 +00:00
|
|
|
--relax-struct-store Allow store from one struct type to a
|
|
|
|
different type with compatible layout and
|
|
|
|
members.
|
2021-08-26 18:33:19 +00:00
|
|
|
--allow-localsizeid Allow use of the LocalSizeId decoration where it would otherwise not
|
|
|
|
be allowed by the target environment.
|
2019-05-13 17:48:17 +00:00
|
|
|
--before-hlsl-legalization Allows code patterns that are intended to be
|
|
|
|
fixed by spirv-opt's legalization passes.
|
2017-02-15 18:29:33 +00:00
|
|
|
--version Display validator version information.
|
2019-06-20 13:41:28 +00:00
|
|
|
--target-env {%s}
|
|
|
|
Use validation rules from the specified environment.
|
2016-02-18 18:40:56 +00:00
|
|
|
)",
|
2019-06-20 13:41:28 +00:00
|
|
|
argv0, argv0, target_env_list.c_str());
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
|
2015-11-02 14:41:20 +00:00
|
|
|
int main(int argc, char** argv) {
|
|
|
|
const char* inFile = nullptr;
|
2021-12-15 19:38:28 +00:00
|
|
|
spv_target_env target_env = SPV_ENV_UNIVERSAL_1_6;
|
2017-03-16 19:19:51 +00:00
|
|
|
spvtools::ValidatorOptions options;
|
2017-02-15 18:29:33 +00:00
|
|
|
bool continue_processing = true;
|
|
|
|
int return_code = 0;
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2017-02-15 18:29:33 +00:00
|
|
|
for (int argi = 1; continue_processing && argi < argc; ++argi) {
|
2016-02-18 23:04:11 +00:00
|
|
|
const char* cur_arg = argv[argi];
|
|
|
|
if ('-' == cur_arg[0]) {
|
2017-02-15 18:29:33 +00:00
|
|
|
if (0 == strncmp(cur_arg, "--max-", 6)) {
|
|
|
|
if (argi + 1 < argc) {
|
|
|
|
spv_validator_limit limit_type;
|
|
|
|
if (spvParseUniversalLimitsOptions(cur_arg, &limit_type)) {
|
|
|
|
uint32_t limit = 0;
|
2018-03-21 09:15:56 +00:00
|
|
|
if (sscanf(argv[++argi], "%u", &limit)) {
|
2017-03-16 19:19:51 +00:00
|
|
|
options.SetUniversalLimit(limit_type, limit);
|
2017-02-15 18:29:33 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: missing argument to %s\n", cur_arg);
|
|
|
|
continue_processing = false;
|
|
|
|
return_code = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: unrecognized option: %s\n", cur_arg);
|
|
|
|
continue_processing = false;
|
|
|
|
return_code = 1;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: Missing argument to %s\n", cur_arg);
|
|
|
|
continue_processing = false;
|
|
|
|
return_code = 1;
|
|
|
|
}
|
|
|
|
} else if (0 == strcmp(cur_arg, "--version")) {
|
2016-04-22 00:50:11 +00:00
|
|
|
printf("%s\n", spvSoftwareVersionDetailsString());
|
2019-05-07 16:27:18 +00:00
|
|
|
printf(
|
|
|
|
"Targets:\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n "
|
2022-01-25 15:36:08 +00:00
|
|
|
"%s\n %s\n %s\n %s\n",
|
2019-05-07 16:27:18 +00:00
|
|
|
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_0),
|
|
|
|
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_1),
|
|
|
|
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_2),
|
|
|
|
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_3),
|
|
|
|
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_4),
|
2019-09-13 18:59:02 +00:00
|
|
|
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_5),
|
2021-12-15 19:38:28 +00:00
|
|
|
spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_6),
|
2019-05-07 16:27:18 +00:00
|
|
|
spvTargetEnvDescription(SPV_ENV_OPENCL_2_2),
|
|
|
|
spvTargetEnvDescription(SPV_ENV_VULKAN_1_0),
|
|
|
|
spvTargetEnvDescription(SPV_ENV_VULKAN_1_1),
|
2021-12-15 19:38:28 +00:00
|
|
|
spvTargetEnvDescription(SPV_ENV_VULKAN_1_1_SPIRV_1_4),
|
2022-01-25 15:36:08 +00:00
|
|
|
spvTargetEnvDescription(SPV_ENV_VULKAN_1_2),
|
|
|
|
spvTargetEnvDescription(SPV_ENV_VULKAN_1_3));
|
2017-02-15 18:29:33 +00:00
|
|
|
continue_processing = false;
|
|
|
|
return_code = 0;
|
2016-03-17 18:04:32 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) {
|
|
|
|
print_usage(argv[0]);
|
2017-02-15 18:29:33 +00:00
|
|
|
continue_processing = false;
|
|
|
|
return_code = 0;
|
2016-05-11 13:48:52 +00:00
|
|
|
} 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);
|
2017-02-15 18:29:33 +00:00
|
|
|
continue_processing = false;
|
|
|
|
return_code = 1;
|
2016-05-11 13:48:52 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: Missing argument to --target-env\n");
|
2017-02-15 18:29:33 +00:00
|
|
|
continue_processing = false;
|
|
|
|
return_code = 1;
|
2016-05-11 13:48:52 +00:00
|
|
|
}
|
2019-05-13 17:48:17 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--before-hlsl-legalization")) {
|
|
|
|
options.SetBeforeHlslLegalization(true);
|
2018-01-07 15:50:01 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--relax-logical-pointer")) {
|
|
|
|
options.SetRelaxLogicalPointer(true);
|
2018-05-17 07:49:19 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--relax-block-layout")) {
|
|
|
|
options.SetRelaxBlockLayout(true);
|
2019-05-08 22:01:10 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--uniform-buffer-standard-layout")) {
|
|
|
|
options.SetUniformBufferStandardLayout(true);
|
2018-10-10 00:43:09 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--scalar-block-layout")) {
|
|
|
|
options.SetScalarBlockLayout(true);
|
2021-01-28 00:38:38 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--workgroup-scalar-block-layout")) {
|
|
|
|
options.SetWorkgroupScalarBlockLayout(true);
|
2018-07-11 20:53:19 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--skip-block-layout")) {
|
|
|
|
options.SetSkipBlockLayout(true);
|
2021-08-26 18:33:19 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--allow-localsizeid")) {
|
|
|
|
options.SetAllowLocalSizeId(true);
|
2017-10-24 19:13:13 +00:00
|
|
|
} else if (0 == strcmp(cur_arg, "--relax-struct-store")) {
|
|
|
|
options.SetRelaxStructStore(true);
|
2016-02-18 23:04:11 +00:00
|
|
|
} else if (0 == cur_arg[1]) {
|
2016-02-18 18:40:56 +00:00
|
|
|
// Setting a filename of "-" to indicate stdin.
|
|
|
|
if (!inFile) {
|
2016-02-18 23:04:11 +00:00
|
|
|
inFile = cur_arg;
|
2016-02-18 18:40:56 +00:00
|
|
|
} else {
|
|
|
|
fprintf(stderr, "error: More than one input file specified\n");
|
2017-02-15 18:29:33 +00:00
|
|
|
continue_processing = false;
|
|
|
|
return_code = 1;
|
2016-02-18 18:40:56 +00:00
|
|
|
}
|
2015-05-22 17:26:19 +00:00
|
|
|
} else {
|
|
|
|
print_usage(argv[0]);
|
2017-02-15 18:29:33 +00:00
|
|
|
continue_processing = false;
|
|
|
|
return_code = 1;
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if (!inFile) {
|
2016-02-18 23:04:11 +00:00
|
|
|
inFile = cur_arg;
|
2015-05-22 17:26:19 +00:00
|
|
|
} else {
|
2016-02-18 18:40:56 +00:00
|
|
|
fprintf(stderr, "error: More than one input file specified\n");
|
2017-02-15 18:29:33 +00:00
|
|
|
continue_processing = false;
|
|
|
|
return_code = 1;
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-02-15 18:29:33 +00:00
|
|
|
// Exit if command line parsing was not successful.
|
|
|
|
if (!continue_processing) {
|
|
|
|
return return_code;
|
|
|
|
}
|
|
|
|
|
2015-05-22 17:26:19 +00:00
|
|
|
std::vector<uint32_t> contents;
|
2021-03-01 14:38:49 +00:00
|
|
|
if (!ReadBinaryFile<uint32_t>(inFile, &contents)) return 1;
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2017-03-16 19:19:51 +00:00
|
|
|
spvtools::SpirvTools tools(target_env);
|
2018-08-08 15:16:19 +00:00
|
|
|
tools.SetMessageConsumer(spvtools::utils::CLIMessageConsumer);
|
2017-02-15 18:29:33 +00:00
|
|
|
|
2017-03-16 19:19:51 +00:00
|
|
|
bool succeed = tools.Validate(contents.data(), contents.size(), options);
|
2015-05-22 17:26:19 +00:00
|
|
|
|
2017-03-16 19:19:51 +00:00
|
|
|
return !succeed;
|
2015-05-22 17:26:19 +00:00
|
|
|
}
|