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