builtin.h 10.6 KB
Newer Older
1
2
3
4
5
6
7
8
9
/*!
 * \file tl/op/builtin.h
 * \brief Builtin intrinsics.
 *
 */

#ifndef TVM_TL_OP_BUILTIN_H_
#define TVM_TL_OP_BUILTIN_H_

10
#include "operator.h"
11
#include <tvm/ir/transform.h>
12
13

namespace tvm {
14
15
16
17
18
19
20
21
/*!
 * \brief Create the TVM intrinsic that initializes a PTX fence barrier.
 *
 * Initializes a PTX fence-style barrier used to coordinate asynchronous memory
 * operations (for example, TMA/TMA_STORE). Returns the Op representing this
 * intrinsic for use in TIR lowering and code generation.
 *
 */
22
namespace tl {
23
24
25

namespace attr {
static constexpr const char *kPaddingMap = "padding_map";
26
27
static constexpr const char *kWarpSpecializationScope =
    "kWarpSpecializationScope";
28
29
static constexpr const char *kCustomWarpSpecialization =
    "kCustomWarpSpecialization";
30
31
} // namespace attr

32
33
static constexpr const char *kDebugMergeSharedMemoryAllocations =
    "tl.debug_merge_shared_memory_allocations";
34
static constexpr const char *kDisableTMALower = "tl.disable_tma_lower";
35
36
static constexpr const char *kDisableSafeMemoryLegalize =
    "tl.disable_safe_memory_legalize";
37
38
static constexpr const char *kDisableWarpSpecialized =
    "tl.disable_warp_specialized";
39
static constexpr const char *kConfigIndexBitwidth = "tl.config_index_bitwidth";
40
41
static constexpr const char *kEnableAggressiveSharedMemoryMerge =
    "tl.enable_aggressive_shared_memory_merge";
42
static constexpr const char *kDisableFastMath = "tl.disable_fast_math";
43
static constexpr const char *kEnableFastMath = "tl.enable_fast_math";
44
45
static constexpr const char *kPtxasRegisterUsageLevel =
    "tl.ptxas_register_usage_level";
46
47
static constexpr const char *kEnablePTXASVerboseOutput =
    "tl.enable_ptxas_verbose_output";
48
static constexpr const char *kDisableVectorize256 = "tl.disable_vectorize_256";
49
static constexpr const char *kDisableWGMMA = "tl.disable_wgmma";
50
static constexpr const char *kDisableShuffleElect = "tl.disable_shuffle_elect";
51
52
53
54
55
56
57
58
59
/*!
 * \brief Whether to disable dynamic tail split
 *
 * kDisableDynamicTailSplit = "tl.disable_dynamic_tail_split"
 *
 */
static constexpr const char *kDisableDynamicTailSplit =
    "tl.disable_dynamic_tail_split";

60
61
62
63
64
65
66
67
68
69
70
71
72
73
/*!
 * \brief Whether to disable thread storage synchronization
 *
 * When enabled, disables the automatic insertion of thread synchronization
 * barriers (e.g., __syncthreads()) for shared memory access coordination.
 * This can be useful for performance optimization in cases where manual
 * synchronization is preferred or when synchronization is not needed.
 *
 * kDisableThreadStorageSync = "tl.disable_thread_storage_sync"
 *
 */
static constexpr const char *kDisableThreadStorageSync =
    "tl.disable_thread_storage_sync";

74
75
76
77
78
79
80
81
82
83
84
85
/*!
 * \brief The size of the vectorized dimension in buffer, designed by user
 *
 * For example, if the vectorized dimension is 128 bits and the dtype of buffer
 * A[m, k] is float16, the size of the vectorized dimension (i.e. k) in buffer A
 * should be divisible by 8 (8 = 128 / 16).
 *
 * kDynamicAlignment = "tl.dynamic_alignment"
 *
 */
static constexpr const char *kDynamicAlignment = "tl.dynamic_alignment";

86
87
88
89
90
91
92
93
/*!
 * \brief Get the type of the CUDA tensor map
 *
 * DataType cuTensorMapType()
 *
 */
DataType cuTensorMapType();

94
// fast math related op
95
// __exp(x) - fast exponential
96
TVM_DLL const Op &__exp();
97
// __exp10(x) - fast base-10 exponential
98
TVM_DLL const Op &__exp10();
99
// __log(x) - fast natural logarithm
100
TVM_DLL const Op &__log();
101
// __log2(x) - fast base-2 logarithm
102
TVM_DLL const Op &__log2();
103
// __log10(x) - fast base-10 logarithm
104
TVM_DLL const Op &__log10();
105
// __tan(x) - fast tangent
106
TVM_DLL const Op &__tan();
107
// __cos(x) - fast cosine
108
TVM_DLL const Op &__cos();
109
// __sin(x) - fast sine
110
111
TVM_DLL const Op &__sin();

112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
// high precision with IEEE-compliant.
// ieee_add(x, y, rounding_mode) - IEEE-compliant addition
TVM_DLL const Op &ieee_add();
// ieee_sub(x, y, rounding_mode) - IEEE-compliant subtraction
TVM_DLL const Op &ieee_sub();
// ieee_mul(x, y, rounding_mode) - IEEE-compliant multiplication
TVM_DLL const Op &ieee_mul();
// ieee_fmaf(x, y, z, rounding_mode) - IEEE-compliant fused multiply-add
TVM_DLL const Op &ieee_fmaf();
// ieee_frcp(x, rounding_mode) - IEEE-compliant reciprocal
TVM_DLL const Op &ieee_frcp();
// ieee_fsqrt(x, rounding_mode) - IEEE-compliant square root
TVM_DLL const Op &ieee_fsqrt();
// ieee_frsqrt(x) - IEEE-compliant reciprocal square root (rn only)
TVM_DLL const Op &ieee_frsqrt();
// ieee_fdiv(x, y, rounding_mode) - IEEE-compliant division
TVM_DLL const Op &ieee_fdiv();

130
131
132
/*!
 * \brief tvm intrinsics for TMADescriptor creation for tiled load
 *
133
 * CuTensorMap* create_tma_descriptor(data_type, rank, global_addr,
134
135
 * global_shape..., global_stride..., smem_box..., smem_stride..., interleave,
 * swizzle, l2_promotion, oob_fill)
136
137
 *
 */
138
TVM_DLL const Op &create_tma_descriptor();
139
140
141
142

/*!
 * \brief tvm intrinsics for TMADescriptor creation for image to column load
 *
143
 * CuTensorMap* create_tma_im2col_descriptor(data_type, rank, global_addr,
144
145
146
 * global_shape..., global_stride..., elem_stride..., lower_corner...,
 * upper_corner..., smme_box_pixel, smem_box_channel, interleave, swizzle,
 * l2_promotion, oob_fill)
147
148
 *
 */
149
TVM_DLL const Op &create_tma_im2col_descriptor();
150
151
152
153

/*!
 * \brief Create a list of mbarrier with num_threads
 *
154
 * create_list_of_mbarrier(num_threads0, num_threads1, ...)
155
156
 *
 */
157
TVM_DLL const Op &create_list_of_mbarrier();
158
159
160
161
162
163
164

/*!
 * \brief Get the mbarrier with barrier_id
 *
 * int64_t* GetMBarrier(barrier_id)
 *
 */
165
TVM_DLL const Op &get_mbarrier();
166
167

/*!
168
169
 * \brief tvm intrinsics for loading data from global tensor descriptor to
 * shared memory
170
 *
171
 * tma_load(descriptor, mbarrier, smem_data, coord_0, coord_1, ...)
172
173
 *
 */
174
TVM_DLL const Op &tma_load();
175
176

/*!
177
178
 * \brief tvm intrinsics for loading image from global tensor to columns in
 * shared memory
179
 *
180
 * tma_load(descriptor, mbarrier, smem_data, coord_0, coord_1, ...,
181
 * image_offset, ...)
182
183
 *
 */
184
TVM_DLL const Op &tma_load_im2col();
185
186

/*!
187
188
 * \brief tvm intrinsics for storing data from shared memory to global tensor
 * descriptor
189
 *
190
 * tma_store(descriptor, smem_data, coord_0, coord_1, ...)
191
192
 *
 */
193
TVM_DLL const Op &tma_store();
194

195
196
197
198
199
200
201
202
/*!
 * \brief tvm intrinsics for barrier initialization fence
 *
 * ptx_fence_barrier_init()
 *
 */
const Op &ptx_fence_barrier_init();

203
204
205
/*!
 * \brief tvm intrinsics for mbarrier wait with parity bit
 *
206
 * mbarrier_wait_parity(mbarrier, parity)
207
208
 *
 */
209
TVM_DLL const Op &mbarrier_wait_parity();
210
211
212
213

/*!
 * \brief tvm intrinsics for mbarrier expect tx
 *
214
 * mbarrier_expect_tx(mbarrier, transaction_bytes)
215
216
 *
 */
217
TVM_DLL const Op &mbarrier_expect_tx();
218

219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/*!
 * \brief tvm intrinsics for initializing tensor memory
 *
 * ptx_init_tensor_memory(tmem_buffer, num_cols)
 *
 */
const Op &ptx_init_tensor_memory();

/*!
 * \brief tvm intrinsics for deallocating tensor memory
 *
 * tmem_deallocate(tmem_buffer)
 *
 */
const Op &ptx_deallocate_tensor_memory();

235
236
237
/*!
 * \brief tvm intrinsics for ldmatrix
 *
238
 * ptx_ldmatrix(transposed, num, shared_addr, local_addr)
239
240
 *
 */
241
TVM_DLL const Op &ptx_ldmatrix();
242
243
244
245

/*!
 * \brief tvm intrinsics for stmatrix
 *
246
 * ptx_ldmatrix(transposed, num, shared_addr, int32_values...)
247
248
 *
 */
249
TVM_DLL const Op &ptx_stmatrix();
250

251
252
253
254
255
256
257
258
/*!
 * \brief tvm intrinsic for ptx async copy barrier using
 * cp.async.mbarrier.arrive.noinc
 *
 *  This op is used to represent a ptx async copy barrier operation in tilelang.
 */
TVM_DLL const Op &ptx_cp_async_barrier_noinc();

259
260
261
/*!
 * \brief Pack two b16 value into a b32 value
 *
262
 * int32 pack_b16(b16_value, b16_value)
263
264
 *
 */
265
TVM_DLL const Op &pack_b16();
266
267
268
269
270
271
272

/*!
 * \brief Issue a shared memory fence for async operations
 *
 * FenceProxyAsync()
 *
 */
273
TVM_DLL const Op &fence_proxy_async();
274

275
276
277
/*!
 * \brief Indicate arrival of warp issuing TMA_STORE
 *
278
 * tma_store_arrive()
279
280
 *
 */
281
TVM_DLL const Op &tma_store_arrive();
282
283
284
285

/*!
 * \brief Wait for TMA_STORE to finish
 *
286
 * tma_store_wait()
287
288
 *
 */
289
TVM_DLL const Op &tma_store_wait();
290

291
292
293
294
295
296
/*!
 * \brief Set reg hint for warp-specialized branched
 *
 * SetMaxNRegInc(num_reg, is_inc)
 *
 */
297
TVM_DLL const Op &set_max_nreg();
298

299
300
301
/*!
 * \brief No set reg hint for warp-specialized branched
 *
302
 * no_set_max_nreg()
303
304
 *
 */
305
TVM_DLL const Op &no_set_max_nreg();
306

307
308
309
/*!
 * \brief Wait the previous wgmma to finish
 *
310
 * wait_wgmma(num_mma)
311
312
 *
 */
313
TVM_DLL const Op &wait_wgmma();
314

315
316
317
318
319
320
/*!
 * \brief Synchronize all threads in a grid
 *
 * sync_grid()
 *
 */
321
TVM_DLL const Op &sync_grid();
322
323
324
325
326
327
328

/*!
 * \brief tvm intrinsic for loop continue
 *
 * loop_break()
 *
 */
329
TVM_DLL const Op &loop_break();
330

331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
/*!
 * \brief tvm intrinsic for amd matrix core mfma instructions.
 *
 *  void tvm_mfma(StringImm shape, StringImm A_layout, StringImm B_layout,
 *               StringImm A_dtype, StringImm B_dtype, StringImm C_dtype,
 *               Var multiplicand_a, Expr a_index,
 *               Var multiplicand_b, Expr b_index,
 *               Var accumulator, Expr c_index);
 */
TVM_DLL const Op &tvm_mfma();

/*!
 * \brief tvm intrinsic for storing the result of AMD MFMA into a destination
 * pointer.
 *
 *        There is no real instruction that does that, but we want to hide
 * details of complex index manipulation behind this intrinsic to simplify TIR
 * lowering passes (e.g. LowerWarpMemory) like cuda ptx backend does.
 *
 * void tvm_mfma_store(IntImm m, IntImm n, Var dst_ptr, Var src_ptr, Expr
 * src_offset, Var dst_stride);
 */
TVM_DLL const Op &tvm_mfma_store();

/*!
 * \brief tvm intrinsic for amd rdna matrix core instructions.
 *
 *  void tvm_rdna_wmma(StringImm shape, StringImm A_layout, StringImm B_layout,
 *               StringImm A_dtype, StringImm B_dtype, StringImm C_dtype,
 *               Var multiplicand_a, Expr a_index,
 *               Var multiplicand_b, Expr b_index,
 *               Var accumulator, Expr c_index);
 */
TVM_DLL const Op &tvm_rdna_wmma();

/*!
 * \brief tvm intrinsic for storing the result of AMD RDNA WMMA into a
 * destination pointer.
 *
 *        There is no real instruction that does that, but we want to hide
 * details of complex index manipulation behind this intrinsic to simplify TIR
 * lowering passes (e.g. LowerWarpMemory) like cuda ptx backend does.
 *
 * void tvm_rdna_wmma_store(IntImm m, IntImm n, Var dst_ptr, Var src_ptr, Expr
 * src_offset, Var dst_stride);
 */
TVM_DLL const Op &tvm_rdna_wmma_store();

379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*!
 * \brief tilelang intrinsic for general matrix multiplication (GEMM).
 *
 *  This op is used to represent a generic GEMM operation in tilelang.
 */
TVM_DLL const Op &tl_gemm();

/*!
 * \brief tilelang intrinsic for sparse matrix multiplication (GEMM with
 * sparsity).
 *
 *  This op is used to represent a sparse GEMM operation in tilelang.
 */
TVM_DLL const Op &tl_gemm_sp();

394
395
396
397
398
399
400
/*!
 * \brief tilelang intrinsic for shuffle elect.
 *
 *  This op is used to represent a shuffle elect operation in tilelang.
 */
TVM_DLL const Op &tl_shuffle_elect();

401
402
} // namespace tl
} // namespace tvm
403

404
#endif //  TVM_TL_OP_BUILTIN_H_