transformation_propagate_instruction_up.h 3.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  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_PROPAGATE_INSTRUCTION_UP_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_PROPAGATE_INSTRUCTION_UP_H_
  16. #include <map>
  17. #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
  18. #include "source/fuzz/transformation.h"
  19. #include "source/fuzz/transformation_context.h"
  20. #include "source/opt/ir_context.h"
  21. namespace spvtools {
  22. namespace fuzz {
  23. class TransformationPropagateInstructionUp : public Transformation {
  24. public:
  25. explicit TransformationPropagateInstructionUp(
  26. protobufs::TransformationPropagateInstructionUp message);
  27. TransformationPropagateInstructionUp(
  28. uint32_t block_id,
  29. const std::map<uint32_t, uint32_t>& predecessor_id_to_fresh_id);
  30. // - |block_id| must be a valid result id of some OpLabel instruction.
  31. // - |block_id| must have at least one predecessor
  32. // - |block_id| must contain an instruction that can be propagated using this
  33. // transformation
  34. // - the instruction can be propagated if:
  35. // - it's not an OpPhi
  36. // - it is supported by this transformation
  37. // - it depends only on instructions from different basic blocks or on
  38. // OpPhi instructions from the same basic block
  39. // - it should be possible to insert the propagated instruction at the end of
  40. // each |block_id|'s predecessor
  41. // - |predecessor_id_to_fresh_id| must have an entry for at least every
  42. // predecessor of |block_id|
  43. // - each value in the |predecessor_id_to_fresh_id| map must be a fresh id
  44. // - all fresh ids in the |predecessor_id_to_fresh_id| must be unique
  45. bool IsApplicable(
  46. opt::IRContext* ir_context,
  47. const TransformationContext& transformation_context) const override;
  48. // Inserts a copy of the propagated instruction into each |block_id|'s
  49. // predecessor. Replaces the original instruction with an OpPhi referring
  50. // inserted copies.
  51. void Apply(opt::IRContext* ir_context,
  52. TransformationContext* transformation_context) const override;
  53. std::unordered_set<uint32_t> GetFreshIds() const override;
  54. protobufs::Transformation ToMessage() const override;
  55. // Returns true if this transformation can be applied to the block with id
  56. // |block_id|. Concretely, returns true iff:
  57. // - |block_id| is a valid id of some block in the module
  58. // - |block_id| has predecessors
  59. // - |block_id| contains an instruction that can be propagated
  60. // - it is possible to insert the propagated instruction into every
  61. // |block_id|'s predecessor
  62. static bool IsApplicableToBlock(opt::IRContext* ir_context,
  63. uint32_t block_id);
  64. private:
  65. // Returns the instruction that will be propagated into the predecessors of
  66. // the |block_id|. Returns nullptr if no such an instruction exists.
  67. static opt::Instruction* GetInstructionToPropagate(opt::IRContext* ir_context,
  68. uint32_t block_id);
  69. // Returns true if |opcode| is supported by this transformation.
  70. static bool IsOpcodeSupported(spv::Op opcode);
  71. protobufs::TransformationPropagateInstructionUp message_;
  72. };
  73. } // namespace fuzz
  74. } // namespace spvtools
  75. #endif // SOURCE_FUZZ_TRANSFORMATION_PROPAGATE_INSTRUCTION_UP_H_