transformation_move_instruction_down.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  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_MOVE_INSTRUCTION_DOWN_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_MOVE_INSTRUCTION_DOWN_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 TransformationMoveInstructionDown : public Transformation {
  23. public:
  24. explicit TransformationMoveInstructionDown(
  25. protobufs::TransformationMoveInstructionDown message);
  26. explicit TransformationMoveInstructionDown(
  27. const protobufs::InstructionDescriptor& instruction);
  28. // - |instruction| should be a descriptor of a valid instruction in the module
  29. // - |instruction|'s opcode should be supported by this transformation
  30. // - neither |instruction| nor its successor may be the last instruction in
  31. // the block
  32. // - |instruction|'s successor may not be dependent on the |instruction|
  33. // - it should be possible to insert |instruction|'s opcode after its
  34. // successor
  35. bool IsApplicable(
  36. opt::IRContext* ir_context,
  37. const TransformationContext& transformation_context) const override;
  38. // Swaps |instruction| with its successor.
  39. void Apply(opt::IRContext* ir_context,
  40. TransformationContext* transformation_context) const override;
  41. std::unordered_set<uint32_t> GetFreshIds() const override;
  42. protobufs::Transformation ToMessage() const override;
  43. private:
  44. // Returns true if the |inst| is supported by this transformation.
  45. static bool IsInstructionSupported(opt::IRContext* ir_context,
  46. const opt::Instruction& inst);
  47. // Returns true if |inst| represents a "simple" instruction. That is, it
  48. // neither reads from nor writes to the memory and is not a barrier.
  49. static bool IsSimpleInstruction(opt::IRContext* ir_context,
  50. const opt::Instruction& inst);
  51. // Returns true if |inst| reads from memory.
  52. static bool IsMemoryReadInstruction(opt::IRContext* ir_context,
  53. const opt::Instruction& inst);
  54. // Returns id being used by |inst| to read from. |inst| must be a memory read
  55. // instruction (see IsMemoryReadInstruction). Returned id is not guaranteed to
  56. // have pointer type.
  57. static uint32_t GetMemoryReadTarget(opt::IRContext* ir_context,
  58. const opt::Instruction& inst);
  59. // Returns true if |inst| that writes to the memory.
  60. static bool IsMemoryWriteInstruction(opt::IRContext* ir_context,
  61. const opt::Instruction& inst);
  62. // Returns id being used by |inst| to write into. |inst| must be a memory
  63. // write instruction (see IsMemoryWriteInstruction). Returned id is not
  64. // guaranteed to have pointer type.
  65. static uint32_t GetMemoryWriteTarget(opt::IRContext* ir_context,
  66. const opt::Instruction& inst);
  67. // Returns true if |inst| either reads from or writes to the memory
  68. // (see IsMemoryReadInstruction and IsMemoryWriteInstruction accordingly).
  69. static bool IsMemoryInstruction(opt::IRContext* ir_context,
  70. const opt::Instruction& inst);
  71. // Returns true if |inst| is a barrier instruction.
  72. static bool IsBarrierInstruction(const opt::Instruction& inst);
  73. // Returns true if it is possible to swap |a| and |b| without changing the
  74. // module's semantics. |a| and |b| are required to be supported instructions
  75. // (see IsInstructionSupported). In particular, if either |a| or |b| are
  76. // memory or barrier instructions, some checks are used to only say that they
  77. // can be swapped if the swap is definitely semantics-preserving.
  78. static bool CanSafelySwapInstructions(opt::IRContext* ir_context,
  79. const opt::Instruction& a,
  80. const opt::Instruction& b,
  81. const FactManager& fact_manager);
  82. protobufs::TransformationMoveInstructionDown message_;
  83. };
  84. } // namespace fuzz
  85. } // namespace spvtools
  86. #endif // SOURCE_FUZZ_TRANSFORMATION_MOVE_INSTRUCTION_DOWN_H_