tvm
Loading...
Searching...
No Matches
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_SYM_ITER_AFFINE_MAP_H_
49#define TVM_SYM_ITER_AFFINE_MAP_H_
50
51#include <tvm/ffi/reflection/registry.h>
52#include <tvm/ir/cow.h>
53#include <tvm/ir/expr.h>
54#include <tvm/sym/analyzer.h>
55
56namespace tvm {
57namespace sym {
58
66class IterMapExprNode : public ExprNode {
67 public:
68 static constexpr const uint32_t _type_child_slots = 2;
70};
71
76class IterMapExpr : public PrimExpr {
77 public:
78 explicit IterMapExpr(ffi::ObjectPtr<IterMapExprNode> node) : PrimExpr(std::move(node)) {}
79
81 static constexpr bool _type_container_is_exact = true;
82};
83
90class IterMarkNode : public ffi::Object {
91 public:
93 : source(std::move(source)), extent(std::move(extent)) {}
94 explicit IterMarkNode(ffi::UnsafeInit) : source(ffi::UnsafeInit{}), extent(ffi::UnsafeInit{}) {}
95
105
106 static void RegisterReflection() {
107 namespace refl = tvm::ffi::reflection;
108 refl::ObjectDef<IterMarkNode>()
109 .def_ro("source", &IterMarkNode::source)
110 .def_ro("extent", &IterMarkNode::extent);
111 }
112
113 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindDAGNode;
114 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterMark", IterMarkNode, ffi::Object);
115};
116
133
140 public:
143 : source(std::move(source)),
144 lower_factor(std::move(lower_factor)),
145 extent(std::move(extent)),
146 scale(std::move(scale)) {}
147 explicit IterSplitExprNode(ffi::UnsafeInit)
148 : source(ffi::UnsafeInit{}),
149 lower_factor(ffi::UnsafeInit{}),
150 extent(ffi::UnsafeInit{}),
151 scale(ffi::UnsafeInit{}) {}
152
161
162 static void RegisterReflection() {
163 namespace refl = tvm::ffi::reflection;
164 refl::ObjectDef<IterSplitExprNode>()
165 .def_ro("source", &IterSplitExprNode::source)
166 .def_ro("lower_factor", &IterSplitExprNode::lower_factor)
167 .def_ro("extent", &IterSplitExprNode::extent)
168 .def_ro("scale", &IterSplitExprNode::scale);
169 }
170
171 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
173};
174
180 public:
199 TVM_TIRX_DLL explicit IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent,
200 PrimExpr scale);
201
202 explicit IterSplitExpr(ffi::ObjectPtr<IterSplitExprNode> node) : IterMapExpr(std::move(node)) {}
203
206};
207
214 public:
215 explicit IterSumExprNode(PrimExpr base) : base(std::move(base)) {}
216 explicit IterSumExprNode(ffi::UnsafeInit) : base(ffi::UnsafeInit{}) {}
217
219 ffi::Array<IterSplitExpr> args;
222
223 static void RegisterReflection() {
224 namespace refl = tvm::ffi::reflection;
225 refl::ObjectDef<IterSumExprNode>()
226 .def_ro("args", &IterSumExprNode::args)
227 .def_ro("base", &IterSumExprNode::base);
228 }
229
230 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
232};
233
238class IterSumExpr : public IterMapExpr {
239 public:
245 TVM_TIRX_DLL IterSumExpr(ffi::Array<IterSplitExpr> args, PrimExpr base);
246
247 explicit IterSumExpr(ffi::ObjectPtr<IterSumExprNode> node) : IterMapExpr(std::move(node)) {}
248
251};
252
253} // namespace sym
254
255namespace ffi {
256template <>
257inline constexpr bool object_ref_contains_v<PrimExpr, sym::IterSplitExprNode> = true;
258template <>
259inline constexpr bool object_ref_contains_v<PrimExpr, sym::IterSumExprNode> = true;
260} // namespace ffi
261
262namespace sym {
263
266 // Require the mapping to be bijective.
268 // Require the mapping to be surjective.
270 // No mapping safety check.
271 NoCheck = 3
273
277class IterMapResultObj : public ffi::Object {
278 public:
279 // The detected pattern if a match exists.
280 ffi::Array<IterSumExpr> indices;
281
282 // Any errors that occurred while converting the input indices. If
283 // the array is empty, the conversion was successful.
284 ffi::Array<ffi::String> errors;
285
294 ffi::Optional<PrimExpr> padding_predicate;
295
296 static void RegisterReflection() {
297 namespace refl = tvm::ffi::reflection;
298 refl::ObjectDef<IterMapResultObj>()
299 .def_ro("indices", &IterMapResultObj::indices)
300 .def_ro("errors", &IterMapResultObj::errors)
301 .def_ro("padding_predicate", &IterMapResultObj::padding_predicate);
302 }
303 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterMapResult", IterMapResultObj, ffi::Object);
304};
305
310class IterMapResult : public ffi::ObjectRef {
311 public:
312 static constexpr bool _type_is_nullable = false;
313 // constructor
314 IterMapResult() { data_ = ffi::make_object<IterMapResultObj>(); }
315
317 IterMapResultObj* operator->() const { return static_cast<IterMapResultObj*>(get_mutable()); }
318};
319
343IterMapResult DetectIterMap(const ffi::Array<PrimExpr>& indices,
344 const ffi::Map<PrimVar, Range>& input_iters, const PrimExpr& predicate,
345 IterMapLevel check_level, const sym::Analyzer& analyzer,
346 bool simplify_trivial_iterators = true);
347
359ffi::Array<PrimExpr> IterMapSimplify(const ffi::Array<PrimExpr>& indices,
360 const ffi::Map<PrimVar, Range>& input_iters,
361 const PrimExpr& input_pred, IterMapLevel check_level,
362 const sym::Analyzer& analyzer,
363 bool simplify_trivial_iterators = true);
364
385ffi::Map<Var, PrimExpr> InverseAffineIterMap(const ffi::Array<IterSumExpr>& iter_map,
386 const ffi::Array<PrimExpr> outputs);
387
415ffi::Array<ffi::Array<IterMark>> SubspaceDivide(const ffi::Array<PrimExpr>& bindings,
416 const ffi::Map<PrimVar, Range>& input_iters,
417 const ffi::Array<PrimVar>& sub_iters,
418 const PrimExpr& predicate, IterMapLevel check_level,
419 const sym::Analyzer& analyzer,
420 bool simplify_trivial_iterators = true);
421
428
445IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map<PrimVar, Range>& input_iters,
446 const sym::Analyzer& analyzer);
447
448} // namespace sym
449} // namespace tvm
450#endif // TVM_SYM_ITER_AFFINE_MAP_H_
Algebra expression simplifications.
Base type of all the expressions.
Definition base_expr.h:324
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
Managed reference to AnalyzerObj.
Definition analyzer.h:916
Base class of all iter map expressions.
Definition iter_affine_map.h:66
static constexpr const uint32_t _type_child_slots
Definition iter_affine_map.h:68
TVM_FFI_DECLARE_OBJECT_INFO("sym.IterMapExpr", IterMapExprNode, ExprNode)
Managed reference to IterMapExprNode.
Definition iter_affine_map.h:76
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IterMapExpr, PrimExpr, IterMapExprNode)
IterMapExpr(ffi::ObjectPtr< IterMapExprNode > node)
Definition iter_affine_map.h:78
static constexpr bool _type_container_is_exact
Definition iter_affine_map.h:81
Result of DetectIterMap.
Definition iter_affine_map.h:277
ffi::Array< ffi::String > errors
Definition iter_affine_map.h:284
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterMapResult", IterMapResultObj, ffi::Object)
static void RegisterReflection()
Definition iter_affine_map.h:296
ffi::Optional< PrimExpr > padding_predicate
Boolean expression indicating if a specific value w.
Definition iter_affine_map.h:294
ffi::Array< IterSumExpr > indices
Definition iter_affine_map.h:280
Managed reference to IterMapResultObj.
Definition iter_affine_map.h:310
IterMapResultObj * operator->() const
Definition iter_affine_map.h:317
static constexpr bool _type_is_nullable
Definition iter_affine_map.h:312
IterMapResult()
Definition iter_affine_map.h:314
Mark the source as an iterator in [0, extent).
Definition iter_affine_map.h:90
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition iter_affine_map.h:113
PrimExpr source
The source expression, can either be a IterSumExpr or a Var.
Definition iter_affine_map.h:100
IterMarkNode(PrimExpr source, PrimExpr extent)
Definition iter_affine_map.h:92
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterMark", IterMarkNode, ffi::Object)
IterMarkNode(ffi::UnsafeInit)
Definition iter_affine_map.h:94
static void RegisterReflection()
Definition iter_affine_map.h:106
PrimExpr extent
The extent of the iteration.
Definition iter_affine_map.h:104
Managed reference to IterMarkExprNode.
Definition iter_affine_map.h:121
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterMarkNode)
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IterMark, ffi::ObjectRef, IterMarkNode)
TVM_TIRX_DLL IterMark(PrimExpr source, PrimExpr extent)
constructor.
Split of an iterator.
Definition iter_affine_map.h:139
IterSplitExprNode(ffi::UnsafeInit)
Definition iter_affine_map.h:147
PrimExpr extent
The extent of the split.
Definition iter_affine_map.h:158
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterSplitExpr", IterSplitExprNode, IterMapExprNode)
PrimExpr lower_factor
The lower factor to split the source.
Definition iter_affine_map.h:156
PrimExpr scale
Additional scale.
Definition iter_affine_map.h:160
static void RegisterReflection()
Definition iter_affine_map.h:162
IterSplitExprNode(IterMark source, PrimExpr lower_factor, PrimExpr extent, PrimExpr scale)
Definition iter_affine_map.h:141
IterMark source
The source marked iterator.
Definition iter_affine_map.h:154
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition iter_affine_map.h:171
Managed reference to IterSplitExprNode.
Definition iter_affine_map.h:179
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IterSplitExpr, IterMapExpr, IterSplitExprNode)
TVM_TIRX_DLL IterSplitExpr(IterMark source)
constructor from just source.
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSplitExprNode)
TVM_TIRX_DLL IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent, PrimExpr scale)
constructor
IterSplitExpr(ffi::ObjectPtr< IterSplitExprNode > node)
Definition iter_affine_map.h:202
TVM_TIRX_DLL IterSplitExpr(IterMark source, PrimExpr scale)
constructor from just source.
Fuse multiple iterators by summing them with scaling.
Definition iter_affine_map.h:213
ffi::Array< IterSplitExpr > args
The args to the sum.
Definition iter_affine_map.h:219
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterSumExpr", IterSumExprNode, IterMapExprNode)
IterSumExprNode(PrimExpr base)
Definition iter_affine_map.h:215
static void RegisterReflection()
Definition iter_affine_map.h:223
IterSumExprNode(ffi::UnsafeInit)
Definition iter_affine_map.h:216
PrimExpr base
The base offset.
Definition iter_affine_map.h:221
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition iter_affine_map.h:230
Managed reference to IterSumExprNode.
Definition iter_affine_map.h:238
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprNode)
IterSumExpr(ffi::ObjectPtr< IterSumExprNode > node)
Definition iter_affine_map.h:247
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IterSumExpr, IterMapExpr, IterSumExprNode)
TVM_TIRX_DLL IterSumExpr(ffi::Array< IterSplitExpr > args, PrimExpr base)
constructor.
Copy-on-write helper macro for IR ffi::ObjectRef types.
Base expr nodes in TVM.
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< ffi::Array< IterMark > > SubspaceDivide(const ffi::Array< PrimExpr > &bindings, const ffi::Map< PrimVar, Range > &input_iters, const ffi::Array< PrimVar > &sub_iters, const PrimExpr &predicate, IterMapLevel check_level, const sym::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].
IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map< PrimVar, Range > &input_iters, const sym::Analyzer &analyzer)
Rewrite index as IterSumExpr.
IterMapResult DetectIterMap(const ffi::Array< PrimExpr > &indices, const ffi::Map< PrimVar, Range > &input_iters, const PrimExpr &predicate, IterMapLevel check_level, const sym::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< PrimVar, Range > &input_iters, const PrimExpr &input_pred, IterMapLevel check_level, const sym::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.
IterMapLevel
Mapping level for iterators.
Definition iter_affine_map.h:265
@ Surjective
Definition iter_affine_map.h:269
@ NoCheck
Definition iter_affine_map.h:271
@ Bijective
Definition iter_affine_map.h:267
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
#define TVM_TIRX_DLL
Definition base.h:83