Debian Codegraph @

third_party/spirv-tools/source/fuzz/fuzzer_pass_interchange_zero_like_constants.cpp

raw
1 // Copyright (c) 2020 Stefano Milizia
2 // Copyright (c) 2020 Google LLC
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include "source/fuzz/fuzzer_pass_interchange_zero_like_constants.h"
17
18 #include "source/fuzz/fuzzer_util.h"
19 #include "source/fuzz/id_use_descriptor.h"
20 #include "source/fuzz/transformation_record_synonymous_constants.h"
21 #include "source/fuzz/transformation_replace_id_with_synonym.h"
22
23 namespace spvtools {
24 namespace fuzz {
25 FuzzerPassInterchangeZeroLikeConstants::FuzzerPassInterchangeZeroLikeConstants(
26 opt::IRContext* ir_context, TransformationContext* transformation_context,
27 FuzzerContext* fuzzer_context,
28 protobufs::TransformationSequence* transformations)
29 : FuzzerPass(ir_context, transformation_context, fuzzer_context,
30 transformations) {}
31
32 FuzzerPassInterchangeZeroLikeConstants::
33 ~FuzzerPassInterchangeZeroLikeConstants() = default;
34
35 uint32_t FuzzerPassInterchangeZeroLikeConstants::FindOrCreateToggledConstant(
36 opt::Instruction* declaration) {
37 // |declaration| must not be a specialization constant because we do not know
38 // the value of specialization constants.
39 if (opt::IsSpecConstantInst(declaration->opcode())) {
40 return 0;
41 }
42
43 auto constant = GetIRContext()->get_constant_mgr()->FindDeclaredConstant(
44 declaration->result_id());
45
46 // This pass only toggles zero-like constants
47 if (!constant->IsZero()) {
48 return 0;
49 }
50
51 if (constant->AsScalarConstant()) {
52 return FindOrCreateNullConstant(declaration->type_id());
53 } else if (constant->AsNullConstant()) {
54 // Add declaration of equivalent scalar constant
55 auto kind = constant->type()->kind();
56 if (kind == opt::analysis::Type::kBool ||
57 kind == opt::analysis::Type::kInteger ||
58 kind == opt::analysis::Type::kFloat) {
59 return FindOrCreateZeroConstant(declaration->type_id(), false);
60 }
61 }
62
63 return 0;
64 }
65
66 void FuzzerPassInterchangeZeroLikeConstants::Apply() {
67 // Make vector keeping track of all the uses we want to replace.
68 // This is a vector of pairs, where the first element is an id use descriptor
69 // identifying the use of a constant id and the second is the id that should
70 // be used to replace it.
71 std::vector<std::pair<protobufs::IdUseDescriptor, uint32_t>> uses_to_replace;
72
73 for (auto constant : GetIRContext()->GetConstants()) {
74 uint32_t constant_id = constant->result_id();
75 if (GetTransformationContext()->GetFactManager()->IdIsIrrelevant(
76 constant_id)) {
77 continue;
78 }
79
80 uint32_t toggled_id = FindOrCreateToggledConstant(constant);
81 if (!toggled_id) {
82 // Not a zero-like constant
83 continue;
84 }
85
86 assert(!GetTransformationContext()->GetFactManager()->IdIsIrrelevant(
87 toggled_id) &&
88 "FindOrCreateToggledConstant can't produce an irrelevant id");
89
90 // Record synonymous constants
91 ApplyTransformation(
92 TransformationRecordSynonymousConstants(constant_id, toggled_id));
93
94 // Find all the uses of the constant and, for each, probabilistically
95 // decide whether to replace it.
96 GetIRContext()->get_def_use_mgr()->ForEachUse(
97 constant_id,
98 [this, toggled_id, &uses_to_replace](opt::Instruction* use_inst,
99 uint32_t use_index) -> void {
100 if (GetFuzzerContext()->ChoosePercentage(
101 GetFuzzerContext()
102 ->GetChanceOfInterchangingZeroLikeConstants())) {
103 MaybeAddUseToReplace(use_inst, use_index, toggled_id,
104 &uses_to_replace);
105 }
106 });
107 }
108
109 // Replace the ids if it is allowed.
110 for (auto use_to_replace : uses_to_replace) {
111 MaybeApplyTransformation(TransformationReplaceIdWithSynonym(
112 use_to_replace.first, use_to_replace.second));
113 }
114 }
115 } // namespace fuzz
116 } // namespace spvtools