• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 The Khronos Group Inc.
2 // Copyright (c) 2017 Valve Corporation
3 // Copyright (c) 2017 LunarG Inc.
4 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #include "source/opt/local_single_store_elim_pass.h"
18 
19 #include "source/cfa.h"
20 #include "source/util/string_utils.h"
21 
22 namespace spvtools {
23 namespace opt {
24 namespace {
25 constexpr uint32_t kStoreValIdInIdx = 1;
26 constexpr uint32_t kVariableInitIdInIdx = 1;
27 }  // namespace
28 
LocalSingleStoreElim(Function * func)29 bool LocalSingleStoreElimPass::LocalSingleStoreElim(Function* func) {
30   bool modified = false;
31 
32   // Check all function scope variables in |func|.
33   BasicBlock* entry_block = &*func->begin();
34   for (Instruction& inst : *entry_block) {
35     if (inst.opcode() != spv::Op::OpVariable) {
36       break;
37     }
38 
39     modified |= ProcessVariable(&inst);
40   }
41   return modified;
42 }
43 
AllExtensionsSupported() const44 bool LocalSingleStoreElimPass::AllExtensionsSupported() const {
45   // If any extension not in allowlist, return false
46   for (auto& ei : get_module()->extensions()) {
47     const std::string extName = ei.GetInOperand(0).AsString();
48     if (extensions_allowlist_.find(extName) == extensions_allowlist_.end())
49       return false;
50   }
51   // only allow NonSemantic.Shader.DebugInfo.100, we cannot safely optimise
52   // around unknown extended
53   // instruction sets even if they are non-semantic
54   for (auto& inst : context()->module()->ext_inst_imports()) {
55     assert(inst.opcode() == spv::Op::OpExtInstImport &&
56            "Expecting an import of an extension's instruction set.");
57     const std::string extension_name = inst.GetInOperand(0).AsString();
58     if (spvtools::utils::starts_with(extension_name, "NonSemantic.") &&
59         extension_name != "NonSemantic.Shader.DebugInfo.100") {
60       return false;
61     }
62   }
63   return true;
64 }
65 
ProcessImpl()66 Pass::Status LocalSingleStoreElimPass::ProcessImpl() {
67   // Assumes relaxed logical addressing only (see instruction.h)
68   if (context()->get_feature_mgr()->HasCapability(spv::Capability::Addresses))
69     return Status::SuccessWithoutChange;
70 
71   // Do not process if any disallowed extensions are enabled
72   if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
73   // Process all entry point functions
74   ProcessFunction pfn = [this](Function* fp) {
75     return LocalSingleStoreElim(fp);
76   };
77   bool modified = context()->ProcessReachableCallTree(pfn);
78   return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
79 }
80 
81 LocalSingleStoreElimPass::LocalSingleStoreElimPass() = default;
82 
Process()83 Pass::Status LocalSingleStoreElimPass::Process() {
84   InitExtensionAllowList();
85   return ProcessImpl();
86 }
87 
InitExtensionAllowList()88 void LocalSingleStoreElimPass::InitExtensionAllowList() {
89   extensions_allowlist_.insert({"SPV_AMD_shader_explicit_vertex_parameter",
90                                 "SPV_AMD_shader_trinary_minmax",
91                                 "SPV_AMD_gcn_shader",
92                                 "SPV_KHR_shader_ballot",
93                                 "SPV_AMD_shader_ballot",
94                                 "SPV_AMD_gpu_shader_half_float",
95                                 "SPV_KHR_shader_draw_parameters",
96                                 "SPV_KHR_subgroup_vote",
97                                 "SPV_KHR_8bit_storage",
98                                 "SPV_KHR_16bit_storage",
99                                 "SPV_KHR_device_group",
100                                 "SPV_KHR_multiview",
101                                 "SPV_NVX_multiview_per_view_attributes",
102                                 "SPV_NV_viewport_array2",
103                                 "SPV_NV_stereo_view_rendering",
104                                 "SPV_NV_sample_mask_override_coverage",
105                                 "SPV_NV_geometry_shader_passthrough",
106                                 "SPV_AMD_texture_gather_bias_lod",
107                                 "SPV_KHR_storage_buffer_storage_class",
108                                 "SPV_KHR_variable_pointers",
109                                 "SPV_AMD_gpu_shader_int16",
110                                 "SPV_KHR_post_depth_coverage",
111                                 "SPV_KHR_shader_atomic_counter_ops",
112                                 "SPV_EXT_shader_stencil_export",
113                                 "SPV_EXT_shader_viewport_index_layer",
114                                 "SPV_AMD_shader_image_load_store_lod",
115                                 "SPV_AMD_shader_fragment_mask",
116                                 "SPV_EXT_fragment_fully_covered",
117                                 "SPV_AMD_gpu_shader_half_float_fetch",
118                                 "SPV_GOOGLE_decorate_string",
119                                 "SPV_GOOGLE_hlsl_functionality1",
120                                 "SPV_NV_shader_subgroup_partitioned",
121                                 "SPV_EXT_descriptor_indexing",
122                                 "SPV_NV_fragment_shader_barycentric",
123                                 "SPV_NV_compute_shader_derivatives",
124                                 "SPV_NV_shader_image_footprint",
125                                 "SPV_NV_shading_rate",
126                                 "SPV_NV_mesh_shader",
127                                 "SPV_EXT_mesh_shader",
128                                 "SPV_NV_ray_tracing",
129                                 "SPV_KHR_ray_query",
130                                 "SPV_EXT_fragment_invocation_density",
131                                 "SPV_EXT_physical_storage_buffer",
132                                 "SPV_KHR_physical_storage_buffer",
133                                 "SPV_KHR_terminate_invocation",
134                                 "SPV_KHR_subgroup_uniform_control_flow",
135                                 "SPV_KHR_integer_dot_product",
136                                 "SPV_EXT_shader_image_int64",
137                                 "SPV_KHR_non_semantic_info",
138                                 "SPV_KHR_uniform_group_instructions",
139                                 "SPV_KHR_fragment_shader_barycentric",
140                                 "SPV_KHR_vulkan_memory_model",
141                                 "SPV_NV_bindless_texture",
142                                 "SPV_EXT_shader_atomic_float_add",
143                                 "SPV_EXT_fragment_shader_interlock",
144                                 "SPV_NV_compute_shader_derivatives"});
145 }
ProcessVariable(Instruction * var_inst)146 bool LocalSingleStoreElimPass::ProcessVariable(Instruction* var_inst) {
147   std::vector<Instruction*> users;
148   FindUses(var_inst, &users);
149 
150   Instruction* store_inst = FindSingleStoreAndCheckUses(var_inst, users);
151 
152   if (store_inst == nullptr) {
153     return false;
154   }
155 
156   bool all_rewritten;
157   bool modified = RewriteLoads(store_inst, users, &all_rewritten);
158 
159   // If all uses are rewritten and the variable has a DebugDeclare and the
160   // variable is not an aggregate, add a DebugValue after the store and remove
161   // the DebugDeclare.
162   uint32_t var_id = var_inst->result_id();
163   if (all_rewritten &&
164       context()->get_debug_info_mgr()->IsVariableDebugDeclared(var_id)) {
165     const analysis::Type* var_type =
166         context()->get_type_mgr()->GetType(var_inst->type_id());
167     const analysis::Type* store_type = var_type->AsPointer()->pointee_type();
168     if (!(store_type->AsStruct() || store_type->AsArray())) {
169       modified |= RewriteDebugDeclares(store_inst, var_id);
170     }
171   }
172 
173   return modified;
174 }
175 
RewriteDebugDeclares(Instruction * store_inst,uint32_t var_id)176 bool LocalSingleStoreElimPass::RewriteDebugDeclares(Instruction* store_inst,
177                                                     uint32_t var_id) {
178   uint32_t value_id = store_inst->GetSingleWordInOperand(1);
179   bool modified = context()->get_debug_info_mgr()->AddDebugValueForVariable(
180       store_inst, var_id, value_id, store_inst);
181   modified |= context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
182   return modified;
183 }
184 
FindSingleStoreAndCheckUses(Instruction * var_inst,const std::vector<Instruction * > & users) const185 Instruction* LocalSingleStoreElimPass::FindSingleStoreAndCheckUses(
186     Instruction* var_inst, const std::vector<Instruction*>& users) const {
187   // Make sure there is exactly 1 store.
188   Instruction* store_inst = nullptr;
189 
190   // If |var_inst| has an initializer, then that will count as a store.
191   if (var_inst->NumInOperands() > 1) {
192     store_inst = var_inst;
193   }
194 
195   for (Instruction* user : users) {
196     switch (user->opcode()) {
197       case spv::Op::OpStore:
198         // Since we are in the relaxed addressing mode, the use has to be the
199         // base address of the store, and not the value being store.  Otherwise,
200         // we would have a pointer to a pointer to function scope memory, which
201         // is not allowed.
202         if (store_inst == nullptr) {
203           store_inst = user;
204         } else {
205           // More than 1 store.
206           return nullptr;
207         }
208         break;
209       case spv::Op::OpAccessChain:
210       case spv::Op::OpInBoundsAccessChain:
211         if (FeedsAStore(user)) {
212           // Has a partial store.  Cannot propagate that.
213           return nullptr;
214         }
215         break;
216       case spv::Op::OpLoad:
217       case spv::Op::OpImageTexelPointer:
218       case spv::Op::OpName:
219       case spv::Op::OpCopyObject:
220         break;
221       case spv::Op::OpExtInst: {
222         auto dbg_op = user->GetCommonDebugOpcode();
223         if (dbg_op == CommonDebugInfoDebugDeclare ||
224             dbg_op == CommonDebugInfoDebugValue) {
225           break;
226         }
227         return nullptr;
228       }
229       default:
230         if (!user->IsDecoration()) {
231           // Don't know if this instruction modifies the variable.
232           // Conservatively assume it is a store.
233           return nullptr;
234         }
235         break;
236     }
237   }
238   return store_inst;
239 }
240 
FindUses(const Instruction * var_inst,std::vector<Instruction * > * users) const241 void LocalSingleStoreElimPass::FindUses(
242     const Instruction* var_inst, std::vector<Instruction*>* users) const {
243   analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
244   def_use_mgr->ForEachUser(var_inst, [users, this](Instruction* user) {
245     users->push_back(user);
246     if (user->opcode() == spv::Op::OpCopyObject) {
247       FindUses(user, users);
248     }
249   });
250 }
251 
FeedsAStore(Instruction * inst) const252 bool LocalSingleStoreElimPass::FeedsAStore(Instruction* inst) const {
253   analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
254   return !def_use_mgr->WhileEachUser(inst, [this](Instruction* user) {
255     switch (user->opcode()) {
256       case spv::Op::OpStore:
257         return false;
258       case spv::Op::OpAccessChain:
259       case spv::Op::OpInBoundsAccessChain:
260       case spv::Op::OpCopyObject:
261         return !FeedsAStore(user);
262       case spv::Op::OpLoad:
263       case spv::Op::OpImageTexelPointer:
264       case spv::Op::OpName:
265         return true;
266       default:
267         // Don't know if this instruction modifies the variable.
268         // Conservatively assume it is a store.
269         return user->IsDecoration();
270     }
271   });
272 }
273 
RewriteLoads(Instruction * store_inst,const std::vector<Instruction * > & uses,bool * all_rewritten)274 bool LocalSingleStoreElimPass::RewriteLoads(
275     Instruction* store_inst, const std::vector<Instruction*>& uses,
276     bool* all_rewritten) {
277   BasicBlock* store_block = context()->get_instr_block(store_inst);
278   DominatorAnalysis* dominator_analysis =
279       context()->GetDominatorAnalysis(store_block->GetParent());
280 
281   uint32_t stored_id;
282   if (store_inst->opcode() == spv::Op::OpStore)
283     stored_id = store_inst->GetSingleWordInOperand(kStoreValIdInIdx);
284   else
285     stored_id = store_inst->GetSingleWordInOperand(kVariableInitIdInIdx);
286 
287   *all_rewritten = true;
288   bool modified = false;
289   for (Instruction* use : uses) {
290     if (use->opcode() == spv::Op::OpStore) continue;
291     auto dbg_op = use->GetCommonDebugOpcode();
292     if (dbg_op == CommonDebugInfoDebugDeclare ||
293         dbg_op == CommonDebugInfoDebugValue)
294       continue;
295     if (use->opcode() == spv::Op::OpLoad &&
296         dominator_analysis->Dominates(store_inst, use)) {
297       modified = true;
298       context()->KillNamesAndDecorates(use->result_id());
299       context()->ReplaceAllUsesWith(use->result_id(), stored_id);
300       context()->KillInst(use);
301     } else {
302       *all_rewritten = false;
303     }
304   }
305 
306   return modified;
307 }
308 
309 }  // namespace opt
310 }  // namespace spvtools
311