"vscode:/vscode.git/clone" did not exist on "05ad529f646ade0cc27dbe3f20ff5c5b2cf173b0"
amd_buffer_addressing.hpp 31.3 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
#ifndef CK_AMD_BUFFER_ADDRESSING_HPP
#define CK_AMD_BUFFER_ADDRESSING_HPP

#include "float_type.hpp"

namespace ck {

// For 128bit SGPRs in buffer_load and buffer_store instructions
// https://rocm-documentation.readthedocs.io/en/latest/GCN_ISA_Manuals/testdocbook.html#vector-memory-buffer-instructions
template <typename T>
Chao Liu's avatar
Chao Liu committed
11
union BufferAddressConfig
12
13
14
15
16
17
{
    int32x4_t data;
    T* address[2];
    int32_t range[4];
};

Chao Liu's avatar
Chao Liu committed
18
19
20
21
22
__device__ float __llvm_amdgcn_buffer_load_f32(int32x4_t rsrc,
                                               index_t vindex,
                                               index_t offset,
                                               bool glc,
                                               bool slc) __asm("llvm.amdgcn.buffer.load.f32");
23

Chao Liu's avatar
Chao Liu committed
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
__device__ float2_t
__llvm_amdgcn_buffer_load_f32x2(int32x4_t rsrc,
                                index_t vindex,
                                index_t offset,
                                bool glc,
                                bool slc) __asm("llvm.amdgcn.buffer.load.v2f32");

__device__ float4_t
__llvm_amdgcn_buffer_load_f32x4(int32x4_t rsrc,
                                index_t vindex,
                                index_t offset,
                                bool glc,
                                bool slc) __asm("llvm.amdgcn.buffer.load.v4f32");

__device__ half_t __llvm_amdgcn_buffer_load_f16(int32x4_t rsrc,
39
40
41
                                                index_t vindex,
                                                index_t offset,
                                                bool glc,
Chao Liu's avatar
Chao Liu committed
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
                                                bool slc) __asm("llvm.amdgcn.buffer.load.f16");

__device__ half2_t __llvm_amdgcn_buffer_load_f16x2(int32x4_t rsrc,
                                                   index_t vindex,
                                                   index_t offset,
                                                   bool glc,
                                                   bool slc) __asm("llvm.amdgcn.buffer.load.v2f16");

__device__ half4_t __llvm_amdgcn_buffer_load_f16x4(int32x4_t rsrc,
                                                   index_t vindex,
                                                   index_t offset,
                                                   bool glc,
                                                   bool slc) __asm("llvm.amdgcn.buffer.load.v4f16");

__device__ ushort __llvm_amdgcn_buffer_load_bf16(int32x4_t rsrc,
                                                 index_t vindex,
                                                 index_t offset,
                                                 bool glc,
                                                 bool slc) __asm("llvm.amdgcn.buffer.load.bf16");

__device__ ushort2_t
__llvm_amdgcn_buffer_load_bf16x2(int32x4_t rsrc,
                                 index_t vindex,
                                 index_t offset,
                                 bool glc,
                                 bool slc) __asm("llvm.amdgcn.buffer.load.v2bf16");

__device__ ushort4_t
__llvm_amdgcn_buffer_load_bf16x4(int32x4_t rsrc,
                                 index_t vindex,
                                 index_t offset,
                                 bool glc,
                                 bool slc) __asm("llvm.amdgcn.buffer.load.v4bf16");

__device__ void __llvm_amdgcn_buffer_store_f32(float vdata,
                                               int32x4_t rsrc,
                                               index_t vindex,
                                               index_t offset,
                                               bool glc,
                                               bool slc) __asm("llvm.amdgcn.buffer.store.f32");

__device__ void __llvm_amdgcn_buffer_store_f32x2(float2_t vdata,
                                                 int32x4_t rsrc,
                                                 index_t vindex,
                                                 index_t offset,
                                                 bool glc,
                                                 bool slc) __asm("llvm.amdgcn.buffer.store.v2f32");

__device__ void __llvm_amdgcn_buffer_store_f32x4(float4_t vdata,
                                                 int32x4_t rsrc,
                                                 index_t vindex,
                                                 index_t offset,
                                                 bool glc,
                                                 bool slc) __asm("llvm.amdgcn.buffer.store.v4f32");

__device__ void __llvm_amdgcn_buffer_store_f16(half_t vdata,
                                               int32x4_t rsrc,
                                               index_t vindex,
                                               index_t offset,
                                               bool glc,
                                               bool slc) __asm("llvm.amdgcn.buffer.store.f16");

__device__ void __llvm_amdgcn_buffer_store_f16x2(half2_t vdata,
                                                 int32x4_t rsrc,
                                                 index_t vindex,
                                                 index_t offset,
                                                 bool glc,
                                                 bool slc) __asm("llvm.amdgcn.buffer.store.v2f16");

__device__ void __llvm_amdgcn_buffer_store_f16x4(half4_t vdata,
                                                 int32x4_t rsrc,
                                                 index_t vindex,
                                                 index_t offset,
                                                 bool glc,
                                                 bool slc) __asm("llvm.amdgcn.buffer.store.v4f16");

__device__ void __llvm_amdgcn_buffer_store_bf16(ushort vdata,
                                                int32x4_t rsrc,
120
121
122
                                                index_t vindex,
                                                index_t offset,
                                                bool glc,
Chao Liu's avatar
Chao Liu committed
123
                                                bool slc) __asm("llvm.amdgcn.buffer.store.bf16");
124

125
__device__ void
Chao Liu's avatar
Chao Liu committed
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
__llvm_amdgcn_buffer_store_bf16x2(ushort2_t vdata,
                                  int32x4_t rsrc,
                                  index_t vindex,
                                  index_t offset,
                                  bool glc,
                                  bool slc) __asm("llvm.amdgcn.buffer.store.v2bf16");

__device__ void
__llvm_amdgcn_buffer_store_bf16x4(ushort4_t vdata,
                                  int32x4_t rsrc,
                                  index_t vindex,
                                  index_t offset,
                                  bool glc,
                                  bool slc) __asm("llvm.amdgcn.buffer.store.v4bf16");

__device__ void
__llvm_amdgcn_buffer_atomic_add_f32(float vdata,
                                    int32x4_t rsrc,
                                    index_t vindex,
                                    index_t offset,
                                    bool slc) __asm("llvm.amdgcn.buffer.atomic.fadd.f32");
147

Chao Liu's avatar
Chao Liu committed
148
149
150
151
// buffer_load requires:
//   1) p_src must be in global memory space, d_dst must be vgpr
//   2) p_src to be a block-invariant pointer.
// It is user's responsibility to make sure that is true.
152
template <typename T, index_t VectorSize>
Chao Liu's avatar
Chao Liu committed
153
__device__ typename vector_type<T, VectorSize>::MemoryType amd_buffer_load(
154
    const T* p_src_block, index_t src_thread_data_offset, index_t src_const_data_offset);
155

Chao Liu's avatar
Chao Liu committed
156
157
158
159
// buffer_store requires:
//   1) p_src must be in vgpr space, d_dst must be global memory
//   2) p_dst to be a block-invariant pointer.
// It is user's responsibility to make sure that is true.
160
template <typename T, index_t VectorSize>
Chao Liu's avatar
Chao Liu committed
161
162
163
164
__device__ void amd_buffer_store(const T* p_src,
                                 T* p_dst_block,
                                 index_t dst_thread_data_offset,
                                 index_t dst_const_data_offset);
165

166
template <typename T, index_t VectorSize>
Chao Liu's avatar
Chao Liu committed
167
168
169
170
__device__ void amd_buffer_atomic_add(const T* p_src,
                                      T* p_dst_block,
                                      index_t dst_thread_data_offset,
                                      index_t dst_const_data_offset);
171

172
template <>
Chao Liu's avatar
Chao Liu committed
173
174
175
__device__ float amd_buffer_load<float, 1>(const float* p_src_block,
                                           index_t src_thread_data_offset,
                                           index_t src_const_data_offset)
176
{
Chao Liu's avatar
Chao Liu committed
177
178
179
180
181
182
183
184
    BufferAddressConfig<float> src_block_config;

    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<float*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;
185
186
187
188

    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(float);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(float);

Chao Liu's avatar
Chao Liu committed
189
190
191
192
193
194
195
196
197
198
    return __llvm_amdgcn_buffer_load_f32(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
}

template <>
__device__ float2_t amd_buffer_load<float, 2>(const float* p_src_block,
                                              index_t src_thread_data_offset,
                                              index_t src_const_data_offset)
{
    BufferAddressConfig<float> src_block_config;
199
200
201
202
203
204
205
206

    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<float*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

Chao Liu's avatar
Chao Liu committed
207
208
    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(float);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(float);
209

Chao Liu's avatar
Chao Liu committed
210
211
    return __llvm_amdgcn_buffer_load_f32x2(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
212
213
214
}

template <>
Chao Liu's avatar
Chao Liu committed
215
216
217
__device__ float4_t amd_buffer_load<float, 4>(const float* p_src_block,
                                              index_t src_thread_data_offset,
                                              index_t src_const_data_offset)
218
{
Chao Liu's avatar
Chao Liu committed
219
220
221
222
223
224
225
226
    BufferAddressConfig<float> src_block_config;

    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<float*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;
227
228
229
230

    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(float);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(float);

Chao Liu's avatar
Chao Liu committed
231
232
233
234
235
236
237
238
239
240
    return __llvm_amdgcn_buffer_load_f32x4(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
}

template <>
__device__ half_t amd_buffer_load<half_t, 1>(const half_t* p_src_block,
                                             index_t src_thread_data_offset,
                                             index_t src_const_data_offset)
{
    BufferAddressConfig<half_t> src_block_config;
241
242

    // fill in byte 0 - 1
Chao Liu's avatar
Chao Liu committed
243
    src_block_config.address[0] = const_cast<half_t*>(p_src_block);
244
245
246
247
248
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

Chao Liu's avatar
Chao Liu committed
249
250
251
252
253
#if !CK_WORKAROUND_SWDEV_231101
    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(half_t);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(half_t);

    return __llvm_amdgcn_buffer_load_f16(
254
255
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
#else
Chao Liu's avatar
Chao Liu committed
256
    return p_src_block[src_thread_data_offset + src_const_data_offset];
257
#endif
Chao Liu's avatar
Chao Liu committed
258
}
259

Chao Liu's avatar
Chao Liu committed
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
template <>
__device__ half2_t amd_buffer_load<half_t, 2>(const half_t* p_src_block,
                                              index_t src_thread_data_offset,
                                              index_t src_const_data_offset)
{
    BufferAddressConfig<half_t> src_block_config;

    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<half_t*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(half_t);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(half_t);

#if !CK_WORKAROUND_SWDEV_231101
    return __llvm_amdgcn_buffer_load_f16x2(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
#else
    float dst_out_tmp = __llvm_amdgcn_buffer_load_f32(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);

    return *reinterpret_cast<half2_t*>(&dst_out_tmp);
#endif
286
287
288
}

template <>
Chao Liu's avatar
Chao Liu committed
289
290
291
__device__ half4_t amd_buffer_load<half_t, 4>(const half_t* p_src_block,
                                              index_t src_thread_data_offset,
                                              index_t src_const_data_offset)
292
{
Chao Liu's avatar
Chao Liu committed
293
    BufferAddressConfig<half_t> src_block_config;
294

Chao Liu's avatar
Chao Liu committed
295
296
297
298
299
300
301
302
303
    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<half_t*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(half_t);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(half_t);
304

Chao Liu's avatar
Chao Liu committed
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
#if !CK_WORKAROUND_SWDEV_231101
    return __llvm_amdgcn_buffer_load_f16x4(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
#else
    float2_t dst_out_tmp = __llvm_amdgcn_buffer_load_f32x2(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);

    return *reinterpret_cast<half4_t*>(&dst_out_tmp);
#endif
}

template <>
__device__ half8_t amd_buffer_load<half_t, 8>(const half_t* p_src_block,
                                              index_t src_thread_data_offset,
                                              index_t src_const_data_offset)
{
    BufferAddressConfig<half_t> src_block_config;
322
323

    // fill in byte 0 - 1
Chao Liu's avatar
Chao Liu committed
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
    src_block_config.address[0] = const_cast<half_t*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(half_t);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(half_t);

#if !CK_WORKAROUND_SWDEV_231101
    static_assert(false, "wrong! not supported");
#else
    float4_t dst_out_tmp = __llvm_amdgcn_buffer_load_f32x4(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);

    return *reinterpret_cast<half8_t*>(&dst_out_tmp);
#endif
}

template <>
__device__ ushort amd_buffer_load<ushort, 1>(const ushort* p_src_block,
                                             index_t src_thread_data_offset,
                                             index_t src_const_data_offset)
{
    BufferAddressConfig<ushort> src_block_config;

    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<ushort*>(p_src_block);
352
353
354
355
356
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

Chao Liu's avatar
Chao Liu committed
357
358
359
360
361
#if !CK_WORKAROUND_SWDEV_231101
    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(ushort);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(ushort);

    return __llvm_amdgcn_buffer_load_bf16(
362
363
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
#else
Chao Liu's avatar
Chao Liu committed
364
    return p_src_block[src_thread_data_offset + src_const_data_offset];
365
#endif
Chao Liu's avatar
Chao Liu committed
366
367
368
369
370
371
372
373
}

template <>
__device__ ushort2_t amd_buffer_load<ushort, 2>(const ushort* p_src_block,
                                                index_t src_thread_data_offset,
                                                index_t src_const_data_offset)
{
    BufferAddressConfig<ushort> src_block_config;
374

Chao Liu's avatar
Chao Liu committed
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<ushort*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(ushort);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(ushort);

#if !CK_WORKAROUND_SWDEV_231101
    return __llvm_amdgcn_buffer_load_bf16x2(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
#else
    float dst_out_tmp = __llvm_amdgcn_buffer_load_f32(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);

    return *reinterpret_cast<ushort2_t*>(&dst_out_tmp);
#endif
394
395
396
}

template <>
Chao Liu's avatar
Chao Liu committed
397
398
399
__device__ ushort4_t amd_buffer_load<ushort, 4>(const ushort* p_src_block,
                                                index_t src_thread_data_offset,
                                                index_t src_const_data_offset)
400
{
Chao Liu's avatar
Chao Liu committed
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
    BufferAddressConfig<ushort> src_block_config;

    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<ushort*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(ushort);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(ushort);

#if !CK_WORKAROUND_SWDEV_231101
    return __llvm_amdgcn_buffer_load_bf16x4(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);
#else
    float2_t dst_out_tmp = __llvm_amdgcn_buffer_load_f32x2(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);

    return *reinterpret_cast<ushort4_t*>(&dst_out_tmp);
#endif
}

template <>
__device__ ushort8_t amd_buffer_load<ushort, 8>(const ushort* p_src_block,
                                                index_t src_thread_data_offset,
                                                index_t src_const_data_offset)
{
    BufferAddressConfig<ushort> src_block_config;

    // fill in byte 0 - 1
    src_block_config.address[0] = const_cast<ushort*>(p_src_block);
    // fill in byte 2
    src_block_config.range[2] = -1;
    // fill in byte 3
    src_block_config.range[3] = 0x00027000;

    index_t src_thread_addr_offset = src_thread_data_offset * sizeof(ushort);
    index_t src_const_addr_offset  = src_const_data_offset * sizeof(ushort);

#if !CK_WORKAROUND_SWDEV_231101
    static_assert(false, "wrong! not implemented");
#else
    float4_t dst_out_tmp = __llvm_amdgcn_buffer_load_f32x4(
        src_block_config.data, 0, src_thread_addr_offset + src_const_addr_offset, false, false);

    return *reinterpret_cast<ushort8_t*>(&dst_out_tmp);
#endif
}

template <>
__device__ void amd_buffer_store<float, 1>(const float* p_src,
                                           float* p_dst_block,
                                           index_t dst_thread_data_offset,
                                           index_t dst_const_data_offset)
{
    BufferAddressConfig<float> dst_block_config;

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

466
467
468
    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(float);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(float);

Chao Liu's avatar
Chao Liu committed
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
    __llvm_amdgcn_buffer_store_f32(*p_src,
                                   dst_block_config.data,
                                   0,
                                   dst_thread_addr_offset + dst_const_addr_offset,
                                   false,
                                   false);
}

template <>
__device__ void amd_buffer_store<float, 2>(const float* p_src,
                                           float* p_dst_block,
                                           index_t dst_thread_data_offset,
                                           index_t dst_const_data_offset)
{
    BufferAddressConfig<float> dst_block_config;
484
485
486
487
488
489
490
491

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

Chao Liu's avatar
Chao Liu committed
492
493
494
495
496
497
498
499
500
    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(float);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(float);

    __llvm_amdgcn_buffer_store_f32x2(*reinterpret_cast<const float2_t*>(p_src),
                                     dst_block_config.data,
                                     0,
                                     dst_thread_addr_offset + dst_const_addr_offset,
                                     false,
                                     false);
501
502
503
}

template <>
Chao Liu's avatar
Chao Liu committed
504
505
506
507
__device__ void amd_buffer_store<float, 4>(const float* p_src,
                                           float* p_dst_block,
                                           index_t dst_thread_data_offset,
                                           index_t dst_const_data_offset)
508
{
Chao Liu's avatar
Chao Liu committed
509
510
511
512
513
514
515
516
517
    BufferAddressConfig<float> dst_block_config;

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

518
519
520
    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(float);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(float);

Chao Liu's avatar
Chao Liu committed
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
    __llvm_amdgcn_buffer_store_f32x4(*reinterpret_cast<const float4_t*>(p_src),
                                     dst_block_config.data,
                                     0,
                                     dst_thread_addr_offset + dst_const_addr_offset,
                                     false,
                                     false);
}

template <>
__device__ void amd_buffer_store<half_t, 1>(const half_t* p_src,
                                            half_t* p_dst_block,
                                            index_t dst_thread_data_offset,
                                            index_t dst_const_data_offset)
{
    BufferAddressConfig<half_t> dst_block_config;
536
537
538
539
540
541
542
543

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

Chao Liu's avatar
Chao Liu committed
544
545
546
547
548
549
550
551
552
553
#if !CK_WORKAROUND_SWDEV_231101
    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(half_t);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(half_t);

    __llvm_amdgcn_buffer_store_f16(*p_src,
                                   dst_block_config.data,
                                   0,
                                   dst_thread_addr_offset + dst_const_addr_offset,
                                   false,
                                   false);
554
#else
Chao Liu's avatar
Chao Liu committed
555
    p_dst_block[dst_thread_data_offset + dst_const_data_offset] = *p_src;
556
557
558
559
#endif
}

template <>
Chao Liu's avatar
Chao Liu committed
560
561
562
563
__device__ void amd_buffer_store<half_t, 2>(const half_t* p_src,
                                            half_t* p_dst_block,
                                            index_t dst_thread_data_offset,
                                            index_t dst_const_data_offset)
564
{
Chao Liu's avatar
Chao Liu committed
565
566
567
568
569
570
571
572
573
574
575
    BufferAddressConfig<half_t> dst_block_config;

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(half_t);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(half_t);
576

Chao Liu's avatar
Chao Liu committed
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
#if !CK_WORKAROUND_SWDEV_231101
    __llvm_amdgcn_buffer_store_f16x2(*reinterpret_cast<const half2_t*>(p_src),
                                     dst_block_config.data,
                                     0,
                                     dst_thread_addr_offset + dst_const_addr_offset,
                                     false,
                                     false);
#else
    const float* p_src_tmp = reinterpret_cast<const float*>(p_src);

    __llvm_amdgcn_buffer_store_f32(*p_src_tmp,
                                   dst_block_config.data,
                                   0,
                                   dst_thread_addr_offset + dst_const_addr_offset,
                                   false,
                                   false);
#endif
}

template <>
__device__ void amd_buffer_store<half_t, 4>(const half_t* p_src,
                                            half_t* p_dst_block,
                                            index_t dst_thread_data_offset,
                                            index_t dst_const_data_offset)
{
    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(half_t);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(half_t);

    BufferAddressConfig<half_t> dst_block_config;
606
607
608
609
610
611
612
613

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

Chao Liu's avatar
Chao Liu committed
614
615
616
617
618
619
620
#if !CK_WORKAROUND_SWDEV_231101
    __llvm_amdgcn_buffer_store_f16x4(*reinterpret_cast<const half4_t*>(p_src),
                                     dst_block_config.data,
                                     0,
                                     dst_thread_addr_offset + dst_const_addr_offset,
                                     false,
                                     false);
621
#else
Chao Liu's avatar
Chao Liu committed
622
623
624
625
626
627
628
629
    const float2_t* p_src_tmp = reinterpret_cast<const float2_t*>(p_src);

    __llvm_amdgcn_buffer_store_f32x2(*p_src_tmp,
                                     dst_block_config.data,
                                     0,
                                     dst_thread_addr_offset + dst_const_addr_offset,
                                     false,
                                     false);
630
631
632
#endif
}

633
template <>
Chao Liu's avatar
Chao Liu committed
634
635
636
637
__device__ void amd_buffer_store<ushort, 1>(const ushort* p_src,
                                            ushort* p_dst_block,
                                            index_t dst_thread_data_offset,
                                            index_t dst_const_data_offset)
638
{
Chao Liu's avatar
Chao Liu committed
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
    BufferAddressConfig<ushort> dst_block_config;

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

#if !CK_WORKAROUND_SWDEV_231101
    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(ushort);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(ushort);

    __llvm_amdgcn_buffer_store_bf16(*p_src,
                                    dst_block_config.data,
                                    0,
                                    dst_thread_addr_offset + dst_const_addr_offset,
                                    false,
                                    false);
#else
    p_dst_block[dst_thread_data_offset + dst_const_data_offset] = *p_src;
#endif
}

template <>
__device__ void amd_buffer_store<ushort, 2>(const ushort* p_src,
                                            ushort* p_dst_block,
                                            index_t dst_thread_data_offset,
                                            index_t dst_const_data_offset)
{
    BufferAddressConfig<ushort> dst_block_config;

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;
677

Chao Liu's avatar
Chao Liu committed
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(ushort);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(ushort);

#if !CK_WORKAROUND_SWDEV_231101
    __llvm_amdgcn_buffer_store_bf16x2(*p_src,
                                      dst_block_config.data,
                                      0,
                                      dst_thread_addr_offset + dst_const_addr_offset,
                                      false,
                                      false);
#else
    const float* p_src_tmp = reinterpret_cast<const float*>(p_src);

    __llvm_amdgcn_buffer_store_f32(*p_src_tmp,
                                   dst_block_config.data,
                                   0,
                                   dst_thread_addr_offset + dst_const_addr_offset,
                                   false,
                                   false);
#endif
}

template <>
__device__ void amd_buffer_store<ushort, 4>(const ushort* p_src,
                                            ushort* p_dst_block,
                                            index_t dst_thread_data_offset,
                                            index_t dst_const_data_offset)
{
    BufferAddressConfig<ushort> dst_block_config;
707
708
709
710
711
712
713
714

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

Chao Liu's avatar
Chao Liu committed
715
716
717
718
719
720
721
722
723
724
    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(ushort);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(ushort);

#if !CK_WORKAROUND_SWDEV_231101
    __llvm_amdgcn_buffer_store_bf16x4(*p_src,
                                      dst_block_config.data,
                                      0,
                                      dst_thread_addr_offset + dst_const_addr_offset,
                                      false,
                                      false);
725
#else
Chao Liu's avatar
Chao Liu committed
726
727
728
729
730
731
732
733
    const float2_t* p_src_tmp = reinterpret_cast<const float2_t*>(p_src);

    __llvm_amdgcn_buffer_store_f32x2(*p_src_tmp,
                                     dst_block_config.data,
                                     0,
                                     dst_thread_addr_offset + dst_const_addr_offset,
                                     false,
                                     false);
734
735
736
#endif
}

Chao Liu's avatar
Chao Liu committed
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
template <>
__device__ void amd_buffer_atomic_add<float, 1>(const float* p_src,
                                                float* p_dst_block,
                                                index_t dst_thread_data_offset,
                                                index_t dst_const_data_offset)
{
    BufferAddressConfig<float> dst_block_config;

    // fill in byte 0 - 1
    dst_block_config.address[0] = p_dst_block;
    // fill in byte 2
    dst_block_config.range[2] = -1;
    // fill in byte 3
    dst_block_config.range[3] = 0x00027000;

    index_t dst_thread_addr_offset = dst_thread_data_offset * sizeof(float);
    index_t dst_const_addr_offset  = dst_const_data_offset * sizeof(float);

    __llvm_amdgcn_buffer_atomic_add_f32(
        *p_src, dst_block_config.data, 0, dst_thread_addr_offset + dst_const_addr_offset, false);
}

template <>
__device__ void amd_buffer_atomic_add<float, 2>(const float* p_src,
                                                float* p_dst_block,
                                                index_t dst_thread_data_offset,
                                                index_t dst_const_data_offset)
{
    for(index_t i = 0; i < 2; ++i)
    {
        amd_buffer_atomic_add<float, 1>(
            &p_src[i], p_dst_block, dst_thread_data_offset, dst_const_data_offset + i);
    }
}

template <>
__device__ void amd_buffer_atomic_add<float, 4>(const float* p_src,
                                                float* p_dst_block,
                                                index_t dst_thread_data_offset,
                                                index_t dst_const_data_offset)
{
    for(index_t i = 0; i < 4; ++i)
    {
        amd_buffer_atomic_add<float, 1>(
            &p_src[i], p_dst_block, dst_thread_data_offset, dst_const_data_offset + i);
    }
}

785
786
} // namespace ck
#endif