transformation_expand_vector_reduction.cpp 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. // Copyright (c) 2020 André Perez Maselco
  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_expand_vector_reduction.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationExpandVectorReduction::TransformationExpandVectorReduction(
  20. protobufs::TransformationExpandVectorReduction message)
  21. : message_(std::move(message)) {}
  22. TransformationExpandVectorReduction::TransformationExpandVectorReduction(
  23. const uint32_t instruction_result_id,
  24. const std::vector<uint32_t>& fresh_ids) {
  25. message_.set_instruction_result_id(instruction_result_id);
  26. *message_.mutable_fresh_ids() =
  27. google::protobuf::RepeatedField<google::protobuf::uint32>(
  28. fresh_ids.begin(), fresh_ids.end());
  29. }
  30. bool TransformationExpandVectorReduction::IsApplicable(
  31. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  32. auto* instruction =
  33. ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
  34. // |instruction| must be defined.
  35. if (!instruction) {
  36. return false;
  37. }
  38. // |instruction| must be OpAny or OpAll.
  39. if (instruction->opcode() != spv::Op::OpAny &&
  40. instruction->opcode() != spv::Op::OpAll) {
  41. return false;
  42. }
  43. // |message_.fresh_ids.size| must have the exact number of fresh ids required
  44. // to apply the transformation.
  45. if (static_cast<uint32_t>(message_.fresh_ids().size()) !=
  46. GetRequiredFreshIdCount(ir_context, instruction)) {
  47. return false;
  48. }
  49. std::set<uint32_t> ids_used_by_this_transformation;
  50. for (uint32_t fresh_id : message_.fresh_ids()) {
  51. // All ids in |message_.fresh_ids| must be fresh.
  52. if (!fuzzerutil::IsFreshId(ir_context, fresh_id)) {
  53. return false;
  54. }
  55. // All fresh ids need to be distinct.
  56. if (!CheckIdIsFreshAndNotUsedByThisTransformation(
  57. fresh_id, ir_context, &ids_used_by_this_transformation)) {
  58. return false;
  59. }
  60. }
  61. return true;
  62. }
  63. void TransformationExpandVectorReduction::Apply(
  64. opt::IRContext* ir_context,
  65. TransformationContext* transformation_context) const {
  66. auto* instruction =
  67. ir_context->get_def_use_mgr()->GetDef(message_.instruction_result_id());
  68. auto* vector = ir_context->get_def_use_mgr()->GetDef(
  69. instruction->GetSingleWordInOperand(0));
  70. uint32_t vector_component_count = ir_context->get_type_mgr()
  71. ->GetType(vector->type_id())
  72. ->AsVector()
  73. ->element_count();
  74. // Fresh id iterator.
  75. auto fresh_id = message_.fresh_ids().begin();
  76. // |vector_components| are the ids of the extracted components from |vector|.
  77. std::vector<uint32_t> vector_components;
  78. for (uint32_t i = 0; i < vector_component_count; i++) {
  79. // Extracts the i-th |vector| component.
  80. auto vector_component =
  81. opt::Instruction(ir_context, spv::Op::OpCompositeExtract,
  82. instruction->type_id(), *fresh_id++,
  83. {{SPV_OPERAND_TYPE_ID, {vector->result_id()}},
  84. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {i}}});
  85. instruction->InsertBefore(MakeUnique<opt::Instruction>(vector_component));
  86. fuzzerutil::UpdateModuleIdBound(ir_context, vector_component.result_id());
  87. vector_components.push_back(vector_component.result_id());
  88. }
  89. // The first two |vector| components are used in the first logical operation.
  90. auto logical_instruction = opt::Instruction(
  91. ir_context,
  92. instruction->opcode() == spv::Op::OpAny ? spv::Op::OpLogicalOr
  93. : spv::Op::OpLogicalAnd,
  94. instruction->type_id(), *fresh_id++,
  95. {{SPV_OPERAND_TYPE_ID, {vector_components[0]}},
  96. {SPV_OPERAND_TYPE_ID, {vector_components[1]}}});
  97. instruction->InsertBefore(MakeUnique<opt::Instruction>(logical_instruction));
  98. fuzzerutil::UpdateModuleIdBound(ir_context, logical_instruction.result_id());
  99. // Evaluates the remaining components.
  100. for (uint32_t i = 2; i < vector_components.size(); i++) {
  101. logical_instruction = opt::Instruction(
  102. ir_context, logical_instruction.opcode(), instruction->type_id(),
  103. *fresh_id++,
  104. {{SPV_OPERAND_TYPE_ID, {vector_components[i]}},
  105. {SPV_OPERAND_TYPE_ID, {logical_instruction.result_id()}}});
  106. instruction->InsertBefore(
  107. MakeUnique<opt::Instruction>(logical_instruction));
  108. fuzzerutil::UpdateModuleIdBound(ir_context,
  109. logical_instruction.result_id());
  110. }
  111. ir_context->InvalidateAnalysesExceptFor(opt::IRContext::kAnalysisNone);
  112. // If it's possible to make a synonym of |instruction|, then add the fact that
  113. // the last |logical_instruction| is a synonym of |instruction|.
  114. if (fuzzerutil::CanMakeSynonymOf(ir_context, *transformation_context,
  115. *instruction)) {
  116. transformation_context->GetFactManager()->AddFactDataSynonym(
  117. MakeDataDescriptor(logical_instruction.result_id(), {}),
  118. MakeDataDescriptor(instruction->result_id(), {}));
  119. }
  120. }
  121. protobufs::Transformation TransformationExpandVectorReduction::ToMessage()
  122. const {
  123. protobufs::Transformation result;
  124. *result.mutable_expand_vector_reduction() = message_;
  125. return result;
  126. }
  127. uint32_t TransformationExpandVectorReduction::GetRequiredFreshIdCount(
  128. opt::IRContext* ir_context, opt::Instruction* instruction) {
  129. // For each vector component, 1 OpCompositeExtract and 1 OpLogical* (except
  130. // for the first component) instructions will be inserted.
  131. return 2 * ir_context->get_type_mgr()
  132. ->GetType(ir_context->get_def_use_mgr()
  133. ->GetDef(instruction->GetSingleWordInOperand(0))
  134. ->type_id())
  135. ->AsVector()
  136. ->element_count() -
  137. 1;
  138. }
  139. std::unordered_set<uint32_t> TransformationExpandVectorReduction::GetFreshIds()
  140. const {
  141. std::unordered_set<uint32_t> result;
  142. for (auto id : message_.fresh_ids()) {
  143. result.insert(id);
  144. }
  145. return result;
  146. }
  147. } // namespace fuzz
  148. } // namespace spvtools