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_io_components_pass.h"
17
18 #include <vector>
19
20 #include "source/opt/instruction.h"
21 #include "source/opt/ir_context.h"
22 #include "source/util/bit_vector.h"
23
24 namespace spvtools {
25 namespace opt {
26 namespace {
27 constexpr uint32_t kAccessChainBaseInIdx = 0;
28 constexpr uint32_t kAccessChainIndex0InIdx = 1;
29 constexpr uint32_t kAccessChainIndex1InIdx = 2;
30 constexpr uint32_t kConstantValueInIdx = 0;
31 } // namespace
32
Process()33 Pass::Status EliminateDeadIOComponentsPass::Process() {
34 // Only process input and output variables
35 if (elim_sclass_ != spv::StorageClass::Input &&
36 elim_sclass_ != spv::StorageClass::Output) {
37 if (consumer()) {
38 std::string message =
39 "EliminateDeadIOComponentsPass only valid for input and output "
40 "variables.";
41 consumer()(SPV_MSG_ERROR, 0, {0, 0, 0}, message.c_str());
42 }
43 return Status::Failure;
44 }
45 // If safe mode, only process Input variables in vertex shader
46 const auto stage = context()->GetStage();
47 if (safe_mode_ && !(stage == spv::ExecutionModel::Vertex &&
48 elim_sclass_ == spv::StorageClass::Input))
49 return Status::SuccessWithoutChange;
50 // Current functionality assumes shader capability.
51 if (!context()->get_feature_mgr()->HasCapability(spv::Capability::Shader))
52 return Status::SuccessWithoutChange;
53 // Current functionality assumes vert, frag, tesc, tese or geom shader.
54 // TODO(issue #4988): Add GLCompute.
55 if (stage != spv::ExecutionModel::Vertex &&
56 stage != spv::ExecutionModel::Fragment &&
57 stage != spv::ExecutionModel::TessellationControl &&
58 stage != spv::ExecutionModel::TessellationEvaluation &&
59 stage != spv::ExecutionModel::Geometry)
60 return Status::SuccessWithoutChange;
61 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
62 analysis::TypeManager* type_mgr = context()->get_type_mgr();
63 bool modified = false;
64 std::vector<Instruction*> vars_to_move;
65 for (auto& var : context()->types_values()) {
66 if (var.opcode() != spv::Op::OpVariable) {
67 continue;
68 }
69 analysis::Type* var_type = type_mgr->GetType(var.type_id());
70 analysis::Pointer* ptr_type = var_type->AsPointer();
71 if (ptr_type == nullptr) {
72 continue;
73 }
74 const auto sclass = ptr_type->storage_class();
75 if (sclass != elim_sclass_) {
76 continue;
77 }
78 // For tesc, or input variables in tese or geom shaders,
79 // there is a outer per-vertex-array that must be ignored
80 // for the purposes of this analysis/optimization. Do the
81 // analysis on the inner type in these cases.
82 bool skip_first_index = false;
83 auto core_type = ptr_type->pointee_type();
84 if (stage == spv::ExecutionModel::TessellationControl ||
85 (sclass == spv::StorageClass::Input &&
86 (stage == spv::ExecutionModel::TessellationEvaluation ||
87 stage == spv::ExecutionModel::Geometry))) {
88 auto arr_type = core_type->AsArray();
89 if (!arr_type) continue;
90 core_type = arr_type->element_type();
91 skip_first_index = true;
92 }
93 const analysis::Array* arr_type = core_type->AsArray();
94 if (arr_type != nullptr) {
95 // Only process array if input of vertex shader, or output of
96 // fragment shader. Otherwise, if one shader has a runtime index and the
97 // other does not, interface incompatibility can occur.
98 if (!((sclass == spv::StorageClass::Input &&
99 stage == spv::ExecutionModel::Vertex) ||
100 (sclass == spv::StorageClass::Output &&
101 stage == spv::ExecutionModel::Fragment)))
102 continue;
103 unsigned arr_len_id = arr_type->LengthId();
104 Instruction* arr_len_inst = def_use_mgr->GetDef(arr_len_id);
105 if (arr_len_inst->opcode() != spv::Op::OpConstant) {
106 continue;
107 }
108 // SPIR-V requires array size is >= 1, so this works for signed or
109 // unsigned size.
110 unsigned original_max =
111 arr_len_inst->GetSingleWordInOperand(kConstantValueInIdx) - 1;
112 unsigned max_idx = FindMaxIndex(var, original_max);
113 if (max_idx != original_max) {
114 ChangeArrayLength(var, max_idx + 1);
115 vars_to_move.push_back(&var);
116 modified = true;
117 }
118 continue;
119 }
120 const analysis::Struct* struct_type = core_type->AsStruct();
121 if (struct_type == nullptr) continue;
122 const auto elt_types = struct_type->element_types();
123 unsigned original_max = static_cast<unsigned>(elt_types.size()) - 1;
124 unsigned max_idx = FindMaxIndex(var, original_max, skip_first_index);
125 if (max_idx != original_max) {
126 ChangeIOVarStructLength(var, max_idx + 1);
127 vars_to_move.push_back(&var);
128 modified = true;
129 }
130 }
131
132 // Move changed vars after their new type instruction to preserve backward
133 // referencing.
134 for (auto var : vars_to_move) {
135 auto type_id = var->type_id();
136 auto type_inst = def_use_mgr->GetDef(type_id);
137 var->RemoveFromList();
138 var->InsertAfter(type_inst);
139 }
140
141 return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
142 }
143
FindMaxIndex(const Instruction & var,const unsigned original_max,const bool skip_first_index)144 unsigned EliminateDeadIOComponentsPass::FindMaxIndex(
145 const Instruction& var, const unsigned original_max,
146 const bool skip_first_index) {
147 unsigned max = 0;
148 bool seen_non_const_ac = false;
149 assert(var.opcode() == spv::Op::OpVariable && "must be variable");
150 context()->get_def_use_mgr()->WhileEachUser(
151 var.result_id(), [&max, &seen_non_const_ac, var, skip_first_index,
152 this](Instruction* use) {
153 auto use_opcode = use->opcode();
154 if (use_opcode == spv::Op::OpLoad || use_opcode == spv::Op::OpStore ||
155 use_opcode == spv::Op::OpCopyMemory ||
156 use_opcode == spv::Op::OpCopyMemorySized ||
157 use_opcode == spv::Op::OpCopyObject) {
158 seen_non_const_ac = true;
159 return false;
160 }
161 if (use->opcode() != spv::Op::OpAccessChain &&
162 use->opcode() != spv::Op::OpInBoundsAccessChain) {
163 return true;
164 }
165 // OpAccessChain with no indices currently not optimized
166 if (use->NumInOperands() == 1 ||
167 (skip_first_index && use->NumInOperands() == 2)) {
168 seen_non_const_ac = true;
169 return false;
170 }
171 const unsigned base_id =
172 use->GetSingleWordInOperand(kAccessChainBaseInIdx);
173 USE_ASSERT(base_id == var.result_id() && "unexpected base");
174 const unsigned in_idx = skip_first_index ? kAccessChainIndex1InIdx
175 : kAccessChainIndex0InIdx;
176 const unsigned idx_id = use->GetSingleWordInOperand(in_idx);
177 Instruction* idx_inst = context()->get_def_use_mgr()->GetDef(idx_id);
178 if (idx_inst->opcode() != spv::Op::OpConstant) {
179 seen_non_const_ac = true;
180 return false;
181 }
182 unsigned value = idx_inst->GetSingleWordInOperand(kConstantValueInIdx);
183 if (value > max) max = value;
184 return true;
185 });
186 return seen_non_const_ac ? original_max : max;
187 }
188
ChangeArrayLength(Instruction & arr_var,unsigned length)189 void EliminateDeadIOComponentsPass::ChangeArrayLength(Instruction& arr_var,
190 unsigned length) {
191 analysis::TypeManager* type_mgr = context()->get_type_mgr();
192 analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
193 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
194 analysis::Pointer* ptr_type =
195 type_mgr->GetType(arr_var.type_id())->AsPointer();
196 const analysis::Array* arr_ty = ptr_type->pointee_type()->AsArray();
197 assert(arr_ty && "expecting array type");
198 uint32_t length_id = const_mgr->GetUIntConstId(length);
199 analysis::Array new_arr_ty(arr_ty->element_type(),
200 arr_ty->GetConstantLengthInfo(length_id, length));
201 analysis::Type* reg_new_arr_ty = type_mgr->GetRegisteredType(&new_arr_ty);
202 analysis::Pointer new_ptr_ty(reg_new_arr_ty, ptr_type->storage_class());
203 analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
204 uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
205 arr_var.SetResultType(new_ptr_ty_id);
206 def_use_mgr->AnalyzeInstUse(&arr_var);
207 }
208
ChangeIOVarStructLength(Instruction & io_var,unsigned length)209 void EliminateDeadIOComponentsPass::ChangeIOVarStructLength(Instruction& io_var,
210 unsigned length) {
211 analysis::TypeManager* type_mgr = context()->get_type_mgr();
212 analysis::Pointer* ptr_type =
213 type_mgr->GetType(io_var.type_id())->AsPointer();
214 auto core_type = ptr_type->pointee_type();
215 // Check for per-vertex-array of struct from tesc, tese and geom and grab
216 // embedded struct type.
217 const auto arr_type = core_type->AsArray();
218 if (arr_type) core_type = arr_type->element_type();
219 const analysis::Struct* struct_ty = core_type->AsStruct();
220 assert(struct_ty && "expecting struct type");
221 const auto orig_elt_types = struct_ty->element_types();
222 std::vector<const analysis::Type*> new_elt_types;
223 for (unsigned u = 0; u < length; ++u)
224 new_elt_types.push_back(orig_elt_types[u]);
225 analysis::Struct new_struct_ty(new_elt_types);
226 uint32_t old_struct_ty_id = type_mgr->GetTypeInstruction(struct_ty);
227 std::vector<Instruction*> decorations =
228 context()->get_decoration_mgr()->GetDecorationsFor(old_struct_ty_id,
229 true);
230 for (auto dec : decorations) {
231 if (dec->opcode() == spv::Op::OpMemberDecorate) {
232 uint32_t midx = dec->GetSingleWordInOperand(1);
233 if (midx >= length) continue;
234 }
235 type_mgr->AttachDecoration(*dec, &new_struct_ty);
236 }
237 // Clone name instructions for new struct type
238 analysis::Type* reg_new_str_ty = type_mgr->GetRegisteredType(&new_struct_ty);
239 uint32_t new_struct_ty_id = type_mgr->GetTypeInstruction(reg_new_str_ty);
240 context()->CloneNames(old_struct_ty_id, new_struct_ty_id, length);
241 // Attach new type to var
242 analysis::Type* reg_new_var_ty = reg_new_str_ty;
243 if (arr_type) {
244 analysis::Array new_arr_ty(reg_new_var_ty, arr_type->length_info());
245 reg_new_var_ty = type_mgr->GetRegisteredType(&new_arr_ty);
246 }
247 analysis::Pointer new_ptr_ty(reg_new_var_ty, elim_sclass_);
248 analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
249 uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
250 io_var.SetResultType(new_ptr_ty_id);
251 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
252 def_use_mgr->AnalyzeInstUse(&io_var);
253 }
254
255 } // namespace opt
256 } // namespace spvtools
257