Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 0 additions & 3 deletions layers/gpuav/spirv/buffer_device_address_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions layers/gpuav/spirv/descriptor_buffer_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions layers/gpuav/spirv/descriptor_heap_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 0 additions & 3 deletions layers/gpuav/spirv/descriptor_indexing_oob_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions layers/gpuav/spirv/mesh_shading_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
52 changes: 52 additions & 0 deletions layers/gpuav/spirv/pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
// <instructions> -> OpLoopMerge %merge %continue
// OpLoopMerge OpBranch %body
// OpBranchConditional %body = OpLabel
// <instructions>
// 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<BasicBlock>(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,
Expand Down
1 change: 1 addition & 0 deletions layers/gpuav/spirv/pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
3 changes: 0 additions & 3 deletions layers/gpuav/spirv/sanitizer_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
3 changes: 0 additions & 3 deletions layers/gpuav/spirv/shared_memory_data_race_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 0 additions & 3 deletions layers/gpuav/spirv/trace_ray_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
74 changes: 74 additions & 0 deletions tests/unit/gpu_av_buffer_device_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down
5 changes: 3 additions & 2 deletions tests/unit/gpu_av_spirv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand Down Expand Up @@ -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();
}
Loading