tvm
Loading...
Searching...
No Matches
int_set.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
24#ifndef TVM_ARITH_INT_SET_H_
25#define TVM_ARITH_INT_SET_H_
26
27#include <tvm/ir/expr.h>
28#include <tvm/ir/prim/expr.h>
29#include <tvm/tirx/var.h>
30
31#include <unordered_map>
32
33namespace tvm {
34namespace arith {
35
36using tirx::IterVar;
37using tirx::Var;
38using tirx::VarNode;
39
40class AnalyzerObj;
41class Analyzer;
42
43//-----------------------------------------------
44// Integer set data structure.
45//
46// This is a API build on top of the base
47// integer analysis API to provide set analysis.
48//------------------------------------------------
53
59class IntSetNode : public ffi::Object {
60 public:
61 TVM_FFI_DECLARE_OBJECT_INFO("ir.IntSet", IntSetNode, ffi::Object);
62};
63
68class IntSet : public ffi::ObjectRef {
69 public:
77 PrimExpr min() const;
79 PrimExpr max() const;
83 bool IsNothing() const;
85 bool IsEverything() const;
87 bool IsSinglePoint() const;
101 bool CanProveSinglePoint(const Analyzer& ana) const;
102 // TODO(tvm-team): update all CanProve to explicitly take
103 // analyzer to encourage more analyzer reuse
105 bool CanProvePositive() const;
107 bool CanProveNegative() const;
113 bool HasUpperBound() const;
115 bool HasLowerBound() const;
116
128 bool MatchRange(const tvm::Range& r) const;
130 static IntSet Nothing();
165
167};
168
169//-----------------------------------------------
170// Integer set legacy API.
171//------------------------------------------------
178ffi::Map<Var, IntSet> ConvertDomMap(const std::unordered_map<const VarNode*, IntSet>& dom_map);
187IntSet EvalSet(PrimExpr e, const ffi::Map<IterVar, IntSet>& dom_map);
196IntSet EvalSet(PrimExpr e, const ffi::Map<Var, IntSet>& dom_map);
204IntSet EvalSet(PrimExpr e, const std::unordered_map<const tirx::VarNode*, IntSet>& dom_map);
213IntSet EvalSet(Range r, const ffi::Map<IterVar, IntSet>& dom_map);
214
223IntSet EvalSet(IntSet s, const std::unordered_map<const VarNode*, IntSet>& dom_map);
231IntSet EvalSet(Range r, const std::unordered_map<const VarNode*, IntSet>& dom_map);
239ffi::Array<IntSet> EvalSet(const ffi::Array<Range>& region, const ffi::Map<Var, IntSet>& dom_map);
241using ExprIntSetMap = std::unordered_map<PrimExpr, IntSet, ffi::ObjectPtrHash, ffi::ObjectPtrEqual>;
251 const std::unordered_map<const VarNode*, IntSet>& dom_map);
252
258IntSet Union(const ffi::Array<IntSet>& sets);
259
265ffi::Array<IntSet> UnionRegion(const ffi::Array<ffi::Array<IntSet>>& nd_int_sets);
266
272IntSet UnionLowerBound(const ffi::Array<IntSet>& sets);
273
279ffi::Array<IntSet> UnionRegionLowerBound(const ffi::Array<ffi::Array<IntSet>>& nd_int_sets);
280
286IntSet Intersect(const ffi::Array<IntSet>& sets);
287
293ffi::Map<Var, arith::IntSet> AsIntSet(const ffi::Map<Var, Range>& var_dom);
294
305TVM_DLL ffi::Optional<ffi::Array<IntSet>> EstimateRegionStrictBound(
306 const ffi::Array<Range>& region, const ffi::Map<Var, Range>& var_dom, const PrimExpr& predicate,
308
319TVM_DLL ffi::Optional<ffi::Array<IntSet>> EstimateRegionLowerBound(
320 const ffi::Array<Range>& region, const ffi::Map<Var, Range>& var_dom, const PrimExpr& predicate,
322
333TVM_DLL ffi::Array<IntSet> EstimateRegionUpperBound(const ffi::Array<Range>& region,
334 const ffi::Map<Var, Range>& var_dom,
335 const PrimExpr& predicate,
337
338} // namespace arith
339} // namespace tvm
340#endif // TVM_ARITH_INT_SET_H_
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Range container
Definition expr.h:610
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 Integer set containers. represent a set of integers in one dimension.
Definition int_set.h:59
TVM_FFI_DECLARE_OBJECT_INFO("ir.IntSet", IntSetNode, ffi::Object)
Managed reference to IntSetNode.
Definition int_set.h:68
static IntSet Vector(PrimExpr vec)
construct a integer set from vector expression.
bool MatchRange(const tvm::Range &r) const
Try to match IntSet with range r.
bool IsNothing() const
Range CoverRange(Range max_range) const
Find a range that covers the region.
bool HasLowerBound() const
static IntSet SinglePoint(PrimExpr point)
construct a point set.
static IntSet FromMinExtent(PrimExpr min, PrimExpr extent)
Construct a set representing a range [min, min + extent).
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(IntSet, ffi::ObjectRef, IntSetNode)
bool CanProveNonPositive() const
bool IsSinglePoint() const
bool HasUpperBound() const
bool IsEverything() const
bool CanProveNonNegative() const
SignType GetSignType() const
static IntSet Interval(PrimExpr min, PrimExpr max)
Construct a set representing a interval.
static IntSet Nothing()
bool CanProveNegative() const
bool CanProvePositive() const
PrimExpr max() const
static IntSet FromRange(tvm::Range r)
Construct a set representing a range.
static IntSet Everything()
PrimExpr min() const
bool CanProveSinglePoint(const Analyzer &ana) const
Check if we can prove it is a single point.
PrimExpr PointValue() const
The single point value, call only if IsSinglePoint is true.
Base expr nodes in TVM.
TIR expressions.
ffi::Optional< ffi::Array< IntSet > > EstimateRegionStrictBound(const ffi::Array< Range > &region, const ffi::Map< Var, Range > &var_dom, const PrimExpr &predicate, const arith::Analyzer &analyzer)
Analyze the region with affine map, given the domain of variables and their predicate....
std::unordered_map< PrimExpr, IntSet, ffi::ObjectPtrHash, ffi::ObjectPtrEqual > ExprIntSetMap
Map from Expr to IntSet.
Definition int_set.h:241
IntSet UnionLowerBound(const ffi::Array< IntSet > &sets)
Create a lower-bound of union set, where some of the segments may be dropped.
ffi::Optional< ffi::Array< IntSet > > EstimateRegionLowerBound(const ffi::Array< Range > &region, const ffi::Map< Var, Range > &var_dom, const PrimExpr &predicate, const arith::Analyzer &analyzer)
Analyze the region with affine map, given the domain of variables and their predicate....
ExprIntSetMap EvalSetForEachSubExpr(PrimExpr e, const std::unordered_map< const VarNode *, IntSet > &dom_map)
Find the integer set of every sub-expression, given the domain of each iteration variables.
ffi::Array< IntSet > UnionRegionLowerBound(const ffi::Array< ffi::Array< IntSet > > &nd_int_sets)
The union of N-dimensional integer sets.
ffi::Array< IntSet > EstimateRegionUpperBound(const ffi::Array< Range > &region, const ffi::Map< Var, Range > &var_dom, const PrimExpr &predicate, const arith::Analyzer &analyzer)
Analyze the region with affine map, given the domain of variables and their predicate Relaxation of t...
IntSet Union(const ffi::Array< IntSet > &sets)
Create a union set of all sets, possibly relaxed.
IntSet Intersect(const ffi::Array< IntSet > &sets)
Create an intersected set of all sets.
ffi::Array< IntSet > UnionRegion(const ffi::Array< ffi::Array< IntSet > > &nd_int_sets)
The union of N-dimensional integer sets.
ffi::Map< Var, arith::IntSet > AsIntSet(const ffi::Map< Var, Range > &var_dom)
Converts the Ranges to IntSets.
ffi::Map< Var, IntSet > ConvertDomMap(const std::unordered_map< const VarNode *, IntSet > &dom_map)
Convert std::unordered_map<const VarNode*, IntSet> to ffi::Map<Var, IntSet>
SignType
Sign type of an integer expression.
Definition int_set.h:52
@ kNegative
Definition int_set.h:52
@ kPositive
Definition int_set.h:52
@ kUnknown
Definition int_set.h:52
@ kZero
Definition int_set.h:52
IntSet EvalSet(PrimExpr e, const ffi::Map< IterVar, IntSet > &dom_map)
Find an symbolic integer set that contains all possible values of e given the domain of each iteratio...
tvm::Var Var
Definition var.h:38
tvm::VarNode VarNode
Definition var.h:37
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
Variables in the TIR.