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