1 // Copyright (c) 2018 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_REDUCE_LOAD_SIZE_H_ 16 #define SOURCE_OPT_REDUCE_LOAD_SIZE_H_ 17 18 #include <unordered_map> 19 20 #include "source/opt/ir_context.h" 21 #include "source/opt/module.h" 22 #include "source/opt/pass.h" 23 24 namespace spvtools { 25 namespace opt { 26 27 // See optimizer.hpp for documentation. 28 class ReduceLoadSize : public Pass { 29 public: ReduceLoadSize(double replacement_threshold)30 explicit ReduceLoadSize(double replacement_threshold) 31 : replacement_threshold_(replacement_threshold) {} 32 name()33 const char* name() const override { return "reduce-load-size"; } 34 Status Process() override; 35 36 // Return the mask of preserved Analyses. GetPreservedAnalyses()37 IRContext::Analysis GetPreservedAnalyses() override { 38 return IRContext::kAnalysisDefUse | 39 IRContext::kAnalysisInstrToBlockMapping | 40 IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG | 41 IRContext::kAnalysisDominatorAnalysis | 42 IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisNameMap | 43 IRContext::kAnalysisConstants | IRContext::kAnalysisTypes; 44 } 45 46 private: 47 // Replaces |inst|, which must be an OpCompositeExtract instruction, with 48 // an OpAccessChain and a load if possible. This happens only if it is a load 49 // feeding |inst|. Returns true if the substitution happened. The position 50 // of the new instructions will be in the same place as the load feeding the 51 // extract. 52 bool ReplaceExtract(Instruction* inst); 53 54 // Returns true if the OpCompositeExtract instruction |inst| should be replace 55 // or not. This is determined by looking at the load that feeds |inst| if 56 // it is a load. |should_replace_cache_| is used to cache the results based 57 // on the load feeding |inst|. 58 bool ShouldReplaceExtract(Instruction* inst); 59 60 // Threshold to determine whether we have to replace the load or not. If the 61 // ratio of the used components of the load is less than the threshold, we 62 // replace the load. 63 double replacement_threshold_; 64 65 // Maps the result id of an OpLoad instruction to the result of whether or 66 // not the OpCompositeExtract that use the id should be replaced. 67 std::unordered_map<uint32_t, bool> should_replace_cache_; 68 }; 69 70 } // namespace opt 71 } // namespace spvtools 72 73 #endif // SOURCE_OPT_REDUCE_LOAD_SIZE_H_ 74