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 OpCompositeExtract |extract| and all of its references 68 // with an OpLoad of a replacement variable. |var| is the variable with 69 // composite type whose value is being used by |extract|. Assumes that 70 // |extract| is extracting one index only. Returns true on success, and false 71 // otherwise. 72 bool ReplaceCompositeExtract(Instruction* var, Instruction* extract); 73 74 // Returns the id of the variable that will be used to replace the |idx|th 75 // element of |var|. The variable is created if it has not already been 76 // created. 77 uint32_t GetReplacementVariable(Instruction* var, uint32_t idx); 78 79 // Returns the id of a new variable that can be used to replace the |idx|th 80 // element of |var|. 81 uint32_t CreateReplacementVariable(Instruction* var, uint32_t idx); 82 83 // Returns the number of bindings used by the given |type_id|. 84 // All types are considered to use 1 binding slot, except: 85 // 1- A pointer type consumes as many binding numbers as its pointee. 86 // 2- An array of size N consumes N*M binding numbers, where M is the number 87 // of bindings used by each array element. 88 // 3- The number of bindings consumed by a structure is the sum of the 89 // bindings used by its members. 90 uint32_t GetNumBindingsUsedByType(uint32_t type_id); 91 92 // Copy all of the decorations of variable |old_var| and make them as 93 // decorations for the new variable whose id is |new_var_id|. The new variable 94 // is supposed to replace |index|th element of |old_var|. 95 // |new_var_ptr_type_id| is the id of the pointer to the type of the new 96 // variable. |is_old_var_array| is true if |old_var| has an array type. 97 // |is_old_var_struct| is true if |old_var| has a structure type. 98 // |old_var_type| is the pointee type of |old_var|. 99 void CopyDecorationsForNewVariable(Instruction* old_var, uint32_t index, 100 uint32_t new_var_id, 101 uint32_t new_var_ptr_type_id, 102 const bool is_old_var_array, 103 const bool is_old_var_struct, 104 Instruction* old_var_type); 105 106 // Get the new binding number for a new variable that will be replaced with an 107 // |index|th element of an old variable. The old variable has |old_binding| 108 // as its binding number. |ptr_elem_type_id| the id of the pointer to the 109 // element type. |is_old_var_array| is true if the old variable has an array 110 // type. |is_old_var_struct| is true if the old variable has a structure type. 111 // |old_var_type| is the pointee type of the old variable. 112 uint32_t GetNewBindingForElement(uint32_t old_binding, uint32_t index, 113 uint32_t ptr_elem_type_id, 114 const bool is_old_var_array, 115 const bool is_old_var_struct, 116 Instruction* old_var_type); 117 118 // Create a new OpDecorate(String) instruction by cloning |old_decoration|. 119 // The new OpDecorate(String) instruction will be used for a variable whose id 120 // is |new_var_ptr_type_id|. If |old_decoration| is a decoration for a 121 // binding, the new OpDecorate(String) instruction will have |new_binding| as 122 // its binding. 123 void CreateNewDecorationForNewVariable(Instruction* old_decoration, 124 uint32_t new_var_id, 125 uint32_t new_binding); 126 127 // Create a new OpDecorate instruction whose operand is the same as an 128 // OpMemberDecorate instruction |old_member_decoration| except Target operand. 129 // The Target operand of the new OpDecorate instruction will be |new_var_id|. 130 void CreateNewDecorationForMemberDecorate(Instruction* old_decoration, 131 uint32_t new_var_id); 132 133 // A map from an OpVariable instruction to the set of variables that will be 134 // used to replace it. The entry |replacement_variables_[var][i]| is the id of 135 // a variable that will be used in the place of the ith element of the 136 // array |var|. If the entry is |0|, then the variable has not been 137 // created yet. 138 std::map<Instruction*, std::vector<uint32_t>> replacement_variables_; 139 }; 140 141 } // namespace opt 142 } // namespace spvtools 143 144 #endif // SOURCE_OPT_DESC_SROA_H_ 145