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 
15 #ifndef SOURCE_FUZZ_TRANSFORMATION_ADD_PARAMETER_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_ADD_PARAMETER_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 TransformationAddParameter : public Transformation {
27  public:
28   explicit TransformationAddParameter(
29       const protobufs::TransformationAddParameter& message);
30 
31   TransformationAddParameter(uint32_t function_id, uint32_t parameter_fresh_id,
32                              uint32_t parameter_type_id,
33                              std::map<uint32_t, uint32_t> call_parameter_ids,
34                              uint32_t function_type_fresh_id);
35 
36   // - |function_id| must be a valid result id of some non-entry-point function
37   //   in the module.
38   // - |parameter_type_id| is a type id of the new parameter. The type must be
39   //   supported by this transformation as specified by IsParameterTypeSupported
40   //   function.
41   // - |call_parameter_id| must map from every id of an OpFunctionCall
42   //   instruction of this function to the id that will be passed as the new
43   //   parameter at that call site. There could be no callers, therefore this
44   //   map can be empty.
45   // - |parameter_fresh_id| and |function_type_fresh_id| are fresh ids and are
46   //   not equal.
47   bool IsApplicable(
48       opt::IRContext* ir_context,
49       const TransformationContext& transformation_context) const override;
50 
51   // - Creates a new OpFunctionParameter instruction with result id
52   //   |parameter_fresh_id| for the function with |function_id|.
53   // - Adjusts function's type to include a new parameter.
54   // - Adds an argument to every caller of the function to account for the added
55   //   parameter. The argument is the value in |call_parameter_id| map.
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   // Returns true if the type of the parameter is supported by this
64   // transformation.
65   static bool IsParameterTypeSupported(opt::IRContext* ir_context,
66                                        uint32_t type_id);
67 
68  private:
69   protobufs::TransformationAddParameter message_;
70 };
71 
72 }  // namespace fuzz
73 }  // namespace spvtools
74 
75 #endif  // SOURCE_FUZZ_TRANSFORMATION_ADD_PARAMETER_H_
76