transformation_vector_shuffle.cpp 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  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_vector_shuffle.h"
  15. #include "source/fuzz/fuzzer_util.h"
  16. #include "source/fuzz/instruction_descriptor.h"
  17. namespace spvtools {
  18. namespace fuzz {
  19. TransformationVectorShuffle::TransformationVectorShuffle(
  20. const spvtools::fuzz::protobufs::TransformationVectorShuffle& message)
  21. : message_(message) {}
  22. TransformationVectorShuffle::TransformationVectorShuffle(
  23. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  24. uint32_t fresh_id, uint32_t vector1, uint32_t vector2,
  25. const std::vector<uint32_t>& component) {
  26. *message_.mutable_instruction_to_insert_before() =
  27. instruction_to_insert_before;
  28. message_.set_fresh_id(fresh_id);
  29. message_.set_vector1(vector1);
  30. message_.set_vector2(vector2);
  31. for (auto a_component : component) {
  32. message_.add_component(a_component);
  33. }
  34. }
  35. bool TransformationVectorShuffle::IsApplicable(
  36. opt::IRContext* context,
  37. const spvtools::fuzz::FactManager& /*unused*/) const {
  38. // The fresh id must not already be in use.
  39. if (!fuzzerutil::IsFreshId(context, message_.fresh_id())) {
  40. return false;
  41. }
  42. // The instruction before which the shuffle will be inserted must exist.
  43. auto instruction_to_insert_before =
  44. FindInstruction(message_.instruction_to_insert_before(), context);
  45. if (!instruction_to_insert_before) {
  46. return false;
  47. }
  48. // The first vector must be an instruction with a type id
  49. auto vector1_instruction =
  50. context->get_def_use_mgr()->GetDef(message_.vector1());
  51. if (!vector1_instruction || !vector1_instruction->type_id()) {
  52. return false;
  53. }
  54. // The second vector must be an instruction with a type id
  55. auto vector2_instruction =
  56. context->get_def_use_mgr()->GetDef(message_.vector2());
  57. if (!vector2_instruction || !vector2_instruction->type_id()) {
  58. return false;
  59. }
  60. auto vector1_type =
  61. context->get_type_mgr()->GetType(vector1_instruction->type_id());
  62. // The first vector instruction's type must actually be a vector type.
  63. if (!vector1_type->AsVector()) {
  64. return false;
  65. }
  66. auto vector2_type =
  67. context->get_type_mgr()->GetType(vector2_instruction->type_id());
  68. // The second vector instruction's type must actually be a vector type.
  69. if (!vector2_type->AsVector()) {
  70. return false;
  71. }
  72. // The element types of the vectors must be the same.
  73. if (vector1_type->AsVector()->element_type() !=
  74. vector2_type->AsVector()->element_type()) {
  75. return false;
  76. }
  77. uint32_t combined_size = vector1_type->AsVector()->element_count() +
  78. vector2_type->AsVector()->element_count();
  79. for (auto a_compoment : message_.component()) {
  80. // 0xFFFFFFFF is used to represent an undefined component. Unless
  81. // undefined, a component must be less than the combined size of the
  82. // vectors.
  83. if (a_compoment != 0xFFFFFFFF && a_compoment >= combined_size) {
  84. return false;
  85. }
  86. }
  87. // The module must already declare an appropriate type in which to store the
  88. // result of the shuffle.
  89. if (!GetResultTypeId(context, *vector1_type->AsVector()->element_type())) {
  90. return false;
  91. }
  92. // Each of the vectors used in the shuffle must be available at the insertion
  93. // point.
  94. for (auto used_instruction : {vector1_instruction, vector2_instruction}) {
  95. if (auto block = context->get_instr_block(used_instruction)) {
  96. if (!context->GetDominatorAnalysis(block->GetParent())
  97. ->Dominates(used_instruction, instruction_to_insert_before)) {
  98. return false;
  99. }
  100. }
  101. }
  102. // It must be legitimate to insert an OpVectorShuffle before the identified
  103. // instruction.
  104. return fuzzerutil::CanInsertOpcodeBeforeInstruction(
  105. SpvOpVectorShuffle, instruction_to_insert_before);
  106. }
  107. void TransformationVectorShuffle::Apply(
  108. opt::IRContext* context, spvtools::fuzz::FactManager* fact_manager) const {
  109. // Make input operands for a shuffle instruction - these comprise the two
  110. // vectors being shuffled, followed by the integer literal components.
  111. opt::Instruction::OperandList shuffle_operands = {
  112. {SPV_OPERAND_TYPE_ID, {message_.vector1()}},
  113. {SPV_OPERAND_TYPE_ID, {message_.vector2()}}};
  114. for (auto a_component : message_.component()) {
  115. shuffle_operands.push_back(
  116. {SPV_OPERAND_TYPE_LITERAL_INTEGER, {a_component}});
  117. }
  118. uint32_t result_type_id = GetResultTypeId(
  119. context, *GetVectorType(context, message_.vector1())->element_type());
  120. // Add a shuffle instruction right before the instruction identified by
  121. // |message_.instruction_to_insert_before|.
  122. FindInstruction(message_.instruction_to_insert_before(), context)
  123. ->InsertBefore(MakeUnique<opt::Instruction>(
  124. context, SpvOpVectorShuffle, result_type_id, message_.fresh_id(),
  125. shuffle_operands));
  126. fuzzerutil::UpdateModuleIdBound(context, message_.fresh_id());
  127. context->InvalidateAnalysesExceptFor(opt::IRContext::Analysis::kAnalysisNone);
  128. // Add synonym facts relating the defined elements of the shuffle result to
  129. // the vector components that they come from.
  130. for (uint32_t component_index = 0;
  131. component_index < static_cast<uint32_t>(message_.component_size());
  132. component_index++) {
  133. uint32_t component = message_.component(component_index);
  134. if (component == 0xFFFFFFFF) {
  135. // This component is undefined, so move on - but first note that the
  136. // overall shuffle result cannot be synonymous with any vector.
  137. continue;
  138. }
  139. // This describes the element of the result vector associated with
  140. // |component_index|.
  141. protobufs::DataDescriptor descriptor_for_result_component =
  142. MakeDataDescriptor(message_.fresh_id(), {component_index});
  143. protobufs::DataDescriptor descriptor_for_source_component;
  144. // Get a data descriptor for the component of the input vector to which
  145. // |component| refers.
  146. if (component <
  147. GetVectorType(context, message_.vector1())->element_count()) {
  148. descriptor_for_source_component =
  149. MakeDataDescriptor(message_.vector1(), {component});
  150. } else {
  151. auto index_into_vector_2 =
  152. component -
  153. GetVectorType(context, message_.vector1())->element_count();
  154. assert(index_into_vector_2 <
  155. GetVectorType(context, message_.vector2())->element_count() &&
  156. "Vector shuffle index is out of bounds.");
  157. descriptor_for_source_component =
  158. MakeDataDescriptor(message_.vector2(), {index_into_vector_2});
  159. }
  160. // Add a fact relating this input vector component with the associated
  161. // result component.
  162. fact_manager->AddFactDataSynonym(descriptor_for_result_component,
  163. descriptor_for_source_component, context);
  164. }
  165. }
  166. protobufs::Transformation TransformationVectorShuffle::ToMessage() const {
  167. protobufs::Transformation result;
  168. *result.mutable_vector_shuffle() = message_;
  169. return result;
  170. }
  171. uint32_t TransformationVectorShuffle::GetResultTypeId(
  172. opt::IRContext* context, const opt::analysis::Type& element_type) const {
  173. opt::analysis::Vector result_type(
  174. &element_type, static_cast<uint32_t>(message_.component_size()));
  175. return context->get_type_mgr()->GetId(&result_type);
  176. }
  177. opt::analysis::Vector* TransformationVectorShuffle::GetVectorType(
  178. opt::IRContext* context, uint32_t id_of_vector) {
  179. return context->get_type_mgr()
  180. ->GetType(context->get_def_use_mgr()->GetDef(id_of_vector)->type_id())
  181. ->AsVector();
  182. }
  183. } // namespace fuzz
  184. } // namespace spvtools