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/ffi/reflection/registry.h>
53 #include <tvm/ir/cow.h>
54 #include <tvm/ir/diagnostic.h>
55 #include <tvm/ir/expr.h>
56 #include <tvm/tirx/var.h>
57 
58 namespace tvm {
59 namespace arith {
60 
68 class IterMapExprNode : public PrimExprNode {
69  public:
70  static constexpr const uint32_t _type_child_slots = 2;
72 };
73 
78 class IterMapExpr : public PrimExpr {
79  public:
81 };
82 
89 class IterMarkNode : public ffi::Object {
90  public:
100 
101  static void RegisterReflection() {
102  namespace refl = tvm::ffi::reflection;
103  refl::ObjectDef<IterMarkNode>()
104  .def_ro("source", &IterMarkNode::source)
105  .def_ro("extent", &IterMarkNode::extent);
106  }
107 
108  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindDAGNode;
109  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterMark", IterMarkNode, ffi::Object);
110 };
111 
116 class IterMark : public ffi::ObjectRef {
117  public:
123  TVM_DLL IterMark(PrimExpr source, PrimExpr extent);
124 
127 };
128 
135  public:
144 
145  static void RegisterReflection() {
146  namespace refl = tvm::ffi::reflection;
147  refl::ObjectDef<IterSplitExprNode>()
148  .def_ro("source", &IterSplitExprNode::source)
149  .def_ro("lower_factor", &IterSplitExprNode::lower_factor)
150  .def_ro("extent", &IterSplitExprNode::extent)
151  .def_ro("scale", &IterSplitExprNode::scale);
152  }
153 
154  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
156 };
157 
162 class IterSplitExpr : public IterMapExpr {
163  public:
168  TVM_DLL explicit IterSplitExpr(IterMark source);
174  TVM_DLL explicit IterSplitExpr(IterMark source, PrimExpr scale);
182  TVM_DLL explicit IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent,
183  PrimExpr scale);
184 
187 };
188 
195  public:
197  ffi::Array<IterSplitExpr> args;
200 
201  static void RegisterReflection() {
202  namespace refl = tvm::ffi::reflection;
203  refl::ObjectDef<IterSumExprNode>()
204  .def_ro("args", &IterSumExprNode::args)
205  .def_ro("base", &IterSumExprNode::base);
206  }
207 
208  static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
210 };
211 
216 class IterSumExpr : public IterMapExpr {
217  public:
223  TVM_DLL IterSumExpr(ffi::Array<IterSplitExpr> args, PrimExpr base);
224 
227 };
228 
231  // Require the mapping to be bijective.
233  // Require the mapping to be surjective.
235  // No mapping safety check.
236  NoCheck = 3
237 };
238 
242 class IterMapResultNode : public ffi::Object {
243  public:
244  // The detected pattern if a match exists.
245  ffi::Array<IterSumExpr> indices;
246 
247  // Any errors that occurred while converting the input indices. If
248  // the array is empty, the conversion was successful.
249  ffi::Array<ffi::String> errors;
250 
260 
261  static void RegisterReflection() {
262  namespace refl = tvm::ffi::reflection;
263  refl::ObjectDef<IterMapResultNode>()
264  .def_ro("indices", &IterMapResultNode::indices)
265  .def_ro("errors", &IterMapResultNode::errors)
266  .def_ro("padding_predicate", &IterMapResultNode::padding_predicate);
267  }
268  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterMapResult", IterMapResultNode, ffi::Object);
269 };
270 
275 class IterMapResult : public ffi::ObjectRef {
276  public:
277  // constructor
278  IterMapResult() { data_ = ffi::make_object<IterMapResultNode>(); }
279 
281  IterMapResultNode* operator->() const { return static_cast<IterMapResultNode*>(get_mutable()); }
282 };
283 
307 IterMapResult DetectIterMap(const ffi::Array<PrimExpr>& indices,
308  const ffi::Map<Var, Range>& input_iters, const PrimExpr& predicate,
309  IterMapLevel check_level, arith::Analyzer* analyzer,
310  bool simplify_trivial_iterators = true);
311 
323 ffi::Array<PrimExpr> IterMapSimplify(const ffi::Array<PrimExpr>& indices,
324  const ffi::Map<Var, Range>& input_iters,
325  const PrimExpr& input_pred, IterMapLevel check_level,
326  arith::Analyzer* analyzer,
327  bool simplify_trivial_iterators = true);
328 
349 ffi::Map<Var, PrimExpr> InverseAffineIterMap(const ffi::Array<IterSumExpr>& iter_map,
350  const ffi::Array<PrimExpr> outputs);
351 
379 ffi::Array<ffi::Array<IterMark>> SubspaceDivide(const ffi::Array<PrimExpr>& bindings,
380  const ffi::Map<Var, Range>& input_iters,
381  const ffi::Array<Var>& sub_iters,
382  const PrimExpr& predicate, IterMapLevel check_level,
383  arith::Analyzer* analyzer,
384  bool simplify_trivial_iterators = true);
385 
392 
409 IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map<Var, Range>& input_iters,
410  arith::Analyzer* analyzer);
411 
412 } // namespace arith
413 } // namespace tvm
414 #endif // TVM_ARITH_ITER_AFFINE_MAP_H_
Algebra expression simplifications.
Base node of all primitive expressions.
Definition: expr.h:93
Reference to PrimExprNode.
Definition: expr.h:126
Analyzer that contains bunch of sub-analyzers.
Definition: analyzer.h:635
Base class of all iter map expressions.
Definition: iter_affine_map.h:68
static constexpr const uint32_t _type_child_slots
Definition: iter_affine_map.h:70
TVM_FFI_DECLARE_OBJECT_INFO("arith.IterMapExpr", IterMapExprNode, PrimExprNode)
Managed reference to IterMapExprNode.
Definition: iter_affine_map.h:78
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IterMapExpr, PrimExpr, IterMapExprNode)
Result of DetectIterMap.
Definition: iter_affine_map.h:242
ffi::Array< ffi::String > errors
Definition: iter_affine_map.h:249
ffi::Array< IterSumExpr > indices
Definition: iter_affine_map.h:245
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterMapResult", IterMapResultNode, ffi::Object)
static void RegisterReflection()
Definition: iter_affine_map.h:261
PrimExpr padding_predicate
Boolean expression indicating if a specific value w.
Definition: iter_affine_map.h:259
Managed reference to IterMapResultNode.
Definition: iter_affine_map.h:275
IterMapResult()
Definition: iter_affine_map.h:278
IterMapResultNode * operator->() const
Definition: iter_affine_map.h:281
Mark the source as an iterator in [0, extent).
Definition: iter_affine_map.h:89
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterMark", IterMarkNode, ffi::Object)
PrimExpr extent
The extent of the iteration.
Definition: iter_affine_map.h:99
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: iter_affine_map.h:108
PrimExpr source
The source expression, can either be a IterSumExpr or a Var.
Definition: iter_affine_map.h:95
static void RegisterReflection()
Definition: iter_affine_map.h:101
Managed reference to IterMarkExprNode.
Definition: iter_affine_map.h:116
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterMarkNode)
IterMark(PrimExpr source, PrimExpr extent)
constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IterMark, ffi::ObjectRef, IterMarkNode)
Split of an iterator.
Definition: iter_affine_map.h:134
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterSplitExpr", IterSplitExprNode, IterMapExprNode)
PrimExpr lower_factor
The lower factor to split the source.
Definition: iter_affine_map.h:139
IterMark source
The source marked iterator.
Definition: iter_affine_map.h:137
PrimExpr scale
Additional scale.
Definition: iter_affine_map.h:143
static void RegisterReflection()
Definition: iter_affine_map.h:145
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: iter_affine_map.h:154
PrimExpr extent
The extent of the split.
Definition: iter_affine_map.h:141
Managed reference to IterSplitExprNode.
Definition: iter_affine_map.h:162
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSplitExprNode)
IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent, PrimExpr scale)
constructor
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IterSplitExpr, IterMapExpr, IterSplitExprNode)
IterSplitExpr(IterMark source)
constructor from just source.
IterSplitExpr(IterMark source, PrimExpr scale)
constructor from just source.
Fuse multiple iterators by summing them with scaling.
Definition: iter_affine_map.h:194
ffi::Array< IterSplitExpr > args
The args to the sum.
Definition: iter_affine_map.h:197
static void RegisterReflection()
Definition: iter_affine_map.h:201
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition: iter_affine_map.h:208
PrimExpr base
The base offset.
Definition: iter_affine_map.h:199
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterSumExpr", IterSumExprNode, IterMapExprNode)
Managed reference to IterSumExprNode.
Definition: iter_affine_map.h:216
IterSumExpr(ffi::Array< IterSplitExpr > args, PrimExpr base)
constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IterSumExpr, IterMapExpr, IterSumExprNode)
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode)
Copy-on-write helper macro for IR ffi::ObjectRef types.
A new diagnostic interface for TVM error reporting.
Base expr nodes in TVM.
ffi::Array< ffi::Array< IterMark > > SubspaceDivide(const ffi::Array< PrimExpr > &bindings, const ffi::Map< Var, Range > &input_iters, const ffi::Array< Var > &sub_iters, const PrimExpr &predicate, IterMapLevel check_level, arith::Analyzer *analyzer, bool simplify_trivial_iterators=true)
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].
IterMapLevel
Mapping level for iterators.
Definition: iter_affine_map.h:230
@ NoCheck
Definition: iter_affine_map.h:236
@ Bijective
Definition: iter_affine_map.h:232
@ Surjective
Definition: iter_affine_map.h:234
IterMapResult DetectIterMap(const ffi::Array< PrimExpr > &indices, const ffi::Map< Var, Range > &input_iters, const PrimExpr &predicate, IterMapLevel check_level, arith::Analyzer *analyzer, bool simplify_trivial_iterators=true)
Detect if indices can be written as [y_0 + c_0, y_1 + c_1, ..., y_n + c_n].
ffi::Array< PrimExpr > IterMapSimplify(const ffi::Array< PrimExpr > &indices, const ffi::Map< Var, Range > &input_iters, const PrimExpr &input_pred, IterMapLevel check_level, arith::Analyzer *analyzer, bool simplify_trivial_iterators=true)
Use IterVarMap detector to rewrite and simplify the indices.
PrimExpr NormalizeIterMapToExpr(const PrimExpr &expr)
Given an expression that may contain IterMapExpr, transform it to normal PrimExpr.
ffi::Map< Var, PrimExpr > InverseAffineIterMap(const ffi::Array< IterSumExpr > &iter_map, const ffi::Array< PrimExpr > outputs)
Apply the inverse of the affine transformation to the outputs.
IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map< Var, Range > &input_iters, arith::Analyzer *analyzer)
Rewrite index as IterSumExpr.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:37
Variables in the TIR.