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 "opencl.std.insts.inc"
32
33 #include "spv-amd-gcn-shader.insts.inc"
34 #include "spv-amd-shader-ballot.insts.inc"
35 #include "spv-amd-shader-explicit-vertex-parameter.insts.inc"
36 #include "spv-amd-shader-trinary-minmax.insts.inc"
37
38 static const spv_ext_inst_group_t kGroups_1_0[] = {
39 {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glsl_entries), glsl_entries},
40 {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(opencl_entries), opencl_entries},
41 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER,
42 ARRAY_SIZE(spv_amd_shader_explicit_vertex_parameter_entries),
43 spv_amd_shader_explicit_vertex_parameter_entries},
44 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX,
45 ARRAY_SIZE(spv_amd_shader_trinary_minmax_entries),
46 spv_amd_shader_trinary_minmax_entries},
47 {SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER,
48 ARRAY_SIZE(spv_amd_gcn_shader_entries), spv_amd_gcn_shader_entries},
49 {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT,
50 ARRAY_SIZE(spv_amd_shader_ballot_entries), spv_amd_shader_ballot_entries},
51 {SPV_EXT_INST_TYPE_DEBUGINFO, ARRAY_SIZE(debuginfo_entries),
52 debuginfo_entries},
53 };
54
55 static const spv_ext_inst_table_t kTable_1_0 = {ARRAY_SIZE(kGroups_1_0),
56 kGroups_1_0};
57
spvExtInstTableGet(spv_ext_inst_table * pExtInstTable,spv_target_env env)58 spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable,
59 spv_target_env env) {
60 if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER;
61
62 switch (env) {
63 // The extended instruction sets are all version 1.0 so far.
64 case SPV_ENV_UNIVERSAL_1_0:
65 case SPV_ENV_VULKAN_1_0:
66 case SPV_ENV_UNIVERSAL_1_1:
67 case SPV_ENV_UNIVERSAL_1_2:
68 case SPV_ENV_OPENCL_1_2:
69 case SPV_ENV_OPENCL_EMBEDDED_1_2:
70 case SPV_ENV_OPENCL_2_0:
71 case SPV_ENV_OPENCL_EMBEDDED_2_0:
72 case SPV_ENV_OPENCL_2_1:
73 case SPV_ENV_OPENCL_EMBEDDED_2_1:
74 case SPV_ENV_OPENCL_2_2:
75 case SPV_ENV_OPENCL_EMBEDDED_2_2:
76 case SPV_ENV_OPENGL_4_0:
77 case SPV_ENV_OPENGL_4_1:
78 case SPV_ENV_OPENGL_4_2:
79 case SPV_ENV_OPENGL_4_3:
80 case SPV_ENV_OPENGL_4_5:
81 case SPV_ENV_UNIVERSAL_1_3:
82 case SPV_ENV_VULKAN_1_1:
83 case SPV_ENV_WEBGPU_0:
84 *pExtInstTable = &kTable_1_0;
85 return SPV_SUCCESS;
86 default:
87 return SPV_ERROR_INVALID_TABLE;
88 }
89 }
90
spvExtInstImportTypeGet(const char * name)91 spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) {
92 // The names are specified by the respective extension instruction
93 // specifications.
94 if (!strcmp("GLSL.std.450", name)) {
95 return SPV_EXT_INST_TYPE_GLSL_STD_450;
96 }
97 if (!strcmp("OpenCL.std", name)) {
98 return SPV_EXT_INST_TYPE_OPENCL_STD;
99 }
100 if (!strcmp("SPV_AMD_shader_explicit_vertex_parameter", name)) {
101 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER;
102 }
103 if (!strcmp("SPV_AMD_shader_trinary_minmax", name)) {
104 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX;
105 }
106 if (!strcmp("SPV_AMD_gcn_shader", name)) {
107 return SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER;
108 }
109 if (!strcmp("SPV_AMD_shader_ballot", name)) {
110 return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT;
111 }
112 if (!strcmp("DebugInfo", name)) {
113 return SPV_EXT_INST_TYPE_DEBUGINFO;
114 }
115 return SPV_EXT_INST_TYPE_NONE;
116 }
117
spvExtInstTableNameLookup(const spv_ext_inst_table table,const spv_ext_inst_type_t type,const char * name,spv_ext_inst_desc * pEntry)118 spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table,
119 const spv_ext_inst_type_t type,
120 const char* name,
121 spv_ext_inst_desc* pEntry) {
122 if (!table) return SPV_ERROR_INVALID_TABLE;
123 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
124
125 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
126 const auto& group = table->groups[groupIndex];
127 if (type != group.type) continue;
128 for (uint32_t index = 0; index < group.count; index++) {
129 const auto& entry = group.entries[index];
130 if (!strcmp(name, entry.name)) {
131 *pEntry = &entry;
132 return SPV_SUCCESS;
133 }
134 }
135 }
136
137 return SPV_ERROR_INVALID_LOOKUP;
138 }
139
spvExtInstTableValueLookup(const spv_ext_inst_table table,const spv_ext_inst_type_t type,const uint32_t value,spv_ext_inst_desc * pEntry)140 spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table,
141 const spv_ext_inst_type_t type,
142 const uint32_t value,
143 spv_ext_inst_desc* pEntry) {
144 if (!table) return SPV_ERROR_INVALID_TABLE;
145 if (!pEntry) return SPV_ERROR_INVALID_POINTER;
146
147 for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) {
148 const auto& group = table->groups[groupIndex];
149 if (type != group.type) continue;
150 for (uint32_t index = 0; index < group.count; index++) {
151 const auto& entry = group.entries[index];
152 if (value == entry.ext_inst) {
153 *pEntry = &entry;
154 return SPV_SUCCESS;
155 }
156 }
157 }
158
159 return SPV_ERROR_INVALID_LOOKUP;
160 }
161