• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #ifndef SOURCE_OPT_ELIMINATE_DEAD_INPUT_COMPONENTS_H_
17 #define SOURCE_OPT_ELIMINATE_DEAD_INPUT_COMPONENTS_H_
18 
19 #include <unordered_map>
20 
21 #include "source/opt/ir_context.h"
22 #include "source/opt/module.h"
23 #include "source/opt/pass.h"
24 
25 namespace spvtools {
26 namespace opt {
27 
28 // See optimizer.hpp for documentation.
29 class EliminateDeadIOComponentsPass : public Pass {
30  public:
31   explicit EliminateDeadIOComponentsPass(spv::StorageClass elim_sclass,
32                                          bool safe_mode = true)
elim_sclass_(elim_sclass)33       : elim_sclass_(elim_sclass), safe_mode_(safe_mode) {}
34 
name()35   const char* name() const override {
36     return "eliminate-dead-input-components";
37   }
38   Status Process() override;
39 
40   // Return the mask of preserved Analyses.
GetPreservedAnalyses()41   IRContext::Analysis GetPreservedAnalyses() override {
42     return IRContext::kAnalysisDefUse |
43            IRContext::kAnalysisInstrToBlockMapping |
44            IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG |
45            IRContext::kAnalysisDominatorAnalysis |
46            IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisNameMap |
47            IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
48   }
49 
50  private:
51   // Find the max constant used to index the variable declared by |var|
52   // through OpAccessChain or OpInBoundsAccessChain. If any non-constant
53   // indices or non-Op*AccessChain use of |var|, return |original_max|.
54   unsigned FindMaxIndex(const Instruction& var, const unsigned original_max,
55                         const bool skip_first_index = false);
56 
57   // Change the length of the array |inst| to |length|
58   void ChangeArrayLength(Instruction& inst, unsigned length);
59 
60   // Change the length of the struct in |io_var| to |length|. |io_var|
61   // is either the struct or a per-vertex-array of the struct.
62   void ChangeIOVarStructLength(Instruction& io_var, unsigned length);
63 
64   // Storage class to be optimized. Must be Input or Output.
65   spv::StorageClass elim_sclass_;
66 
67   // Only make changes that will not cause interface incompatibility if done
68   // standalone. Currently this is only Input variables in vertex shaders.
69   bool safe_mode_;
70 };
71 
72 }  // namespace opt
73 }  // namespace spvtools
74 
75 #endif  // SOURCE_OPT_ELIMINATE_DEAD_INPUT_COMPONENTS_H_
76