transformation_composite_construct.h 3.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  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/fact_manager.h"
  17. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  18. #include "source/fuzz/transformation.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. const 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(opt::IRContext* context,
  47. const FactManager& fact_manager) const override;
  48. // Inserts a new OpCompositeConstruct instruction, with id
  49. // |message_.fresh_id|, directly before the instruction identified by
  50. // |message_.base_instruction_id| and |message_.offset|. The instruction
  51. // creates a composite of type |message_.composite_type_id| using the ids of
  52. // |message_.component|.
  53. void Apply(opt::IRContext* context, FactManager* fact_manager) const override;
  54. protobufs::Transformation ToMessage() const override;
  55. private:
  56. // Helper to decide whether the components of the transformation are suitable
  57. // for constructing an array of the given type.
  58. bool ComponentsForArrayConstructionAreOK(
  59. opt::IRContext* context, const opt::analysis::Array& array_type) const;
  60. // Similar, but for matrices.
  61. bool ComponentsForMatrixConstructionAreOK(
  62. opt::IRContext* context, const opt::analysis::Matrix& matrix_type) const;
  63. // Similar, but for structs.
  64. bool ComponentsForStructConstructionAreOK(
  65. opt::IRContext* context, const opt::analysis::Struct& struct_type) const;
  66. // Similar, but for vectors.
  67. bool ComponentsForVectorConstructionAreOK(
  68. opt::IRContext* context, const opt::analysis::Vector& vector_type) const;
  69. protobufs::TransformationCompositeConstruct message_;
  70. };
  71. } // namespace fuzz
  72. } // namespace spvtools
  73. #endif // SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_CONSTRUCT_H_