transformation_duplicate_region_with_selection.h 4.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2020 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_DUPLICATE_REGION_WITH_SELECTION_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_DUPLICATE_REGION_WITH_SELECTION_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 TransformationDuplicateRegionWithSelection : public Transformation {
  23. public:
  24. explicit TransformationDuplicateRegionWithSelection(
  25. protobufs::TransformationDuplicateRegionWithSelection message);
  26. explicit TransformationDuplicateRegionWithSelection(
  27. uint32_t new_entry_fresh_id, uint32_t condition_id,
  28. uint32_t merge_label_fresh_id, uint32_t entry_block_id,
  29. uint32_t exit_block_id,
  30. const std::map<uint32_t, uint32_t>& original_label_to_duplicate_label,
  31. const std::map<uint32_t, uint32_t>& original_id_to_duplicate_id,
  32. const std::map<uint32_t, uint32_t>& original_id_to_phi_id);
  33. // - |new_entry_fresh_id|, |merge_label_fresh_id| must be fresh and distinct.
  34. // - |condition_id| must refer to a valid instruction of boolean type.
  35. // - |entry_block_id| and |exit_block_id| must refer to valid blocks and they
  36. // must form a single-entry, single-exit region. Its constructs and their
  37. // merge blocks must be either wholly within or wholly outside of the
  38. // region.
  39. // - |original_label_to_duplicate_label| must contain at least a key for every
  40. // block in the original region.
  41. // - |original_id_to_duplicate_id| must contain at least a key for every
  42. // result id in the original region.
  43. // - |original_id_to_phi_id| must contain at least a key for every result id
  44. // available at the end of the original region.
  45. // - In each of these three maps, each value must be a distinct, fresh id.
  46. bool IsApplicable(
  47. opt::IRContext* ir_context,
  48. const TransformationContext& transformation_context) const override;
  49. // A transformation that inserts a conditional statement with a boolean
  50. // expression of arbitrary value and duplicates a given single-entry,
  51. // single-exit region, so that it is present in each conditional branch and
  52. // will be executed regardless of which branch will be taken.
  53. void Apply(opt::IRContext* ir_context,
  54. TransformationContext* transformation_context) const override;
  55. // Returns the set of blocks dominated by |entry_block| and post-dominated
  56. // by |exit_block|.
  57. static std::set<opt::BasicBlock*> GetRegionBlocks(
  58. opt::IRContext* ir_context, opt::BasicBlock* entry_block,
  59. opt::BasicBlock* exit_block);
  60. // Returns true if and only if |instr| is available at the end of the region
  61. // for which |exit_block| is the final block.
  62. static bool AvailableAfterRegion(const opt::Instruction& instr,
  63. opt::BasicBlock* exit_block,
  64. opt::IRContext* ir_context);
  65. // Returns true if and only if |instr| is valid as an argument to an OpPhi
  66. // instruction.
  67. static bool ValidOpPhiArgument(const opt::Instruction& instr,
  68. opt::IRContext* ir_context);
  69. std::unordered_set<uint32_t> GetFreshIds() const override;
  70. protobufs::Transformation ToMessage() const override;
  71. private:
  72. protobufs::TransformationDuplicateRegionWithSelection message_;
  73. };
  74. } // namespace fuzz
  75. } // namespace spvtools
  76. #endif // SOURCE_FUZZ_TRANSFORMATION_DUPLICATE_REGION_WITH_SELECTION_H_