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_block_elim_pass.h"
18
19 #include <vector>
20
21 #include "source/opt/iterator.h"
22
23 namespace spvtools {
24 namespace opt {
25 namespace {
26
27 const uint32_t kStoreValIdInIdx = 1;
28
29 } // anonymous namespace
30
HasOnlySupportedRefs(uint32_t ptrId)31 bool LocalSingleBlockLoadStoreElimPass::HasOnlySupportedRefs(uint32_t ptrId) {
32 if (supported_ref_ptrs_.find(ptrId) != supported_ref_ptrs_.end()) return true;
33 if (get_def_use_mgr()->WhileEachUser(ptrId, [this](Instruction* user) {
34 auto dbg_op = user->GetOpenCL100DebugOpcode();
35 if (dbg_op == OpenCLDebugInfo100DebugDeclare ||
36 dbg_op == OpenCLDebugInfo100DebugValue) {
37 return true;
38 }
39 SpvOp op = user->opcode();
40 if (IsNonPtrAccessChain(op) || op == SpvOpCopyObject) {
41 if (!HasOnlySupportedRefs(user->result_id())) {
42 return false;
43 }
44 } else if (op != SpvOpStore && op != SpvOpLoad && op != SpvOpName &&
45 !IsNonTypeDecorate(op)) {
46 return false;
47 }
48 return true;
49 })) {
50 supported_ref_ptrs_.insert(ptrId);
51 return true;
52 }
53 return false;
54 }
55
LocalSingleBlockLoadStoreElim(Function * func)56 bool LocalSingleBlockLoadStoreElimPass::LocalSingleBlockLoadStoreElim(
57 Function* func) {
58 // Perform local store/load, load/load and store/store elimination
59 // on each block
60 bool modified = false;
61 std::vector<Instruction*> instructions_to_kill;
62 std::unordered_set<Instruction*> instructions_to_save;
63 for (auto bi = func->begin(); bi != func->end(); ++bi) {
64 var2store_.clear();
65 var2load_.clear();
66 auto next = bi->begin();
67 for (auto ii = next; ii != bi->end(); ii = next) {
68 ++next;
69 switch (ii->opcode()) {
70 case SpvOpStore: {
71 // Verify store variable is target type
72 uint32_t varId;
73 Instruction* ptrInst = GetPtr(&*ii, &varId);
74 if (!IsTargetVar(varId)) continue;
75 if (!HasOnlySupportedRefs(varId)) continue;
76 // If a store to the whole variable, remember it for succeeding
77 // loads and stores. Otherwise forget any previous store to that
78 // variable.
79 if (ptrInst->opcode() == SpvOpVariable) {
80 // If a previous store to same variable, mark the store
81 // for deletion if not still used. Don't delete store
82 // if debugging; let ssa-rewrite and DCE handle it
83 auto prev_store = var2store_.find(varId);
84 if (prev_store != var2store_.end() &&
85 instructions_to_save.count(prev_store->second) == 0 &&
86 !context()->get_debug_info_mgr()->IsVariableDebugDeclared(
87 varId)) {
88 instructions_to_kill.push_back(prev_store->second);
89 modified = true;
90 }
91
92 bool kill_store = false;
93 auto li = var2load_.find(varId);
94 if (li != var2load_.end()) {
95 if (ii->GetSingleWordInOperand(kStoreValIdInIdx) ==
96 li->second->result_id()) {
97 // We are storing the same value that already exists in the
98 // memory location. The store does nothing.
99 kill_store = true;
100 }
101 }
102
103 if (!kill_store) {
104 var2store_[varId] = &*ii;
105 var2load_.erase(varId);
106 } else {
107 instructions_to_kill.push_back(&*ii);
108 modified = true;
109 }
110 } else {
111 assert(IsNonPtrAccessChain(ptrInst->opcode()));
112 var2store_.erase(varId);
113 var2load_.erase(varId);
114 }
115 } break;
116 case SpvOpLoad: {
117 // Verify store variable is target type
118 uint32_t varId;
119 Instruction* ptrInst = GetPtr(&*ii, &varId);
120 if (!IsTargetVar(varId)) continue;
121 if (!HasOnlySupportedRefs(varId)) continue;
122 uint32_t replId = 0;
123 if (ptrInst->opcode() == SpvOpVariable) {
124 // If a load from a variable, look for a previous store or
125 // load from that variable and use its value.
126 auto si = var2store_.find(varId);
127 if (si != var2store_.end()) {
128 replId = si->second->GetSingleWordInOperand(kStoreValIdInIdx);
129 } else {
130 auto li = var2load_.find(varId);
131 if (li != var2load_.end()) {
132 replId = li->second->result_id();
133 }
134 }
135 } else {
136 // If a partial load of a previously seen store, remember
137 // not to delete the store.
138 auto si = var2store_.find(varId);
139 if (si != var2store_.end()) instructions_to_save.insert(si->second);
140 }
141 if (replId != 0) {
142 // replace load's result id and delete load
143 context()->KillNamesAndDecorates(&*ii);
144 context()->ReplaceAllUsesWith(ii->result_id(), replId);
145 instructions_to_kill.push_back(&*ii);
146 modified = true;
147 } else {
148 if (ptrInst->opcode() == SpvOpVariable)
149 var2load_[varId] = &*ii; // register load
150 }
151 } break;
152 case SpvOpFunctionCall: {
153 // Conservatively assume all locals are redefined for now.
154 // TODO(): Handle more optimally
155 var2store_.clear();
156 var2load_.clear();
157 } break;
158 default:
159 break;
160 }
161 }
162 }
163
164 for (Instruction* inst : instructions_to_kill) {
165 context()->KillInst(inst);
166 }
167
168 return modified;
169 }
170
Initialize()171 void LocalSingleBlockLoadStoreElimPass::Initialize() {
172 // Initialize Target Type Caches
173 seen_target_vars_.clear();
174 seen_non_target_vars_.clear();
175
176 // Clear collections
177 supported_ref_ptrs_.clear();
178
179 // Initialize extensions allowlist
180 InitExtensions();
181 }
182
AllExtensionsSupported() const183 bool LocalSingleBlockLoadStoreElimPass::AllExtensionsSupported() const {
184 // If any extension not in allowlist, return false
185 for (auto& ei : get_module()->extensions()) {
186 const char* extName =
187 reinterpret_cast<const char*>(&ei.GetInOperand(0).words[0]);
188 if (extensions_allowlist_.find(extName) == extensions_allowlist_.end())
189 return false;
190 }
191 return true;
192 }
193
ProcessImpl()194 Pass::Status LocalSingleBlockLoadStoreElimPass::ProcessImpl() {
195 // Assumes relaxed logical addressing only (see instruction.h).
196 if (context()->get_feature_mgr()->HasCapability(SpvCapabilityAddresses))
197 return Status::SuccessWithoutChange;
198
199 // Do not process if module contains OpGroupDecorate. Additional
200 // support required in KillNamesAndDecorates().
201 // TODO(greg-lunarg): Add support for OpGroupDecorate
202 for (auto& ai : get_module()->annotations())
203 if (ai.opcode() == SpvOpGroupDecorate) return Status::SuccessWithoutChange;
204 // If any extensions in the module are not explicitly supported,
205 // return unmodified.
206 if (!AllExtensionsSupported()) return Status::SuccessWithoutChange;
207 // Process all entry point functions
208 ProcessFunction pfn = [this](Function* fp) {
209 return LocalSingleBlockLoadStoreElim(fp);
210 };
211
212 bool modified = context()->ProcessEntryPointCallTree(pfn);
213 return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
214 }
215
216 LocalSingleBlockLoadStoreElimPass::LocalSingleBlockLoadStoreElimPass() =
217 default;
218
Process()219 Pass::Status LocalSingleBlockLoadStoreElimPass::Process() {
220 Initialize();
221 return ProcessImpl();
222 }
223
InitExtensions()224 void LocalSingleBlockLoadStoreElimPass::InitExtensions() {
225 extensions_allowlist_.clear();
226 extensions_allowlist_.insert({
227 "SPV_AMD_shader_explicit_vertex_parameter",
228 "SPV_AMD_shader_trinary_minmax",
229 "SPV_AMD_gcn_shader",
230 "SPV_KHR_shader_ballot",
231 "SPV_AMD_shader_ballot",
232 "SPV_AMD_gpu_shader_half_float",
233 "SPV_KHR_shader_draw_parameters",
234 "SPV_KHR_subgroup_vote",
235 "SPV_KHR_8bit_storage",
236 "SPV_KHR_16bit_storage",
237 "SPV_KHR_device_group",
238 "SPV_KHR_multiview",
239 "SPV_NVX_multiview_per_view_attributes",
240 "SPV_NV_viewport_array2",
241 "SPV_NV_stereo_view_rendering",
242 "SPV_NV_sample_mask_override_coverage",
243 "SPV_NV_geometry_shader_passthrough",
244 "SPV_AMD_texture_gather_bias_lod",
245 "SPV_KHR_storage_buffer_storage_class",
246 "SPV_KHR_variable_pointers",
247 "SPV_AMD_gpu_shader_int16",
248 "SPV_KHR_post_depth_coverage",
249 "SPV_KHR_shader_atomic_counter_ops",
250 "SPV_EXT_shader_stencil_export",
251 "SPV_EXT_shader_viewport_index_layer",
252 "SPV_AMD_shader_image_load_store_lod",
253 "SPV_AMD_shader_fragment_mask",
254 "SPV_EXT_fragment_fully_covered",
255 "SPV_AMD_gpu_shader_half_float_fetch",
256 "SPV_GOOGLE_decorate_string",
257 "SPV_GOOGLE_hlsl_functionality1",
258 "SPV_GOOGLE_user_type",
259 "SPV_NV_shader_subgroup_partitioned",
260 "SPV_EXT_demote_to_helper_invocation",
261 "SPV_EXT_descriptor_indexing",
262 "SPV_NV_fragment_shader_barycentric",
263 "SPV_NV_compute_shader_derivatives",
264 "SPV_NV_shader_image_footprint",
265 "SPV_NV_shading_rate",
266 "SPV_NV_mesh_shader",
267 "SPV_NV_ray_tracing",
268 "SPV_KHR_ray_tracing",
269 "SPV_KHR_ray_query",
270 "SPV_EXT_fragment_invocation_density",
271 "SPV_EXT_physical_storage_buffer",
272 "SPV_KHR_terminate_invocation",
273 });
274 }
275
276 } // namespace opt
277 } // namespace spvtools
278