• 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_LIVENESS_H_
17 #define SOURCE_OPT_LIVENESS_H_
18 
19 #include <cstdint>
20 #include <unordered_set>
21 
22 namespace spvtools {
23 namespace opt {
24 
25 class IRContext;
26 class Instruction;
27 
28 namespace analysis {
29 
30 class Type;
31 
32 // This class represents the liveness of the input variables of a module
33 class LivenessManager {
34  public:
35   LivenessManager(IRContext* ctx);
36 
37   // Copy liveness info into |live_locs| and |builtin_locs|.
38   void GetLiveness(std::unordered_set<uint32_t>* live_locs,
39                    std::unordered_set<uint32_t>* live_builtins);
40 
41   // Return true if builtin |bi| is being analyzed.
42   bool IsAnalyzedBuiltin(uint32_t bi);
43 
44   // Return the result type of |ac| when applied to |cur_type_id|. Set
45   // |no_loc| to true if no loc found. Set |is_patch| indicates if the variable
46   // is a patch variable. Set |input| if the variable is an input variable.
47   // Otherwise it is assumed that the variable is an output variable.
48   uint32_t AnalyzeAccessChainLoc(const Instruction* ac, uint32_t curr_type_id,
49                                  uint32_t* offset, bool* no_loc, bool is_patch,
50                                  bool input = true);
51 
52   // Return size of |type_id| in units of locations
53   uint32_t GetLocSize(const analysis::Type* type) const;
54 
55  private:
context()56   IRContext* context() const { return ctx_; }
57 
58   // Initialize analysis
59   void InitializeAnalysis();
60 
61   // Analyze |id| for builtin var and struct members. Return true if builtins
62   // found.
63   bool AnalyzeBuiltIn(uint32_t id);
64 
65   // Mark all live locations resulting from |user| of |var| at |loc|.
66   void MarkRefLive(const Instruction* user, Instruction* var);
67 
68   // Mark |count| locations starting at location |start|.
69   void MarkLocsLive(uint32_t start, uint32_t count);
70 
71   // Return type of the member |index| in the aggregate type |agg_type_id|.
72   uint32_t GetComponentType(uint32_t index, uint32_t agg_type_id) const;
73 
74   // Return offset of member |index| in the aggregate type |agg_type_id| in
75   // units of input locations.
76   uint32_t GetLocOffset(uint32_t index, uint32_t agg_type_id) const;
77 
78   // Populate live_locs_ and live_builtins_
79   void ComputeLiveness();
80 
81   // IR context that owns this liveness manager.
82   IRContext* ctx_;
83 
84   // True if live_locs_ and live_builtins_ are computed
85   bool computed_;
86 
87   // Live locations
88   std::unordered_set<uint32_t> live_locs_;
89 
90   // Live builtins
91   std::unordered_set<uint32_t> live_builtins_;
92 };
93 
94 }  // namespace analysis
95 }  // namespace opt
96 }  // namespace spvtools
97 
98 #endif  // SOURCE_OPT_LIVENESS_H_
99