transformation_vector_shuffle.cpp 8.0 KB

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