transformation_composite_extract.cpp 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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_composite_extract.h"
  15. #include <vector>
  16. #include "source/fuzz/data_descriptor.h"
  17. #include "source/fuzz/fuzzer_util.h"
  18. #include "source/fuzz/instruction_descriptor.h"
  19. namespace spvtools {
  20. namespace fuzz {
  21. TransformationCompositeExtract::TransformationCompositeExtract(
  22. protobufs::TransformationCompositeExtract message)
  23. : message_(std::move(message)) {}
  24. TransformationCompositeExtract::TransformationCompositeExtract(
  25. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  26. uint32_t fresh_id, uint32_t composite_id,
  27. const std::vector<uint32_t>& index) {
  28. *message_.mutable_instruction_to_insert_before() =
  29. instruction_to_insert_before;
  30. message_.set_fresh_id(fresh_id);
  31. message_.set_composite_id(composite_id);
  32. for (auto an_index : index) {
  33. message_.add_index(an_index);
  34. }
  35. }
  36. bool TransformationCompositeExtract::IsApplicable(
  37. opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
  38. if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
  39. return false;
  40. }
  41. auto instruction_to_insert_before =
  42. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  43. if (!instruction_to_insert_before) {
  44. return false;
  45. }
  46. auto composite_instruction =
  47. ir_context->get_def_use_mgr()->GetDef(message_.composite_id());
  48. if (!composite_instruction) {
  49. return false;
  50. }
  51. if (!fuzzerutil::IdIsAvailableBeforeInstruction(
  52. ir_context, instruction_to_insert_before, message_.composite_id())) {
  53. return false;
  54. }
  55. auto composite_type =
  56. ir_context->get_type_mgr()->GetType(composite_instruction->type_id());
  57. if (!fuzzerutil::IsCompositeType(composite_type)) {
  58. return false;
  59. }
  60. if (!fuzzerutil::CanInsertOpcodeBeforeInstruction(
  61. spv::Op::OpCompositeExtract, instruction_to_insert_before)) {
  62. return false;
  63. }
  64. return fuzzerutil::WalkCompositeTypeIndices(ir_context,
  65. composite_instruction->type_id(),
  66. message_.index()) != 0;
  67. }
  68. void TransformationCompositeExtract::Apply(
  69. opt::IRContext* ir_context,
  70. TransformationContext* transformation_context) const {
  71. opt::Instruction::OperandList extract_operands;
  72. extract_operands.push_back({SPV_OPERAND_TYPE_ID, {message_.composite_id()}});
  73. for (auto an_index : message_.index()) {
  74. extract_operands.push_back({SPV_OPERAND_TYPE_LITERAL_INTEGER, {an_index}});
  75. }
  76. auto composite_instruction =
  77. ir_context->get_def_use_mgr()->GetDef(message_.composite_id());
  78. auto extracted_type = fuzzerutil::WalkCompositeTypeIndices(
  79. ir_context, composite_instruction->type_id(), message_.index());
  80. auto insert_before =
  81. FindInstruction(message_.instruction_to_insert_before(), ir_context);
  82. opt::Instruction* new_instruction =
  83. insert_before->InsertBefore(MakeUnique<opt::Instruction>(
  84. ir_context, spv::Op::OpCompositeExtract, extracted_type,
  85. message_.fresh_id(), extract_operands));
  86. ir_context->get_def_use_mgr()->AnalyzeInstDefUse(new_instruction);
  87. ir_context->set_instr_block(new_instruction,
  88. ir_context->get_instr_block(insert_before));
  89. fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
  90. // No analyses need to be invalidated since the transformation is local to a
  91. // block and the def-use and instruction-to-block mappings have been updated.
  92. AddDataSynonymFacts(ir_context, transformation_context);
  93. }
  94. protobufs::Transformation TransformationCompositeExtract::ToMessage() const {
  95. protobufs::Transformation result;
  96. *result.mutable_composite_extract() = message_;
  97. return result;
  98. }
  99. std::unordered_set<uint32_t> TransformationCompositeExtract::GetFreshIds()
  100. const {
  101. return {message_.fresh_id()};
  102. }
  103. void TransformationCompositeExtract::AddDataSynonymFacts(
  104. opt::IRContext* ir_context,
  105. TransformationContext* transformation_context) const {
  106. // Don't add synonyms if the composite being extracted from is not suitable,
  107. // or if the result id into which we are extracting is irrelevant.
  108. if (!fuzzerutil::CanMakeSynonymOf(
  109. ir_context, *transformation_context,
  110. *ir_context->get_def_use_mgr()->GetDef(message_.composite_id())) ||
  111. transformation_context->GetFactManager()->IdIsIrrelevant(
  112. message_.fresh_id())) {
  113. return;
  114. }
  115. // Add the fact that the id storing the extracted element is synonymous with
  116. // the index into the structure.
  117. std::vector<uint32_t> indices(message_.index().begin(),
  118. message_.index().end());
  119. auto data_descriptor_for_extracted_element =
  120. MakeDataDescriptor(message_.composite_id(), indices);
  121. auto data_descriptor_for_result_id =
  122. MakeDataDescriptor(message_.fresh_id(), {});
  123. transformation_context->GetFactManager()->AddFactDataSynonym(
  124. data_descriptor_for_extracted_element, data_descriptor_for_result_id);
  125. }
  126. } // namespace fuzz
  127. } // namespace spvtools