fuzzer_pass_replace_linear_algebra_instructions.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061
  1. // Copyright (c) 2020 André Perez Maselco
  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_replace_linear_algebra_instructions.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. #include "source/fuzz/transformation_replace_linear_algebra_instruction.h"
  18. namespace spvtools {
  19. namespace fuzz {
  20. FuzzerPassReplaceLinearAlgebraInstructions::
  21. FuzzerPassReplaceLinearAlgebraInstructions(
  22. opt::IRContext* ir_context,
  23. TransformationContext* transformation_context,
  24. FuzzerContext* fuzzer_context,
  25. protobufs::TransformationSequence* transformations,
  26. bool ignore_inapplicable_transformations)
  27. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  28. transformations, ignore_inapplicable_transformations) {}
  29. void FuzzerPassReplaceLinearAlgebraInstructions::Apply() {
  30. // For each instruction, checks whether it is a linear algebra instruction. In
  31. // this case, the transformation is randomly applied.
  32. for (auto& function : *GetIRContext()->module()) {
  33. for (auto& block : function) {
  34. for (auto& instruction : block) {
  35. if (!spvOpcodeIsLinearAlgebra(instruction.opcode())) {
  36. continue;
  37. }
  38. if (!GetFuzzerContext()->ChoosePercentage(
  39. GetFuzzerContext()
  40. ->GetChanceOfReplacingLinearAlgebraInstructions())) {
  41. continue;
  42. }
  43. ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(
  44. GetFuzzerContext()->GetFreshIds(
  45. TransformationReplaceLinearAlgebraInstruction::
  46. GetRequiredFreshIdCount(GetIRContext(), &instruction)),
  47. MakeInstructionDescriptor(GetIRContext(), &instruction)));
  48. }
  49. }
  50. }
  51. }
  52. } // namespace fuzz
  53. } // namespace spvtools