• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   auto ptr_type = type_mgr->GetType(var->type_id())->AsPointer();
96   assert(ptr_type && "unexpected var type");
97   auto var_type = ptr_type->pointee_type();
98   uint32_t ref_loc = start_loc;
99   auto curr_type = var_type;
100   if (ref->opcode() == spv::Op::OpAccessChain ||
101       ref->opcode() == spv::Op::OpInBoundsAccessChain) {
102     live_mgr->AnalyzeAccessChainLoc(ref, &curr_type, &ref_loc, &no_loc,
103                                     is_patch, /* input */ false);
104   }
105   if (no_loc || AnyLocsAreLive(ref_loc, live_mgr->GetLocSize(curr_type)))
106     return;
107   // Kill all stores based on this reference
108   KillAllStoresOfRef(ref);
109 }
110 
KillAllDeadStoresOfBuiltinRef(Instruction * ref,Instruction * var)111 void EliminateDeadOutputStoresPass::KillAllDeadStoresOfBuiltinRef(
112     Instruction* ref, Instruction* var) {
113   auto deco_mgr = context()->get_decoration_mgr();
114   auto def_use_mgr = context()->get_def_use_mgr();
115   auto type_mgr = context()->get_type_mgr();
116   auto live_mgr = context()->get_liveness_mgr();
117   // Search for builtin decoration of base variable
118   uint32_t builtin = uint32_t(spv::BuiltIn::Max);
119   auto var_id = var->result_id();
120   (void)deco_mgr->WhileEachDecoration(
121       var_id, uint32_t(spv::Decoration::BuiltIn),
122       [&builtin](const Instruction& deco) {
123         assert(deco.opcode() == spv::Op::OpDecorate && "unexpected decoration");
124         builtin = deco.GetSingleWordInOperand(kOpDecorateBuiltInLiteralInIdx);
125         return false;
126       });
127   // If analyzed builtin and not live, kill stores.
128   if (builtin != uint32_t(spv::BuiltIn::Max)) {
129     if (live_mgr->IsAnalyzedBuiltin(builtin) && !IsLiveBuiltin(builtin))
130       KillAllStoresOfRef(ref);
131     return;
132   }
133   // Search for builtin decoration on indexed member
134   auto ref_op = ref->opcode();
135   if (ref_op != spv::Op::OpAccessChain &&
136       ref_op != spv::Op::OpInBoundsAccessChain) {
137     return;
138   }
139   uint32_t in_idx = kOpAccessChainIdx0InIdx;
140   analysis::Type* var_type = type_mgr->GetType(var->type_id());
141   analysis::Pointer* ptr_type = var_type->AsPointer();
142   auto curr_type = ptr_type->pointee_type();
143   auto arr_type = curr_type->AsArray();
144   if (arr_type) {
145     curr_type = arr_type->element_type();
146     ++in_idx;
147   }
148   auto str_type = curr_type->AsStruct();
149   auto str_type_id = type_mgr->GetId(str_type);
150   auto member_idx_id = ref->GetSingleWordInOperand(in_idx);
151   auto member_idx_inst = def_use_mgr->GetDef(member_idx_id);
152   assert(member_idx_inst->opcode() == spv::Op::OpConstant &&
153          "unexpected non-constant index");
154   auto ac_idx = member_idx_inst->GetSingleWordInOperand(kOpConstantValueInIdx);
155   (void)deco_mgr->WhileEachDecoration(
156       str_type_id, uint32_t(spv::Decoration::BuiltIn),
157       [ac_idx, &builtin](const Instruction& deco) {
158         assert(deco.opcode() == spv::Op::OpMemberDecorate &&
159                "unexpected decoration");
160         auto deco_idx =
161             deco.GetSingleWordInOperand(kOpDecorateMemberMemberInIdx);
162         if (deco_idx == ac_idx) {
163           builtin =
164               deco.GetSingleWordInOperand(kOpDecorateMemberBuiltInLiteralInIdx);
165           return false;
166         }
167         return true;
168       });
169   assert(builtin != uint32_t(spv::BuiltIn::Max) && "builtin not found");
170   // If analyzed builtin and not live, kill stores.
171   if (live_mgr->IsAnalyzedBuiltin(builtin) && !IsLiveBuiltin(builtin))
172     KillAllStoresOfRef(ref);
173 }
174 
DoDeadOutputStoreElimination()175 Pass::Status EliminateDeadOutputStoresPass::DoDeadOutputStoreElimination() {
176   // Current implementation only supports vert, tesc, tese, geom shaders
177   auto stage = context()->GetStage();
178   if (stage != spv::ExecutionModel::Vertex &&
179       stage != spv::ExecutionModel::TessellationControl &&
180       stage != spv::ExecutionModel::TessellationEvaluation &&
181       stage != spv::ExecutionModel::Geometry)
182     return Status::Failure;
183   InitializeElimination();
184   analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
185   analysis::TypeManager* type_mgr = context()->get_type_mgr();
186   analysis::DecorationManager* deco_mgr = context()->get_decoration_mgr();
187   // Process all output variables
188   for (auto& var : context()->types_values()) {
189     if (var.opcode() != spv::Op::OpVariable) {
190       continue;
191     }
192     analysis::Type* var_type = type_mgr->GetType(var.type_id());
193     analysis::Pointer* ptr_type = var_type->AsPointer();
194     if (ptr_type->storage_class() != spv::StorageClass::Output) {
195       continue;
196     }
197     // If builtin decoration on variable, process as builtin.
198     auto var_id = var.result_id();
199     bool is_builtin = false;
200     if (deco_mgr->HasDecoration(var_id, uint32_t(spv::Decoration::BuiltIn))) {
201       is_builtin = true;
202     } else {
203       // If interface block with builtin members, process as builtin.
204       // Strip off outer array type if present.
205       auto curr_type = ptr_type->pointee_type();
206       auto arr_type = curr_type->AsArray();
207       if (arr_type) curr_type = arr_type->element_type();
208       auto str_type = curr_type->AsStruct();
209       if (str_type) {
210         auto str_type_id = type_mgr->GetId(str_type);
211         if (deco_mgr->HasDecoration(str_type_id,
212                                     uint32_t(spv::Decoration::BuiltIn)))
213           is_builtin = true;
214       }
215     }
216     // For each store or access chain using var, if dead builtin or all its
217     // locations are dead, kill store or all access chain's stores
218     def_use_mgr->ForEachUser(
219         var_id, [this, &var, is_builtin](Instruction* user) {
220           auto op = user->opcode();
221           if (op == spv::Op::OpEntryPoint || op == spv::Op::OpName ||
222               op == spv::Op::OpDecorate || user->IsNonSemanticInstruction())
223             return;
224           if (is_builtin)
225             KillAllDeadStoresOfBuiltinRef(user, &var);
226           else
227             KillAllDeadStoresOfLocRef(user, &var);
228         });
229   }
230   for (auto& kinst : kill_list_) context()->KillInst(kinst);
231 
232   return kill_list_.empty() ? Status::SuccessWithoutChange
233                             : Status::SuccessWithChange;
234 }
235 
236 }  // namespace opt
237 }  // namespace spvtools
238