blob: 37b423ee460ba929dd66cd768a0a440b15a0f377 [file] [log] [blame]
Dejan Mircevskib6fe02f2016-01-07 13:44:22 -05001// Copyright (c) 2015-2016 The Khronos Group Inc.
Lei Zhang2a0b7732015-11-11 13:57:43 -05002//
David Neto9fc86582016-09-01 15:33:59 -04003// 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
Lei Zhang2a0b7732015-11-11 13:57:43 -05006//
David Neto9fc86582016-09-01 15:33:59 -04007// http://www.apache.org/licenses/LICENSE-2.0
Lei Zhang2a0b7732015-11-11 13:57:43 -05008//
David Neto9fc86582016-09-01 15:33:59 -04009// 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.
Lei Zhang2a0b7732015-11-11 13:57:43 -050014
dan sinclaireda2cfb2018-08-03 15:06:09 -040015#include "source/ext_inst.h"
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010016
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040017#include <cstring>
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010018
David Neto59de6102017-12-03 12:30:08 -050019// 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
dan sinclaireda2cfb2018-08-03 15:06:09 -040024#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"
David Netod1bb51d2016-02-17 12:17:04 -050028
dan sinclaireda2cfb2018-08-03 15:06:09 -040029#include "debuginfo.insts.inc"
30#include "glsl.std.450.insts.inc"
alan-baker50300452020-07-30 12:08:53 -040031#include "nonsemantic.clspvreflection.insts.inc"
Greg Fischer1454c952021-09-15 12:38:53 -060032#include "nonsemantic.shader.debuginfo.100.insts.inc"
David Neto64f36ea2019-12-19 17:16:26 -050033#include "opencl.debuginfo.100.insts.inc"
dan sinclaireda2cfb2018-08-03 15:06:09 -040034#include "opencl.std.insts.inc"
Lei Zhang063dbea2017-10-25 12:15:51 -040035
David Neto63f57d92019-05-07 12:27:18 -040036#include "spirv-tools/libspirv.h"
Lei Zhang063dbea2017-10-25 12:15:51 -040037#include "spv-amd-gcn-shader.insts.inc"
38#include "spv-amd-shader-ballot.insts.inc"
39#include "spv-amd-shader-explicit-vertex-parameter.insts.inc"
40#include "spv-amd-shader-trinary-minmax.insts.inc"
41
42static const spv_ext_inst_group_t kGroups_1_0[] = {
43 {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glsl_entries), glsl_entries},
44 {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(opencl_entries), opencl_entries},
45 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER,
46 ARRAY_SIZE(spv_amd_shader_explicit_vertex_parameter_entries),
47 spv_amd_shader_explicit_vertex_parameter_entries},
48 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX,
49 ARRAY_SIZE(spv_amd_shader_trinary_minmax_entries),
50 spv_amd_shader_trinary_minmax_entries},
51 {SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER,
52 ARRAY_SIZE(spv_amd_gcn_shader_entries), spv_amd_gcn_shader_entries},
53 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT,
54 ARRAY_SIZE(spv_amd_shader_ballot_entries), spv_amd_shader_ballot_entries},
David Neto59de6102017-12-03 12:30:08 -050055 {SPV_EXT_INST_TYPE_DEBUGINFO, ARRAY_SIZE(debuginfo_entries),
56 debuginfo_entries},
David Neto64f36ea2019-12-19 17:16:26 -050057 {SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100,
58 ARRAY_SIZE(opencl_debuginfo_100_entries), opencl_debuginfo_100_entries},
Greg Fischer1454c952021-09-15 12:38:53 -060059 {SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100,
60 ARRAY_SIZE(nonsemantic_shader_debuginfo_100_entries),
61 nonsemantic_shader_debuginfo_100_entries},
alan-baker50300452020-07-30 12:08:53 -040062 {SPV_EXT_INST_TYPE_NONSEMANTIC_CLSPVREFLECTION,
63 ARRAY_SIZE(nonsemantic_clspvreflection_entries),
64 nonsemantic_clspvreflection_entries},
Lei Zhang063dbea2017-10-25 12:15:51 -040065};
66
67static const spv_ext_inst_table_t kTable_1_0 = {ARRAY_SIZE(kGroups_1_0),
68 kGroups_1_0};
69
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040070spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable,
71 spv_target_env env) {
Lei Zhang40056702015-09-11 14:31:27 -040072 if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +010073
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -040074 switch (env) {
75 // The extended instruction sets are all version 1.0 so far.
76 case SPV_ENV_UNIVERSAL_1_0:
77 case SPV_ENV_VULKAN_1_0:
78 case SPV_ENV_UNIVERSAL_1_1:
David Netodbc20492017-03-14 12:43:41 -040079 case SPV_ENV_UNIVERSAL_1_2:
Pierre Moreau12447d82017-11-30 00:49:23 +010080 case SPV_ENV_OPENCL_1_2:
81 case SPV_ENV_OPENCL_EMBEDDED_1_2:
82 case SPV_ENV_OPENCL_2_0:
83 case SPV_ENV_OPENCL_EMBEDDED_2_0:
David Netoc2967012016-08-05 18:19:30 -040084 case SPV_ENV_OPENCL_2_1:
Pierre Moreau12447d82017-11-30 00:49:23 +010085 case SPV_ENV_OPENCL_EMBEDDED_2_1:
David Netoc2967012016-08-05 18:19:30 -040086 case SPV_ENV_OPENCL_2_2:
Pierre Moreau12447d82017-11-30 00:49:23 +010087 case SPV_ENV_OPENCL_EMBEDDED_2_2:
David Netoc2967012016-08-05 18:19:30 -040088 case SPV_ENV_OPENGL_4_0:
89 case SPV_ENV_OPENGL_4_1:
90 case SPV_ENV_OPENGL_4_2:
91 case SPV_ENV_OPENGL_4_3:
92 case SPV_ENV_OPENGL_4_5:
David Neto00fa3932018-02-09 14:29:02 -050093 case SPV_ENV_UNIVERSAL_1_3:
94 case SPV_ENV_VULKAN_1_1:
David Neto63f57d92019-05-07 12:27:18 -040095 case SPV_ENV_VULKAN_1_1_SPIRV_1_4:
David Neto63f57d92019-05-07 12:27:18 -040096 case SPV_ENV_UNIVERSAL_1_4:
alan-baker5a48c0d2019-09-13 14:59:02 -040097 case SPV_ENV_UNIVERSAL_1_5:
David Netod46a1b02019-08-28 18:49:33 -040098 case SPV_ENV_VULKAN_1_2:
alan-baker7d768812021-12-15 14:38:28 -050099 case SPV_ENV_UNIVERSAL_1_6:
Lei Zhang063dbea2017-10-25 12:15:51 -0400100 *pExtInstTable = &kTable_1_0;
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -0400101 return SPV_SUCCESS;
102 default:
Dejan Mircevskicb3c49e2016-04-07 14:41:34 -0400103 return SPV_ERROR_INVALID_TABLE;
104 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100105}
106
Lei Zhang1a0334e2015-11-02 09:41:20 -0500107spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
David Neto21c4ad42015-10-14 17:02:11 -0400108 // The names are specified by the respective extension instruction
109 // specifications.
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100110 if (!strcmp("GLSL.std.450", name)) {
111 return SPV_EXT_INST_TYPE_GLSL_STD_450;
112 }
David Neto21c4ad42015-10-14 17:02:11 -0400113 if (!strcmp("OpenCL.std", name)) {
114 return SPV_EXT_INST_TYPE_OPENCL_STD;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100115 }
Rex Xu5fbbadc2017-06-21 15:58:00 +0800116 if (!strcmp("SPV_AMD_shader_explicit_vertex_parameter", name)) {
117 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER;
118 }
119 if (!strcmp("SPV_AMD_shader_trinary_minmax", name)) {
120 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX;
121 }
David Neto7fe8a572017-03-21 12:43:26 -0400122 if (!strcmp("SPV_AMD_gcn_shader", name)) {
123 return SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER;
124 }
Rex Xu5fbbadc2017-06-21 15:58:00 +0800125 if (!strcmp("SPV_AMD_shader_ballot", name)) {
126 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT;
127 }
David Neto59de6102017-12-03 12:30:08 -0500128 if (!strcmp("DebugInfo", name)) {
129 return SPV_EXT_INST_TYPE_DEBUGINFO;
130 }
David Neto64f36ea2019-12-19 17:16:26 -0500131 if (!strcmp("OpenCL.DebugInfo.100", name)) {
132 return SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100;
133 }
Greg Fischer1454c952021-09-15 12:38:53 -0600134 if (!strcmp("NonSemantic.Shader.DebugInfo.100", name)) {
135 return SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100;
Greg Fischer3b6abf42021-07-12 03:51:08 -0600136 }
alan-baker50300452020-07-30 12:08:53 -0400137 if (!strncmp("NonSemantic.ClspvReflection.", name, 28)) {
138 return SPV_EXT_INST_TYPE_NONSEMANTIC_CLSPVREFLECTION;
139 }
David Netoe70b0092019-12-18 18:10:29 -0500140 // ensure to add any known non-semantic extended instruction sets
141 // above this point, and update spvExtInstIsNonSemantic()
142 if (!strncmp("NonSemantic.", name, 12)) {
143 return SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN;
144 }
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100145 return SPV_EXT_INST_TYPE_NONE;
146}
147
David Netoe70b0092019-12-18 18:10:29 -0500148bool spvExtInstIsNonSemantic(const spv_ext_inst_type_t type) {
alan-baker50300452020-07-30 12:08:53 -0400149 if (type == SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN ||
Greg Fischer1454c952021-09-15 12:38:53 -0600150 type == SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100 ||
alan-baker50300452020-07-30 12:08:53 -0400151 type == SPV_EXT_INST_TYPE_NONSEMANTIC_CLSPVREFLECTION) {
David Netoe70b0092019-12-18 18:10:29 -0500152 return true;
153 }
154 return false;
155}
156
Jaebaek Seodd37d732020-01-23 17:04:30 -0500157bool spvExtInstIsDebugInfo(const spv_ext_inst_type_t type) {
158 if (type == SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100 ||
Greg Fischer1454c952021-09-15 12:38:53 -0600159 type == SPV_EXT_INST_TYPE_NONSEMANTIC_SHADER_DEBUGINFO_100 ||
Jaebaek Seodd37d732020-01-23 17:04:30 -0500160 type == SPV_EXT_INST_TYPE_DEBUGINFO) {
161 return true;
162 }
163 return false;
164}
165
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100166spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
167 const spv_ext_inst_type_t type,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500168 const char* name,
169 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400170 if (!table) return SPV_ERROR_INVALID_TABLE;
171 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100172
173 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhangca1bf942016-04-27 16:47:13 -0400174 const auto& group = table->groups[groupIndex];
175 if (type != group.type) continue;
176 for (uint32_t index = 0; index < group.count; index++) {
177 const auto& entry = group.entries[index];
178 if (!strcmp(name, entry.name)) {
179 *pEntry = &entry;
180 return SPV_SUCCESS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100181 }
182 }
183 }
184
185 return SPV_ERROR_INVALID_LOOKUP;
186}
187
188spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
189 const spv_ext_inst_type_t type,
190 const uint32_t value,
Lei Zhang1a0334e2015-11-02 09:41:20 -0500191 spv_ext_inst_desc* pEntry) {
Lei Zhang40056702015-09-11 14:31:27 -0400192 if (!table) return SPV_ERROR_INVALID_TABLE;
193 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100194
195 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
Lei Zhangca1bf942016-04-27 16:47:13 -0400196 const auto& group = table->groups[groupIndex];
197 if (type != group.type) continue;
198 for (uint32_t index = 0; index < group.count; index++) {
199 const auto& entry = group.entries[index];
200 if (value == entry.ext_inst) {
201 *pEntry = &entry;
202 return SPV_SUCCESS;
Kenneth Benzie (Benie)83e5a292015-05-22 18:26:19 +0100203 }
204 }
205 }
206
207 return SPV_ERROR_INVALID_LOOKUP;
208}