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/latest_version_glsl_std_450_header.h"
21 #include "source/opt/iterator.h"
22 
23 namespace spvtools {
24 namespace opt {
25 
26 namespace {
27 
28 const uint32_t kStoreValIdInIdx = 1;
29 const uint32_t kVariableInitIdInIdx = 1;
30 
31 }  // anonymous namespace
32 
LocalSingleStoreElim(Function * func)33 bool LocalSingleStoreElimPass::LocalSingleStoreElim(Function* func) {
34   bool modified = false;
35 
36   // Check all function scope variables in |func|.
37   BasicBlock* entry_block = &*func->begin();
38   for (Instruction& inst : *entry_block) {
39     if (inst.opcode() != SpvOpVariable) {
40       break;
41     }
42 
43     modified |= ProcessVariable(&inst);
44   }
45   return modified;
46 }
47 
AllExtensionsSupported() const48 bool LocalSingleStoreElimPass::AllExtensionsSupported() const {
49   // If any extension not in allowlist, return false
50   for (auto& ei : get_module()->extensions()) {
51     const char* extName =
52         reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
53     if (extensions_allowlist_.find(extName) == extensions_allowlist_.end())
54       return false;
55   }
56   return true;
57 }
58 
ProcessImpl()59 Pass::Status LocalSingleStoreElimPass::ProcessImpl() {
60   // Assumes relaxed logical addressing only (see instruction.h)
61   if (context()->get_feature_mgr()->HasCapability(SpvCapabilityAddresses))
62     return Status::SuccessWithoutChange;
63 
64   // Do not process if any disallowed extensions are enabled
65   if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
66   // Process all entry point functions
67   ProcessFunction pfn = [this](Function* fp) {
68     return LocalSingleStoreElim(fp);
69   };
70   bool modified = context()->ProcessEntryPointCallTree(pfn);
71   return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
72 }
73 
74 LocalSingleStoreElimPass::LocalSingleStoreElimPass() = default;
75 
Process()76 Pass::Status LocalSingleStoreElimPass::Process() {
77   InitExtensionAllowList();
78   return ProcessImpl();
79 }
80 
InitExtensionAllowList()81 void LocalSingleStoreElimPass::InitExtensionAllowList() {
82   extensions_allowlist_.insert({
83       "SPV_AMD_shader_explicit_vertex_parameter",
84       "SPV_AMD_shader_trinary_minmax",
85       "SPV_AMD_gcn_shader",
86       "SPV_KHR_shader_ballot",
87       "SPV_AMD_shader_ballot",
88       "SPV_AMD_gpu_shader_half_float",
89       "SPV_KHR_shader_draw_parameters",
90       "SPV_KHR_subgroup_vote",
91       "SPV_KHR_8bit_storage",
92       "SPV_KHR_16bit_storage",
93       "SPV_KHR_device_group",
94       "SPV_KHR_multiview",
95       "SPV_NVX_multiview_per_view_attributes",
96       "SPV_NV_viewport_array2",
97       "SPV_NV_stereo_view_rendering",
98       "SPV_NV_sample_mask_override_coverage",
99       "SPV_NV_geometry_shader_passthrough",
100       "SPV_AMD_texture_gather_bias_lod",
101       "SPV_KHR_storage_buffer_storage_class",
102       "SPV_KHR_variable_pointers",
103       "SPV_AMD_gpu_shader_int16",
104       "SPV_KHR_post_depth_coverage",
105       "SPV_KHR_shader_atomic_counter_ops",
106       "SPV_EXT_shader_stencil_export",
107       "SPV_EXT_shader_viewport_index_layer",
108       "SPV_AMD_shader_image_load_store_lod",
109       "SPV_AMD_shader_fragment_mask",
110       "SPV_EXT_fragment_fully_covered",
111       "SPV_AMD_gpu_shader_half_float_fetch",
112       "SPV_GOOGLE_decorate_string",
113       "SPV_GOOGLE_hlsl_functionality1",
114       "SPV_NV_shader_subgroup_partitioned",
115       "SPV_EXT_descriptor_indexing",
116       "SPV_NV_fragment_shader_barycentric",
117       "SPV_NV_compute_shader_derivatives",
118       "SPV_NV_shader_image_footprint",
119       "SPV_NV_shading_rate",
120       "SPV_NV_mesh_shader",
121       "SPV_NV_ray_tracing",
122       "SPV_KHR_ray_query",
123       "SPV_EXT_fragment_invocation_density",
124       "SPV_EXT_physical_storage_buffer",
125       "SPV_KHR_terminate_invocation",
126   });
127 }
ProcessVariable(Instruction * var_inst)128 bool LocalSingleStoreElimPass::ProcessVariable(Instruction* var_inst) {
129   std::vector<Instruction*> users;
130   FindUses(var_inst, &users);
131 
132   Instruction* store_inst = FindSingleStoreAndCheckUses(var_inst, users);
133 
134   if (store_inst == nullptr) {
135     return false;
136   }
137 
138   bool all_rewritten;
139   bool modified = RewriteLoads(store_inst, users, &all_rewritten);
140 
141   // If all uses are rewritten and the variable has a DebugDeclare and the
142   // variable is not an aggregate, add a DebugValue after the store and remove
143   // the DebugDeclare.
144   uint32_t var_id = var_inst->result_id();
145   if (all_rewritten &&
146       context()->get_debug_info_mgr()->IsVariableDebugDeclared(var_id)) {
147     const analysis::Type* var_type =
148         context()->get_type_mgr()->GetType(var_inst->type_id());
149     const analysis::Type* store_type = var_type->AsPointer()->pointee_type();
150     if (!(store_type->AsStruct() || store_type->AsArray())) {
151       modified |= RewriteDebugDeclares(store_inst, var_id);
152     }
153   }
154 
155   return modified;
156 }
157 
RewriteDebugDeclares(Instruction * store_inst,uint32_t var_id)158 bool LocalSingleStoreElimPass::RewriteDebugDeclares(Instruction* store_inst,
159                                                     uint32_t var_id) {
160   std::unordered_set<Instruction*> invisible_decls;
161   uint32_t value_id = store_inst->GetSingleWordInOperand(1);
162   bool modified =
163       context()->get_debug_info_mgr()->AddDebugValueIfVarDeclIsVisible(
164           store_inst, var_id, value_id, store_inst, &invisible_decls);
165 
166   // For cases like the argument passing for an inlined function, the value
167   // assignment is out of DebugDeclare's scope, but we have to preserve the
168   // value assignment information using DebugValue. Generally, we need
169   // ssa-rewrite analysis to decide a proper value assignment but at this point
170   // we confirm that |var_id| has a single store. We can safely add DebugValue.
171   if (!invisible_decls.empty()) {
172     BasicBlock* store_block = context()->get_instr_block(store_inst);
173     DominatorAnalysis* dominator_analysis =
174         context()->GetDominatorAnalysis(store_block->GetParent());
175     for (auto* decl : invisible_decls) {
176       if (dominator_analysis->Dominates(store_inst, decl)) {
177         context()->get_debug_info_mgr()->AddDebugValueForDecl(decl, value_id);
178         modified = true;
179       }
180     }
181   }
182   modified |= context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
183   return modified;
184 }
185 
FindSingleStoreAndCheckUses(Instruction * var_inst,const std::vector<Instruction * > & users) const186 Instruction* LocalSingleStoreElimPass::FindSingleStoreAndCheckUses(
187     Instruction* var_inst, const std::vector<Instruction*>& users) const {
188   // Make sure there is exactly 1 store.
189   Instruction* store_inst = nullptr;
190 
191   // If |var_inst| has an initializer, then that will count as a store.
192   if (var_inst->NumInOperands() > 1) {
193     store_inst = var_inst;
194   }
195 
196   for (Instruction* user : users) {
197     switch (user->opcode()) {
198       case SpvOpStore:
199         // Since we are in the relaxed addressing mode, the use has to be the
200         // base address of the store, and not the value being store.  Otherwise,
201         // we would have a pointer to a pointer to function scope memory, which
202         // is not allowed.
203         if (store_inst == nullptr) {
204           store_inst = user;
205         } else {
206           // More than 1 store.
207           return nullptr;
208         }
209         break;
210       case SpvOpAccessChain:
211       case SpvOpInBoundsAccessChain:
212         if (FeedsAStore(user)) {
213           // Has a partial store.  Cannot propagate that.
214           return nullptr;
215         }
216         break;
217       case SpvOpLoad:
218       case SpvOpImageTexelPointer:
219       case SpvOpName:
220       case SpvOpCopyObject:
221         break;
222       case SpvOpExtInst: {
223         auto dbg_op = user->GetOpenCL100DebugOpcode();
224         if (dbg_op == OpenCLDebugInfo100DebugDeclare ||
225             dbg_op == OpenCLDebugInfo100DebugValue) {
226           break;
227         }
228         return nullptr;
229       }
230       default:
231         if (!user->IsDecoration()) {
232           // Don't know if this instruction modifies the variable.
233           // Conservatively assume it is a store.
234           return nullptr;
235         }
236         break;
237     }
238   }
239   return store_inst;
240 }
241 
FindUses(const Instruction * var_inst,std::vector<Instruction * > * users) const242 void LocalSingleStoreElimPass::FindUses(
243     const Instruction* var_inst, std::vector<Instruction*>* users) const {
244   analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
245   def_use_mgr->ForEachUser(var_inst, [users, this](Instruction* user) {
246     users->push_back(user);
247     if (user->opcode() == SpvOpCopyObject) {
248       FindUses(user, users);
249     }
250   });
251 }
252 
FeedsAStore(Instruction * inst) const253 bool LocalSingleStoreElimPass::FeedsAStore(Instruction* inst) const {
254   analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
255   return !def_use_mgr->WhileEachUser(inst, [this](Instruction* user) {
256     switch (user->opcode()) {
257       case SpvOpStore:
258         return false;
259       case SpvOpAccessChain:
260       case SpvOpInBoundsAccessChain:
261       case SpvOpCopyObject:
262         return !FeedsAStore(user);
263       case SpvOpLoad:
264       case SpvOpImageTexelPointer:
265       case SpvOpName:
266         return true;
267       default:
268         // Don't know if this instruction modifies the variable.
269         // Conservatively assume it is a store.
270         return user->IsDecoration();
271     }
272   });
273 }
274 
RewriteLoads(Instruction * store_inst,const std::vector<Instruction * > & uses,bool * all_rewritten)275 bool LocalSingleStoreElimPass::RewriteLoads(
276     Instruction* store_inst, const std::vector<Instruction*>& uses,
277     bool* all_rewritten) {
278   BasicBlock* store_block = context()->get_instr_block(store_inst);
279   DominatorAnalysis* dominator_analysis =
280       context()->GetDominatorAnalysis(store_block->GetParent());
281 
282   uint32_t stored_id;
283   if (store_inst->opcode() == SpvOpStore)
284     stored_id = store_inst->GetSingleWordInOperand(kStoreValIdInIdx);
285   else
286     stored_id = store_inst->GetSingleWordInOperand(kVariableInitIdInIdx);
287 
288   *all_rewritten = true;
289   bool modified = false;
290   for (Instruction* use : uses) {
291     if (use->opcode() == SpvOpStore) continue;
292     auto dbg_op = use->GetOpenCL100DebugOpcode();
293     if (dbg_op == OpenCLDebugInfo100DebugDeclare ||
294         dbg_op == OpenCLDebugInfo100DebugValue)
295       continue;
296     if (use->opcode() == SpvOpLoad &&
297         dominator_analysis->Dominates(store_inst, use)) {
298       modified = true;
299       context()->KillNamesAndDecorates(use->result_id());
300       context()->ReplaceAllUsesWith(use->result_id(), stored_id);
301       context()->KillInst(use);
302     } else {
303       *all_rewritten = false;
304     }
305   }
306 
307   return modified;
308 }
309 
310 }  // namespace opt
311 }  // namespace spvtools
312