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
29namespace s_tir {
30
31// Forward declaration
32class Schedule;
33
43 ffi::TypedFunction<ffi::Array<Any>(Schedule sch, const ffi::Array<Any>& inputs,
44 const ffi::Array<Any>& attrs, const Any& decision)>;
45
55 ffi::TypedFunction<ffi::String(const ffi::Array<Any>& inputs, const ffi::Array<Any>& attrs,
56 const Any& decision, const ffi::Array<ffi::String>& outputs)>;
57
64using FInstructionAttrsAsJSON = ffi::TypedFunction<ffi::ObjectRef(ffi::Array<Any> attrs)>;
65
72using FInstructionAttrsFromJSON = ffi::TypedFunction<ffi::Array<Any>(ffi::ObjectRef json_attrs)>;
73
86class InstructionKindObj : public ffi::Object {
87 public:
89 ffi::String name;
96 bool is_pure{false};
111
112 static void RegisterReflection() {
113 namespace refl = tvm::ffi::reflection;
114 refl::ObjectDef<InstructionKindObj>()
115 .def_ro("name", &InstructionKindObj::name)
116 .def_ro("_is_pure", &InstructionKindObj::is_pure);
117 }
118
120 bool IsPostproc() const;
121 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.InstructionKind", InstructionKindObj, ffi::Object);
122};
123
128class InstructionKind : public ffi::ObjectRef {
129 public:
130 explicit InstructionKind(ffi::UnsafeInit tag, ffi::ObjectPtr<InstructionKindObj> data)
131 : ffi::ObjectRef(tag) {
132 data_ = std::move(data);
133 }
134
140 static InstructionKind Get(const ffi::String& name);
143};
144
146class InstructionNode : public ffi::Object {
147 public:
148 explicit InstructionNode(ffi::UnsafeInit tag) : kind(tag) {}
149
150 explicit InstructionNode(InstructionKind kind) : kind(std::move(kind)) {}
164 ffi::Array<Any> inputs;
170 ffi::Array<Any> attrs;
177 ffi::Array<Any> outputs;
178
179 static void RegisterReflection() {
180 namespace refl = tvm::ffi::reflection;
181 refl::ObjectDef<InstructionNode>()
182 .def_ro("kind", &InstructionNode::kind)
183 .def_ro("inputs", &InstructionNode::inputs)
184 .def_ro("attrs", &InstructionNode::attrs)
185 .def_ro("outputs", &InstructionNode::outputs);
186 }
187 TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Instruction", InstructionNode, ffi::Object);
188};
189
194class Instruction : public ffi::ObjectRef {
195 public:
203 explicit Instruction(InstructionKind kind, ffi::Array<Any> inputs, ffi::Array<Any> attrs,
204 ffi::Array<Any> outputs);
205
207};
208
211 public:
215 explicit InstructionKindDef(const ffi::String& name);
216
222 get_mutable()->is_pure = is_pure;
223 return *this;
224 }
225
231 get_mutable()->f_apply_to_schedule = std::move(f_apply_to_schedule);
232 return *this;
233 }
234
240 get_mutable()->f_as_python = std::move(f_as_python);
241 return *this;
242 }
243
249 get_mutable()->f_attrs_as_json = std::move(f_attrs_as_json);
250 return *this;
251 }
252
258 get_mutable()->f_attrs_from_json = std::move(f_attrs_from_json);
259 return *this;
260 }
261
262 private:
263 InstructionKindObj* get_mutable() const {
264 return const_cast<InstructionKindObj*>(inst_kind_.get());
265 }
266
267 InstructionKind inst_kind_;
268};
269
270} // namespace s_tir
271} // namespace tvm
272
273#endif // TVM_S_TIR_SCHEDULE_INSTRUCTION_H_
Temporary builder for a canonical named instruction kind.
Definition instruction.h:210
InstructionKindDef & set_attrs_as_json(FInstructionAttrsAsJSON f_attrs_as_json)
Set the optional JSON serialization callback.
Definition instruction.h:248
InstructionKindDef & set_as_python(FInstructionAsPython f_as_python)
Set the Python trace formatting callback.
Definition instruction.h:239
InstructionKindDef & set_attrs_from_json(FInstructionAttrsFromJSON f_attrs_from_json)
Set the optional JSON deserialization callback.
Definition instruction.h:257
InstructionKindDef(const ffi::String &name)
Find or create the canonical kind and assign its name.
InstructionKindDef & set_is_pure(bool is_pure)
Set whether removing this instruction leaves schedule state unchanged.
Definition instruction.h:221
InstructionKindDef & set_apply_to_schedule(FInstructionApply f_apply_to_schedule)
Set the schedule application callback.
Definition instruction.h:230
Kind of an instruction, e.g. Split, Reorder, etc. Besides the name, every kind of instruction has its...
Definition instruction.h:86
FInstructionAttrsAsJSON f_attrs_as_json
A functor that serialize its attributes to JSON.
Definition instruction.h:105
ffi::String name
The name of a kind of instructions.
Definition instruction.h:89
bool IsPostproc() const
Checks if the instruction kind is EnterPostproc.
FInstructionApply f_apply_to_schedule
A functor that applies the instruction to a TensorIR schedule.
Definition instruction.h:98
FInstructionAttrsFromJSON f_attrs_from_json
A functor that deserialize its attributes from JSON.
Definition instruction.h:110
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.InstructionKind", InstructionKindObj, ffi::Object)
static void RegisterReflection()
Definition instruction.h:112
bool is_pure
Indicates if the instruction is pure, i.e. removing it alone doesn't mutate the schedule state....
Definition instruction.h:96
FInstructionAsPython f_as_python
A functor that converts the instruction to a statement in python syntax.
Definition instruction.h:100
Managed reference to InstructionKindObj.
Definition instruction.h:128
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(InstructionKind, ffi::ObjectRef, InstructionKindObj)
static InstructionKind Get(const ffi::String &name)
Retrieve an InstructionKind using its name.
InstructionKind(ffi::UnsafeInit tag, ffi::ObjectPtr< InstructionKindObj > data)
Definition instruction.h:130
Schedule instructions each corresponds to a schedule primitive.
Definition instruction.h:146
InstructionKind kind
The kind of the instruction.
Definition instruction.h:152
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.Instruction", InstructionNode, ffi::Object)
InstructionNode(ffi::UnsafeInit tag)
Definition instruction.h:148
InstructionNode(InstructionKind kind)
Definition instruction.h:150
static void RegisterReflection()
Definition instruction.h:179
ffi::Array< Any > attrs
The attributes of the instruction. Similar to attributes of an operator, attributes of an instruction...
Definition instruction.h:170
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:164
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:177
Managed reference to InstructionNode.
Definition instruction.h:194
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Instruction, ffi::ObjectRef, InstructionNode)
Instruction(InstructionKind kind, ffi::Array< Any > inputs, ffi::Array< Any > attrs, ffi::Array< Any > outputs)
Constructor.
Managed reference to ScheduleObj.
Definition schedule.h:887
TIR expressions.
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:44
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:56
ffi::TypedFunction< ffi::ObjectRef(ffi::Array< Any > attrs)> FInstructionAttrsAsJSON
Type of the functor that serialize its attributes to JSON.
Definition instruction.h:64
ffi::TypedFunction< ffi::Array< Any >(ffi::ObjectRef json_attrs)> FInstructionAttrsFromJSON
Type of the functor that deserialize its attributes from JSON.
Definition instruction.h:72
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23