fuzzer_pass_propagate_instructions_up.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) 2020 Vasyl Teliman
  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_propagate_instructions_up.h"
  15. #include "source/fuzz/fuzzer_context.h"
  16. #include "source/fuzz/fuzzer_util.h"
  17. #include "source/fuzz/instruction_descriptor.h"
  18. #include "source/fuzz/transformation_propagate_instruction_up.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. FuzzerPassPropagateInstructionsUp::FuzzerPassPropagateInstructionsUp(
  22. opt::IRContext* ir_context, TransformationContext* transformation_context,
  23. FuzzerContext* fuzzer_context,
  24. protobufs::TransformationSequence* transformations,
  25. bool ignore_inapplicable_transformations)
  26. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  27. transformations, ignore_inapplicable_transformations) {}
  28. void FuzzerPassPropagateInstructionsUp::Apply() {
  29. for (const auto& function : *GetIRContext()->module()) {
  30. for (const auto& block : function) {
  31. if (!GetFuzzerContext()->ChoosePercentage(
  32. GetFuzzerContext()->GetChanceOfPropagatingInstructionsUp())) {
  33. continue;
  34. }
  35. if (TransformationPropagateInstructionUp::IsApplicableToBlock(
  36. GetIRContext(), block.id())) {
  37. std::map<uint32_t, uint32_t> fresh_ids;
  38. for (auto id : GetIRContext()->cfg()->preds(block.id())) {
  39. auto& fresh_id = fresh_ids[id];
  40. if (!fresh_id) {
  41. // Create a fresh id if it hasn't been created yet. |fresh_id| will
  42. // be default-initialized to 0 in this case.
  43. fresh_id = GetFuzzerContext()->GetFreshId();
  44. }
  45. }
  46. ApplyTransformation(
  47. TransformationPropagateInstructionUp(block.id(), fresh_ids));
  48. }
  49. }
  50. }
  51. }
  52. } // namespace fuzz
  53. } // namespace spvtools