graphics_robust_access_pass.h 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156
  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_GRAPHICS_ROBUST_ACCESS_PASS_H_
  15. #define SOURCE_OPT_GRAPHICS_ROBUST_ACCESS_PASS_H_
  16. #include <map>
  17. #include <unordered_map>
  18. #include "constants.h"
  19. #include "def_use_manager.h"
  20. #include "instruction.h"
  21. #include "module.h"
  22. #include "pass.h"
  23. #include "source/diagnostic.h"
  24. #include "type_manager.h"
  25. namespace spvtools {
  26. namespace opt {
  27. // See optimizer.hpp for documentation.
  28. class GraphicsRobustAccessPass : public Pass {
  29. public:
  30. GraphicsRobustAccessPass();
  31. const char* name() const override { return "graphics-robust-access"; }
  32. Status Process() override;
  33. IRContext::Analysis GetPreservedAnalyses() override {
  34. return IRContext::kAnalysisDefUse |
  35. IRContext::kAnalysisInstrToBlockMapping |
  36. IRContext::kAnalysisConstants | IRContext::kAnalysisTypes |
  37. IRContext::kAnalysisIdToFuncMapping;
  38. }
  39. private:
  40. // Records failure for the current module, and returns a stream
  41. // that can be used to provide user error information to the message
  42. // consumer.
  43. spvtools::DiagnosticStream Fail();
  44. // Returns SPV_SUCCESS if this pass can correctly process the module,
  45. // as far as we can tell from capabilities and the memory model.
  46. // Otherwise logs a message and returns a failure code.
  47. spv_result_t IsCompatibleModule();
  48. // Transform the current module, if possible. Failure and modification
  49. // status is recorded in the |_| member. On failure, error information is
  50. // posted to the message consumer. The return value has no significance.
  51. spv_result_t ProcessCurrentModule();
  52. // Process the given function. Updates the state value |_|. Returns true
  53. // if the module was modified. This can log a failure.
  54. bool ProcessAFunction(opt::Function*);
  55. // Clamps indices in the OpAccessChain or OpInBoundsAccessChain instruction
  56. // |access_chain|. Inserts instructions before the given instruction. Updates
  57. // analyses and records that the module is modified. This can log a failure.
  58. void ClampIndicesForAccessChain(Instruction* access_chain);
  59. // Returns the id of the instruction importing the "GLSL.std.450" extended
  60. // instruction set. If it does not yet exist, the import instruction is
  61. // created and inserted into the module, and updates |_.modified| and
  62. // |_.glsl_insts_id|.
  63. uint32_t GetGlslInsts();
  64. // Returns an instruction which is constant with the given value of the given
  65. // type. Ignores any value bits beyond the width of the type.
  66. Instruction* GetValueForType(uint64_t value, const analysis::Integer* type);
  67. // Converts an integer value to an unsigned wider integer type, using either
  68. // sign extension or zero extension. The new instruction is inserted
  69. // immediately before |before_inst|, and is analyzed for definitions and uses.
  70. // Returns the newly inserted instruction. Assumes the |value| is an integer
  71. // scalar of a narrower type than |bit_width| bits.
  72. Instruction* WidenInteger(bool sign_extend, uint32_t bit_width,
  73. Instruction* value, Instruction* before_inst);
  74. // Returns a new instruction that invokes the UMin GLSL.std.450 extended
  75. // instruction with the two given operands. That is, the result of the
  76. // instruction is:
  77. // - |x| if |x| is unsigned-less than |y|
  78. // - |y| otherwise
  79. // We assume that |x| and |y| are scalar integer types with the same
  80. // width. The instruction is inserted before |where|.
  81. opt::Instruction* MakeUMinInst(const analysis::TypeManager& tm,
  82. Instruction* x, Instruction* y,
  83. Instruction* where);
  84. // Returns a new instruction that invokes the SClamp GLSL.std.450 extended
  85. // instruction with the three given operands. That is, the result of the
  86. // instruction is:
  87. // - |min| if |x| is signed-less than |min|
  88. // - |max| if |x| is signed-more than |max|
  89. // - |x| otherwise.
  90. // We assume that |min| is signed-less-or-equal to |max|, and that the
  91. // operands all have the same scalar integer type. The instruction is
  92. // inserted before |where|.
  93. opt::Instruction* MakeSClampInst(const analysis::TypeManager& tm,
  94. Instruction* x, Instruction* min,
  95. Instruction* max, Instruction* where);
  96. // Returns a new instruction which evaluates to the length the runtime array
  97. // referenced by the access chain at the specified index. The instruction is
  98. // inserted before the access chain instruction. Returns a null pointer in
  99. // some cases if assumptions are violated (rather than asserting out).
  100. opt::Instruction* MakeRuntimeArrayLengthInst(Instruction* access_chain,
  101. uint32_t operand_index);
  102. // Clamps the coordinate for an OpImageTexelPointer so it stays within
  103. // the bounds of the size of the image. Updates analyses and records that
  104. // the module is modified. Returns a status code to indicate success
  105. // or failure. If assumptions are not met, returns an error status code
  106. // and emits a diagnostic.
  107. spv_result_t ClampCoordinateForImageTexelPointer(
  108. opt::Instruction* image_texel_pointer);
  109. // Gets the instruction that defines the given id.
  110. opt::Instruction* GetDef(uint32_t id) {
  111. return context()->get_def_use_mgr()->GetDef(id);
  112. }
  113. // Returns a new instruction inserted before |where_inst|, and created from
  114. // the remaining arguments. Registers the definitions and uses of the new
  115. // instruction and also records its block.
  116. opt::Instruction* InsertInst(opt::Instruction* where_inst, spv::Op opcode,
  117. uint32_t type_id, uint32_t result_id,
  118. const Instruction::OperandList& operands);
  119. // State required for the current module.
  120. struct PerModuleState {
  121. // This pass modified the module.
  122. bool modified = false;
  123. // True if there is an error processing the current module, e.g. if
  124. // preconditions are not met.
  125. bool failed = false;
  126. // The id of the GLSL.std.450 extended instruction set. Zero if it does
  127. // not exist.
  128. uint32_t glsl_insts_id = 0;
  129. } module_status_;
  130. };
  131. } // namespace opt
  132. } // namespace spvtools
  133. #endif // SOURCE_OPT_GRAPHICS_ROBUST_ACCESS_PASS_H_