transformation_add_copy_memory.h 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // Copyright (c) 2020 Vasyl Teliman
  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_ADD_COPY_MEMORY_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_ADD_COPY_MEMORY_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 TransformationAddCopyMemory : public Transformation {
  23. public:
  24. explicit TransformationAddCopyMemory(
  25. protobufs::TransformationAddCopyMemory message);
  26. TransformationAddCopyMemory(
  27. const protobufs::InstructionDescriptor& instruction_descriptor,
  28. uint32_t fresh_id, uint32_t source_id, spv::StorageClass storage_class,
  29. uint32_t initializer_id);
  30. // - |instruction_descriptor| must point to a valid instruction in the module.
  31. // - it should be possible to insert OpCopyMemory before
  32. // |instruction_descriptor| (i.e. the module remains valid after the
  33. // insertion).
  34. // - |source_id| must be a result id for some valid instruction in the module.
  35. // - |fresh_id| must be a fresh id to copy memory into.
  36. // - type of |source_id| must be OpTypePointer where pointee can be used with
  37. // OpCopyMemory.
  38. // - If the pointee type of |source_id| is a struct type, it must not have the
  39. // Block or BufferBlock decoration.
  40. // - |storage_class| must be either Private or Function.
  41. // - type ids of instructions with result ids |source_id| and |initialize_id|
  42. // must be the same.
  43. bool IsApplicable(
  44. opt::IRContext* ir_context,
  45. const TransformationContext& transformation_context) const override;
  46. // A global or local variable with id |target_id| and |storage_class| class is
  47. // created. An 'OpCopyMemory %fresh_id %source_id' instruction is inserted
  48. // before the |instruction_descriptor|.
  49. void Apply(opt::IRContext* ir_context,
  50. TransformationContext* transformation_context) const override;
  51. std::unordered_set<uint32_t> GetFreshIds() const override;
  52. protobufs::Transformation ToMessage() const override;
  53. // Returns true if we can copy memory from |instruction| using OpCopyMemory.
  54. static bool IsInstructionSupported(opt::IRContext* ir_context,
  55. opt::Instruction* inst);
  56. private:
  57. // Returns whether the type, pointed to by some OpTypePointer, can be used
  58. // with OpCopyMemory instruction.
  59. static bool CanUsePointeeWithCopyMemory(const opt::analysis::Type& type);
  60. protobufs::TransformationAddCopyMemory message_;
  61. };
  62. } // namespace fuzz
  63. } // namespace spvtools
  64. #endif // SOURCE_FUZZ_TRANSFORMATION_ADD_COPY_MEMORY_H_