fuzzer_pass_merge_blocks.cpp 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  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_merge_blocks.h"
  15. #include <vector>
  16. #include "source/fuzz/transformation_merge_blocks.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. FuzzerPassMergeBlocks::FuzzerPassMergeBlocks(
  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 FuzzerPassMergeBlocks::Apply() {
  27. // First we populate a sequence of transformations that we might consider
  28. // applying.
  29. std::vector<TransformationMergeBlocks> potential_transformations;
  30. // We do this by considering every block of every function.
  31. for (auto& function : *GetIRContext()->module()) {
  32. for (auto& block : function) {
  33. // We probabilistically decide to ignore some blocks.
  34. if (!GetFuzzerContext()->ChoosePercentage(
  35. GetFuzzerContext()->GetChanceOfMergingBlocks())) {
  36. continue;
  37. }
  38. // For other blocks, we add a transformation to merge the block into its
  39. // predecessor if that transformation would be applicable.
  40. TransformationMergeBlocks transformation(block.id());
  41. if (transformation.IsApplicable(GetIRContext(),
  42. *GetTransformationContext())) {
  43. potential_transformations.push_back(transformation);
  44. }
  45. }
  46. }
  47. while (!potential_transformations.empty()) {
  48. auto transformation =
  49. GetFuzzerContext()->RemoveAtRandomIndex(&potential_transformations);
  50. MaybeApplyTransformation(transformation);
  51. }
  52. }
  53. } // namespace fuzz
  54. } // namespace spvtools