-
Notifications
You must be signed in to change notification settings - Fork 3.6k
/
Copy pathnd_int_set.h
150 lines (137 loc) · 4.48 KB
/
nd_int_set.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
#ifndef TVM_SUPPORT_ND_INT_SET_H_
#define TVM_SUPPORT_ND_INT_SET_H_
#include <tvm/arith/int_set.h>
#include <tvm/ir/expr.h>
#include <unordered_map>
#include <vector>
namespace tvm {
namespace support {
/*! \brief An N-dimensional integer set representing a rectangle region */
using NDIntSet = std::vector<arith::IntSet>;
/*!
* \brief Construct an N-dimensional integer set representing a region.
* \param region The region.
* \return The constructed set.
*/
inline NDIntSet NDIntSetFromRegion(const tir::Region& region) {
NDIntSet result;
result.reserve(region.size());
for (const Range& range : region) {
result.push_back(arith::IntSet::FromRange(range));
}
return result;
}
/*!
* \brief Construct an N-dimensional integer set representing a shape.
* \param shape The shape which is an array of the length of each dimension.
* \return The constructed set.
*/
inline NDIntSet NDIntSetFromShape(const Array<PrimExpr>& shape) {
PrimExpr zero = Integer(0);
NDIntSet result;
result.reserve(shape.size());
for (const PrimExpr& extent : shape) {
result.push_back(arith::IntSet::FromMinExtent(zero, extent));
}
return result;
}
/*!
* \brief Construct an N-dimensional integer set representing a point.
* \param indices The N-dimensional indices representing the point.
* \return The constructed set.
*/
inline NDIntSet NDIntSetFromPoint(const Array<PrimExpr>& indices) {
NDIntSet result;
result.reserve(indices.size());
for (const PrimExpr& index : indices) {
result.push_back(arith::IntSet::SinglePoint(index));
}
return result;
}
/*!
* \brief Create a union set of two sets, possibly relaxed. The RHS set will be combined into the
* LHS set.
* \param lhs The first N-dimensional integer set
* \param rhs The second N-dimensional integer set
*/
inline void NDIntSetUnionWith(NDIntSet* lhs, const NDIntSet& rhs) {
ICHECK_EQ(lhs->size(), rhs.size());
int ndim = rhs.size();
for (int i = 0; i < ndim; ++i) {
arith::IntSet& int_set = lhs->at(i);
int_set = arith::Union({int_set, rhs.at(i)});
}
}
/*!
* \brief Union a list of N-dimensional integer sets
* \param nd_int_sets The N-dimensional integer sets to be merged.
* \return The result of the union
*/
inline NDIntSet NDIntSetUnion(const std::vector<NDIntSet>& nd_int_sets) {
ICHECK(!nd_int_sets.empty());
int n = nd_int_sets.size();
if (n == 1) {
return nd_int_sets[0];
}
int ndim = nd_int_sets[0].size();
for (int i = 1; i < n; ++i) {
ICHECK_EQ(nd_int_sets[i].size(), ndim);
}
NDIntSet result;
result.reserve(ndim);
Array<arith::IntSet> int_sets(n, arith::IntSet{nullptr});
for (int dim = 0; dim < ndim; ++dim) {
for (int i = 0; i < n; ++i) {
int_sets.Set(i, nd_int_sets[i][dim]);
}
result.push_back(arith::Union(int_sets));
}
return result;
}
/*!
* \brief Create an empty N-dimensional integer set.
* \param ndim The number of dimensions.
* \return The constructed set.
*/
inline NDIntSet NDIntSetEmpty(int ndim) {
return std::vector<arith::IntSet>(ndim, arith::IntSet::Nothing());
}
/*!
* \brief The N-dimensional version of EvalSet.
* \param nd_int_set The N-dimensional integer set to be evaluated.
* \param dom_map The domain of each variable.
* \return An N-dimensional integer set that can cover all the possible values of the N-dimensional
* integer set.
* \sa EvalSet
*/
inline NDIntSet NDIntSetEval(
const NDIntSet& nd_int_set,
const std::unordered_map<const tir::VarNode*, arith::IntSet>& dom_map) {
NDIntSet ret;
ret.reserve(nd_int_set.size());
for (const arith::IntSet& s : nd_int_set) {
ret.push_back(EvalSet(s, dom_map));
}
return ret;
}
} // namespace support
} // namespace tvm
#endif // TVM_SUPPORT_ND_INT_SET_H_