fuzzer_pass_adjust_loop_controls.cpp 5.2 KB

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