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>
34 namespace ir_builder {
37 using tvm::ffi::Tuple;
38 using tvm::ffi::Variant;
61 ffi::Optional<Var> data, ffi::Optional<ffi::Array<PrimExpr>> strides,
62 ffi::Optional<PrimExpr> elem_offset, ffi::String storage_scope,
int align,
63 int offset_factor, ffi::String buffer_type,
65 ffi::Optional<Layout> layout = std::nullopt,
66 ffi::Array<PrimExpr> allocated_addr = {});
127 ffi::String storage_scope =
"global",
int align = -1,
int offset_factor = 0,
128 ffi::String buffer_type =
"default",
130 ffi::Optional<Layout> layout = std::nullopt);
142 ffi::Array<tvm::tirx::Var>
KernelId(ffi::Array<PrimExpr> extents, ffi::String parent);
144 ffi::Array<tvm::tirx::Var>
CtaId(ffi::Array<PrimExpr> extents, ffi::String parent);
148 ffi::Array<tvm::tirx::Var>
WarpId(ffi::Array<PrimExpr> extents, ffi::String parent);
150 ffi::Array<tvm::tirx::Var>
ThreadId(ffi::Array<PrimExpr> extents, ffi::String parent);
168 void Reads(ffi::Array<ffi::ObjectRef> buffer_slices);
174 void Writes(ffi::Array<ffi::ObjectRef> buffer_slices);
201 ffi::Optional<Var> data = std::nullopt, ffi::Array<PrimExpr> strides = {},
202 PrimExpr elem_offset =
PrimExpr(), ffi::String storage_scope =
"",
int align = -1,
203 int offset_factor = 0, ffi::String buffer_type =
"default",
205 ffi::Optional<Layout> layout = std::nullopt, ffi::Array<PrimExpr> allocated_addr = {});
252 ffi::Array<Var>
Remap(ffi::String kinds, ffi::Array<PrimExpr> bindings,
266 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt,
267 ffi::Optional<PrimExpr> step = std::nullopt);
277 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt,
278 ffi::Optional<PrimExpr> step = std::nullopt);
288 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt,
289 ffi::Optional<PrimExpr> step = std::nullopt);
299 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt,
300 ffi::Optional<PrimExpr> step = std::nullopt);
310 ffi::Optional<ffi::Map<ffi::String, Any>> annotations = std::nullopt);
326 ffi::Array<ffi::String> message_parts);
340 Var Bind(
Expr value, ffi::Optional<Type> type_annotation = std::nullopt,
341 ffi::Optional<Var>
var = std::nullopt);
416 ffi::Optional<Var> data, ffi::Optional<ffi::Array<PrimExpr>> strides,
417 ffi::Optional<PrimExpr> elem_offset, ffi::String storage_scope,
418 int align,
int offset_factor, ffi::String buffer_type,
420 ffi::Optional<Layout> layout = std::nullopt,
421 ffi::Optional<PrimExpr> allocated_addr = std::nullopt);
432 ffi::String storage_scope =
"global",
433 ffi::Optional<ffi::Map<ffi::String, ffi::Any>> annotations = std::nullopt);
459 ffi::Map<ffi::String, ffi::Any> config,
460 ffi::Optional<ffi::String> dispatch = std::nullopt);
479 ffi::Optional<PrimExpr> predicate);
497 inline Var Handle(ffi::Optional<PrimType> dtype = std::nullopt,
498 ffi::String storage_scope =
"global") {
506 #define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName, DType) \
507 inline PrimExpr FuncName(ffi::Optional<PrimExpr> expr = std::nullopt) { \
508 PrimType dtype = DType; \
509 return expr.has_value() ? tvm::cast(dtype, expr.value()) \
510 : tvm::tirx::Var("", dtype).as_or_throw<PrimExpr>(); \
513 #define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES(DType, Code) \
514 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##8, (PrimType(DLDataType{Code, 8, 1}))); \
515 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##16, (PrimType(DLDataType{Code, 16, 1}))); \
516 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##32, (PrimType(DLDataType{Code, 32, 1}))); \
517 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##64, (PrimType(DLDataType{Code, 64, 1})));
524 #define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(FuncName, Code, Size) \
525 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x2, (PrimType(DLDataType{Code, Size, 2}))) \
526 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x4, (PrimType(DLDataType{Code, Size, 4}))); \
527 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x8, (PrimType(DLDataType{Code, Size, 8}))); \
528 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x16, (PrimType(DLDataType{Code, Size, 16}))); \
529 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x32, (PrimType(DLDataType{Code, Size, 32}))); \
530 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName##x64, (PrimType(DLDataType{Code, Size, 64})));
532 #define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES_LANES(DType, Code) \
533 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##8, Code, 8); \
534 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##16, Code, 16); \
535 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##32, Code, 32); \
536 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES(DType##64, Code, 64);
543 #define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES_FIXED_SIZE(DType, Code, Bits) \
544 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType, (PrimType(DLDataType{Code, Bits, 1}))); \
545 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x2, (PrimType(DLDataType{Code, Bits, 2}))); \
546 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x4, (PrimType(DLDataType{Code, Bits, 4}))); \
547 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x8, (PrimType(DLDataType{Code, Bits, 8}))); \
548 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x16, (PrimType(DLDataType{Code, Bits, 16}))); \
549 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x32, (PrimType(DLDataType{Code, Bits, 32}))); \
550 TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(DType##x64, (PrimType(DLDataType{Code, Bits, 64})));
569 #undef TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST
Managed reference to ExprNode.
Definition: base_expr.h:311
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:354
Definition: base_expr.h:113
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:484
Managed reference to TensorMapTypeNode.
Definition: type.h:199
Managed reference to TypeNode.
Definition: base_expr.h:77
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
Buffer is a symbolic n-darray structure. It is a composition of primitive symbolic types,...
Definition: buffer.h:187
Definition: exec_scope.h:228
Managed reference to TilePrimitiveCallNode.
Definition: tirx_stmt.h:84
a named variable in TIR
Definition: var.h:68
constexpr const char * axis_separators
Marks the physical axis separators.
Definition: stmt.h:233
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.
ffi::Array< Var > Remap(ffi::String kinds, ffi::Array< PrimExpr > bindings, PrimType dtype=PrimType::Int(32))
The block axis remapping function.
Var Scan(Range dom, PrimExpr binding, PrimType dtype=PrimType::Int(32))
The scanning block axis defining function.
WhileFrame While(PrimExpr condition)
Create a while loop.
PrimExpr Float8E4M3(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:553
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.
ComposeOpFrame ComposeOp(ffi::Map< ffi::String, Buffer > workspace, ffi::Map< ffi::String, ffi::Any > config, ffi::Optional< ffi::String > dispatch=std::nullopt)
Compose TIRx op.
Var EnvThread(ffi::String thread_tag, PrimType dtype=PrimType::Int(32))
Bind a var to thread env.
PrimExpr Float8E5M2(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:557
ElseFrame Else()
Create an else.
ForFrame Serial(PrimExpr start, PrimExpr stop, ffi::Optional< ffi::Map< ffi::String, Any >> annotations=std::nullopt, ffi::Optional< PrimExpr > step=std::nullopt)
The serial For statement.
PrimExpr Float6E3M2FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:562
void BufferStore(Buffer buffer, PrimExpr value, ffi::Array< PrimExpr > indices, ffi::Optional< PrimExpr > predicate)
Store data in a buffer.
ffi::Array< tvm::tirx::Var > KernelId(ffi::Array< PrimExpr > extents, ffi::String parent)
ForFrame Unroll(PrimExpr start, PrimExpr stop, ffi::Optional< ffi::Map< ffi::String, Any >> annotations=std::nullopt, ffi::Optional< PrimExpr > step=std::nullopt)
The unrolled For statement.
ForFrame Parallel(PrimExpr start, PrimExpr stop, ffi::Optional< ffi::Map< ffi::String, Any >> annotations=std::nullopt, ffi::Optional< PrimExpr > step=std::nullopt)
The parallel For statement.
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:558
void Reads(ffi::Array< ffi::ObjectRef > buffer_slices)
The block buffer region reading statement.
void Continue()
Create a continue statement.
IfFrame If(PrimExpr condition)
Create an if statement.
Var TensorMap()
Definition: ir.h:504
Buffer 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).
ForFrame Vectorized(PrimExpr start, PrimExpr stop, ffi::Optional< ffi::Map< ffi::String, Any >> annotations=std::nullopt, ffi::Optional< PrimExpr > step=std::nullopt)
The vectorized For statement.
Buffer MatchBuffer(ffi::ObjectRef param, ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::Optional< Var > data=std::nullopt, ffi::Array< PrimExpr > strides={}, PrimExpr elem_offset=PrimExpr(), ffi::String storage_scope="global", int align=-1, int offset_factor=0, ffi::String buffer_type="default", ffi::Optional< ffi::Array< IntImm >> axis_separators=std::nullopt, ffi::Optional< Layout > layout=std::nullopt)
The buffer match statement.
PrimExpr Float8E4M3FNUZ(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:556
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:497
PrimExpr Float8E8M0FNU(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:559
PrimExpr Float6E2M3FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:561
void Break()
Create a break statement.
void FuncAttrs(ffi::Map< ffi::String, ffi::Any > attrs)
The PrimFunc annotation statement.
ffi::Array< tvm::tirx::Var > WarpId(ffi::Array< PrimExpr > extents, ffi::String parent)
void TilePrimitiveCall(tvm::tirx::TilePrimitiveCall op_call)
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.
ffi::Array< tvm::tirx::Var > CtaId(ffi::Array< PrimExpr > extents, ffi::String parent)
PrimExpr Float8E3M4(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:552
PrimExpr Void(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:567
DeclBufferFrame DeclBuffer(ffi::Array< PrimExpr > shape, PrimType dtype, ffi::String buffer_name, ffi::Optional< Var > data, ffi::Optional< ffi::Array< PrimExpr >> strides, ffi::Optional< PrimExpr > elem_offset, ffi::String storage_scope, int align, int offset_factor, ffi::String buffer_type, ffi::Optional< ffi::Array< IntImm >> axis_separators, ffi::Optional< Layout > layout=std::nullopt, ffi::Optional< PrimExpr > allocated_addr=std::nullopt)
The buffer declaration frame.
void Where(PrimExpr predicate)
The block predicate statement.
ForFrame ThreadBinding(PrimExpr start, PrimExpr stop, ffi::String thread, ffi::Optional< ffi::Map< ffi::String, Any >> annotations=std::nullopt)
The thread-binding For statement.
Var Arg(ffi::String name, Var var)
The PrimFunc variable arguments adding function.
PrimExpr Boolean(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:566
PrimExpr Float4E2M1FN(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:564
LaunchThreadFrame LaunchThread(Var var, PrimExpr extent)
Launch a thread.
ffi::Array< tvm::tirx::Var > CtaIdInPair()
ForFrame Grid(ffi::Array< Variant< PrimExpr, ffi::Tuple< PrimExpr, PrimExpr >>> extents)
The grid For statement.
AssertFrame Assert(PrimExpr condition, ffi::String error_kind, ffi::Array< ffi::String > message_parts)
The assertion statement.
ThenFrame Then()
Create a then.
ffi::Variant< Buffer, AllocBufferFrame > SBlockAllocBuffer(ffi::Array< PrimExpr > shape, PrimType dtype=PrimType::Float(32), ffi::Optional< Var > data=std::nullopt, ffi::Array< PrimExpr > strides={}, PrimExpr elem_offset=PrimExpr(), ffi::String storage_scope="", int align=-1, int offset_factor=0, ffi::String buffer_type="default", ffi::Optional< ffi::Array< IntImm >> axis_separators=std::nullopt, ffi::Optional< Layout > layout=std::nullopt, ffi::Array< PrimExpr > allocated_addr={})
The buffer allocation function.
PrimExpr Float8E4M3B11FNUZ(ffi::Optional< PrimExpr > expr=std::nullopt)
Definition: ir.h:554
Buffer BufferDecl(ffi::Array< PrimExpr > shape, PrimType dtype, ffi::String buffer_name, ffi::Optional< Var > data, ffi::Optional< ffi::Array< PrimExpr >> strides, ffi::Optional< PrimExpr > elem_offset, ffi::String storage_scope, int align, int offset_factor, ffi::String buffer_type, ffi::Optional< ffi::Array< IntImm >> axis_separators, ffi::Optional< Layout > layout=std::nullopt, ffi::Array< PrimExpr > allocated_addr={})
The buffer declaration function.
void Writes(ffi::Array< ffi::ObjectRef > buffer_slices)
The block buffer region writing statement.
ffi::Array< tvm::tirx::Var > ThreadId(ffi::Array< PrimExpr > extents, ffi::String parent)
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:555
PrimVar var(std::string name_hint, PrimType t=PrimType::Int(32))
Construct a new Var expression.
Tensor shape(const Tensor &src, PrimType dtype, const std::string name="T_shape", const std::string tag=kInjective)
Get the shape of input tensor.
Definition: transform.h:2010
An object that builds and maintains block scope and StmtSref mapping for Dependence analysis.
Definition: analyzer.h:40
A device-independent managed Tensor abstraction.
Common operators defined for Expr.
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES_LANES(DType, Code)
Definition: ir.h:532
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST(FuncName, DType)
Definition: ir.h:506
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_SIZES(DType, Code)
Definition: ir.h:513
#define TVM_TIRX_IR_BUILDER_DEF_DTYPE_CAST_LANES_FIXED_SIZE(DType, Code, Bits)
Definition: ir.h:543