tvm
Loading...
Searching...
No Matches
ir.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_TIRX_SCRIPT_IR_BUILDER_IR_H_
20#define TVM_TIRX_SCRIPT_IR_BUILDER_IR_H_
21
22#include <tvm/ffi/container/tuple.h>
23#include <tvm/ffi/container/variant.h>
24#include <tvm/ir/prim/op.h>
25#include <tvm/runtime/tensor.h>
28#include <tvm/tirx/stmt.h>
30#include <tvm/tirx/type.h>
31
32namespace tvm {
33namespace script {
34namespace ir_builder {
35namespace tirx {
36
37using ir::Assert;
38using ir::Bind;
39using ir::Break;
40using ir::Continue;
41using ir::Else;
42using ir::Evaluate;
43using ir::For;
44using ir::Grid;
45using ir::If;
46using ir::Region;
47using ir::Return;
48using ir::Then;
49using ir::While;
50
51using tvm::Var;
52using tvm::ffi::Tensor;
53using tvm::ffi::Tuple;
54using tvm::ffi::Variant;
57
62FunctionFrame Function(bool is_private, bool persistent = false);
63
65FunctionFrame DeclFunction(bool is_private = false, bool persistent = false);
66
73Var Arg(ffi::String name, Var var);
74
81TensorVar Arg(ffi::String name, TensorVar tensor);
82
87void FuncName(ffi::String name);
88
93void FuncAttrs(ffi::Map<ffi::String, ffi::Any> attrs);
94
100Type FuncRet(Type ret_type);
101
109tvm::Stmt TensorStore(Expr dest, ffi::Array<PrimExpr> indices, PrimExpr value);
110
121inline Var Handle(ffi::Optional<PrimType> dtype = std::nullopt,
122 ffi::String storage_scope = "global") {
123 Type type_annotation = dtype.has_value() ? Type(PtrType(dtype.value(), storage_scope))
124 : Type(PtrType::VoidPtrType(storage_scope));
125 return tvm::Var("", type_annotation);
126}
127
129
130#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName, DType) \
131 inline PrimExpr FuncName(ffi::Optional<PrimExpr> expr = std::nullopt) { \
132 PrimType dtype = DType; \
133 return expr.has_value() ? tvm::prim::cast(dtype, expr.value()) \
134 : tvm::Var("", dtype).as_or_throw<PrimExpr>(); \
135 }
136
137#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES(DType, Code) \
138 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##8, (PrimType(DLDataType{Code, 8, 1}))); \
139 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##16, (PrimType(DLDataType{Code, 16, 1}))); \
140 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##32, (PrimType(DLDataType{Code, 32, 1}))); \
141 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##64, (PrimType(DLDataType{Code, 64, 1})));
142
147
148#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(FuncName, Code, Size) \
149 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x2, (PrimType(DLDataType{Code, Size, 2}))) \
150 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x4, (PrimType(DLDataType{Code, Size, 4}))); \
151 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x8, (PrimType(DLDataType{Code, Size, 8}))); \
152 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x16, (PrimType(DLDataType{Code, Size, 16}))); \
153 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x32, (PrimType(DLDataType{Code, Size, 32}))); \
154 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x64, (PrimType(DLDataType{Code, Size, 64})));
155
156#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES_LANES(DType, Code) \
157 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##8, Code, 8); \
158 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##16, Code, 16); \
159 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##32, Code, 32); \
160 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##64, Code, 64);
161
166
167#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES_FIXED_SIZE(DType, Code, Bits) \
168 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType, (PrimType(DLDataType{Code, Bits, 1}))); \
169 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x2, (PrimType(DLDataType{Code, Bits, 2}))); \
170 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x4, (PrimType(DLDataType{Code, Bits, 4}))); \
171 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x8, (PrimType(DLDataType{Code, Bits, 8}))); \
172 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x16, (PrimType(DLDataType{Code, Bits, 16}))); \
173 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x32, (PrimType(DLDataType{Code, Bits, 32}))); \
174 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x64, (PrimType(DLDataType{Code, Bits, 64})));
175
184
187
189
192
193#undef TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST
194
195} // namespace tirx
196} // namespace ir_builder
197} // namespace script
198} // namespace tvm
199
200#endif // TVM_TIRX_SCRIPT_IR_BUILDER_IR_H_
Managed reference to ExprNode.
Definition base_expr.h:359
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:447
static TVM_TIRX_DLL PrimType Void()
Construct the void sentinel type, encoded as handle(0, 0).
static TVM_TIRX_DLL PrimType Bool(int lanes=1)
Construct a boolean type with fixed lanes.
Definition type.h:127
static TVM_TIRX_DLL PtrType VoidPtrType(ffi::String storage_scope="")
Construct an opaque pointer with void element type.
Container of all statements.
Definition stmt.h:62
Managed reference to TensorStoreNode.
Definition stmt.h:335
Managed reference to TypeNode.
Definition base_expr.h:80
Managed reference to VarNode.
Definition expr.h:714
Managed reference to FunctionFrameObj.
Definition frame.h:114
Definition exec_scope.h:47
Managed reference to TensorMapTypeNode.
Definition type.h:178
Checked zero-state view over an ordinary VarNode with TensorType.
Definition expr.h:50
Shared primitive construction and constant helpers.
WhileFrame While(PrimExpr condition)
Create a while loop.
IfFrame If(PrimExpr condition)
Create an if statement.
tvm::Stmt Evaluate(Expr value)
Evaluate the input expression.
ForFrame For(ForKind kind, PrimExpr start, PrimExpr stop, ffi::Optional< PrimExpr > step=std::nullopt, ffi::Optional< PrimType > dtype=std::nullopt, ffi::Optional< ffi::Map< ffi::String, Any > > annotations=std::nullopt)
Construct a For statement of the given kind.
Var Bind(Expr value, ffi::Optional< Type > type_annotation=std::nullopt, ffi::Optional< Var > var=std::nullopt)
Create a Bind (variable binding).
ThenFrame Then()
Create a then.
ElseFrame Else()
Create an else.
ForFrame Grid(ffi::Array< ffi::Variant< PrimExpr, ffi::Tuple< PrimExpr, PrimExpr > > > extents, ffi::Optional< PrimType > dtype=std::nullopt)
The grid For statement.
tvm::Stmt Continue()
Create a continue statement.
tvm::Stmt Return(Expr value)
Create a return statement.
AssertFrame Assert(PrimExpr condition, ffi::String error_kind, ffi::Array< ffi::String > message_parts)
The assertion statement.
RegionFrame Region(Op op, ffi::Array< Expr > args, ffi::Optional< ffi::Array< Var > > body_params=std::nullopt, DictAttrs attrs=DictAttrs())
Construct a result-free region with lexical body parameters.
tvm::Stmt Break()
Create a break statement.
PrimExpr Float8E4M3(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:177
PrimExpr Float8E5M2(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:181
PrimExpr Float6E3M2FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:186
PrimExpr Float8E5M2FNUZ(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:182
FunctionFrame DeclFunction(bool is_private=false, bool persistent=false)
Construct a bodyless function signature using the ordinary signature operations.
Var TensorMap()
Definition ir.h:128
PrimExpr Float8E4M3FNUZ(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:180
void FuncName(ffi::String name)
The Function naming statement.
Var Handle(ffi::Optional< PrimType > dtype=std::nullopt, ffi::String storage_scope="global")
Create a TIR var that represents a pointer.
Definition ir.h:121
PrimExpr Float8E8M0FNU(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:183
PrimExpr Float6E2M3FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:185
void FuncAttrs(ffi::Map< ffi::String, ffi::Any > attrs)
The Function annotation statement.
FunctionFrame Function(bool is_private, bool persistent=false)
The primitive function statement.
PrimExpr Float8E3M4(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:176
PrimExpr Void(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:191
Var Arg(ffi::String name, Var var)
The Function variable arguments adding function.
PrimExpr Boolean(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:190
PrimExpr Float4E2M1FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:188
PrimExpr Float8E4M3B11FNUZ(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:178
Type FuncRet(Type ret_type)
The Function return type statement.
PrimExpr Float8E4M3FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:179
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition attr.h:23
Device-independent tensor allocation, copies, views and serialization.
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES_LANES(DType, Code)
Definition ir.h:156
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName, DType)
Definition ir.h:130
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES(DType, Code)
Definition ir.h:137
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES_FIXED_SIZE(DType, Code, Bits)
Definition ir.h:167
TIRx statements and shared statement entry header.
Types specific to TIRX.