diff --git a/layers/gpuav/spirv/buffer_device_address_pass.cpp b/layers/gpuav/spirv/buffer_device_address_pass.cpp index 87f95195473..c2a0993d0bb 100644 --- a/layers/gpuav/spirv/buffer_device_address_pass.cpp +++ b/layers/gpuav/spirv/buffer_device_address_pass.cpp @@ -161,9 +161,6 @@ bool BufferDeviceAddressPass::Instrument() { continue; } - if (current_block.IsLoopHeader()) { - continue; // Currently can't properly handle injecting CFG logic into a loop header block - } auto& block_instructions = current_block.instructions_; if (!module_.settings_.safe_mode) { diff --git a/layers/gpuav/spirv/descriptor_buffer_pass.cpp b/layers/gpuav/spirv/descriptor_buffer_pass.cpp index 8efe8f32b87..8cbf0832f88 100644 --- a/layers/gpuav/spirv/descriptor_buffer_pass.cpp +++ b/layers/gpuav/spirv/descriptor_buffer_pass.cpp @@ -114,9 +114,6 @@ bool DescriptorBufferPass::Instrument() { continue; } - if (current_block.IsLoopHeader()) { - continue; // Currently can't properly handle injecting CFG logic into a loop header block - } auto& block_instructions = current_block.instructions_; // We only need to instrument the set/binding/index combo once per block (in unsafe mode) diff --git a/layers/gpuav/spirv/descriptor_heap_pass.cpp b/layers/gpuav/spirv/descriptor_heap_pass.cpp index 5a553ab3504..3dc44c58393 100644 --- a/layers/gpuav/spirv/descriptor_heap_pass.cpp +++ b/layers/gpuav/spirv/descriptor_heap_pass.cpp @@ -764,9 +764,6 @@ bool DescriptorHeapPass::Instrument() { continue; } - if (current_block.IsLoopHeader()) { - continue; // Currently can't properly handle injecting CFG logic into a loop header block - } auto& block_instructions = current_block.instructions_; // We only need to instrument the set/binding/index combo once per block (in unsafe mode) diff --git a/layers/gpuav/spirv/descriptor_indexing_oob_pass.cpp b/layers/gpuav/spirv/descriptor_indexing_oob_pass.cpp index 03ba91861c9..994082881b2 100644 --- a/layers/gpuav/spirv/descriptor_indexing_oob_pass.cpp +++ b/layers/gpuav/spirv/descriptor_indexing_oob_pass.cpp @@ -206,9 +206,6 @@ bool DescriptorIndexingOOBPass::Instrument() { continue; } - if (current_block.IsLoopHeader()) { - continue; // Currently can't properly handle injecting CFG logic into a loop header block - } auto& block_instructions = current_block.instructions_; // Don't clear if the new block occurs from control flow breaking one up diff --git a/layers/gpuav/spirv/mesh_shading_pass.cpp b/layers/gpuav/spirv/mesh_shading_pass.cpp index fde75dd2dc1..d7b9f9b4ef3 100644 --- a/layers/gpuav/spirv/mesh_shading_pass.cpp +++ b/layers/gpuav/spirv/mesh_shading_pass.cpp @@ -124,9 +124,6 @@ bool MeshShading::Instrument() { continue; } - if (current_block.IsLoopHeader()) { - continue; // Currently can't properly handle injecting CFG logic into a loop header block - } auto& block_instructions = current_block.instructions_; for (auto inst_it = block_instructions.begin(); inst_it != block_instructions.end(); ++inst_it) { diff --git a/layers/gpuav/spirv/pass.cpp b/layers/gpuav/spirv/pass.cpp index a40db81c396..34f1cc1f736 100644 --- a/layers/gpuav/spirv/pass.cpp +++ b/layers/gpuav/spirv/pass.cpp @@ -746,6 +746,9 @@ InjectConditionalData Pass::InjectFunctionPre(Function& function, const BasicBlo // We turn the block into 4 separate blocks BasicBlock& original_block = **original_block_it; const uint32_t original_label = original_block.GetLabelId(); + if (original_block.IsLoopHeader()) { + SplitLoopHeader(function, original_block_it, inst_it); + } // Where we call targeted instruction if it is valid BasicBlockIt valid_block_it = function.InsertNewBlock(original_block_it); @@ -842,6 +845,55 @@ InjectConditionalData Pass::InjectFunctionPre(Function& function, const BasicBlo return InjectConditionalData{merge_block_label, valid_block_label, invalid_block_label, 0, merge_block_it}; } +// Don't worry, this isn't a compiler, but it is possible (but rare) to need to validate in the loop header which break when we want +// to try and wrap the check for safe mode. +// +// The issues is the loop header can't also be a selection header, so before splitting it we move the label, OpPhi and OpLoopMerge +// into a new header block placed before it. The original block (still holding the terminator) becomes a normal block in the loop +// +// here is some LLM generated ascii visual I had generated: +// +// %header = OpLabel %header = OpLabel +// OpPhi OpPhi +// -> OpLoopMerge %merge %continue +// OpLoopMerge OpBranch %body +// OpBranchConditional %body = OpLabel +// +// OpBranchConditional +void Pass::SplitLoopHeader(Function& function, BasicBlockIt block_it, InstructionIt& inst_it) { + BasicBlock& block = **block_it; + InstructionList& insts = block.instructions_; + const uint32_t header_label = block.GetLabelId(); + const uint32_t body_label = module_.TakeNextId(); + const size_t inst_index = inst_it - insts.begin(); + + auto header = std::make_unique(std::move(insts[0])); + header->function_ = &function; + size_t moved_count = 1; + while (insts[moved_count]->Opcode() == spv::OpPhi) { + header->instructions_.push_back(std::move(insts[moved_count])); + moved_count++; + } + + auto loop_merge_it = insts.end() - 2; + if ((*loop_merge_it)->Word(2) == header_label) { + // was its own continue target + (*loop_merge_it)->UpdateWord(2, body_label); + } + header->instructions_.push_back(std::move(*loop_merge_it)); + header->CreateInstruction(spv::OpBranch, {body_label}); + header->loop_header_merge_target_ = block.loop_header_merge_target_; + block.loop_header_merge_target_ = 0; + + insts.erase(loop_merge_it); + insts.erase(insts.begin(), insts.begin() + moved_count); + InstructionIt front = insts.begin(); + block.CreateInstruction(spv::OpLabel, {body_label}, &front); + inst_it = insts.begin() + (inst_index - moved_count + 1); + + function.blocks_.insert(block_it, std::move(header)); +} + void Pass::InjectFunctionPost(BasicBlock& original_block, const InjectConditionalData& ic_data) { original_block.CreateInstruction(spv::OpSelectionMerge, {ic_data.merge_block_label, spv::SelectionControlMaskNone}); original_block.CreateInstruction(spv::OpBranchConditional, diff --git a/layers/gpuav/spirv/pass.h b/layers/gpuav/spirv/pass.h index a579a7dea79..5a490b319b1 100644 --- a/layers/gpuav/spirv/pass.h +++ b/layers/gpuav/spirv/pass.h @@ -84,6 +84,7 @@ class Pass { InjectConditionalData InjectFunctionPre(Function& function, const BasicBlockIt original_block_it, InstructionIt inst_it); void InjectFunctionPost(BasicBlock& original_block, const InjectConditionalData& ic_data); + void SplitLoopHeader(Function& function, BasicBlockIt block_it, InstructionIt& inst_it); protected: Pass(Module& module, const OfflineModule& offline); diff --git a/layers/gpuav/spirv/sanitizer_pass.cpp b/layers/gpuav/spirv/sanitizer_pass.cpp index 53bd5851eb4..635f7834c04 100644 --- a/layers/gpuav/spirv/sanitizer_pass.cpp +++ b/layers/gpuav/spirv/sanitizer_pass.cpp @@ -556,9 +556,6 @@ bool SanitizerPass::Instrument() { continue; } - if (current_block.IsLoopHeader()) { - continue; // Currently can't properly handle injecting CFG logic into a loop header block - } auto& block_instructions = current_block.instructions_; for (auto inst_it = block_instructions.begin(); inst_it != block_instructions.end(); ++inst_it) { diff --git a/layers/gpuav/spirv/shared_memory_data_race_pass.cpp b/layers/gpuav/spirv/shared_memory_data_race_pass.cpp index 6c11d7c657a..9fbd2eb0efb 100644 --- a/layers/gpuav/spirv/shared_memory_data_race_pass.cpp +++ b/layers/gpuav/spirv/shared_memory_data_race_pass.cpp @@ -402,9 +402,6 @@ bool SharedMemoryDataRacePass::Instrument() { continue; } - if (current_block.IsLoopHeader()) { - continue; // Currently can't properly handle injecting CFG logic into a loop header block - } auto& block_instructions = current_block.instructions_; // Call init_shadow at the start of the entry point diff --git a/layers/gpuav/spirv/trace_ray_pass.cpp b/layers/gpuav/spirv/trace_ray_pass.cpp index 4efeaed96a5..fa6ecc40d1e 100644 --- a/layers/gpuav/spirv/trace_ray_pass.cpp +++ b/layers/gpuav/spirv/trace_ray_pass.cpp @@ -241,9 +241,6 @@ bool TraceRayPass::Instrument() { continue; } - if (current_block.IsLoopHeader()) { - continue; // Currently can't properly handle injecting CFG logic into a loop header block - } auto& block_instructions = current_block.instructions_; for (auto inst_it = block_instructions.begin(); inst_it != block_instructions.end(); ++inst_it) { diff --git a/tests/unit/gpu_av_buffer_device_address.cpp b/tests/unit/gpu_av_buffer_device_address.cpp index 2881feff243..2e102d8b209 100644 --- a/tests/unit/gpu_av_buffer_device_address.cpp +++ b/tests/unit/gpu_av_buffer_device_address.cpp @@ -1390,6 +1390,80 @@ TEST_F(NegativeGpuAVBufferDeviceAddress, ProxyStructLoadSlang) { m_errorMonitor->VerifyFound(); } +TEST_F(NegativeGpuAVBufferDeviceAddress, SingleBlockLoop) { + RETURN_IF_SKIP(InitGpuVUBufferDeviceAddress()); + + const char* shader_source = R"( + OpCapability Shader + OpCapability PhysicalStorageBufferAddresses + OpExtension "SPV_KHR_physical_storage_buffer" + OpMemoryModel PhysicalStorageBuffer64 GLSL450 + OpEntryPoint GLCompute %main "main" %pc + OpExecutionMode %main LocalSize 1 1 1 + OpDecorate %pc_block Block + OpMemberDecorate %pc_block 0 Offset 0 + OpDecorate %rta ArrayStride 4 + OpDecorate %buf Block + OpMemberDecorate %buf 0 Offset 0 + %void = OpTypeVoid + %func_t = OpTypeFunction %void + %int = OpTypeInt 32 1 + %int_0 = OpConstant %int 0 + %int_1 = OpConstant %int 1 + %int_5 = OpConstant %int 5 + %bool = OpTypeBool + %rta = OpTypeRuntimeArray %int + %buf = OpTypeStruct %rta + %buf_ptr = OpTypePointer PhysicalStorageBuffer %buf + %pc_block = OpTypeStruct %buf_ptr + %pc_ptr = OpTypePointer PushConstant %pc_block +%pc_buf_ptr = OpTypePointer PushConstant %buf_ptr + %int_ptr = OpTypePointer PhysicalStorageBuffer %int + %pc = OpVariable %pc_ptr PushConstant + %main = OpFunction %void None %func_t + %entry = OpLabel + %pc_ac = OpAccessChain %pc_buf_ptr %pc %int_0 + %bda = OpLoad %buf_ptr %pc_ac + OpBranch %loop + %loop = OpLabel + %i = OpPhi %int %int_0 %entry %i_next %loop + %sum = OpPhi %int %int_0 %entry %sum_next %loop + %ac_i = OpAccessChain %int_ptr %bda %int_0 %i + %val = OpLoad %int %ac_i Aligned 4 + %sum_next = OpIAdd %int %sum %val + %i_next = OpIAdd %int %i %int_1 + %cond = OpSLessThan %bool %i_next %int_5 + OpLoopMerge %merge %loop None + OpBranchConditional %cond %loop %merge + %merge = OpLabel + %ac_0 = OpAccessChain %int_ptr %bda %int_0 %int_0 + OpStore %ac_0 %sum_next Aligned 4 + OpReturn + OpFunctionEnd + )"; + + vkt::Buffer bda_buffer(*m_device, 16, 0, vkt::device_address); + VkPushConstantRange pc_range = {VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(VkDeviceAddress)}; + const vkt::PipelineLayout pipeline_layout(*m_device, {}, {pc_range}); + + CreateComputePipelineHelper pipe(*this); + pipe.cs_ = VkShaderObj(*m_device, shader_source, VK_SHADER_STAGE_COMPUTE_BIT, SPV_ENV_VULKAN_1_2, SPV_SOURCE_ASM); + pipe.cp_ci_.layout = pipeline_layout; + pipe.CreateComputePipeline(); + + m_command_buffer.Begin(); + VkDeviceAddress bda_buffer_addr = bda_buffer.Address(); + vk::CmdPushConstants(m_command_buffer, pipeline_layout, VK_SHADER_STAGE_COMPUTE_BIT, 0, sizeof(VkDeviceAddress), + &bda_buffer_addr); + vk::CmdBindPipeline(m_command_buffer, VK_PIPELINE_BIND_POINT_COMPUTE, pipe); + vk::CmdDispatch(m_command_buffer, 1, 1, 1); + m_command_buffer.End(); + + m_errorMonitor->SetDesiredError("VUID-RuntimeSpirv-PhysicalStorageBuffer64-11819"); + m_default_queue->SubmitAndWait(m_command_buffer); + m_errorMonitor->VerifyFound(); +} + TEST_F(NegativeGpuAVBufferDeviceAddress, ProxyStructLoadUint64) { TEST_DESCRIPTION("https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/8073"); RETURN_IF_SKIP(InitGpuVUBufferDeviceAddress()); diff --git a/tests/unit/gpu_av_spirv.cpp b/tests/unit/gpu_av_spirv.cpp index d45dec767dd..1ea189a2be1 100644 --- a/tests/unit/gpu_av_spirv.cpp +++ b/tests/unit/gpu_av_spirv.cpp @@ -21,8 +21,7 @@ class NegativeGpuAVSpirv : public GpuAVTest {}; -// https://github.com/KhronosGroup/Vulkan-ValidationLayers/issues/7462 -TEST_F(NegativeGpuAVSpirv, DISABLED_LoopHeaderPhi) { +TEST_F(NegativeGpuAVSpirv, LoopHeaderPhi) { TEST_DESCRIPTION("Require injection in the Loop Header block that contains a Phi"); SetTargetApiVersion(VK_API_VERSION_1_2); RETURN_IF_SKIP(InitGpuAvFramework()); @@ -112,5 +111,7 @@ TEST_F(NegativeGpuAVSpirv, DISABLED_LoopHeaderPhi) { vk::CmdDispatch(m_command_buffer, 1, 1, 1); m_command_buffer.End(); + m_errorMonitor->SetDesiredError("VUID-vkCmdDispatch-storageBuffers-06936"); m_default_queue->SubmitAndWait(m_command_buffer); + m_errorMonitor->VerifyFound(); }