Skip to content

Commit

Permalink
Add structs to eliminate dead input components
Browse files Browse the repository at this point in the history
Will eliminate all trailing members of input struct that are not
referenced.
  • Loading branch information
greg-lunarg committed Aug 12, 2022
1 parent 3a8a961 commit 917b601
Show file tree
Hide file tree
Showing 3 changed files with 129 additions and 20 deletions.
79 changes: 60 additions & 19 deletions source/opt/eliminate_dead_input_components_pass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,21 +56,31 @@ Pass::Status EliminateDeadInputComponentsPass::Process() {
continue;
}
const analysis::Array* arr_type = ptr_type->pointee_type()->AsArray();
if (arr_type == nullptr) {
if (arr_type != nullptr) {
unsigned arr_len_id = arr_type->LengthId();
Instruction* arr_len_inst = def_use_mgr->GetDef(arr_len_id);
if (arr_len_inst->opcode() != SpvOpConstant) {
continue;
}
// SPIR-V requires array size is >= 1, so this works for signed or
// unsigned size
unsigned original_max =
arr_len_inst->GetSingleWordInOperand(kConstantValueInIdx) - 1;
unsigned max_idx = FindMaxIndex(var, original_max);
if (max_idx != original_max) {
ChangeArrayLength(var, max_idx + 1);
modified = true;
}
continue;
}
unsigned arr_len_id = arr_type->LengthId();
Instruction* arr_len_inst = def_use_mgr->GetDef(arr_len_id);
if (arr_len_inst->opcode() != SpvOpConstant) {
const analysis::Struct* struct_type = ptr_type->pointee_type()->AsStruct();
if (struct_type == nullptr)
continue;
}
// SPIR-V requires array size is >= 1, so this works for signed or
// unsigned size
unsigned original_max =
arr_len_inst->GetSingleWordInOperand(kConstantValueInIdx) - 1;
const auto elt_types = struct_type->element_types();
unsigned original_max = static_cast<unsigned>(elt_types.size()) - 1;
unsigned max_idx = FindMaxIndex(var, original_max);
if (max_idx != original_max) {
ChangeArrayLength(var, max_idx + 1);
ChangeStructLength(var, max_idx + 1);
modified = true;
}
}
Expand Down Expand Up @@ -116,12 +126,12 @@ unsigned EliminateDeadInputComponentsPass::FindMaxIndex(Instruction& var,
return seen_non_const_ac ? original_max : max;
}

void EliminateDeadInputComponentsPass::ChangeArrayLength(Instruction& arr,
void EliminateDeadInputComponentsPass::ChangeArrayLength(Instruction& arr_var,
unsigned length) {
analysis::TypeManager* type_mgr = context()->get_type_mgr();
analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
analysis::Pointer* ptr_type = type_mgr->GetType(arr.type_id())->AsPointer();
analysis::Pointer* ptr_type = type_mgr->GetType(arr_var.type_id())->AsPointer();
const analysis::Array* arr_ty = ptr_type->pointee_type()->AsArray();
assert(arr_ty && "expecting array type");
uint32_t length_id = const_mgr->GetUIntConst(length);
Expand All @@ -131,15 +141,46 @@ void EliminateDeadInputComponentsPass::ChangeArrayLength(Instruction& arr,
analysis::Pointer new_ptr_ty(reg_new_arr_ty, SpvStorageClassInput);
analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
arr.SetResultType(new_ptr_ty_id);
def_use_mgr->AnalyzeInstUse(&arr);
// Move array OpVariable instruction after its new type to preserve order
USE_ASSERT(arr.GetSingleWordInOperand(kVariableStorageClassInIdx) !=
arr_var.SetResultType(new_ptr_ty_id);
def_use_mgr->AnalyzeInstUse(&arr_var);
// Move arr_var after its new type to preserve order
USE_ASSERT(arr_var.GetSingleWordInOperand(kVariableStorageClassInIdx) !=
SpvStorageClassFunction &&
"cannot move Function variable");
"cannot move Function variable");
Instruction* new_ptr_ty_inst = def_use_mgr->GetDef(new_ptr_ty_id);
arr_var.RemoveFromList();
arr_var.InsertAfter(new_ptr_ty_inst);
}

void EliminateDeadInputComponentsPass::ChangeStructLength(Instruction& struct_var,
unsigned length) {
analysis::TypeManager* type_mgr = context()->get_type_mgr();
analysis::Pointer* ptr_type = type_mgr->GetType(struct_var.type_id())->AsPointer();
const analysis::Struct* struct_ty = ptr_type->pointee_type()->AsStruct();
assert(struct_ty && "expecting struct type");
const auto orig_elt_types = struct_ty->element_types();
std::vector<const analysis::Type*> new_elt_types;
for (unsigned u = 0; u < length; ++u)
new_elt_types.push_back(orig_elt_types[u]);
analysis::Struct new_struct_ty(new_elt_types);
analysis::Type* reg_new_struct_ty = type_mgr->GetRegisteredType(&new_struct_ty);
uint32_t new_struct_ty_id = type_mgr->GetTypeInstruction(reg_new_struct_ty);
uint32_t old_struct_ty_id = type_mgr->GetTypeInstruction(struct_ty);
analysis::DecorationManager* deco_mgr = context()->get_decoration_mgr();
deco_mgr->CloneDecorations(old_struct_ty_id, new_struct_ty_id);
analysis::Pointer new_ptr_ty(reg_new_struct_ty, SpvStorageClassInput);
analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
struct_var.SetResultType(new_ptr_ty_id);
analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
def_use_mgr->AnalyzeInstUse(&struct_var);
// Move struct_var after its new type to preserve order
USE_ASSERT(struct_var.GetSingleWordInOperand(kVariableStorageClassInIdx) !=
SpvStorageClassFunction &&
"cannot move Function variable");
Instruction* new_ptr_ty_inst = def_use_mgr->GetDef(new_ptr_ty_id);
arr.RemoveFromList();
arr.InsertAfter(new_ptr_ty_inst);
struct_var.RemoveFromList();
struct_var.InsertAfter(new_ptr_ty_inst);
}

} // namespace opt
Expand Down
6 changes: 5 additions & 1 deletion source/opt/eliminate_dead_input_components_pass.h
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,8 @@ class EliminateDeadInputComponentsPass : public Pass {
public:
explicit EliminateDeadInputComponentsPass() {}

const char* name() const override { return "reduce-load-size"; }
const char* name() const override { return "eliminate-dead-input-components"; }

Status Process() override;

// Return the mask of preserved Analyses.
Expand All @@ -51,6 +52,9 @@ class EliminateDeadInputComponentsPass : public Pass {

// Change the length of the array |inst| to |length|
void ChangeArrayLength(Instruction& inst, unsigned length);

// Change the length of the struct |struct_var| to |length|
void ChangeStructLength(Instruction& struct_var, unsigned length);
};

} // namespace opt
Expand Down
64 changes: 64 additions & 0 deletions test/opt/eliminate_dead_input_components_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -399,6 +399,70 @@ TEST_F(ElimDeadInputComponentsTest, NoElimNonIndexedAccessChain) {
SinglePassRunAndMatch<EliminateDeadInputComponentsPass>(text, true);
}

TEST_F(ElimDeadInputComponentsTest, ElimStructMember) {
// Should eliminate uv
//
// #version 450
//
// in Vertex {
// vec4 Cd;
// vec2 uv;
// } iVert;
//
// out vec4 fragColor;
//
// void main()
// {
// vec4 color = vec4(iVert.Cd);
// fragColor = color;
// }
const std::string text = R"(
OpCapability Shader
%1 = OpExtInstImport "GLSL.std.450"
OpMemoryModel Logical GLSL450
OpEntryPoint Fragment %main "main" %iVert %fragColor
OpExecutionMode %main OriginUpperLeft
OpSource GLSL 450
OpName %main "main"
OpName %Vertex "Vertex"
OpMemberName %Vertex 0 "Cd"
OpMemberName %Vertex 1 "uv"
OpName %iVert "iVert"
OpName %fragColor "fragColor"
OpDecorate %Vertex Block
OpDecorate %iVert Location 0
OpDecorate %fragColor Location 0
%void = OpTypeVoid
%3 = OpTypeFunction %void
%float = OpTypeFloat 32
%v4float = OpTypeVector %float 4
%v2float = OpTypeVector %float 2
%Vertex = OpTypeStruct %v4float %v2float
; CHECK: %Vertex = OpTypeStruct %v4float %v2float
; CHECK: [[sty:%\w+]] = OpTypeStruct %v4float
%_ptr_Input_Vertex = OpTypePointer Input %Vertex
; CHECK: [[pty:%\w+]] = OpTypePointer Input [[sty]]
%iVert = OpVariable %_ptr_Input_Vertex Input
; CHECK: %iVert = OpVariable [[pty]] Input
%int = OpTypeInt 32 1
%int_0 = OpConstant %int 0
%_ptr_Input_v4float = OpTypePointer Input %v4float
%_ptr_Output_v4float = OpTypePointer Output %v4float
%fragColor = OpVariable %_ptr_Output_v4float Output
%main = OpFunction %void None %3
%5 = OpLabel
%17 = OpAccessChain %_ptr_Input_v4float %iVert %int_0
%18 = OpLoad %v4float %17
OpStore %fragColor %18
OpReturn
OpFunctionEnd
)";

SetTargetEnv(SPV_ENV_VULKAN_1_3);
SetAssembleOptions(SPV_TEXT_TO_BINARY_OPTION_PRESERVE_NUMERIC_IDS);
SinglePassRunAndMatch<EliminateDeadInputComponentsPass>(text, true, false, false);
}

} // namespace
} // namespace opt
} // namespace spvtools

0 comments on commit 917b601

Please sign in to comment.