fuzzer_pass_adjust_loop_controls.cpp 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/fuzzer_pass_adjust_loop_controls.h"
  15. #include "source/fuzz/transformation_set_loop_control.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. FuzzerPassAdjustLoopControls::FuzzerPassAdjustLoopControls(
  19. opt::IRContext* ir_context, FactManager* fact_manager,
  20. FuzzerContext* fuzzer_context,
  21. protobufs::TransformationSequence* transformations)
  22. : FuzzerPass(ir_context, fact_manager, fuzzer_context, transformations) {}
  23. FuzzerPassAdjustLoopControls::~FuzzerPassAdjustLoopControls() = default;
  24. void FuzzerPassAdjustLoopControls::Apply() {
  25. // Consider every merge instruction in the module (via looking through all
  26. // functions and blocks).
  27. for (auto& function : *GetIRContext()->module()) {
  28. for (auto& block : function) {
  29. if (auto merge_inst = block.GetMergeInst()) {
  30. // Ignore the instruction if it is not a loop merge.
  31. if (merge_inst->opcode() != SpvOpLoopMerge) {
  32. continue;
  33. }
  34. // Decide randomly whether to adjust this loop merge.
  35. if (!GetFuzzerContext()->ChoosePercentage(
  36. GetFuzzerContext()->GetChanceOfAdjustingLoopControl())) {
  37. continue;
  38. }
  39. uint32_t existing_mask = merge_inst->GetSingleWordOperand(
  40. TransformationSetLoopControl::kLoopControlMaskInOperandIndex);
  41. // First, set the new mask to one of None, Unroll or DontUnroll.
  42. std::vector<uint32_t> basic_masks = {SpvLoopControlMaskNone,
  43. SpvLoopControlUnrollMask,
  44. SpvLoopControlDontUnrollMask};
  45. uint32_t new_mask =
  46. basic_masks[GetFuzzerContext()->RandomIndex(basic_masks)];
  47. // For the loop controls that depend on guarantees about what the loop
  48. // does, check which of these were present in the existing mask and
  49. // randomly decide whether to keep them. They are just hints, so
  50. // removing them should not change the semantics of the module.
  51. for (auto mask_bit :
  52. {SpvLoopControlDependencyInfiniteMask,
  53. SpvLoopControlDependencyLengthMask,
  54. SpvLoopControlMinIterationsMask, SpvLoopControlMaxIterationsMask,
  55. SpvLoopControlIterationMultipleMask}) {
  56. if ((existing_mask & mask_bit) && GetFuzzerContext()->ChooseEven()) {
  57. // The mask bits we are considering are not available in all SPIR-V
  58. // versions. However, we only include a mask bit if it was present
  59. // in the original loop control mask, and we work under the
  60. // assumption that we are transforming a valid module, thus we don't
  61. // need to actually check whether the SPIR-V version being used
  62. // supports these loop control mask bits.
  63. new_mask |= mask_bit;
  64. }
  65. }
  66. // We use 0 for peel count and partial count in the case that we choose
  67. // not to set these controls.
  68. uint32_t peel_count = 0;
  69. uint32_t partial_count = 0;
  70. // PeelCount and PartialCount are not compatible with DontUnroll, so
  71. // we check whether DontUnroll is set.
  72. if (!(new_mask & SpvLoopControlDontUnrollMask)) {
  73. // If PeelCount is supported by this SPIR-V version, randomly choose
  74. // whether to set it. If it was set in the original mask and is not
  75. // selected for setting here, that amounts to dropping it.
  76. if (TransformationSetLoopControl::PeelCountIsSupported(
  77. GetIRContext()) &&
  78. GetFuzzerContext()->ChooseEven()) {
  79. new_mask |= SpvLoopControlPeelCountMask;
  80. // The peel count is chosen randomly - if PeelCount was already set
  81. // this will overwrite whatever peel count was previously used.
  82. peel_count = GetFuzzerContext()->GetRandomLoopControlPeelCount();
  83. }
  84. // Similar, but for PartialCount.
  85. if (TransformationSetLoopControl::PartialCountIsSupported(
  86. GetIRContext()) &&
  87. GetFuzzerContext()->ChooseEven()) {
  88. new_mask |= SpvLoopControlPartialCountMask;
  89. partial_count =
  90. GetFuzzerContext()->GetRandomLoopControlPartialCount();
  91. }
  92. }
  93. // Apply the transformation and add it to the output transformation
  94. // sequence.
  95. TransformationSetLoopControl transformation(block.id(), new_mask,
  96. peel_count, partial_count);
  97. assert(transformation.IsApplicable(GetIRContext(), *GetFactManager()) &&
  98. "Transformation should be applicable by construction.");
  99. transformation.Apply(GetIRContext(), GetFactManager());
  100. *GetTransformations()->add_transformation() =
  101. transformation.ToMessage();
  102. }
  103. }
  104. }
  105. }
  106. } // namespace fuzz
  107. } // namespace spvtools