• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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   // Returns true if |var| is an OpVariable instruction that represents a
50   // descriptor array.  These are the variables that we want to replace.
51   bool IsCandidate(Instruction* var);
52 
53   // Replaces all references to |var| by new variables, one for each element of
54   // the array |var|.  The binding for the new variables corresponding to
55   // element i will be the binding of |var| plus i.  Returns true if successful.
56   bool ReplaceCandidate(Instruction* var);
57 
58   // Replaces the base address |var| in the OpAccessChain or
59   // OpInBoundsAccessChain instruction |use| by the variable that the access
60   // chain accesses.  The first index in |use| must be an |OpConstant|.  Returns
61   // |true| if successful.
62   bool ReplaceAccessChain(Instruction* var, Instruction* use);
63 
64   // Replaces the given compososite variable |var| loaded by OpLoad |value| with
65   // replacement variables, one for each component that's accessed in the
66   // shader. Assumes that |value| is only used by OpCompositeExtract
67   // instructions, one index at a time. Returns true on success, and false
68   // otherwise.
69   bool ReplaceLoadedValue(Instruction* var, Instruction* value);
70 
71   // Replaces the given OpCompositeExtract |extract| and all of its references
72   // with an OpLoad of a replacement variable. |var| is the variable with
73   // composite type whose value is being used by |extract|. Assumes that
74   // |extract| is extracting one index only. Returns true on success, and false
75   // otherwise.
76   bool ReplaceCompositeExtract(Instruction* var, Instruction* extract);
77 
78   // Returns the id of the variable that will be used to replace the |idx|th
79   // element of |var|.  The variable is created if it has not already been
80   // created.
81   uint32_t GetReplacementVariable(Instruction* var, uint32_t idx);
82 
83   // Returns the id of a new variable that can be used to replace the |idx|th
84   // element of |var|.
85   uint32_t CreateReplacementVariable(Instruction* var, uint32_t idx);
86 
87   // Returns the number of bindings used by the given |type_id|.
88   // All types are considered to use 1 binding slot, except:
89   // 1- A pointer type consumes as many binding numbers as its pointee.
90   // 2- An array of size N consumes N*M binding numbers, where M is the number
91   // of bindings used by each array element.
92   // 3- The number of bindings consumed by a structure is the sum of the
93   // bindings used by its members.
94   uint32_t GetNumBindingsUsedByType(uint32_t type_id);
95 
96   // Returns true if |type| is a type that could be used for a structured buffer
97   // as opposed to a type that would be used for a structure of resource
98   // descriptors.
99   bool IsTypeOfStructuredBuffer(const Instruction* type) const;
100 
101   // A map from an OpVariable instruction to the set of variables that will be
102   // used to replace it. The entry |replacement_variables_[var][i]| is the id of
103   // a variable that will be used in the place of the the ith element of the
104   // array |var|. If the entry is |0|, then the variable has not been
105   // created yet.
106   std::map<Instruction*, std::vector<uint32_t>> replacement_variables_;
107 };
108 
109 }  // namespace opt
110 }  // namespace spvtools
111 
112 #endif  // SOURCE_OPT_DESC_SROA_H_
113