transformation_add_constant_scalar.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  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_scalar.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. namespace spvtools {
  17. namespace fuzz {
  18. TransformationAddConstantScalar::TransformationAddConstantScalar(
  19. spvtools::fuzz::protobufs::TransformationAddConstantScalar message)
  20. : message_(std::move(message)) {}
  21. TransformationAddConstantScalar::TransformationAddConstantScalar(
  22. uint32_t fresh_id, uint32_t type_id, const std::vector<uint32_t>& words,
  23. bool is_irrelevant) {
  24. message_.set_fresh_id(fresh_id);
  25. message_.set_type_id(type_id);
  26. message_.set_is_irrelevant(is_irrelevant);
  27. for (auto word : words) {
  28. message_.add_word(word);
  29. }
  30. }
  31. bool TransformationAddConstantScalar::IsApplicable(
  32. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  33. // The id needs to be fresh.
  34. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  35. return false;
  36. }
  37. // The type id for the scalar must exist and be a type.
  38. auto type = ir_context->get_type_mgr()->GetType(message_.type_id());
  39. if (!type) {
  40. return false;
  41. }
  42. uint32_t width;
  43. if (type->AsFloat()) {
  44. width = type->AsFloat()->width();
  45. } else if (type->AsInteger()) {
  46. width = type->AsInteger()->width();
  47. } else {
  48. return false;
  49. }
  50. // The number of words is the integer floor of the width.
  51. auto words = (width + 32 - 1) / 32;
  52. // The number of words provided by the transformation needs to match the
  53. // width of the type.
  54. return static_cast<uint32_t>(message_.word().size()) == words;
  55. }
  56. void TransformationAddConstantScalar::Apply(
  57. opt::IRContext* ir_context,
  58. TransformationContext* transformation_context) const {
  59. auto new_instruction = MakeUnique<opt::Instruction>(
  60. ir_context, spv::Op::OpConstant, message_.type_id(), message_.fresh_id(),
  61. opt::Instruction::OperandList(
  62. {{SPV_OPERAND_TYPE_LITERAL_INTEGER,
  63. std::vector<uint32_t>(message_.word().begin(),
  64. message_.word().end())}}));
  65. auto new_instruction_ptr = new_instruction.get();
  66. ir_context->module()->AddGlobalValue(std::move(new_instruction));
  67. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  68. // Inform the def-use manager about the new instruction. Invalidate the
  69. // constant manager as we have added a new constant.
  70. ir_context->get_def_use_mgr()->AnalyzeInstDef(new_instruction_ptr);
  71. ir_context->InvalidateAnalyses(opt::IRContext::kAnalysisConstants);
  72. if (message_.is_irrelevant()) {
  73. transformation_context->GetFactManager()->AddFactIdIsIrrelevant(
  74. message_.fresh_id());
  75. }
  76. }
  77. protobufs::Transformation TransformationAddConstantScalar::ToMessage() const {
  78. protobufs::Transformation result;
  79. *result.mutable_add_constant_scalar() = message_;
  80. return result;
  81. }
  82. std::unordered_set<uint32_t> TransformationAddConstantScalar::GetFreshIds()
  83. const {
  84. return {message_.fresh_id()};
  85. }
  86. } // namespace fuzz
  87. } // namespace spvtools