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_NV_ray_tracing",
128 "SPV_KHR_ray_query",
129 "SPV_EXT_fragment_invocation_density",
130 "SPV_EXT_physical_storage_buffer",
131 "SPV_KHR_physical_storage_buffer",
132 "SPV_KHR_terminate_invocation",
133 "SPV_KHR_subgroup_uniform_control_flow",
134 "SPV_KHR_integer_dot_product",
135 "SPV_EXT_shader_image_int64",
136 "SPV_KHR_non_semantic_info",
137 "SPV_KHR_uniform_group_instructions",
138 "SPV_KHR_fragment_shader_barycentric",
139 "SPV_KHR_vulkan_memory_model",
140 "SPV_NV_bindless_texture",
141 "SPV_EXT_shader_atomic_float_add",
142 "SPV_EXT_fragment_shader_interlock",
143 "SPV_NV_compute_shader_derivatives"});
144 }
ProcessVariable(Instruction * var_inst)145 bool LocalSingleStoreElimPass::ProcessVariable(Instruction* var_inst) {
146 std::vector<Instruction*> users;
147 FindUses(var_inst, &users);
148
149 Instruction* store_inst = FindSingleStoreAndCheckUses(var_inst, users);
150
151 if (store_inst == nullptr) {
152 return false;
153 }
154
155 bool all_rewritten;
156 bool modified = RewriteLoads(store_inst, users, &all_rewritten);
157
158 // If all uses are rewritten and the variable has a DebugDeclare and the
159 // variable is not an aggregate, add a DebugValue after the store and remove
160 // the DebugDeclare.
161 uint32_t var_id = var_inst->result_id();
162 if (all_rewritten &&
163 context()->get_debug_info_mgr()->IsVariableDebugDeclared(var_id)) {
164 const analysis::Type* var_type =
165 context()->get_type_mgr()->GetType(var_inst->type_id());
166 const analysis::Type* store_type = var_type->AsPointer()->pointee_type();
167 if (!(store_type->AsStruct() || store_type->AsArray())) {
168 modified |= RewriteDebugDeclares(store_inst, var_id);
169 }
170 }
171
172 return modified;
173 }
174
RewriteDebugDeclares(Instruction * store_inst,uint32_t var_id)175 bool LocalSingleStoreElimPass::RewriteDebugDeclares(Instruction* store_inst,
176 uint32_t var_id) {
177 uint32_t value_id = store_inst->GetSingleWordInOperand(1);
178 bool modified = context()->get_debug_info_mgr()->AddDebugValueForVariable(
179 store_inst, var_id, value_id, store_inst);
180 modified |= context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
181 return modified;
182 }
183
FindSingleStoreAndCheckUses(Instruction * var_inst,const std::vector<Instruction * > & users) const184 Instruction* LocalSingleStoreElimPass::FindSingleStoreAndCheckUses(
185 Instruction* var_inst, const std::vector<Instruction*>& users) const {
186 // Make sure there is exactly 1 store.
187 Instruction* store_inst = nullptr;
188
189 // If |var_inst| has an initializer, then that will count as a store.
190 if (var_inst->NumInOperands() > 1) {
191 store_inst = var_inst;
192 }
193
194 for (Instruction* user : users) {
195 switch (user->opcode()) {
196 case spv::Op::OpStore:
197 // Since we are in the relaxed addressing mode, the use has to be the
198 // base address of the store, and not the value being store. Otherwise,
199 // we would have a pointer to a pointer to function scope memory, which
200 // is not allowed.
201 if (store_inst == nullptr) {
202 store_inst = user;
203 } else {
204 // More than 1 store.
205 return nullptr;
206 }
207 break;
208 case spv::Op::OpAccessChain:
209 case spv::Op::OpInBoundsAccessChain:
210 if (FeedsAStore(user)) {
211 // Has a partial store. Cannot propagate that.
212 return nullptr;
213 }
214 break;
215 case spv::Op::OpLoad:
216 case spv::Op::OpImageTexelPointer:
217 case spv::Op::OpName:
218 case spv::Op::OpCopyObject:
219 break;
220 case spv::Op::OpExtInst: {
221 auto dbg_op = user->GetCommonDebugOpcode();
222 if (dbg_op == CommonDebugInfoDebugDeclare ||
223 dbg_op == CommonDebugInfoDebugValue) {
224 break;
225 }
226 return nullptr;
227 }
228 default:
229 if (!user->IsDecoration()) {
230 // Don't know if this instruction modifies the variable.
231 // Conservatively assume it is a store.
232 return nullptr;
233 }
234 break;
235 }
236 }
237 return store_inst;
238 }
239
FindUses(const Instruction * var_inst,std::vector<Instruction * > * users) const240 void LocalSingleStoreElimPass::FindUses(
241 const Instruction* var_inst, std::vector<Instruction*>* users) const {
242 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
243 def_use_mgr->ForEachUser(var_inst, [users, this](Instruction* user) {
244 users->push_back(user);
245 if (user->opcode() == spv::Op::OpCopyObject) {
246 FindUses(user, users);
247 }
248 });
249 }
250
FeedsAStore(Instruction * inst) const251 bool LocalSingleStoreElimPass::FeedsAStore(Instruction* inst) const {
252 analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
253 return !def_use_mgr->WhileEachUser(inst, [this](Instruction* user) {
254 switch (user->opcode()) {
255 case spv::Op::OpStore:
256 return false;
257 case spv::Op::OpAccessChain:
258 case spv::Op::OpInBoundsAccessChain:
259 case spv::Op::OpCopyObject:
260 return !FeedsAStore(user);
261 case spv::Op::OpLoad:
262 case spv::Op::OpImageTexelPointer:
263 case spv::Op::OpName:
264 return true;
265 default:
266 // Don't know if this instruction modifies the variable.
267 // Conservatively assume it is a store.
268 return user->IsDecoration();
269 }
270 });
271 }
272
RewriteLoads(Instruction * store_inst,const std::vector<Instruction * > & uses,bool * all_rewritten)273 bool LocalSingleStoreElimPass::RewriteLoads(
274 Instruction* store_inst, const std::vector<Instruction*>& uses,
275 bool* all_rewritten) {
276 BasicBlock* store_block = context()->get_instr_block(store_inst);
277 DominatorAnalysis* dominator_analysis =
278 context()->GetDominatorAnalysis(store_block->GetParent());
279
280 uint32_t stored_id;
281 if (store_inst->opcode() == spv::Op::OpStore)
282 stored_id = store_inst->GetSingleWordInOperand(kStoreValIdInIdx);
283 else
284 stored_id = store_inst->GetSingleWordInOperand(kVariableInitIdInIdx);
285
286 *all_rewritten = true;
287 bool modified = false;
288 for (Instruction* use : uses) {
289 if (use->opcode() == spv::Op::OpStore) continue;
290 auto dbg_op = use->GetCommonDebugOpcode();
291 if (dbg_op == CommonDebugInfoDebugDeclare ||
292 dbg_op == CommonDebugInfoDebugValue)
293 continue;
294 if (use->opcode() == spv::Op::OpLoad &&
295 dominator_analysis->Dominates(store_inst, use)) {
296 modified = true;
297 context()->KillNamesAndDecorates(use->result_id());
298 context()->ReplaceAllUsesWith(use->result_id(), stored_id);
299 context()->KillInst(use);
300 } else {
301 *all_rewritten = false;
302 }
303 }
304
305 return modified;
306 }
307
308 } // namespace opt
309 } // namespace spvtools
310