transformation_propagate_instruction_down.h 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  1. // Copyright (c) 2020 Vasyl Teliman
  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_FUZZ_TRANSFORMATION_PROPAGATE_INSTRUCTION_DOWN_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_PROPAGATE_INSTRUCTION_DOWN_H_
  16. #include <map>
  17. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  18. #include "source/fuzz/transformation.h"
  19. #include "source/fuzz/transformation_context.h"
  20. #include "source/opt/ir_context.h"
  21. namespace spvtools {
  22. namespace fuzz {
  23. class TransformationPropagateInstructionDown : public Transformation {
  24. public:
  25. explicit TransformationPropagateInstructionDown(
  26. protobufs::TransformationPropagateInstructionDown message);
  27. TransformationPropagateInstructionDown(
  28. uint32_t block_id, uint32_t phi_fresh_id,
  29. const std::map<uint32_t, uint32_t>& successor_id_to_fresh_id);
  30. // - It should be possible to apply this transformation to |block_id| (see
  31. // IsApplicableToBlock method).
  32. // - Every acceptable successor of |block_id| (see GetAcceptableSuccessors
  33. // method) must have an entry in the |successor_id_to_fresh_id| map unless
  34. // overflow ids are available.
  35. // - All values in |successor_id_to_fresh_id| and |phi_fresh_id| must be
  36. // unique and fresh.
  37. bool IsApplicable(
  38. opt::IRContext* ir_context,
  39. const TransformationContext& transformation_context) const override;
  40. // - Adds a clone of the propagated instruction into every acceptable
  41. // successor of |block_id|.
  42. // - Removes the original instruction.
  43. // - Creates an OpPhi instruction if possible, that tries to group created
  44. // clones.
  45. // - If the original instruction's id was irrelevant - marks created
  46. // instructions as irrelevant. Otherwise, marks the created instructions as
  47. // synonymous to each other if possible (i.e. skips instructions, copied
  48. // into dead blocks).
  49. void Apply(opt::IRContext* ir_context,
  50. TransformationContext* transformation_context) const override;
  51. protobufs::Transformation ToMessage() const override;
  52. // Returns true if this transformation can be applied to the block with id
  53. // |block_id|. Concretely, returns true iff:
  54. // - |block_id| is a result id of some reachable basic block in the module.
  55. // - the block has an instruction to propagate (see
  56. // GetInstructionToPropagate method).
  57. // - the block has at least one acceptable successor (see
  58. // GetAcceptableSuccessors method).
  59. // - none of the acceptable successors have OpPhi instructions that use the
  60. // original instruction.
  61. // - it is possible to replace every use of the original instruction with some
  62. // of the propagated instructions (or an OpPhi if we can create it - see
  63. // GetOpPhiBlockId method).
  64. static bool IsApplicableToBlock(opt::IRContext* ir_context,
  65. uint32_t block_id);
  66. // Returns ids of successors of |block_id|, that can be used to propagate an
  67. // instruction into. Concretely, a successor block is acceptable if all
  68. // dependencies of the propagated instruction dominate it. Note that this
  69. // implies that an acceptable successor must be reachable in the CFG.
  70. // For example:
  71. // %1 = OpLabel
  72. // OpSelectionMerge %2 None
  73. // OpBranchConditional %cond %2 %3
  74. // %3 = OpLabel
  75. // %4 = OpUndef %int
  76. // %5 = OpCopyObject %int %4
  77. // OpBranch %2
  78. // %2 = OpLabel
  79. // ...
  80. // In this example, %2 is not an acceptable successor of %3 since one of the
  81. // dependencies (%4) of the propagated instruction (%5) does not dominate it.
  82. static std::unordered_set<uint32_t> GetAcceptableSuccessors(
  83. opt::IRContext* ir_context, uint32_t block_id);
  84. std::unordered_set<uint32_t> GetFreshIds() const override;
  85. private:
  86. // Returns the last possible instruction in the |block_id| that satisfies the
  87. // following properties:
  88. // - has result id
  89. // - has type id
  90. // - has supported opcode (see IsOpcodeSupported method)
  91. // - has no users in its basic block.
  92. // Returns nullptr if no such an instruction exists. For example:
  93. // %1 = OpLabel
  94. // %2 = OpUndef %int
  95. // %3 = OpUndef %int
  96. // OpStore %var %3
  97. // OpBranch %some_block
  98. // In this example:
  99. // - We cannot propagate OpBranch nor OpStore since they both have unsupported
  100. // opcodes and have neither result ids nor type ids.
  101. // - We cannot propagate %3 either since it is used by OpStore.
  102. // - We can propagate %2 since it satisfies all our conditions.
  103. // The basic idea behind this method it to make sure that the returned
  104. // instruction will not break domination rules in its original block when
  105. // propagated.
  106. static opt::Instruction* GetInstructionToPropagate(opt::IRContext* ir_context,
  107. uint32_t block_id);
  108. // Returns true if |opcode| is supported by this transformation.
  109. static bool IsOpcodeSupported(spv::Op opcode);
  110. // Returns the first instruction in the |block| that allows us to insert
  111. // |opcode| above itself. Returns nullptr is no such instruction exists.
  112. static opt::Instruction* GetFirstInsertBeforeInstruction(
  113. opt::IRContext* ir_context, uint32_t block_id, spv::Op opcode);
  114. // Returns a result id of a basic block, where an OpPhi instruction can be
  115. // inserted. Returns nullptr if it's not possible to create an OpPhi. The
  116. // created OpPhi instruction groups all the propagated clones of the original
  117. // instruction. |block_id| is a result id of the block we propagate the
  118. // instruction from. |successor_ids| contains result ids of the successors we
  119. // propagate the instruction into. Concretely, returns a non-null value if:
  120. // - |block_id| is in some construct.
  121. // - The merge block of that construct is reachable.
  122. // - |block_id| dominates that merge block.
  123. // - That merge block may not be an acceptable successor of |block_id|.
  124. // - There must be at least one |block_id|'s acceptable successor for every
  125. // predecessor of the merge block, dominating that predecessor.
  126. // - We can't create an OpPhi if the module has neither VariablePointers nor
  127. // VariablePointersStorageBuffer capabilities.
  128. // A simple example of when we can insert an OpPhi instruction is:
  129. // - This snippet of code:
  130. // %1 = OpLabel
  131. // %2 = OpUndef %int
  132. // OpSelectionMerge %5 None
  133. // OpBranchConditional %cond %3 %4
  134. // %3 = OpLabel
  135. // OpBranch %5
  136. // %4 = OpLabel
  137. // OpBranch %5
  138. // %5 = OpLabel
  139. // ...
  140. // will be transformed into the following one (if %2 is propagated):
  141. // %1 = OpLabel
  142. // OpSelectionMerge %5 None
  143. // OpBranchConditional %cond %3 %4
  144. // %3 = OpLabel
  145. // %6 = OpUndef %int
  146. // OpBranch %5
  147. // %4 = OpLabel
  148. // %7 = OpUndef %int
  149. // OpBranch %5
  150. // %5 = OpLabel
  151. // %8 = OpPhi %int %6 %3 %7 %4
  152. // ...
  153. // The fact that we introduce an OpPhi allows us to increase the applicability
  154. // of the transformation. Concretely, we wouldn't be able to apply it in the
  155. // example above if %2 were used in %5. Some more complicated examples can be
  156. // found in unit tests.
  157. static uint32_t GetOpPhiBlockId(
  158. opt::IRContext* ir_context, uint32_t block_id,
  159. const opt::Instruction& inst_to_propagate,
  160. const std::unordered_set<uint32_t>& successor_ids);
  161. protobufs::TransformationPropagateInstructionDown message_;
  162. };
  163. } // namespace fuzz
  164. } // namespace spvtools
  165. #endif // SOURCE_FUZZ_TRANSFORMATION_PROPAGATE_INSTRUCTION_DOWN_H_