| 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_COMPOSITE_CONSTRUCT_H_ |
| 16 |
#define SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_CONSTRUCT_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 TransformationCompositeConstruct : public Transformation { |
| 27 |
public: |
| 28 |
explicit TransformationCompositeConstruct( |
| 29 |
const protobufs::TransformationCompositeConstruct& message); |
| 30 |
|
| 31 |
TransformationCompositeConstruct( |
| 32 |
uint32_t composite_type_id, std::vector<uint32_t> component, |
| 33 |
const protobufs::InstructionDescriptor& instruction_to_insert_before, |
| 34 |
uint32_t fresh_id); |
| 35 |
|
| 36 |
// - |message_.fresh_id| must not be used by the module. |
| 37 |
// - |message_.composite_type_id| must be the id of a composite type |
| 38 |
// - The elements of |message_.component| must be result ids that are |
| 39 |
// suitable for constructing an element of the given composite type, in |
| 40 |
// order |
| 41 |
// - The elements of |message_.component| must not be the target of any |
| 42 |
// decorations. |
| 43 |
// - |message_.base_instruction_id| must be the result id of an instruction |
| 44 |
// 'base' in some block 'blk'. |
| 45 |
// - 'blk' must contain an instruction 'inst' located |message_.offset| |
| 46 |
// instructions after 'base' (if |message_.offset| = 0 then 'inst' = |
| 47 |
// 'base'). |
| 48 |
// - It must be legal to insert an OpCompositeConstruct instruction directly |
| 49 |
// before 'inst'. |
| 50 |
// - Each element of |message_.component| must be available directly before |
| 51 |
// 'inst'. |
| 52 |
bool IsApplicable( |
| 53 |
opt::IRContext* ir_context, |
| 54 |
const TransformationContext& transformation_context) const override; |
| 55 |
|
| 56 |
// Inserts a new OpCompositeConstruct instruction, with id |
| 57 |
// |message_.fresh_id|, directly before the instruction identified by |
| 58 |
// |message_.base_instruction_id| and |message_.offset|. The instruction |
| 59 |
// creates a composite of type |message_.composite_type_id| using the ids of |
| 60 |
// |message_.component|. |
| 61 |
// |
| 62 |
// Synonym facts are added between the elements of the resulting composite |
| 63 |
// and the components used to construct it, as long as the associated ids |
| 64 |
// support synonym creation. |
| 65 |
void Apply(opt::IRContext* ir_context, |
| 66 |
TransformationContext* transformation_context) const override; |
| 67 |
|
| 68 |
std::unordered_set<uint32_t> GetFreshIds() const override; |
| 69 |
|
| 70 |
protobufs::Transformation ToMessage() const override; |
| 71 |
|
| 72 |
private: |
| 73 |
// Helper to decide whether the components of the transformation are suitable |
| 74 |
// for constructing an array of the given type. |
| 75 |
bool ComponentsForArrayConstructionAreOK( |
| 76 |
opt::IRContext* ir_context, const opt::analysis::Array& array_type) const; |
| 77 |
|
| 78 |
// Similar, but for matrices. |
| 79 |
bool ComponentsForMatrixConstructionAreOK( |
| 80 |
opt::IRContext* ir_context, |
| 81 |
const opt::analysis::Matrix& matrix_type) const; |
| 82 |
|
| 83 |
// Similar, but for structs. |
| 84 |
bool ComponentsForStructConstructionAreOK( |
| 85 |
opt::IRContext* ir_context, |
| 86 |
const opt::analysis::Struct& struct_type) const; |
| 87 |
|
| 88 |
// Similar, but for vectors. |
| 89 |
bool ComponentsForVectorConstructionAreOK( |
| 90 |
opt::IRContext* ir_context, |
| 91 |
const opt::analysis::Vector& vector_type) const; |
| 92 |
|
| 93 |
// Helper method for adding data synonym facts when applying the |
| 94 |
// transformation to |ir_context| and |transformation_context|. |
| 95 |
void AddDataSynonymFacts(opt::IRContext* ir_context, |
| 96 |
TransformationContext* transformation_context) const; |
| 97 |
|
| 98 |
protobufs::TransformationCompositeConstruct message_; |
| 99 |
}; |
| 100 |
|
| 101 |
} // namespace fuzz |
| 102 |
} // namespace spvtools |
| 103 |
|
| 104 |
#endif // SOURCE_FUZZ_TRANSFORMATION_COMPOSITE_CONSTRUCT_H_ |