fuzzer_pass_add_loop_preheaders.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. // Copyright (c) 2020 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_add_loop_preheaders.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/transformation_add_loop_preheader.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. FuzzerPassAddLoopPreheaders::FuzzerPassAddLoopPreheaders(
  20. opt::IRContext* ir_context, TransformationContext* transformation_context,
  21. FuzzerContext* fuzzer_context,
  22. protobufs::TransformationSequence* transformations,
  23. bool ignore_inapplicable_transformations)
  24. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  25. transformations, ignore_inapplicable_transformations) {}
  26. void FuzzerPassAddLoopPreheaders::Apply() {
  27. for (auto& function : *GetIRContext()->module()) {
  28. // Keep track of all the loop headers we want to add a preheader to.
  29. std::vector<uint32_t> loop_header_ids_to_consider;
  30. for (auto& block : function) {
  31. // We only care about loop headers.
  32. if (!block.IsLoopHeader()) {
  33. continue;
  34. }
  35. // Randomly decide whether to consider this header.
  36. if (!GetFuzzerContext()->ChoosePercentage(
  37. GetFuzzerContext()->GetChanceOfAddingLoopPreheader())) {
  38. continue;
  39. }
  40. // We exclude loop headers with just one predecessor (the back-edge block)
  41. // because they are unreachable.
  42. if (GetIRContext()->cfg()->preds(block.id()).size() < 2) {
  43. continue;
  44. }
  45. loop_header_ids_to_consider.push_back(block.id());
  46. }
  47. for (uint32_t header_id : loop_header_ids_to_consider) {
  48. // If not already present, add a preheader which is not also a loop
  49. // header.
  50. GetOrCreateSimpleLoopPreheader(header_id);
  51. }
  52. }
  53. }
  54. } // namespace fuzz
  55. } // namespace spvtools