• 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 #include "source/fuzz/transformation_add_global_variable.h"
16 
17 #include "source/fuzz/fuzzer_util.h"
18 
19 namespace spvtools {
20 namespace fuzz {
21 
TransformationAddGlobalVariable(const spvtools::fuzz::protobufs::TransformationAddGlobalVariable & message)22 TransformationAddGlobalVariable::TransformationAddGlobalVariable(
23     const spvtools::fuzz::protobufs::TransformationAddGlobalVariable& message)
24     : message_(message) {}
25 
TransformationAddGlobalVariable(uint32_t fresh_id,uint32_t type_id,SpvStorageClass storage_class,uint32_t initializer_id,bool value_is_irrelevant)26 TransformationAddGlobalVariable::TransformationAddGlobalVariable(
27     uint32_t fresh_id, uint32_t type_id, SpvStorageClass storage_class,
28     uint32_t initializer_id, bool value_is_irrelevant) {
29   message_.set_fresh_id(fresh_id);
30   message_.set_type_id(type_id);
31   message_.set_storage_class(storage_class);
32   message_.set_initializer_id(initializer_id);
33   message_.set_value_is_irrelevant(value_is_irrelevant);
34 }
35 
IsApplicable(opt::IRContext * ir_context,const TransformationContext &) const36 bool TransformationAddGlobalVariable::IsApplicable(
37     opt::IRContext* ir_context, const TransformationContext& /*unused*/) const {
38   // The result id must be fresh.
39   if (!fuzzerutil::IsFreshId(ir_context, message_.fresh_id())) {
40     return false;
41   }
42 
43   // The storage class must be Private or Workgroup.
44   auto storage_class = static_cast<SpvStorageClass>(message_.storage_class());
45   switch (storage_class) {
46     case SpvStorageClassPrivate:
47     case SpvStorageClassWorkgroup:
48       break;
49     default:
50       assert(false && "Unsupported storage class.");
51       return false;
52   }
53   // The type id must correspond to a type.
54   auto type = ir_context->get_type_mgr()->GetType(message_.type_id());
55   if (!type) {
56     return false;
57   }
58   // That type must be a pointer type ...
59   auto pointer_type = type->AsPointer();
60   if (!pointer_type) {
61     return false;
62   }
63   // ... with the right storage class.
64   if (pointer_type->storage_class() != storage_class) {
65     return false;
66   }
67   if (message_.initializer_id()) {
68     // An initializer is not allowed if the storage class is Workgroup.
69     if (storage_class == SpvStorageClassWorkgroup) {
70       assert(false &&
71              "By construction this transformation should not have an "
72              "initializer when Workgroup storage class is used.");
73       return false;
74     }
75     // The initializer id must be the id of a constant.  Check this with the
76     // constant manager.
77     auto constant_id = ir_context->get_constant_mgr()->GetConstantsFromIds(
78         {message_.initializer_id()});
79     if (constant_id.empty()) {
80       return false;
81     }
82     assert(constant_id.size() == 1 &&
83            "We asked for the constant associated with a single id; we should "
84            "get a single constant.");
85     // The type of the constant must match the pointee type of the pointer.
86     if (pointer_type->pointee_type() != constant_id[0]->type()) {
87       return false;
88     }
89   }
90   return true;
91 }
92 
Apply(opt::IRContext * ir_context,TransformationContext * transformation_context) const93 void TransformationAddGlobalVariable::Apply(
94     opt::IRContext* ir_context,
95     TransformationContext* transformation_context) const {
96   opt::Instruction::OperandList input_operands;
97   input_operands.push_back(
98       {SPV_OPERAND_TYPE_STORAGE_CLASS, {message_.storage_class()}});
99   if (message_.initializer_id()) {
100     input_operands.push_back(
101         {SPV_OPERAND_TYPE_ID, {message_.initializer_id()}});
102   }
103   ir_context->module()->AddGlobalValue(MakeUnique<opt::Instruction>(
104       ir_context, SpvOpVariable, message_.type_id(), message_.fresh_id(),
105       input_operands));
106   fuzzerutil::UpdateModuleIdBound(ir_context, message_.fresh_id());
107 
108   if (GlobalVariablesMustBeDeclaredInEntryPointInterfaces(ir_context)) {
109     // Conservatively add this global to the interface of every entry point in
110     // the module.  This means that the global is available for other
111     // transformations to use.
112     //
113     // A downside of this is that the global will be in the interface even if it
114     // ends up never being used.
115     //
116     // TODO(https://github.com/KhronosGroup/SPIRV-Tools/issues/3111) revisit
117     //  this if a more thorough approach to entry point interfaces is taken.
118     for (auto& entry_point : ir_context->module()->entry_points()) {
119       entry_point.AddOperand({SPV_OPERAND_TYPE_ID, {message_.fresh_id()}});
120     }
121   }
122 
123   if (message_.value_is_irrelevant()) {
124     transformation_context->GetFactManager()->AddFactValueOfPointeeIsIrrelevant(
125         message_.fresh_id());
126   }
127 
128   // We have added an instruction to the module, so need to be careful about the
129   // validity of existing analyses.
130   ir_context->InvalidateAnalysesExceptFor(
131       opt::IRContext::Analysis::kAnalysisNone);
132 }
133 
ToMessage() const134 protobufs::Transformation TransformationAddGlobalVariable::ToMessage() const {
135   protobufs::Transformation result;
136   *result.mutable_add_global_variable() = message_;
137   return result;
138 }
139 
140 bool TransformationAddGlobalVariable::
GlobalVariablesMustBeDeclaredInEntryPointInterfaces(opt::IRContext * ir_context)141     GlobalVariablesMustBeDeclaredInEntryPointInterfaces(
142         opt::IRContext* ir_context) {
143   // TODO(afd): We capture the universal environments for which this requirement
144   //  holds.  The check should be refined on demand for other target
145   //  environments.
146   switch (ir_context->grammar().target_env()) {
147     case SPV_ENV_UNIVERSAL_1_0:
148     case SPV_ENV_UNIVERSAL_1_1:
149     case SPV_ENV_UNIVERSAL_1_2:
150     case SPV_ENV_UNIVERSAL_1_3:
151       return false;
152     default:
153       return true;
154   }
155 }
156 
157 }  // namespace fuzz
158 }  // namespace spvtools
159