transformation_composite_construct.h 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  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_CONSTRUCT_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_CONSTRUCT_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 TransformationCompositeConstruct : public Transformation {
  23. public:
  24. explicit TransformationCompositeConstruct(
  25. protobufs::TransformationCompositeConstruct message);
  26. TransformationCompositeConstruct(
  27. uint32_t composite_type_id, std::vector<uint32_t> component,
  28. const protobufs::InstructionDescriptor& instruction_to_insert_before,
  29. uint32_t fresh_id);
  30. // - |message_.fresh_id| must not be used by the module.
  31. // - |message_.composite_type_id| must be the id of a composite type
  32. // - The elements of |message_.component| must be result ids that are
  33. // suitable for constructing an element of the given composite type, in
  34. // order
  35. // - The elements of |message_.component| must not be the target of any
  36. // decorations.
  37. // - |message_.base_instruction_id| must be the result id of an instruction
  38. // 'base' in some block 'blk'.
  39. // - 'blk' must contain an instruction 'inst' located |message_.offset|
  40. // instructions after 'base' (if |message_.offset| = 0 then 'inst' =
  41. // 'base').
  42. // - It must be legal to insert an OpCompositeConstruct instruction directly
  43. // before 'inst'.
  44. // - Each element of |message_.component| must be available directly before
  45. // 'inst'.
  46. bool IsApplicable(
  47. opt::IRContext* ir_context,
  48. const TransformationContext& transformation_context) const override;
  49. // Inserts a new OpCompositeConstruct instruction, with id
  50. // |message_.fresh_id|, directly before the instruction identified by
  51. // |message_.base_instruction_id| and |message_.offset|. The instruction
  52. // creates a composite of type |message_.composite_type_id| using the ids of
  53. // |message_.component|.
  54. //
  55. // Synonym facts are added between the elements of the resulting composite
  56. // and the components used to construct it, as long as the associated ids
  57. // support synonym creation.
  58. void Apply(opt::IRContext* ir_context,
  59. TransformationContext* transformation_context) const override;
  60. std::unordered_set<uint32_t> GetFreshIds() const override;
  61. protobufs::Transformation ToMessage() const override;
  62. private:
  63. // Helper to decide whether the components of the transformation are suitable
  64. // for constructing an array of the given type.
  65. bool ComponentsForArrayConstructionAreOK(
  66. opt::IRContext* ir_context, const opt::analysis::Array& array_type) const;
  67. // Similar, but for matrices.
  68. bool ComponentsForMatrixConstructionAreOK(
  69. opt::IRContext* ir_context,
  70. const opt::analysis::Matrix& matrix_type) const;
  71. // Similar, but for structs.
  72. bool ComponentsForStructConstructionAreOK(
  73. opt::IRContext* ir_context,
  74. const opt::analysis::Struct& struct_type) const;
  75. // Similar, but for vectors.
  76. bool ComponentsForVectorConstructionAreOK(
  77. opt::IRContext* ir_context,
  78. const opt::analysis::Vector& vector_type) const;
  79. // Helper method for adding data synonym facts when applying the
  80. // transformation to |ir_context| and |transformation_context|.
  81. void AddDataSynonymFacts(opt::IRContext* ir_context,
  82. TransformationContext* transformation_context) const;
  83. protobufs::TransformationCompositeConstruct message_;
  84. };
  85. } // namespace fuzz
  86. } // namespace spvtools
  87. #endif // SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_CONSTRUCT_H_