19#ifndef TVM_SCRIPT_IR_BUILDER_TIR_IR_H_
20#define TVM_SCRIPT_IR_BUILDER_TIR_IR_H_
22#include <tvm/ffi/container/tuple.h>
23#include <tvm/ffi/container/variant.h>
38using tvm::ffi::Variant;
59 ffi::Optional<Expr> data, ffi::Optional<ffi::Array<PrimExpr>> strides,
60 ffi::Optional<PrimExpr> elem_offset, ffi::String storage_scope,
int align,
61 int offset_factor, ffi::Optional<Layout> layout = std::nullopt,
62 ffi::Array<PrimExpr> allocated_addr = {});
96void FuncAttrs(ffi::Map<ffi::String, ffi::Any> attrs);
121 ffi::String storage_scope =
"global",
int align = -1,
int offset_factor = 0,
122 ffi::Optional<Layout> layout = std::nullopt);
144ffi::Array<tvm::tirx::Var>
ScopeId(ffi::Optional<ffi::Array<PrimExpr>> extents, ffi::String parent,
145 ffi::String name, ffi::String
cur,
148ffi::Array<tvm::tirx::Var>
ClusterId(ffi::Optional<ffi::Array<PrimExpr>> extents,
151ffi::Array<tvm::tirx::Var>
CtaId(ffi::Optional<ffi::Array<PrimExpr>> extents, ffi::String parent,
152 ffi::Optional<ffi::Array<PrimExpr>>
preferred = std::nullopt,
157ffi::Array<tvm::tirx::Var>
WarpgroupId(ffi::Optional<ffi::Array<PrimExpr>> extents,
160ffi::Array<tvm::tirx::Var>
WarpId(ffi::Optional<ffi::Array<PrimExpr>> extents, ffi::String parent,
163ffi::Array<tvm::tirx::Var>
ThreadId(ffi::Optional<ffi::Array<PrimExpr>> extents, ffi::String parent,
213 ffi::Optional<Expr> data = std::nullopt, ffi::Array<PrimExpr> strides = {},
215 int offset_factor = 0, ffi::Optional<Layout> layout = std::nullopt,
216 ffi::Array<PrimExpr> allocated_addr = {});
263ffi::Array<Var>
Remap(ffi::String
kinds, ffi::Array<PrimExpr> bindings,
279 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt,
280 ffi::Optional<PrimExpr> step = std::nullopt,
281 ffi::Optional<PrimType> dtype = std::nullopt);
292 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt,
293 ffi::Optional<PrimExpr> step = std::nullopt,
294 ffi::Optional<PrimType> dtype = std::nullopt);
305 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt,
306 ffi::Optional<PrimExpr> step = std::nullopt,
307 ffi::Optional<PrimType> dtype = std::nullopt);
318 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt,
319 ffi::Optional<PrimExpr> step = std::nullopt,
320 ffi::Optional<PrimType> dtype = std::nullopt);
330 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt);
339 ffi::Optional<PrimType> dtype = std::nullopt);
349 ffi::Array<ffi::String> message_parts);
364 ffi::Optional<Var> var = std::nullopt);
443 ffi::Optional<Expr> data, ffi::Optional<ffi::Array<PrimExpr>> strides,
444 ffi::Optional<PrimExpr> elem_offset, ffi::String storage_scope,
445 int align,
int offset_factor,
446 ffi::Optional<Layout> layout = std::nullopt,
447 ffi::Optional<PrimExpr> allocated_addr = std::nullopt);
458 ffi::String storage_scope =
"global",
459 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> annotations = std::nullopt);
485 ffi::Map<ffi::String, ffi::Any> config,
486 ffi::Optional<ffi::String> dispatch = std::nullopt);
520inline Var Handle(ffi::Optional<PrimType> dtype = std::nullopt,
521 ffi::String storage_scope =
"global") {
529#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName, DType) \
530 inline PrimExpr FuncName(ffi::Optional<PrimExpr> expr = std::nullopt) { \
531 PrimType dtype = DType; \
532 return expr.has_value() ? tvm::cast(dtype, expr.value()) \
533 : tvm::tirx::Var("", dtype).as_or_throw<PrimExpr>(); \
536#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES(DType, Code) \
537 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##8, (PrimType(DLDataType{Code, 8, 1}))); \
538 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##16, (PrimType(DLDataType{Code, 16, 1}))); \
539 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##32, (PrimType(DLDataType{Code, 32, 1}))); \
540 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##64, (PrimType(DLDataType{Code, 64, 1})));
547#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(FuncName, Code, Size) \
548 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x2, (PrimType(DLDataType{Code, Size, 2}))) \
549 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x4, (PrimType(DLDataType{Code, Size, 4}))); \
550 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x8, (PrimType(DLDataType{Code, Size, 8}))); \
551 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x16, (PrimType(DLDataType{Code, Size, 16}))); \
552 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x32, (PrimType(DLDataType{Code, Size, 32}))); \
553 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x64, (PrimType(DLDataType{Code, Size, 64})));
555#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES_LANES(DType, Code) \
556 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##8, Code, 8); \
557 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##16, Code, 16); \
558 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##32, Code, 32); \
559 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##64, Code, 64);
566#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES_FIXED_SIZE(DType, Code, Bits) \
567 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType, (PrimType(DLDataType{Code, Bits, 1}))); \
568 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x2, (PrimType(DLDataType{Code, Bits, 2}))); \
569 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x4, (PrimType(DLDataType{Code, Bits, 4}))); \
570 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x8, (PrimType(DLDataType{Code, Bits, 8}))); \
571 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x16, (PrimType(DLDataType{Code, Bits, 16}))); \
572 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x32, (PrimType(DLDataType{Code, Bits, 32}))); \
573 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x64, (PrimType(DLDataType{Code, Bits, 64})));
592#undef TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST
Managed reference to ExprNode.
Definition base_expr.h:335
static PointerType VoidPointerTy(ffi::String storage_scope="")
Construct an opaque pointer with void element type.
Typed reference/view over any Expr whose ExprNode::ty is PrimType.
Definition base_expr.h:401
Definition base_expr.h:137
static PrimType Float(int bits, int lanes=1)
Construct a floating-point type with fixed lanes.
static PrimType Void()
Construct the void sentinel type, encoded as handle(0, 0).
static PrimType Int(int bits, int lanes=1)
Construct a signed integer type with fixed lanes.
static PrimType Bool(int lanes=1)
Construct a boolean type with fixed lanes.
Range container
Definition expr.h:610
Managed reference to TensorMapTypeNode.
Definition type.h:199
Managed reference to TypeNode.
Definition base_expr.h:77
Managed reference to VarNode.
Definition expr.h:372
RAII wrapper function to enter and exit a context object similar to python's with syntax.
Definition with_context.h:59
Managed Tensor. The array is backed by reference counted blocks.
Definition tensor.h:49
Managed reference to AssertFrameNode.
Definition frame.h:345
Managed reference to AttrFrameNode.
Definition frame.h:437
Managed reference to BlockInitFrameNode.
Definition frame.h:241
Managed reference to ElseFrameNode.
Definition frame.h:601
Managed reference to ForFrameNode.
Definition frame.h:298
Managed reference to IfFrameNode.
Definition frame.h:521
Managed reference to LaunchThreadFrameNode.
Definition frame.h:390
Managed reference to PrimFuncFrameNode.
Definition frame.h:126
Managed reference to SBlockFrameNode.
Definition frame.h:200
Managed reference to ThenFrameNode.
Definition frame.h:561
Managed reference to WhileFrameNode.
Definition frame.h:476
Checked zero-state view over an ordinary VarNode with BufferType.
Definition buffer.h:179
Definition exec_scope.h:228
Managed reference to TilePrimitiveCallNode.
Definition tile_primitive.h:239
Var Spatial(Range dom, PrimExpr binding, PrimType dtype=PrimType::Int(32))
The spatial block axis defining function.
Var Reduce(Range dom, PrimExpr binding, PrimType dtype=PrimType::Int(32))
The reduced block axis defining function.
Var Opaque(Range dom, PrimExpr binding, PrimType dtype=PrimType::Int(32))
The opaque block axis defining function.
Var Scan(Range dom, PrimExpr binding, PrimType dtype=PrimType::Int(32))
The scanning block axis defining function.
ffi::Array< Var > Remap(ffi::String kinds, ffi::Array< PrimExpr > bindings, PrimType dtype=PrimType::Int(32))
The block axis remapping function.
WhileFrame While(PrimExpr condition)
Create a while loop.
PrimExpr Float8E4M3(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:576
Var Bind(Expr value, ffi::Optional< Type > type_annotation=std::nullopt, ffi::Optional< Var > var=std::nullopt)
Create a Bind (variable binding).
BlockInitFrame Init()
The block initialization statement.
BufferVar AllocBuffer(ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::String storage_scope="global", ffi::Optional< ffi::Map< ffi::String, ffi::Any > > annotations=std::nullopt)
Statement-level buffer allocation (creates an AllocBuffer IR node).
Var EnvThread(ffi::String thread_tag, PrimType dtype=PrimType::Int(32))
Bind a var to thread env.
ffi::Array< tvm::tirx::Var > WarpId(ffi::Optional< ffi::Array< PrimExpr > > extents, ffi::String parent, PrimType dtype=PrimType::Int(32))
PrimExpr Float8E5M2(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:580
ElseFrame Else()
Create an else.
ComposeOpFrame ComposeOp(ffi::Map< ffi::String, BufferVar > workspace, ffi::Map< ffi::String, ffi::Any > config, ffi::Optional< ffi::String > dispatch=std::nullopt)
Compose TIRx op.
PrimExpr Float6E3M2FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:585
void Evaluate(Expr value)
Evaluate the input expression.
void BlockAttrs(ffi::Map< ffi::String, ffi::Any > attrs)
The block annotation statement.
PrimExpr Float8E5M2FNUZ(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:581
void Reads(ffi::Array< ffi::ObjectRef > buffer_slices)
The block buffer region reading statement.
ForFrame Vectorized(PrimExpr start, PrimExpr stop, ffi::Optional< ffi::Map< ffi::String, Any > > annotations=std::nullopt, ffi::Optional< PrimExpr > step=std::nullopt, ffi::Optional< PrimType > dtype=std::nullopt)
The vectorized For statement.
BufferVar BufferDecl(ffi::Array< PrimExpr > shape, PrimType dtype, ffi::String buffer_name, ffi::Optional< Expr > data, ffi::Optional< ffi::Array< PrimExpr > > strides, ffi::Optional< PrimExpr > elem_offset, ffi::String storage_scope, int align, int offset_factor, ffi::Optional< Layout > layout=std::nullopt, ffi::Array< PrimExpr > allocated_addr={})
The buffer declaration function.
void Continue()
Create a continue statement.
IfFrame If(PrimExpr condition)
Create an if statement.
Var TensorMap()
Definition ir.h:527
ffi::Array< tvm::tirx::Var > CtaIdInPair(PrimType dtype=PrimType::Int(32))
void Return(Expr value)
Create a return statement.
BufferVar MatchBuffer(ffi::ObjectRef param, ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::Optional< Expr > data=std::nullopt, ffi::Array< PrimExpr > strides={}, PrimExpr elem_offset=PrimExpr(), ffi::String storage_scope="global", int align=-1, int offset_factor=0, ffi::Optional< Layout > layout=std::nullopt)
The buffer match statement.
void BufferStore(BufferVar buffer, PrimExpr value, ffi::Array< PrimExpr > indices)
Store data in a buffer.
PrimExpr Float8E4M3FNUZ(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:579
void FuncName(ffi::String name)
The PrimFunc 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:520
PrimExpr Float8E8M0FNU(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:582
PrimExpr Float6E2M3FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:584
void Break()
Create a break statement.
ffi::Array< tvm::tirx::Var > CtaId(ffi::Optional< ffi::Array< PrimExpr > > extents, ffi::String parent, ffi::Optional< ffi::Array< PrimExpr > > preferred=std::nullopt, PrimType dtype=PrimType::Int(32))
void FuncAttrs(ffi::Map< ffi::String, ffi::Any > attrs)
The PrimFunc annotation statement.
void TilePrimitiveCall(tvm::tirx::TilePrimitiveCall op_call)
ForFrame Grid(ffi::Array< Variant< PrimExpr, ffi::Tuple< PrimExpr, PrimExpr > > > extents, ffi::Optional< PrimType > dtype=std::nullopt)
The grid For statement.
AttrFrame DeviceEntry()
Mark the device-region entry within the enclosing PrimFunc body. Returns an AttrFrame keyed tirx....
AttrFrame Attr(ffi::Any node, ffi::String attr_key, PrimExpr value)
Create an attribute.
SBlockFrame Block(ffi::String name, bool no_realize=false, ffi::String exec_scope="")
The block declaration statement.
PrimExpr Float8E3M4(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:575
PrimExpr Void(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:590
void Where(PrimExpr predicate)
The block predicate statement.
ForFrame Serial(PrimExpr start, PrimExpr stop, ffi::Optional< ffi::Map< ffi::String, Any > > annotations=std::nullopt, ffi::Optional< PrimExpr > step=std::nullopt, ffi::Optional< PrimType > dtype=std::nullopt)
The serial For statement.
Var Arg(ffi::String name, Var var)
The PrimFunc variable arguments adding function.
ForFrame ThreadBinding(PrimExpr start, PrimExpr stop, ffi::String thread, ffi::Optional< ffi::Map< ffi::String, Any > > annotations=std::nullopt)
The thread-binding For statement.
PrimExpr Boolean(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:589
ForFrame Unroll(PrimExpr start, PrimExpr stop, ffi::Optional< ffi::Map< ffi::String, Any > > annotations=std::nullopt, ffi::Optional< PrimExpr > step=std::nullopt, ffi::Optional< PrimType > dtype=std::nullopt)
The unrolled For statement.
PrimExpr Float4E2M1FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:587
DeclBufferFrame DeclBuffer(ffi::Array< PrimExpr > shape, PrimType dtype, ffi::String buffer_name, ffi::Optional< Expr > data, ffi::Optional< ffi::Array< PrimExpr > > strides, ffi::Optional< PrimExpr > elem_offset, ffi::String storage_scope, int align, int offset_factor, ffi::Optional< Layout > layout=std::nullopt, ffi::Optional< PrimExpr > allocated_addr=std::nullopt)
The buffer declaration frame.
LaunchThreadFrame LaunchThread(Var var, PrimExpr extent)
Launch a thread.
ffi::Array< tvm::tirx::Var > ThreadId(ffi::Optional< ffi::Array< PrimExpr > > extents, ffi::String parent, PrimType dtype=PrimType::Int(32))
ffi::Array< tvm::tirx::Var > ScopeId(ffi::Optional< ffi::Array< PrimExpr > > extents, ffi::String parent, ffi::String name, ffi::String cur, PrimType dtype=PrimType::Int(32))
Define a scope id. Pass extents=std::nullopt to defer the extent; it is inferred at LowerTIRx from th...
AssertFrame Assert(PrimExpr condition, ffi::String error_kind, ffi::Array< ffi::String > message_parts)
The assertion statement.
ffi::Array< tvm::tirx::Var > ClusterId(ffi::Optional< ffi::Array< PrimExpr > > extents, ffi::String parent, PrimType dtype=PrimType::Int(32))
ffi::Array< tvm::tirx::Var > WarpgroupId(ffi::Optional< ffi::Array< PrimExpr > > extents, ffi::String parent, PrimType dtype=PrimType::Int(32))
ThenFrame Then()
Create a then.
ForFrame Parallel(PrimExpr start, PrimExpr stop, ffi::Optional< ffi::Map< ffi::String, Any > > annotations=std::nullopt, ffi::Optional< PrimExpr > step=std::nullopt, ffi::Optional< PrimType > dtype=std::nullopt)
The parallel For statement.
PrimExpr Float8E4M3B11FNUZ(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:577
ffi::Variant< BufferVar, AllocBufferFrame > SBlockAllocBuffer(ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::Optional< Expr > data=std::nullopt, ffi::Array< PrimExpr > strides={}, PrimExpr elem_offset=PrimExpr(), ffi::String storage_scope="", int align=-1, int offset_factor=0, ffi::Optional< Layout > layout=std::nullopt, ffi::Array< PrimExpr > allocated_addr={})
The buffer allocation function.
void Writes(ffi::Array< ffi::ObjectRef > buffer_slices)
The block buffer region writing statement.
PrimFuncFrame PrimFunc(bool is_private, bool s_tir=false, bool persistent=false)
The primitive function statement.
Type FuncRet(Type ret_type)
The PrimFunc return type statement.
PrimExpr Float8E4M3FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition ir.h:578
tvm::Var Var
Definition var.h:38
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition analyzer.h:40
A device-independent managed Tensor abstraction.
TIRX tile primitive statements, operators, and reified lambda expressions.
Common operators defined for Expr.
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES_LANES(DType, Code)
Definition ir.h:555
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName, DType)
Definition ir.h:529
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES(DType, Code)
Definition ir.h:536
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES_FIXED_SIZE(DType, Code, Bits)
Definition ir.h:566