local_single_store_elim_pass.cpp 11 KB

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