2
0

liveness.h 3.1 KB

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