|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +/*! |
| 21 | + * \file data_type_rewriter.h |
| 22 | + * \brief Rewrite the data type of expressions. |
| 23 | + */ |
| 24 | +#ifndef TVM_TIR_DATA_TYPE_REWRITER_H_ |
| 25 | +#define TVM_TIR_DATA_TYPE_REWRITER_H_ |
| 26 | + |
| 27 | +#include <tvm/tir/stmt_functor.h> |
| 28 | + |
| 29 | +#include <unordered_map> |
| 30 | + |
| 31 | +namespace tvm { |
| 32 | +namespace tir { |
| 33 | + |
| 34 | +/*! |
| 35 | + * \brief Legalize the data types of expressions to make sure they are consistent with other |
| 36 | + * parts of the program. |
| 37 | + * |
| 38 | + * It enforces the following rules: |
| 39 | + * - The data type of the index variable in a loop must be consistent with the data type of the loop |
| 40 | + * bounds. |
| 41 | + * - The data type of the binary and ternary expressions must be consistent with the data types of |
| 42 | + * each of their operands. |
| 43 | + * - The data type of the bounds and binding values of block iter vars must be consistent with the |
| 44 | + * data type of the block iter vars. |
| 45 | + * |
| 46 | + * Usually we enforce the consistency of data types when constructing the IR nodes. However, such |
| 47 | + * inconsistency may happen as a result of IR mutation in some passes. This class can be used as |
| 48 | + * base class of such passes to ensure the consistency of data types. |
| 49 | + */ |
| 50 | +class DataTypeLegalizer : public StmtExprMutator { |
| 51 | + protected: |
| 52 | + Stmt VisitStmt_(const ForNode* op) override; |
| 53 | + Stmt VisitStmt_(const AttrStmtNode* op) override; |
| 54 | + Stmt VisitStmt_(const BlockRealizeNode* op) override; |
| 55 | + Stmt VisitStmt_(const BlockNode* op) override; |
| 56 | + PrimExpr VisitExpr_(const SelectNode* op) override; |
| 57 | + PrimExpr VisitExpr_(const RampNode* op) override; |
| 58 | + PrimExpr VisitExpr_(const AddNode* op) override; |
| 59 | + PrimExpr VisitExpr_(const SubNode* op) override; |
| 60 | + PrimExpr VisitExpr_(const MulNode* op) override; |
| 61 | + PrimExpr VisitExpr_(const DivNode* op) override; |
| 62 | + PrimExpr VisitExpr_(const ModNode* op) override; |
| 63 | + PrimExpr VisitExpr_(const FloorDivNode* op) override; |
| 64 | + PrimExpr VisitExpr_(const FloorModNode* op) override; |
| 65 | + PrimExpr VisitExpr_(const MinNode* op) override; |
| 66 | + PrimExpr VisitExpr_(const MaxNode* op) override; |
| 67 | + PrimExpr VisitExpr_(const EQNode* op) override; |
| 68 | + PrimExpr VisitExpr_(const NENode* op) override; |
| 69 | + PrimExpr VisitExpr_(const LTNode* op) override; |
| 70 | + PrimExpr VisitExpr_(const LENode* op) override; |
| 71 | + PrimExpr VisitExpr_(const GTNode* op) override; |
| 72 | + PrimExpr VisitExpr_(const GENode* op) override; |
| 73 | + PrimExpr VisitExpr_(const CallNode* op) override; |
| 74 | + |
| 75 | + using StmtExprMutator::VisitExpr_; |
| 76 | + using StmtExprMutator::VisitStmt_; |
| 77 | + |
| 78 | + // a map from IterVar before rewrite to that after rewrite, |
| 79 | + // ensures one old IterVar maps to exactly one new IterVar |
| 80 | + std::unordered_map<const IterVarNode*, IterVar> ivmap_; |
| 81 | +}; |
| 82 | + |
| 83 | +/*! |
| 84 | + * \brief Data type rewriter for buffer indices. |
| 85 | + * |
| 86 | + * Detect the components of buffer indices that should be considered for data type rewriting. |
| 87 | + * This class doesn't perform actual rewriting of data types. During recursive visiting, the |
| 88 | + * internal flags `is_enabled_` and `is_conditional_` are used to indicate whether the current |
| 89 | + * expression is a buffer index or a conditional expression, which can be used in the sub-classes to |
| 90 | + * implement different rewriting rules. |
| 91 | + */ |
| 92 | +class IndexDataTypeRewriter : public DataTypeLegalizer { |
| 93 | + using Parent = DataTypeLegalizer; |
| 94 | + |
| 95 | + protected: |
| 96 | + Stmt VisitStmt_(const BlockRealizeNode* op) override; |
| 97 | + Stmt VisitStmt_(const BlockNode* op) override; |
| 98 | + Stmt VisitStmt_(const BufferStoreNode* op) override; |
| 99 | + PrimExpr VisitExpr_(const BufferLoadNode* op) override; |
| 100 | + Array<PrimExpr> VisitIndices(Array<PrimExpr> indices); |
| 101 | + Stmt VisitStmt_(const IfThenElseNode* op) override; |
| 102 | + Stmt VisitStmt_(const DeclBufferNode* op) override; |
| 103 | + Stmt VisitStmt_(const AllocateNode* op) override; |
| 104 | + PrimExpr VisitExpr_(const EQNode* op) override; |
| 105 | + PrimExpr VisitExpr_(const NENode* op) override; |
| 106 | + PrimExpr VisitExpr_(const LTNode* op) override; |
| 107 | + PrimExpr VisitExpr_(const LENode* op) override; |
| 108 | + PrimExpr VisitExpr_(const GTNode* op) override; |
| 109 | + PrimExpr VisitExpr_(const GENode* op) override; |
| 110 | + PrimExpr VisitExpr_(const CallNode* op) override; |
| 111 | + Stmt VisitStmt_(const ForNode* op) override; |
| 112 | + |
| 113 | + using DataTypeLegalizer::VisitExpr_; |
| 114 | + using DataTypeLegalizer::VisitStmt_; |
| 115 | + |
| 116 | + Buffer VisitBuffer(const Buffer& buffer); |
| 117 | + Buffer GetRemappedBuffer(const Buffer& buffer); |
| 118 | + Map<String, ObjectRef> VisitBlockAnnotations(const Map<String, ObjectRef>& annotations); |
| 119 | + BufferRegion VisitBufferRegion(const BufferRegion& region); |
| 120 | + IterVar VisitIterVar(const IterVar& iter_var); |
| 121 | + // indicator of index expr to rewrite |
| 122 | + bool is_enabled_{false}; |
| 123 | + // indicator of condition |
| 124 | + bool is_condition_{false}; |
| 125 | + |
| 126 | + Map<Var, Var> var_remap_; |
| 127 | + Map<Buffer, Buffer> buffer_remap_; |
| 128 | +}; |
| 129 | + |
| 130 | +/*! |
| 131 | + * \brief Normalize the data types of buffer shapes and indices to the same data type. |
| 132 | + * |
| 133 | + * This pass rewrites the data types of buffer shapes and indices to the specified data type. It |
| 134 | + * assumes the specified data type is large enough to hold the original ranges of buffer shapes and |
| 135 | + * indices. |
| 136 | + */ |
| 137 | +class IndexDataTypeNormalizer : public IndexDataTypeRewriter { |
| 138 | + public: |
| 139 | + explicit IndexDataTypeNormalizer(DataType target_data_type); |
| 140 | + PrimFunc Rewrite(PrimFunc func); |
| 141 | + |
| 142 | + private: |
| 143 | + PrimExpr VisitExpr_(const IntImmNode* op) final; |
| 144 | + PrimExpr VisitExpr_(const VarNode* op) final; |
| 145 | + PrimExpr VisitExpr_(const SizeVarNode* op) final; |
| 146 | + |
| 147 | + DataType target_data_type_ = DataType::Int(64); |
| 148 | +}; |
| 149 | + |
| 150 | +} // namespace tir |
| 151 | +} // namespace tvm |
| 152 | + |
| 153 | +#endif // TVM_TIR_DATA_TYPE_REWRITER_H_ |
0 commit comments