eliminate_dead_io_components_pass.cpp 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256
  1. // Copyright (c) 2022 The Khronos Group Inc.
  2. // Copyright (c) 2022 LunarG Inc.
  3. //
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. //
  8. // http://www.apache.org/licenses/LICENSE-2.0
  9. //
  10. // Unless required by applicable law or agreed to in writing, software
  11. // distributed under the License is distributed on an "AS IS" BASIS,
  12. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. // See the License for the specific language governing permissions and
  14. // limitations under the License.
  15. #include "source/opt/eliminate_dead_io_components_pass.h"
  16. #include <vector>
  17. #include "source/opt/instruction.h"
  18. #include "source/opt/ir_context.h"
  19. #include "source/util/bit_vector.h"
  20. namespace spvtools {
  21. namespace opt {
  22. namespace {
  23. constexpr uint32_t kAccessChainBaseInIdx = 0;
  24. constexpr uint32_t kAccessChainIndex0InIdx = 1;
  25. constexpr uint32_t kAccessChainIndex1InIdx = 2;
  26. constexpr uint32_t kConstantValueInIdx = 0;
  27. } // namespace
  28. Pass::Status EliminateDeadIOComponentsPass::Process() {
  29. // Only process input and output variables
  30. if (elim_sclass_ != spv::StorageClass::Input &&
  31. elim_sclass_ != spv::StorageClass::Output) {
  32. if (consumer()) {
  33. std::string message =
  34. "EliminateDeadIOComponentsPass only valid for input and output "
  35. "variables.";
  36. consumer()(SPV_MSG_ERROR, 0, {0, 0, 0}, message.c_str());
  37. }
  38. return Status::Failure;
  39. }
  40. // If safe mode, only process Input variables in vertex shader
  41. const auto stage = context()->GetStage();
  42. if (safe_mode_ && !(stage == spv::ExecutionModel::Vertex &&
  43. elim_sclass_ == spv::StorageClass::Input))
  44. return Status::SuccessWithoutChange;
  45. // Current functionality assumes shader capability.
  46. if (!context()->get_feature_mgr()->HasCapability(spv::Capability::Shader))
  47. return Status::SuccessWithoutChange;
  48. // Current functionality assumes vert, frag, tesc, tese or geom shader.
  49. // TODO(issue #4988): Add GLCompute.
  50. if (stage != spv::ExecutionModel::Vertex &&
  51. stage != spv::ExecutionModel::Fragment &&
  52. stage != spv::ExecutionModel::TessellationControl &&
  53. stage != spv::ExecutionModel::TessellationEvaluation &&
  54. stage != spv::ExecutionModel::Geometry)
  55. return Status::SuccessWithoutChange;
  56. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  57. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  58. bool modified = false;
  59. std::vector<Instruction*> vars_to_move;
  60. for (auto& var : context()->types_values()) {
  61. if (var.opcode() != spv::Op::OpVariable) {
  62. continue;
  63. }
  64. analysis::Type* var_type = type_mgr->GetType(var.type_id());
  65. analysis::Pointer* ptr_type = var_type->AsPointer();
  66. if (ptr_type == nullptr) {
  67. continue;
  68. }
  69. const auto sclass = ptr_type->storage_class();
  70. if (sclass != elim_sclass_) {
  71. continue;
  72. }
  73. // For tesc, or input variables in tese or geom shaders,
  74. // there is a outer per-vertex-array that must be ignored
  75. // for the purposes of this analysis/optimization. Do the
  76. // analysis on the inner type in these cases.
  77. bool skip_first_index = false;
  78. auto core_type = ptr_type->pointee_type();
  79. if (stage == spv::ExecutionModel::TessellationControl ||
  80. (sclass == spv::StorageClass::Input &&
  81. (stage == spv::ExecutionModel::TessellationEvaluation ||
  82. stage == spv::ExecutionModel::Geometry))) {
  83. auto arr_type = core_type->AsArray();
  84. if (!arr_type) continue;
  85. core_type = arr_type->element_type();
  86. skip_first_index = true;
  87. }
  88. const analysis::Array* arr_type = core_type->AsArray();
  89. if (arr_type != nullptr) {
  90. // Only process array if input of vertex shader, or output of
  91. // fragment shader. Otherwise, if one shader has a runtime index and the
  92. // other does not, interface incompatibility can occur.
  93. if (!((sclass == spv::StorageClass::Input &&
  94. stage == spv::ExecutionModel::Vertex) ||
  95. (sclass == spv::StorageClass::Output &&
  96. stage == spv::ExecutionModel::Fragment)))
  97. continue;
  98. unsigned arr_len_id = arr_type->LengthId();
  99. Instruction* arr_len_inst = def_use_mgr->GetDef(arr_len_id);
  100. if (arr_len_inst->opcode() != spv::Op::OpConstant) {
  101. continue;
  102. }
  103. // SPIR-V requires array size is >= 1, so this works for signed or
  104. // unsigned size.
  105. unsigned original_max =
  106. arr_len_inst->GetSingleWordInOperand(kConstantValueInIdx) - 1;
  107. unsigned max_idx = FindMaxIndex(var, original_max);
  108. if (max_idx != original_max) {
  109. ChangeArrayLength(var, max_idx + 1);
  110. vars_to_move.push_back(&var);
  111. modified = true;
  112. }
  113. continue;
  114. }
  115. const analysis::Struct* struct_type = core_type->AsStruct();
  116. if (struct_type == nullptr) continue;
  117. const auto elt_types = struct_type->element_types();
  118. unsigned original_max = static_cast<unsigned>(elt_types.size()) - 1;
  119. unsigned max_idx = FindMaxIndex(var, original_max, skip_first_index);
  120. if (max_idx != original_max) {
  121. ChangeIOVarStructLength(var, max_idx + 1);
  122. vars_to_move.push_back(&var);
  123. modified = true;
  124. }
  125. }
  126. // Move changed vars after their new type instruction to preserve backward
  127. // referencing.
  128. for (auto var : vars_to_move) {
  129. auto type_id = var->type_id();
  130. auto type_inst = def_use_mgr->GetDef(type_id);
  131. var->RemoveFromList();
  132. var->InsertAfter(type_inst);
  133. }
  134. return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
  135. }
  136. unsigned EliminateDeadIOComponentsPass::FindMaxIndex(
  137. const Instruction& var, const unsigned original_max,
  138. const bool skip_first_index) {
  139. unsigned max = 0;
  140. bool seen_non_const_ac = false;
  141. assert(var.opcode() == spv::Op::OpVariable && "must be variable");
  142. context()->get_def_use_mgr()->WhileEachUser(
  143. var.result_id(), [&max, &seen_non_const_ac, var, skip_first_index,
  144. this](Instruction* use) {
  145. auto use_opcode = use->opcode();
  146. if (use_opcode == spv::Op::OpLoad || use_opcode == spv::Op::OpStore ||
  147. use_opcode == spv::Op::OpCopyMemory ||
  148. use_opcode == spv::Op::OpCopyMemorySized ||
  149. use_opcode == spv::Op::OpCopyObject) {
  150. seen_non_const_ac = true;
  151. return false;
  152. }
  153. if (use->opcode() != spv::Op::OpAccessChain &&
  154. use->opcode() != spv::Op::OpInBoundsAccessChain) {
  155. return true;
  156. }
  157. // OpAccessChain with no indices currently not optimized
  158. if (use->NumInOperands() == 1 ||
  159. (skip_first_index && use->NumInOperands() == 2)) {
  160. seen_non_const_ac = true;
  161. return false;
  162. }
  163. const unsigned base_id =
  164. use->GetSingleWordInOperand(kAccessChainBaseInIdx);
  165. USE_ASSERT(base_id == var.result_id() && "unexpected base");
  166. const unsigned in_idx = skip_first_index ? kAccessChainIndex1InIdx
  167. : kAccessChainIndex0InIdx;
  168. const unsigned idx_id = use->GetSingleWordInOperand(in_idx);
  169. Instruction* idx_inst = context()->get_def_use_mgr()->GetDef(idx_id);
  170. if (idx_inst->opcode() != spv::Op::OpConstant) {
  171. seen_non_const_ac = true;
  172. return false;
  173. }
  174. unsigned value = idx_inst->GetSingleWordInOperand(kConstantValueInIdx);
  175. if (value > max) max = value;
  176. return true;
  177. });
  178. return seen_non_const_ac ? original_max : max;
  179. }
  180. void EliminateDeadIOComponentsPass::ChangeArrayLength(Instruction& arr_var,
  181. unsigned length) {
  182. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  183. analysis::ConstantManager* const_mgr = context()->get_constant_mgr();
  184. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  185. analysis::Pointer* ptr_type =
  186. type_mgr->GetType(arr_var.type_id())->AsPointer();
  187. const analysis::Array* arr_ty = ptr_type->pointee_type()->AsArray();
  188. assert(arr_ty && "expecting array type");
  189. uint32_t length_id = const_mgr->GetUIntConstId(length);
  190. analysis::Array new_arr_ty(arr_ty->element_type(),
  191. arr_ty->GetConstantLengthInfo(length_id, length));
  192. analysis::Type* reg_new_arr_ty = type_mgr->GetRegisteredType(&new_arr_ty);
  193. analysis::Pointer new_ptr_ty(reg_new_arr_ty, ptr_type->storage_class());
  194. analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
  195. uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
  196. arr_var.SetResultType(new_ptr_ty_id);
  197. def_use_mgr->AnalyzeInstUse(&arr_var);
  198. }
  199. void EliminateDeadIOComponentsPass::ChangeIOVarStructLength(Instruction& io_var,
  200. unsigned length) {
  201. analysis::TypeManager* type_mgr = context()->get_type_mgr();
  202. analysis::Pointer* ptr_type =
  203. type_mgr->GetType(io_var.type_id())->AsPointer();
  204. auto core_type = ptr_type->pointee_type();
  205. // Check for per-vertex-array of struct from tesc, tese and geom and grab
  206. // embedded struct type.
  207. const auto arr_type = core_type->AsArray();
  208. if (arr_type) core_type = arr_type->element_type();
  209. const analysis::Struct* struct_ty = core_type->AsStruct();
  210. assert(struct_ty && "expecting struct type");
  211. const auto orig_elt_types = struct_ty->element_types();
  212. std::vector<const analysis::Type*> new_elt_types;
  213. for (unsigned u = 0; u < length; ++u)
  214. new_elt_types.push_back(orig_elt_types[u]);
  215. analysis::Struct new_struct_ty(new_elt_types);
  216. uint32_t old_struct_ty_id = type_mgr->GetTypeInstruction(struct_ty);
  217. std::vector<Instruction*> decorations =
  218. context()->get_decoration_mgr()->GetDecorationsFor(old_struct_ty_id,
  219. true);
  220. for (auto dec : decorations) {
  221. if (dec->opcode() == spv::Op::OpMemberDecorate) {
  222. uint32_t midx = dec->GetSingleWordInOperand(1);
  223. if (midx >= length) continue;
  224. }
  225. type_mgr->AttachDecoration(*dec, &new_struct_ty);
  226. }
  227. // Clone name instructions for new struct type
  228. analysis::Type* reg_new_str_ty = type_mgr->GetRegisteredType(&new_struct_ty);
  229. uint32_t new_struct_ty_id = type_mgr->GetTypeInstruction(reg_new_str_ty);
  230. context()->CloneNames(old_struct_ty_id, new_struct_ty_id, length);
  231. // Attach new type to var
  232. analysis::Type* reg_new_var_ty = reg_new_str_ty;
  233. if (arr_type) {
  234. analysis::Array new_arr_ty(reg_new_var_ty, arr_type->length_info());
  235. reg_new_var_ty = type_mgr->GetRegisteredType(&new_arr_ty);
  236. }
  237. analysis::Pointer new_ptr_ty(reg_new_var_ty, elim_sclass_);
  238. analysis::Type* reg_new_ptr_ty = type_mgr->GetRegisteredType(&new_ptr_ty);
  239. uint32_t new_ptr_ty_id = type_mgr->GetTypeInstruction(reg_new_ptr_ty);
  240. io_var.SetResultType(new_ptr_ty_id);
  241. analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
  242. def_use_mgr->AnalyzeInstUse(&io_var);
  243. }
  244. } // namespace opt
  245. } // namespace spvtools