• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_ADD_GLOBAL_VARIABLE_H_
16 #define SOURCE_FUZZ_TRANSFORMATION_ADD_GLOBAL_VARIABLE_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 TransformationAddGlobalVariable : public Transformation {
27  public:
28   explicit TransformationAddGlobalVariable(
29       protobufs::TransformationAddGlobalVariable message);
30 
31   TransformationAddGlobalVariable(uint32_t fresh_id, uint32_t type_id,
32                                   SpvStorageClass storage_class,
33                                   uint32_t initializer_id,
34                                   bool value_is_irrelevant);
35 
36   // - |message_.fresh_id| must be fresh
37   // - |message_.type_id| must be the id of a pointer type with the same storage
38   //   class as |message_.storage_class|
39   // - |message_.storage_class| must be Private or Workgroup
40   // - |message_.initializer_id| must be 0 if |message_.storage_class| is
41   //   Workgroup, and otherwise may either be 0 or the id of a constant whose
42   //   type is the pointee type of |message_.type_id|
43   bool IsApplicable(
44       opt::IRContext* ir_context,
45       const TransformationContext& transformation_context) const override;
46 
47   // Adds a global variable with storage class |message_.storage_class| to the
48   // module, with type |message_.type_id| and either no initializer or
49   // |message_.initializer_id| as an initializer, depending on whether
50   // |message_.initializer_id| is 0.  The global variable has result id
51   // |message_.fresh_id|.
52   //
53   // If |message_.value_is_irrelevant| holds, adds a corresponding fact to the
54   // fact manager in |transformation_context|.
55   void Apply(opt::IRContext* ir_context,
56              TransformationContext* transformation_context) const override;
57 
58   std::unordered_set<uint32_t> GetFreshIds() const override;
59 
60   protobufs::Transformation ToMessage() const override;
61 
62  private:
63   protobufs::TransformationAddGlobalVariable message_;
64 };
65 
66 }  // namespace fuzz
67 }  // namespace spvtools
68 
69 #endif  // SOURCE_FUZZ_TRANSFORMATION_ADD_GLOBAL_VARIABLE_H_
70