transformation_set_loop_control.cpp 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  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. #include "source/fuzz/transformation_set_loop_control.h"
  15. namespace spvtools {
  16. namespace fuzz {
  17. TransformationSetLoopControl::TransformationSetLoopControl(
  18. const spvtools::fuzz::protobufs::TransformationSetLoopControl& message)
  19. : message_(message) {}
  20. TransformationSetLoopControl::TransformationSetLoopControl(
  21. uint32_t block_id, uint32_t loop_control, uint32_t peel_count,
  22. uint32_t partial_count) {
  23. message_.set_block_id(block_id);
  24. message_.set_loop_control(loop_control);
  25. message_.set_peel_count(peel_count);
  26. message_.set_partial_count(partial_count);
  27. }
  28. bool TransformationSetLoopControl::IsApplicable(
  29. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  30. // |message_.block_id| must identify a block that ends with OpLoopMerge.
  31. auto block = ir_context->get_instr_block(message_.block_id());
  32. if (!block) {
  33. return false;
  34. }
  35. auto merge_inst = block->GetMergeInst();
  36. if (!merge_inst || merge_inst->opcode() != SpvOpLoopMerge) {
  37. return false;
  38. }
  39. // We sanity-check that the transformation does not try to set any meaningless
  40. // bits of the loop control mask.
  41. uint32_t all_loop_control_mask_bits_set =
  42. SpvLoopControlUnrollMask | SpvLoopControlDontUnrollMask |
  43. SpvLoopControlDependencyInfiniteMask |
  44. SpvLoopControlDependencyLengthMask | SpvLoopControlMinIterationsMask |
  45. SpvLoopControlMaxIterationsMask | SpvLoopControlIterationMultipleMask |
  46. SpvLoopControlPeelCountMask | SpvLoopControlPartialCountMask;
  47. // The variable is only used in an assertion; the following keeps release-mode
  48. // compilers happy.
  49. (void)(all_loop_control_mask_bits_set);
  50. // No additional bits should be set.
  51. assert(!(message_.loop_control() & ~all_loop_control_mask_bits_set));
  52. // Grab the loop control mask currently associated with the OpLoopMerge
  53. // instruction.
  54. auto existing_loop_control_mask =
  55. merge_inst->GetSingleWordInOperand(kLoopControlMaskInOperandIndex);
  56. // Check that there is no attempt to set one of the loop controls that
  57. // requires guarantees to hold.
  58. for (SpvLoopControlMask mask :
  59. {SpvLoopControlDependencyInfiniteMask,
  60. SpvLoopControlDependencyLengthMask, SpvLoopControlMinIterationsMask,
  61. SpvLoopControlMaxIterationsMask, SpvLoopControlIterationMultipleMask}) {
  62. // We have a problem if this loop control bit was not set in the original
  63. // loop control mask but is set by the transformation.
  64. if (LoopControlBitIsAddedByTransformation(mask,
  65. existing_loop_control_mask)) {
  66. return false;
  67. }
  68. }
  69. if ((message_.loop_control() &
  70. (SpvLoopControlPeelCountMask | SpvLoopControlPartialCountMask)) &&
  71. !(PeelCountIsSupported(ir_context) &&
  72. PartialCountIsSupported(ir_context))) {
  73. // At least one of PeelCount or PartialCount is used, but the SPIR-V version
  74. // in question does not support these loop controls.
  75. return false;
  76. }
  77. if (message_.peel_count() > 0 &&
  78. !(message_.loop_control() & SpvLoopControlPeelCountMask)) {
  79. // Peel count provided, but peel count mask bit not set.
  80. return false;
  81. }
  82. if (message_.partial_count() > 0 &&
  83. !(message_.loop_control() & SpvLoopControlPartialCountMask)) {
  84. // Partial count provided, but partial count mask bit not set.
  85. return false;
  86. }
  87. // We must not set both 'don't unroll' and one of 'peel count' or 'partial
  88. // count'.
  89. return !((message_.loop_control() & SpvLoopControlDontUnrollMask) &&
  90. (message_.loop_control() &
  91. (SpvLoopControlPeelCountMask | SpvLoopControlPartialCountMask)));
  92. }
  93. void TransformationSetLoopControl::Apply(
  94. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  95. // Grab the loop merge instruction and its associated loop control mask.
  96. auto merge_inst =
  97. ir_context->get_instr_block(message_.block_id())->GetMergeInst();
  98. auto existing_loop_control_mask =
  99. merge_inst->GetSingleWordInOperand(kLoopControlMaskInOperandIndex);
  100. // We are going to replace the OpLoopMerge's operands with this list.
  101. opt::Instruction::OperandList new_operands;
  102. // We add the existing merge block and continue target ids.
  103. new_operands.push_back(merge_inst->GetInOperand(0));
  104. new_operands.push_back(merge_inst->GetInOperand(1));
  105. // We use the loop control mask from the transformation.
  106. new_operands.push_back(
  107. {SPV_OPERAND_TYPE_LOOP_CONTROL, {message_.loop_control()}});
  108. // It remains to determine what literals to provide, in association with
  109. // the new loop control mask.
  110. //
  111. // For the loop controls that require guarantees to hold about the number
  112. // of loop iterations, we need to keep, from the original OpLoopMerge, any
  113. // literals associated with loop control bits that are still set.
  114. uint32_t literal_index = 0; // Indexes into the literals from the original
  115. // instruction.
  116. for (SpvLoopControlMask mask :
  117. {SpvLoopControlDependencyLengthMask, SpvLoopControlMinIterationsMask,
  118. SpvLoopControlMaxIterationsMask, SpvLoopControlIterationMultipleMask}) {
  119. // Check whether the bit was set in the original loop control mask.
  120. if (existing_loop_control_mask & mask) {
  121. // Check whether the bit is set in the new loop control mask.
  122. if (message_.loop_control() & mask) {
  123. // Add the associated literal to our sequence of replacement operands.
  124. new_operands.push_back(
  125. {SPV_OPERAND_TYPE_LITERAL_INTEGER,
  126. {merge_inst->GetSingleWordInOperand(
  127. kLoopControlFirstLiteralInOperandIndex + literal_index)}});
  128. }
  129. // Increment our index into the original loop control mask's literals,
  130. // whether or not the bit was set in the new mask.
  131. literal_index++;
  132. }
  133. }
  134. // If PeelCount is set in the new mask, |message_.peel_count| provides the
  135. // associated peel count.
  136. if (message_.loop_control() & SpvLoopControlPeelCountMask) {
  137. new_operands.push_back(
  138. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {message_.peel_count()}});
  139. }
  140. // Similar, but for PartialCount.
  141. if (message_.loop_control() & SpvLoopControlPartialCountMask) {
  142. new_operands.push_back(
  143. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {message_.partial_count()}});
  144. }
  145. // Replace the input operands of the OpLoopMerge with the new operands we have
  146. // accumulated.
  147. merge_inst->SetInOperands(std::move(new_operands));
  148. }
  149. protobufs::Transformation TransformationSetLoopControl::ToMessage() const {
  150. protobufs::Transformation result;
  151. *result.mutable_set_loop_control() = message_;
  152. return result;
  153. }
  154. bool TransformationSetLoopControl::LoopControlBitIsAddedByTransformation(
  155. SpvLoopControlMask loop_control_single_bit_mask,
  156. uint32_t existing_loop_control_mask) const {
  157. return !(loop_control_single_bit_mask & existing_loop_control_mask) &&
  158. (loop_control_single_bit_mask & message_.loop_control());
  159. }
  160. bool TransformationSetLoopControl::PartialCountIsSupported(
  161. opt::IRContext* ir_context) {
  162. // TODO(afd): We capture the universal environments for which this loop
  163. // control is definitely not supported. The check should be refined on
  164. // demand for other target environments.
  165. switch (ir_context->grammar().target_env()) {
  166. case SPV_ENV_UNIVERSAL_1_0:
  167. case SPV_ENV_UNIVERSAL_1_1:
  168. case SPV_ENV_UNIVERSAL_1_2:
  169. case SPV_ENV_UNIVERSAL_1_3:
  170. return false;
  171. default:
  172. return true;
  173. }
  174. }
  175. bool TransformationSetLoopControl::PeelCountIsSupported(
  176. opt::IRContext* ir_context) {
  177. // TODO(afd): We capture the universal environments for which this loop
  178. // control is definitely not supported. The check should be refined on
  179. // demand for other target environments.
  180. switch (ir_context->grammar().target_env()) {
  181. case SPV_ENV_UNIVERSAL_1_0:
  182. case SPV_ENV_UNIVERSAL_1_1:
  183. case SPV_ENV_UNIVERSAL_1_2:
  184. case SPV_ENV_UNIVERSAL_1_3:
  185. return false;
  186. default:
  187. return true;
  188. }
  189. }
  190. } // namespace fuzz
  191. } // namespace spvtools