blob: d4ee86fd0e2fac274a05f0e79e78c1b1192b3f1c [file] [log] [blame]
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001/* Copyright (c) 2021-2022 The Khronos Group Inc.
sfricke-samsung962cad92021-04-13 00:46:29 -07002 *
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 * Author: Spencer Fricke <s.fricke@samsung.com>
16 */
17
18#include "shader_module.h"
19
20#include <sstream>
21#include <string>
22
23#include "vk_layer_data.h"
24#include "vk_layer_utils.h"
Jeremy Gebben5d970742021-05-31 16:04:14 -060025#include "pipeline_state.h"
26#include "descriptor_sets.h"
sfricke-samsung3c5dee22021-10-14 09:58:14 -070027#include "spirv_grammar_helper.h"
sfricke-samsung962cad92021-04-13 00:46:29 -070028
29void decoration_set::merge(decoration_set const &other) {
30 if (other.flags & location_bit) location = other.location;
31 if (other.flags & component_bit) component = other.component;
32 if (other.flags & input_attachment_index_bit) input_attachment_index = other.input_attachment_index;
33 if (other.flags & descriptor_set_bit) descriptor_set = other.descriptor_set;
34 if (other.flags & binding_bit) binding = other.binding;
35 if (other.flags & builtin_bit) builtin = other.builtin;
36 flags |= other.flags;
37}
38
39void decoration_set::add(uint32_t decoration, uint32_t value) {
40 switch (decoration) {
41 case spv::DecorationLocation:
42 flags |= location_bit;
43 location = value;
44 break;
45 case spv::DecorationPatch:
46 flags |= patch_bit;
47 break;
48 case spv::DecorationRelaxedPrecision:
49 flags |= relaxed_precision_bit;
50 break;
51 case spv::DecorationBlock:
52 flags |= block_bit;
53 break;
54 case spv::DecorationBufferBlock:
55 flags |= buffer_block_bit;
56 break;
57 case spv::DecorationComponent:
58 flags |= component_bit;
59 component = value;
60 break;
61 case spv::DecorationInputAttachmentIndex:
62 flags |= input_attachment_index_bit;
63 input_attachment_index = value;
64 break;
65 case spv::DecorationDescriptorSet:
66 flags |= descriptor_set_bit;
67 descriptor_set = value;
68 break;
69 case spv::DecorationBinding:
70 flags |= binding_bit;
71 binding = value;
72 break;
73 case spv::DecorationNonWritable:
74 flags |= nonwritable_bit;
75 break;
76 case spv::DecorationBuiltIn:
77 flags |= builtin_bit;
78 builtin = value;
79 break;
Lionel Landwerlin38d2e122021-07-21 14:21:47 +030080 case spv::DecorationNonReadable:
81 flags |= nonreadable_bit;
82 break;
ziga-lunarg9e94e112021-09-27 00:21:10 +020083 case spv::DecorationPerVertexNV:
84 flags |= per_vertex_bit;
85 break;
86 case spv::DecorationPassthroughNV:
87 flags |= passthrough_bit;
88 break;
sfricke-samsung962cad92021-04-13 00:46:29 -070089 }
90}
91
92std::string shader_struct_member::GetLocationDesc(uint32_t index_used_bytes) const {
93 std::string desc = "";
94 if (array_length_hierarchy.size() > 0) {
95 desc += " index:";
96 for (const auto block_size : array_block_size) {
97 desc += "[";
98 desc += std::to_string(index_used_bytes / (block_size * size));
99 desc += "]";
100 index_used_bytes = index_used_bytes % (block_size * size);
101 }
102 }
103 const int struct_members_size = static_cast<int>(struct_members.size());
104 if (struct_members_size > 0) {
105 desc += " member:";
106 for (int i = struct_members_size - 1; i >= 0; --i) {
107 if (index_used_bytes > struct_members[i].offset) {
108 desc += std::to_string(i);
109 desc += struct_members[i].GetLocationDesc(index_used_bytes - struct_members[i].offset);
110 break;
111 }
112 }
113 } else {
114 desc += " offset:";
115 desc += std::to_string(index_used_bytes);
116 }
117 return desc;
118}
119
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800120static uint32_t ExecutionModelToShaderStageFlagBits(uint32_t mode) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700121 switch (mode) {
122 case spv::ExecutionModelVertex:
123 return VK_SHADER_STAGE_VERTEX_BIT;
124 case spv::ExecutionModelTessellationControl:
125 return VK_SHADER_STAGE_TESSELLATION_CONTROL_BIT;
126 case spv::ExecutionModelTessellationEvaluation:
127 return VK_SHADER_STAGE_TESSELLATION_EVALUATION_BIT;
128 case spv::ExecutionModelGeometry:
129 return VK_SHADER_STAGE_GEOMETRY_BIT;
130 case spv::ExecutionModelFragment:
131 return VK_SHADER_STAGE_FRAGMENT_BIT;
132 case spv::ExecutionModelGLCompute:
133 return VK_SHADER_STAGE_COMPUTE_BIT;
134 case spv::ExecutionModelRayGenerationNV:
135 return VK_SHADER_STAGE_RAYGEN_BIT_NV;
136 case spv::ExecutionModelAnyHitNV:
137 return VK_SHADER_STAGE_ANY_HIT_BIT_NV;
138 case spv::ExecutionModelClosestHitNV:
139 return VK_SHADER_STAGE_CLOSEST_HIT_BIT_NV;
140 case spv::ExecutionModelMissNV:
141 return VK_SHADER_STAGE_MISS_BIT_NV;
142 case spv::ExecutionModelIntersectionNV:
143 return VK_SHADER_STAGE_INTERSECTION_BIT_NV;
144 case spv::ExecutionModelCallableNV:
145 return VK_SHADER_STAGE_CALLABLE_BIT_NV;
146 case spv::ExecutionModelTaskNV:
147 return VK_SHADER_STAGE_TASK_BIT_NV;
148 case spv::ExecutionModelMeshNV:
149 return VK_SHADER_STAGE_MESH_BIT_NV;
150 default:
151 return 0;
152 }
153}
154
155// For some analyses, we need to know about all ids referenced by the static call tree of a particular entrypoint. This is
156// important for identifying the set of shader resources actually used by an entrypoint, for example.
157// Note: we only explore parts of the image which might actually contain ids we care about for the above analyses.
158// - NOT the shader input/output interfaces.
159//
160// TODO: The set of interesting opcodes here was determined by eyeballing the SPIRV spec. It might be worth
161// converting parts of this to be generated from the machine-readable spec instead.
162layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::MarkAccessibleIds(spirv_inst_iter entrypoint) const {
163 layer_data::unordered_set<uint32_t> ids;
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600164 if (entrypoint == end() || !has_valid_spirv) {
165 return ids;
166 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700167 layer_data::unordered_set<uint32_t> worklist;
168 worklist.insert(entrypoint.word(2));
169
170 while (!worklist.empty()) {
171 auto id_iter = worklist.begin();
172 auto id = *id_iter;
173 worklist.erase(id_iter);
174
175 auto insn = get_def(id);
176 if (insn == end()) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600177 // ID is something we didn't collect in SpirvStaticData. that's OK -- we'll stumble across all kinds of things here
sfricke-samsung962cad92021-04-13 00:46:29 -0700178 // that we may not care about.
179 continue;
180 }
181
182 // Try to add to the output set
183 if (!ids.insert(id).second) {
184 continue; // If we already saw this id, we don't want to walk it again.
185 }
186
187 switch (insn.opcode()) {
188 case spv::OpFunction:
189 // Scan whole body of the function, enlisting anything interesting
190 while (++insn, insn.opcode() != spv::OpFunctionEnd) {
191 switch (insn.opcode()) {
192 case spv::OpLoad:
193 worklist.insert(insn.word(3)); // ptr
194 break;
195 case spv::OpStore:
196 worklist.insert(insn.word(1)); // ptr
197 break;
198 case spv::OpAccessChain:
199 case spv::OpInBoundsAccessChain:
200 worklist.insert(insn.word(3)); // base ptr
201 break;
202 case spv::OpSampledImage:
203 case spv::OpImageSampleImplicitLod:
204 case spv::OpImageSampleExplicitLod:
205 case spv::OpImageSampleDrefImplicitLod:
206 case spv::OpImageSampleDrefExplicitLod:
207 case spv::OpImageSampleProjImplicitLod:
208 case spv::OpImageSampleProjExplicitLod:
209 case spv::OpImageSampleProjDrefImplicitLod:
210 case spv::OpImageSampleProjDrefExplicitLod:
211 case spv::OpImageFetch:
212 case spv::OpImageGather:
213 case spv::OpImageDrefGather:
214 case spv::OpImageRead:
215 case spv::OpImage:
216 case spv::OpImageQueryFormat:
217 case spv::OpImageQueryOrder:
218 case spv::OpImageQuerySizeLod:
219 case spv::OpImageQuerySize:
220 case spv::OpImageQueryLod:
221 case spv::OpImageQueryLevels:
222 case spv::OpImageQuerySamples:
223 case spv::OpImageSparseSampleImplicitLod:
224 case spv::OpImageSparseSampleExplicitLod:
225 case spv::OpImageSparseSampleDrefImplicitLod:
226 case spv::OpImageSparseSampleDrefExplicitLod:
227 case spv::OpImageSparseSampleProjImplicitLod:
228 case spv::OpImageSparseSampleProjExplicitLod:
229 case spv::OpImageSparseSampleProjDrefImplicitLod:
230 case spv::OpImageSparseSampleProjDrefExplicitLod:
231 case spv::OpImageSparseFetch:
232 case spv::OpImageSparseGather:
233 case spv::OpImageSparseDrefGather:
234 case spv::OpImageTexelPointer:
235 worklist.insert(insn.word(3)); // Image or sampled image
236 break;
237 case spv::OpImageWrite:
238 worklist.insert(insn.word(1)); // Image -- different operand order to above
239 break;
240 case spv::OpFunctionCall:
241 for (uint32_t i = 3; i < insn.len(); i++) {
242 worklist.insert(insn.word(i)); // fn itself, and all args
243 }
244 break;
245
246 case spv::OpExtInst:
247 for (uint32_t i = 5; i < insn.len(); i++) {
248 worklist.insert(insn.word(i)); // Operands to ext inst
249 }
250 break;
251
252 default: {
253 if (AtomicOperation(insn.opcode())) {
254 if (insn.opcode() == spv::OpAtomicStore) {
255 worklist.insert(insn.word(1)); // ptr
256 } else {
257 worklist.insert(insn.word(3)); // ptr
258 }
259 }
260 break;
261 }
262 }
263 }
264 break;
265 }
266 }
267
268 return ids;
269}
270
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600271layer_data::optional<VkPrimitiveTopology> SHADER_MODULE_STATE::GetTopology(const spirv_inst_iter &entrypoint) const {
272 layer_data::optional<VkPrimitiveTopology> result;
273
sfricke-samsung962cad92021-04-13 00:46:29 -0700274 auto entrypoint_id = entrypoint.word(2);
275 bool is_point_mode = false;
276
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600277 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
278 if (it != static_data_.execution_mode_inst.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700279 for (auto insn : it->second) {
280 switch (insn.word(2)) {
281 case spv::ExecutionModePointMode:
282 // In tessellation shaders, PointMode is separate and trumps the tessellation topology.
283 is_point_mode = true;
284 break;
285
286 case spv::ExecutionModeOutputPoints:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600287 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
sfricke-samsung962cad92021-04-13 00:46:29 -0700288 break;
289
290 case spv::ExecutionModeIsolines:
291 case spv::ExecutionModeOutputLineStrip:
Ricardo Garcia122f8f02021-09-28 16:47:19 +0200292 case spv::ExecutionModeOutputLinesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600293 result.emplace(VK_PRIMITIVE_TOPOLOGY_LINE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700294 break;
295
296 case spv::ExecutionModeTriangles:
297 case spv::ExecutionModeQuads:
298 case spv::ExecutionModeOutputTriangleStrip:
299 case spv::ExecutionModeOutputTrianglesNV:
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600300 result.emplace(VK_PRIMITIVE_TOPOLOGY_TRIANGLE_STRIP);
sfricke-samsung962cad92021-04-13 00:46:29 -0700301 break;
302 }
303 }
304 }
305
Jeremy Gebben84b838b2021-08-23 08:41:39 -0600306 if (is_point_mode) {
307 result.emplace(VK_PRIMITIVE_TOPOLOGY_POINT_LIST);
308 }
309
310 return result;
sfricke-samsung962cad92021-04-13 00:46:29 -0700311}
312
sfricke-samsungef15e482022-01-26 11:32:49 -0800313SHADER_MODULE_STATE::SpirvStaticData::SpirvStaticData(const SHADER_MODULE_STATE &module_state) {
314 for (auto insn : module_state) {
sfricke-samsung5a48ed42022-02-13 17:37:13 -0800315 const uint32_t result_word = OpcodeResultWord(insn.opcode());
316 if (result_word != 0) {
317 def_index[insn.word(result_word)] = insn.offset();
318 }
319
sfricke-samsung962cad92021-04-13 00:46:29 -0700320 switch (insn.opcode()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700321 // Specialization constants
322 case spv::OpSpecConstantTrue:
323 case spv::OpSpecConstantFalse:
324 case spv::OpSpecConstant:
325 case spv::OpSpecConstantComposite:
326 case spv::OpSpecConstantOp:
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600327 has_specialization_constants = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700328 break;
329
sfricke-samsung962cad92021-04-13 00:46:29 -0700330 // Decorations
331 case spv::OpDecorate: {
332 auto target_id = insn.word(1);
333 decorations[target_id].add(insn.word(2), insn.len() > 3u ? insn.word(3) : 0u);
334 decoration_inst.push_back(insn);
335 if (insn.word(2) == spv::DecorationBuiltIn) {
336 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(3)));
Nathaniel Cesariocf69bda2021-06-22 13:23:42 -0600337 } else if (insn.word(2) == spv::DecorationSpecId) {
338 spec_const_map[insn.word(3)] = target_id;
sfricke-samsung962cad92021-04-13 00:46:29 -0700339 }
340
341 } break;
342 case spv::OpGroupDecorate: {
343 auto const &src = decorations[insn.word(1)];
344 for (auto i = 2u; i < insn.len(); i++) decorations[insn.word(i)].merge(src);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600345 has_group_decoration = true;
346 } break;
347 case spv::OpDecorationGroup:
348 case spv::OpGroupMemberDecorate: {
349 has_group_decoration = true;
sfricke-samsung962cad92021-04-13 00:46:29 -0700350 } break;
351 case spv::OpMemberDecorate: {
352 member_decoration_inst.push_back(insn);
353 if (insn.word(3) == spv::DecorationBuiltIn) {
354 builtin_decoration_list.emplace_back(insn.offset(), static_cast<spv::BuiltIn>(insn.word(4)));
355 }
356 } break;
357
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600358 // Execution Mode
359 case spv::OpExecutionMode: {
360 execution_mode_inst[insn.word(1)].push_back(insn);
361 } break;
362
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600363 default:
364 if (AtomicOperation(insn.opcode()) == true) {
365 // All atomics have a pointer referenced
366 spirv_inst_iter access;
367 if (insn.opcode() == spv::OpAtomicStore) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800368 access = module_state.get_def(insn.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600369 } else {
sfricke-samsungef15e482022-01-26 11:32:49 -0800370 access = module_state.get_def(insn.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600371 }
372
373 atomic_instruction atomic;
374
sfricke-samsungef15e482022-01-26 11:32:49 -0800375 auto pointer = module_state.get_def(access.word(1));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600376 // spirv-val should catch if not pointer
377 assert(pointer.opcode() == spv::OpTypePointer);
378 atomic.storage_class = pointer.word(2);
379
sfricke-samsungef15e482022-01-26 11:32:49 -0800380 auto data_type = module_state.get_def(pointer.word(3));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600381 atomic.type = data_type.opcode();
382
383 // TODO - Should have a proper GetBitWidth like spirv-val does
384 assert(data_type.opcode() == spv::OpTypeFloat || data_type.opcode() == spv::OpTypeInt);
385 atomic.bit_width = data_type.word(2);
386
387 atomic_inst[insn.offset()] = atomic;
388 }
389 // We don't care about any other defs for now.
390 break;
391 }
392 }
393
sfricke-samsungef15e482022-01-26 11:32:49 -0800394 entry_points = SHADER_MODULE_STATE::ProcessEntryPoints(module_state);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600395 multiple_entry_points = entry_points.size() > 1;
396}
397
398// static
399std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> SHADER_MODULE_STATE::ProcessEntryPoints(
sfricke-samsungef15e482022-01-26 11:32:49 -0800400 const SHADER_MODULE_STATE &module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600401 std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> entry_points;
402 function_set func_set = {};
403 EntryPoint *entry_point = nullptr;
404
sfricke-samsungef15e482022-01-26 11:32:49 -0800405 for (auto insn : module_state) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600406 // offset is not 0, it means it's updated and the offset is in a Function.
407 if (func_set.offset) {
408 func_set.op_lists.emplace(insn.opcode(), insn.offset());
409 } else if (entry_point) {
410 entry_point->decorate_list.emplace(insn.opcode(), insn.offset());
411 }
412
413 switch (insn.opcode()) {
414 // Functions
415 case spv::OpFunction:
416 func_set.id = insn.word(2);
417 func_set.offset = insn.offset();
418 func_set.op_lists.clear();
419 break;
420
421 // Entry points ... add to the entrypoint table
422 case spv::OpEntryPoint: {
sfricke-samsung962cad92021-04-13 00:46:29 -0700423 // Entry points do not have an id (the id is the function id) and thus need their own table
424 auto entrypoint_name = reinterpret_cast<char const *>(&insn.word(3));
425 auto execution_model = insn.word(1);
426 auto entrypoint_stage = ExecutionModelToShaderStageFlagBits(execution_model);
427 entry_points.emplace(entrypoint_name,
428 EntryPoint{insn.offset(), static_cast<VkShaderStageFlagBits>(entrypoint_stage)});
429
430 auto range = entry_points.equal_range(entrypoint_name);
431 for (auto it = range.first; it != range.second; ++it) {
432 if (it->second.offset == insn.offset()) {
433 entry_point = &(it->second);
434 break;
435 }
436 }
437 assert(entry_point != nullptr);
438 break;
439 }
440 case spv::OpFunctionEnd: {
441 assert(entry_point != nullptr);
442 func_set.length = insn.offset() - func_set.offset;
443 entry_point->function_set_list.emplace_back(func_set);
444 break;
445 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700446 }
447 }
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600448
sfricke-samsungef15e482022-01-26 11:32:49 -0800449 SHADER_MODULE_STATE::SetPushConstantUsedInShader(module_state, entry_points);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600450 return entry_points;
sfricke-samsung962cad92021-04-13 00:46:29 -0700451}
452
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600453void SHADER_MODULE_STATE::PreprocessShaderBinary(const spv_target_env env) {
454 if (static_data_.has_group_decoration) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700455 spvtools::Optimizer optimizer(env);
456 optimizer.RegisterPass(spvtools::CreateFlattenDecorationPass());
457 std::vector<uint32_t> optimized_binary;
458 // Run optimizer to flatten decorations only, set skip_validation so as to not re-run validator
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600459 auto result = optimizer.Run(words.data(), words.size(), &optimized_binary, spvtools::ValidatorOptions(), true);
460
sfricke-samsung962cad92021-04-13 00:46:29 -0700461 if (result) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600462 // NOTE: We need to update words with the result from the spirv-tools optimizer.
463 // **THIS ONLY HAPPENS ON INITIALIZATION**. words should remain const for the lifetime
464 // of the SHADER_MODULE_STATE instance.
465 *const_cast<std::vector<uint32_t> *>(&words) = std::move(optimized_binary);
sfricke-samsung962cad92021-04-13 00:46:29 -0700466 }
467 }
sfricke-samsung962cad92021-04-13 00:46:29 -0700468}
469
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800470char const *StorageClassName(uint32_t sc) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700471 switch (sc) {
472 case spv::StorageClassInput:
473 return "input";
474 case spv::StorageClassOutput:
475 return "output";
476 case spv::StorageClassUniformConstant:
477 return "const uniform";
478 case spv::StorageClassUniform:
479 return "uniform";
480 case spv::StorageClassWorkgroup:
481 return "workgroup local";
482 case spv::StorageClassCrossWorkgroup:
483 return "workgroup global";
484 case spv::StorageClassPrivate:
485 return "private global";
486 case spv::StorageClassFunction:
487 return "function";
488 case spv::StorageClassGeneric:
489 return "generic";
490 case spv::StorageClassAtomicCounter:
491 return "atomic counter";
492 case spv::StorageClassImage:
493 return "image";
494 case spv::StorageClassPushConstant:
495 return "push constant";
496 case spv::StorageClassStorageBuffer:
497 return "storage buffer";
498 default:
499 return "unknown";
500 }
501}
502
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800503void SHADER_MODULE_STATE::DescribeTypeInner(std::ostringstream &ss, uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700504 auto insn = get_def(type);
505 assert(insn != end());
506
507 switch (insn.opcode()) {
508 case spv::OpTypeBool:
509 ss << "bool";
510 break;
511 case spv::OpTypeInt:
512 ss << (insn.word(3) ? 's' : 'u') << "int" << insn.word(2);
513 break;
514 case spv::OpTypeFloat:
515 ss << "float" << insn.word(2);
516 break;
517 case spv::OpTypeVector:
518 ss << "vec" << insn.word(3) << " of ";
519 DescribeTypeInner(ss, insn.word(2));
520 break;
521 case spv::OpTypeMatrix:
522 ss << "mat" << insn.word(3) << " of ";
523 DescribeTypeInner(ss, insn.word(2));
524 break;
525 case spv::OpTypeArray:
526 ss << "arr[" << GetConstantValueById(insn.word(3)) << "] of ";
527 DescribeTypeInner(ss, insn.word(2));
528 break;
529 case spv::OpTypeRuntimeArray:
530 ss << "runtime arr[] of ";
531 DescribeTypeInner(ss, insn.word(2));
532 break;
533 case spv::OpTypePointer:
534 ss << "ptr to " << StorageClassName(insn.word(2)) << " ";
535 DescribeTypeInner(ss, insn.word(3));
536 break;
537 case spv::OpTypeStruct: {
538 ss << "struct of (";
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800539 for (uint32_t i = 2; i < insn.len(); i++) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700540 DescribeTypeInner(ss, insn.word(i));
541 if (i == insn.len() - 1) {
542 ss << ")";
543 } else {
544 ss << ", ";
545 }
546 }
547 break;
548 }
549 case spv::OpTypeSampler:
550 ss << "sampler";
551 break;
552 case spv::OpTypeSampledImage:
553 ss << "sampler+";
554 DescribeTypeInner(ss, insn.word(2));
555 break;
556 case spv::OpTypeImage:
557 ss << "image(dim=" << insn.word(3) << ", sampled=" << insn.word(7) << ")";
558 break;
559 case spv::OpTypeAccelerationStructureNV:
560 ss << "accelerationStruture";
561 break;
562 default:
563 ss << "oddtype";
564 break;
565 }
566}
567
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800568std::string SHADER_MODULE_STATE::DescribeType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700569 std::ostringstream ss;
570 DescribeTypeInner(ss, type);
571 return ss.str();
572}
573
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800574std::string SHADER_MODULE_STATE::DescribeInstruction(const spirv_inst_iter &insn) const {
575 std::ostringstream ss;
576 const uint32_t opcode = insn.opcode();
577 uint32_t operand_offset = 1; // where to start printing operands
578 // common disassembled for SPIR-V is
579 // %result = Opcode %result_type %operands
580 if (OpcodeHasResult(opcode)) {
581 operand_offset++;
582 ss << "%" << (OpcodeHasType(opcode) ? insn.word(2) : insn.word(1)) << " = ";
583 }
584
585 ss << string_SpvOpcode(opcode);
586
587 if (OpcodeHasType(opcode)) {
588 operand_offset++;
589 ss << " %" << insn.word(1);
590 }
591
sfricke-samsunged00aa42022-01-27 19:03:01 -0800592 // TODO - For now don't list the '%' for any operands since they are only for reference IDs. Without generating a table of each
593 // instructions operand types and covering the many edge cases (such as optional, paired, or variable operands) this is the
594 // simplest way to print the instruction and give the developer something to look into when an error occurs.
595 //
596 // For now this safely should be able to assume it will never come across a LiteralString such as in OpExtInstImport or
597 // OpEntryPoint
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800598 for (uint32_t i = operand_offset; i < insn.len(); i++) {
sfricke-samsunged00aa42022-01-27 19:03:01 -0800599 ss << " " << insn.word(i);
sfricke-samsung7a9bdca2022-01-24 14:38:03 -0800600 }
601 return ss.str();
602}
603
sfricke-samsung962cad92021-04-13 00:46:29 -0700604const SHADER_MODULE_STATE::EntryPoint *SHADER_MODULE_STATE::FindEntrypointStruct(char const *name,
605 VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600606 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700607 for (auto it = range.first; it != range.second; ++it) {
608 if (it->second.stage == stageBits) {
609 return &(it->second);
610 }
611 }
612 return nullptr;
613}
614
615spirv_inst_iter SHADER_MODULE_STATE::FindEntrypoint(char const *name, VkShaderStageFlagBits stageBits) const {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600616 auto range = static_data_.entry_points.equal_range(name);
sfricke-samsung962cad92021-04-13 00:46:29 -0700617 for (auto it = range.first; it != range.second; ++it) {
618 if (it->second.stage == stageBits) {
619 return at(it->second.offset);
620 }
621 }
622 return end();
623}
624
625// Because the following is legal, need the entry point
626// OpEntryPoint GLCompute %main "name_a"
627// OpEntryPoint GLCompute %main "name_b"
628bool SHADER_MODULE_STATE::FindLocalSize(const spirv_inst_iter &entrypoint, uint32_t &local_size_x, uint32_t &local_size_y,
629 uint32_t &local_size_z) const {
630 auto entrypoint_id = entrypoint.word(2);
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600631 auto it = static_data_.execution_mode_inst.find(entrypoint_id);
632 if (it != static_data_.execution_mode_inst.end()) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700633 for (auto insn : it->second) {
634 // Future Note: For now, Vulkan doesn't have a valid mode that can makes use of OpExecutionModeId
635 // In the future if something like LocalSizeId is supported, the <id> will need to be checked also
636 assert(insn.opcode() == spv::OpExecutionMode);
637 if (insn.word(2) == spv::ExecutionModeLocalSize) {
638 local_size_x = insn.word(3);
639 local_size_y = insn.word(4);
640 local_size_z = insn.word(5);
641 return true;
642 }
643 }
644 }
645 return false;
646}
647
648// If the instruction at id is a constant or copy of a constant, returns a valid iterator pointing to that instruction.
649// Otherwise, returns src->end().
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800650spirv_inst_iter SHADER_MODULE_STATE::GetConstantDef(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700651 auto value = get_def(id);
652
653 // If id is a copy, see where it was copied from
654 if ((end() != value) && ((value.opcode() == spv::OpCopyObject) || (value.opcode() == spv::OpCopyLogical))) {
655 id = value.word(3);
656 value = get_def(id);
657 }
658
659 if ((end() != value) && (value.opcode() == spv::OpConstant)) {
660 return value;
661 }
662 return end();
663}
664
665// Either returns the constant value described by the instruction at id, or 1
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800666uint32_t SHADER_MODULE_STATE::GetConstantValueById(uint32_t id) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700667 auto value = GetConstantDef(id);
668
669 if (end() == value) {
670 // TODO: Either ensure that the specialization transform is already performed on a module we're
671 // considering here, OR -- specialize on the fly now.
672 return 1;
673 }
674 return GetConstantValue(value);
675}
676
677// Returns an int32_t corresponding to the spv::Dim of the given resource, when positive, and corresponding to an unknown type, when
678// negative.
679int32_t SHADER_MODULE_STATE::GetShaderResourceDimensionality(const interface_var &resource) const {
680 auto type = get_def(resource.type_id);
681 while (true) {
682 switch (type.opcode()) {
683 case spv::OpTypeSampledImage:
684 type = get_def(type.word(2));
685 break;
686 case spv::OpTypePointer:
687 type = get_def(type.word(3));
688 break;
689 case spv::OpTypeImage:
690 return type.word(3);
691 default:
692 return -1;
693 }
694 }
695}
696
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800697uint32_t SHADER_MODULE_STATE::GetLocationsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700698 auto insn = get_def(type);
699 assert(insn != end());
700
701 switch (insn.opcode()) {
702 case spv::OpTypePointer:
703 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
704 // pointers around.
705 return GetLocationsConsumedByType(insn.word(3), strip_array_level);
706 case spv::OpTypeArray:
707 if (strip_array_level) {
708 return GetLocationsConsumedByType(insn.word(2), false);
709 } else {
710 return GetConstantValueById(insn.word(3)) * GetLocationsConsumedByType(insn.word(2), false);
711 }
712 case spv::OpTypeMatrix:
713 // Num locations is the dimension * element size
714 return insn.word(3) * GetLocationsConsumedByType(insn.word(2), false);
715 case spv::OpTypeVector: {
716 auto scalar_type = get_def(insn.word(2));
717 auto bit_width =
718 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
719
720 // Locations are 128-bit wide; 3- and 4-component vectors of 64 bit types require two.
721 return (bit_width * insn.word(3) + 127) / 128;
722 }
723 default:
724 // Everything else is just 1.
725 return 1;
726
727 // TODO: extend to handle 64bit scalar types, whose vectors may need multiple locations.
728 }
729}
730
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800731uint32_t SHADER_MODULE_STATE::GetComponentsConsumedByType(uint32_t type, bool strip_array_level) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700732 auto insn = get_def(type);
733 assert(insn != end());
734
735 switch (insn.opcode()) {
736 case spv::OpTypePointer:
737 // See through the ptr -- this is only ever at the toplevel for graphics shaders we're never actually passing
738 // pointers around.
739 return GetComponentsConsumedByType(insn.word(3), strip_array_level);
740 case spv::OpTypeStruct: {
741 uint32_t sum = 0;
742 for (uint32_t i = 2; i < insn.len(); i++) { // i=2 to skip word(0) and word(1)=ID of struct
743 sum += GetComponentsConsumedByType(insn.word(i), false);
744 }
745 return sum;
746 }
747 case spv::OpTypeArray:
748 if (strip_array_level) {
749 return GetComponentsConsumedByType(insn.word(2), false);
750 } else {
751 return GetConstantValueById(insn.word(3)) * GetComponentsConsumedByType(insn.word(2), false);
752 }
753 case spv::OpTypeMatrix:
754 // Num locations is the dimension * element size
755 return insn.word(3) * GetComponentsConsumedByType(insn.word(2), false);
756 case spv::OpTypeVector: {
757 auto scalar_type = get_def(insn.word(2));
758 auto bit_width =
759 (scalar_type.opcode() == spv::OpTypeInt || scalar_type.opcode() == spv::OpTypeFloat) ? scalar_type.word(2) : 32;
760 // One component is 32-bit
761 return (bit_width * insn.word(3) + 31) / 32;
762 }
763 case spv::OpTypeFloat: {
764 auto bit_width = insn.word(2);
765 return (bit_width + 31) / 32;
766 }
767 case spv::OpTypeInt: {
768 auto bit_width = insn.word(2);
769 return (bit_width + 31) / 32;
770 }
771 case spv::OpConstant:
772 return GetComponentsConsumedByType(insn.word(1), false);
773 default:
774 return 0;
775 }
776}
777
778// characterizes a SPIR-V type appearing in an interface to a FF stage, for comparison to a VkFormat's characterization above.
779// also used for input attachments, as we statically know their format.
sfricke-samsung7fac88a2022-01-26 11:44:22 -0800780uint32_t SHADER_MODULE_STATE::GetFundamentalType(uint32_t type) const {
sfricke-samsung962cad92021-04-13 00:46:29 -0700781 auto insn = get_def(type);
782 assert(insn != end());
783
784 switch (insn.opcode()) {
785 case spv::OpTypeInt:
786 return insn.word(3) ? FORMAT_TYPE_SINT : FORMAT_TYPE_UINT;
787 case spv::OpTypeFloat:
788 return FORMAT_TYPE_FLOAT;
789 case spv::OpTypeVector:
790 case spv::OpTypeMatrix:
791 case spv::OpTypeArray:
792 case spv::OpTypeRuntimeArray:
793 case spv::OpTypeImage:
794 return GetFundamentalType(insn.word(2));
795 case spv::OpTypePointer:
796 return GetFundamentalType(insn.word(3));
797
798 default:
799 return 0;
800 }
801}
802
803spirv_inst_iter SHADER_MODULE_STATE::GetStructType(spirv_inst_iter def, bool is_array_of_verts) const {
804 while (true) {
805 if (def.opcode() == spv::OpTypePointer) {
806 def = get_def(def.word(3));
807 } else if (def.opcode() == spv::OpTypeArray && is_array_of_verts) {
808 def = get_def(def.word(2));
809 is_array_of_verts = false;
810 } else if (def.opcode() == spv::OpTypeStruct) {
811 return def;
812 } else {
813 return end();
814 }
815 }
816}
817
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800818void SHADER_MODULE_STATE::DefineStructMember(const spirv_inst_iter &it, const std::vector<uint32_t> &member_decorate_offsets,
sfricke-samsung962cad92021-04-13 00:46:29 -0700819 shader_struct_member &data) const {
820 const auto struct_it = GetStructType(it, false);
821 assert(struct_it != end());
822 data.size = 0;
823
824 shader_struct_member data1;
825 uint32_t i = 2;
826 uint32_t local_offset = 0;
827 std::vector<uint32_t> offsets;
828 offsets.resize(struct_it.len() - i);
829
830 // The members of struct in SPRIV_R aren't always sort, so we need to know their order.
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800831 for (const auto offset : member_decorate_offsets) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700832 const auto member_decorate = at(offset);
833 if (member_decorate.word(1) != struct_it.word(1)) {
834 continue;
835 }
836
837 offsets[member_decorate.word(2)] = member_decorate.word(4);
838 }
839
840 for (const auto offset : offsets) {
841 local_offset = offset;
842 data1 = {};
843 data1.root = data.root;
844 data1.offset = local_offset;
845 auto def_member = get_def(struct_it.word(i));
846
847 // Array could be multi-dimensional
848 while (def_member.opcode() == spv::OpTypeArray) {
849 const auto len_id = def_member.word(3);
850 const auto def_len = get_def(len_id);
851 data1.array_length_hierarchy.emplace_back(def_len.word(3)); // array length
852 def_member = get_def(def_member.word(2));
853 }
854
855 if (def_member.opcode() == spv::OpTypeStruct) {
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800856 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700857 } else if (def_member.opcode() == spv::OpTypePointer) {
858 if (def_member.word(2) == spv::StorageClassPhysicalStorageBuffer) {
859 // If it's a pointer with PhysicalStorageBuffer class, this member is essentially a uint64_t containing an address
860 // that "points to something."
861 data1.size = 8;
862 } else {
863 // If it's OpTypePointer. it means the member is a buffer, the type will be TypePointer, and then struct
sfricke-samsungad55ccc2022-01-19 20:06:17 -0800864 DefineStructMember(def_member, member_decorate_offsets, data1);
sfricke-samsung962cad92021-04-13 00:46:29 -0700865 }
866 } else {
867 if (def_member.opcode() == spv::OpTypeMatrix) {
868 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // matrix's columns. matrix's row is vector.
869 def_member = get_def(def_member.word(2));
870 }
871
872 if (def_member.opcode() == spv::OpTypeVector) {
873 data1.array_length_hierarchy.emplace_back(def_member.word(3)); // vector length
874 def_member = get_def(def_member.word(2));
875 }
876
877 // Get scalar type size. The value in SPRV-R is bit. It needs to translate to byte.
878 data1.size = (def_member.word(2) / 8);
879 }
880 const auto array_length_hierarchy_szie = data1.array_length_hierarchy.size();
881 if (array_length_hierarchy_szie > 0) {
882 data1.array_block_size.resize(array_length_hierarchy_szie, 1);
883
884 for (int i2 = static_cast<int>(array_length_hierarchy_szie - 1); i2 > 0; --i2) {
885 data1.array_block_size[i2 - 1] = data1.array_length_hierarchy[i2] * data1.array_block_size[i2];
886 }
887 }
888 data.struct_members.emplace_back(data1);
889 ++i;
890 }
891 uint32_t total_array_length = 1;
892 for (const auto length : data1.array_length_hierarchy) {
893 total_array_length *= length;
894 }
895 data.size = local_offset + data1.size * total_array_length;
896}
897
898static uint32_t UpdateOffset(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
899 int array_indices_size = static_cast<int>(array_indices.size());
900 if (array_indices_size) {
901 uint32_t array_index = 0;
902 uint32_t i = 0;
903 for (const auto index : array_indices) {
904 array_index += (data.array_block_size[i] * index);
905 ++i;
906 }
907 offset += (array_index * data.size);
908 }
909 return offset;
910}
911
912static void SetUsedBytes(uint32_t offset, const std::vector<uint32_t> &array_indices, const shader_struct_member &data) {
913 int array_indices_size = static_cast<int>(array_indices.size());
914 uint32_t block_memory_size = data.size;
915 for (uint32_t i = static_cast<int>(array_indices_size); i < data.array_length_hierarchy.size(); ++i) {
916 block_memory_size *= data.array_length_hierarchy[i];
917 }
918
919 offset = UpdateOffset(offset, array_indices, data);
920
921 uint32_t end = offset + block_memory_size;
922 auto used_bytes = data.GetUsedbytes();
923 if (used_bytes->size() < end) {
924 used_bytes->resize(end, 0);
925 }
926 std::memset(used_bytes->data() + offset, true, static_cast<std::size_t>(block_memory_size));
927}
928
929void SHADER_MODULE_STATE::RunUsedArray(uint32_t offset, std::vector<uint32_t> array_indices, uint32_t access_chain_word_index,
930 spirv_inst_iter &access_chain_it, const shader_struct_member &data) const {
931 if (access_chain_word_index < access_chain_it.len()) {
932 if (data.array_length_hierarchy.size() > array_indices.size()) {
933 auto def_it = get_def(access_chain_it.word(access_chain_word_index));
934 ++access_chain_word_index;
935
936 if (def_it != end() && def_it.opcode() == spv::OpConstant) {
937 array_indices.emplace_back(def_it.word(3));
938 RunUsedArray(offset, array_indices, access_chain_word_index, access_chain_it, data);
939 } else {
940 // If it is a variable, set the all array is used.
941 if (access_chain_word_index < access_chain_it.len()) {
942 uint32_t array_length = data.array_length_hierarchy[array_indices.size()];
943 for (uint32_t i = 0; i < array_length; ++i) {
944 auto array_indices2 = array_indices;
945 array_indices2.emplace_back(i);
946 RunUsedArray(offset, array_indices2, access_chain_word_index, access_chain_it, data);
947 }
948 } else {
949 SetUsedBytes(offset, array_indices, data);
950 }
951 }
952 } else {
953 offset = UpdateOffset(offset, array_indices, data);
954 RunUsedStruct(offset, access_chain_word_index, access_chain_it, data);
955 }
956 } else {
957 SetUsedBytes(offset, array_indices, data);
958 }
959}
960
961void SHADER_MODULE_STATE::RunUsedStruct(uint32_t offset, uint32_t access_chain_word_index, spirv_inst_iter &access_chain_it,
962 const shader_struct_member &data) const {
963 std::vector<uint32_t> array_indices_emptry;
964
965 if (access_chain_word_index < access_chain_it.len()) {
966 auto strcut_member_index = GetConstantValueById(access_chain_it.word(access_chain_word_index));
967 ++access_chain_word_index;
968
969 auto data1 = data.struct_members[strcut_member_index];
970 RunUsedArray(offset + data1.offset, array_indices_emptry, access_chain_word_index, access_chain_it, data1);
971 }
972}
973
974void SHADER_MODULE_STATE::SetUsedStructMember(const uint32_t variable_id, const std::vector<function_set> &function_set_list,
975 const shader_struct_member &data) const {
976 for (const auto &func_set : function_set_list) {
977 auto range = func_set.op_lists.equal_range(spv::OpAccessChain);
978 for (auto it = range.first; it != range.second; ++it) {
979 auto access_chain = at(it->second);
980 if (access_chain.word(3) == variable_id) {
981 RunUsedStruct(0, 4, access_chain, data);
982 }
983 }
984 }
985}
986
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -0600987// static
988void SHADER_MODULE_STATE::SetPushConstantUsedInShader(
sfricke-samsungef15e482022-01-26 11:32:49 -0800989 const SHADER_MODULE_STATE &module_state, std::unordered_multimap<std::string, SHADER_MODULE_STATE::EntryPoint> &entry_points) {
sfricke-samsung962cad92021-04-13 00:46:29 -0700990 for (auto &entrypoint : entry_points) {
991 auto range = entrypoint.second.decorate_list.equal_range(spv::OpVariable);
992 for (auto it = range.first; it != range.second; ++it) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800993 const auto def_insn = module_state.at(it->second);
sfricke-samsung962cad92021-04-13 00:46:29 -0700994
995 if (def_insn.word(3) == spv::StorageClassPushConstant) {
sfricke-samsungef15e482022-01-26 11:32:49 -0800996 spirv_inst_iter type = module_state.get_def(def_insn.word(1));
sfricke-samsung962cad92021-04-13 00:46:29 -0700997 const auto range2 = entrypoint.second.decorate_list.equal_range(spv::OpMemberDecorate);
998 std::vector<uint32_t> offsets;
999
1000 for (auto it2 = range2.first; it2 != range2.second; ++it2) {
sfricke-samsungef15e482022-01-26 11:32:49 -08001001 auto member_decorate = module_state.at(it2->second);
sfricke-samsung962cad92021-04-13 00:46:29 -07001002 if (member_decorate.len() == 5 && member_decorate.word(3) == spv::DecorationOffset) {
1003 offsets.emplace_back(member_decorate.offset());
1004 }
1005 }
1006 entrypoint.second.push_constant_used_in_shader.root = &entrypoint.second.push_constant_used_in_shader;
sfricke-samsungef15e482022-01-26 11:32:49 -08001007 module_state.DefineStructMember(type, offsets, entrypoint.second.push_constant_used_in_shader);
1008 module_state.SetUsedStructMember(def_insn.word(2), entrypoint.second.function_set_list,
1009 entrypoint.second.push_constant_used_in_shader);
sfricke-samsung962cad92021-04-13 00:46:29 -07001010 }
1011 }
1012 }
1013}
1014
1015uint32_t SHADER_MODULE_STATE::DescriptorTypeToReqs(uint32_t type_id) const {
1016 auto type = get_def(type_id);
1017
1018 while (true) {
1019 switch (type.opcode()) {
1020 case spv::OpTypeArray:
1021 case spv::OpTypeRuntimeArray:
1022 case spv::OpTypeSampledImage:
1023 type = get_def(type.word(2));
1024 break;
1025 case spv::OpTypePointer:
1026 type = get_def(type.word(3));
1027 break;
1028 case spv::OpTypeImage: {
1029 auto dim = type.word(3);
1030 auto arrayed = type.word(5);
1031 auto msaa = type.word(6);
1032
1033 uint32_t bits = 0;
1034 switch (GetFundamentalType(type.word(2))) {
1035 case FORMAT_TYPE_FLOAT:
1036 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_FLOAT;
1037 break;
1038 case FORMAT_TYPE_UINT:
1039 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_UINT;
1040 break;
1041 case FORMAT_TYPE_SINT:
1042 bits = DESCRIPTOR_REQ_COMPONENT_TYPE_SINT;
1043 break;
1044 default:
1045 break;
1046 }
1047
1048 switch (dim) {
1049 case spv::Dim1D:
1050 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_1D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_1D;
1051 return bits;
1052 case spv::Dim2D:
1053 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1054 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_2D_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_2D;
1055 return bits;
1056 case spv::Dim3D:
1057 bits |= DESCRIPTOR_REQ_VIEW_TYPE_3D;
1058 return bits;
1059 case spv::DimCube:
1060 bits |= arrayed ? DESCRIPTOR_REQ_VIEW_TYPE_CUBE_ARRAY : DESCRIPTOR_REQ_VIEW_TYPE_CUBE;
1061 return bits;
1062 case spv::DimSubpassData:
1063 bits |= msaa ? DESCRIPTOR_REQ_MULTI_SAMPLE : DESCRIPTOR_REQ_SINGLE_SAMPLE;
1064 return bits;
1065 default: // buffer, etc.
1066 return bits;
1067 }
1068 }
1069 default:
1070 return 0;
1071 }
1072 }
1073}
1074
1075// For some built-in analysis we need to know if the variable decorated with as the built-in was actually written to.
1076// This function examines instructions in the static call tree for a write to this variable.
1077bool SHADER_MODULE_STATE::IsBuiltInWritten(spirv_inst_iter builtin_instr, spirv_inst_iter entrypoint) const {
1078 auto type = builtin_instr.opcode();
1079 uint32_t target_id = builtin_instr.word(1);
1080 bool init_complete = false;
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001081 uint32_t target_member_offset = 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001082
1083 if (type == spv::OpMemberDecorate) {
1084 // Built-in is part of a structure -- examine instructions up to first function body to get initial IDs
1085 auto insn = entrypoint;
1086 while (!init_complete && (insn.opcode() != spv::OpFunction)) {
1087 switch (insn.opcode()) {
1088 case spv::OpTypePointer:
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001089 if (insn.word(2) == spv::StorageClassOutput) {
1090 const auto type_id = insn.word(3);
1091 if (type_id == target_id) {
1092 target_id = insn.word(1);
1093 } else {
1094 // If the output is an array, check if the element type is what we're looking for
1095 const auto type_insn = get_def(type_id);
1096 if ((type_insn.opcode() == spv::OpTypeArray) && (type_insn.word(2) == target_id)) {
1097 target_id = insn.word(1);
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001098 target_member_offset = 1;
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001099 }
1100 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001101 }
1102 break;
1103 case spv::OpVariable:
1104 if (insn.word(1) == target_id) {
1105 target_id = insn.word(2);
1106 init_complete = true;
1107 }
1108 break;
1109 }
1110 insn++;
1111 }
1112 }
1113
1114 if (!init_complete && (type == spv::OpMemberDecorate)) return false;
1115
1116 bool found_write = false;
1117 layer_data::unordered_set<uint32_t> worklist;
1118 worklist.insert(entrypoint.word(2));
1119
1120 // Follow instructions in call graph looking for writes to target
1121 while (!worklist.empty() && !found_write) {
1122 auto id_iter = worklist.begin();
1123 auto id = *id_iter;
1124 worklist.erase(id_iter);
1125
1126 auto insn = get_def(id);
1127 if (insn == end()) {
1128 continue;
1129 }
1130
1131 if (insn.opcode() == spv::OpFunction) {
1132 // Scan body of function looking for other function calls or items in our ID chain
Nathaniel Cesario58fc2282021-08-18 12:20:40 -06001133 while (++insn, (insn.opcode() != spv::OpFunctionEnd) && !found_write) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001134 switch (insn.opcode()) {
1135 case spv::OpAccessChain:
1136 if (insn.word(3) == target_id) {
1137 if (type == spv::OpMemberDecorate) {
Nathaniel Cesario80c0e0f2021-10-14 13:25:54 -06001138 // Get the target member of the struct
1139 // NOTE: this will only work for structs and arrays of structs. Deeper levels of nesting (e.g.,
1140 // arrays of structs of structs) is not currently supported.
1141 const auto value_itr = GetConstantDef(insn.word(4 + target_member_offset));
1142 if (value_itr != end()) {
1143 auto value = GetConstantValue(value_itr);
1144 if (value == builtin_instr.word(2)) {
1145 target_id = insn.word(2);
1146 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001147 }
1148 } else {
1149 target_id = insn.word(2);
1150 }
1151 }
1152 break;
1153 case spv::OpStore:
1154 if (insn.word(1) == target_id) {
1155 found_write = true;
1156 }
1157 break;
1158 case spv::OpFunctionCall:
1159 worklist.insert(insn.word(3));
1160 break;
1161 }
1162 }
1163 }
1164 }
1165 return found_write;
1166}
1167
1168// Used by the collection functions to help aid in state tracking
1169struct shader_module_used_operators {
1170 bool updated;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001171 std::vector<uint32_t> image_read_members;
1172 std::vector<uint32_t> image_write_members;
1173 std::vector<uint32_t> atomic_members;
1174 std::vector<uint32_t> store_members;
1175 std::vector<uint32_t> atomic_store_members;
1176 std::vector<uint32_t> sampler_implicitLod_dref_proj_members; // sampler Load id
1177 std::vector<uint32_t> sampler_bias_offset_members; // sampler Load id
1178 std::vector<uint32_t> image_dref_members;
1179 std::vector<std::pair<uint32_t, uint32_t>> sampled_image_members; // <image,sampler> Load id
1180 layer_data::unordered_map<uint32_t, uint32_t> load_members;
1181 layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> accesschain_members;
1182 layer_data::unordered_map<uint32_t, uint32_t> image_texel_pointer_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001183
1184 shader_module_used_operators() : updated(false) {}
1185
1186 bool CheckImageOperandsBiasOffset(uint32_t type) {
1187 return type & (spv::ImageOperandsBiasMask | spv::ImageOperandsConstOffsetMask | spv::ImageOperandsOffsetMask |
1188 spv::ImageOperandsConstOffsetsMask)
1189 ? true
1190 : false;
1191 }
1192
sfricke-samsungef15e482022-01-26 11:32:49 -08001193 void update(SHADER_MODULE_STATE const *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001194 if (updated) return;
1195 updated = true;
1196
sfricke-samsungef15e482022-01-26 11:32:49 -08001197 for (auto insn : *module_state) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001198 switch (insn.opcode()) {
1199 case spv::OpImageSampleImplicitLod:
1200 case spv::OpImageSampleProjImplicitLod:
1201 case spv::OpImageSampleProjExplicitLod:
1202 case spv::OpImageSparseSampleImplicitLod:
1203 case spv::OpImageSparseSampleProjImplicitLod:
1204 case spv::OpImageSparseSampleProjExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001205 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001206 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001207 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1208 sampler_implicitLod_dref_proj_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001209 // ImageOperands in index: 5
1210 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001211 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001212 }
1213 break;
1214 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001215 case spv::OpImageDrefGather:
1216 case spv::OpImageSparseDrefGather: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001217 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001218 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001219 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1220 image_dref_members.emplace_back(load_id);
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001221 break;
1222 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001223 case spv::OpImageSampleDrefImplicitLod:
1224 case spv::OpImageSampleDrefExplicitLod:
1225 case spv::OpImageSampleProjDrefImplicitLod:
1226 case spv::OpImageSampleProjDrefExplicitLod:
1227 case spv::OpImageSparseSampleDrefImplicitLod:
1228 case spv::OpImageSparseSampleDrefExplicitLod:
1229 case spv::OpImageSparseSampleProjDrefImplicitLod:
1230 case spv::OpImageSparseSampleProjDrefExplicitLod: {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001231 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001232 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001233 auto sampler_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1234 auto image_load_id = (id.opcode() == spv::OpSampledImage) ? id.word(3) : insn.word(3);
1235
1236 image_dref_members.emplace_back(image_load_id);
1237 sampler_implicitLod_dref_proj_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001238 // ImageOperands in index: 6
1239 if (insn.len() > 6 && CheckImageOperandsBiasOffset(insn.word(6))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001240 sampler_bias_offset_members.emplace_back(sampler_load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001241 }
1242 break;
1243 }
1244 case spv::OpImageSampleExplicitLod:
1245 case spv::OpImageSparseSampleExplicitLod: {
1246 // ImageOperands in index: 5
1247 if (insn.len() > 5 && CheckImageOperandsBiasOffset(insn.word(5))) {
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001248 // combined image samples are just OpLoad, but also can be separate image and sampler
sfricke-samsungef15e482022-01-26 11:32:49 -08001249 auto id = module_state->get_def(insn.word(3)); // <id> Sampled Image
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001250 auto load_id = (id.opcode() == spv::OpSampledImage) ? id.word(4) : insn.word(3);
1251 sampler_bias_offset_members.emplace_back(load_id);
sfricke-samsung962cad92021-04-13 00:46:29 -07001252 }
1253 break;
1254 }
1255 case spv::OpStore: {
1256 store_members.emplace_back(insn.word(1)); // object id or AccessChain id
1257 break;
1258 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001259 case spv::OpImageRead:
1260 case spv::OpImageSparseRead: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001261 image_read_members.emplace_back(insn.word(3)); // Load id
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001262 break;
1263 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001264 case spv::OpImageWrite: {
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001265 image_write_members.emplace_back(insn.word(1)); // Load id
sfricke-samsung962cad92021-04-13 00:46:29 -07001266 break;
1267 }
1268 case spv::OpSampledImage: {
1269 // 3: image load id, 4: sampler load id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001270 sampled_image_members.emplace_back(std::pair<uint32_t, uint32_t>(insn.word(3), insn.word(4)));
sfricke-samsung962cad92021-04-13 00:46:29 -07001271 break;
1272 }
1273 case spv::OpLoad: {
1274 // 2: Load id, 3: object id or AccessChain id
1275 load_members.emplace(insn.word(2), insn.word(3));
1276 break;
1277 }
1278 case spv::OpAccessChain: {
1279 if (insn.len() == 4) {
1280 // If it is for struct, the length is only 4.
1281 // 2: AccessChain id, 3: object id
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001282 accesschain_members.emplace(insn.word(2), std::pair<uint32_t, uint32_t>(insn.word(3), 0));
sfricke-samsung962cad92021-04-13 00:46:29 -07001283 } else {
1284 // 2: AccessChain id, 3: object id, 4: object id of array index
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001285 accesschain_members.emplace(insn.word(2), std::pair<uint32_t, uint32_t>(insn.word(3), insn.word(4)));
sfricke-samsung962cad92021-04-13 00:46:29 -07001286 }
1287 break;
1288 }
1289 case spv::OpImageTexelPointer: {
1290 // 2: ImageTexelPointer id, 3: object id
1291 image_texel_pointer_members.emplace(insn.word(2), insn.word(3));
1292 break;
1293 }
1294 default: {
1295 if (AtomicOperation(insn.opcode())) {
1296 if (insn.opcode() == spv::OpAtomicStore) {
1297 atomic_store_members.emplace_back(insn.word(1)); // ImageTexelPointer id
1298 } else {
1299 atomic_members.emplace_back(insn.word(3)); // ImageTexelPointer id
1300 }
1301 }
1302 break;
1303 }
1304 }
1305 }
1306 }
1307};
1308
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001309static bool CheckObjectIDFromOpLoad(uint32_t object_id, const std::vector<uint32_t> &operator_members,
1310 const layer_data::unordered_map<uint32_t, uint32_t> &load_members,
1311 const layer_data::unordered_map<uint32_t, std::pair<uint32_t, uint32_t>> &accesschain_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001312 for (auto load_id : operator_members) {
1313 if (object_id == load_id) return true;
1314 auto load_it = load_members.find(load_id);
1315 if (load_it == load_members.end()) {
1316 continue;
1317 }
1318 if (load_it->second == object_id) {
1319 return true;
1320 }
1321
1322 auto accesschain_it = accesschain_members.find(load_it->second);
1323 if (accesschain_it == accesschain_members.end()) {
1324 continue;
1325 }
1326 if (accesschain_it->second.first == object_id) {
1327 return true;
1328 }
1329 }
1330 return false;
1331}
1332
1333// Takes a OpVariable and looks at the the descriptor type it uses. This will find things such as if the variable is writable, image
1334// atomic operation, matching images to samplers, etc
1335void SHADER_MODULE_STATE::IsSpecificDescriptorType(const spirv_inst_iter &id_it, bool is_storage_buffer, bool is_check_writable,
1336 interface_var &out_interface_var,
1337 shader_module_used_operators &used_operators) const {
1338 uint32_t type_id = id_it.word(1);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001339 uint32_t id = id_it.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001340
1341 auto type = get_def(type_id);
1342
1343 // Strip off any array or ptrs. Where we remove array levels, adjust the descriptor count for each dimension.
1344 while (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypePointer || type.opcode() == spv::OpTypeRuntimeArray ||
1345 type.opcode() == spv::OpTypeSampledImage) {
1346 if (type.opcode() == spv::OpTypeArray || type.opcode() == spv::OpTypeRuntimeArray ||
1347 type.opcode() == spv::OpTypeSampledImage) {
1348 type = get_def(type.word(2)); // Element type
1349 } else {
1350 type = get_def(type.word(3)); // Pointer type
1351 }
1352 }
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001353
sfricke-samsung962cad92021-04-13 00:46:29 -07001354 switch (type.opcode()) {
1355 case spv::OpTypeImage: {
1356 auto dim = type.word(3);
1357 if (dim != spv::DimSubpassData) {
1358 used_operators.update(this);
1359
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001360 // Sampled == 2 indicates used without a sampler (a storage image)
1361 bool is_image_without_format = false;
1362 if (type.word(7) == 2) is_image_without_format = type.word(8) == spv::ImageFormatUnknown;
1363
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001364 if (CheckObjectIDFromOpLoad(id, used_operators.image_write_members, used_operators.load_members,
sfricke-samsung962cad92021-04-13 00:46:29 -07001365 used_operators.accesschain_members)) {
1366 out_interface_var.is_writable = true;
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001367 if (is_image_without_format) out_interface_var.is_write_without_format = true;
1368 }
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001369 if (CheckObjectIDFromOpLoad(id, used_operators.image_read_members, used_operators.load_members,
Lionel Landwerlinbc7401b2021-12-07 15:43:05 +02001370 used_operators.accesschain_members)) {
1371 out_interface_var.is_readable = true;
1372 if (is_image_without_format) out_interface_var.is_read_without_format = true;
sfricke-samsung962cad92021-04-13 00:46:29 -07001373 }
1374 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_implicitLod_dref_proj_members, used_operators.load_members,
1375 used_operators.accesschain_members)) {
1376 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1377 }
1378 if (CheckObjectIDFromOpLoad(id, used_operators.sampler_bias_offset_members, used_operators.load_members,
1379 used_operators.accesschain_members)) {
1380 out_interface_var.is_sampler_bias_offset = true;
1381 }
1382 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_members, used_operators.image_texel_pointer_members,
1383 used_operators.accesschain_members) ||
1384 CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1385 used_operators.accesschain_members)) {
1386 out_interface_var.is_atomic_operation = true;
1387 }
Lionel Landwerlincdbe8682021-12-08 15:10:37 +02001388 if (CheckObjectIDFromOpLoad(id, used_operators.image_dref_members, used_operators.load_members,
1389 used_operators.accesschain_members)) {
1390 out_interface_var.is_dref_operation = true;
1391 }
sfricke-samsung962cad92021-04-13 00:46:29 -07001392
sfricke-samsungad55ccc2022-01-19 20:06:17 -08001393 for (auto &itp_id : used_operators.sampled_image_members) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001394 // Find if image id match.
1395 uint32_t image_index = 0;
1396 auto load_it = used_operators.load_members.find(itp_id.first);
1397 if (load_it == used_operators.load_members.end()) {
1398 continue;
1399 } else {
1400 if (load_it->second != id) {
1401 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1402 if (accesschain_it == used_operators.accesschain_members.end()) {
1403 continue;
1404 } else {
1405 if (accesschain_it->second.first != id) {
1406 continue;
1407 }
1408
1409 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1410 if (const_itr == end()) {
1411 // access chain index not a constant, skip.
1412 break;
1413 }
1414 image_index = GetConstantValue(const_itr);
1415 }
1416 }
1417 }
1418 // Find sampler's set binding.
1419 load_it = used_operators.load_members.find(itp_id.second);
1420 if (load_it == used_operators.load_members.end()) {
1421 continue;
1422 } else {
1423 uint32_t sampler_id = load_it->second;
1424 uint32_t sampler_index = 0;
1425 auto accesschain_it = used_operators.accesschain_members.find(load_it->second);
1426
1427 if (accesschain_it != used_operators.accesschain_members.end()) {
1428 const auto const_itr = GetConstantDef(accesschain_it->second.second);
1429 if (const_itr == end()) {
1430 // access chain index representing sampler index is not a constant, skip.
1431 break;
1432 }
1433 sampler_id = const_itr.offset();
1434 sampler_index = GetConstantValue(const_itr);
1435 }
1436 auto sampler_dec = get_decorations(sampler_id);
1437 if (image_index >= out_interface_var.samplers_used_by_image.size()) {
1438 out_interface_var.samplers_used_by_image.resize(image_index + 1);
1439 }
sfricke-samsungdb3f3f82022-01-18 06:41:15 -08001440
1441 // Need to check again for these properties in case not using a combined image sampler
1442 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_implicitLod_dref_proj_members,
1443 used_operators.load_members, used_operators.accesschain_members)) {
1444 out_interface_var.is_sampler_implicitLod_dref_proj = true;
1445 }
1446 if (CheckObjectIDFromOpLoad(sampler_id, used_operators.sampler_bias_offset_members,
1447 used_operators.load_members, used_operators.accesschain_members)) {
1448 out_interface_var.is_sampler_bias_offset = true;
1449 }
1450
sfricke-samsung962cad92021-04-13 00:46:29 -07001451 out_interface_var.samplers_used_by_image[image_index].emplace(
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001452 SamplerUsedByImage{DescriptorSlot{sampler_dec.descriptor_set, sampler_dec.binding}, sampler_index});
sfricke-samsung962cad92021-04-13 00:46:29 -07001453 }
1454 }
1455 }
1456 return;
1457 }
1458
1459 case spv::OpTypeStruct: {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001460 layer_data::unordered_set<uint32_t> nonwritable_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001461 if (get_decorations(type.word(1)).flags & decoration_set::buffer_block_bit) is_storage_buffer = true;
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001462 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001463 if (insn.word(1) == type.word(1) && insn.word(3) == spv::DecorationNonWritable) {
1464 nonwritable_members.insert(insn.word(2));
1465 }
1466 }
1467
1468 // A buffer is writable if it's either flavor of storage buffer, and has any member not decorated
1469 // as nonwritable.
1470 if (is_storage_buffer && nonwritable_members.size() != type.len() - 2) {
1471 used_operators.update(this);
1472
1473 for (auto oid : used_operators.store_members) {
1474 if (id == oid) {
1475 out_interface_var.is_writable = true;
1476 return;
1477 }
1478 auto accesschain_it = used_operators.accesschain_members.find(oid);
1479 if (accesschain_it == used_operators.accesschain_members.end()) {
1480 continue;
1481 }
1482 if (accesschain_it->second.first == id) {
1483 out_interface_var.is_writable = true;
1484 return;
1485 }
1486 }
1487 if (CheckObjectIDFromOpLoad(id, used_operators.atomic_store_members, used_operators.image_texel_pointer_members,
1488 used_operators.accesschain_members)) {
1489 out_interface_var.is_writable = true;
1490 return;
1491 }
1492 }
1493 }
1494 }
1495}
1496
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001497std::vector<std::pair<DescriptorSlot, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByDescriptorSlot(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001498 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
Jeremy Gebben7fc88a22021-08-25 13:30:45 -06001499 std::vector<std::pair<DescriptorSlot, interface_var>> out;
sfricke-samsung962cad92021-04-13 00:46:29 -07001500 shader_module_used_operators operators;
1501
1502 for (auto id : accessible_ids) {
1503 auto insn = get_def(id);
1504 assert(insn != end());
1505
1506 if (insn.opcode() == spv::OpVariable &&
Lionel Landwerlin6a9f89c2021-12-07 15:46:46 +02001507 (insn.word(3) == spv::StorageClassUniform ||
1508 insn.word(3) == spv::StorageClassUniformConstant ||
sfricke-samsung962cad92021-04-13 00:46:29 -07001509 insn.word(3) == spv::StorageClassStorageBuffer)) {
1510 auto d = get_decorations(insn.word(2));
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001511 uint32_t set = d.descriptor_set;
1512 uint32_t binding = d.binding;
sfricke-samsung962cad92021-04-13 00:46:29 -07001513
1514 interface_var v = {};
1515 v.id = insn.word(2);
1516 v.type_id = insn.word(1);
1517
1518 IsSpecificDescriptorType(insn, insn.word(3) == spv::StorageClassStorageBuffer,
1519 !(d.flags & decoration_set::nonwritable_bit), v, operators);
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001520 out.emplace_back(DescriptorSlot{set, binding}, v);
sfricke-samsung962cad92021-04-13 00:46:29 -07001521 }
1522 }
1523
1524 return out;
1525}
1526
1527layer_data::unordered_set<uint32_t> SHADER_MODULE_STATE::CollectWritableOutputLocationinFS(
Jeremy Gebben84b838b2021-08-23 08:41:39 -06001528 const spirv_inst_iter &entrypoint) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001529 layer_data::unordered_set<uint32_t> location_list;
sfricke-samsung962cad92021-04-13 00:46:29 -07001530 const auto outputs = CollectInterfaceByLocation(entrypoint, spv::StorageClassOutput, false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001531 layer_data::unordered_set<uint32_t> store_members;
1532 layer_data::unordered_map<uint32_t, uint32_t> accesschain_members;
sfricke-samsung962cad92021-04-13 00:46:29 -07001533
1534 for (auto insn : *this) {
1535 switch (insn.opcode()) {
1536 case spv::OpStore:
1537 case spv::OpAtomicStore: {
1538 store_members.insert(insn.word(1)); // object id or AccessChain id
1539 break;
1540 }
1541 case spv::OpAccessChain: {
1542 // 2: AccessChain id, 3: object id
1543 if (insn.word(3)) accesschain_members.emplace(insn.word(2), insn.word(3));
1544 break;
1545 }
1546 default:
1547 break;
1548 }
1549 }
1550 if (store_members.empty()) {
1551 return location_list;
1552 }
1553 for (auto output : outputs) {
1554 auto store_it = store_members.find(output.second.id);
1555 if (store_it != store_members.end()) {
1556 location_list.insert(output.first.first);
1557 store_members.erase(store_it);
1558 continue;
1559 }
1560 store_it = store_members.begin();
1561 while (store_it != store_members.end()) {
1562 auto accesschain_it = accesschain_members.find(*store_it);
1563 if (accesschain_it == accesschain_members.end()) {
1564 ++store_it;
1565 continue;
1566 }
1567 if (accesschain_it->second == output.second.id) {
1568 location_list.insert(output.first.first);
1569 store_members.erase(store_it);
1570 accesschain_members.erase(accesschain_it);
1571 break;
1572 }
1573 ++store_it;
1574 }
1575 }
1576 return location_list;
1577}
1578
1579bool SHADER_MODULE_STATE::CollectInterfaceBlockMembers(std::map<location_t, interface_var> *out, bool is_array_of_verts,
ziga-lunarg9e94e112021-09-27 00:21:10 +02001580 uint32_t id, uint32_t type_id, bool is_patch,
1581 uint32_t /*first_location*/) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001582 // Walk down the type_id presented, trying to determine whether it's actually an interface block.
1583 auto type = GetStructType(get_def(type_id), is_array_of_verts && !is_patch);
1584 if (type == end() || !(get_decorations(type.word(1)).flags & decoration_set::block_bit)) {
1585 // This isn't an interface block.
1586 return false;
1587 }
1588
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001589 layer_data::unordered_map<uint32_t, uint32_t> member_components;
1590 layer_data::unordered_map<uint32_t, uint32_t> member_relaxed_precision;
1591 layer_data::unordered_map<uint32_t, uint32_t> member_patch;
sfricke-samsung962cad92021-04-13 00:46:29 -07001592
1593 // Walk all the OpMemberDecorate for type's result id -- first pass, collect components.
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001594 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001595 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001596 uint32_t member_index = insn.word(2);
sfricke-samsung962cad92021-04-13 00:46:29 -07001597
1598 if (insn.word(3) == spv::DecorationComponent) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001599 uint32_t component = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001600 member_components[member_index] = component;
1601 }
1602
1603 if (insn.word(3) == spv::DecorationRelaxedPrecision) {
1604 member_relaxed_precision[member_index] = 1;
1605 }
1606
1607 if (insn.word(3) == spv::DecorationPatch) {
1608 member_patch[member_index] = 1;
1609 }
1610 }
1611 }
1612
1613 // TODO: correctly handle location assignment from outside
1614
1615 // Second pass -- produce the output, from Location decorations
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001616 for (auto insn : static_data_.member_decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001617 if (insn.word(1) == type.word(1)) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001618 uint32_t member_index = insn.word(2);
1619 uint32_t member_type_id = type.word(2 + member_index);
sfricke-samsung962cad92021-04-13 00:46:29 -07001620
1621 if (insn.word(3) == spv::DecorationLocation) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001622 uint32_t location = insn.word(4);
1623 uint32_t num_locations = GetLocationsConsumedByType(member_type_id, false);
sfricke-samsung962cad92021-04-13 00:46:29 -07001624 auto component_it = member_components.find(member_index);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001625 uint32_t component = component_it == member_components.end() ? 0 : component_it->second;
sfricke-samsung962cad92021-04-13 00:46:29 -07001626 bool is_relaxed_precision = member_relaxed_precision.find(member_index) != member_relaxed_precision.end();
1627 bool member_is_patch = is_patch || member_patch.count(member_index) > 0;
1628
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001629 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001630 interface_var v = {};
1631 v.id = id;
1632 // TODO: member index in interface_var too?
1633 v.type_id = member_type_id;
1634 v.offset = offset;
1635 v.is_patch = member_is_patch;
1636 v.is_block_member = true;
1637 v.is_relaxed_precision = is_relaxed_precision;
1638 (*out)[std::make_pair(location + offset, component)] = v;
1639 }
1640 }
1641 }
1642 }
1643
1644 return true;
1645}
1646
1647std::map<location_t, interface_var> SHADER_MODULE_STATE::CollectInterfaceByLocation(spirv_inst_iter entrypoint,
1648 spv::StorageClass sinterface,
1649 bool is_array_of_verts) const {
1650 // TODO: handle index=1 dual source outputs from FS -- two vars will have the same location, and we DON'T want to clobber.
1651
1652 std::map<location_t, interface_var> out;
1653
1654 for (uint32_t iid : FindEntrypointInterfaces(entrypoint)) {
1655 auto insn = get_def(iid);
1656 assert(insn != end());
1657 assert(insn.opcode() == spv::OpVariable);
1658
ziga-lunarg9e94e112021-09-27 00:21:10 +02001659 const auto d = get_decorations(iid);
1660 bool passthrough = sinterface == spv::StorageClassOutput && insn.word(3) == spv::StorageClassInput &&
1661 (d.flags & decoration_set::passthrough_bit) != 0;
1662 if (insn.word(3) == static_cast<uint32_t>(sinterface) || passthrough) {
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001663 uint32_t id = insn.word(2);
1664 uint32_t type = insn.word(1);
sfricke-samsung962cad92021-04-13 00:46:29 -07001665
ziga-lunarg9e94e112021-09-27 00:21:10 +02001666 auto location = d.location;
sfricke-samsung962cad92021-04-13 00:46:29 -07001667 int builtin = d.builtin;
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001668 uint32_t component = d.component;
sfricke-samsung962cad92021-04-13 00:46:29 -07001669 bool is_patch = (d.flags & decoration_set::patch_bit) != 0;
1670 bool is_relaxed_precision = (d.flags & decoration_set::relaxed_precision_bit) != 0;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001671 bool is_per_vertex = (d.flags & decoration_set::per_vertex_bit) != 0;
sfricke-samsung962cad92021-04-13 00:46:29 -07001672
1673 if (builtin != -1) {
1674 continue;
ziga-lunarg9e94e112021-09-27 00:21:10 +02001675 } else if (!CollectInterfaceBlockMembers(&out, is_array_of_verts, id, type, is_patch, location) ||
1676 location != decoration_set::kInvalidValue) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001677 // A user-defined interface variable, with a location. Where a variable occupied multiple locations, emit
1678 // one result for each.
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001679 uint32_t num_locations = GetLocationsConsumedByType(type, (is_array_of_verts && !is_patch) || is_per_vertex);
1680 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001681 interface_var v = {};
1682 v.id = id;
1683 v.type_id = type;
1684 v.offset = offset;
1685 v.is_patch = is_patch;
1686 v.is_relaxed_precision = is_relaxed_precision;
1687 out[std::make_pair(location + offset, component)] = v;
1688 }
1689 }
1690 }
1691 }
1692
1693 return out;
1694}
1695
1696std::vector<uint32_t> SHADER_MODULE_STATE::CollectBuiltinBlockMembers(spirv_inst_iter entrypoint, uint32_t storageClass) const {
sfricke-samsung962cad92021-04-13 00:46:29 -07001697 // Find all interface variables belonging to the entrypoint and matching the storage class
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001698 std::vector<uint32_t> variables;
sfricke-samsung962cad92021-04-13 00:46:29 -07001699 for (uint32_t id : FindEntrypointInterfaces(entrypoint)) {
1700 auto def = get_def(id);
1701 assert(def != end());
1702 assert(def.opcode() == spv::OpVariable);
1703
1704 if (def.word(3) == storageClass) variables.push_back(def.word(1));
1705 }
1706
1707 // Find all members belonging to the builtin block selected
1708 std::vector<uint32_t> builtin_block_members;
1709 for (auto &var : variables) {
1710 auto def = get_def(get_def(var).word(3));
1711
1712 // It could be an array of IO blocks. The element type should be the struct defining the block contents
1713 if (def.opcode() == spv::OpTypeArray) def = get_def(def.word(2));
1714
1715 // Now find all members belonging to the struct defining the IO block
1716 if (def.opcode() == spv::OpTypeStruct) {
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001717 for (auto set : static_data_.builtin_decoration_list) {
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001718 auto insn = at(set.offset);
1719 if ((insn.opcode() == spv::OpMemberDecorate) && (def.word(1) == insn.word(1))) {
1720 // Start with undefined builtin for each struct member.
1721 // But only when confirmed the struct is the built-in inteface block (can only be one per shader)
1722 if (builtin_block_members.size() == 0) {
1723 builtin_block_members.resize(def.len() - 2, spv::BuiltInMax);
sfricke-samsung962cad92021-04-13 00:46:29 -07001724 }
sfricke-samsung0df5ee72021-07-24 23:27:16 -07001725 auto struct_index = insn.word(2);
1726 assert(struct_index < builtin_block_members.size());
1727 builtin_block_members[struct_index] = insn.word(4);
sfricke-samsung962cad92021-04-13 00:46:29 -07001728 }
1729 }
1730 }
1731 }
1732
1733 return builtin_block_members;
1734}
1735
1736std::vector<std::pair<uint32_t, interface_var>> SHADER_MODULE_STATE::CollectInterfaceByInputAttachmentIndex(
1737 layer_data::unordered_set<uint32_t> const &accessible_ids) const {
1738 std::vector<std::pair<uint32_t, interface_var>> out;
1739
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001740 for (auto insn : static_data_.decoration_inst) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001741 if (insn.word(2) == spv::DecorationInputAttachmentIndex) {
1742 auto attachment_index = insn.word(3);
1743 auto id = insn.word(1);
1744
1745 if (accessible_ids.count(id)) {
1746 auto def = get_def(id);
1747 assert(def != end());
1748 if (def.opcode() == spv::OpVariable && def.word(3) == spv::StorageClassUniformConstant) {
1749 auto num_locations = GetLocationsConsumedByType(def.word(1), false);
sfricke-samsung7fac88a2022-01-26 11:44:22 -08001750 for (uint32_t offset = 0; offset < num_locations; offset++) {
sfricke-samsung962cad92021-04-13 00:46:29 -07001751 interface_var v = {};
1752 v.id = id;
1753 v.type_id = def.word(1);
1754 v.offset = offset;
1755 out.emplace_back(attachment_index + offset, v);
1756 }
1757 }
1758 }
1759 }
1760 }
1761
1762 return out;
1763}
1764
ziga-lunarg8346fe82021-08-22 17:30:50 +02001765uint32_t SHADER_MODULE_STATE::GetNumComponentsInBaseType(const spirv_inst_iter &iter) const {
1766 const uint32_t opcode = iter.opcode();
1767 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1768 return 1;
1769 } else if (opcode == spv::OpTypeVector) {
1770 const uint32_t component_count = iter.word(3);
1771 return component_count;
1772 } else if (opcode == spv::OpTypeMatrix) {
1773 const auto column_type = get_def(iter.word(2));
1774 const uint32_t vector_length = GetNumComponentsInBaseType(column_type);
1775 const uint32_t column_count = iter.word(3);
1776 return vector_length * column_count;
1777 } else if (opcode == spv::OpTypeArray) {
1778 const auto element_type = get_def(iter.word(2));
1779 const uint32_t element_length = GetNumComponentsInBaseType(element_type);
1780 return element_length;
1781 } else if (opcode == spv::OpTypeStruct) {
1782 uint32_t total_size = 0;
1783 for (uint32_t i = 2; i < iter.len(); ++i) {
1784 total_size += GetNumComponentsInBaseType(get_def(iter.word(i)));
1785 }
1786 return total_size;
1787 } else if (opcode == spv::OpTypePointer) {
1788 const auto type = get_def(iter.word(3));
1789 return GetNumComponentsInBaseType(type);
1790 }
1791 return 0;
1792}
1793
ziga-lunarg2818f492021-08-12 14:30:51 +02001794std::array<uint32_t, 3> SHADER_MODULE_STATE::GetWorkgroupSize(
1795 VkPipelineShaderStageCreateInfo const *pStage, const std::unordered_map<uint32_t, std::vector<uint32_t>>& id_value_map) const {
1796 std::array<uint32_t, 3> work_group_size = {1, 1, 1};
1797
1798 uint32_t work_group_size_id = std::numeric_limits<uint32_t>::max();
1799
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001800 for (const auto &builtin : static_data_.builtin_decoration_list) {
ziga-lunarg2818f492021-08-12 14:30:51 +02001801 if (builtin.builtin == spv::BuiltInWorkgroupSize) {
1802 work_group_size_id = at(builtin.offset).word(1);
1803 break;
1804 }
1805 }
1806 for (auto insn : *this) {
1807 uint32_t opcode = insn.opcode();
1808 if (opcode == spv::OpSpecConstantComposite) { // WorkGroupSize must be a composite
1809 uint32_t result_id = insn.word(2);
1810 if (result_id == work_group_size_id) {
1811 uint32_t result_type_id = insn.word(1);
1812 auto result_type = get_def(result_type_id);
1813 if (result_type.opcode() == spv::OpTypeVector) {
1814 uint32_t component_count = result_type.word(3);
1815 for (uint32_t i = 0; i < component_count; ++i) {
1816 auto constituent = get_def(insn.word(3 + i));
Nathaniel Cesario77cd59b2021-10-11 23:52:24 -06001817 for (const auto &sc : static_data_.spec_const_map) {
ziga-lunarg2818f492021-08-12 14:30:51 +02001818 if (sc.second == constituent.word(2)) {
1819 const auto iter = id_value_map.find(sc.first);
1820 if (iter != id_value_map.cend()) {
1821 work_group_size[i] = *iter->second.begin();
1822 }
1823 break;
1824 }
1825 }
1826 }
1827 }
1828 }
1829 }
1830 }
1831
1832 return work_group_size;
1833}
1834
ziga-lunarga26b3602021-08-08 15:53:00 +02001835uint32_t SHADER_MODULE_STATE::GetTypeBitsSize(const spirv_inst_iter &iter) const {
1836 const uint32_t opcode = iter.opcode();
1837 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt) {
1838 return iter.word(2);
1839 } else if (opcode == spv::OpTypeVector) {
1840 const auto component_type = get_def(iter.word(2));
1841 uint32_t scalar_width = GetTypeBitsSize(component_type);
1842 uint32_t component_count = iter.word(3);
1843 return scalar_width * component_count;
1844 } else if (opcode == spv::OpTypeMatrix) {
1845 const auto column_type = get_def(iter.word(2));
1846 uint32_t vector_width = GetTypeBitsSize(column_type);
1847 uint32_t column_count = iter.word(3);
1848 return vector_width * column_count;
1849 } else if (opcode == spv::OpTypeArray) {
1850 const auto element_type = get_def(iter.word(2));
1851 uint32_t element_width = GetTypeBitsSize(element_type);
1852 const auto length_type = get_def(iter.word(3));
1853 uint32_t length = GetConstantValue(length_type);
1854 return element_width * length;
1855 } else if (opcode == spv::OpTypeStruct) {
1856 uint32_t total_size = 0;
1857 for (uint32_t i = 2; i < iter.len(); ++i) {
1858 total_size += GetTypeBitsSize(get_def(iter.word(i)));
1859 }
1860 return total_size;
ziga-lunarg8346fe82021-08-22 17:30:50 +02001861 } else if (opcode == spv::OpTypePointer) {
1862 const auto type = get_def(iter.word(3));
1863 return GetTypeBitsSize(type);
ziga-lunargef2c3172021-11-07 10:35:29 +01001864 } else if (opcode == spv::OpVariable) {
1865 const auto type = get_def(iter.word(1));
1866 return GetTypeBitsSize(type);
ziga-lunarga26b3602021-08-08 15:53:00 +02001867 }
1868 return 0;
1869}
1870
1871uint32_t SHADER_MODULE_STATE::GetTypeBytesSize(const spirv_inst_iter &iter) const { return GetTypeBitsSize(iter) / 8; }
1872
ziga-lunarg19fc6ae2021-09-09 00:05:19 +02001873// Returns the base type (float, int or unsigned int) or struct (can have multiple different base types inside)
ziga-lunarg8346fe82021-08-22 17:30:50 +02001874uint32_t SHADER_MODULE_STATE::GetBaseType(const spirv_inst_iter &iter) const {
1875 const uint32_t opcode = iter.opcode();
1876 if (opcode == spv::OpTypeFloat || opcode == spv::OpTypeInt || opcode == spv::OpTypeStruct) {
1877 return iter.word(1);
1878 } else if (opcode == spv::OpTypeVector) {
1879 const auto& component_type = get_def(iter.word(2));
1880 return GetBaseType(component_type);
1881 } else if (opcode == spv::OpTypeMatrix) {
1882 const auto& column_type = get_def(iter.word(2));
1883 return GetBaseType(column_type);
1884 } else if (opcode == spv::OpTypeArray) {
1885 const auto& element_type = get_def(iter.word(2));
1886 return GetBaseType(element_type);
1887 } else if (opcode == spv::OpTypePointer) {
1888 const auto& type = get_def(iter.word(3));
1889 return GetBaseType(type);
1890 }
1891 return 0;
1892}
1893
sfricke-samsunga6c1ddc2022-01-23 14:15:40 -08001894// Returns type_id if id has type or zero otherwise
1895uint32_t SHADER_MODULE_STATE::GetTypeId(uint32_t id) const {
1896 const auto type = get_def(id);
1897 return OpcodeHasType(type.opcode()) ? type.word(1) : 0;
1898}
1899
ziga-lunarga26b3602021-08-08 15:53:00 +02001900uint32_t SHADER_MODULE_STATE::CalcComputeSharedMemory(VkShaderStageFlagBits stage,
1901 const spirv_inst_iter &insn) const {
1902 if (stage == VK_SHADER_STAGE_COMPUTE_BIT && insn.opcode() == spv::OpVariable) {
1903 uint32_t storage_class = insn.word(3);
1904 if (storage_class == spv::StorageClassWorkgroup) { // StorageClass Workgroup is shared memory
1905 uint32_t result_type_id = insn.word(1);
1906 auto result_type = get_def(result_type_id);
1907 auto type = get_def(result_type.word(3));
1908 return GetTypeBytesSize(type);
1909 }
1910 }
1911
1912 return 0;
1913}
1914
sfricke-samsung962cad92021-04-13 00:46:29 -07001915// Assumes itr points to an OpConstant instruction
1916uint32_t GetConstantValue(const spirv_inst_iter &itr) { return itr.word(3); }
1917
1918std::vector<uint32_t> FindEntrypointInterfaces(const spirv_inst_iter &entrypoint) {
1919 assert(entrypoint.opcode() == spv::OpEntryPoint);
1920
1921 std::vector<uint32_t> interfaces;
1922 // Find the end of the entrypoint's name string. additional zero bytes follow the actual null terminator, to fill out the
1923 // rest of the word - so we only need to look at the last byte in the word to determine which word contains the terminator.
1924 uint32_t word = 3;
1925 while (entrypoint.word(word) & 0xff000000u) {
1926 ++word;
1927 }
1928 ++word;
1929
1930 for (; word < entrypoint.len(); word++) interfaces.push_back(entrypoint.word(word));
1931
1932 return interfaces;
1933}