1 // Copyright (c) 2019 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/transformation_add_constant_boolean.h"
17 #include "source/fuzz/fuzzer_util.h"
18 #include "source/opt/types.h"
23 TransformationAddConstantBoolean::TransformationAddConstantBoolean(
24 protobufs::TransformationAddConstantBoolean message
)
25 : message_(std::move(message
)) {}
27 TransformationAddConstantBoolean::TransformationAddConstantBoolean(
28 uint32_t fresh_id
, bool is_true
, bool is_irrelevant
) {
29 message_
.set_fresh_id(fresh_id
);
30 message_
.set_is_true(is_true
);
31 message_
.set_is_irrelevant(is_irrelevant
);
34 bool TransformationAddConstantBoolean::IsApplicable(
35 opt::IRContext
* ir_context
, const TransformationContext
& /*unused*/) const {
36 return fuzzerutil::MaybeGetBoolType(ir_context
) != 0 &&
37 fuzzerutil::IsFreshId(ir_context
, message_
.fresh_id());
40 void TransformationAddConstantBoolean::Apply(
41 opt::IRContext
* ir_context
,
42 TransformationContext
* transformation_context
) const {
43 // Add the boolean constant to the module, ensuring the module's id bound is
45 auto new_instruction
= MakeUnique
<opt::Instruction
>(
47 message_
.is_true() ? spv::Op::OpConstantTrue
: spv::Op::OpConstantFalse
,
48 fuzzerutil::MaybeGetBoolType(ir_context
), message_
.fresh_id(),
49 opt::Instruction::OperandList());
50 auto new_instruction_ptr
= new_instruction
.get();
51 ir_context
->module()->AddGlobalValue(std::move(new_instruction
));
52 fuzzerutil::UpdateModuleIdBound(ir_context
, message_
.fresh_id());
54 // Inform the def-use manager about the new instruction. Invalidate the
55 // constant manager as we have added a new constant.
56 ir_context
->get_def_use_mgr()->AnalyzeInstDef(new_instruction_ptr
);
57 ir_context
->InvalidateAnalyses(opt::IRContext::kAnalysisConstants
);
59 if (message_
.is_irrelevant()) {
60 transformation_context
->GetFactManager()->AddFactIdIsIrrelevant(
65 protobufs::Transformation
TransformationAddConstantBoolean::ToMessage() const {
66 protobufs::Transformation result
;
67 *result
.mutable_add_constant_boolean() = message_
;
71 std::unordered_set
<uint32_t> TransformationAddConstantBoolean::GetFreshIds()
73 return {message_
.fresh_id()};
77 } // namespace spvtools