1 // Copyright (c) 2022 The Khronos Group Inc.
2 // Copyright (c) 2022 LunarG Inc.
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "source/opt/eliminate_dead_input_components_pass.h"
17
18 #include <set>
19 #include <vector>
20
21 #include "source/opt/instruction.h"
22 #include "source/opt/ir_builder.h"
23 #include "source/opt/ir_context.h"
24 #include "source/util/bit_vector.h"
25
26 namespace {
27
28 const uint32_t kAccessChainBaseInIdx = 0;
29 const uint32_t kAccessChainIndex0InIdx = 1;
30 const uint32_t kConstantValueInIdx = 0;
31 const uint32_t kVariableStorageClassInIdx = 0;
32
33 } // namespace
34
35 namespace spvtools {
36 namespace opt {
37
Process()38 Pass::Status EliminateDeadInputComponentsPass::Process() {
39 // Current functionality assumes shader capability
40 if (!context()->get_feature_mgr()->HasCapability(SpvCapabilityShader))
41 return Status::SuccessWithoutChange;
42 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
43 analysis::TypeManager* type_mgr = context()->get_type_mgr();
44 bool modified = false;
45 std::vector<std::pair<Instruction*, unsigned>> arrays_to_change;
46 for (auto& var : context()->types_values()) {
47 if (var.opcode() != SpvOpVariable) {
48 continue;
49 }
50 analysis::Type* var_type = type_mgr->GetType(var.type_id());
51 analysis::Pointer* ptr_type = var_type->AsPointer();
52 if (ptr_type == nullptr) {
53 continue;
54 }
55 if (ptr_type->storage_class() != SpvStorageClassInput) {
56 continue;
57 }
58 const analysis::Array* arr_type = ptr_type->pointee_type()->AsArray();
59 if (arr_type != nullptr) {
60 unsigned arr_len_id = arr_type->LengthId();
61 Instruction* arr_len_inst = def_use_mgr->GetDef(arr_len_id);
62 if (arr_len_inst->opcode() != SpvOpConstant) {
63 continue;
64 }
65 // SPIR-V requires array size is >= 1, so this works for signed or
66 // unsigned size
67 unsigned original_max =
68 arr_len_inst->GetSingleWordInOperand(kConstantValueInIdx) - 1;
69 unsigned max_idx = FindMaxIndex(var, original_max);
70 if (max_idx != original_max) {
71 ChangeArrayLength(var, max_idx + 1);
72 modified = true;
73 }
74 continue;
75 }
76 const analysis::Struct* struct_type = ptr_type->pointee_type()->AsStruct();
77 if (struct_type == nullptr) continue;
78 const auto elt_types = struct_type->element_types();
79 unsigned original_max = static_cast<unsigned>(elt_types.size()) - 1;
80 unsigned max_idx = FindMaxIndex(var, original_max);
81 if (max_idx != original_max) {
82 ChangeStructLength(var, max_idx + 1);
83 modified = true;
84 }
85 }
86
87 return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
88 }
89
FindMaxIndex(Instruction & var,unsigned original_max)90 unsigned EliminateDeadInputComponentsPass::FindMaxIndex(Instruction& var,
91 unsigned original_max) {
92 unsigned max = 0;
93 bool seen_non_const_ac = false;
94 assert(var.opcode() == SpvOpVariable && "must be variable");
95 context()->get_def_use_mgr()->WhileEachUser(
96 var.result_id(), [&max, &seen_non_const_ac, var, this](Instruction* use) {
97 auto use_opcode = use->opcode();
98 if (use_opcode == SpvOpLoad || use_opcode == SpvOpCopyMemory ||
99 use_opcode == SpvOpCopyMemorySized ||
100 use_opcode == SpvOpCopyObject) {
101 seen_non_const_ac = true;
102 return false;
103 }
104 if (use->opcode() != SpvOpAccessChain &&
105 use->opcode() != SpvOpInBoundsAccessChain) {
106 return true;
107 }
108 // OpAccessChain with no indices currently not optimized
109 if (use->NumInOperands() == 1) {
110 seen_non_const_ac = true;
111 return false;
112 }
113 unsigned base_id = use->GetSingleWordInOperand(kAccessChainBaseInIdx);
114 USE_ASSERT(base_id == var.result_id() && "unexpected base");
115 unsigned idx_id = use->GetSingleWordInOperand(kAccessChainIndex0InIdx);
116 Instruction* idx_inst = context()->get_def_use_mgr()->GetDef(idx_id);
117 if (idx_inst->opcode() != SpvOpConstant) {
118 seen_non_const_ac = true;
119 return false;
120 }
121 unsigned value = idx_inst->GetSingleWordInOperand(kConstantValueInIdx);
122 if (value > max) max = value;
123 return true;
124 });
125 return seen_non_const_ac ? original_max : max;
126 }
127
ChangeArrayLength(Instruction & arr_var,unsigned length)128 void EliminateDeadInputComponentsPass::ChangeArrayLength(Instruction& arr_var,
129 unsigned length) {
130 analysis::TypeManager* type_mgr = context()->get_type_mgr();
131 analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
132 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
133 analysis::Pointer* ptr_type =
134 type_mgr->GetType(arr_var.type_id())->AsPointer();
135 const analysis::Array* arr_ty = ptr_type->pointee_type()->AsArray();
136 assert(arr_ty && "expecting array type");
137 uint32_t length_id = const_mgr->GetUIntConst(length);
138 analysis::Array new_arr_ty(arr_ty->element_type(),
139 arr_ty->GetConstantLengthInfo(length_id, length));
140 analysis::Type* reg_new_arr_ty = type_mgr->GetRegisteredType(&new_arr_ty);
141 analysis::Pointer new_ptr_ty(reg_new_arr_ty, SpvStorageClassInput);
142 analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
143 uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
144 arr_var.SetResultType(new_ptr_ty_id);
145 def_use_mgr->AnalyzeInstUse(&arr_var);
146 // Move arr_var after its new type to preserve order
147 USE_ASSERT(arr_var.GetSingleWordInOperand(kVariableStorageClassInIdx) !=
148 SpvStorageClassFunction &&
149 "cannot move Function variable");
150 Instruction* new_ptr_ty_inst = def_use_mgr->GetDef(new_ptr_ty_id);
151 arr_var.RemoveFromList();
152 arr_var.InsertAfter(new_ptr_ty_inst);
153 }
154
ChangeStructLength(Instruction & struct_var,unsigned length)155 void EliminateDeadInputComponentsPass::ChangeStructLength(
156 Instruction& struct_var, unsigned length) {
157 analysis::TypeManager* type_mgr = context()->get_type_mgr();
158 analysis::Pointer* ptr_type =
159 type_mgr->GetType(struct_var.type_id())->AsPointer();
160 const analysis::Struct* struct_ty = ptr_type->pointee_type()->AsStruct();
161 assert(struct_ty && "expecting struct type");
162 const auto orig_elt_types = struct_ty->element_types();
163 std::vector<const analysis::Type*> new_elt_types;
164 for (unsigned u = 0; u < length; ++u)
165 new_elt_types.push_back(orig_elt_types[u]);
166 analysis::Struct new_struct_ty(new_elt_types);
167 analysis::Type* reg_new_struct_ty =
168 type_mgr->GetRegisteredType(&new_struct_ty);
169 uint32_t new_struct_ty_id = type_mgr->GetTypeInstruction(reg_new_struct_ty);
170 uint32_t old_struct_ty_id = type_mgr->GetTypeInstruction(struct_ty);
171 analysis::DecorationManager* deco_mgr = context()->get_decoration_mgr();
172 deco_mgr->CloneDecorations(old_struct_ty_id, new_struct_ty_id);
173 analysis::Pointer new_ptr_ty(reg_new_struct_ty, SpvStorageClassInput);
174 analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
175 uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
176 struct_var.SetResultType(new_ptr_ty_id);
177 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
178 def_use_mgr->AnalyzeInstUse(&struct_var);
179 // Move struct_var after its new type to preserve order
180 USE_ASSERT(struct_var.GetSingleWordInOperand(kVariableStorageClassInIdx) !=
181 SpvStorageClassFunction &&
182 "cannot move Function variable");
183 Instruction* new_ptr_ty_inst = def_use_mgr->GetDef(new_ptr_ty_id);
184 struct_var.RemoveFromList();
185 struct_var.InsertAfter(new_ptr_ty_inst);
186 }
187
188 } // namespace opt
189 } // namespace spvtools
190