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