fuzzer_pass_replace_linear_algebra_instructions.cpp 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  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. : FuzzerPass(ir_context, transformation_context, fuzzer_context,
  27. transformations) {}
  28. FuzzerPassReplaceLinearAlgebraInstructions::
  29. ~FuzzerPassReplaceLinearAlgebraInstructions() = default;
  30. void FuzzerPassReplaceLinearAlgebraInstructions::Apply() {
  31. // For each instruction, checks whether it is a supported linear algebra
  32. // instruction. In this case, the transformation is randomly applied.
  33. GetIRContext()->module()->ForEachInst([this](opt::Instruction* instruction) {
  34. // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3354):
  35. // Right now we only support certain operations. When this issue is
  36. // addressed the following conditional can use the function
  37. // |spvOpcodeIsLinearAlgebra|.
  38. if (instruction->opcode() != SpvOpVectorTimesScalar &&
  39. instruction->opcode() != SpvOpMatrixTimesScalar &&
  40. instruction->opcode() != SpvOpVectorTimesMatrix &&
  41. instruction->opcode() != SpvOpMatrixTimesVector &&
  42. instruction->opcode() != SpvOpDot) {
  43. return;
  44. }
  45. if (!GetFuzzerContext()->ChoosePercentage(
  46. GetFuzzerContext()
  47. ->GetChanceOfReplacingLinearAlgebraInstructions())) {
  48. return;
  49. }
  50. ApplyTransformation(TransformationReplaceLinearAlgebraInstruction(
  51. GetFuzzerContext()->GetFreshIds(
  52. TransformationReplaceLinearAlgebraInstruction::
  53. GetRequiredFreshIdCount(GetIRContext(), instruction)),
  54. MakeInstructionDescriptor(GetIRContext(), instruction)));
  55. });
  56. }
  57. } // namespace fuzz
  58. } // namespace spvtools