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_output_stores_pass.h"
17
18 #include "source/opt/instruction.h"
19 #include "source/opt/ir_context.h"
20
21 namespace spvtools {
22 namespace opt {
23 namespace {
24 constexpr uint32_t kDecorationLocationInIdx = 2;
25 constexpr uint32_t kOpDecorateMemberMemberInIdx = 1;
26 constexpr uint32_t kOpDecorateBuiltInLiteralInIdx = 2;
27 constexpr uint32_t kOpDecorateMemberBuiltInLiteralInIdx = 3;
28 constexpr uint32_t kOpAccessChainIdx0InIdx = 1;
29 constexpr uint32_t kOpConstantValueInIdx = 0;
30 } // namespace
31
Process()32 Pass::Status EliminateDeadOutputStoresPass::Process() {
33 // Current functionality assumes shader capability
34 if (!context()->get_feature_mgr()->HasCapability(spv::Capability::Shader))
35 return Status::SuccessWithoutChange;
36 Pass::Status status = DoDeadOutputStoreElimination();
37 return status;
38 }
39
InitializeElimination()40 void EliminateDeadOutputStoresPass::InitializeElimination() {
41 kill_list_.clear();
42 }
43
IsLiveBuiltin(uint32_t bi)44 bool EliminateDeadOutputStoresPass::IsLiveBuiltin(uint32_t bi) {
45 return live_builtins_->find(bi) != live_builtins_->end();
46 }
47
AnyLocsAreLive(uint32_t start,uint32_t count)48 bool EliminateDeadOutputStoresPass::AnyLocsAreLive(uint32_t start,
49 uint32_t count) {
50 auto finish = start + count;
51 for (uint32_t u = start; u < finish; ++u) {
52 if (live_locs_->find(u) != live_locs_->end()) return true;
53 }
54 return false;
55 }
56
KillAllStoresOfRef(Instruction * ref)57 void EliminateDeadOutputStoresPass::KillAllStoresOfRef(Instruction* ref) {
58 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
59 if (ref->opcode() == spv::Op::OpStore) {
60 kill_list_.push_back(ref);
61 return;
62 }
63 assert((ref->opcode() == spv::Op::OpAccessChain ||
64 ref->opcode() == spv::Op::OpInBoundsAccessChain) &&
65 "unexpected use of output variable");
66 def_use_mgr->ForEachUser(ref, [this](Instruction* user) {
67 if (user->opcode() == spv::Op::OpStore) kill_list_.push_back(user);
68 });
69 }
70
KillAllDeadStoresOfLocRef(Instruction * ref,Instruction * var)71 void EliminateDeadOutputStoresPass::KillAllDeadStoresOfLocRef(
72 Instruction* ref, Instruction* var) {
73 analysis::TypeManager* type_mgr = context()->get_type_mgr();
74 analysis::DecorationManager* deco_mgr = context()->get_decoration_mgr();
75 analysis::LivenessManager* live_mgr = context()->get_liveness_mgr();
76 // Find variable location if present.
77 uint32_t start_loc = 0;
78 auto var_id = var->result_id();
79 bool no_loc = deco_mgr->WhileEachDecoration(
80 var_id, uint32_t(spv::Decoration::Location),
81 [&start_loc](const Instruction& deco) {
82 assert(deco.opcode() == spv::Op::OpDecorate && "unexpected decoration");
83 start_loc = deco.GetSingleWordInOperand(kDecorationLocationInIdx);
84 return false;
85 });
86 // Find patch decoration if present
87 bool is_patch = !deco_mgr->WhileEachDecoration(
88 var_id, uint32_t(spv::Decoration::Patch), [](const Instruction& deco) {
89 if (deco.opcode() != spv::Op::OpDecorate)
90 assert(false && "unexpected decoration");
91 return false;
92 });
93 // Compute offset and final type of reference. If no location found
94 // or any stored locations are live, return without removing stores.
95
96 Instruction* ptr_type = get_def_use_mgr()->GetDef(var->type_id());
97 assert(ptr_type && "unexpected var type");
98 const uint32_t kPointerTypePointeeIdx = 1;
99 uint32_t var_type_id =
100 ptr_type->GetSingleWordInOperand(kPointerTypePointeeIdx);
101 uint32_t ref_loc = start_loc;
102 if (ref->opcode() == spv::Op::OpAccessChain ||
103 ref->opcode() == spv::Op::OpInBoundsAccessChain) {
104 var_type_id = live_mgr->AnalyzeAccessChainLoc(
105 ref, var_type_id, &ref_loc, &no_loc, is_patch, /* input */ false);
106 }
107 const analysis::Type* curr_type = type_mgr->GetType(var_type_id);
108 if (no_loc || AnyLocsAreLive(ref_loc, live_mgr->GetLocSize(curr_type)))
109 return;
110 // Kill all stores based on this reference
111 KillAllStoresOfRef(ref);
112 }
113
KillAllDeadStoresOfBuiltinRef(Instruction * ref,Instruction * var)114 void EliminateDeadOutputStoresPass::KillAllDeadStoresOfBuiltinRef(
115 Instruction* ref, Instruction* var) {
116 auto deco_mgr = context()->get_decoration_mgr();
117 auto def_use_mgr = context()->get_def_use_mgr();
118 auto type_mgr = context()->get_type_mgr();
119 auto live_mgr = context()->get_liveness_mgr();
120 // Search for builtin decoration of base variable
121 uint32_t builtin = uint32_t(spv::BuiltIn::Max);
122 auto var_id = var->result_id();
123 (void)deco_mgr->WhileEachDecoration(
124 var_id, uint32_t(spv::Decoration::BuiltIn),
125 [&builtin](const Instruction& deco) {
126 assert(deco.opcode() == spv::Op::OpDecorate && "unexpected decoration");
127 builtin = deco.GetSingleWordInOperand(kOpDecorateBuiltInLiteralInIdx);
128 return false;
129 });
130 // If analyzed builtin and not live, kill stores.
131 if (builtin != uint32_t(spv::BuiltIn::Max)) {
132 if (live_mgr->IsAnalyzedBuiltin(builtin) && !IsLiveBuiltin(builtin))
133 KillAllStoresOfRef(ref);
134 return;
135 }
136 // Search for builtin decoration on indexed member
137 auto ref_op = ref->opcode();
138 if (ref_op != spv::Op::OpAccessChain &&
139 ref_op != spv::Op::OpInBoundsAccessChain) {
140 return;
141 }
142 uint32_t in_idx = kOpAccessChainIdx0InIdx;
143 analysis::Type* var_type = type_mgr->GetType(var->type_id());
144 analysis::Pointer* ptr_type = var_type->AsPointer();
145 auto curr_type = ptr_type->pointee_type();
146 auto arr_type = curr_type->AsArray();
147 if (arr_type) {
148 curr_type = arr_type->element_type();
149 ++in_idx;
150 }
151 auto str_type = curr_type->AsStruct();
152 auto str_type_id = type_mgr->GetId(str_type);
153 auto member_idx_id = ref->GetSingleWordInOperand(in_idx);
154 auto member_idx_inst = def_use_mgr->GetDef(member_idx_id);
155 assert(member_idx_inst->opcode() == spv::Op::OpConstant &&
156 "unexpected non-constant index");
157 auto ac_idx = member_idx_inst->GetSingleWordInOperand(kOpConstantValueInIdx);
158 (void)deco_mgr->WhileEachDecoration(
159 str_type_id, uint32_t(spv::Decoration::BuiltIn),
160 [ac_idx, &builtin](const Instruction& deco) {
161 assert(deco.opcode() == spv::Op::OpMemberDecorate &&
162 "unexpected decoration");
163 auto deco_idx =
164 deco.GetSingleWordInOperand(kOpDecorateMemberMemberInIdx);
165 if (deco_idx == ac_idx) {
166 builtin =
167 deco.GetSingleWordInOperand(kOpDecorateMemberBuiltInLiteralInIdx);
168 return false;
169 }
170 return true;
171 });
172 assert(builtin != uint32_t(spv::BuiltIn::Max) && "builtin not found");
173 // If analyzed builtin and not live, kill stores.
174 if (live_mgr->IsAnalyzedBuiltin(builtin) && !IsLiveBuiltin(builtin))
175 KillAllStoresOfRef(ref);
176 }
177
DoDeadOutputStoreElimination()178 Pass::Status EliminateDeadOutputStoresPass::DoDeadOutputStoreElimination() {
179 // Current implementation only supports vert, tesc, tese, geom shaders
180 auto stage = context()->GetStage();
181 if (stage != spv::ExecutionModel::Vertex &&
182 stage != spv::ExecutionModel::TessellationControl &&
183 stage != spv::ExecutionModel::TessellationEvaluation &&
184 stage != spv::ExecutionModel::Geometry)
185 return Status::Failure;
186 InitializeElimination();
187 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
188 analysis::TypeManager* type_mgr = context()->get_type_mgr();
189 analysis::DecorationManager* deco_mgr = context()->get_decoration_mgr();
190 // Process all output variables
191 for (auto& var : context()->types_values()) {
192 if (var.opcode() != spv::Op::OpVariable) {
193 continue;
194 }
195 analysis::Type* var_type = type_mgr->GetType(var.type_id());
196 analysis::Pointer* ptr_type = var_type->AsPointer();
197 if (ptr_type->storage_class() != spv::StorageClass::Output) {
198 continue;
199 }
200 // If builtin decoration on variable, process as builtin.
201 auto var_id = var.result_id();
202 bool is_builtin = false;
203 if (deco_mgr->HasDecoration(var_id, uint32_t(spv::Decoration::BuiltIn))) {
204 is_builtin = true;
205 } else {
206 // If interface block with builtin members, process as builtin.
207 // Strip off outer array type if present.
208 auto curr_type = ptr_type->pointee_type();
209 auto arr_type = curr_type->AsArray();
210 if (arr_type) curr_type = arr_type->element_type();
211 auto str_type = curr_type->AsStruct();
212 if (str_type) {
213 auto str_type_id = type_mgr->GetId(str_type);
214 if (deco_mgr->HasDecoration(str_type_id,
215 uint32_t(spv::Decoration::BuiltIn)))
216 is_builtin = true;
217 }
218 }
219 // For each store or access chain using var, if dead builtin or all its
220 // locations are dead, kill store or all access chain's stores
221 def_use_mgr->ForEachUser(
222 var_id, [this, &var, is_builtin](Instruction* user) {
223 auto op = user->opcode();
224 if (op == spv::Op::OpEntryPoint || op == spv::Op::OpName ||
225 op == spv::Op::OpDecorate || user->IsNonSemanticInstruction())
226 return;
227 if (is_builtin)
228 KillAllDeadStoresOfBuiltinRef(user, &var);
229 else
230 KillAllDeadStoresOfLocRef(user, &var);
231 });
232 }
233 for (auto& kinst : kill_list_) context()->KillInst(kinst);
234
235 return kill_list_.empty() ? Status::SuccessWithoutChange
236 : Status::SuccessWithChange;
237 }
238
239 } // namespace opt
240 } // namespace spvtools
241