desc_sroa.h 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. // Copyright (c) 2019 Google LLC
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #ifndef SOURCE_OPT_DESC_SROA_H_
  15. #define SOURCE_OPT_DESC_SROA_H_
  16. #include <cstdio>
  17. #include <memory>
  18. #include <queue>
  19. #include <unordered_map>
  20. #include <unordered_set>
  21. #include <vector>
  22. #include "source/opt/function.h"
  23. #include "source/opt/pass.h"
  24. #include "source/opt/type_manager.h"
  25. namespace spvtools {
  26. namespace opt {
  27. // Documented in optimizer.hpp
  28. class DescriptorScalarReplacement : public Pass {
  29. public:
  30. DescriptorScalarReplacement(bool flatten_composites, bool flatten_arrays)
  31. : flatten_composites_(flatten_composites),
  32. flatten_arrays_(flatten_arrays) {}
  33. const char* name() const override {
  34. if (flatten_composites_ && flatten_arrays_)
  35. return "descriptor-scalar-replacement";
  36. if (flatten_composites_) return "descriptor-compososite-scalar-replacement";
  37. return "descriptor-array-scalar-replacement";
  38. }
  39. Status Process() override;
  40. IRContext::Analysis GetPreservedAnalyses() override {
  41. return IRContext::kAnalysisDefUse |
  42. IRContext::kAnalysisInstrToBlockMapping |
  43. IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG |
  44. IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
  45. }
  46. private:
  47. // Replaces all references to |var| by new variables, one for each element of
  48. // the array |var|. The binding for the new variables corresponding to
  49. // element i will be the binding of |var| plus i. Returns true if successful.
  50. bool ReplaceCandidate(Instruction* var);
  51. // Replaces the base address |var| in the OpAccessChain or
  52. // OpInBoundsAccessChain instruction |use| by the variable that the access
  53. // chain accesses. The first index in |use| must be an |OpConstant|. Returns
  54. // |true| if successful.
  55. bool ReplaceAccessChain(Instruction* var, Instruction* use);
  56. // Replaces the given compososite variable |var| loaded by OpLoad |value| with
  57. // replacement variables, one for each component that's accessed in the
  58. // shader. Assumes that |value| is only used by OpCompositeExtract
  59. // instructions, one index at a time. Returns true on success, and false
  60. // otherwise.
  61. bool ReplaceLoadedValue(Instruction* var, Instruction* value);
  62. // Replaces the given composite variable |var| in the OpEntryPoint with the
  63. // new replacement variables, one for each element of the array |var|. Returns
  64. // |true| if successful, and |false| otherwise.
  65. bool ReplaceEntryPoint(Instruction* var, Instruction* use);
  66. // Replaces the given OpCompositeExtract |extract| and all of its references
  67. // with an OpLoad of a replacement variable. |var| is the variable with
  68. // composite type whose value is being used by |extract|. Assumes that
  69. // |extract| is extracting one index only. Returns true on success, and false
  70. // otherwise.
  71. bool ReplaceCompositeExtract(Instruction* var, Instruction* extract);
  72. // Returns the id of the variable that will be used to replace the |idx|th
  73. // element of |var|. The variable is created if it has not already been
  74. // created.
  75. uint32_t GetReplacementVariable(Instruction* var, uint32_t idx);
  76. // Returns the id of a new variable that can be used to replace the |idx|th
  77. // element of |var|.
  78. uint32_t CreateReplacementVariable(Instruction* var, uint32_t idx);
  79. // Returns the number of bindings used by the given |type_id|.
  80. // All types are considered to use 1 binding slot, except:
  81. // 1- A pointer type consumes as many binding numbers as its pointee.
  82. // 2- An array of size N consumes N*M binding numbers, where M is the number
  83. // of bindings used by each array element.
  84. // 3- The number of bindings consumed by a structure is the sum of the
  85. // bindings used by its members.
  86. uint32_t GetNumBindingsUsedByType(uint32_t type_id);
  87. // Copy all of the decorations of variable |old_var| and make them as
  88. // decorations for the new variable whose id is |new_var_id|. The new variable
  89. // is supposed to replace |index|th element of |old_var|.
  90. // |new_var_ptr_type_id| is the id of the pointer to the type of the new
  91. // variable. |is_old_var_array| is true if |old_var| has an array type.
  92. // |is_old_var_struct| is true if |old_var| has a structure type.
  93. // |old_var_type| is the pointee type of |old_var|.
  94. void CopyDecorationsForNewVariable(Instruction* old_var, uint32_t index,
  95. uint32_t new_var_id,
  96. uint32_t new_var_ptr_type_id,
  97. const bool is_old_var_array,
  98. const bool is_old_var_struct,
  99. Instruction* old_var_type);
  100. // Get the new binding number for a new variable that will be replaced with an
  101. // |index|th element of an old variable. The old variable has |old_binding|
  102. // as its binding number. |ptr_elem_type_id| the id of the pointer to the
  103. // element type. |is_old_var_array| is true if the old variable has an array
  104. // type. |is_old_var_struct| is true if the old variable has a structure type.
  105. // |old_var_type| is the pointee type of the old variable.
  106. uint32_t GetNewBindingForElement(uint32_t old_binding, uint32_t index,
  107. uint32_t ptr_elem_type_id,
  108. const bool is_old_var_array,
  109. const bool is_old_var_struct,
  110. Instruction* old_var_type);
  111. // Create a new OpDecorate(String) instruction by cloning |old_decoration|.
  112. // The new OpDecorate(String) instruction will be used for a variable whose id
  113. // is |new_var_ptr_type_id|. If |old_decoration| is a decoration for a
  114. // binding, the new OpDecorate(String) instruction will have |new_binding| as
  115. // its binding.
  116. void CreateNewDecorationForNewVariable(Instruction* old_decoration,
  117. uint32_t new_var_id,
  118. uint32_t new_binding);
  119. // Create a new OpDecorate instruction whose operand is the same as an
  120. // OpMemberDecorate instruction |old_member_decoration| except Target operand.
  121. // The Target operand of the new OpDecorate instruction will be |new_var_id|.
  122. void CreateNewDecorationForMemberDecorate(Instruction* old_decoration,
  123. uint32_t new_var_id);
  124. // A map from an OpVariable instruction to the set of variables that will be
  125. // used to replace it. The entry |replacement_variables_[var][i]| is the id of
  126. // a variable that will be used in the place of the ith element of the
  127. // array |var|. If the entry is |0|, then the variable has not been
  128. // created yet.
  129. std::map<Instruction*, std::vector<uint32_t>> replacement_variables_;
  130. bool flatten_composites_;
  131. bool flatten_arrays_;
  132. };
  133. } // namespace opt
  134. } // namespace spvtools
  135. #endif // SOURCE_OPT_DESC_SROA_H_