set_spec_constant_default_value_pass.h 4.7 KB

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