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/expr.h>
55 #include <tvm/tirx/var.h>
56 
57 namespace tvm {
58 namespace arith {
59 
67 class IterMapExprNode : public ExprNode {
68  public:
69  static constexpr const uint32_t _type_child_slots = 2;
71 };
72 
77 class IterMapExpr : public PrimExpr {
78  public:
80  static constexpr bool _type_container_is_exact = true;
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 
229 } // namespace arith
230 
231 namespace ffi {
232 template <>
233 inline constexpr bool object_ref_contains_v<PrimExpr, arith::IterSplitExprNode> = true;
234 template <>
235 inline constexpr bool object_ref_contains_v<PrimExpr, arith::IterSumExprNode> = true;
236 } // namespace ffi
237 
238 namespace arith {
239 
242  // Require the mapping to be bijective.
244  // Require the mapping to be surjective.
246  // No mapping safety check.
247  NoCheck = 3
248 };
249 
253 class IterMapResultNode : public ffi::Object {
254  public:
255  // The detected pattern if a match exists.
256  ffi::Array<IterSumExpr> indices;
257 
258  // Any errors that occurred while converting the input indices. If
259  // the array is empty, the conversion was successful.
260  ffi::Array<ffi::String> errors;
261 
271 
272  static void RegisterReflection() {
273  namespace refl = tvm::ffi::reflection;
274  refl::ObjectDef<IterMapResultNode>()
275  .def_ro("indices", &IterMapResultNode::indices)
276  .def_ro("errors", &IterMapResultNode::errors)
277  .def_ro("padding_predicate", &IterMapResultNode::padding_predicate);
278  }
279  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterMapResult", IterMapResultNode, ffi::Object);
280 };
281 
286 class IterMapResult : public ffi::ObjectRef {
287  public:
288  // constructor
289  IterMapResult() { data_ = ffi::make_object<IterMapResultNode>(); }
290 
292  IterMapResultNode* operator->() const { return static_cast<IterMapResultNode*>(get_mutable()); }
293 };
294 
318 IterMapResult DetectIterMap(const ffi::Array<PrimExpr>& indices,
319  const ffi::Map<Var, Range>& input_iters, const PrimExpr& predicate,
320  IterMapLevel check_level, const arith::Analyzer& analyzer,
321  bool simplify_trivial_iterators = true);
322 
334 ffi::Array<PrimExpr> IterMapSimplify(const ffi::Array<PrimExpr>& indices,
335  const ffi::Map<Var, Range>& input_iters,
336  const PrimExpr& input_pred, IterMapLevel check_level,
337  const arith::Analyzer& analyzer,
338  bool simplify_trivial_iterators = true);
339 
360 ffi::Map<Var, PrimExpr> InverseAffineIterMap(const ffi::Array<IterSumExpr>& iter_map,
361  const ffi::Array<PrimExpr> outputs);
362 
390 ffi::Array<ffi::Array<IterMark>> SubspaceDivide(const ffi::Array<PrimExpr>& bindings,
391  const ffi::Map<Var, Range>& input_iters,
392  const ffi::Array<Var>& sub_iters,
393  const PrimExpr& predicate, IterMapLevel check_level,
394  const arith::Analyzer& analyzer,
395  bool simplify_trivial_iterators = true);
396 
403 
420 IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map<Var, Range>& input_iters,
421  const arith::Analyzer& analyzer);
422 
423 } // namespace arith
424 } // namespace tvm
425 #endif // TVM_ARITH_ITER_AFFINE_MAP_H_
Algebra expression simplifications.
Base type of all the expressions.
Definition: base_expr.h:276
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition: base_expr.h:354
Managed reference to AnalyzerObj.
Definition: analyzer.h:913
Base class of all iter map expressions.
Definition: iter_affine_map.h:67
TVM_FFI_DECLARE_OBJECT_INFO("arith.IterMapExpr", IterMapExprNode, ExprNode)
static constexpr const uint32_t _type_child_slots
Definition: iter_affine_map.h:69
Managed reference to IterMapExprNode.
Definition: iter_affine_map.h:77
static constexpr bool _type_container_is_exact
Definition: iter_affine_map.h:80
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IterMapExpr, PrimExpr, IterMapExprNode)
Result of DetectIterMap.
Definition: iter_affine_map.h:253
ffi::Array< ffi::String > errors
Definition: iter_affine_map.h:260
ffi::Array< IterSumExpr > indices
Definition: iter_affine_map.h:256
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterMapResult", IterMapResultNode, ffi::Object)
static void RegisterReflection()
Definition: iter_affine_map.h:272
PrimExpr padding_predicate
Boolean expression indicating if a specific value w.
Definition: iter_affine_map.h:270
Managed reference to IterMapResultNode.
Definition: iter_affine_map.h:286
IterMapResult()
Definition: iter_affine_map.h:289
IterMapResultNode * operator->() const
Definition: iter_affine_map.h:292
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.
Base expr nodes in TVM.
IterMapLevel
Mapping level for iterators.
Definition: iter_affine_map.h:241
@ NoCheck
Definition: iter_affine_map.h:247
@ Bijective
Definition: iter_affine_map.h:243
@ Surjective
Definition: iter_affine_map.h:245
IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map< Var, Range > &input_iters, const arith::Analyzer &analyzer)
Rewrite index as IterSumExpr.
IterMapResult DetectIterMap(const ffi::Array< PrimExpr > &indices, const ffi::Map< Var, Range > &input_iters, const PrimExpr &predicate, IterMapLevel check_level, const 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].
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.
ffi::Array< PrimExpr > IterMapSimplify(const ffi::Array< PrimExpr > &indices, const ffi::Map< Var, Range > &input_iters, const PrimExpr &input_pred, IterMapLevel check_level, const arith::Analyzer &analyzer, bool simplify_trivial_iterators=true)
Use IterVarMap detector to rewrite and simplify the indices.
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, const 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].
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
Variables in the TIR.