| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| | |
| |
|
| | |
| | |
| | |
| |
|
| | #include <cassert> |
| | #include <cstdlib> |
| |
|
| | #include <unordered_map> |
| | #include <unordered_set> |
| | #include <algorithm> |
| |
|
| | #include "SpvBuilder.h" |
| | #include "spirv.hpp" |
| |
|
| | namespace spv { |
| | #include "GLSL.std.450.h" |
| | #include "GLSL.ext.KHR.h" |
| | #include "GLSL.ext.EXT.h" |
| | #include "GLSL.ext.AMD.h" |
| | #include "GLSL.ext.NV.h" |
| | #include "GLSL.ext.ARM.h" |
| | } |
| |
|
| | namespace spv { |
| |
|
| | #ifndef GLSLANG_WEB |
| | |
| | |
| | |
| | void Builder::postProcessType(const Instruction& inst, Id typeId) |
| | { |
| | |
| | Id basicTypeOp = getMostBasicTypeClass(typeId); |
| | int width = 0; |
| | if (basicTypeOp == OpTypeFloat || basicTypeOp == OpTypeInt) |
| | width = getScalarTypeWidth(typeId); |
| |
|
| | |
| | switch (inst.getOpCode()) { |
| | case OpLoad: |
| | case OpStore: |
| | if (basicTypeOp == OpTypeStruct) { |
| | if (containsType(typeId, OpTypeInt, 8)) |
| | addCapability(CapabilityInt8); |
| | if (containsType(typeId, OpTypeInt, 16)) |
| | addCapability(CapabilityInt16); |
| | if (containsType(typeId, OpTypeFloat, 16)) |
| | addCapability(CapabilityFloat16); |
| | } else { |
| | StorageClass storageClass = getStorageClass(inst.getIdOperand(0)); |
| | if (width == 8) { |
| | switch (storageClass) { |
| | case StorageClassPhysicalStorageBufferEXT: |
| | case StorageClassUniform: |
| | case StorageClassStorageBuffer: |
| | case StorageClassPushConstant: |
| | break; |
| | default: |
| | addCapability(CapabilityInt8); |
| | break; |
| | } |
| | } else if (width == 16) { |
| | switch (storageClass) { |
| | case StorageClassPhysicalStorageBufferEXT: |
| | case StorageClassUniform: |
| | case StorageClassStorageBuffer: |
| | case StorageClassPushConstant: |
| | case StorageClassInput: |
| | case StorageClassOutput: |
| | break; |
| | default: |
| | if (basicTypeOp == OpTypeInt) |
| | addCapability(CapabilityInt16); |
| | if (basicTypeOp == OpTypeFloat) |
| | addCapability(CapabilityFloat16); |
| | break; |
| | } |
| | } |
| | } |
| | break; |
| | case OpCopyObject: |
| | break; |
| | case OpFConvert: |
| | case OpSConvert: |
| | case OpUConvert: |
| | |
| | |
| | if (containsType(typeId, OpTypeFloat, 16) || containsType(typeId, OpTypeInt, 16)) { |
| | bool foundStorage = false; |
| | for (auto it = capabilities.begin(); it != capabilities.end(); ++it) { |
| | spv::Capability cap = *it; |
| | if (cap == spv::CapabilityStorageInputOutput16 || |
| | cap == spv::CapabilityStoragePushConstant16 || |
| | cap == spv::CapabilityStorageUniformBufferBlock16 || |
| | cap == spv::CapabilityStorageUniform16) { |
| | foundStorage = true; |
| | break; |
| | } |
| | } |
| | if (!foundStorage) { |
| | if (containsType(typeId, OpTypeFloat, 16)) |
| | addCapability(CapabilityFloat16); |
| | if (containsType(typeId, OpTypeInt, 16)) |
| | addCapability(CapabilityInt16); |
| | } |
| | } |
| | if (containsType(typeId, OpTypeInt, 8)) { |
| | bool foundStorage = false; |
| | for (auto it = capabilities.begin(); it != capabilities.end(); ++it) { |
| | spv::Capability cap = *it; |
| | if (cap == spv::CapabilityStoragePushConstant8 || |
| | cap == spv::CapabilityUniformAndStorageBuffer8BitAccess || |
| | cap == spv::CapabilityStorageBuffer8BitAccess) { |
| | foundStorage = true; |
| | break; |
| | } |
| | } |
| | if (!foundStorage) { |
| | addCapability(CapabilityInt8); |
| | } |
| | } |
| | break; |
| | case OpExtInst: |
| | switch (inst.getImmediateOperand(1)) { |
| | case GLSLstd450Frexp: |
| | case GLSLstd450FrexpStruct: |
| | if (getSpvVersion() < spv::Spv_1_3 && containsType(typeId, OpTypeInt, 16)) |
| | addExtension(spv::E_SPV_AMD_gpu_shader_int16); |
| | break; |
| | case GLSLstd450InterpolateAtCentroid: |
| | case GLSLstd450InterpolateAtSample: |
| | case GLSLstd450InterpolateAtOffset: |
| | if (getSpvVersion() < spv::Spv_1_3 && containsType(typeId, OpTypeFloat, 16)) |
| | addExtension(spv::E_SPV_AMD_gpu_shader_half_float); |
| | break; |
| | default: |
| | break; |
| | } |
| | break; |
| | case OpAccessChain: |
| | case OpPtrAccessChain: |
| | if (isPointerType(typeId)) |
| | break; |
| | if (basicTypeOp == OpTypeInt) { |
| | if (width == 16) |
| | addCapability(CapabilityInt16); |
| | else if (width == 8) |
| | addCapability(CapabilityInt8); |
| | } |
| | default: |
| | if (basicTypeOp == OpTypeInt) { |
| | if (width == 16) |
| | addCapability(CapabilityInt16); |
| | else if (width == 8) |
| | addCapability(CapabilityInt8); |
| | else if (width == 64) |
| | addCapability(CapabilityInt64); |
| | } else if (basicTypeOp == OpTypeFloat) { |
| | if (width == 16) |
| | addCapability(CapabilityFloat16); |
| | else if (width == 64) |
| | addCapability(CapabilityFloat64); |
| | } |
| | break; |
| | } |
| | } |
| |
|
| | |
| | void Builder::postProcess(Instruction& inst) |
| | { |
| | |
| | switch (inst.getOpCode()) { |
| | case OpExtInst: |
| | switch (inst.getImmediateOperand(1)) { |
| | case GLSLstd450InterpolateAtCentroid: |
| | case GLSLstd450InterpolateAtSample: |
| | case GLSLstd450InterpolateAtOffset: |
| | addCapability(CapabilityInterpolationFunction); |
| | break; |
| | default: |
| | break; |
| | } |
| | break; |
| | case OpDPdxFine: |
| | case OpDPdyFine: |
| | case OpFwidthFine: |
| | case OpDPdxCoarse: |
| | case OpDPdyCoarse: |
| | case OpFwidthCoarse: |
| | addCapability(CapabilityDerivativeControl); |
| | break; |
| |
|
| | case OpImageQueryLod: |
| | case OpImageQuerySize: |
| | case OpImageQuerySizeLod: |
| | case OpImageQuerySamples: |
| | case OpImageQueryLevels: |
| | addCapability(CapabilityImageQuery); |
| | break; |
| |
|
| | case OpGroupNonUniformPartitionNV: |
| | addExtension(E_SPV_NV_shader_subgroup_partitioned); |
| | addCapability(CapabilityGroupNonUniformPartitionedNV); |
| | break; |
| |
|
| | case OpLoad: |
| | case OpStore: |
| | { |
| | |
| | |
| | |
| | |
| | |
| | Instruction *accessChain = module.getInstruction(inst.getIdOperand(0)); |
| | if (accessChain->getOpCode() == OpAccessChain) { |
| | Instruction *base = module.getInstruction(accessChain->getIdOperand(0)); |
| | |
| | Id typeId = base->getTypeId(); |
| | Instruction *type = module.getInstruction(typeId); |
| | assert(type->getOpCode() == OpTypePointer); |
| | if (type->getImmediateOperand(0) != StorageClassPhysicalStorageBufferEXT) { |
| | break; |
| | } |
| | |
| | typeId = type->getIdOperand(1); |
| | type = module.getInstruction(typeId); |
| | |
| | |
| | |
| | |
| | int alignment = 0; |
| | for (int i = 1; i < accessChain->getNumOperands(); ++i) { |
| | Instruction *idx = module.getInstruction(accessChain->getIdOperand(i)); |
| | if (type->getOpCode() == OpTypeStruct) { |
| | assert(idx->getOpCode() == OpConstant); |
| | unsigned int c = idx->getImmediateOperand(0); |
| |
|
| | const auto function = [&](const std::unique_ptr<Instruction>& decoration) { |
| | if (decoration.get()->getOpCode() == OpMemberDecorate && |
| | decoration.get()->getIdOperand(0) == typeId && |
| | decoration.get()->getImmediateOperand(1) == c && |
| | (decoration.get()->getImmediateOperand(2) == DecorationOffset || |
| | decoration.get()->getImmediateOperand(2) == DecorationMatrixStride)) { |
| | alignment |= decoration.get()->getImmediateOperand(3); |
| | } |
| | }; |
| | std::for_each(decorations.begin(), decorations.end(), function); |
| | |
| | typeId = type->getIdOperand(c); |
| | type = module.getInstruction(typeId); |
| | } else if (type->getOpCode() == OpTypeArray || |
| | type->getOpCode() == OpTypeRuntimeArray) { |
| | const auto function = [&](const std::unique_ptr<Instruction>& decoration) { |
| | if (decoration.get()->getOpCode() == OpDecorate && |
| | decoration.get()->getIdOperand(0) == typeId && |
| | decoration.get()->getImmediateOperand(1) == DecorationArrayStride) { |
| | alignment |= decoration.get()->getImmediateOperand(2); |
| | } |
| | }; |
| | std::for_each(decorations.begin(), decorations.end(), function); |
| | |
| | typeId = type->getIdOperand(0); |
| | type = module.getInstruction(typeId); |
| | } else { |
| | |
| | break; |
| | } |
| | } |
| | assert(inst.getNumOperands() >= 3); |
| | unsigned int memoryAccess = inst.getImmediateOperand((inst.getOpCode() == OpStore) ? 2 : 1); |
| | assert(memoryAccess & MemoryAccessAlignedMask); |
| | static_cast<void>(memoryAccess); |
| | |
| | int alignmentIdx = 2; |
| | if (inst.getOpCode() == OpStore) |
| | alignmentIdx++; |
| | |
| | alignment |= inst.getImmediateOperand(alignmentIdx); |
| | |
| | alignment = alignment & ~(alignment & (alignment-1)); |
| | |
| | inst.setImmediateOperand(alignmentIdx, alignment); |
| | } |
| | break; |
| | } |
| |
|
| | default: |
| | break; |
| | } |
| |
|
| | |
| | if (inst.getTypeId() != NoType) |
| | postProcessType(inst, inst.getTypeId()); |
| | for (int op = 0; op < inst.getNumOperands(); ++op) { |
| | if (inst.isIdOperand(op)) { |
| | |
| | |
| | if (getTypeId(inst.getIdOperand(op)) != NoType) |
| | postProcessType(inst, getTypeId(inst.getIdOperand(op))); |
| | } |
| | } |
| | } |
| | #endif |
| |
|
| | |
| | void Builder::postProcessCFG() |
| | { |
| | |
| | |
| | std::unordered_set<const Block*> reachableBlocks; |
| | std::unordered_map<Block*, Block*> headerForUnreachableContinue; |
| | std::unordered_set<Block*> unreachableMerges; |
| | std::unordered_set<Id> unreachableDefinitions; |
| | |
| | |
| | |
| | for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) { |
| | Function* f = *fi; |
| | Block* entry = f->getEntryBlock(); |
| | inReadableOrder(entry, |
| | [&reachableBlocks, &unreachableMerges, &headerForUnreachableContinue] |
| | (Block* b, ReachReason why, Block* header) { |
| | reachableBlocks.insert(b); |
| | if (why == ReachDeadContinue) headerForUnreachableContinue[b] = header; |
| | if (why == ReachDeadMerge) unreachableMerges.insert(b); |
| | }); |
| | for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) { |
| | Block* b = *bi; |
| | if (unreachableMerges.count(b) != 0 || headerForUnreachableContinue.count(b) != 0) { |
| | auto ii = b->getInstructions().cbegin(); |
| | ++ii; |
| | for (; ii != b->getInstructions().cend(); ++ii) |
| | unreachableDefinitions.insert(ii->get()->getResultId()); |
| | } else if (reachableBlocks.count(b) == 0) { |
| | |
| | for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ++ii) |
| | unreachableDefinitions.insert(ii->get()->getResultId()); |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | for (auto mergeIter = unreachableMerges.begin(); mergeIter != unreachableMerges.end(); ++mergeIter) { |
| | (*mergeIter)->rewriteAsCanonicalUnreachableMerge(); |
| | } |
| | for (auto continueIter = headerForUnreachableContinue.begin(); |
| | continueIter != headerForUnreachableContinue.end(); |
| | ++continueIter) { |
| | Block* continue_target = continueIter->first; |
| | Block* header = continueIter->second; |
| | continue_target->rewriteAsCanonicalUnreachableContinue(header); |
| | } |
| |
|
| | |
| | decorations.erase(std::remove_if(decorations.begin(), decorations.end(), |
| | [&unreachableDefinitions](std::unique_ptr<Instruction>& I) -> bool { |
| | Id decoration_id = I.get()->getIdOperand(0); |
| | return unreachableDefinitions.count(decoration_id) != 0; |
| | }), |
| | decorations.end()); |
| | } |
| |
|
| | #ifndef GLSLANG_WEB |
| | |
| | void Builder::postProcessFeatures() { |
| | |
| |
|
| | |
| | |
| | |
| | for (int t = 0; t < (int)groupedTypes[OpTypePointer].size(); ++t) { |
| | Instruction* type = groupedTypes[OpTypePointer][t]; |
| | if (type->getImmediateOperand(0) == (unsigned)StorageClassPhysicalStorageBufferEXT) { |
| | if (containsType(type->getIdOperand(1), OpTypeInt, 8)) { |
| | addIncorporatedExtension(spv::E_SPV_KHR_8bit_storage, spv::Spv_1_5); |
| | addCapability(spv::CapabilityStorageBuffer8BitAccess); |
| | } |
| | if (containsType(type->getIdOperand(1), OpTypeInt, 16) || |
| | containsType(type->getIdOperand(1), OpTypeFloat, 16)) { |
| | addIncorporatedExtension(spv::E_SPV_KHR_16bit_storage, spv::Spv_1_3); |
| | addCapability(spv::CapabilityStorageBuffer16BitAccess); |
| | } |
| | } |
| | } |
| |
|
| | |
| | for (auto fi = module.getFunctions().cbegin(); fi != module.getFunctions().cend(); fi++) { |
| | Function* f = *fi; |
| | for (auto bi = f->getBlocks().cbegin(); bi != f->getBlocks().cend(); bi++) { |
| | Block* b = *bi; |
| | for (auto ii = b->getInstructions().cbegin(); ii != b->getInstructions().cend(); ii++) |
| | postProcess(*ii->get()); |
| |
|
| | |
| | |
| | |
| | for (auto vi = b->getLocalVariables().cbegin(); vi != b->getLocalVariables().cend(); vi++) { |
| | const Instruction& inst = *vi->get(); |
| | Id resultId = inst.getResultId(); |
| | if (containsPhysicalStorageBufferOrArray(getDerefTypeId(resultId))) { |
| | bool foundDecoration = false; |
| | const auto function = [&](const std::unique_ptr<Instruction>& decoration) { |
| | if (decoration.get()->getIdOperand(0) == resultId && |
| | decoration.get()->getOpCode() == OpDecorate && |
| | (decoration.get()->getImmediateOperand(1) == spv::DecorationAliasedPointerEXT || |
| | decoration.get()->getImmediateOperand(1) == spv::DecorationRestrictPointerEXT)) { |
| | foundDecoration = true; |
| | } |
| | }; |
| | std::for_each(decorations.begin(), decorations.end(), function); |
| | if (!foundDecoration) { |
| | addDecoration(resultId, spv::DecorationAliasedPointerEXT); |
| | } |
| | } |
| | } |
| | } |
| | } |
| |
|
| | |
| | |
| | if (capabilities.find(spv::CapabilityVulkanMemoryModelKHR) != capabilities.end()) { |
| | memoryModel = spv::MemoryModelVulkanKHR; |
| | addIncorporatedExtension(spv::E_SPV_KHR_vulkan_memory_model, spv::Spv_1_5); |
| | } |
| |
|
| | |
| | if (capabilities.find(spv::CapabilityWorkgroupMemoryExplicitLayoutKHR) != capabilities.end()) { |
| | assert(entryPoints.size() == 1); |
| | auto &ep = entryPoints[0]; |
| |
|
| | std::vector<Id> workgroup_variables; |
| | for (int i = 0; i < (int)ep->getNumOperands(); i++) { |
| | if (!ep->isIdOperand(i)) |
| | continue; |
| |
|
| | const Id id = ep->getIdOperand(i); |
| | const Instruction *instr = module.getInstruction(id); |
| | if (instr->getOpCode() != spv::OpVariable) |
| | continue; |
| |
|
| | if (instr->getImmediateOperand(0) == spv::StorageClassWorkgroup) |
| | workgroup_variables.push_back(id); |
| | } |
| |
|
| | if (workgroup_variables.size() > 1) { |
| | for (size_t i = 0; i < workgroup_variables.size(); i++) |
| | addDecoration(workgroup_variables[i], spv::DecorationAliased); |
| | } |
| | } |
| | } |
| | #endif |
| |
|
| | |
| | void Builder::postProcess() { |
| | postProcessCFG(); |
| | #ifndef GLSLANG_WEB |
| | postProcessFeatures(); |
| | #endif |
| | } |
| |
|
| | }; |
| |
|