transformation_composite_extract.h 3.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #ifndef SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_EXTRACT_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_EXTRACT_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 TransformationCompositeExtract : public Transformation {
  23. public:
  24. explicit TransformationCompositeExtract(
  25. protobufs::TransformationCompositeExtract message);
  26. TransformationCompositeExtract(
  27. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  28. uint32_t fresh_id, uint32_t composite_id,
  29. const std::vector<uint32_t>& index);
  30. // - |message_.fresh_id| must be available
  31. // - |message_.instruction_to_insert_before| must identify an instruction
  32. // before which it is valid to place an OpCompositeExtract
  33. // - |message_.composite_id| must be the id of an instruction that defines
  34. // a composite object, and this id must be available at the instruction
  35. // identified by |message_.instruction_to_insert_before|
  36. // - |message_.index| must be a suitable set of indices for
  37. // |message_.composite_id|, i.e. it must be possible to follow this chain
  38. // of indices to reach a sub-object of |message_.composite_id|
  39. bool IsApplicable(
  40. opt::IRContext* ir_context,
  41. const TransformationContext& transformation_context) const override;
  42. // Adds an OpCompositeConstruct instruction before the instruction identified
  43. // by |message_.instruction_to_insert_before|, that extracts from
  44. // |message_.composite_id| via indices |message_.index| into
  45. // |message_.fresh_id|.
  46. //
  47. // Adds a synonym fact associating |message_.fresh_id| with the relevant
  48. // element of |message_.composite_id|, as long as these ids support synonym
  49. // creation.
  50. void Apply(opt::IRContext* ir_context,
  51. TransformationContext* transformation_context) const override;
  52. std::unordered_set<uint32_t> GetFreshIds() const override;
  53. protobufs::Transformation ToMessage() const override;
  54. private:
  55. // Helper method for adding data synonym facts when applying the
  56. // transformation to |ir_context| and |transformation_context|.
  57. void AddDataSynonymFacts(opt::IRContext* ir_context,
  58. TransformationContext* transformation_context) const;
  59. protobufs::TransformationCompositeExtract message_;
  60. };
  61. } // namespace fuzz
  62. } // namespace spvtools
  63. #endif // SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_EXTRACT_H_