From 7dfd9b8680f4aa1df6b0e33d929c28cdc9e590ab Mon Sep 17 00:00:00 2001 From: Stefano Milizia Date: Fri, 17 Jul 2020 12:01:35 +0000 Subject: [PATCH] spirv-fuzz: Implement MaybeApplyTransformation helper function (#3540) This function can be used to apply a transformation only if it is applicable and use it wherever this pattern is used. Fixes #3530. --- source/fuzz/fuzzer_pass.h | 11 +++++++++++ source/fuzz/fuzzer_pass_add_dead_blocks.cpp | 6 +----- source/fuzz/fuzzer_pass_add_dead_breaks.cpp | 7 ++----- source/fuzz/fuzzer_pass_add_dead_continues.cpp | 9 ++------- .../fuzzer_pass_interchange_zero_like_constants.cpp | 9 ++------- source/fuzz/fuzzer_pass_merge_blocks.cpp | 6 +----- source/fuzz/fuzzer_pass_outline_functions.cpp | 6 +----- source/fuzz/fuzzer_pass_split_blocks.cpp | 6 +----- 8 files changed, 21 insertions(+), 39 deletions(-) diff --git a/source/fuzz/fuzzer_pass.h b/source/fuzz/fuzzer_pass.h index 4d905b5ea..f8acff24c 100644 --- a/source/fuzz/fuzzer_pass.h +++ b/source/fuzz/fuzzer_pass.h @@ -103,6 +103,17 @@ class FuzzerPass { *GetTransformations()->add_transformation() = transformation.ToMessage(); } + // A generic helper for applying a transformation only if it is applicable. + // If it is applicable, the transformation is applied and then added to the + // sequence of applied transformations. Otherwise, nothing happens. + void MaybeApplyTransformation(const Transformation& transformation) { + if (transformation.IsApplicable(GetIRContext(), + *GetTransformationContext())) { + transformation.Apply(GetIRContext(), GetTransformationContext()); + *GetTransformations()->add_transformation() = transformation.ToMessage(); + } + } + // Returns the id of an OpTypeBool instruction. If such an instruction does // not exist, a transformation is applied to add it. uint32_t FindOrCreateBoolType(); diff --git a/source/fuzz/fuzzer_pass_add_dead_blocks.cpp b/source/fuzz/fuzzer_pass_add_dead_blocks.cpp index 30a414544..56d33e56f 100644 --- a/source/fuzz/fuzzer_pass_add_dead_blocks.cpp +++ b/source/fuzz/fuzzer_pass_add_dead_blocks.cpp @@ -59,11 +59,7 @@ void FuzzerPassAddDeadBlocks::Apply() { } // Apply all those transformations that are in fact applicable. for (auto& transformation : candidate_transformations) { - if (transformation.IsApplicable(GetIRContext(), - *GetTransformationContext())) { - transformation.Apply(GetIRContext(), GetTransformationContext()); - *GetTransformations()->add_transformation() = transformation.ToMessage(); - } + MaybeApplyTransformation(transformation); } } diff --git a/source/fuzz/fuzzer_pass_add_dead_breaks.cpp b/source/fuzz/fuzzer_pass_add_dead_breaks.cpp index f3900aa69..120661670 100644 --- a/source/fuzz/fuzzer_pass_add_dead_breaks.cpp +++ b/source/fuzz/fuzzer_pass_add_dead_breaks.cpp @@ -114,12 +114,9 @@ void FuzzerPassAddDeadBreaks::Apply() { candidate_transformations.erase(candidate_transformations.begin() + index); // Probabilistically decide whether to try to apply it vs. ignore it, in the // case that it is applicable. - if (transformation.IsApplicable(GetIRContext(), - *GetTransformationContext()) && - GetFuzzerContext()->ChoosePercentage( + if (GetFuzzerContext()->ChoosePercentage( GetFuzzerContext()->GetChanceOfAddingDeadBreak())) { - transformation.Apply(GetIRContext(), GetTransformationContext()); - *GetTransformations()->add_transformation() = transformation.ToMessage(); + MaybeApplyTransformation(transformation); } } } diff --git a/source/fuzz/fuzzer_pass_add_dead_continues.cpp b/source/fuzz/fuzzer_pass_add_dead_continues.cpp index 56a7fd17c..a383c2ebd 100644 --- a/source/fuzz/fuzzer_pass_add_dead_continues.cpp +++ b/source/fuzz/fuzzer_pass_add_dead_continues.cpp @@ -80,14 +80,9 @@ void FuzzerPassAddDeadContinues::Apply() { block.id(), condition_value, std::move(phi_ids)); // Probabilistically decide whether to apply the transformation in the // case that it is applicable. - if (candidate_transformation.IsApplicable(GetIRContext(), - *GetTransformationContext()) && - GetFuzzerContext()->ChoosePercentage( + if (GetFuzzerContext()->ChoosePercentage( GetFuzzerContext()->GetChanceOfAddingDeadContinue())) { - candidate_transformation.Apply(GetIRContext(), - GetTransformationContext()); - *GetTransformations()->add_transformation() = - candidate_transformation.ToMessage(); + MaybeApplyTransformation(candidate_transformation); } } } diff --git a/source/fuzz/fuzzer_pass_interchange_zero_like_constants.cpp b/source/fuzz/fuzzer_pass_interchange_zero_like_constants.cpp index ebf0ef445..201c6ffc0 100644 --- a/source/fuzz/fuzzer_pass_interchange_zero_like_constants.cpp +++ b/source/fuzz/fuzzer_pass_interchange_zero_like_constants.cpp @@ -111,13 +111,8 @@ void FuzzerPassInterchangeZeroLikeConstants::Apply() { // Replace the ids for (auto use_to_replace : uses_to_replace) { - auto transformation = TransformationReplaceIdWithSynonym( - use_to_replace.first, use_to_replace.second); - if (transformation.IsApplicable(GetIRContext(), - *GetTransformationContext())) { - transformation.Apply(GetIRContext(), GetTransformationContext()); - *GetTransformations()->add_transformation() = transformation.ToMessage(); - } + MaybeApplyTransformation(TransformationReplaceIdWithSynonym( + use_to_replace.first, use_to_replace.second)); } } } // namespace fuzz diff --git a/source/fuzz/fuzzer_pass_merge_blocks.cpp b/source/fuzz/fuzzer_pass_merge_blocks.cpp index 49778aea5..ac2a22f87 100644 --- a/source/fuzz/fuzzer_pass_merge_blocks.cpp +++ b/source/fuzz/fuzzer_pass_merge_blocks.cpp @@ -56,11 +56,7 @@ void FuzzerPassMergeBlocks::Apply() { uint32_t index = GetFuzzerContext()->RandomIndex(potential_transformations); auto transformation = potential_transformations.at(index); potential_transformations.erase(potential_transformations.begin() + index); - if (transformation.IsApplicable(GetIRContext(), - *GetTransformationContext())) { - transformation.Apply(GetIRContext(), GetTransformationContext()); - *GetTransformations()->add_transformation() = transformation.ToMessage(); - } + MaybeApplyTransformation(transformation); } } diff --git a/source/fuzz/fuzzer_pass_outline_functions.cpp b/source/fuzz/fuzzer_pass_outline_functions.cpp index 1665d050a..e4281d107 100644 --- a/source/fuzz/fuzzer_pass_outline_functions.cpp +++ b/source/fuzz/fuzzer_pass_outline_functions.cpp @@ -89,11 +89,7 @@ void FuzzerPassOutlineFunctions::Apply() { /*new_callee_result_id*/ GetFuzzerContext()->GetFreshId(), /*input_id_to_fresh_id*/ std::move(input_id_to_fresh_id), /*output_id_to_fresh_id*/ std::move(output_id_to_fresh_id)); - if (transformation.IsApplicable(GetIRContext(), - *GetTransformationContext())) { - transformation.Apply(GetIRContext(), GetTransformationContext()); - *GetTransformations()->add_transformation() = transformation.ToMessage(); - } + MaybeApplyTransformation(transformation); } } diff --git a/source/fuzz/fuzzer_pass_split_blocks.cpp b/source/fuzz/fuzzer_pass_split_blocks.cpp index 15c67901e..481cd9607 100644 --- a/source/fuzz/fuzzer_pass_split_blocks.cpp +++ b/source/fuzz/fuzzer_pass_split_blocks.cpp @@ -96,11 +96,7 @@ void FuzzerPassSplitBlocks::Apply() { // If the position we have chosen turns out to be a valid place to split // the block, we apply the split. Otherwise the block just doesn't get // split. - if (transformation.IsApplicable(GetIRContext(), - *GetTransformationContext())) { - transformation.Apply(GetIRContext(), GetTransformationContext()); - *GetTransformations()->add_transformation() = transformation.ToMessage(); - } + MaybeApplyTransformation(transformation); } }