tvm
iter_affine_map.h
Go to the documentation of this file.
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 
48 #ifndef TVM_ARITH_ITER_AFFINE_MAP_H_
49 #define TVM_ARITH_ITER_AFFINE_MAP_H_
50 
51 #include <tvm/arith/analyzer.h>
52 #include <tvm/ir/expr.h>
53 #include <tvm/tir/var.h>
54 
55 namespace tvm {
56 namespace arith {
57 
65 class IterMapExprNode : public PrimExprNode {
66  public:
67  // overrides
69 
70  static constexpr const char* _type_key = "arith.IterMapExpr";
71  static constexpr const uint32_t _type_child_slots = 3;
73 };
74 
79 class IterMapExpr : public PrimExpr {
80  public:
82 };
83 
90 class IterMarkNode : public Object {
91  public:
101 
102  // overrides
104  v->Visit("source", &source);
105  v->Visit("extent", &extent);
106  }
107 
108  bool SEqualReduce(const IterMarkNode* other, SEqualReducer equal) const {
109  equal->MarkGraphNode();
110  return equal(source, other->source) && equal(extent, other->extent);
111  }
112 
113  void SHashReduce(SHashReducer hash_reduce) const {
114  hash_reduce->MarkGraphNode();
115  hash_reduce(source);
116  hash_reduce(extent);
117  }
118 
119  static constexpr const bool _type_has_method_sequal_reduce = true;
120  static constexpr const bool _type_has_method_shash_reduce = true;
121  static constexpr const char* _type_key = "arith.IterMark";
123 };
124 
129 class IterMark : public ObjectRef {
130  public:
136  TVM_DLL IterMark(PrimExpr source, PrimExpr extent);
137 
140 };
141 
148  public:
157 
158  // overrides
160  v->Visit("source", &source);
161  v->Visit("lower_factor", &lower_factor);
162  v->Visit("extent", &extent);
163  v->Visit("scale", &scale);
164  }
165 
166  bool SEqualReduce(const IterSplitExprNode* other, SEqualReducer equal) const {
167  return equal(source, other->source) && equal(lower_factor, other->lower_factor) &&
168  equal(extent, other->extent) && equal(scale, other->scale);
169  }
170 
171  void SHashReduce(SHashReducer hash_reduce) const {
172  hash_reduce(source);
173  hash_reduce(lower_factor);
174  hash_reduce(extent);
175  hash_reduce(scale);
176  }
177 
178  static constexpr const char* _type_key = "arith.IterSplitExpr";
180 };
181 
186 class IterSplitExpr : public IterMapExpr {
187  public:
192  TVM_DLL explicit IterSplitExpr(IterMark source);
198  TVM_DLL explicit IterSplitExpr(IterMark source, PrimExpr scale);
206  TVM_DLL explicit IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent,
207  PrimExpr scale);
208 
211 };
212 
219  public:
224 
225  // overrides
227  v->Visit("args", &args);
228  v->Visit("base", &base);
229  }
230 
231  bool SEqualReduce(const IterSumExprNode* other, SEqualReducer equal) const {
232  return equal(args, other->args) && equal(base, other->base);
233  }
234 
235  void SHashReduce(SHashReducer hash_reduce) const {
236  hash_reduce(args);
237  hash_reduce(base);
238  }
239 
240  static constexpr const char* _type_key = "arith.IterSumExpr";
242 };
243 
248 class IterSumExpr : public IterMapExpr {
249  public:
255  TVM_DLL IterSumExpr(Array<IterSplitExpr> args, PrimExpr base);
256 
259 };
260 
282 Array<IterSumExpr> DetectIterMap(const Array<PrimExpr>& indices, const Map<Var, Range>& input_iters,
283  const PrimExpr& predicate, bool require_bijective,
284  arith::Analyzer* analyzer);
295 Array<PrimExpr> IterMapSimplify(const Array<PrimExpr>& indices, const Map<Var, Range>& input_iters,
296  const PrimExpr& input_pred, bool require_bijective);
297 
317  const Array<PrimExpr> outputs);
318 
345  const Map<Var, Range>& input_iters,
346  const Array<Var>& sub_iters, const PrimExpr& predicate,
347  bool require_bijective, arith::Analyzer* analyzer);
348 
349 } // namespace arith
350 } // namespace tvm
351 #endif // TVM_ARITH_ITER_AFFINE_MAP_H_
static constexpr const char * _type_key
Definition: iter_affine_map.h:70
Managed reference to IterSplitExprNode.
Definition: iter_affine_map.h:186
Fuse multiple iterators by summing them with scaling.
Definition: iter_affine_map.h:218
bool SEqualReduce(const IterSumExprNode *other, SEqualReducer equal) const
Definition: iter_affine_map.h:231
Map< Var, PrimExpr > InverseAffineIterMap(const Array< IterSumExpr > &iter_map, const Array< PrimExpr > outputs)
Apply the inverse of the affine transformation to the outputs.
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:68
Split of an iterator.
Definition: iter_affine_map.h:147
A Reducer class to reduce the structural equality result of two objects.
Definition: structural_equal.h:102
Base expr nodes in TVM.
static constexpr const bool _type_has_method_shash_reduce
Definition: expr.h:56
PrimExpr extent
The extent of the iteration.
Definition: iter_affine_map.h:100
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:36
A Reducer class to reduce the structural hash value.
Definition: structural_hash.h:101
Variables in the TIR.
PrimExpr equal(PrimExpr a, PrimExpr b, Span span=Span())
equal
Algebra expression simplifications.
PrimExpr scale
Additional scale.
Definition: iter_affine_map.h:156
PrimExpr source
The source expression, can either be a IterSumExpr or a Var.
Definition: iter_affine_map.h:96
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:103
base class of all object containers.
Definition: object.h:165
bool SEqualReduce(const IterSplitExprNode *other, SEqualReducer equal) const
Definition: iter_affine_map.h:166
Base class of all iter map expressions.
Definition: iter_affine_map.h:65
Managed reference to IterSumExprNode.
Definition: iter_affine_map.h:248
virtual void MarkGraphNode()=0
Mark current comparison as graph node equal comparison.
Array< Array< IterMark > > SubspaceDivide(const Array< PrimExpr > &bindings, const Map< Var, Range > &input_iters, const Array< Var > &sub_iters, const PrimExpr &predicate, bool require_bijective, arith::Analyzer *analyzer)
Detect if bindings can be written as [a_0*e_0 + b_0 + c_0, a_1*e_1 + b_1, ..., a_n*e_n + b_n]...
PrimExpr lower_factor
The lower factor to split the source.
Definition: iter_affine_map.h:152
Visitor class to get the attributes of an AST/IR node. The content is going to be called for each fie...
Definition: reflection.h:52
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:226
PrimExpr extent
The extent of the split.
Definition: iter_affine_map.h:154
Array< IterSplitExpr > args
The args to the sum.
Definition: iter_affine_map.h:221
virtual void MarkGraphNode()=0
Mark current comparison as graph node in hashing. Graph node hash will depends on the graph structure...
bool SEqualReduce(const IterMarkNode *other, SEqualReducer equal) const
Definition: iter_affine_map.h:108
Array, container representing a contiguous sequence of ObjectRefs.
Definition: array.h:270
static constexpr const bool _type_has_method_sequal_reduce
Definition: expr.h:55
void VisitAttrs(tvm::AttrVisitor *v)
Definition: iter_affine_map.h:159
IterMark source
The source marked iterator.
Definition: iter_affine_map.h:150
#define TVM_DEFINE_OBJECT_REF_METHODS(TypeName, ParentType, ObjectName)
Definition: object.h:706
Managed reference to IterMarkExprNode.
Definition: iter_affine_map.h:129
Array< IterSumExpr > DetectIterMap(const Array< PrimExpr > &indices, const Map< Var, Range > &input_iters, const PrimExpr &predicate, bool require_bijective, arith::Analyzer *analyzer)
Detect if indices can be written as [y_0 + c_0, y_1 + c_1, ..., y_n + c_n].
Base class of all object reference.
Definition: object.h:504
#define TVM_DEFINE_OBJECT_REF_COW_METHOD(ObjectName)
Define CopyOnWrite function in an ObjectRef.
Definition: object.h:778
Mark the source as an iterator in [0, extent).
Definition: iter_affine_map.h:90
#define TVM_DECLARE_FINAL_OBJECT_INFO(TypeName, ParentType)
helper macro to declare type information in a final class.
Definition: object.h:664
static constexpr const uint32_t _type_child_slots
Definition: iter_affine_map.h:71
TVM_DECLARE_BASE_OBJECT_INFO(IterMapExprNode, PrimExprNode)
void SHashReduce(SHashReducer hash_reduce) const
Definition: iter_affine_map.h:235
Map container of NodeRef->NodeRef in DSL graph. Map implements copy on write semantics, which means map is mutable but copy will happen when array is referenced in more than two places.
Definition: map.h:1235
Managed reference to IterMapExprNode.
Definition: iter_affine_map.h:79
void SHashReduce(SHashReducer hash_reduce) const
Definition: iter_affine_map.h:171
Array< PrimExpr > IterMapSimplify(const Array< PrimExpr > &indices, const Map< Var, Range > &input_iters, const PrimExpr &input_pred, bool require_bijective)
Use IterVarMap detector to rewrite and simplify the indices.
Reference to PrimExprNode.
Definition: expr.h:109
PrimExpr base
The base offset.
Definition: iter_affine_map.h:223
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:387
void SHashReduce(SHashReducer hash_reduce) const
Definition: iter_affine_map.h:113
Base node of all primitive expressions.
Definition: expr.h:82