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_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
57namespace tvm {
58namespace arith {
59
67class IterMapExprNode : public ExprNode {
68 public:
69 static constexpr const uint32_t _type_child_slots = 2;
71};
72
82
89class 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
109 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("arith.IterMark", IterMarkNode, ffi::Object);
110};
111
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
156};
157
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
210};
211
228
229} // namespace arith
230
231namespace ffi {
232template <>
234template <>
236} // namespace ffi
237
238namespace arith {
239
242 // Require the mapping to be bijective.
244 // Require the mapping to be surjective.
246 // No mapping safety check.
247 NoCheck = 3
249
253class 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
286class 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
318IterMapResult DetectIterMap(const ffi::Array<PrimExpr>& indices,
319 const ffi::Map<tirx::PrimVar, Range>& input_iters,
320 const PrimExpr& predicate, IterMapLevel check_level,
322 bool simplify_trivial_iterators = true);
323
335ffi::Array<PrimExpr> IterMapSimplify(const ffi::Array<PrimExpr>& indices,
336 const ffi::Map<tirx::PrimVar, Range>& input_iters,
339 bool simplify_trivial_iterators = true);
340
361ffi::Map<Var, PrimExpr> InverseAffineIterMap(const ffi::Array<IterSumExpr>& iter_map,
362 const ffi::Array<PrimExpr> outputs);
363
391ffi::Array<ffi::Array<IterMark>> SubspaceDivide(const ffi::Array<PrimExpr>& bindings,
392 const ffi::Map<tirx::PrimVar, Range>& input_iters,
393 const ffi::Array<tirx::PrimVar>& sub_iters,
394 const PrimExpr& predicate, IterMapLevel check_level,
396 bool simplify_trivial_iterators = true);
397
404
421IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map<tirx::PrimVar, Range>& input_iters,
423
424} // namespace arith
425} // namespace tvm
426#endif // TVM_ARITH_ITER_AFFINE_MAP_H_
Algebra expression simplifications.
Base type of all the expressions.
Definition base_expr.h:300
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed reference to AnalyzerObj.
Definition analyzer.h:931
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.
IterSumExpr NormalizeToIterSum(PrimExpr index, const ffi::Map< tirx::PrimVar, Range > &input_iters, const arith::Analyzer &analyzer)
Rewrite index as IterSumExpr.
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
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< tirx::PrimVar, Range > &input_iters, const ffi::Array< tirx::PrimVar > &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].
PrimExpr NormalizeIterMapToExpr(const PrimExpr &expr)
Given an expression that may contain IterMapExpr, transform it to normal PrimExpr.
IterMapResult DetectIterMap(const ffi::Array< PrimExpr > &indices, const ffi::Map< tirx::PrimVar, 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].
ffi::Array< PrimExpr > IterMapSimplify(const ffi::Array< PrimExpr > &indices, const ffi::Map< tirx::PrimVar, 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.
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
Variables in the TIR.