local_single_store_elim_pass.cpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  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({"SPV_AMD_shader_explicit_vertex_parameter",
  80. "SPV_AMD_shader_trinary_minmax",
  81. "SPV_AMD_gcn_shader",
  82. "SPV_KHR_shader_ballot",
  83. "SPV_AMD_shader_ballot",
  84. "SPV_AMD_gpu_shader_half_float",
  85. "SPV_KHR_shader_draw_parameters",
  86. "SPV_KHR_subgroup_vote",
  87. "SPV_KHR_8bit_storage",
  88. "SPV_KHR_16bit_storage",
  89. "SPV_KHR_device_group",
  90. "SPV_KHR_multiview",
  91. "SPV_NVX_multiview_per_view_attributes",
  92. "SPV_NV_viewport_array2",
  93. "SPV_NV_stereo_view_rendering",
  94. "SPV_NV_sample_mask_override_coverage",
  95. "SPV_NV_geometry_shader_passthrough",
  96. "SPV_AMD_texture_gather_bias_lod",
  97. "SPV_KHR_storage_buffer_storage_class",
  98. "SPV_KHR_variable_pointers",
  99. "SPV_AMD_gpu_shader_int16",
  100. "SPV_KHR_post_depth_coverage",
  101. "SPV_KHR_shader_atomic_counter_ops",
  102. "SPV_EXT_shader_stencil_export",
  103. "SPV_EXT_shader_viewport_index_layer",
  104. "SPV_AMD_shader_image_load_store_lod",
  105. "SPV_AMD_shader_fragment_mask",
  106. "SPV_EXT_fragment_fully_covered",
  107. "SPV_AMD_gpu_shader_half_float_fetch",
  108. "SPV_GOOGLE_decorate_string",
  109. "SPV_GOOGLE_hlsl_functionality1",
  110. "SPV_NV_shader_subgroup_partitioned",
  111. "SPV_EXT_descriptor_indexing",
  112. "SPV_NV_fragment_shader_barycentric",
  113. "SPV_NV_compute_shader_derivatives",
  114. "SPV_NV_shader_image_footprint",
  115. "SPV_NV_shading_rate",
  116. "SPV_NV_mesh_shader",
  117. "SPV_EXT_mesh_shader",
  118. "SPV_NV_ray_tracing",
  119. "SPV_KHR_ray_query",
  120. "SPV_EXT_fragment_invocation_density",
  121. "SPV_EXT_physical_storage_buffer",
  122. "SPV_KHR_physical_storage_buffer",
  123. "SPV_KHR_terminate_invocation",
  124. "SPV_KHR_subgroup_uniform_control_flow",
  125. "SPV_KHR_integer_dot_product",
  126. "SPV_EXT_shader_image_int64",
  127. "SPV_KHR_non_semantic_info",
  128. "SPV_KHR_uniform_group_instructions",
  129. "SPV_KHR_fragment_shader_barycentric",
  130. "SPV_KHR_vulkan_memory_model",
  131. "SPV_NV_bindless_texture",
  132. "SPV_EXT_shader_atomic_float_add",
  133. "SPV_EXT_fragment_shader_interlock",
  134. "SPV_KHR_compute_shader_derivatives",
  135. "SPV_NV_cooperative_matrix",
  136. "SPV_KHR_cooperative_matrix",
  137. "SPV_KHR_ray_tracing_position_fetch",
  138. "SPV_AMDX_shader_enqueue",
  139. "SPV_KHR_fragment_shading_rate",
  140. "SPV_KHR_ray_tracing"});
  141. }
  142. bool LocalSingleStoreElimPass::ProcessVariable(Instruction* var_inst) {
  143. std::vector<Instruction*> users;
  144. FindUses(var_inst, &users);
  145. Instruction* store_inst = FindSingleStoreAndCheckUses(var_inst, users);
  146. if (store_inst == nullptr) {
  147. return false;
  148. }
  149. bool all_rewritten;
  150. bool modified = RewriteLoads(store_inst, users, &all_rewritten);
  151. // If all uses are rewritten and the variable has a DebugDeclare and the
  152. // variable is not an aggregate, add a DebugValue after the store and remove
  153. // the DebugDeclare.
  154. uint32_t var_id = var_inst->result_id();
  155. if (all_rewritten &&
  156. context()->get_debug_info_mgr()->IsVariableDebugDeclared(var_id)) {
  157. const analysis::Type* var_type =
  158. context()->get_type_mgr()->GetType(var_inst->type_id());
  159. const analysis::Type* store_type = var_type->AsPointer()->pointee_type();
  160. if (!(store_type->AsStruct() || store_type->AsArray())) {
  161. modified |= RewriteDebugDeclares(store_inst, var_id);
  162. }
  163. }
  164. return modified;
  165. }
  166. bool LocalSingleStoreElimPass::RewriteDebugDeclares(Instruction* store_inst,
  167. uint32_t var_id) {
  168. uint32_t value_id = store_inst->GetSingleWordInOperand(1);
  169. bool modified = context()->get_debug_info_mgr()->AddDebugValueForVariable(
  170. store_inst, var_id, value_id, store_inst);
  171. modified |= context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
  172. return modified;
  173. }
  174. Instruction* LocalSingleStoreElimPass::FindSingleStoreAndCheckUses(
  175. Instruction* var_inst, const std::vector<Instruction*>& users) const {
  176. // Make sure there is exactly 1 store.
  177. Instruction* store_inst = nullptr;
  178. // If |var_inst| has an initializer, then that will count as a store.
  179. if (var_inst->NumInOperands() > 1) {
  180. store_inst = var_inst;
  181. }
  182. for (Instruction* user : users) {
  183. switch (user->opcode()) {
  184. case spv::Op::OpStore:
  185. // Since we are in the relaxed addressing mode, the use has to be the
  186. // base address of the store, and not the value being store. Otherwise,
  187. // we would have a pointer to a pointer to function scope memory, which
  188. // is not allowed.
  189. if (store_inst == nullptr) {
  190. store_inst = user;
  191. } else {
  192. // More than 1 store.
  193. return nullptr;
  194. }
  195. break;
  196. case spv::Op::OpAccessChain:
  197. case spv::Op::OpInBoundsAccessChain:
  198. if (FeedsAStore(user)) {
  199. // Has a partial store. Cannot propagate that.
  200. return nullptr;
  201. }
  202. break;
  203. case spv::Op::OpLoad:
  204. case spv::Op::OpImageTexelPointer:
  205. case spv::Op::OpName:
  206. case spv::Op::OpCopyObject:
  207. break;
  208. case spv::Op::OpExtInst: {
  209. auto dbg_op = user->GetCommonDebugOpcode();
  210. if (dbg_op == CommonDebugInfoDebugDeclare ||
  211. dbg_op == CommonDebugInfoDebugValue) {
  212. break;
  213. }
  214. return nullptr;
  215. }
  216. default:
  217. if (!user->IsDecoration()) {
  218. // Don't know if this instruction modifies the variable.
  219. // Conservatively assume it is a store.
  220. return nullptr;
  221. }
  222. break;
  223. }
  224. }
  225. return store_inst;
  226. }
  227. void LocalSingleStoreElimPass::FindUses(
  228. const Instruction* var_inst, std::vector<Instruction*>* users) const {
  229. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  230. def_use_mgr->ForEachUser(var_inst, [users, this](Instruction* user) {
  231. users->push_back(user);
  232. if (user->opcode() == spv::Op::OpCopyObject) {
  233. FindUses(user, users);
  234. }
  235. });
  236. }
  237. bool LocalSingleStoreElimPass::FeedsAStore(Instruction* inst) const {
  238. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  239. return !def_use_mgr->WhileEachUser(inst, [this](Instruction* user) {
  240. switch (user->opcode()) {
  241. case spv::Op::OpStore:
  242. return false;
  243. case spv::Op::OpAccessChain:
  244. case spv::Op::OpInBoundsAccessChain:
  245. case spv::Op::OpCopyObject:
  246. return !FeedsAStore(user);
  247. case spv::Op::OpLoad:
  248. case spv::Op::OpImageTexelPointer:
  249. case spv::Op::OpName:
  250. return true;
  251. default:
  252. // Don't know if this instruction modifies the variable.
  253. // Conservatively assume it is a store.
  254. return user->IsDecoration();
  255. }
  256. });
  257. }
  258. bool LocalSingleStoreElimPass::RewriteLoads(
  259. Instruction* store_inst, const std::vector<Instruction*>& uses,
  260. bool* all_rewritten) {
  261. BasicBlock* store_block = context()->get_instr_block(store_inst);
  262. DominatorAnalysis* dominator_analysis =
  263. context()->GetDominatorAnalysis(store_block->GetParent());
  264. uint32_t stored_id;
  265. if (store_inst->opcode() == spv::Op::OpStore)
  266. stored_id = store_inst->GetSingleWordInOperand(kStoreValIdInIdx);
  267. else
  268. stored_id = store_inst->GetSingleWordInOperand(kVariableInitIdInIdx);
  269. *all_rewritten = true;
  270. bool modified = false;
  271. for (Instruction* use : uses) {
  272. if (use->opcode() == spv::Op::OpStore) continue;
  273. auto dbg_op = use->GetCommonDebugOpcode();
  274. if (dbg_op == CommonDebugInfoDebugDeclare ||
  275. dbg_op == CommonDebugInfoDebugValue)
  276. continue;
  277. if (use->opcode() == spv::Op::OpLoad &&
  278. dominator_analysis->Dominates(store_inst, use)) {
  279. modified = true;
  280. context()->KillNamesAndDecorates(use->result_id());
  281. context()->ReplaceAllUsesWith(use->result_id(), stored_id);
  282. context()->KillInst(use);
  283. } else {
  284. *all_rewritten = false;
  285. }
  286. }
  287. return modified;
  288. }
  289. } // namespace opt
  290. } // namespace spvtools