| 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_REPLACE_PARAMETER_WITH_GLOBAL_H_ |
| 16 |
#define SOURCE_FUZZ_TRANSFORMATION_REPLACE_PARAMETER_WITH_GLOBAL_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 TransformationReplaceParameterWithGlobal : public Transformation { |
| 27 |
public: |
| 28 |
explicit TransformationReplaceParameterWithGlobal( |
| 29 |
const protobufs::TransformationReplaceParameterWithGlobal& message); |
| 30 |
|
| 31 |
TransformationReplaceParameterWithGlobal(uint32_t function_type_fresh_id, |
| 32 |
uint32_t parameter_id, |
| 33 |
uint32_t global_variable_fresh_id); |
| 34 |
|
| 35 |
// - |function_type_fresh_id| is a fresh id. |
| 36 |
// - |parameter_id| is the result id of the parameter to replace. |
| 37 |
// - |global_variable_fresh_id| is a fresh id. |
| 38 |
// - |function_type_fresh_id| is not equal to |global_variable_fresh_id|. |
| 39 |
// - the function that contains |parameter_id| may not be an entry-point |
| 40 |
// function. |
| 41 |
bool IsApplicable( |
| 42 |
opt::IRContext* ir_context, |
| 43 |
const TransformationContext& transformation_context) const override; |
| 44 |
|
| 45 |
// - Removes parameter with result id |parameter_id| from its function |
| 46 |
// - Adds a global variable to store the value for the parameter |
| 47 |
// - Add an OpStore instruction before each function call to |
| 48 |
// store parameter's value into the variable |
| 49 |
// - Adds OpLoad at the beginning of the function to load the |
| 50 |
// value from the variable into the old parameter's id |
| 51 |
void Apply(opt::IRContext* ir_context, |
| 52 |
TransformationContext* transformation_context) const override; |
| 53 |
|
| 54 |
std::unordered_set<uint32_t> GetFreshIds() const override; |
| 55 |
|
| 56 |
protobufs::Transformation ToMessage() const override; |
| 57 |
|
| 58 |
// Returns true if the type of the parameter is supported by this |
| 59 |
// transformation. |
| 60 |
static bool IsParameterTypeSupported(opt::IRContext* ir_context, |
| 61 |
uint32_t param_type_id); |
| 62 |
|
| 63 |
private: |
| 64 |
protobufs::TransformationReplaceParameterWithGlobal message_; |
| 65 |
}; |
| 66 |
|
| 67 |
} // namespace fuzz |
| 68 |
} // namespace spvtools |
| 69 |
|
| 70 |
#endif // SOURCE_FUZZ_TRANSFORMATION_REPLACE_PARAMETER_WITH_GLOBAL_H_ |