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