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 decl, store_inst);
179 modified = true;
180 }
181 }
182 }
183 modified |= context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
184 return modified;
185 }
186
FindSingleStoreAndCheckUses(Instruction * var_inst,const std::vector<Instruction * > & users) const187 Instruction* LocalSingleStoreElimPass::FindSingleStoreAndCheckUses(
188 Instruction* var_inst, const std::vector<Instruction*>& users) const {
189 // Make sure there is exactly 1 store.
190 Instruction* store_inst = nullptr;
191
192 // If |var_inst| has an initializer, then that will count as a store.
193 if (var_inst->NumInOperands() > 1) {
194 store_inst = var_inst;
195 }
196
197 for (Instruction* user : users) {
198 switch (user->opcode()) {
199 case SpvOpStore:
200 // Since we are in the relaxed addressing mode, the use has to be the
201 // base address of the store, and not the value being store. Otherwise,
202 // we would have a pointer to a pointer to function scope memory, which
203 // is not allowed.
204 if (store_inst == nullptr) {
205 store_inst = user;
206 } else {
207 // More than 1 store.
208 return nullptr;
209 }
210 break;
211 case SpvOpAccessChain:
212 case SpvOpInBoundsAccessChain:
213 if (FeedsAStore(user)) {
214 // Has a partial store. Cannot propagate that.
215 return nullptr;
216 }
217 break;
218 case SpvOpLoad:
219 case SpvOpImageTexelPointer:
220 case SpvOpName:
221 case SpvOpCopyObject:
222 break;
223 case SpvOpExtInst: {
224 auto dbg_op = user->GetOpenCL100DebugOpcode();
225 if (dbg_op == OpenCLDebugInfo100DebugDeclare ||
226 dbg_op == OpenCLDebugInfo100DebugValue) {
227 break;
228 }
229 return nullptr;
230 }
231 default:
232 if (!user->IsDecoration()) {
233 // Don't know if this instruction modifies the variable.
234 // Conservatively assume it is a store.
235 return nullptr;
236 }
237 break;
238 }
239 }
240 return store_inst;
241 }
242
FindUses(const Instruction * var_inst,std::vector<Instruction * > * users) const243 void LocalSingleStoreElimPass::FindUses(
244 const Instruction* var_inst, std::vector<Instruction*>* users) const {
245 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
246 def_use_mgr->ForEachUser(var_inst, [users, this](Instruction* user) {
247 users->push_back(user);
248 if (user->opcode() == SpvOpCopyObject) {
249 FindUses(user, users);
250 }
251 });
252 }
253
FeedsAStore(Instruction * inst) const254 bool LocalSingleStoreElimPass::FeedsAStore(Instruction* inst) const {
255 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
256 return !def_use_mgr->WhileEachUser(inst, [this](Instruction* user) {
257 switch (user->opcode()) {
258 case SpvOpStore:
259 return false;
260 case SpvOpAccessChain:
261 case SpvOpInBoundsAccessChain:
262 case SpvOpCopyObject:
263 return !FeedsAStore(user);
264 case SpvOpLoad:
265 case SpvOpImageTexelPointer:
266 case SpvOpName:
267 return true;
268 default:
269 // Don't know if this instruction modifies the variable.
270 // Conservatively assume it is a store.
271 return user->IsDecoration();
272 }
273 });
274 }
275
RewriteLoads(Instruction * store_inst,const std::vector<Instruction * > & uses,bool * all_rewritten)276 bool LocalSingleStoreElimPass::RewriteLoads(
277 Instruction* store_inst, const std::vector<Instruction*>& uses,
278 bool* all_rewritten) {
279 BasicBlock* store_block = context()->get_instr_block(store_inst);
280 DominatorAnalysis* dominator_analysis =
281 context()->GetDominatorAnalysis(store_block->GetParent());
282
283 uint32_t stored_id;
284 if (store_inst->opcode() == SpvOpStore)
285 stored_id = store_inst->GetSingleWordInOperand(kStoreValIdInIdx);
286 else
287 stored_id = store_inst->GetSingleWordInOperand(kVariableInitIdInIdx);
288
289 *all_rewritten = true;
290 bool modified = false;
291 for (Instruction* use : uses) {
292 if (use->opcode() == SpvOpStore) continue;
293 auto dbg_op = use->GetOpenCL100DebugOpcode();
294 if (dbg_op == OpenCLDebugInfo100DebugDeclare ||
295 dbg_op == OpenCLDebugInfo100DebugValue)
296 continue;
297 if (use->opcode() == SpvOpLoad &&
298 dominator_analysis->Dominates(store_inst, use)) {
299 modified = true;
300 context()->KillNamesAndDecorates(use->result_id());
301 context()->ReplaceAllUsesWith(use->result_id(), stored_id);
302 context()->KillInst(use);
303 } else {
304 *all_rewritten = false;
305 }
306 }
307
308 return modified;
309 }
310
311 } // namespace opt
312 } // namespace spvtools
313