transformation_set_loop_control.h 3.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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_SET_LOOP_CONTROL_H_
  15. #define SOURCE_FUZZ_TRANSFORMATION_SET_LOOP_CONTROL_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 TransformationSetLoopControl : public Transformation {
  23. public:
  24. const static uint32_t kLoopControlMaskInOperandIndex = 2;
  25. const static uint32_t kLoopControlFirstLiteralInOperandIndex = 3;
  26. explicit TransformationSetLoopControl(
  27. protobufs::TransformationSetLoopControl message);
  28. TransformationSetLoopControl(uint32_t block_id, uint32_t loop_control,
  29. uint32_t peel_count, uint32_t partial_count);
  30. // - |message_.block_id| must be a block containing an OpLoopMerge
  31. // instruction.
  32. // - |message_.loop_control| must be a legal loop control mask that
  33. // only uses controls available in the SPIR-V version associated with
  34. // |ir_context|, and must not add loop controls that are only valid in the
  35. // presence of guarantees about what the loop does (e.g. MinIterations).
  36. // - |message_.peel_count| (respectively |message_.partial_count|) must be
  37. // zero PeelCount (respectively PartialCount) is set in
  38. // |message_.loop_control|.
  39. bool IsApplicable(
  40. opt::IRContext* ir_context,
  41. const TransformationContext& transformation_context) const override;
  42. // - The loop control operand of the OpLoopMergeInstruction in
  43. // |message_.block_id| is overwritten with |message_.loop_control|.
  44. // - The literals associated with the loop control are updated to reflect any
  45. // controls with associated literals that have been removed (e.g.
  46. // MinIterations), and any that have been added (PeelCount and/or
  47. // PartialCount).
  48. void Apply(opt::IRContext* ir_context,
  49. TransformationContext* transformation_context) const override;
  50. std::unordered_set<uint32_t> GetFreshIds() const override;
  51. protobufs::Transformation ToMessage() const override;
  52. // Does the version of SPIR-V being used support the PartialCount loop
  53. // control?
  54. static bool PartialCountIsSupported(opt::IRContext* ir_context);
  55. // Does the version of SPIR-V being used support the PeelCount loop control?
  56. static bool PeelCountIsSupported(opt::IRContext* ir_context);
  57. private:
  58. // Returns true if and only if |loop_single_bit_mask| is *not* set in
  59. // |existing_loop_control| but *is* set in |message_.loop_control|.
  60. bool LoopControlBitIsAddedByTransformation(
  61. spv::LoopControlMask loop_control_single_bit_mask,
  62. uint32_t existing_loop_control_mask) const;
  63. protobufs::TransformationSetLoopControl message_;
  64. };
  65. } // namespace fuzz
  66. } // namespace spvtools
  67. #endif // SOURCE_FUZZ_TRANSFORMATION_SET_LOOP_CONTROL_H_