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_FACT_MANAGER_CONSTANT_UNIFORM_FACTS_H_
16 #define SOURCE_FUZZ_FACT_MANAGER_CONSTANT_UNIFORM_FACTS_H_
17 
18 #include <vector>
19 
20 #include "source/fuzz/protobufs/spirvfuzz_protobufs.h"
21 #include "source/opt/ir_context.h"
22 
23 namespace spvtools {
24 namespace fuzz {
25 namespace fact_manager {
26 
27 // The purpose of this class is to group the fields and data used to represent
28 // facts about uniform constants.
29 class ConstantUniformFacts {
30  public:
31   explicit ConstantUniformFacts(opt::IRContext* ir_context);
32 
33   // See method in FactManager which delegates to this method.
34   bool MaybeAddFact(const protobufs::FactConstantUniform& fact);
35 
36   // See method in FactManager which delegates to this method.
37   std::vector<uint32_t> GetConstantsAvailableFromUniformsForType(
38       uint32_t type_id) const;
39 
40   // See method in FactManager which delegates to this method.
41   std::vector<protobufs::UniformBufferElementDescriptor>
42   GetUniformDescriptorsForConstant(uint32_t constant_id) const;
43 
44   // See method in FactManager which delegates to this method.
45   uint32_t GetConstantFromUniformDescriptor(
46       const protobufs::UniformBufferElementDescriptor& uniform_descriptor)
47       const;
48 
49   // See method in FactManager which delegates to this method.
50   std::vector<uint32_t> GetTypesForWhichUniformValuesAreKnown() const;
51 
52   // See method in FactManager which delegates to this method.
53   const std::vector<std::pair<protobufs::FactConstantUniform, uint32_t>>&
54   GetConstantUniformFactsAndTypes() const;
55 
56  private:
57   // Returns true if and only if the words associated with
58   // |constant_instruction| exactly match the words for the constant associated
59   // with |constant_uniform_fact|.
60   static bool DataMatches(
61       const opt::Instruction& constant_instruction,
62       const protobufs::FactConstantUniform& constant_uniform_fact);
63 
64   // Yields the constant words associated with |constant_uniform_fact|.
65   static std::vector<uint32_t> GetConstantWords(
66       const protobufs::FactConstantUniform& constant_uniform_fact);
67 
68   // Yields the id of a constant of type |type_id| whose data matches the
69   // constant data in |constant_uniform_fact|, or 0 if no such constant is
70   // declared.
71   uint32_t GetConstantId(
72       const protobufs::FactConstantUniform& constant_uniform_fact,
73       uint32_t type_id) const;
74 
75   // Checks that the width of a floating-point constant is supported, and that
76   // the constant is finite.
77   static bool FloatingPointValueIsSuitable(
78       const protobufs::FactConstantUniform& fact, uint32_t width);
79 
80   std::vector<std::pair<protobufs::FactConstantUniform, uint32_t>>
81       facts_and_type_ids_;
82 
83   opt::IRContext* ir_context_;
84 };
85 
86 }  // namespace fact_manager
87 }  // namespace fuzz
88 }  // namespace spvtools
89 
90 #endif  // SOURCE_FUZZ_FACT_MANAGER_CONSTANT_UNIFORM_FACTS_H_
91