transformation_add_constant_boolean.cpp 2.8 KB

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