transformation_add_spec_constant_op.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. // Copyright (c) 2020 Vasyl Teliman
  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 <utility>
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/transformation_add_spec_constant_op.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationAddSpecConstantOp::TransformationAddSpecConstantOp(
  20. spvtools::fuzz::protobufs::TransformationAddSpecConstantOp message)
  21. : message_(std::move(message)) {}
  22. TransformationAddSpecConstantOp::TransformationAddSpecConstantOp(
  23. uint32_t fresh_id, uint32_t type_id, SpvOp opcode,
  24. const opt::Instruction::OperandList& operands) {
  25. message_.set_fresh_id(fresh_id);
  26. message_.set_type_id(type_id);
  27. message_.set_opcode(opcode);
  28. for (const auto& operand : operands) {
  29. auto* op = message_.add_operand();
  30. op->set_operand_type(operand.type);
  31. for (auto word : operand.words) {
  32. op->add_operand_data(word);
  33. }
  34. }
  35. }
  36. bool TransformationAddSpecConstantOp::IsApplicable(
  37. opt::IRContext* ir_context,
  38. const TransformationContext& transformation_context) const {
  39. auto clone = fuzzerutil::CloneIRContext(ir_context);
  40. ApplyImpl(clone.get());
  41. return fuzzerutil::IsValid(clone.get(),
  42. transformation_context.GetValidatorOptions());
  43. }
  44. void TransformationAddSpecConstantOp::Apply(
  45. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  46. ApplyImpl(ir_context);
  47. ir_context->InvalidateAnalysesExceptFor(
  48. opt::IRContext::Analysis::kAnalysisNone);
  49. }
  50. void TransformationAddSpecConstantOp::ApplyImpl(
  51. opt::IRContext* ir_context) const {
  52. opt::Instruction::OperandList operands = {
  53. {SPV_OPERAND_TYPE_SPEC_CONSTANT_OP_NUMBER, {message_.opcode()}}};
  54. for (const auto& operand : message_.operand()) {
  55. std::vector<uint32_t> words(operand.operand_data().begin(),
  56. operand.operand_data().end());
  57. operands.push_back({static_cast<spv_operand_type_t>(operand.operand_type()),
  58. std::move(words)});
  59. }
  60. ir_context->AddGlobalValue(MakeUnique<opt::Instruction>(
  61. ir_context, SpvOpSpecConstantOp, message_.type_id(), message_.fresh_id(),
  62. std::move(operands)));
  63. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  64. }
  65. protobufs::Transformation TransformationAddSpecConstantOp::ToMessage() const {
  66. protobufs::Transformation result;
  67. *result.mutable_add_spec_constant_op() = message_;
  68. return result;
  69. }
  70. } // namespace fuzz
  71. } // namespace spvtools