1 | // Copyright (c) 2016 Google Inc. |
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_OPT_SET_SPEC_CONSTANT_DEFAULT_VALUE_PASS_H_ |
16 | #define SOURCE_OPT_SET_SPEC_CONSTANT_DEFAULT_VALUE_PASS_H_ |
17 | |
18 | #include <memory> |
19 | #include <string> |
20 | #include <unordered_map> |
21 | #include <utility> |
22 | #include <vector> |
23 | |
24 | #include "source/opt/ir_context.h" |
25 | #include "source/opt/module.h" |
26 | #include "source/opt/pass.h" |
27 | |
28 | namespace spvtools { |
29 | namespace opt { |
30 | |
31 | // See optimizer.hpp for documentation. |
32 | class SetSpecConstantDefaultValuePass : public Pass { |
33 | public: |
34 | using SpecIdToValueStrMap = std::unordered_map<uint32_t, std::string>; |
35 | using SpecIdToValueBitPatternMap = |
36 | std::unordered_map<uint32_t, std::vector<uint32_t>>; |
37 | using SpecIdToInstMap = std::unordered_map<uint32_t, Instruction*>; |
38 | |
39 | // Constructs a pass instance with a map from spec ids to default values |
40 | // in the form of string. |
41 | explicit SetSpecConstantDefaultValuePass( |
42 | const SpecIdToValueStrMap& default_values) |
43 | : spec_id_to_value_str_(default_values), |
44 | spec_id_to_value_bit_pattern_() {} |
45 | explicit SetSpecConstantDefaultValuePass(SpecIdToValueStrMap&& default_values) |
46 | : spec_id_to_value_str_(std::move(default_values)), |
47 | spec_id_to_value_bit_pattern_() {} |
48 | |
49 | // Constructs a pass instance with a map from spec ids to default values in |
50 | // the form of bit pattern. |
51 | explicit SetSpecConstantDefaultValuePass( |
52 | const SpecIdToValueBitPatternMap& default_values) |
53 | : spec_id_to_value_str_(), |
54 | spec_id_to_value_bit_pattern_(default_values) {} |
55 | explicit SetSpecConstantDefaultValuePass( |
56 | SpecIdToValueBitPatternMap&& default_values) |
57 | : spec_id_to_value_str_(), |
58 | spec_id_to_value_bit_pattern_(std::move(default_values)) {} |
59 | |
60 | const char* name() const override { return "set-spec-const-default-value" ; } |
61 | Status Process() override; |
62 | |
63 | // Parses the given null-terminated C string to get a mapping from Spec Id to |
64 | // default value strings. Returns a unique pointer of the mapping from spec |
65 | // ids to spec constant default value strings built from the given |str| on |
66 | // success. Returns a nullptr if the given string is not valid for building |
67 | // the mapping. |
68 | // A valid string for building the mapping should follow the rule below: |
69 | // |
70 | // "<spec id A>:<default value for A> <spec id B>:<default value for B> ..." |
71 | // Example: |
72 | // "200:0x11 201:3.14 202:1.4728" |
73 | // |
74 | // Entries are separated with blank spaces (i.e.:' ', '\n', '\r', '\t', |
75 | // '\f', '\v'). Each entry corresponds to a Spec Id and default value pair. |
76 | // Multiple spaces between, before or after entries are allowed. However, |
77 | // spaces are not allowed within spec id or the default value string because |
78 | // spaces are always considered as delimiter to separate entries. |
79 | // |
80 | // In each entry, the spec id and value string is separated by ':'. Missing |
81 | // ':' in any entry is invalid. And it is invalid to have blank spaces in |
82 | // between the spec id and ':' or the default value and ':'. |
83 | // |
84 | // <spec id>: specifies the spec id value. |
85 | // The text must represent a valid uint32_t number. |
86 | // Hex format with '0x' prefix is allowed. |
87 | // Empty <spec id> is not allowed. |
88 | // One spec id value can only be defined once, multiple default values |
89 | // defined for the same spec id is not allowed. Spec ids with same value |
90 | // but different formats (e.g. 0x100 and 256) are considered the same. |
91 | // |
92 | // <default value>: the default value string. |
93 | // Spaces before and after default value text is allowed. |
94 | // Spaces within the text is not allowed. |
95 | // Empty <default value> is not allowed. |
96 | static std::unique_ptr<SpecIdToValueStrMap> ParseDefaultValuesString( |
97 | const char* str); |
98 | |
99 | private: |
100 | // The mappings from spec ids to default values. Two maps are defined here, |
101 | // each to be used for one specific form of the default values. Only one of |
102 | // them will be populated in practice. |
103 | |
104 | // The mapping from spec ids to their string-form default values to be set. |
105 | const SpecIdToValueStrMap spec_id_to_value_str_; |
106 | // The mapping from spec ids to their bitpattern-form default values to be |
107 | // set. |
108 | const SpecIdToValueBitPatternMap spec_id_to_value_bit_pattern_; |
109 | }; |
110 | |
111 | } // namespace opt |
112 | } // namespace spvtools |
113 | |
114 | #endif // SOURCE_OPT_SET_SPEC_CONSTANT_DEFAULT_VALUE_PASS_H_ |
115 | |