ext_inst.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192
  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 "source/ext_inst.h"
  15. #include <cstring>
  16. // DebugInfo extended instruction set.
  17. // See https://www.khronos.org/registry/spir-v/specs/1.0/DebugInfo.html
  18. // TODO(dneto): DebugInfo.h should probably move to SPIRV-Headers.
  19. #include "DebugInfo.h"
  20. #include "source/latest_version_glsl_std_450_header.h"
  21. #include "source/latest_version_opencl_std_header.h"
  22. #include "source/macro.h"
  23. #include "source/spirv_definition.h"
  24. #include "debuginfo.insts.inc"
  25. #include "glsl.std.450.insts.inc"
  26. #include "opencl.debuginfo.100.insts.inc"
  27. #include "opencl.std.insts.inc"
  28. #include "spirv-tools/libspirv.h"
  29. #include "spv-amd-gcn-shader.insts.inc"
  30. #include "spv-amd-shader-ballot.insts.inc"
  31. #include "spv-amd-shader-explicit-vertex-parameter.insts.inc"
  32. #include "spv-amd-shader-trinary-minmax.insts.inc"
  33. static const spv_ext_inst_group_t kGroups_1_0[] = {
  34. {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glsl_entries), glsl_entries},
  35. {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(opencl_entries), opencl_entries},
  36. {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER,
  37. ARRAY_SIZE(spv_amd_shader_explicit_vertex_parameter_entries),
  38. spv_amd_shader_explicit_vertex_parameter_entries},
  39. {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX,
  40. ARRAY_SIZE(spv_amd_shader_trinary_minmax_entries),
  41. spv_amd_shader_trinary_minmax_entries},
  42. {SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER,
  43. ARRAY_SIZE(spv_amd_gcn_shader_entries), spv_amd_gcn_shader_entries},
  44. {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT,
  45. ARRAY_SIZE(spv_amd_shader_ballot_entries), spv_amd_shader_ballot_entries},
  46. {SPV_EXT_INST_TYPE_DEBUGINFO, ARRAY_SIZE(debuginfo_entries),
  47. debuginfo_entries},
  48. {SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100,
  49. ARRAY_SIZE(opencl_debuginfo_100_entries), opencl_debuginfo_100_entries},
  50. };
  51. static const spv_ext_inst_table_t kTable_1_0 = {ARRAY_SIZE(kGroups_1_0),
  52. kGroups_1_0};
  53. spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable,
  54. spv_target_env env) {
  55. if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
  56. switch (env) {
  57. // The extended instruction sets are all version 1.0 so far.
  58. case SPV_ENV_UNIVERSAL_1_0:
  59. case SPV_ENV_VULKAN_1_0:
  60. case SPV_ENV_UNIVERSAL_1_1:
  61. case SPV_ENV_UNIVERSAL_1_2:
  62. case SPV_ENV_OPENCL_1_2:
  63. case SPV_ENV_OPENCL_EMBEDDED_1_2:
  64. case SPV_ENV_OPENCL_2_0:
  65. case SPV_ENV_OPENCL_EMBEDDED_2_0:
  66. case SPV_ENV_OPENCL_2_1:
  67. case SPV_ENV_OPENCL_EMBEDDED_2_1:
  68. case SPV_ENV_OPENCL_2_2:
  69. case SPV_ENV_OPENCL_EMBEDDED_2_2:
  70. case SPV_ENV_OPENGL_4_0:
  71. case SPV_ENV_OPENGL_4_1:
  72. case SPV_ENV_OPENGL_4_2:
  73. case SPV_ENV_OPENGL_4_3:
  74. case SPV_ENV_OPENGL_4_5:
  75. case SPV_ENV_UNIVERSAL_1_3:
  76. case SPV_ENV_VULKAN_1_1:
  77. case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
  78. case SPV_ENV_WEBGPU_0:
  79. case SPV_ENV_UNIVERSAL_1_4:
  80. case SPV_ENV_UNIVERSAL_1_5:
  81. case SPV_ENV_VULKAN_1_2:
  82. *pExtInstTable = &kTable_1_0;
  83. return SPV_SUCCESS;
  84. default:
  85. return SPV_ERROR_INVALID_TABLE;
  86. }
  87. }
  88. spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
  89. // The names are specified by the respective extension instruction
  90. // specifications.
  91. if (!strcmp("GLSL.std.450", name)) {
  92. return SPV_EXT_INST_TYPE_GLSL_STD_450;
  93. }
  94. if (!strcmp("OpenCL.std", name)) {
  95. return SPV_EXT_INST_TYPE_OPENCL_STD;
  96. }
  97. if (!strcmp("SPV_AMD_shader_explicit_vertex_parameter", name)) {
  98. return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER;
  99. }
  100. if (!strcmp("SPV_AMD_shader_trinary_minmax", name)) {
  101. return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX;
  102. }
  103. if (!strcmp("SPV_AMD_gcn_shader", name)) {
  104. return SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER;
  105. }
  106. if (!strcmp("SPV_AMD_shader_ballot", name)) {
  107. return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT;
  108. }
  109. if (!strcmp("DebugInfo", name)) {
  110. return SPV_EXT_INST_TYPE_DEBUGINFO;
  111. }
  112. if (!strcmp("OpenCL.DebugInfo.100", name)) {
  113. return SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100;
  114. }
  115. // ensure to add any known non-semantic extended instruction sets
  116. // above this point, and update spvExtInstIsNonSemantic()
  117. if (!strncmp("NonSemantic.", name, 12)) {
  118. return SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN;
  119. }
  120. return SPV_EXT_INST_TYPE_NONE;
  121. }
  122. bool spvExtInstIsNonSemantic(const spv_ext_inst_type_t type) {
  123. if (type == SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN) {
  124. return true;
  125. }
  126. return false;
  127. }
  128. bool spvExtInstIsDebugInfo(const spv_ext_inst_type_t type) {
  129. if (type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 ||
  130. type == SPV_EXT_INST_TYPE_DEBUGINFO) {
  131. return true;
  132. }
  133. return false;
  134. }
  135. spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
  136. const spv_ext_inst_type_t type,
  137. const char* name,
  138. spv_ext_inst_desc* pEntry) {
  139. if (!table) return SPV_ERROR_INVALID_TABLE;
  140. if (!pEntry) return SPV_ERROR_INVALID_POINTER;
  141. for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
  142. const auto& group = table->groups[groupIndex];
  143. if (type != group.type) continue;
  144. for (uint32_t index = 0; index < group.count; index++) {
  145. const auto& entry = group.entries[index];
  146. if (!strcmp(name, entry.name)) {
  147. *pEntry = &entry;
  148. return SPV_SUCCESS;
  149. }
  150. }
  151. }
  152. return SPV_ERROR_INVALID_LOOKUP;
  153. }
  154. spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
  155. const spv_ext_inst_type_t type,
  156. const uint32_t value,
  157. spv_ext_inst_desc* pEntry) {
  158. if (!table) return SPV_ERROR_INVALID_TABLE;
  159. if (!pEntry) return SPV_ERROR_INVALID_POINTER;
  160. for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
  161. const auto& group = table->groups[groupIndex];
  162. if (type != group.type) continue;
  163. for (uint32_t index = 0; index < group.count; index++) {
  164. const auto& entry = group.entries[index];
  165. if (value == entry.ext_inst) {
  166. *pEntry = &entry;
  167. return SPV_SUCCESS;
  168. }
  169. }
  170. }
  171. return SPV_ERROR_INVALID_LOOKUP;
  172. }