mirror of
https://github.com/KhronosGroup/SPIRV-Tools
synced 2025-01-11 17:10:06 +00:00
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.
This commit is contained in:
parent
de1ff50f28
commit
7dfd9b8680
@ -103,6 +103,17 @@ class FuzzerPass {
|
|||||||
*GetTransformations()->add_transformation() = transformation.ToMessage();
|
*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
|
// Returns the id of an OpTypeBool instruction. If such an instruction does
|
||||||
// not exist, a transformation is applied to add it.
|
// not exist, a transformation is applied to add it.
|
||||||
uint32_t FindOrCreateBoolType();
|
uint32_t FindOrCreateBoolType();
|
||||||
|
@ -59,11 +59,7 @@ void FuzzerPassAddDeadBlocks::Apply() {
|
|||||||
}
|
}
|
||||||
// Apply all those transformations that are in fact applicable.
|
// Apply all those transformations that are in fact applicable.
|
||||||
for (auto& transformation : candidate_transformations) {
|
for (auto& transformation : candidate_transformations) {
|
||||||
if (transformation.IsApplicable(GetIRContext(),
|
MaybeApplyTransformation(transformation);
|
||||||
*GetTransformationContext())) {
|
|
||||||
transformation.Apply(GetIRContext(), GetTransformationContext());
|
|
||||||
*GetTransformations()->add_transformation() = transformation.ToMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,12 +114,9 @@ void FuzzerPassAddDeadBreaks::Apply() {
|
|||||||
candidate_transformations.erase(candidate_transformations.begin() + index);
|
candidate_transformations.erase(candidate_transformations.begin() + index);
|
||||||
// Probabilistically decide whether to try to apply it vs. ignore it, in the
|
// Probabilistically decide whether to try to apply it vs. ignore it, in the
|
||||||
// case that it is applicable.
|
// case that it is applicable.
|
||||||
if (transformation.IsApplicable(GetIRContext(),
|
if (GetFuzzerContext()->ChoosePercentage(
|
||||||
*GetTransformationContext()) &&
|
|
||||||
GetFuzzerContext()->ChoosePercentage(
|
|
||||||
GetFuzzerContext()->GetChanceOfAddingDeadBreak())) {
|
GetFuzzerContext()->GetChanceOfAddingDeadBreak())) {
|
||||||
transformation.Apply(GetIRContext(), GetTransformationContext());
|
MaybeApplyTransformation(transformation);
|
||||||
*GetTransformations()->add_transformation() = transformation.ToMessage();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -80,14 +80,9 @@ void FuzzerPassAddDeadContinues::Apply() {
|
|||||||
block.id(), condition_value, std::move(phi_ids));
|
block.id(), condition_value, std::move(phi_ids));
|
||||||
// Probabilistically decide whether to apply the transformation in the
|
// Probabilistically decide whether to apply the transformation in the
|
||||||
// case that it is applicable.
|
// case that it is applicable.
|
||||||
if (candidate_transformation.IsApplicable(GetIRContext(),
|
if (GetFuzzerContext()->ChoosePercentage(
|
||||||
*GetTransformationContext()) &&
|
|
||||||
GetFuzzerContext()->ChoosePercentage(
|
|
||||||
GetFuzzerContext()->GetChanceOfAddingDeadContinue())) {
|
GetFuzzerContext()->GetChanceOfAddingDeadContinue())) {
|
||||||
candidate_transformation.Apply(GetIRContext(),
|
MaybeApplyTransformation(candidate_transformation);
|
||||||
GetTransformationContext());
|
|
||||||
*GetTransformations()->add_transformation() =
|
|
||||||
candidate_transformation.ToMessage();
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -111,13 +111,8 @@ void FuzzerPassInterchangeZeroLikeConstants::Apply() {
|
|||||||
|
|
||||||
// Replace the ids
|
// Replace the ids
|
||||||
for (auto use_to_replace : uses_to_replace) {
|
for (auto use_to_replace : uses_to_replace) {
|
||||||
auto transformation = TransformationReplaceIdWithSynonym(
|
MaybeApplyTransformation(TransformationReplaceIdWithSynonym(
|
||||||
use_to_replace.first, use_to_replace.second);
|
use_to_replace.first, use_to_replace.second));
|
||||||
if (transformation.IsApplicable(GetIRContext(),
|
|
||||||
*GetTransformationContext())) {
|
|
||||||
transformation.Apply(GetIRContext(), GetTransformationContext());
|
|
||||||
*GetTransformations()->add_transformation() = transformation.ToMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} // namespace fuzz
|
} // namespace fuzz
|
||||||
|
@ -56,11 +56,7 @@ void FuzzerPassMergeBlocks::Apply() {
|
|||||||
uint32_t index = GetFuzzerContext()->RandomIndex(potential_transformations);
|
uint32_t index = GetFuzzerContext()->RandomIndex(potential_transformations);
|
||||||
auto transformation = potential_transformations.at(index);
|
auto transformation = potential_transformations.at(index);
|
||||||
potential_transformations.erase(potential_transformations.begin() + index);
|
potential_transformations.erase(potential_transformations.begin() + index);
|
||||||
if (transformation.IsApplicable(GetIRContext(),
|
MaybeApplyTransformation(transformation);
|
||||||
*GetTransformationContext())) {
|
|
||||||
transformation.Apply(GetIRContext(), GetTransformationContext());
|
|
||||||
*GetTransformations()->add_transformation() = transformation.ToMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -89,11 +89,7 @@ void FuzzerPassOutlineFunctions::Apply() {
|
|||||||
/*new_callee_result_id*/ GetFuzzerContext()->GetFreshId(),
|
/*new_callee_result_id*/ GetFuzzerContext()->GetFreshId(),
|
||||||
/*input_id_to_fresh_id*/ std::move(input_id_to_fresh_id),
|
/*input_id_to_fresh_id*/ std::move(input_id_to_fresh_id),
|
||||||
/*output_id_to_fresh_id*/ std::move(output_id_to_fresh_id));
|
/*output_id_to_fresh_id*/ std::move(output_id_to_fresh_id));
|
||||||
if (transformation.IsApplicable(GetIRContext(),
|
MaybeApplyTransformation(transformation);
|
||||||
*GetTransformationContext())) {
|
|
||||||
transformation.Apply(GetIRContext(), GetTransformationContext());
|
|
||||||
*GetTransformations()->add_transformation() = transformation.ToMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -96,11 +96,7 @@ void FuzzerPassSplitBlocks::Apply() {
|
|||||||
// If the position we have chosen turns out to be a valid place to split
|
// 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
|
// the block, we apply the split. Otherwise the block just doesn't get
|
||||||
// split.
|
// split.
|
||||||
if (transformation.IsApplicable(GetIRContext(),
|
MaybeApplyTransformation(transformation);
|
||||||
*GetTransformationContext())) {
|
|
||||||
transformation.Apply(GetIRContext(), GetTransformationContext());
|
|
||||||
*GetTransformations()->add_transformation() = transformation.ToMessage();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user