Skip to content

Commit

Permalink
Add AliasedPointer decoration
Browse files Browse the repository at this point in the history
Fix KhronosGroup#5607

When inlining, decorate return variable with AliasedPointer if the
storage class of the pointee type is PhysicalStorageBuffer.
  • Loading branch information
jeremy-lunarg committed Apr 4, 2024
1 parent 3a0471c commit 85db945
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 0 deletions.
13 changes: 13 additions & 0 deletions source/opt/inline_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,6 +213,19 @@ uint32_t InlinePass::CreateReturnVar(
{(uint32_t)spv::StorageClass::Function}}}));
new_vars->push_back(std::move(var_inst));
get_decoration_mgr()->CloneDecorations(calleeFn->result_id(), returnVarId);

// Decorate the return var with AliasedPointer if the storage class of the
// pointee type is PhysicalStorageBuffer.
auto const pointee_type =
type_mgr->GetType(returnVarTypeId)->AsPointer()->pointee_type();
if (pointee_type->AsPointer() != nullptr) {
if (pointee_type->AsPointer()->storage_class() ==
spv::StorageClass::PhysicalStorageBuffer) {
get_decoration_mgr()->AddDecoration(
returnVarId, uint32_t(spv::Decoration::AliasedPointer));
}
}

return returnVarId;
}

Expand Down
49 changes: 49 additions & 0 deletions test/opt/inline_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4422,6 +4422,55 @@ OpFunctionEnd
SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
}

TEST_F(InlineTest, DecorateReturnVariableWithAliasedPointer) {
const std::string text = R"(OpCapability Int64
OpCapability VariablePointers
OpCapability PhysicalStorageBufferAddresses
OpCapability Shader
OpExtension "SPV_KHR_storage_buffer_storage_class"
OpExtension "SPV_KHR_variable_pointers"
OpExtension "SPV_KHR_physical_storage_buffer"
OpMemoryModel PhysicalStorageBuffer64 GLSL450
OpEntryPoint GLCompute %1 "main"
OpExecutionMode %1 LocalSize 8 8 1
OpDecorate %_ptr_PhysicalStorageBuffer__struct_5 ArrayStride 8
OpMemberDecorate %_struct_3 0 Offset 0
OpMemberDecorate %_struct_3 1 Offset 8
OpDecorate %_ptr_PhysicalStorageBuffer_int ArrayStride 4
OpMemberDecorate %_struct_5 0 Offset 0
OpMemberDecorate %_struct_5 1 Offset 4
OpDecorate %6 Aliased
; CHECK: OpDecorate %22 AliasedPointer
%void = OpTypeVoid
%8 = OpTypeFunction %void
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
OpTypeForwardPointer %_ptr_PhysicalStorageBuffer__struct_5 PhysicalStorageBuffer
%_struct_3 = OpTypeStruct %int %_ptr_PhysicalStorageBuffer__struct_5
%_ptr_PhysicalStorageBuffer_int = OpTypePointer PhysicalStorageBuffer %int
%_struct_5 = OpTypeStruct %int %int
%11 = OpTypeFunction %_ptr_PhysicalStorageBuffer_int %_ptr_PhysicalStorageBuffer__struct_5
%_ptr_PhysicalStorageBuffer__struct_5 = OpTypePointer PhysicalStorageBuffer %_struct_5
%_ptr_Function__struct_3 = OpTypePointer Function %_struct_3
%1 = OpFunction %void None %8
%13 = OpLabel
%14 = OpVariable %_ptr_Function__struct_3 Function
%15 = OpLoad %_struct_3 %14
%16 = OpCompositeExtract %_ptr_PhysicalStorageBuffer__struct_5 %15 1
%17 = OpFunctionCall %_ptr_PhysicalStorageBuffer_int %18 %16
OpReturn
OpFunctionEnd
%18 = OpFunction %_ptr_PhysicalStorageBuffer_int None %11
%6 = OpFunctionParameter %_ptr_PhysicalStorageBuffer__struct_5
%19 = OpLabel
%20 = OpAccessChain %_ptr_PhysicalStorageBuffer_int %6 %int_0
OpReturnValue %20
OpFunctionEnd)";

SetTargetEnv(SPV_ENV_VULKAN_1_2);
SinglePassRunAndMatch<InlineExhaustivePass>(text, true);
}

// TODO(greg-lunarg): Add tests to verify handling of these cases:
//
// Empty modules
Expand Down

0 comments on commit 85db945

Please # to comment.