transformation_permute_phi_operands.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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 "source/fuzz/transformation_permute_phi_operands.h"
  15. #include <vector>
  16. #include "source/fuzz/fuzzer_util.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationPermutePhiOperands::TransformationPermutePhiOperands(
  20. protobufs::TransformationPermutePhiOperands message)
  21. : message_(std::move(message)) {}
  22. TransformationPermutePhiOperands::TransformationPermutePhiOperands(
  23. uint32_t result_id, const std::vector<uint32_t>& permutation) {
  24. message_.set_result_id(result_id);
  25. for (auto index : permutation) {
  26. message_.add_permutation(index);
  27. }
  28. }
  29. bool TransformationPermutePhiOperands::IsApplicable(
  30. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  31. // Check that |message_.result_id| is valid.
  32. const auto* inst =
  33. ir_context->get_def_use_mgr()->GetDef(message_.result_id());
  34. if (!inst || inst->opcode() != spv::Op::OpPhi) {
  35. return false;
  36. }
  37. // Check that |message_.permutation| has expected size.
  38. auto expected_permutation_size = inst->NumInOperands() / 2;
  39. if (static_cast<uint32_t>(message_.permutation().size()) !=
  40. expected_permutation_size) {
  41. return false;
  42. }
  43. // Check that |message_.permutation| has elements in range
  44. // [0, expected_permutation_size - 1].
  45. std::vector<uint32_t> permutation(message_.permutation().begin(),
  46. message_.permutation().end());
  47. assert(!fuzzerutil::HasDuplicates(permutation) &&
  48. "Permutation has duplicates");
  49. // We must check whether the permutation is empty first because in that case
  50. // |expected_permutation_size - 1| will produce
  51. // |std::numeric_limits<uint32_t>::max()| since it's an unsigned integer.
  52. return permutation.empty() ||
  53. fuzzerutil::IsPermutationOfRange(permutation, 0,
  54. expected_permutation_size - 1);
  55. }
  56. void TransformationPermutePhiOperands::Apply(
  57. opt::IRContext* ir_context, TransformationContext* /*unused*/) const {
  58. auto* inst = ir_context->get_def_use_mgr()->GetDef(message_.result_id());
  59. assert(inst);
  60. opt::Instruction::OperandList permuted_operands;
  61. permuted_operands.reserve(inst->NumInOperands());
  62. for (auto index : message_.permutation()) {
  63. permuted_operands.push_back(std::move(inst->GetInOperand(2 * index)));
  64. permuted_operands.push_back(std::move(inst->GetInOperand(2 * index + 1)));
  65. }
  66. inst->SetInOperands(std::move(permuted_operands));
  67. // Update the def-use manager.
  68. ir_context->UpdateDefUse(inst);
  69. }
  70. protobufs::Transformation TransformationPermutePhiOperands::ToMessage() const {
  71. protobufs::Transformation result;
  72. *result.mutable_permute_phi_operands() = message_;
  73. return result;
  74. }
  75. std::unordered_set<uint32_t> TransformationPermutePhiOperands::GetFreshIds()
  76. const {
  77. return std::unordered_set<uint32_t>();
  78. }
  79. } // namespace fuzz
  80. } // namespace spvtools