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
22 changes: 12 additions & 10 deletions source/opt/convert_to_untyped.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -325,8 +325,7 @@ Instruction* ConvertToUntyped::ConvertPointerType(Instruction* inst) {
void ConvertToUntyped::ConvertVariable(Instruction* inst) {
auto ptr_ty =
context()->get_type_mgr()->GetType(inst->type_id())->AsPointer();
auto data =
context()->get_type_mgr()->GetTypeInstruction(ptr_ty->pointee_type());
auto data = pointer_data_type_ids_.at(inst->type_id());
// Rewrite inst in place. This maintains dominance ordering.
inst->SetOpcode(spv::Op::OpUntypedVariableKHR);
inst->SetInOperands(std::initializer_list<Operand>{
Expand Down Expand Up @@ -355,10 +354,7 @@ void ConvertToUntyped::ConvertAccessChain(Instruction* inst) {
}
auto base =
context()->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0));
auto ptr_ty =
context()->get_type_mgr()->GetType(base->type_id())->AsPointer();
auto data =
context()->get_type_mgr()->GetTypeInstruction(ptr_ty->pointee_type());
auto data = pointer_data_type_ids_.at(base->type_id());
// Add the base type, but then reuse all the other operands.
std::vector<Operand> operands;
operands.reserve(inst->NumInOperands() + 1);
Expand All @@ -373,10 +369,7 @@ void ConvertToUntyped::ConvertAccessChain(Instruction* inst) {
void ConvertToUntyped::ConvertArrayLength(Instruction* inst) {
auto structure =
context()->get_def_use_mgr()->GetDef(inst->GetSingleWordInOperand(0));
auto ptr_ty =
context()->get_type_mgr()->GetType(structure->type_id())->AsPointer();
auto data =
context()->get_type_mgr()->GetTypeInstruction(ptr_ty->pointee_type());
auto data = pointer_data_type_ids_.at(structure->type_id());
// Add the structure type, but then reuse all the other operands.
std::vector<Operand> operands;
operands.reserve(inst->NumInOperands() + 1);
Expand Down Expand Up @@ -750,6 +743,15 @@ bool ConvertToUntyped::ConvertPointers() {
return false;
}

// TypeManager canonicalizes structurally identical types, but untyped
// instructions must preserve the exact pointee type used by each original
// typed pointer. Save those ids before pointer types mutate.
for (auto* inst : to_convert) {
if (inst->opcode() == spv::Op::OpTypePointer)
pointer_data_type_ids_[inst->result_id()] =
inst->GetSingleWordInOperand(1);
}

for (auto* inst : to_convert) {
Convert(inst);
}
Expand Down
3 changes: 3 additions & 0 deletions source/opt/convert_to_untyped.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,9 @@ class ConvertToUntyped : public Pass {
std::unordered_map<uint32_t, Instruction*> base_ptrs_{};
// Tracks result id to its replacement result id
std::unordered_map<uint32_t, uint32_t> remapped_ids_{};
// Tracks each pointer's exact pointee type before pointer types are
// converted and de-duplicated.
std::unordered_map<uint32_t, uint32_t> pointer_data_type_ids_{};
// Converted instructions that need deleted.
std::vector<Instruction*> to_delete_{};

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

TEST_F(ConvertToUntypedTest, PreserveExactPointeeTypes) {
const std::string text = R"(
; CHECK: %var0 = OpUntypedVariableKHR %{{\w+}} StorageBuffer %block0
; CHECK: %var1 = OpUntypedVariableKHR %{{\w+}} StorageBuffer %block1
; CHECK: %access0 = OpUntypedAccessChainKHR %{{\w+}} %block0 %var0
; CHECK: %access1 = OpUntypedAccessChainKHR %{{\w+}} %block1 %var1
; CHECK: %length = OpUntypedArrayLengthKHR %uint %block1 %var1 0
OpCapability Shader
OpMemoryModel Logical GLSL450
OpEntryPoint GLCompute %main "main"
OpExecutionMode %main LocalSize 1 1 1
OpName %block0 "block0"
OpName %block1 "block1"
OpName %var0 "var0"
OpName %var1 "var1"
OpName %access0 "access0"
OpName %access1 "access1"
OpName %length "length"
OpDecorate %array0 ArrayStride 4
OpDecorate %array1 ArrayStride 4
OpDecorate %block0 Block
OpDecorate %block1 Block
OpMemberDecorate %block0 0 Offset 0
OpMemberDecorate %block1 0 Offset 0
%void = OpTypeVoid
%void_fn = OpTypeFunction %void
%uint = OpTypeInt 32 0
%uint_0 = OpConstant %uint 0
%array0 = OpTypeRuntimeArray %uint
%array1 = OpTypeRuntimeArray %uint
%block0 = OpTypeStruct %array0
%block1 = OpTypeStruct %array1
%ptr0 = OpTypePointer StorageBuffer %block0
%ptr1 = OpTypePointer StorageBuffer %block1
%ptr_uint = OpTypePointer StorageBuffer %uint
%var0 = OpVariable %ptr0 StorageBuffer
%var1 = OpVariable %ptr1 StorageBuffer
%main = OpFunction %void None %void_fn
%entry = OpLabel
%access0 = OpAccessChain %ptr_uint %var0 %uint_0 %uint_0
%access1 = OpAccessChain %ptr_uint %var1 %uint_0 %uint_0
%length = OpArrayLength %uint %var1 0
OpReturn
OpFunctionEnd
)";

SinglePassRunAndMatch<opt::ConvertToUntyped>(text, true);
}

TEST_F(ConvertToUntypedTest, ArrayLength) {
const std::string text = R"(
; CHECK: [[uint:%\w+]] = OpTypeInt
Expand Down
Loading