1 // Copyright (c) 2020 Google LLC
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
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
15 #include "source/fuzz/fuzzer_pass_add_relaxed_decorations.h"
17 #include "source/fuzz/transformation_add_relaxed_decoration.h"
22 FuzzerPassAddRelaxedDecorations::FuzzerPassAddRelaxedDecorations(
23 opt::IRContext
* ir_context
, 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
) {}
30 void FuzzerPassAddRelaxedDecorations::Apply() {
31 // Consider every instruction in every block in every function.
32 for (auto& function
: *GetIRContext()->module()) {
33 for (auto& block
: function
) {
34 for (auto& inst
: block
) {
35 // Randomly choose whether to apply the RelaxedPrecision decoration
36 // to this instruction.
37 if (GetFuzzerContext()->ChoosePercentage(
38 GetFuzzerContext()->GetChanceOfAddingRelaxedDecoration())) {
39 TransformationAddRelaxedDecoration
transformation(inst
.result_id());
40 // Restrict attention to numeric instructions (returning 32-bit
41 // floats or ints according to SPIR-V documentation) in dead blocks.
42 if (transformation
.IsApplicable(GetIRContext(),
43 *GetTransformationContext())) {
44 transformation
.Apply(GetIRContext(), GetTransformationContext());
45 *GetTransformations()->add_transformation() =
46 transformation
.ToMessage();
54 } // namespace spvtools