val.cpp 9.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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. std::string target_env_list = spvTargetEnvList(36, 105);
  26. printf(
  27. R"(%s - Validate a SPIR-V binary file.
  28. USAGE: %s [options] [<filename>]
  29. The SPIR-V binary is read from <filename>. If no file is specified,
  30. or if the filename is "-", then the binary is read from standard input.
  31. NOTE: The validator is a work in progress.
  32. Options:
  33. -h, --help Print this help.
  34. --max-struct-members <maximum number of structure members allowed>
  35. --max-struct-depth <maximum allowed nesting depth of structures>
  36. --max-local-variables <maximum number of local variables allowed>
  37. --max-global-variables <maximum number of global variables allowed>
  38. --max-switch-branches <maximum number of branches allowed in switch statements>
  39. --max-function-args <maximum number arguments allowed per function>
  40. --max-control-flow-nesting-depth <maximum Control Flow nesting depth allowed>
  41. --max-access-chain-indexes <maximum number of indexes allowed to use for Access Chain instructions>
  42. --max-id-bound <maximum value for the id bound>
  43. --relax-logical-pointer Allow allocating an object of a pointer type and returning
  44. a pointer value from a function in logical addressing mode
  45. --relax-block-layout Enable VK_KHR_relaxed_block_layout when checking standard
  46. uniform, storage buffer, and push constant layouts.
  47. This is the default when targeting Vulkan 1.1 or later.
  48. --uniform-buffer-standard-layout Enable VK_KHR_uniform_buffer_standard_layout when checking standard
  49. uniform buffer layouts.
  50. --scalar-block-layout Enable VK_EXT_scalar_block_layout when checking standard
  51. uniform, storage buffer, and push constant layouts. Scalar layout
  52. rules are more permissive than relaxed block layout so in effect
  53. this will override the --relax-block-layout option.
  54. --workgroup-scalar-block-layout Enable scalar block layout when checking Workgroup block layouts.
  55. --skip-block-layout Skip checking standard uniform/storage buffer layout.
  56. Overrides any --relax-block-layout or --scalar-block-layout option.
  57. --relax-struct-store Allow store from one struct type to a
  58. different type with compatible layout and
  59. members.
  60. --allow-localsizeid Allow use of the LocalSizeId decoration where it would otherwise not
  61. be allowed by the target environment.
  62. --allow-offset-texture-operand Allow use of the Offset texture operands where it would otherwise not
  63. be allowed by the target environment.
  64. --allow-vulkan-32-bit-bitwise Allow use of non-32 bit for the Base operand where it would otherwise
  65. not be allowed by the target environment.
  66. --before-hlsl-legalization Allows code patterns that are intended to be
  67. fixed by spirv-opt's legalization passes.
  68. --version Display validator version information.
  69. --target-env {%s}
  70. Use validation rules from the specified environment.
  71. )",
  72. argv0, argv0, target_env_list.c_str());
  73. }
  74. int main(int argc, char** argv) {
  75. const char* inFile = nullptr;
  76. spv_target_env target_env = SPV_ENV_UNIVERSAL_1_6;
  77. spvtools::ValidatorOptions options;
  78. bool continue_processing = true;
  79. int return_code = 0;
  80. for (int argi = 1; continue_processing && argi < argc; ++argi) {
  81. const char* cur_arg = argv[argi];
  82. if ('-' == cur_arg[0]) {
  83. if (0 == strncmp(cur_arg, "--max-", 6)) {
  84. if (argi + 1 < argc) {
  85. spv_validator_limit limit_type;
  86. if (spvParseUniversalLimitsOptions(cur_arg, &limit_type)) {
  87. uint32_t limit = 0;
  88. if (sscanf(argv[++argi], "%u", &limit)) {
  89. options.SetUniversalLimit(limit_type, limit);
  90. } else {
  91. fprintf(stderr, "error: missing argument to %s\n", cur_arg);
  92. continue_processing = false;
  93. return_code = 1;
  94. }
  95. } else {
  96. fprintf(stderr, "error: unrecognized option: %s\n", cur_arg);
  97. continue_processing = false;
  98. return_code = 1;
  99. }
  100. } else {
  101. fprintf(stderr, "error: Missing argument to %s\n", cur_arg);
  102. continue_processing = false;
  103. return_code = 1;
  104. }
  105. } else if (0 == strcmp(cur_arg, "--version")) {
  106. printf("%s\n", spvSoftwareVersionDetailsString());
  107. printf(
  108. "Targets:\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n %s\n "
  109. "%s\n %s\n %s\n %s %s\n",
  110. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_0),
  111. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_1),
  112. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_2),
  113. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_3),
  114. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_4),
  115. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_5),
  116. spvTargetEnvDescription(SPV_ENV_UNIVERSAL_1_6),
  117. spvTargetEnvDescription(SPV_ENV_OPENCL_2_2),
  118. spvTargetEnvDescription(SPV_ENV_VULKAN_1_0),
  119. spvTargetEnvDescription(SPV_ENV_VULKAN_1_1),
  120. spvTargetEnvDescription(SPV_ENV_VULKAN_1_1_SPIRV_1_4),
  121. spvTargetEnvDescription(SPV_ENV_VULKAN_1_2),
  122. spvTargetEnvDescription(SPV_ENV_VULKAN_1_3),
  123. spvTargetEnvDescription(SPV_ENV_VULKAN_1_4));
  124. continue_processing = false;
  125. return_code = 0;
  126. } else if (0 == strcmp(cur_arg, "--help") || 0 == strcmp(cur_arg, "-h")) {
  127. print_usage(argv[0]);
  128. continue_processing = false;
  129. return_code = 0;
  130. } else if (0 == strcmp(cur_arg, "--target-env")) {
  131. if (argi + 1 < argc) {
  132. const auto env_str = argv[++argi];
  133. if (!spvParseTargetEnv(env_str, &target_env)) {
  134. fprintf(stderr, "error: Unrecognized target env: %s\n", env_str);
  135. continue_processing = false;
  136. return_code = 1;
  137. }
  138. } else {
  139. fprintf(stderr, "error: Missing argument to --target-env\n");
  140. continue_processing = false;
  141. return_code = 1;
  142. }
  143. } else if (0 == strcmp(cur_arg, "--before-hlsl-legalization")) {
  144. options.SetBeforeHlslLegalization(true);
  145. } else if (0 == strcmp(cur_arg, "--relax-logical-pointer")) {
  146. options.SetRelaxLogicalPointer(true);
  147. } else if (0 == strcmp(cur_arg, "--relax-block-layout")) {
  148. options.SetRelaxBlockLayout(true);
  149. } else if (0 == strcmp(cur_arg, "--uniform-buffer-standard-layout")) {
  150. options.SetUniformBufferStandardLayout(true);
  151. } else if (0 == strcmp(cur_arg, "--scalar-block-layout")) {
  152. options.SetScalarBlockLayout(true);
  153. } else if (0 == strcmp(cur_arg, "--workgroup-scalar-block-layout")) {
  154. options.SetWorkgroupScalarBlockLayout(true);
  155. } else if (0 == strcmp(cur_arg, "--skip-block-layout")) {
  156. options.SetSkipBlockLayout(true);
  157. } else if (0 == strcmp(cur_arg, "--allow-localsizeid")) {
  158. options.SetAllowLocalSizeId(true);
  159. } else if (0 == strcmp(cur_arg, "--allow-offset-texture-operand")) {
  160. options.SetAllowOffsetTextureOperand(true);
  161. } else if (0 == strcmp(cur_arg, "--allow-vulkan-32-bit-bitwise")) {
  162. options.SetAllowVulkan32BitBitwise(true);
  163. } else if (0 == strcmp(cur_arg, "--relax-struct-store")) {
  164. options.SetRelaxStructStore(true);
  165. } else if (0 == cur_arg[1]) {
  166. // Setting a filename of "-" to indicate stdin.
  167. if (!inFile) {
  168. inFile = cur_arg;
  169. } else {
  170. fprintf(stderr, "error: More than one input file specified\n");
  171. continue_processing = false;
  172. return_code = 1;
  173. }
  174. } else {
  175. print_usage(argv[0]);
  176. continue_processing = false;
  177. return_code = 1;
  178. }
  179. } else {
  180. if (!inFile) {
  181. inFile = cur_arg;
  182. } else {
  183. fprintf(stderr, "error: More than one input file specified\n");
  184. continue_processing = false;
  185. return_code = 1;
  186. }
  187. }
  188. }
  189. // Exit if command line parsing was not successful.
  190. if (!continue_processing) {
  191. return return_code;
  192. }
  193. std::vector<uint32_t> contents;
  194. if (!ReadBinaryFile(inFile, &contents)) return 1;
  195. spvtools::SpirvTools tools(target_env);
  196. tools.SetMessageConsumer(spvtools::utils::CLIMessageConsumer);
  197. bool succeed = tools.Validate(contents.data(), contents.size(), options);
  198. return !succeed;
  199. }