spread_volatile_semantics.h 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  1. // Copyright (c) 2022 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_SPREAD_VOLATILE_SEMANTICS_H_
  15. #define SOURCE_OPT_SPREAD_VOLATILE_SEMANTICS_H_
  16. #include "source/opt/pass.h"
  17. namespace spvtools {
  18. namespace opt {
  19. // See optimizer.hpp for documentation.
  20. class SpreadVolatileSemantics : public Pass {
  21. public:
  22. SpreadVolatileSemantics() {}
  23. const char* name() const override { return "spread-volatile-semantics"; }
  24. Status Process() override;
  25. IRContext::Analysis GetPreservedAnalyses() override {
  26. return IRContext::kAnalysisDefUse | IRContext::kAnalysisDecorations |
  27. IRContext::kAnalysisInstrToBlockMapping;
  28. }
  29. private:
  30. // Returns true if it does not have an execution model. Linkage shaders do not
  31. // have an execution model.
  32. bool HasNoExecutionModel() {
  33. return get_module()->entry_points().empty() &&
  34. context()->get_feature_mgr()->HasCapability(
  35. spv::Capability::Linkage);
  36. }
  37. // Iterates interface variables and spreads the Volatile semantics if it has
  38. // load instructions for the Volatile semantics.
  39. Pass::Status SpreadVolatileSemanticsToVariables(
  40. const bool is_vk_memory_model_enabled);
  41. // Returns whether |var_id| is the result id of a target builtin variable for
  42. // the volatile semantics for |execution_model| based on the Vulkan spec
  43. // VUID-StandaloneSpirv-VulkanMemoryModel-04678 or
  44. // VUID-StandaloneSpirv-VulkanMemoryModel-04679.
  45. bool IsTargetForVolatileSemantics(uint32_t var_id,
  46. spv::ExecutionModel execution_model);
  47. // Collects interface variables that need the volatile semantics.
  48. // |is_vk_memory_model_enabled| is true if VulkanMemoryModel capability is
  49. // enabled.
  50. void CollectTargetsForVolatileSemantics(
  51. const bool is_vk_memory_model_enabled);
  52. // Reports an error if an interface variable is used by two entry points and
  53. // it needs the Volatile decoration for one but not for another. Returns true
  54. // if the error must be reported.
  55. bool HasInterfaceInConflictOfVolatileSemantics();
  56. // Returns whether the variable whose result is |var_id| is used by a
  57. // non-volatile load or a pointer to it is used by a non-volatile load in
  58. // |entry_point| or not.
  59. bool IsTargetUsedByNonVolatileLoadInEntryPoint(uint32_t var_id,
  60. Instruction* entry_point);
  61. // Visits load instructions of pointers to variable whose result id is
  62. // |var_id| if the load instructions are in reachable functions from entry
  63. // points. |handle_load| is a function to do some actions for the load
  64. // instructions. Finishes the traversal and returns false if |handle_load|
  65. // returns false for a load instruction. Otherwise, returns true after running
  66. // |handle_load| for all the load instructions.
  67. bool VisitLoadsOfPointersToVariableInEntries(
  68. uint32_t var_id, const std::function<bool(Instruction*)>& handle_load,
  69. const std::unordered_set<uint32_t>& function_ids);
  70. // Sets Memory Operands of OpLoad instructions that load |var| or pointers
  71. // of |var| as Volatile if the function id of the OpLoad instruction is
  72. // included in |entry_function_ids|.
  73. void SetVolatileForLoadsInEntries(
  74. Instruction* var, const std::unordered_set<uint32_t>& entry_function_ids);
  75. // Adds OpDecorate Volatile for |var| if it does not exist.
  76. void DecorateVarWithVolatile(Instruction* var);
  77. // Returns a set of entry function ids to spread the volatile semantics for
  78. // the variable with the result id |var_id|.
  79. std::unordered_set<uint32_t> EntryFunctionsToSpreadVolatileSemanticsForVar(
  80. uint32_t var_id) {
  81. auto itr = var_ids_to_entry_fn_for_volatile_semantics_.find(var_id);
  82. if (itr == var_ids_to_entry_fn_for_volatile_semantics_.end()) return {};
  83. return itr->second;
  84. }
  85. // Specifies that we have to spread the volatile semantics for the
  86. // variable with the result id |var_id| for the entry point |entry_point|.
  87. void MarkVolatileSemanticsForVariable(uint32_t var_id,
  88. Instruction* entry_point);
  89. // Result ids of variables to entry function ids for the volatile semantics
  90. // spread.
  91. std::unordered_map<uint32_t, std::unordered_set<uint32_t>>
  92. var_ids_to_entry_fn_for_volatile_semantics_;
  93. };
  94. } // namespace opt
  95. } // namespace spvtools
  96. #endif // SOURCE_OPT_SPREAD_VOLATILE_SEMANTICS_H_