fold.h 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. // Copyright (c) 2017 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_FOLD_H_
  15. #define SOURCE_OPT_FOLD_H_
  16. #include <cstdint>
  17. #include <vector>
  18. #include "source/opt/const_folding_rules.h"
  19. #include "source/opt/constants.h"
  20. #include "source/opt/def_use_manager.h"
  21. #include "source/opt/folding_rules.h"
  22. namespace spvtools {
  23. namespace opt {
  24. class InstructionFolder {
  25. public:
  26. explicit InstructionFolder(IRContext* context)
  27. : context_(context),
  28. const_folding_rules_(new ConstantFoldingRules(context)),
  29. folding_rules_(new FoldingRules(context)) {
  30. folding_rules_->AddFoldingRules();
  31. const_folding_rules_->AddFoldingRules();
  32. }
  33. explicit InstructionFolder(
  34. IRContext* context, std::unique_ptr<FoldingRules>&& folding_rules,
  35. std::unique_ptr<ConstantFoldingRules>&& constant_folding_rules)
  36. : context_(context),
  37. const_folding_rules_(std::move(constant_folding_rules)),
  38. folding_rules_(std::move(folding_rules)) {
  39. folding_rules_->AddFoldingRules();
  40. const_folding_rules_->AddFoldingRules();
  41. }
  42. // Returns the result of folding a scalar instruction with the given |opcode|
  43. // and |operands|. Each entry in |operands| is a pointer to an
  44. // analysis::Constant instance, which should've been created with the constant
  45. // manager (See IRContext::get_constant_mgr).
  46. //
  47. // It is an error to call this function with an opcode that does not pass the
  48. // IsFoldableOpcode test. If any error occurs during folding, the folder will
  49. // fail with a call to assert.
  50. uint32_t FoldScalars(
  51. spv::Op opcode,
  52. const std::vector<const analysis::Constant*>& operands) const;
  53. // Returns the result of performing an operation with the given |opcode| over
  54. // constant vectors with |num_dims| dimensions. Each entry in |operands| is a
  55. // pointer to an analysis::Constant instance, which should've been created
  56. // with the constant manager (See IRContext::get_constant_mgr).
  57. //
  58. // This function iterates through the given vector type constant operands and
  59. // calculates the result for each element of the result vector to return.
  60. // Vectors with longer than 32-bit scalar components are not accepted in this
  61. // function.
  62. //
  63. // It is an error to call this function with an opcode that does not pass the
  64. // IsFoldableOpcode test. If any error occurs during folding, the folder will
  65. // fail with a call to assert.
  66. std::vector<uint32_t> FoldVectors(
  67. spv::Op opcode, uint32_t num_dims,
  68. const std::vector<const analysis::Constant*>& operands) const;
  69. // Returns true if |opcode| represents an operation handled by FoldScalars or
  70. // FoldVectors.
  71. bool IsFoldableOpcode(spv::Op opcode) const;
  72. // Returns true if |cst| is supported by FoldScalars and FoldVectors.
  73. bool IsFoldableConstant(const analysis::Constant* cst) const;
  74. // Returns true if |FoldInstructionToConstant| could fold an instruction whose
  75. // result type is |type_inst|.
  76. bool IsFoldableType(Instruction* type_inst) const;
  77. // Returns true if |FoldInstructionToConstant| could fold an instruction whose
  78. // result type is |type_inst|.
  79. bool IsFoldableScalarType(Instruction* type_inst) const;
  80. // Returns true if |FoldInstructionToConstant| could fold an instruction whose
  81. // result type is |type_inst|.
  82. bool IsFoldableVectorType(Instruction* type_inst) const;
  83. // Tries to fold |inst| to a single constant, when the input ids to |inst|
  84. // have been substituted using |id_map|. Returns a pointer to the OpConstant*
  85. // instruction if successful. If necessary, a new constant instruction is
  86. // created and placed in the global values section.
  87. //
  88. // |id_map| is a function that takes one result id and returns another. It
  89. // can be used for things like CCP where it is known that some ids contain a
  90. // constant, but the instruction itself has not been updated yet. This can
  91. // map those ids to the appropriate constants.
  92. Instruction* FoldInstructionToConstant(
  93. Instruction* inst, std::function<uint32_t(uint32_t)> id_map) const;
  94. // Returns true if |inst| can be folded into a simpler instruction.
  95. // If |inst| can be simplified, |inst| is overwritten with the simplified
  96. // instruction reusing the same result id.
  97. //
  98. // If |inst| is simplified, it is possible that the resulting code in invalid
  99. // because the instruction is in a bad location. Callers of this function
  100. // have to handle the following cases:
  101. //
  102. // 1) An OpPhi becomes and OpCopyObject - If there are OpPhi instruction after
  103. // |inst| in a basic block then this is invalid. The caller must fix this
  104. // up.
  105. bool FoldInstruction(Instruction* inst) const;
  106. // Return true if this opcode has a const folding rule associtated with it.
  107. bool HasConstFoldingRule(const Instruction* inst) const {
  108. return GetConstantFoldingRules().HasFoldingRule(inst);
  109. }
  110. private:
  111. // Returns a reference to the ConstnatFoldingRules instance.
  112. const ConstantFoldingRules& GetConstantFoldingRules() const {
  113. return *const_folding_rules_;
  114. }
  115. // Returns a reference to the FoldingRules instance.
  116. const FoldingRules& GetFoldingRules() const { return *folding_rules_; }
  117. // Returns the single-word result from performing the given unary operation on
  118. // the operand value which is passed in as a 32-bit word.
  119. uint32_t UnaryOperate(spv::Op opcode, uint32_t operand) const;
  120. // Returns the single-word result from performing the given binary operation
  121. // on the operand values which are passed in as two 32-bit word.
  122. uint32_t BinaryOperate(spv::Op opcode, uint32_t a, uint32_t b) const;
  123. // Returns the single-word result from performing the given ternary operation
  124. // on the operand values which are passed in as three 32-bit word.
  125. uint32_t TernaryOperate(spv::Op opcode, uint32_t a, uint32_t b,
  126. uint32_t c) const;
  127. // Returns the single-word result from performing the given operation on the
  128. // operand words. This only works with 32-bit operations and uses boolean
  129. // convention that 0u is false, and anything else is boolean true.
  130. // TODO(qining): Support operands other than 32-bit wide.
  131. uint32_t OperateWords(spv::Op opcode,
  132. const std::vector<uint32_t>& operand_words) const;
  133. bool FoldInstructionInternal(Instruction* inst) const;
  134. // Returns true if |inst| is a binary operation that takes two integers as
  135. // parameters and folds to a constant that can be represented as an unsigned
  136. // 32-bit value when the ids have been replaced by |id_map|. If |inst| can be
  137. // folded, the resulting value is returned in |*result|. Valid result types
  138. // for the instruction are any integer (signed or unsigned) with 32-bits or
  139. // less, or a boolean value.
  140. bool FoldBinaryIntegerOpToConstant(
  141. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  142. uint32_t* result) const;
  143. // Returns true if |inst| is a binary operation on two boolean values, and
  144. // folds
  145. // to a constant boolean value when the ids have been replaced using |id_map|.
  146. // If |inst| can be folded, the result value is returned in |*result|.
  147. bool FoldBinaryBooleanOpToConstant(
  148. Instruction* inst, const std::function<uint32_t(uint32_t)>& id_map,
  149. uint32_t* result) const;
  150. // Returns true if |inst| can be folded to an constant when the ids have been
  151. // substituted using id_map. If it can, the value is returned in |result|. If
  152. // not, |result| is unchanged. It is assumed that not all operands are
  153. // constant. Those cases are handled by |FoldScalar|.
  154. bool FoldIntegerOpToConstant(Instruction* inst,
  155. const std::function<uint32_t(uint32_t)>& id_map,
  156. uint32_t* result) const;
  157. IRContext* context_;
  158. // Folding rules used by |FoldInstructionToConstant| and |FoldInstruction|.
  159. std::unique_ptr<ConstantFoldingRules> const_folding_rules_;
  160. // Folding rules used by |FoldInstruction|.
  161. std::unique_ptr<FoldingRules> folding_rules_;
  162. };
  163. } // namespace opt
  164. } // namespace spvtools
  165. #endif // SOURCE_OPT_FOLD_H_