1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
//! Copyright 2026 0xClandestine, Ekryski, TheTom, Ambisphaeric
//! SPDX-License-Identifier: Apache-2.0
//! Re-exports and placeholder DSL items for `#[kernel]` functions.
//!
//! Import this module with `use metaltile::prelude::*;` in the same Rust module as your kernels.
//! It provides the items you need to **write** and **launch** kernels — types, macros, runtime
//! bindings, and DSL stubs — without the IR/codegen plumbing that is only needed when building
//! compiler passes or inspecting generated code.
//!
//! For raw IR types (`Op`, `Block`, `ValueId`, etc.) use [`metaltile::core::ir`] directly.
//! For codegen types (`MslGenerator`, `TileSchedule`, etc.) use [`metaltile::codegen`] directly.
//!
//! # What's here
//!
//! - **Macros:** [`#[kernel]`], [`#[bench_kernel]`], [`#[constexpr]`], [`#[scalar]`],
//! [`#[strided]`], [`shape!`], [`tile!`]
//! - **IR types (user-facing):** [`ConstExpr`], [`ConstExprValues`], [`DType`], [`Dim`],
//! [`DimExpr`], [`Shape`], [`Kernel`], [`KernelMode`], [`UnaryOpKind`], [`BinOpKind`],
//! [`ActKind`], [`ReduceKind`], [`AtomicKind`], [`AtomicScope`], [`CoopTileScope`],
//! [`CoopTileAccMode`]
//! - **Runtime:** [`Context`], [`DispatchResult`], [`DispatchSpec`], [`ResidentBuffer`],
//! [`MetalTileError`], [`start_gpu_trace`], [`stop_gpu_trace`]
//! - **Other:** [`GpuFamily`], [`KernelEntry`], [`make_tile`]
//! - **DSL stubs:** [`Tensor`], [`program_id`], [`load`], [`store`], [`dot`],
//! `exp`, `log`, `sqrt`, `rsqrt`, `abs`, `silu`, `gelu`, `relu`, `tanh`,
//! `sigmoid`, `sin`, `cos`, `ceil`, `floor`, `recip`
//!
//! The exported functions exist so Rust can parse kernel bodies before the proc macro runs. The
//! `#[kernel]` macro rewrites the function body, so calling these helpers outside a kernel will
//! panic.
//!
//! Output tensors are identified by parameter name today. Use `c`, `out`, or `output` when you
//! want the generated launch path to treat a tensor parameter as writable output.
use ;
/// Compile-time symbolic values used in shape annotations and generated IR.
pub use ConstExpr;
/// A collection of resolved constexpr values for a specific kernel launch.
pub use ConstExprValues;
/// Scalar and tensor element types supported by the IR and MSL codegen.
pub use DType;
/// Apple GPU family inference from Metal device name strings.
pub use GpuFamily;
// IR types — user-facing subset (op-kind enums and kernel-level containers).
// Raw IR plumbing (Op, Block, ValueId, Param, etc.) lives in `metaltile::core::ir`.
/// Neural activation function kind.
pub use ActKind;
/// Atomic operation kind.
pub use AtomicKind;
/// Memory scope for an atomic op (device vs threadgroup).
pub use AtomicScope;
/// Binary operation kind.
pub use BinOpKind;
/// Accumulation mode for cooperative tile matmul.
pub use CoopTileAccMode;
/// Execution scope for cooperative tile operations (simdgroup vs threadgroup).
pub use CoopTileScope;
/// A complete kernel in the IR.
pub use Kernel;
/// Kernel execution mode metadata for IR/codegen inspection.
pub use KernelMode;
/// Reduction kind.
pub use ReduceKind;
/// Unary math operation kind.
pub use UnaryOpKind;
/// Registry entry for a MetalTile kernel available for cross-kernel calling.
///
/// You only need this when registering a kernel for use as an inlined callee via the
/// `inventory::collect!` mechanism. For ordinary `#[kernel]` definitions this is handled
/// automatically by the macro.
pub use KernelEntry;
/// Shape-building helpers.
pub use Dim;
/// Build a 2D tile shape at runtime: `make_tile(rows, cols) -> Shape`.
///
/// For a compile-time equivalent use the [`tile!`] macro instead.
pub use tile as make_tile;
/// A single dimension expression used in shape algebra.
// (grouped by rustfmt)
pub use ;
/// Registers a kernel for automatic benchmarking (place before `#[kernel]`).
pub use bench_kernel;
/// Marks a kernel parameter as a compile-time constant.
pub use constexpr;
/// Marks a function as a MetalTile kernel.
pub use kernel;
/// Marks a `Tensor` parameter for `constant T&` lowering in MSL.
pub use scalar;
/// Constructs a `Shape` from dimension expressions.
pub use shape;
/// Marks a `Tensor` parameter for strided lowering (shape + stride arrays emitted).
pub use strided;
/// Constructs a 2D tile shape at macro-expansion time.
pub use tile;
/// Metal GPU device and command queue context.
pub use Context;
/// Output buffers returned after a kernel dispatch.
pub use DispatchResult;
/// Input buffer spec for the launched dispatch pipeline.
pub use DispatchSpec;
/// Top-level runtime error.
pub use MetalTileError;
/// A resident Metal buffer managed by the context.
pub use ResidentBuffer;
/// Start GPU trace capture (Xcode GPU frame capture).
pub use start_gpu_trace;
/// Stop GPU trace capture.
pub use stop_gpu_trace;
/// Placeholder tensor type used in `#[kernel]` signatures.
///
/// `Tensor<T, S>` is a zero-sized marker that carries element type `T` and optional shape metadata
/// `S` for proc-macro parsing. The generated launch surface still binds raw byte buffers by
/// parameter name; this type does not own storage or runtime shape information yet.
/// `a[idx]` syntax inside a kernel body.
///
/// The body parser recognizes tensor indexing syntactically and lowers it into IR load/store index
/// expressions. This implementation only exists so the Rust parser accepts the syntax.
// ---- DSL function stubs (panic if called outside #[kernel]) ----
/// Return the current program/thread id for the given axis.
/// Load a value from a tensor index expression.
/// Store a value into a tensor index expression.
/// Dot product placeholder used by tiled kernels.
// Elementwise math — the body parser recognizes these by name
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;
unary!;