val.cpp 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. // Copyright (c) 2015-2016 The Khronos Group Inc.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include <cassert>
  15. #include <cstdio>
  16. #include <cstring>
  17. #include <iostream>
  18. #include <vector>
  19. #include "source/spirv_target_env.h"
  20. #include "source/spirv_validator_options.h"
  21. #include "spirv-tools/libspirv.hpp"
  22. #include "tools/io.h"
  23. #include "tools/util/cli_consumer.h"
  24. void print_usage(char* argv0) {
  25. printf(
  26. R"(%s - Validate a SPIR-V binary file.
  27. USAGE: %s [options] [<filename>]
  28. The SPIR-V binary is read from <filename>. If no file is specified,
  29. or if the filename is "-", then the binary is read from standard input.
  30. NOTE: The validator is a work in progress.
  31. Options:
  32. -h, --help Print this help.
  33. --max-struct-members <maximum number of structure members allowed>
  34. --max-struct-depth <maximum allowed nesting depth of structures>
  35. --max-local-variables <maximum number of local variables allowed>
  36. --max-global-variables <maximum number of global variables allowed>
  37. --max-switch-branches <maximum number of branches allowed in switch statements>
  38. --max-function-args <maximum number arguments allowed per function>
  39. --max-control-flow-nesting-depth <maximum Control Flow nesting depth allowed>
  40. --max-access-chain-indexes <maximum number of indexes allowed to use for Access Chain instructions>
  41. --max-id-bound <maximum value for the id bound>
  42. --relax-logical-pointer Allow allocating an object of a pointer type and returning
  43. a pointer value from a function in logical addressing mode
  44. --relax-block-layout Enable VK_KHR_relaxed_block_layout when checking standard
  45. uniform, storage buffer, and push constant layouts.
  46. This is the default when targeting Vulkan 1.1 or later.
  47. --scalar-block-layout Enable VK_EXT_scalar_block_layout when checking standard
  48. uniform, storage buffer, and push constant layouts. Scalar layout
  49. rules are more permissive than relaxed block layout so in effect
  50. this will override the --relax-block-layout option.
  51. --skip-block-layout Skip checking standard uniform/storage buffer layout.
  52. Overrides any --relax-block-layout or --scalar-block-layout option.
  53. --relax-struct-store Allow store from one struct type to a
  54. different type with compatible layout and
  55. members.
  56. --version Display validator version information.
  57. --target-env {vulkan1.0|vulkan1.1|opencl2.2|spv1.0|spv1.1|spv1.2|spv1.3|webgpu0}
  58. Use Vulkan 1.0, Vulkan 1.1, OpenCL 2.2, SPIR-V 1.0,
  59. SPIR-V 1.1, SPIR-V 1.2, SPIR-V 1.3 or WIP WebGPU validation rules.
  60. )",
  61. argv0, argv0);
  62. }
  63. int main(int argc, char** argv) {
  64. const char* inFile = nullptr;
  65. spv_target_env target_env = SPV_ENV_UNIVERSAL_1_3;
  66. spvtools::ValidatorOptions options;
  67. bool continue_processing = true;
  68. int return_code = 0;
  69. for (int argi = 1; continue_processing && argi < argc; ++argi) {
  70. const char* cur_arg = argv[argi];
  71. if ('-' == cur_arg[0]) {
  72. if (0 == strncmp(cur_arg, "--max-", 6)) {
  73. if (argi + 1 < argc) {
  74. spv_validator_limit limit_type;
  75. if (spvParseUniversalLimitsOptions(cur_arg, &limit_type)) {
  76. uint32_t limit = 0;
  77. if (sscanf(argv[++argi], "%u", &limit)) {
  78. options.SetUniversalLimit(limit_type, limit);
  79. } else {
  80. fprintf(stderr, "error: missing argument to %s\n", cur_arg);
  81. continue_processing = false;
  82. return_code = 1;
  83. }
  84. } else {
  85. fprintf(stderr, "error: unrecognized option: %s\n", cur_arg);
  86. continue_processing = false;
  87. return_code = 1;
  88. }
  89. } else {
  90. fprintf(stderr, "error: Missing argument to %s\n", cur_arg);
  91. continue_processing = false;
  92. return_code = 1;
  93. }
  94. } else if (0 == strcmp(cur_arg, "--version")) {
  95. printf("%s\n", spvSoftwareVersionDetailsString());
  96. printf("Targets:\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n",
  97. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_0),
  98. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_1),
  99. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_2),
  100. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_3),
  101. spvTargetEnvDescription(SPV_ENV_OPENCL_2_2),
  102. spvTargetEnvDescription(SPV_ENV_VULKAN_1_0),
  103. spvTargetEnvDescription(SPV_ENV_VULKAN_1_1),
  104. spvTargetEnvDescription(SPV_ENV_WEBGPU_0));
  105. continue_processing = false;
  106. return_code = 0;
  107. } else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) {
  108. print_usage(argv[0]);
  109. continue_processing = false;
  110. return_code = 0;
  111. } else if (0 == strcmp(cur_arg, "--target-env")) {
  112. if (argi + 1 < argc) {
  113. const auto env_str = argv[++argi];
  114. if (!spvParseTargetEnv(env_str, &target_env)) {
  115. fprintf(stderr, "error: Unrecognized target env: %s\n", env_str);
  116. continue_processing = false;
  117. return_code = 1;
  118. }
  119. } else {
  120. fprintf(stderr, "error: Missing argument to --target-env\n");
  121. continue_processing = false;
  122. return_code = 1;
  123. }
  124. } else if (0 == strcmp(cur_arg, "--relax-logical-pointer")) {
  125. options.SetRelaxLogicalPointer(true);
  126. } else if (0 == strcmp(cur_arg, "--relax-block-layout")) {
  127. options.SetRelaxBlockLayout(true);
  128. } else if (0 == strcmp(cur_arg, "--scalar-block-layout")) {
  129. options.SetScalarBlockLayout(true);
  130. } else if (0 == strcmp(cur_arg, "--skip-block-layout")) {
  131. options.SetSkipBlockLayout(true);
  132. } else if (0 == strcmp(cur_arg, "--relax-struct-store")) {
  133. options.SetRelaxStructStore(true);
  134. } else if (0 == cur_arg[1]) {
  135. // Setting a filename of "-" to indicate stdin.
  136. if (!inFile) {
  137. inFile = cur_arg;
  138. } else {
  139. fprintf(stderr, "error: More than one input file specified\n");
  140. continue_processing = false;
  141. return_code = 1;
  142. }
  143. } else {
  144. print_usage(argv[0]);
  145. continue_processing = false;
  146. return_code = 1;
  147. }
  148. } else {
  149. if (!inFile) {
  150. inFile = cur_arg;
  151. } else {
  152. fprintf(stderr, "error: More than one input file specified\n");
  153. continue_processing = false;
  154. return_code = 1;
  155. }
  156. }
  157. }
  158. // Exit if command line parsing was not successful.
  159. if (!continue_processing) {
  160. return return_code;
  161. }
  162. std::vector<uint32_t> contents;
  163. if (!ReadFile<uint32_t>(inFile, "rb", &contents)) return 1;
  164. spvtools::SpirvTools tools(target_env);
  165. tools.SetMessageConsumer(spvtools::utils::CLIMessageConsumer);
  166. bool succeed = tools.Validate(contents.data(), contents.size(), options);
  167. return !succeed;
  168. }