folding_rules.h 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. // Copyright (c) 2018 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. #ifndef SOURCE_OPT_FOLDING_RULES_H_
  15. #define SOURCE_OPT_FOLDING_RULES_H_
  16. #include <cstdint>
  17. #include <unordered_map>
  18. #include <vector>
  19. #include "source/opt/constants.h"
  20. namespace spvtools {
  21. namespace opt {
  22. // Folding Rules:
  23. //
  24. // The folding mechanism is built around the concept of a |FoldingRule|. A
  25. // folding rule is a function that implements a method of simplifying an
  26. // instruction.
  27. //
  28. // The inputs to a folding rule are:
  29. // |inst| - the instruction to be simplified.
  30. // |constants| - if an in-operands is an id of a constant, then the
  31. // corresponding value in |constants| contains that
  32. // constant value. Otherwise, the corresponding entry in
  33. // |constants| is |nullptr|.
  34. //
  35. // A folding rule returns true if |inst| can be simplified using this rule. If
  36. // the instruction can be simplified, then |inst| is changed to the simplified
  37. // instruction. Otherwise, |inst| remains the same.
  38. //
  39. // See folding_rules.cpp for examples on how to write a folding rule. It is
  40. // important to note that if |inst| can be folded to the result of an
  41. // instruction that feed it, then |inst| should be changed to an OpCopyObject
  42. // that copies that id.
  43. //
  44. // Be sure to add new folding rules to the table of folding rules in the
  45. // constructor for FoldingRules. The new rule should be added to the list for
  46. // every opcode that it applies to. Note that earlier rules in the list are
  47. // given priority. That is, if an earlier rule is able to fold an instruction,
  48. // the later rules will not be attempted.
  49. using FoldingRule = std::function<bool(
  50. IRContext* context, Instruction* inst,
  51. const std::vector<const analysis::Constant*>& constants)>;
  52. class FoldingRules {
  53. public:
  54. using FoldingRuleSet = std::vector<FoldingRule>;
  55. explicit FoldingRules(IRContext* ctx) : context_(ctx) {}
  56. virtual ~FoldingRules() = default;
  57. const FoldingRuleSet& GetRulesForInstruction(Instruction* inst) const {
  58. if (inst->opcode() != spv::Op::OpExtInst) {
  59. auto it = rules_.find(inst->opcode());
  60. if (it != rules_.end()) {
  61. return it->second;
  62. }
  63. } else {
  64. uint32_t ext_inst_id = inst->GetSingleWordInOperand(0);
  65. uint32_t ext_opcode = inst->GetSingleWordInOperand(1);
  66. auto it = ext_rules_.find({ext_inst_id, ext_opcode});
  67. if (it != ext_rules_.end()) {
  68. return it->second;
  69. }
  70. }
  71. return empty_vector_;
  72. }
  73. IRContext* context() { return context_; }
  74. // Adds the folding rules for the object.
  75. virtual void AddFoldingRules();
  76. protected:
  77. struct hasher {
  78. size_t operator()(const spv::Op& op) const noexcept {
  79. return std::hash<uint32_t>()(uint32_t(op));
  80. }
  81. };
  82. // The folding rules for core instructions.
  83. std::unordered_map<spv::Op, FoldingRuleSet, hasher> rules_;
  84. // The folding rules for extended instructions.
  85. struct Key {
  86. uint32_t instruction_set;
  87. uint32_t opcode;
  88. };
  89. friend bool operator<(const Key& a, const Key& b) {
  90. if (a.instruction_set < b.instruction_set) {
  91. return true;
  92. }
  93. if (a.instruction_set > b.instruction_set) {
  94. return false;
  95. }
  96. return a.opcode < b.opcode;
  97. }
  98. std::map<Key, FoldingRuleSet> ext_rules_;
  99. private:
  100. IRContext* context_;
  101. FoldingRuleSet empty_vector_;
  102. };
  103. } // namespace opt
  104. } // namespace spvtools
  105. #endif // SOURCE_OPT_FOLDING_RULES_H_