forked from apache/tvm
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconcrete_schedule.h
244 lines (215 loc) · 7.95 KB
/
concrete_schedule.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
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
/*
* 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_TIR_SCHEDULE_CONCRETE_SCHEDULE_H_
#define TVM_TIR_SCHEDULE_CONCRETE_SCHEDULE_H_
#include <tvm/arith/analyzer.h>
#include <tvm/tir/schedule/schedule.h>
#include <memory>
#include <utility>
#include "./utils.h"
namespace tvm {
namespace tir {
class ConcreteScheduleNode : public ScheduleNode {
friend class Schedule;
friend class ScheduleCopier;
public:
using TSymbolTable = Map<ObjectRef, ObjectRef>;
protected:
/*! \brief The internal state of scheduling */
ScheduleState state_;
/*! \brief A symbol table that maps random variables to concrete StmtSRef/Integers */
TSymbolTable symbol_table_;
/*! \brief A persistent stateless arithmetic analyzer. */
std::unique_ptr<arith::Analyzer> analyzer_;
public:
void VisitAttrs(tvm::AttrVisitor* v) {
// `state_` is not visited
// `symbol_table_` is not visited
// `analyzer_` is not visitied
}
virtual ~ConcreteScheduleNode() = default;
static constexpr const char* _type_key = "tir.ConcreteSchedule";
TVM_DECLARE_BASE_OBJECT_INFO(ConcreteScheduleNode, ScheduleNode);
public:
ScheduleState state() const final { return state_; }
Schedule Copy() const override;
public:
/******** Lookup random variables ********/
inline Block Get(const BlockRV& block_rv) const final;
inline For Get(const LoopRV& loop_rv) const final;
inline int64_t Get(const IntRV& int_rv) const final;
inline StmtSRef GetSRef(const BlockRV& block_rv) const final;
inline StmtSRef GetSRef(const LoopRV& loop_rv) const final;
void RemoveRV(const BlockRV& block_rv) final { RemoveFromSymbolTable(block_rv); }
void RemoveRV(const LoopRV& loop_rv) final { RemoveFromSymbolTable(loop_rv); }
void RemoveRV(const IntRV& int_rv) final { RemoveFromSymbolTable(int_rv); }
using ScheduleNode::GetSRef;
public:
/******** Block/Loop relation ********/
BlockRV GetBlock(const String& name, const String& func_name = "main") override;
Array<LoopRV> GetLoops(const BlockRV& block_rv) override;
/******** Utility functions ********/
protected:
/*!
* \brief Copy the schedule state, as well as the symbol table
* \param new_state The ScheduleState copied
* \param new_symbol_table The symbol table copied
*/
void Copy(ScheduleState* new_state, TSymbolTable* new_symbol_table) const;
/*!
* \brief Add srefs as random variables into the symbol table
* \tparam T The type of the random variables
* \param srefs The srefs to be added to the symbol table
* \return The new random variables created
*/
template <class T>
inline Array<T> CreateRV(const Array<StmtSRef>& srefs);
/*!
* \brief Add an sref as a random variable into the symbol table
* \tparam T The type of the random variable
* \param sref The sref to be added to the symbol table
* \return The new random variable created
*/
template <class T>
inline T CreateRV(const StmtSRef& sref);
/*!
* \brief Add an integer as a random variable into the symbol table
* \param number The integer to be added to the symbol table
* \return The new random variable created
*/
inline IntRV CreateRV(int64_t number);
/*!
* \brief Add integers as random variables into the symbol table
* \param numbers The integers to be added to the symbol table
* \return The new random variables created
*/
inline Array<IntRV> CreateRV(const Array<Integer>& numbers);
/*! \brief Remove a random variable from the symbol table */
inline void RemoveFromSymbolTable(const ObjectRef& rv);
};
// implementations
/******** Lookup random variables ********/
inline Block ConcreteScheduleNode::Get(const BlockRV& block_rv) const {
StmtSRef sref = this->GetSRef(block_rv);
const auto* block = TVM_SREF_TO_BLOCK(block, sref);
return GetRef<Block>(block);
}
inline For ConcreteScheduleNode::Get(const LoopRV& loop_rv) const {
StmtSRef sref = this->GetSRef(loop_rv);
const auto* loop = TVM_SREF_TO_FOR(loop, sref);
return GetRef<For>(loop);
}
inline int64_t ConcreteScheduleNode::Get(const IntRV& int_rv) const {
auto it = this->symbol_table_.find(int_rv);
if (it == this->symbol_table_.end()) {
LOG(FATAL) << "IndexError: Cannot find corresponding IntRV: " << int_rv;
}
const ObjectRef& obj = (*it).second;
const auto* int_imm = obj.as<IntImmNode>();
if (int_imm == nullptr) {
LOG(FATAL) << "ValueError: IntRV's corresponding type is invalid: "
<< (obj.defined() ? obj->GetTypeKey() : "None");
}
return int_imm->value;
}
inline StmtSRef ConcreteScheduleNode::GetSRef(const BlockRV& block_rv) const {
auto it = this->symbol_table_.find(block_rv);
if (it == this->symbol_table_.end()) {
LOG(FATAL) << "IndexError: Cannot find corresponding BlockRV: " << block_rv;
}
const ObjectRef& obj = (*it).second;
const auto* sref = obj.as<StmtSRefNode>();
if (sref == nullptr) {
LOG(FATAL) << "ValueError: BlockRV's corresponding type is invalid: "
<< (obj.defined() ? obj->GetTypeKey() : "None");
}
if (sref->stmt == nullptr) {
LOG(FATAL) << "ValueError: The StmtSRef has expired";
}
return GetRef<StmtSRef>(sref);
}
inline StmtSRef ConcreteScheduleNode::GetSRef(const LoopRV& loop_rv) const {
static StmtSRef inline_mark = StmtSRef::InlineMark();
static StmtSRef root_mark = StmtSRef::RootMark();
auto it = this->symbol_table_.find(loop_rv);
if (it == this->symbol_table_.end()) {
LOG(FATAL) << "IndexError: Cannot find corresponding LoopRV: " << loop_rv;
}
const ObjectRef& obj = (*it).second;
if (obj.same_as(inline_mark)) {
return inline_mark;
}
if (obj.same_as(root_mark)) {
return root_mark;
}
const auto* sref = obj.as<StmtSRefNode>();
if (sref == nullptr) {
LOG(FATAL) << "ValueError: LoopRV's corresponding type is invalid: "
<< (obj.defined() ? obj->GetTypeKey() : "None");
}
if (sref->stmt == nullptr) {
LOG(FATAL) << "ValueError: The StmtSRef has expired";
}
return GetRef<StmtSRef>(sref);
}
/******** Adding/Removing elements in the symbol table ********/
template <class T>
inline Array<T> ConcreteScheduleNode::CreateRV(const Array<StmtSRef>& srefs) {
Array<T> result;
result.reserve(srefs.size());
for (const StmtSRef& sref : srefs) {
T rv;
this->symbol_table_.Set(rv, sref);
result.push_back(rv);
}
return result;
}
template <class T>
inline T ConcreteScheduleNode::CreateRV(const StmtSRef& sref) {
T rv;
this->symbol_table_.Set(rv, sref);
return rv;
}
inline IntRV ConcreteScheduleNode::CreateRV(int64_t number) {
Var rv;
this->symbol_table_.Set(rv, Integer(number));
return std::move(rv);
}
inline Array<IntRV> ConcreteScheduleNode::CreateRV(const Array<Integer>& numbers) {
Array<IntRV> result;
result.reserve(numbers.size());
for (int64_t number : numbers) {
Var rv;
this->symbol_table_.Set(rv, IntImm(DataType::Int(32), number));
result.push_back(rv);
}
return result;
}
inline void ConcreteScheduleNode::RemoveFromSymbolTable(const ObjectRef& obj) {
auto it = this->symbol_table_.find(obj);
if (it != this->symbol_table_.end()) {
this->symbol_table_.erase(obj);
} else {
LOG(FATAL) << "IndexError: Cannot find the object in the symbol table: " << obj;
throw;
}
}
} // namespace tir
} // namespace tvm
#endif // TVM_TIR_SCHEDULE_CONCRETE_SCHEDULE_H_