forked from KhronosGroup/SPIRV-Tools
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
spirv-fuzz: TransformationReplaceAddSubMulWithCarryingExtended (Khron…
…osGroup#3598) Replaces OpIAdd with OpIAddCarry, OpISub with OpISubBorrow, OpIMul with OpUMulExtended or OpSMulExtended and stores the result into a fresh_id representing a structure. Extracts the first element of the result into the original result_id. This value is the same as the result of the original instruction. Fixes KhronosGroup#3577
- Loading branch information
Antoni Karpiński
authored
Aug 6, 2020
1 parent
6d7f34f
commit 7b2dd11
Showing
11 changed files
with
1,070 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
76 changes: 76 additions & 0 deletions
76
source/fuzz/fuzzer_pass_replace_adds_subs_muls_with_carrying_extended.cpp
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,76 @@ | ||
// Copyright (c) 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#include "source/fuzz/fuzzer_pass_replace_adds_subs_muls_with_carrying_extended.h" | ||
|
||
#include "source/fuzz/fuzzer_util.h" | ||
#include "source/fuzz/transformation_replace_add_sub_mul_with_carrying_extended.h" | ||
|
||
namespace spvtools { | ||
namespace fuzz { | ||
|
||
namespace { | ||
const uint32_t kArithmeticInstructionIndexLeftInOperand = 0; | ||
} // namespace | ||
|
||
FuzzerPassReplaceAddsSubsMulsWithCarryingExtended:: | ||
FuzzerPassReplaceAddsSubsMulsWithCarryingExtended( | ||
opt::IRContext* ir_context, | ||
TransformationContext* transformation_context, | ||
FuzzerContext* fuzzer_context, | ||
protobufs::TransformationSequence* transformations) | ||
: FuzzerPass(ir_context, transformation_context, fuzzer_context, | ||
transformations) {} | ||
|
||
FuzzerPassReplaceAddsSubsMulsWithCarryingExtended:: | ||
~FuzzerPassReplaceAddsSubsMulsWithCarryingExtended() = default; | ||
|
||
void FuzzerPassReplaceAddsSubsMulsWithCarryingExtended::Apply() { | ||
for (auto& function : *GetIRContext()->module()) { | ||
for (auto& block : function) { | ||
for (auto& instruction : block) { | ||
// Randomly decide whether to apply the transformation. | ||
if (!GetFuzzerContext()->ChoosePercentage( | ||
GetFuzzerContext() | ||
->GetChanceOfReplacingAddSubMulWithCarryingExtended())) { | ||
continue; | ||
} | ||
|
||
// Check if the transformation can be applied to this instruction. | ||
if (!TransformationReplaceAddSubMulWithCarryingExtended:: | ||
IsInstructionSuitable(GetIRContext(), instruction)) { | ||
continue; | ||
} | ||
|
||
// Get the operand type id. We know that both operands have the same | ||
// type. | ||
uint32_t operand_type_id = | ||
GetIRContext() | ||
->get_def_use_mgr() | ||
->GetDef(instruction.GetSingleWordInOperand( | ||
kArithmeticInstructionIndexLeftInOperand)) | ||
->type_id(); | ||
|
||
// Ensure the required struct type exists. The struct type is based on | ||
// the operand type. | ||
FindOrCreateStructType({operand_type_id, operand_type_id}); | ||
|
||
ApplyTransformation(TransformationReplaceAddSubMulWithCarryingExtended( | ||
GetFuzzerContext()->GetFreshId(), instruction.result_id())); | ||
} | ||
} | ||
} | ||
} | ||
} // namespace fuzz | ||
} // namespace spvtools |
42 changes: 42 additions & 0 deletions
42
source/fuzz/fuzzer_pass_replace_adds_subs_muls_with_carrying_extended.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
// Copyright (c) 2020 Google LLC | ||
// | ||
// Licensed under the Apache License, Version 2.0 (the "License"); | ||
// you may not use this file except in compliance with the License. | ||
// You may obtain a copy of the License at | ||
// | ||
// http://www.apache.org/licenses/LICENSE-2.0 | ||
// | ||
// Unless required by applicable law or agreed to in writing, software | ||
// distributed under the License is distributed on an "AS IS" BASIS, | ||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
// See the License for the specific language governing permissions and | ||
// limitations under the License. | ||
|
||
#ifndef SPIRV_TOOLS_FUZZER_PASS_REPLACE_ADDS_SUBS_MULS_WITH_CARRYING_EXTENDED_H | ||
#define SPIRV_TOOLS_FUZZER_PASS_REPLACE_ADDS_SUBS_MULS_WITH_CARRYING_EXTENDED_H | ||
|
||
#include "source/fuzz/fuzzer_pass.h" | ||
|
||
namespace spvtools { | ||
namespace fuzz { | ||
|
||
// A fuzzer pass that replaces instructions OpIAdd, OpISub, OpIMul with pairs of | ||
// instructions. The first one (OpIAddCarry, OpISubBorrow, OpUMulExtended, | ||
// OpSMulExtended) computes the result into a struct. The second one extracts | ||
// the appropriate component from the struct to yield the original result. | ||
class FuzzerPassReplaceAddsSubsMulsWithCarryingExtended : public FuzzerPass { | ||
public: | ||
FuzzerPassReplaceAddsSubsMulsWithCarryingExtended( | ||
opt::IRContext* ir_context, TransformationContext* transformation_context, | ||
FuzzerContext* fuzzer_context, | ||
protobufs::TransformationSequence* transformations); | ||
|
||
~FuzzerPassReplaceAddsSubsMulsWithCarryingExtended() override; | ||
|
||
void Apply() override; | ||
}; | ||
|
||
} // namespace fuzz | ||
} // namespace spvtools | ||
|
||
#endif // SPIRV_TOOLS_FUZZER_PASS_REPLACE_ADDS_SUBS_MULS_WITH_CARRYING_EXTENDED_H |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.