tvm
Loading...
Searching...
No Matches
instruction.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#ifndef TVM_S_TIR_SCHEDULE_INSTRUCTION_H_
20#define TVM_S_TIR_SCHEDULE_INSTRUCTION_H_
21
22#include <tvm/ffi/reflection/registry.h>
23#include <tvm/ir/prim/expr.h>
24
25#include <utility>
26
27namespace tvm {
28
29// Forward declaration
30template <typename, typename>
32
33namespace s_tir {
34using namespace tvm::prim;
35using namespace tvm::tirx;
36
37// Forward declaration
38class Schedule;
39
49 ffi::TypedFunction<ffi::Array<Any>(Schedule sch, const ffi::Array<Any>& inputs,
50 const ffi::Array<Any>& attrs, const Any& decision)>;
51
61 ffi::TypedFunction<ffi::String(const ffi::Array<Any>& inputs, const ffi::Array<Any>& attrs,
62 const Any& decision, const ffi::Array<ffi::String>& outputs)>;
63
70using FInstructionAttrsAsJSON = ffi::TypedFunction<ffi::ObjectRef(ffi::Array<Any> attrs)>;
71
78using FInstructionAttrsFromJSON = ffi::TypedFunction<ffi::Array<Any>(ffi::ObjectRef json_attrs)>;
79
92class InstructionKindNode : public ffi::Object {
93 public:
95 ffi::String name;
102 bool is_pure{false};
117
118 static void RegisterReflection() {
119 namespace refl = tvm::ffi::reflection;
120 refl::ObjectDef<InstructionKindNode>()
121 .def_ro("name", &InstructionKindNode::name)
122 .def_ro("_is_pure", &InstructionKindNode::is_pure);
123 }
124
126 bool IsPostproc() const;
127 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.InstructionKind", InstructionKindNode, ffi::Object);
128};
129
134class InstructionKind : public ffi::ObjectRef {
135 public:
141 static InstructionKind Get(const ffi::String& name);
143};
144
146class InstructionNode : public ffi::Object {
147 public:
161 ffi::Array<Any> inputs;
167 ffi::Array<Any> attrs;
174 ffi::Array<Any> outputs;
175
176 static void RegisterReflection() {
177 namespace refl = tvm::ffi::reflection;
178 refl::ObjectDef<InstructionNode>()
179 .def_ro("kind", &InstructionNode::kind)
180 .def_ro("inputs", &InstructionNode::inputs)
181 .def_ro("attrs", &InstructionNode::attrs)
182 .def_ro("outputs", &InstructionNode::outputs);
183 }
184 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Instruction", InstructionNode, ffi::Object);
185};
186
191class Instruction : public ffi::ObjectRef {
192 public:
200 explicit Instruction(InstructionKind kind, ffi::Array<Any> inputs, ffi::Array<Any> attrs,
201 ffi::Array<Any> outputs);
202
204};
205
211#define TVM_INST_KIND_REGISTER_VAR_DEF \
212 [[maybe_unused]] static ::tvm::s_tir::InstructionKindRegEntry& __make_##InstructionKind
213
231#define TVM_REGISTER_INST_KIND(InstructionKindName) \
232 TVM_FFI_STR_CONCAT(TVM_INST_KIND_REGISTER_VAR_DEF, __COUNTER__) = \
233 ::tvm::s_tir::InstructionKindRegEntry::RegisterOrGet(InstructionKindName).set_name()
234
237 public:
238 static InstructionKindRegEntry& RegisterOrGet(const ffi::String& name);
239
241 get_mutable()->name = this->name;
242 return *this;
243 }
244
246 get_mutable()->is_pure = is_pure;
247 return *this;
248 }
249
251 get_mutable()->f_apply_to_schedule = std::move(f_apply_to_schedule);
252 return *this;
253 }
254
256 get_mutable()->f_as_python = std::move(f_as_python);
257 return *this;
258 }
259
261 get_mutable()->f_attrs_as_json = std::move(f_attrs_as_json);
262 return *this;
263 }
264
266 get_mutable()->f_attrs_from_json = std::move(f_attrs_from_json);
267 return *this;
268 }
269
270 private:
274 InstructionKindNode* get_mutable() const {
275 return const_cast<InstructionKindNode*>(inst_kind_.get());
276 }
277
279 ffi::String name;
281 InstructionKind inst_kind_;
282 template <typename, typename>
283 friend class ::tvm::AttrRegistry;
284 friend class InstructionKind;
285};
286
287} // namespace s_tir
288} // namespace tvm
289
290#endif // TVM_S_TIR_SCHEDULE_INSTRUCTION_H_
Definition instruction.h:31
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Kind of an instruction, e.g. Split, Reorder, etc. Besides the name, every kind of instruction has its...
Definition instruction.h:92
FInstructionApply f_apply_to_schedule
A functor that applies the instruction to a TensorIR schedule.
Definition instruction.h:104
ffi::String name
The name of a kind of instructions.
Definition instruction.h:95
bool IsPostproc() const
Checks if the instruction kind is EnterPostproc.
FInstructionAttrsFromJSON f_attrs_from_json
A functor that deserialize its attributes from JSON.
Definition instruction.h:116
bool is_pure
Indicates if the instruction is pure, i.e. removing it alone doesn't mutate the schedule state....
Definition instruction.h:102
FInstructionAttrsAsJSON f_attrs_as_json
A functor that serialize its attributes to JSON.
Definition instruction.h:111
static void RegisterReflection()
Definition instruction.h:118
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.InstructionKind", InstructionKindNode, ffi::Object)
FInstructionAsPython f_as_python
A functor that converts the instruction to a statement in python syntax.
Definition instruction.h:106
An entry in the registry of InstructionKind.
Definition instruction.h:236
InstructionKindRegEntry & set_attrs_from_json(FInstructionAttrsFromJSON f_attrs_from_json)
Definition instruction.h:265
InstructionKindRegEntry & set_apply_to_schedule(FInstructionApply f_apply_to_schedule)
Definition instruction.h:250
InstructionKindRegEntry & set_attrs_as_json(FInstructionAttrsAsJSON f_attrs_as_json)
Definition instruction.h:260
InstructionKindRegEntry & set_name()
Definition instruction.h:240
static InstructionKindRegEntry & RegisterOrGet(const ffi::String &name)
InstructionKindRegEntry & set_is_pure(bool is_pure)
Definition instruction.h:245
friend class InstructionKind
Definition instruction.h:284
InstructionKindRegEntry & set_as_python(FInstructionAsPython f_as_python)
Definition instruction.h:255
Managed reference to InstructionKindNode.
Definition instruction.h:134
static InstructionKind Get(const ffi::String &name)
Retrieve an InstructionKind using its name.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(InstructionKind, ffi::ObjectRef, InstructionKindNode)
Schedule instructions each corresponds to a schedule primitive.
Definition instruction.h:146
InstructionKind kind
The kind of the instruction.
Definition instruction.h:149
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Instruction", InstructionNode, ffi::Object)
static void RegisterReflection()
Definition instruction.h:176
ffi::Array< Any > attrs
The attributes of the instruction. Similar to attributes of an operator, attributes of an instruction...
Definition instruction.h:167
ffi::Array< Any > inputs
The input random variables of the instruction, and the type of each element can be one of the followi...
Definition instruction.h:161
ffi::Array< Any > outputs
The output random variables of the instruction, and the type of each element can be one of the follow...
Definition instruction.h:174
Managed reference to InstructionNode.
Definition instruction.h:191
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NULLABLE(Instruction, ffi::ObjectRef, InstructionNode)
Instruction(InstructionKind kind, ffi::Array< Any > inputs, ffi::Array< Any > attrs, ffi::Array< Any > outputs)
Constructor.
Managed reference to ScheduleNode.
Definition schedule.h:887
TIR expressions.
Definition builtin.h:25
ffi::TypedFunction< ffi::Array< Any >(Schedule sch, const ffi::Array< Any > &inputs, const ffi::Array< Any > &attrs, const Any &decision)> FInstructionApply
Type of the functor that applies the instruction to a TensorIR schedule.
Definition instruction.h:50
ffi::TypedFunction< ffi::String(const ffi::Array< Any > &inputs, const ffi::Array< Any > &attrs, const Any &decision, const ffi::Array< ffi::String > &outputs)> FInstructionAsPython
Type of the functor that converts the instruction to a statement in python syntax.
Definition instruction.h:62
ffi::TypedFunction< ffi::ObjectRef(ffi::Array< Any > attrs)> FInstructionAttrsAsJSON
Type of the functor that serialize its attributes to JSON.
Definition instruction.h:70
ffi::TypedFunction< ffi::Array< Any >(ffi::ObjectRef json_attrs)> FInstructionAttrsFromJSON
Type of the functor that deserialize its attributes from JSON.
Definition instruction.h:78
Definition axis_group_graph.h:39
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40