code_sink.h 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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. #ifndef SOURCE_OPT_CODE_SINK_H_
  15. #define SOURCE_OPT_CODE_SINK_H_
  16. #include <unordered_map>
  17. #include "source/opt/ir_context.h"
  18. #include "source/opt/module.h"
  19. #include "source/opt/pass.h"
  20. namespace spvtools {
  21. namespace opt {
  22. // This pass does code sinking for OpAccessChain and OpLoad on variables in
  23. // uniform storage or in read only memory. Code sinking is a transformation
  24. // where an instruction is moved into a more deeply nested construct.
  25. //
  26. // The goal is to move these instructions as close as possible to their uses
  27. // without having to execute them more often or to replicate the instruction.
  28. // Moving the instruction in this way can lead to shorter live ranges, which can
  29. // lead to less register pressure. It can also cause instructions to be
  30. // executed less often because they could be moved into one path of a selection
  31. // construct.
  32. //
  33. // This optimization can cause register pressure to rise if the operands of the
  34. // instructions go dead after the instructions being moved. That is why we only
  35. // move certain OpLoad and OpAccessChain instructions. They generally have
  36. // constants, loop induction variables, and global pointers as operands. The
  37. // operands are live for a longer time in most cases.
  38. class CodeSinkingPass : public Pass {
  39. public:
  40. const char* name() const override { return "code-sink"; }
  41. Status Process() override;
  42. // Return the mask of preserved Analyses.
  43. IRContext::Analysis GetPreservedAnalyses() override {
  44. return IRContext::kAnalysisDefUse |
  45. IRContext::kAnalysisInstrToBlockMapping |
  46. IRContext::kAnalysisCombinators | IRContext::kAnalysisCFG |
  47. IRContext::kAnalysisDominatorAnalysis |
  48. IRContext::kAnalysisLoopAnalysis | IRContext::kAnalysisNameMap |
  49. IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
  50. }
  51. private:
  52. // Sinks the instructions in |bb| as much as possible. Returns true if
  53. // something changes.
  54. bool SinkInstructionsInBB(BasicBlock* bb);
  55. // Tries the sink |inst| as much as possible. Returns true if the instruction
  56. // is moved.
  57. bool SinkInstruction(Instruction* inst);
  58. // Returns the basic block in which to move |inst| to move is as close as
  59. // possible to the uses of |inst| without increasing the number of times
  60. // |inst| will be executed. Return |nullptr| if there is no need to move
  61. // |inst|.
  62. BasicBlock* FindNewBasicBlockFor(Instruction* inst);
  63. // Return true if |inst| reference memory and it is possible that the data in
  64. // the memory changes at some point.
  65. bool ReferencesMutableMemory(Instruction* inst);
  66. // Returns true if the module contains an instruction that has a memory
  67. // semantics id as an operand, and the memory semantics enforces a
  68. // synchronization of uniform memory. See section 3.25 of the SPIR-V
  69. // specification.
  70. bool HasUniformMemorySync();
  71. // Returns true if there may be a store to the variable |var_inst|.
  72. bool HasPossibleStore(Instruction* var_inst);
  73. // Returns true if one of the basic blocks in |set| exists on a path from the
  74. // basic block |start| to |end|.
  75. bool IntersectsPath(uint32_t start, uint32_t end,
  76. const std::unordered_set<uint32_t>& set);
  77. // Returns true if |mem_semantics_id| is the id of a constant that, when
  78. // interpreted as a memory semantics mask enforces synchronization of uniform
  79. // memory. See section 3.25 of the SPIR-V specification.
  80. bool IsSyncOnUniform(uint32_t mem_semantics_id) const;
  81. // True if a check has for uniform storage has taken place.
  82. bool checked_for_uniform_sync_;
  83. // Cache of whether or not the module has a memory sync on uniform storage.
  84. // only valid if |check_for_uniform_sync_| is true.
  85. bool has_uniform_sync_;
  86. };
  87. } // namespace opt
  88. } // namespace spvtools
  89. #endif // SOURCE_OPT_CODE_SINK_H_