Dejan Mircevski | b6fe02f | 2016-01-07 13:44:22 -0500 | [diff] [blame] | 1 | // Copyright (c) 2015-2016 The Khronos Group Inc. |
Lei Zhang | 2a0b773 | 2015-11-11 13:57:43 -0500 | [diff] [blame] | 2 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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 |
Lei Zhang | 2a0b773 | 2015-11-11 13:57:43 -0500 | [diff] [blame] | 6 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 7 | // http://www.apache.org/licenses/LICENSE-2.0 |
Lei Zhang | 2a0b773 | 2015-11-11 13:57:43 -0500 | [diff] [blame] | 8 | // |
David Neto | 9fc8658 | 2016-09-01 15:33:59 -0400 | [diff] [blame] | 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. |
Lei Zhang | 2a0b773 | 2015-11-11 13:57:43 -0500 | [diff] [blame] | 14 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 15 | #include "source/ext_inst.h" |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 16 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 17 | #include <cstring> |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 18 | |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 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 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 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" |
David Neto | d1bb51d | 2016-02-17 12:17:04 -0500 | [diff] [blame] | 28 | |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 29 | #include "debuginfo.insts.inc" |
| 30 | #include "glsl.std.450.insts.inc" |
David Neto | 64f36ea | 2019-12-19 17:16:26 -0500 | [diff] [blame] | 31 | #include "opencl.debuginfo.100.insts.inc" |
dan sinclair | eda2cfb | 2018-08-03 15:06:09 -0400 | [diff] [blame] | 32 | #include "opencl.std.insts.inc" |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 33 | |
David Neto | 63f57d9 | 2019-05-07 12:27:18 -0400 | [diff] [blame] | 34 | #include "spirv-tools/libspirv.h" |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 35 | #include "spv-amd-gcn-shader.insts.inc" |
| 36 | #include "spv-amd-shader-ballot.insts.inc" |
| 37 | #include "spv-amd-shader-explicit-vertex-parameter.insts.inc" |
| 38 | #include "spv-amd-shader-trinary-minmax.insts.inc" |
| 39 | |
| 40 | static const spv_ext_inst_group_t kGroups_1_0[] = { |
| 41 | {SPV_EXT_INST_TYPE_GLSL_STD_450, ARRAY_SIZE(glsl_entries), glsl_entries}, |
| 42 | {SPV_EXT_INST_TYPE_OPENCL_STD, ARRAY_SIZE(opencl_entries), opencl_entries}, |
| 43 | {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER, |
| 44 | ARRAY_SIZE(spv_amd_shader_explicit_vertex_parameter_entries), |
| 45 | spv_amd_shader_explicit_vertex_parameter_entries}, |
| 46 | {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX, |
| 47 | ARRAY_SIZE(spv_amd_shader_trinary_minmax_entries), |
| 48 | spv_amd_shader_trinary_minmax_entries}, |
| 49 | {SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER, |
| 50 | ARRAY_SIZE(spv_amd_gcn_shader_entries), spv_amd_gcn_shader_entries}, |
| 51 | {SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT, |
| 52 | ARRAY_SIZE(spv_amd_shader_ballot_entries), spv_amd_shader_ballot_entries}, |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 53 | {SPV_EXT_INST_TYPE_DEBUGINFO, ARRAY_SIZE(debuginfo_entries), |
| 54 | debuginfo_entries}, |
David Neto | 64f36ea | 2019-12-19 17:16:26 -0500 | [diff] [blame] | 55 | {SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100, |
| 56 | ARRAY_SIZE(opencl_debuginfo_100_entries), opencl_debuginfo_100_entries}, |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 57 | }; |
| 58 | |
| 59 | static const spv_ext_inst_table_t kTable_1_0 = {ARRAY_SIZE(kGroups_1_0), |
| 60 | kGroups_1_0}; |
| 61 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 62 | spv_result_t spvExtInstTableGet(spv_ext_inst_table* pExtInstTable, |
| 63 | spv_target_env env) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 64 | if (!pExtInstTable) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 65 | |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 66 | switch (env) { |
| 67 | // The extended instruction sets are all version 1.0 so far. |
| 68 | case SPV_ENV_UNIVERSAL_1_0: |
| 69 | case SPV_ENV_VULKAN_1_0: |
| 70 | case SPV_ENV_UNIVERSAL_1_1: |
David Neto | dbc2049 | 2017-03-14 12:43:41 -0400 | [diff] [blame] | 71 | case SPV_ENV_UNIVERSAL_1_2: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 72 | case SPV_ENV_OPENCL_1_2: |
| 73 | case SPV_ENV_OPENCL_EMBEDDED_1_2: |
| 74 | case SPV_ENV_OPENCL_2_0: |
| 75 | case SPV_ENV_OPENCL_EMBEDDED_2_0: |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 76 | case SPV_ENV_OPENCL_2_1: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 77 | case SPV_ENV_OPENCL_EMBEDDED_2_1: |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 78 | case SPV_ENV_OPENCL_2_2: |
Pierre Moreau | 12447d8 | 2017-11-30 00:49:23 +0100 | [diff] [blame] | 79 | case SPV_ENV_OPENCL_EMBEDDED_2_2: |
David Neto | c296701 | 2016-08-05 18:19:30 -0400 | [diff] [blame] | 80 | case SPV_ENV_OPENGL_4_0: |
| 81 | case SPV_ENV_OPENGL_4_1: |
| 82 | case SPV_ENV_OPENGL_4_2: |
| 83 | case SPV_ENV_OPENGL_4_3: |
| 84 | case SPV_ENV_OPENGL_4_5: |
David Neto | 00fa393 | 2018-02-09 14:29:02 -0500 | [diff] [blame] | 85 | case SPV_ENV_UNIVERSAL_1_3: |
| 86 | case SPV_ENV_VULKAN_1_1: |
David Neto | 63f57d9 | 2019-05-07 12:27:18 -0400 | [diff] [blame] | 87 | case SPV_ENV_VULKAN_1_1_SPIRV_1_4: |
Corentin Wallez | ba602c9 | 2018-06-20 13:29:38 -0400 | [diff] [blame] | 88 | case SPV_ENV_WEBGPU_0: |
David Neto | 63f57d9 | 2019-05-07 12:27:18 -0400 | [diff] [blame] | 89 | case SPV_ENV_UNIVERSAL_1_4: |
alan-baker | 5a48c0d | 2019-09-13 14:59:02 -0400 | [diff] [blame] | 90 | case SPV_ENV_UNIVERSAL_1_5: |
Lei Zhang | 063dbea | 2017-10-25 12:15:51 -0400 | [diff] [blame] | 91 | *pExtInstTable = &kTable_1_0; |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 92 | return SPV_SUCCESS; |
| 93 | default: |
Dejan Mircevski | cb3c49e | 2016-04-07 14:41:34 -0400 | [diff] [blame] | 94 | return SPV_ERROR_INVALID_TABLE; |
| 95 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 96 | } |
| 97 | |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 98 | spv_ext_inst_type_t spvExtInstImportTypeGet(const char* name) { |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 99 | // The names are specified by the respective extension instruction |
| 100 | // specifications. |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 101 | if (!strcmp("GLSL.std.450", name)) { |
| 102 | return SPV_EXT_INST_TYPE_GLSL_STD_450; |
| 103 | } |
David Neto | 21c4ad4 | 2015-10-14 17:02:11 -0400 | [diff] [blame] | 104 | if (!strcmp("OpenCL.std", name)) { |
| 105 | return SPV_EXT_INST_TYPE_OPENCL_STD; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 106 | } |
Rex Xu | 5fbbadc | 2017-06-21 15:58:00 +0800 | [diff] [blame] | 107 | if (!strcmp("SPV_AMD_shader_explicit_vertex_parameter", name)) { |
| 108 | return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_EXPLICIT_VERTEX_PARAMETER; |
| 109 | } |
| 110 | if (!strcmp("SPV_AMD_shader_trinary_minmax", name)) { |
| 111 | return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_TRINARY_MINMAX; |
| 112 | } |
David Neto | 7fe8a57 | 2017-03-21 12:43:26 -0400 | [diff] [blame] | 113 | if (!strcmp("SPV_AMD_gcn_shader", name)) { |
| 114 | return SPV_EXT_INST_TYPE_SPV_AMD_GCN_SHADER; |
| 115 | } |
Rex Xu | 5fbbadc | 2017-06-21 15:58:00 +0800 | [diff] [blame] | 116 | if (!strcmp("SPV_AMD_shader_ballot", name)) { |
| 117 | return SPV_EXT_INST_TYPE_SPV_AMD_SHADER_BALLOT; |
| 118 | } |
David Neto | 59de610 | 2017-12-03 12:30:08 -0500 | [diff] [blame] | 119 | if (!strcmp("DebugInfo", name)) { |
| 120 | return SPV_EXT_INST_TYPE_DEBUGINFO; |
| 121 | } |
David Neto | 64f36ea | 2019-12-19 17:16:26 -0500 | [diff] [blame] | 122 | if (!strcmp("OpenCL.DebugInfo.100", name)) { |
| 123 | return SPV_EXT_INST_TYPE_OPENCL_DEBUGINFO_100; |
| 124 | } |
David Neto | e70b009 | 2019-12-18 18:10:29 -0500 | [diff] [blame] | 125 | // ensure to add any known non-semantic extended instruction sets |
| 126 | // above this point, and update spvExtInstIsNonSemantic() |
| 127 | if (!strncmp("NonSemantic.", name, 12)) { |
| 128 | return SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN; |
| 129 | } |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 130 | return SPV_EXT_INST_TYPE_NONE; |
| 131 | } |
| 132 | |
David Neto | e70b009 | 2019-12-18 18:10:29 -0500 | [diff] [blame] | 133 | bool spvExtInstIsNonSemantic(const spv_ext_inst_type_t type) { |
| 134 | if (type == SPV_EXT_INST_TYPE_NONSEMANTIC_UNKNOWN) { |
| 135 | return true; |
| 136 | } |
| 137 | return false; |
| 138 | } |
| 139 | |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 140 | spv_result_t spvExtInstTableNameLookup(const spv_ext_inst_table table, |
| 141 | const spv_ext_inst_type_t type, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 142 | const char* name, |
| 143 | spv_ext_inst_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 144 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 145 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 146 | |
| 147 | for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) { |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 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 (!strcmp(name, entry.name)) { |
| 153 | *pEntry = &entry; |
| 154 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 155 | } |
| 156 | } |
| 157 | } |
| 158 | |
| 159 | return SPV_ERROR_INVALID_LOOKUP; |
| 160 | } |
| 161 | |
| 162 | spv_result_t spvExtInstTableValueLookup(const spv_ext_inst_table table, |
| 163 | const spv_ext_inst_type_t type, |
| 164 | const uint32_t value, |
Lei Zhang | 1a0334e | 2015-11-02 09:41:20 -0500 | [diff] [blame] | 165 | spv_ext_inst_desc* pEntry) { |
Lei Zhang | 4005670 | 2015-09-11 14:31:27 -0400 | [diff] [blame] | 166 | if (!table) return SPV_ERROR_INVALID_TABLE; |
| 167 | if (!pEntry) return SPV_ERROR_INVALID_POINTER; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 168 | |
| 169 | for (uint32_t groupIndex = 0; groupIndex < table->count; groupIndex++) { |
Lei Zhang | ca1bf94 | 2016-04-27 16:47:13 -0400 | [diff] [blame] | 170 | const auto& group = table->groups[groupIndex]; |
| 171 | if (type != group.type) continue; |
| 172 | for (uint32_t index = 0; index < group.count; index++) { |
| 173 | const auto& entry = group.entries[index]; |
| 174 | if (value == entry.ext_inst) { |
| 175 | *pEntry = &entry; |
| 176 | return SPV_SUCCESS; |
Kenneth Benzie (Benie) | 83e5a29 | 2015-05-22 18:26:19 +0100 | [diff] [blame] | 177 | } |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | return SPV_ERROR_INVALID_LOOKUP; |
| 182 | } |