transformation_add_constant_boolean.cpp 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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. const protobufs::TransformationAddConstantBoolean& message)
  21. : message_(message) {}
  22. TransformationAddConstantBoolean::TransformationAddConstantBoolean(
  23. uint32_t fresh_id, bool is_true) {
  24. message_.set_fresh_id(fresh_id);
  25. message_.set_is_true(is_true);
  26. }
  27. bool TransformationAddConstantBoolean::IsApplicable(
  28. opt::IRContext* context, const FactManager& /*unused*/) const {
  29. opt::analysis::Bool bool_type;
  30. if (!context->get_type_mgr()->GetId(&bool_type)) {
  31. // No OpTypeBool is present.
  32. return false;
  33. }
  34. return fuzzerutil::IsFreshId(context, message_.fresh_id());
  35. }
  36. void TransformationAddConstantBoolean::Apply(opt::IRContext* context,
  37. FactManager* /*unused*/) const {
  38. opt::analysis::Bool bool_type;
  39. // Add the boolean constant to the module, ensuring the module's id bound is
  40. // high enough.
  41. fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
  42. context->module()->AddGlobalValue(
  43. message_.is_true() ? SpvOpConstantTrue : SpvOpConstantFalse,
  44. message_.fresh_id(), context->get_type_mgr()->GetId(&bool_type));
  45. // We have added an instruction to the module, so need to be careful about the
  46. // validity of existing analyses.
  47. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  48. }
  49. protobufs::Transformation TransformationAddConstantBoolean::ToMessage() const {
  50. protobufs::Transformation result;
  51. *result.mutable_add_constant_boolean() = message_;
  52. return result;
  53. }
  54. } // namespace fuzz
  55. } // namespace spvtools