transformation_composite_insert.h 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. // Copyright (c) 2020 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. #ifndef SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_INSERT_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_INSERT_H_
  16. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  17. #include "source/fuzz/transformation.h"
  18. #include "source/fuzz/transformation_context.h"
  19. #include "source/opt/ir_context.h"
  20. namespace spvtools {
  21. namespace fuzz {
  22. class TransformationCompositeInsert : public Transformation {
  23. public:
  24. explicit TransformationCompositeInsert(
  25. protobufs::TransformationCompositeInsert message);
  26. TransformationCompositeInsert(
  27. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  28. uint32_t fresh_id, uint32_t composite_id, uint32_t object_id,
  29. const std::vector<uint32_t>& index);
  30. // - |message_.fresh_id| must be fresh.
  31. // - |message_.composite_id| must refer to an existing composite value.
  32. // - |message_.index| must refer to a correct index in the composite.
  33. // - The type id of the object and the type id of the component of the
  34. // composite at index |message_.index| must be the same.
  35. // - |message_.instruction_to_insert_before| must refer to a defined
  36. // instruction.
  37. // - It must be possible to insert OpCompositeInsert before
  38. // |instruction_to_insert_before|.
  39. bool IsApplicable(
  40. opt::IRContext* ir_context,
  41. const TransformationContext& transformation_context) const override;
  42. // Adds an instruction OpCompositeInsert before
  43. // |instruction_to_insert_before|, which creates a new composite from
  44. // |composite_id| by inserting |object_id| at the specified |index|.
  45. // Synonyms are created between those components which are identical in the
  46. // original and the modified composite and between the inserted object and its
  47. // copy in the modified composite.
  48. void Apply(opt::IRContext* ir_context,
  49. TransformationContext* transformation_context) const override;
  50. std::unordered_set<uint32_t> GetFreshIds() const override;
  51. protobufs::Transformation ToMessage() const override;
  52. // Checks if |instruction| is a instruction of a composite type supported by
  53. // this transformation.
  54. static bool IsCompositeInstructionSupported(opt::IRContext* ir_context,
  55. opt::Instruction* instruction);
  56. private:
  57. // Helper method for adding data synonym facts when applying the
  58. // transformation to |ir_context| and |transformation_context|.
  59. void AddDataSynonymFacts(opt::IRContext* ir_context,
  60. TransformationContext* transformation_context) const;
  61. protobufs::TransformationCompositeInsert message_;
  62. };
  63. } // namespace fuzz
  64. } // namespace spvtools
  65. #endif // SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_INSERT_H_