yum-mirror/glslang

git clone https://git.yummers.dev/yum-mirror/glslang

GinShioAdd small type capabilities for GLSL.STD.450a3a83d09

vulkan-sdk-1.4.350.0
27.8 KiB635 linesraw
1//
2// Copyright (C) 2018 Google, Inc.
3//
4// All rights reserved.
5//
6// Redistribution and use in source and binary forms, with or without
7// modification, are permitted provided that the following conditions
8// are met:
9//
10//    Redistributions of source code must retain the above copyright
11//    notice, this list of conditions and the following disclaimer.
12//
13//    Redistributions in binary form must reproduce the above
14//    copyright notice, this list of conditions and the following
15//    disclaimer in the documentation and/or other materials provided
16//    with the distribution.
17//
18//    Neither the name of 3Dlabs Inc. Ltd. nor the names of its
19//    contributors may be used to endorse or promote products derived
20//    from this software without specific prior written permission.
21//
22// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
25// FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
26// COPYRIGHT HOLDERS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
27// INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
28// BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
29// LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
30// CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31// LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
32// ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
33// POSSIBILITY OF SUCH DAMAGE.
34
35//
36// Post-processing for SPIR-V IR, in internal form, not standard binary form.
37//
38
39#include <cassert>
40#include <cstdlib>
41
42#include <unordered_map>
43#include <unordered_set>
44#include <algorithm>
45
46#include "SPIRV/spvIR.h"
47#include "SpvBuilder.h"
48#include "spirv.hpp11"
49#include "spvUtil.h"
50
51namespace spv {
52    #include "GLSL.std.450.h"
53    #include "GLSL.ext.KHR.h"
54    #include "GLSL.ext.EXT.h"
55    #include "GLSL.ext.AMD.h"
56    #include "GLSL.ext.NV.h"
57    #include "GLSL.ext.ARM.h"
58    #include "GLSL.ext.QCOM.h"
59}
60
61namespace spv {
62
63// Hook to visit each operand type and result type of an instruction.
64// Will be called multiple times for one instruction, once for each typed
65// operand and the result.
66void Builder::postProcessType(const Instruction& inst, Id typeId)
67{
68    // Characterize the type being questioned
69    Op basicTypeOp = getMostBasicTypeClass(typeId);
70    int width = 0;
71    if (basicTypeOp == Op::OpTypeFloat || basicTypeOp == Op::OpTypeInt)
72        width = getScalarTypeWidth(typeId);
73
74    // Do opcode-specific checks
75    switch (inst.getOpCode()) {
76    case Op::OpLoad:
77    case Op::OpStore:
78        if (basicTypeOp == Op::OpTypeStruct) {
79            if (containsType(typeId, Op::OpTypeInt, 8))
80                addCapability(Capability::Int8);
81            if (containsType(typeId, Op::OpTypeInt, 16))
82                addCapability(Capability::Int16);
83            if (containsType(typeId, Op::OpTypeFloat, 16))
84                addCapability(Capability::Float16);
85        } else {
86            StorageClass storageClass = StorageClass::Max;
87            if (module.getInstruction(inst.getIdOperand(0))->getOpCode() != Op::OpUntypedAccessChainKHR) {
88                storageClass = getStorageClass(inst.getIdOperand(0));
89            }
90            if (width == 8) {
91                switch (storageClass) {
92                case StorageClass::PhysicalStorageBufferEXT:
93                case StorageClass::Uniform:
94                case StorageClass::StorageBuffer:
95                case StorageClass::PushConstant:
96                    break;
97                default:
98                    addCapability(Capability::Int8);
99                    break;
100                }
101            } else if (width == 16) {
102                switch (storageClass) {
103                case StorageClass::PhysicalStorageBufferEXT:
104                case StorageClass::Uniform:
105                case StorageClass::StorageBuffer:
106                case StorageClass::PushConstant:
107                case StorageClass::Input:
108                case StorageClass::Output:
109                    break;
110                default:
111                    if (basicTypeOp == Op::OpTypeInt)
112                        addCapability(Capability::Int16);
113                    if (basicTypeOp == Op::OpTypeFloat)
114                        addCapability(Capability::Float16);
115                    break;
116                }
117            }
118        }
119        break;
120    case Op::OpCopyObject:
121        break;
122    case Op::OpFConvert:
123    case Op::OpSConvert:
124    case Op::OpUConvert:
125        // Look for any 8/16-bit storage capabilities. If there are none, assume that
126        // the convert instruction requires the Float16/Int8/16 capability.
127        if (containsType(typeId, Op::OpTypeFloat, 16) || containsType(typeId, Op::OpTypeInt, 16)) {
128            bool foundStorage = false;
129            for (auto it = capabilities.begin(); it != capabilities.end(); ++it) {
130                spv::Capability cap = *it;
131                if (cap == spv::Capability::StorageInputOutput16 ||
132                    cap == spv::Capability::StoragePushConstant16 ||
133                    cap == spv::Capability::StorageUniformBufferBlock16 ||
134                    cap == spv::Capability::StorageUniform16) {
135                    foundStorage = true;
136                    break;
137                }
138            }
139            if (!foundStorage) {
140                if (containsType(typeId, Op::OpTypeFloat, 16))
141                    addCapability(Capability::Float16);
142                if (containsType(typeId, Op::OpTypeInt, 16))
143                    addCapability(Capability::Int16);
144            }
145        }
146        if (containsType(typeId, Op::OpTypeInt, 8)) {
147            bool foundStorage = false;
148            for (auto it = capabilities.begin(); it != capabilities.end(); ++it) {
149                spv::Capability cap = *it;
150                if (cap == spv::Capability::StoragePushConstant8 ||
151                    cap == spv::Capability::UniformAndStorageBuffer8BitAccess ||
152                    cap == spv::Capability::StorageBuffer8BitAccess) {
153                    foundStorage = true;
154                    break;
155                }
156            }
157            if (!foundStorage) {
158                addCapability(Capability::Int8);
159            }
160        }
161        break;
162    case Op::OpExtInst:
163        switch (inst.getImmediateOperand(1)) {
164        case GLSLstd450Frexp:
165        case GLSLstd450FrexpStruct:
166            if (getSpvVersion() < spv::Spv_1_3 && containsType(typeId, Op::OpTypeInt, 16))
167                addExtension(spv::E_SPV_AMD_gpu_shader_int16);
168            break;
169        case GLSLstd450InterpolateAtCentroid:
170        case GLSLstd450InterpolateAtSample:
171        case GLSLstd450InterpolateAtOffset:
172            if (getSpvVersion() < spv::Spv_1_3 && containsType(typeId, Op::OpTypeFloat, 16))
173                addExtension(spv::E_SPV_AMD_gpu_shader_half_float);
174            break;
175        default:
176            break;
177        }
178        if (basicTypeOp == Op::OpTypeInt) {
179            if (width == 16)
180                addCapability(Capability::Int16);
181            else if (width == 8)
182                addCapability(Capability::Int8);
183        } else if (basicTypeOp == Op::OpTypeFloat) {
184            if (width == 16)
185                addCapability(Capability::Float16);
186        }
187        break;
188    case Op::OpAccessChain:
189    case Op::OpPtrAccessChain:
190        if (isPointerType(typeId))
191            break;
192        if (basicTypeOp == Op::OpTypeInt) {
193            if (width == 16)
194                addCapability(Capability::Int16);
195            else if (width == 8)
196                addCapability(Capability::Int8);
197        }
198        break;
199    default:
200        if (basicTypeOp == Op::OpTypeInt) {
201            if (width == 16)
202                addCapability(Capability::Int16);
203            else if (width == 8)
204                addCapability(Capability::Int8);
205            else if (width == 64)
206                addCapability(Capability::Int64);
207        } else if (basicTypeOp == Op::OpTypeFloat) {
208            if (width == 16)
209                addCapability(Capability::Float16);
210            else if (width == 64)
211                addCapability(Capability::Float64);
212        }
213        break;
214    }
215}
216
217unsigned int Builder::postProcessGetLargestScalarSize(const Instruction& type)
218{
219    switch (type.getOpCode()) {
220    case Op::OpTypeBool:
221        return 1;
222    case Op::OpTypeInt:
223    case Op::OpTypeFloat:
224        return type.getImmediateOperand(0) / 8;
225    case Op::OpTypePointer:
226        return 8;
227    case Op::OpTypeVector:
228    case Op::OpTypeMatrix:
229    case Op::OpTypeArray:
230    case Op::OpTypeRuntimeArray: {
231        const Instruction* elem_type = module.getInstruction(type.getIdOperand(0));
232        return postProcessGetLargestScalarSize(*elem_type);
233    }
234    case Op::OpTypeStruct: {
235        unsigned int largest = 0;
236        for (int i = 0; i < type.getNumOperands(); ++i) {
237            const Instruction* elem_type = module.getInstruction(type.getIdOperand(i));
238            unsigned int elem_size = postProcessGetLargestScalarSize(*elem_type);
239            largest = std::max(largest, elem_size);
240        }
241        return largest;
242    }
243    default:
244        return 0;
245    }
246}
247
248// Called for each instruction that resides in a block.
249void Builder::postProcess(Instruction& inst)
250{
251    // Add capabilities based simply on the opcode.
252    switch (inst.getOpCode()) {
253    case Op::OpExtInst:
254        switch (inst.getImmediateOperand(1)) {
255        case GLSLstd450InterpolateAtCentroid:
256        case GLSLstd450InterpolateAtSample:
257        case GLSLstd450InterpolateAtOffset:
258            addCapability(Capability::InterpolationFunction);
259            break;
260        default:
261            break;
262        }
263        break;
264    case Op::OpDPdxFine:
265    case Op::OpDPdyFine:
266    case Op::OpFwidthFine:
267    case Op::OpDPdxCoarse:
268    case Op::OpDPdyCoarse:
269    case Op::OpFwidthCoarse:
270        addCapability(Capability::DerivativeControl);
271        break;
272
273    case Op::OpImageQueryLod:
274    case Op::OpImageQuerySize:
275    case Op::OpImageQuerySizeLod:
276    case Op::OpImageQuerySamples:
277    case Op::OpImageQueryLevels:
278        addCapability(Capability::ImageQuery);
279        break;
280
281    case Op::OpGroupNonUniformPartitionNV:
282        addExtension(E_SPV_NV_shader_subgroup_partitioned);
283        addCapability(Capability::GroupNonUniformPartitionedNV);
284        break;
285
286    case Op::OpLoad:
287    case Op::OpStore:
288        {
289            // For any load/store to a PhysicalStorageBufferEXT, walk the accesschain
290            // index list to compute the misalignment. The pre-existing alignment value
291            // (set via Builder::AccessChain::alignment) only accounts for the base of
292            // the reference type and any scalar component selection in the accesschain,
293            // and this function computes the rest from the SPIR-V Offset decorations.
294            Instruction *accessChain = module.getInstruction(inst.getIdOperand(0));
295            if (accessChain->getOpCode() == Op::OpAccessChain) {
296                const Instruction* base = module.getInstruction(accessChain->getIdOperand(0));
297                // Get the type of the base of the access chain. It must be a pointer type.
298                Id typeId = base->getTypeId();
299                Instruction *type = module.getInstruction(typeId);
300                assert(type->getOpCode() == Op::OpTypePointer);
301                if (type->getImmediateOperand(0) != StorageClass::PhysicalStorageBuffer) {
302                    break;
303                }
304                // Get the pointee type.
305                typeId = type->getIdOperand(1);
306                type = module.getInstruction(typeId);
307                // Walk the index list for the access chain. For each index, find any
308                // misalignment that can apply when accessing the member/element via
309                // Offset/ArrayStride/MatrixStride decorations, and bitwise OR them all
310                // together.
311                int alignment = 0;
312                bool first_struct_elem = false;
313                for (int i = 1; i < accessChain->getNumOperands(); ++i) {
314                    Instruction *idx = module.getInstruction(accessChain->getIdOperand(i));
315                    if (type->getOpCode() == Op::OpTypeStruct) {
316                        assert(idx->getOpCode() == Op::OpConstant);
317                        unsigned int c = idx->getImmediateOperand(0);
318
319                        const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
320                            if (decoration.get()->getOpCode() == Op::OpMemberDecorate &&
321                                decoration.get()->getIdOperand(0) == typeId &&
322                                decoration.get()->getImmediateOperand(1) == c &&
323                                (decoration.get()->getImmediateOperand(2) == Decoration::Offset ||
324                                 decoration.get()->getImmediateOperand(2) == Decoration::MatrixStride)) {
325                                unsigned int opernad_value = decoration.get()->getImmediateOperand(3);
326                                alignment |= opernad_value;
327                                if (opernad_value == 0 &&
328                                    decoration.get()->getImmediateOperand(2) == Decoration::Offset) {
329                                    first_struct_elem = true;
330                                }
331                            }
332                        };
333                        std::for_each(decorations.begin(), decorations.end(), function);
334                        // get the next member type
335                        typeId = type->getIdOperand(c);
336                        type = module.getInstruction(typeId);
337                    } else if (type->getOpCode() == Op::OpTypeArray ||
338                               type->getOpCode() == Op::OpTypeRuntimeArray) {
339                        const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
340                            if (decoration.get()->getOpCode() == Op::OpDecorate &&
341                                decoration.get()->getIdOperand(0) == typeId &&
342                                decoration.get()->getImmediateOperand(1) == Decoration::ArrayStride) {
343                                alignment |= decoration.get()->getImmediateOperand(2);
344                            }
345                        };
346                        std::for_each(decorations.begin(), decorations.end(), function);
347                        // Get the element type
348                        typeId = type->getIdOperand(0);
349                        type = module.getInstruction(typeId);
350                    } else {
351                        // Once we get to any non-aggregate type, we're done.
352                        break;
353                    }
354                }
355                assert(inst.getNumOperands() >= 3);
356                const bool is_store = inst.getOpCode() == Op::OpStore;
357                auto const memoryAccess = (MemoryAccessMask)inst.getImmediateOperand(is_store ? 2 : 1);
358                assert(anySet(memoryAccess, MemoryAccessMask::Aligned));
359                static_cast<void>(memoryAccess);
360
361                // Compute the index of the alignment operand.
362                int alignmentIdx = 2;
363                if (is_store)
364                    alignmentIdx++;
365                // Merge new and old (mis)alignment
366                alignment |= inst.getImmediateOperand(alignmentIdx);
367
368                if (!is_store) {
369                    Instruction* inst_type = module.getInstruction(inst.getTypeId());
370                    if (inst_type->getOpCode() == Op::OpTypePointer &&
371                        inst_type->getImmediateOperand(0) == StorageClass::PhysicalStorageBuffer) {
372                        // This means we are loading a pointer which means need to ensure it is at least 8-byte aligned
373                        // See https://github.com/KhronosGroup/glslang/issues/4084
374                        // In case the alignment is currently 4, need to ensure it is 8 before grabbing the LSB
375                        alignment |= 8;
376                        alignment &= 8;
377                    }
378                }
379
380                // Pick the LSB
381                alignment = alignment & ~(alignment & (alignment-1));
382
383                // The edge case we find is when copying a struct to another struct, we never find the alignment anywhere,
384                // so in this case, fallback to doing a full size lookup on the type
385                if (alignment == 0 && first_struct_elem) {
386                    // Quick get the struct type back
387                    const Instruction* pointer_type = module.getInstruction(base->getTypeId());
388                    const Instruction* struct_type = module.getInstruction(pointer_type->getIdOperand(1));
389                    assert(struct_type->getOpCode() == Op::OpTypeStruct);
390
391                    const Instruction* elem_type = module.getInstruction(struct_type->getIdOperand(0));
392                    unsigned int largest_scalar = postProcessGetLargestScalarSize(*elem_type);
393                    if (largest_scalar != 0) {
394                        alignment = largest_scalar;
395                    } else {
396                        alignment = 16; // fallback if can't determine a godo alignment
397                    }
398                }
399                // update the Aligned operand
400                assert(alignment != 0);
401                inst.setImmediateOperand(alignmentIdx, alignment);
402            }
403            break;
404        }
405
406    default:
407        break;
408    }
409
410    // Checks based on type
411    if (inst.getTypeId() != NoType)
412        postProcessType(inst, inst.getTypeId());
413    for (int op = 0; op < inst.getNumOperands(); ++op) {
414        if (inst.isIdOperand(op)) {
415            // In blocks, these are always result ids, but we are relying on
416            // getTypeId() to return NoType for things like OpLabel.
417            if (getTypeId(inst.getIdOperand(op)) != NoType)
418                postProcessType(inst, getTypeId(inst.getIdOperand(op)));
419        }
420    }
421}
422
423// comment in header
424void Builder::postProcessCFG()
425{
426    // reachableBlocks is the set of blockss reached via control flow, or which are
427    // unreachable continue targert or unreachable merge.
428    std::unordered_set<const Block*> reachableBlocks;
429    std::unordered_map<Block*, Block*> headerForUnreachableContinue;
430    std::unordered_set<Block*> unreachableMerges;
431    std::unordered_set<Id> unreachableDefinitions;
432    // Collect IDs defined in unreachable blocks. For each function, label the
433    // reachable blocks first. Then for each unreachable block, collect the
434    // result IDs of the instructions in it.
435    for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
436        Function* f = *fi;
437        Block* entry = f->getEntryBlock();
438        inReadableOrder(entry,
439            [&reachableBlocks, &unreachableMerges, &headerForUnreachableContinue]
440            (Block* b, ReachReason why, Block* header) {
441               reachableBlocks.insert(b);
442               if (why == ReachDeadContinue) headerForUnreachableContinue[b] = header;
443               if (why == ReachDeadMerge) unreachableMerges.insert(b);
444            });
445        for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
446            Block* b = *bi;
447            if (unreachableMerges.count(b) != 0 || headerForUnreachableContinue.count(b) != 0) {
448                auto ii = b->getInstructions().cbegin();
449                ++ii; // Keep potential decorations on the label.
450                for (; ii != b->getInstructions().cend(); ++ii)
451                    unreachableDefinitions.insert(ii->get()->getResultId());
452            } else if (reachableBlocks.count(b) == 0) {
453                // The normal case for unreachable code.  All definitions are considered dead.
454                for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ++ii)
455                    unreachableDefinitions.insert(ii->get()->getResultId());
456            }
457        }
458    }
459
460    // Modify unreachable merge blocks and unreachable continue targets.
461    // Delete their contents.
462    for (auto mergeIter = unreachableMerges.begin(); mergeIter != unreachableMerges.end(); ++mergeIter) {
463        (*mergeIter)->rewriteAsCanonicalUnreachableMerge();
464    }
465    for (auto continueIter = headerForUnreachableContinue.begin();
466         continueIter != headerForUnreachableContinue.end();
467         ++continueIter) {
468        Block* continue_target = continueIter->first;
469        Block* header = continueIter->second;
470        continue_target->rewriteAsCanonicalUnreachableContinue(header);
471    }
472
473    // Remove unneeded decorations, for unreachable instructions
474    for (auto decorationIter = decorations.begin(); decorationIter != decorations.end();) {
475        Id decorationId = (*decorationIter)->getIdOperand(0);
476        if (unreachableDefinitions.count(decorationId) != 0) {
477            decorationIter = decorations.erase(decorationIter);
478        } else {
479            ++decorationIter;
480        }
481    }
482}
483
484// comment in header
485void Builder::postProcessFeatures() {
486    // Add per-instruction capabilities, extensions, etc.,
487
488    // Look for any 8/16 bit type in physical storage buffer class, and set the
489    // appropriate capability. This happens in createSpvVariable for other storage
490    // classes, but there isn't always a variable for physical storage buffer.
491    for (int t = 0; t < (int)groupedTypes[enumCast(Op::OpTypePointer)].size(); ++t) {
492        Instruction* type = groupedTypes[enumCast(Op::OpTypePointer)][t];
493        if (type->getImmediateOperand(0) == (unsigned)StorageClass::PhysicalStorageBufferEXT) {
494            if (containsType(type->getIdOperand(1), Op::OpTypeInt, 8)) {
495                addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5);
496                addCapability(spv::Capability::StorageBuffer8BitAccess);
497            }
498            if (containsType(type->getIdOperand(1), Op::OpTypeInt, 16) ||
499                containsType(type->getIdOperand(1), Op::OpTypeFloat, 16)) {
500                addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3);
501                addCapability(spv::Capability::StorageBuffer16BitAccess);
502            }
503        }
504    }
505
506    // process all block-contained instructions
507    for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) {
508        Function* f = *fi;
509        for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) {
510            Block* b = *bi;
511            for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ii++)
512                postProcess(*ii->get());
513
514            // For all local variables that contain pointers to PhysicalStorageBufferEXT, check whether
515            // there is an existing restrict/aliased decoration. If we don't find one, add Aliased as the
516            // default.
517            for (auto vi = b->getLocalVariables().cbegin(); vi != b->getLocalVariables().cend(); vi++) {
518                const Instruction& inst = *vi->get();
519                Id resultId = inst.getResultId();
520                if (containsPhysicalStorageBufferOrArray(getDerefTypeId(resultId))) {
521                    bool foundDecoration = false;
522                    const auto function = [&](const std::unique_ptr<Instruction>& decoration) {
523                        if (decoration.get()->getIdOperand(0) == resultId &&
524                            decoration.get()->getOpCode() == Op::OpDecorate &&
525                            (decoration.get()->getImmediateOperand(1) == spv::Decoration::AliasedPointerEXT ||
526                             decoration.get()->getImmediateOperand(1) == spv::Decoration::RestrictPointerEXT)) {
527                            foundDecoration = true;
528                        }
529                    };
530                    std::for_each(decorations.begin(), decorations.end(), function);
531                    if (!foundDecoration) {
532                        addDecoration(resultId, spv::Decoration::AliasedPointerEXT);
533                    }
534                }
535            }
536        }
537    }
538
539    // If any Vulkan memory model-specific functionality is used, update the
540    // OpMemoryModel to match.
541    if (capabilities.find(spv::Capability::VulkanMemoryModelKHR) != capabilities.end()) {
542        memoryModel = spv::MemoryModel::VulkanKHR;
543        addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5);
544    }
545
546    // Add Aliased decoration if there's more than one Workgroup Block variable.
547    if (capabilities.find(spv::Capability::WorkgroupMemoryExplicitLayoutKHR) != capabilities.end()) {
548        assert(entryPoints.size() == 1);
549        auto &ep = entryPoints[0];
550
551        std::vector<Id> workgroup_variables;
552        for (int i = 0; i < (int)ep->getNumOperands(); i++) {
553            if (!ep->isIdOperand(i))
554                continue;
555
556            const Id id = ep->getIdOperand(i);
557            const Instruction *instr = module.getInstruction(id);
558            if (instr->getOpCode() != spv::Op::OpVariable)
559                continue;
560
561            if (instr->getImmediateOperand(0) == spv::StorageClass::Workgroup)
562                workgroup_variables.push_back(id);
563        }
564
565        if (workgroup_variables.size() > 1) {
566            for (size_t i = 0; i < workgroup_variables.size(); i++)
567                addDecoration(workgroup_variables[i], spv::Decoration::Aliased);
568        }
569    }
570}
571
572// SPIR-V requires that any instruction consuming the result of an OpSampledImage
573// be in the same block as the OpSampledImage instruction. This pass goes finds
574// uses of OpSampledImage where that is not the case and duplicates the
575// OpSampledImage to be immediately before the instruction that consumes it.
576// The old OpSampledImage is left in place, potentially with no users.
577void Builder::postProcessSamplers()
578{
579    // first, find all OpSampledImage instructions and store them in a map.
580    std::map<Id, Instruction*> sampledImageInstrs;
581    for (auto f: module.getFunctions()) {
582	for (auto b: f->getBlocks()) {
583	    for (auto &i: b->getInstructions()) {
584        if (i->getOpCode() == spv::Op::OpSampledImage) {
585		    sampledImageInstrs[i->getResultId()] = i.get();
586		}
587	    }
588	}
589    }
590    // next find all uses of the given ids and rewrite them if needed.
591    for (auto f: module.getFunctions()) {
592	for (auto b: f->getBlocks()) {
593            auto &instrs = b->getInstructions();
594            for (size_t idx = 0; idx < instrs.size(); idx++) {
595                Instruction *i = instrs[idx].get();
596                for (int opnum = 0; opnum < i->getNumOperands(); opnum++) {
597                    // Is this operand of the current instruction the result of an OpSampledImage?
598                    if (i->isIdOperand(opnum) &&
599                        sampledImageInstrs.count(i->getIdOperand(opnum)))
600                    {
601                        Instruction *opSampImg = sampledImageInstrs[i->getIdOperand(opnum)];
602                        if (i->getBlock() != opSampImg->getBlock()) {
603                            Instruction *newInstr = new Instruction(getUniqueId(),
604                                                                    opSampImg->getTypeId(),
605                                                                    spv::Op::OpSampledImage);
606                            newInstr->addIdOperand(opSampImg->getIdOperand(0));
607                            newInstr->addIdOperand(opSampImg->getIdOperand(1));
608                            newInstr->setBlock(b);
609
610                            // rewrite the user of the OpSampledImage to use the new instruction.
611                            i->setIdOperand(opnum, newInstr->getResultId());
612                            // insert the new OpSampledImage right before the current instruction.
613                            instrs.insert(instrs.begin() + idx,
614                                    std::unique_ptr<Instruction>(newInstr));
615                            idx++;
616                        }
617                    }
618                }
619            }
620	}
621    }
622}
623
624// comment in header
625void Builder::postProcess(bool compileOnly)
626{
627    // postProcessCFG needs an entrypoint to determine what is reachable, but if we are not creating an "executable" shader, we don't have an entrypoint
628    if (!compileOnly)
629        postProcessCFG();
630
631    postProcessFeatures();
632    postProcessSamplers();
633}
634
635} // end spv namespace