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 IterMapExprObj : public ExprObj {
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<IterMapExprObj> node) : PrimExpr(std::move(node)) {}
79
81 static constexpr bool _type_container_is_exact = true;
82};
83
90class IterMarkObj : public ffi::Object {
91 public:
93 : source(std::move(source)), extent(std::move(extent)) {}
94 explicit IterMarkObj(ffi::UnsafeInit) : source(ffi::UnsafeInit{}), extent(ffi::UnsafeInit{}) {}
95
105
106 static void RegisterReflection() {
107 namespace refl = tvm::ffi::reflection;
108 refl::ObjectDef<IterMarkObj>()
109 .def_ro("source", &IterMarkObj::source)
110 .def_ro("extent", &IterMarkObj::extent);
111 }
112
113 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindDAGNode;
114 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterMark", IterMarkObj, ffi::Object);
115};
116
133
140 public:
142 : source(std::move(source)),
143 lower_factor(std::move(lower_factor)),
144 extent(std::move(extent)),
145 scale(std::move(scale)) {}
146 explicit IterSplitExprObj(ffi::UnsafeInit)
147 : source(ffi::UnsafeInit{}),
148 lower_factor(ffi::UnsafeInit{}),
149 extent(ffi::UnsafeInit{}),
150 scale(ffi::UnsafeInit{}) {}
151
160
161 static void RegisterReflection() {
162 namespace refl = tvm::ffi::reflection;
163 refl::ObjectDef<IterSplitExprObj>()
164 .def_ro("source", &IterSplitExprObj::source)
165 .def_ro("lower_factor", &IterSplitExprObj::lower_factor)
166 .def_ro("extent", &IterSplitExprObj::extent)
167 .def_ro("scale", &IterSplitExprObj::scale);
168 }
169
170 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
172};
173
179 public:
198 TVM_TIRX_DLL explicit IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent,
199 PrimExpr scale);
200
201 explicit IterSplitExpr(ffi::ObjectPtr<IterSplitExprObj> node) : IterMapExpr(std::move(node)) {}
202
205};
206
213 public:
214 explicit IterSumExprObj(PrimExpr base) : base(std::move(base)) {}
215 explicit IterSumExprObj(ffi::UnsafeInit) : base(ffi::UnsafeInit{}) {}
216
218 ffi::Array<IterSplitExpr> args;
221
222 static void RegisterReflection() {
223 namespace refl = tvm::ffi::reflection;
224 refl::ObjectDef<IterSumExprObj>()
225 .def_ro("args", &IterSumExprObj::args)
226 .def_ro("base", &IterSumExprObj::base);
227 }
228
229 static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind = kTVMFFISEqHashKindTreeNode;
231};
232
237class IterSumExpr : public IterMapExpr {
238 public:
244 TVM_TIRX_DLL IterSumExpr(ffi::Array<IterSplitExpr> args, PrimExpr base);
245
246 explicit IterSumExpr(ffi::ObjectPtr<IterSumExprObj> node) : IterMapExpr(std::move(node)) {}
247
250};
251
252} // namespace sym
253
254namespace ffi {
255template <>
256inline constexpr bool object_ref_contains_v<PrimExpr, sym::IterSplitExprObj> = true;
257template <>
258inline constexpr bool object_ref_contains_v<PrimExpr, sym::IterSumExprObj> = true;
259} // namespace ffi
260
261namespace sym {
262
265 // Require the mapping to be bijective.
267 // Require the mapping to be surjective.
269 // No mapping safety check.
270 NoCheck = 3
272
276class IterMapResultObj : public ffi::Object {
277 public:
278 // The detected pattern if a match exists.
279 ffi::Array<IterSumExpr> indices;
280
281 // Any errors that occurred while converting the input indices. If
282 // the array is empty, the conversion was successful.
283 ffi::Array<ffi::String> errors;
284
293 ffi::Optional<PrimExpr> padding_predicate;
294
295 static void RegisterReflection() {
296 namespace refl = tvm::ffi::reflection;
297 refl::ObjectDef<IterMapResultObj>()
298 .def_ro("indices", &IterMapResultObj::indices)
299 .def_ro("errors", &IterMapResultObj::errors)
300 .def_ro("padding_predicate", &IterMapResultObj::padding_predicate);
301 }
302 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterMapResult", IterMapResultObj, ffi::Object);
303 // Shared instances are mutated in place, so the class is mutable.
304 static constexpr bool _type_mutable = true;
305};
306
311class IterMapResult : public ffi::ObjectRef {
312 public:
313 static constexpr bool _type_is_nullable = false;
314 // constructor
315 IterMapResult() { data_ = ffi::make_object<IterMapResultObj>(); }
316
318 IterMapResultObj* operator->() const { return static_cast<IterMapResultObj*>(get_mutable()); }
319};
320
344TVM_TIRX_DLL IterMapResult DetectIterMap(const ffi::Array<PrimExpr>& indices,
345 const ffi::Map<PrimVar, Range>& input_iters,
346 const PrimExpr& predicate, IterMapLevel check_level,
347 const sym::Analyzer& analyzer,
348 bool simplify_trivial_iterators = true);
349
361TVM_TIRX_DLL ffi::Array<PrimExpr> IterMapSimplify(const ffi::Array<PrimExpr>& indices,
362 const ffi::Map<PrimVar, Range>& input_iters,
363 const PrimExpr& input_pred,
364 IterMapLevel check_level,
365 const sym::Analyzer& analyzer,
366 bool simplify_trivial_iterators = true);
367
388TVM_TIRX_DLL ffi::Map<Var, PrimExpr> InverseAffineIterMap(const ffi::Array<IterSumExpr>& iter_map,
389 const ffi::Array<PrimExpr> outputs);
390
418TVM_TIRX_DLL ffi::Array<ffi::Array<IterMark>> SubspaceDivide(
419 const ffi::Array<PrimExpr>& bindings, const ffi::Map<PrimVar, Range>& input_iters,
420 const ffi::Array<PrimVar>& sub_iters, const PrimExpr& predicate, IterMapLevel check_level,
421 const sym::Analyzer& analyzer, bool simplify_trivial_iterators = true);
422
429
447 const ffi::Map<PrimVar, Range>& input_iters,
448 const sym::Analyzer& analyzer);
449
450} // namespace sym
451} // namespace tvm
452#endif // TVM_SYM_ITER_AFFINE_MAP_H_
Base expr nodes in TVM.
#define TVM_TIRX_DLL
Definition base.h:55
Algebra expression simplifications.
Base type of all the expressions.
Definition expr.h:66
Typed reference/view over any Expr whose ExprObj::ty is PrimType.
Definition expr.h:187
Managed reference to AnalyzerObj.
Definition analyzer.h:750
Base class of all iter map expressions.
Definition iter_affine_map.h:66
TVM_FFI_DECLARE_OBJECT_INFO("sym.IterMapExpr", IterMapExprObj, ExprObj)
static constexpr const uint32_t _type_child_slots
Definition iter_affine_map.h:68
Managed reference to IterMapExprObj.
Definition iter_affine_map.h:76
IterMapExpr(ffi::ObjectPtr< IterMapExprObj > node)
Definition iter_affine_map.h:78
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IterMapExpr, PrimExpr, IterMapExprObj)
static constexpr bool _type_container_is_exact
Definition iter_affine_map.h:81
Result of DetectIterMap.
Definition iter_affine_map.h:276
ffi::Array< ffi::String > errors
Definition iter_affine_map.h:283
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterMapResult", IterMapResultObj, ffi::Object)
static void RegisterReflection()
Definition iter_affine_map.h:295
static constexpr bool _type_mutable
Definition iter_affine_map.h:304
ffi::Optional< PrimExpr > padding_predicate
Boolean expression indicating if a specific value w.
Definition iter_affine_map.h:293
ffi::Array< IterSumExpr > indices
Definition iter_affine_map.h:279
Managed reference to IterMapResultObj.
Definition iter_affine_map.h:311
IterMapResultObj * operator->() const
Definition iter_affine_map.h:318
static constexpr bool _type_is_nullable
Definition iter_affine_map.h:313
IterMapResult()
Definition iter_affine_map.h:315
Mark the source as an iterator in [0, extent).
Definition iter_affine_map.h:90
IterMarkObj(PrimExpr source, PrimExpr extent)
Definition iter_affine_map.h:92
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition iter_affine_map.h:113
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterMark", IterMarkObj, ffi::Object)
static void RegisterReflection()
Definition iter_affine_map.h:106
PrimExpr extent
The extent of the iteration.
Definition iter_affine_map.h:104
IterMarkObj(ffi::UnsafeInit)
Definition iter_affine_map.h:94
PrimExpr source
The source expression, can either be a IterSumExpr or a Var.
Definition iter_affine_map.h:100
Managed reference to IterMarkObj.
Definition iter_affine_map.h:121
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterMarkObj)
TVM_TIRX_DLL IterMark(PrimExpr source, PrimExpr extent)
constructor.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IterMark, ffi::ObjectRef, IterMarkObj)
Split of an iterator.
Definition iter_affine_map.h:139
PrimExpr extent
The extent of the split.
Definition iter_affine_map.h:157
PrimExpr scale
Additional scale.
Definition iter_affine_map.h:159
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition iter_affine_map.h:170
IterMark source
The source marked iterator.
Definition iter_affine_map.h:153
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterSplitExpr", IterSplitExprObj, IterMapExprObj)
IterSplitExprObj(IterMark source, PrimExpr lower_factor, PrimExpr extent, PrimExpr scale)
Definition iter_affine_map.h:141
PrimExpr lower_factor
The lower factor to split the source.
Definition iter_affine_map.h:155
IterSplitExprObj(ffi::UnsafeInit)
Definition iter_affine_map.h:146
static void RegisterReflection()
Definition iter_affine_map.h:161
Managed reference to IterSplitExprObj.
Definition iter_affine_map.h:178
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IterSplitExpr, IterMapExpr, IterSplitExprObj)
TVM_TIRX_DLL IterSplitExpr(IterMark source)
constructor from just source.
TVM_TIRX_DLL IterSplitExpr(IterMark source, PrimExpr lower_factor, PrimExpr extent, PrimExpr scale)
constructor
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSplitExprObj)
TVM_TIRX_DLL IterSplitExpr(IterMark source, PrimExpr scale)
constructor from just source.
IterSplitExpr(ffi::ObjectPtr< IterSplitExprObj > node)
Definition iter_affine_map.h:201
Fuse multiple iterators by summing them with scaling.
Definition iter_affine_map.h:212
PrimExpr base
The base offset.
Definition iter_affine_map.h:220
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("sym.IterSumExpr", IterSumExprObj, IterMapExprObj)
IterSumExprObj(ffi::UnsafeInit)
Definition iter_affine_map.h:215
static void RegisterReflection()
Definition iter_affine_map.h:222
IterSumExprObj(PrimExpr base)
Definition iter_affine_map.h:214
static constexpr TVMFFISEqHashKind _type_s_eq_hash_kind
Definition iter_affine_map.h:229
ffi::Array< IterSplitExpr > args
The args to the sum.
Definition iter_affine_map.h:218
Managed reference to IterSumExprObj.
Definition iter_affine_map.h:237
TVM_DEFINE_OBJECT_REF_COW_METHOD(IterSumExprObj)
IterSumExpr(ffi::ObjectPtr< IterSumExprObj > node)
Definition iter_affine_map.h:246
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(IterSumExpr, IterMapExpr, IterSumExprObj)
TVM_TIRX_DLL IterSumExpr(ffi::Array< IterSplitExpr > args, PrimExpr base)
constructor.
Copy-on-write helper macro for IR ffi::ObjectRef types.
TVM_TIRX_DLL 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].
TVM_TIRX_DLL PrimExpr NormalizeIterMapToExpr(const PrimExpr &expr)
Given an expression that may contain IterMapExpr, transform it to normal PrimExpr.
TVM_TIRX_DLL 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].
TVM_TIRX_DLL IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map< PrimVar, Range > &input_iters, const sym::Analyzer &analyzer)
Rewrite index as IterSumExpr.
TVM_TIRX_DLL 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.
TVM_TIRX_DLL 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.
IterMapLevel
Mapping level for iterators.
Definition iter_affine_map.h:264
@ Surjective
Definition iter_affine_map.h:268
@ NoCheck
Definition iter_affine_map.h:270
@ Bijective
Definition iter_affine_map.h:266
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analysis.h:41