Debian Codegraph @

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

raw
1 // Copyright (c) 2019 Google LLC
2 //
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
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
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.
14
15 #include "source/fuzz/transformation_add_constant_scalar.h"
16
17 #include "source/fuzz/fuzzer_util.h"
18
19 namespace spvtools {
20 namespace fuzz {
21
22 TransformationAddConstantScalar::TransformationAddConstantScalar(
23 const spvtools::fuzz::protobufs::TransformationAddConstantScalar& message)
24 : message_(message) {}
25
26 TransformationAddConstantScalar::TransformationAddConstantScalar(
27 uint32_t fresh_id, uint32_t type_id, const std::vector<uint32_t>& words,
28 bool is_irrelevant) {
29 message_.set_fresh_id(fresh_id);
30 message_.set_type_id(type_id);
31 message_.set_is_irrelevant(is_irrelevant);
32 for (auto word : words) {
33 message_.add_word(word);
34 }
35 }
36
37 bool TransformationAddConstantScalar::IsApplicable(
38 opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
39 // The id needs to be fresh.
40 if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
41 return false;
42 }
43 // The type id for the scalar must exist and be a type.
44 auto type = ir_context->get_type_mgr()->GetType(message_.type_id());
45 if (!type) {
46 return false;
47 }
48 uint32_t width;
49 if (type->AsFloat()) {
50 width = type->AsFloat()->width();
51 } else if (type->AsInteger()) {
52 width = type->AsInteger()->width();
53 } else {
54 return false;
55 }
56 // The number of words is the integer floor of the width.
57 auto words = (width + 32 - 1) / 32;
58
59 // The number of words provided by the transformation needs to match the
60 // width of the type.
61 return static_cast<uint32_t>(message_.word().size()) == words;
62 }
63
64 void TransformationAddConstantScalar::Apply(
65 opt::IRContext* ir_context,
66 TransformationContext* transformation_context) const {
67 ir_context->module()->AddGlobalValue(MakeUnique<opt::Instruction>(
68 ir_context, SpvOpConstant, message_.type_id(), message_.fresh_id(),
69 opt::Instruction::OperandList(
70 {{SPV_OPERAND_TYPE_LITERAL_INTEGER,
71 std::vector<uint32_t>(message_.word().begin(),
72 message_.word().end())}})));
73
74 fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
75
76 // We have added an instruction to the module, so need to be careful about the
77 // validity of existing analyses.
78 ir_context->InvalidateAnalysesExceptFor(
79 opt::IRContext::Analysis::kAnalysisNone);
80
81 if (message_.is_irrelevant()) {
82 transformation_context->GetFactManager()->AddFactIdIsIrrelevant(
83 message_.fresh_id());
84 }
85 }
86
87 protobufs::Transformation TransformationAddConstantScalar::ToMessage() const {
88 protobufs::Transformation result;
89 *result.mutable_add_constant_scalar() = message_;
90 return result;
91 }
92
93 std::unordered_set<uint32_t> TransformationAddConstantScalar::GetFreshIds()
94 const {
95 return {message_.fresh_id()};
96 }
97
98 } // namespace fuzz
99 } // namespace spvtools