amd_buffer_addressing.hpp 60.4 KB
Newer Older
Umang Yadav's avatar
Umang Yadav committed
1
2
3

#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Weverything"
Chao Liu's avatar
Chao Liu committed
4
// SPDX-License-Identifier: MIT
Illia Silin's avatar
Illia Silin committed
5
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
Chao Liu's avatar
Chao Liu committed
6

7
#pragma once
8
#include "data_type.hpp"
Chao Liu's avatar
Chao Liu committed
9
10
11
12

namespace ck {

template <typename T>
Chao Liu's avatar
tidy  
Chao Liu committed
13
union BufferResource
Chao Liu's avatar
Chao Liu committed
14
{
15
16
    __device__ constexpr BufferResource() : content{} {}

Chao Liu's avatar
Chao Liu committed
17
18
    // 128 bit SGPRs to supply buffer resource in buffer instructions
    // https://rocm-documentation.readthedocs.io/en/latest/GCN_ISA_Manuals/testdocbook.html#vector-memory-buffer-instructions
19
    int32x4_t content;
20
21
22
    StaticallyIndexedArray<T*, 2> address;
    StaticallyIndexedArray<int32_t, 4> range;
    StaticallyIndexedArray<int32_t, 4> config;
Chao Liu's avatar
Chao Liu committed
23
24
25
};

template <typename T>
26
__device__ int32x4_t make_wave_buffer_resource(T* p_wave, index_t element_space_size)
Chao Liu's avatar
Chao Liu committed
27
{
Chao Liu's avatar
tidy  
Chao Liu committed
28
    BufferResource<T> wave_buffer_resource;
Chao Liu's avatar
Chao Liu committed
29
30

    // wavewise base address (64 bit)
31
    wave_buffer_resource.address(Number<0>{}) = const_cast<remove_cv_t<T>*>(p_wave);
Chao Liu's avatar
Chao Liu committed
32
    // wavewise range (32 bit)
33
    wave_buffer_resource.range(Number<2>{}) = element_space_size * sizeof(T);
Chao Liu's avatar
Chao Liu committed
34
    // wavewise setting (32 bit)
35
    wave_buffer_resource.config(Number<3>{}) = CK_BUFFER_RESOURCE_3RD_DWORD;
Chao Liu's avatar
Chao Liu committed
36

37
    return wave_buffer_resource.content;
Chao Liu's avatar
Chao Liu committed
38
39
}

carlushuang's avatar
carlushuang committed
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
template <typename T>
__device__ int32x4_t make_wave_buffer_resource_with_default_range(T* p_wave)
{
    BufferResource<T> wave_buffer_resource;

    // wavewise base address (64 bit)
    wave_buffer_resource.address(Number<0>{}) = const_cast<remove_cv_t<T>*>(p_wave);
    // wavewise range (32 bit)
    wave_buffer_resource.range(Number<2>{}) = 0xffffffff; // max possible range
    // wavewise setting (32 bit)
    wave_buffer_resource.config(Number<3>{}) = CK_BUFFER_RESOURCE_3RD_DWORD;

    return wave_buffer_resource.content;
}

Chao Liu's avatar
Chao Liu committed
55
// buffer load i8
Chao Liu's avatar
Chao Liu committed
56
__device__ int8_t
57
58
59
60
llvm_amdgcn_raw_buffer_load_i8(int32x4_t srsrc,
                               index_t voffset,
                               index_t soffset,
                               index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.i8");
61
62

__device__ int8x2_t
63
64
65
66
llvm_amdgcn_raw_buffer_load_i8x2(int32x4_t srsrc,
                                 index_t voffset,
                                 index_t soffset,
                                 index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v2i8");
67
68

__device__ int8x4_t
69
70
71
72
llvm_amdgcn_raw_buffer_load_i8x4(int32x4_t srsrc,
                                 index_t voffset,
                                 index_t soffset,
                                 index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v4i8");
73

Chao Liu's avatar
Chao Liu committed
74
// buffer load i16
75
__device__ bhalf_t
76
77
78
llvm_amdgcn_raw_buffer_load_i16(int32x4_t srsrc,
                                index_t voffset,
                                index_t soffset,
79
80
                                index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.i16");

81
__device__ bhalf2_t
82
83
84
85
86
llvm_amdgcn_raw_buffer_load_i16x2(int32x4_t srsrc,
                                  index_t voffset,
                                  index_t soffset,
                                  index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v2i16");

87
__device__ bhalf4_t
88
89
90
91
92
llvm_amdgcn_raw_buffer_load_i16x4(int32x4_t srsrc,
                                  index_t voffset,
                                  index_t soffset,
                                  index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v4i16");

Chao Liu's avatar
Chao Liu committed
93
// buffer load i32
Chao Liu's avatar
Chao Liu committed
94
__device__ int32_t
95
96
97
98
llvm_amdgcn_raw_buffer_load_i32(int32x4_t srsrc,
                                index_t voffset,
                                index_t soffset,
                                index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.i32");
Chao Liu's avatar
Chao Liu committed
99
100

__device__ int32x2_t
101
102
103
104
llvm_amdgcn_raw_buffer_load_i32x2(int32x4_t srsrc,
                                  index_t voffset,
                                  index_t soffset,
                                  index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v2i32");
Chao Liu's avatar
Chao Liu committed
105
106

__device__ int32x4_t
107
108
109
110
llvm_amdgcn_raw_buffer_load_i32x4(int32x4_t srsrc,
                                  index_t voffset,
                                  index_t soffset,
                                  index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v4i32");
111

Chao Liu's avatar
Chao Liu committed
112
// buffer load fp16
113
__device__ half_t
114
115
116
117
llvm_amdgcn_raw_buffer_load_fp16(int32x4_t srsrc,
                                 index_t voffset,
                                 index_t soffset,
                                 index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.f16");
118
119

__device__ half2_t
120
121
122
123
llvm_amdgcn_raw_buffer_load_fp16x2(int32x4_t srsrc,
                                   index_t voffset,
                                   index_t soffset,
                                   index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v2f16");
124
125

__device__ half4_t
126
127
128
129
llvm_amdgcn_raw_buffer_load_fp16x4(int32x4_t srsrc,
                                   index_t voffset,
                                   index_t soffset,
                                   index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v4f16");
Chao Liu's avatar
Chao Liu committed
130

Chao Liu's avatar
Chao Liu committed
131
// buffer load fp32
Chao Liu's avatar
Chao Liu committed
132
__device__ float
133
134
135
136
llvm_amdgcn_raw_buffer_load_fp32(int32x4_t srsrc,
                                 index_t voffset,
                                 index_t soffset,
                                 index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.f32");
Chao Liu's avatar
Chao Liu committed
137
138

__device__ float2_t
139
140
141
142
llvm_amdgcn_raw_buffer_load_fp32x2(int32x4_t srsrc,
                                   index_t voffset,
                                   index_t soffset,
                                   index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v2f32");
Chao Liu's avatar
Chao Liu committed
143
144

__device__ float4_t
145
146
147
148
llvm_amdgcn_raw_buffer_load_fp32x4(int32x4_t srsrc,
                                   index_t voffset,
                                   index_t soffset,
                                   index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.load.v4f32");
Chao Liu's avatar
Chao Liu committed
149

Chao Liu's avatar
Chao Liu committed
150
// buffer store i8
Chao Liu's avatar
Chao Liu committed
151
__device__ void
152
153
154
155
156
157
158
159
llvm_amdgcn_raw_buffer_store_i8(int8_t vdata,
                                int32x4_t rsrc,
                                index_t voffset,
                                index_t soffset,
                                index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.i8");

__device__ void
llvm_amdgcn_raw_buffer_store_i8x2(int8x2_t vdata,
Chao Liu's avatar
Chao Liu committed
160
161
162
                                  int32x4_t rsrc,
                                  index_t voffset,
                                  index_t soffset,
163
                                  index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v2i8");
Chao Liu's avatar
Chao Liu committed
164

165
__device__ void
166
167
168
169
170
llvm_amdgcn_raw_buffer_store_i8x4(int8x4_t vdata,
                                  int32x4_t rsrc,
                                  index_t voffset,
                                  index_t soffset,
                                  index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v4i8");
171

Chao Liu's avatar
Chao Liu committed
172
// buffer store i16
173
__device__ void
174
llvm_amdgcn_raw_buffer_store_i16(bhalf_t vdata,
175
176
177
178
179
                                 int32x4_t rsrc,
                                 index_t voffset,
                                 index_t soffset,
                                 index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.i16");

180
__device__ void
181
llvm_amdgcn_raw_buffer_store_i16x2(bhalf2_t vdata,
182
183
184
185
186
187
                                   int32x4_t rsrc,
                                   index_t voffset,
                                   index_t soffset,
                                   index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v2i16");

__device__ void
188
llvm_amdgcn_raw_buffer_store_i16x4(bhalf4_t vdata,
189
190
191
192
193
                                   int32x4_t rsrc,
                                   index_t voffset,
                                   index_t soffset,
                                   index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v4i16");

Chao Liu's avatar
Chao Liu committed
194
// buffer store i32
195
196
197
198
199
200
__device__ void
llvm_amdgcn_raw_buffer_store_i32(int32_t vdata,
                                 int32x4_t rsrc,
                                 index_t voffset,
                                 index_t soffset,
                                 index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.i32");
201

Chao Liu's avatar
Chao Liu committed
202
__device__ void
203
llvm_amdgcn_raw_buffer_store_i32x2(int32x2_t vdata,
Chao Liu's avatar
Chao Liu committed
204
205
206
                                   int32x4_t rsrc,
                                   index_t voffset,
                                   index_t soffset,
207
                                   index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v2i32");
Chao Liu's avatar
Chao Liu committed
208
209

__device__ void
210
llvm_amdgcn_raw_buffer_store_i32x4(int32x4_t vdata,
Chao Liu's avatar
Chao Liu committed
211
212
213
                                   int32x4_t rsrc,
                                   index_t voffset,
                                   index_t soffset,
214
                                   index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v4i32");
Chao Liu's avatar
Chao Liu committed
215

Chao Liu's avatar
Chao Liu committed
216
// buffer store fp16
Chao Liu's avatar
Chao Liu committed
217
__device__ void
218
219
220
221
222
llvm_amdgcn_raw_buffer_store_fp16(half_t vdata,
                                  int32x4_t rsrc,
                                  index_t voffset,
                                  index_t soffset,
                                  index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.f16");
Chao Liu's avatar
Chao Liu committed
223
224

__device__ void
225
226
227
228
229
llvm_amdgcn_raw_buffer_store_fp16x2(half2_t vdata,
                                    int32x4_t rsrc,
                                    index_t voffset,
                                    index_t soffset,
                                    index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v2f16");
Chao Liu's avatar
Chao Liu committed
230

231
__device__ void
232
llvm_amdgcn_raw_buffer_store_fp16x4(half4_t vdata,
233
234
235
                                    int32x4_t rsrc,
                                    index_t voffset,
                                    index_t soffset,
236
                                    index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v4f16");
237

Chao Liu's avatar
Chao Liu committed
238
// buffer store fp32
239
__device__ void
240
241
242
243
244
llvm_amdgcn_raw_buffer_store_fp32(float vdata,
                                  int32x4_t rsrc,
                                  index_t voffset,
                                  index_t soffset,
                                  index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.f32");
245
246

__device__ void
247
llvm_amdgcn_raw_buffer_store_fp32x2(float2_t vdata,
Chao Liu's avatar
Chao Liu committed
248
249
250
                                    int32x4_t rsrc,
                                    index_t voffset,
                                    index_t soffset,
251
                                    index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v2f32");
Chao Liu's avatar
Chao Liu committed
252
253

__device__ void
254
255
256
257
258
llvm_amdgcn_raw_buffer_store_fp32x4(float4_t vdata,
                                    int32x4_t rsrc,
                                    index_t voffset,
                                    index_t soffset,
                                    index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.store.v4f32");
259

Chao Liu's avatar
Chao Liu committed
260
261
262
263
264
265
266
267
268
// buffer atomic-add fp16
__device__ half2_t llvm_amdgcn_raw_buffer_atomic_add_fp16x2(
    half2_t vdata,
    int32x4_t rsrc,
    index_t voffset,
    index_t soffset,
    index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.atomic.fadd.v2f16");

// buffer atomic-add i32
zjing14's avatar
zjing14 committed
269
270
271
272
273
274
275
__device__ int32_t llvm_amdgcn_raw_buffer_atomic_add_i32(
    int32_t vdata,
    int32x4_t rsrc,
    index_t voffset,
    index_t soffset,
    index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.atomic.add.i32");

Chao Liu's avatar
Chao Liu committed
276
// buffer atomic-add fp32
zjing14's avatar
zjing14 committed
277
278
279
280
281
282
__device__ float llvm_amdgcn_raw_buffer_atomic_add_fp32(
    float vdata,
    int32x4_t rsrc,
    index_t voffset,
    index_t soffset,
    index_t glc_slc) __asm("llvm.amdgcn.raw.buffer.atomic.fadd.f32");
Chao Liu's avatar
Chao Liu committed
283

rocking5566's avatar
rocking5566 committed
284
285
286
287
288
289
290
291
// buffer atomic-add fp32
__device__ double
llvm_amdgcn_raw_buffer_atomic_max_fp64(double vdata,
                                       int32x4_t rsrc, // dst_wave_buffer_resource
                                       int voffset,    // dst_thread_addr_offset
                                       int soffset,    // dst_wave_addr_offset
                                       int glc_slc) __asm("llvm.amdgcn.raw.buffer.atomic.fmax.f64");

292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// memory coherency bit for buffer store/load instruction
// check ISA manual for each GFX target
// e.g. for
// https://www.amd.com/system/files/TechDocs/instinct-mi200-cdna2-instruction-set-architecture.pdf,
// page 67~68
enum struct AmdBufferCoherenceEnum
{
    DefaultCoherence = 0, // default value
    GLC              = 1,
    SLC              = 2,
    GLC_SLC          = 3,
};

template <typename T,
          index_t N,
          AmdBufferCoherenceEnum coherence = AmdBufferCoherenceEnum::DefaultCoherence>
308
309
310
__device__ typename vector_type<T, N>::type amd_buffer_load_impl(int32x4_t src_wave_buffer_resource,
                                                                 index_t src_thread_addr_offset,
                                                                 index_t src_wave_addr_offset)
Chao Liu's avatar
Chao Liu committed
311
{
312
    static_assert(
Chao Liu's avatar
Chao Liu committed
313
314
        (is_same<T, double>::value && (N == 1 || N == 2 || N == 4)) ||
            (is_same<T, float>::value && (N == 1 || N == 2 || N == 4 || N == 8)) ||
315
            (is_same<T, half_t>::value && (N == 1 || N == 2 || N == 4 || N == 8)) ||
316
            (is_same<T, bhalf_t>::value && (N == 1 || N == 2 || N == 4 || N == 8)) ||
Chao Liu's avatar
Chao Liu committed
317
318
            (is_same<T, int32_t>::value && (N == 1 || N == 2 || N == 4 || N == 8)) ||
            (is_same<T, int8_t>::value && (N == 1 || N == 2 || N == 4 || N == 8 || N == 16)),
319
        "wrong! not implemented");
Chao Liu's avatar
Chao Liu committed
320

Chao Liu's avatar
Chao Liu committed
321
322
323
324
325
    if constexpr(is_same<T, double>::value)
    {
        // use fp32 load to mimic fp64 load
        if constexpr(N == 1)
        {
326
327
328
329
330
            const float2_t tmp =
                llvm_amdgcn_raw_buffer_load_fp32x2(src_wave_buffer_resource,
                                                   src_thread_addr_offset,
                                                   src_wave_addr_offset,
                                                   static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
331

332
            return bit_cast<double>(tmp);
Chao Liu's avatar
Chao Liu committed
333
334
335
        }
        else if constexpr(N == 2)
        {
336
337
338
339
340
            const float4_t tmp =
                llvm_amdgcn_raw_buffer_load_fp32x4(src_wave_buffer_resource,
                                                   src_thread_addr_offset,
                                                   src_wave_addr_offset,
                                                   static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
341

342
            return bit_cast<double2_t>(tmp);
Chao Liu's avatar
Chao Liu committed
343
344
345
        }
        else if constexpr(N == 4)
        {
346
347
348
349
350
            const float4_t f32_0 =
                llvm_amdgcn_raw_buffer_load_fp32x4(src_wave_buffer_resource,
                                                   src_thread_addr_offset,
                                                   src_wave_addr_offset,
                                                   static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
351
352
353
354
355

            const float4_t f32_1 =
                llvm_amdgcn_raw_buffer_load_fp32x4(src_wave_buffer_resource,
                                                   src_thread_addr_offset,
                                                   src_wave_addr_offset + 4 * sizeof(float),
356
                                                   static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
357
358
            vector_type<double, 4> tmp;

359
360
            tmp.AsType<double2_t>()(Number<0>{}) = bit_cast<double2_t>(f32_0);
            tmp.AsType<double2_t>()(Number<1>{}) = bit_cast<double2_t>(f32_1);
Chao Liu's avatar
Chao Liu committed
361
362
363
364
365

            return tmp.AsType<double4_t>()(Number<0>{});
        }
    }
    else if constexpr(is_same<T, float>::value)
Chao Liu's avatar
Chao Liu committed
366
367
368
    {
        if constexpr(N == 1)
        {
369
370
371
372
            return llvm_amdgcn_raw_buffer_load_fp32(src_wave_buffer_resource,
                                                    src_thread_addr_offset,
                                                    src_wave_addr_offset,
                                                    static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
373
374
375
        }
        else if constexpr(N == 2)
        {
376
377
378
379
            return llvm_amdgcn_raw_buffer_load_fp32x2(src_wave_buffer_resource,
                                                      src_thread_addr_offset,
                                                      src_wave_addr_offset,
                                                      static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
380
381
382
        }
        else if constexpr(N == 4)
        {
383
384
385
386
            return llvm_amdgcn_raw_buffer_load_fp32x4(src_wave_buffer_resource,
                                                      src_thread_addr_offset,
                                                      src_wave_addr_offset,
                                                      static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
387
388
389
390
391
        }
        else if constexpr(N == 8)
        {
            vector_type<float, 8> tmp;

392
393
394
395
396
            tmp.AsType<float4_t>()(Number<0>{}) =
                llvm_amdgcn_raw_buffer_load_fp32x4(src_wave_buffer_resource,
                                                   src_thread_addr_offset,
                                                   src_wave_addr_offset,
                                                   static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
397

398
            tmp.AsType<float4_t>()(Number<1>{}) =
399
400
401
                llvm_amdgcn_raw_buffer_load_fp32x4(src_wave_buffer_resource,
                                                   src_thread_addr_offset,
                                                   src_wave_addr_offset + 4 * sizeof(float),
402
                                                   static_cast<index_t>(coherence));
403

404
            return tmp.AsType<float8_t>()(Number<0>{});
405
406
407
408
409
410
        }
    }
    else if constexpr(is_same<T, half_t>::value)
    {
        if constexpr(N == 1)
        {
411
412
413
414
            return llvm_amdgcn_raw_buffer_load_fp16(src_wave_buffer_resource,
                                                    src_thread_addr_offset,
                                                    src_wave_addr_offset,
                                                    static_cast<index_t>(coherence));
415
416
417
        }
        else if constexpr(N == 2)
        {
418
419
420
421
            return llvm_amdgcn_raw_buffer_load_fp16x2(src_wave_buffer_resource,
                                                      src_thread_addr_offset,
                                                      src_wave_addr_offset,
                                                      static_cast<index_t>(coherence));
422
423
424
        }
        else if constexpr(N == 4)
        {
425
426
427
428
            return llvm_amdgcn_raw_buffer_load_fp16x4(src_wave_buffer_resource,
                                                      src_thread_addr_offset,
                                                      src_wave_addr_offset,
                                                      static_cast<index_t>(coherence));
429
        }
430
        else if constexpr(N == 8)
431
        {
Chao Liu's avatar
Chao Liu committed
432
            // use fp32 load to mimic fp16 load
433
434
435
436
            float4_t tmp = llvm_amdgcn_raw_buffer_load_fp32x4(src_wave_buffer_resource,
                                                              src_thread_addr_offset,
                                                              src_wave_addr_offset,
                                                              static_cast<index_t>(coherence));
zjing14's avatar
zjing14 committed
437

438
            return bit_cast<half8_t>(tmp);
Chao Liu's avatar
Chao Liu committed
439
440
        }
    }
441
    else if constexpr(is_same<T, bhalf_t>::value)
442
443
444
    {
        if constexpr(N == 1)
        {
445
446
447
448
            return llvm_amdgcn_raw_buffer_load_i16(src_wave_buffer_resource,
                                                   src_thread_addr_offset,
                                                   src_wave_addr_offset,
                                                   static_cast<index_t>(coherence));
449
450
451
        }
        else if constexpr(N == 2)
        {
452
453
454
455
            return llvm_amdgcn_raw_buffer_load_i16x2(src_wave_buffer_resource,
                                                     src_thread_addr_offset,
                                                     src_wave_addr_offset,
                                                     static_cast<index_t>(coherence));
456
457
458
        }
        else if constexpr(N == 4)
        {
459
460
461
462
            return llvm_amdgcn_raw_buffer_load_i16x4(src_wave_buffer_resource,
                                                     src_thread_addr_offset,
                                                     src_wave_addr_offset,
                                                     static_cast<index_t>(coherence));
463
464
465
        }
        else if constexpr(N == 8)
        {
466
467
468
469
            int32x4_t tmp = llvm_amdgcn_raw_buffer_load_i32x4(src_wave_buffer_resource,
                                                              src_thread_addr_offset,
                                                              src_wave_addr_offset,
                                                              static_cast<index_t>(coherence));
470

471
            return bit_cast<bhalf8_t>(tmp);
472
473
        }
    }
Chao Liu's avatar
Chao Liu committed
474
475
476
477
    else if constexpr(is_same<T, int32_t>::value)
    {
        if constexpr(N == 1)
        {
478
479
480
481
            return llvm_amdgcn_raw_buffer_load_i32(src_wave_buffer_resource,
                                                   src_thread_addr_offset,
                                                   src_wave_addr_offset,
                                                   static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
482
483
484
        }
        else if constexpr(N == 2)
        {
485
486
487
488
            return llvm_amdgcn_raw_buffer_load_i32x2(src_wave_buffer_resource,
                                                     src_thread_addr_offset,
                                                     src_wave_addr_offset,
                                                     static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
489
490
491
        }
        else if constexpr(N == 4)
        {
492
493
494
495
            return llvm_amdgcn_raw_buffer_load_i32x4(src_wave_buffer_resource,
                                                     src_thread_addr_offset,
                                                     src_wave_addr_offset,
                                                     static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
496
497
498
499
500
        }
        else if constexpr(N == 8)
        {
            vector_type<int32_t, 8> tmp;

501
502
503
504
505
            tmp.AsType<int32x4_t>()(Number<0>{}) =
                llvm_amdgcn_raw_buffer_load_i32x4(src_wave_buffer_resource,
                                                  src_thread_addr_offset,
                                                  src_wave_addr_offset,
                                                  static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
506

507
            tmp.AsType<int32x4_t>()(Number<1>{}) =
508
509
510
                llvm_amdgcn_raw_buffer_load_i32x4(src_wave_buffer_resource,
                                                  src_thread_addr_offset,
                                                  src_wave_addr_offset + 4 * sizeof(int32_t),
511
                                                  static_cast<index_t>(coherence));
512
            return tmp.AsType<int32x8_t>()(Number<0>{});
Chao Liu's avatar
Chao Liu committed
513
514
        }
    }
515
    else if constexpr(is_same<T, int8_t>::value)
516
517
518
    {
        if constexpr(N == 1)
        {
519
520
521
522
            return llvm_amdgcn_raw_buffer_load_i8(src_wave_buffer_resource,
                                                  src_thread_addr_offset,
                                                  src_wave_addr_offset,
                                                  static_cast<index_t>(coherence));
523
        }
524
        else if constexpr(N == 2)
525
        {
526
#if !CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE
527
528
529
530
            return llvm_amdgcn_raw_buffer_load_i8x2(src_wave_buffer_resource,
                                                    src_thread_addr_offset,
                                                    src_wave_addr_offset,
                                                    static_cast<index_t>(coherence));
531
#else
532
533
534
535
            int16_t tmp = llvm_amdgcn_raw_buffer_load_i16(src_wave_buffer_resource,
                                                          src_thread_addr_offset,
                                                          src_wave_addr_offset,
                                                          static_cast<index_t>(coherence));
536

537
            return bit_cast<int8x2_t>(tmp);
538
539
540
541
#endif
        }
        else if constexpr(N == 4)
        {
542
#if !CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE
543
544
545
546
            return llvm_amdgcn_raw_buffer_load_i8x4(src_wave_buffer_resource,
                                                    src_thread_addr_offset,
                                                    src_wave_addr_offset,
                                                    static_cast<index_t>(coherence));
547
#else
548
549
550
551
            int32_t tmp = llvm_amdgcn_raw_buffer_load_i32(src_wave_buffer_resource,
                                                          src_thread_addr_offset,
                                                          src_wave_addr_offset,
                                                          static_cast<index_t>(coherence));
552

553
            return bit_cast<int8x4_t>(tmp);
554
555
556
557
#endif
        }
        else if constexpr(N == 8)
        {
558
#if !CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE
559
560
            vector_type<int8_t, 8> tmp;

561
562
563
564
565
            tmp.AsType<int8x4_t>()(Number<0>{}) =
                llvm_amdgcn_raw_buffer_load_i8x4(src_wave_buffer_resource,
                                                 src_thread_addr_offset,
                                                 src_wave_addr_offset,
                                                 static_cast<index_t>(coherence));
566
567

            tmp.AsType<int8x4_t>()(Number<1>{}) =
568
569
570
                llvm_amdgcn_raw_buffer_load_i8x4(src_wave_buffer_resource,
                                                 src_thread_addr_offset,
                                                 src_wave_addr_offset + 4 * sizeof(int8_t),
571
                                                 static_cast<index_t>(coherence));
572
573
574

            return tmp.AsType<int8x8_t>()(Number<0>{});
#else
575
576
577
578
            int32x2_t tmp = llvm_amdgcn_raw_buffer_load_i32x2(src_wave_buffer_resource,
                                                              src_thread_addr_offset,
                                                              src_wave_addr_offset,
                                                              static_cast<index_t>(coherence));
579

580
            return bit_cast<int8x8_t>(tmp);
581
582
583
584
#endif
        }
        else if constexpr(N == 16)
        {
585
#if !CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE
586
587
            vector_type<int8_t, 16> tmp;

588
589
590
591
592
            tmp.AsType<int8x4_t>()(Number<0>{}) =
                llvm_amdgcn_raw_buffer_load_i8x4(src_wave_buffer_resource,
                                                 src_thread_addr_offset,
                                                 src_wave_addr_offset,
                                                 static_cast<index_t>(coherence));
593
594

            tmp.AsType<int8x4_t>()(Number<1>{}) =
595
596
597
                llvm_amdgcn_raw_buffer_load_i8x4(src_wave_buffer_resource,
                                                 src_thread_addr_offset,
                                                 src_wave_addr_offset + 4 * sizeof(int8_t),
598
                                                 static_cast<index_t>(coherence));
599
600

            tmp.AsType<int8x4_t>()(Number<2>{}) =
601
602
603
                llvm_amdgcn_raw_buffer_load_i8x4(src_wave_buffer_resource,
                                                 src_thread_addr_offset,
                                                 src_wave_addr_offset + 8 * sizeof(int8_t),
604
                                                 static_cast<index_t>(coherence));
605
606

            tmp.AsType<int8x4_t>()(Number<3>{}) =
607
608
609
                llvm_amdgcn_raw_buffer_load_i8x4(src_wave_buffer_resource,
                                                 src_thread_addr_offset,
                                                 src_wave_addr_offset + 12 * sizeof(int8_t),
610
                                                 static_cast<index_t>(coherence));
611
612
613

            return tmp.AsType<int8x16_t>()(Number<0>{});
#else
614
615
616
617
            int32x4_t tmp = llvm_amdgcn_raw_buffer_load_i32x4(src_wave_buffer_resource,
                                                              src_thread_addr_offset,
                                                              src_wave_addr_offset,
                                                              static_cast<index_t>(coherence));
618

619
            return bit_cast<int8x16_t>(tmp);
620
#endif
621
622
        }
    }
Chao Liu's avatar
Chao Liu committed
623
624
}

625
626
627
template <typename T,
          index_t N,
          AmdBufferCoherenceEnum coherence = AmdBufferCoherenceEnum::DefaultCoherence>
628
629
630
631
__device__ void amd_buffer_store_impl(const typename vector_type<T, N>::type src_thread_data,
                                      int32x4_t dst_wave_buffer_resource,
                                      index_t dst_thread_addr_offset,
                                      index_t dst_wave_addr_offset)
Chao Liu's avatar
Chao Liu committed
632
{
633
    static_assert(
Chao Liu's avatar
Chao Liu committed
634
        (is_same<T, double>::value && (N == 1 || N == 2)) ||
635
            (is_same<T, float>::value && (N == 1 || N == 2 || N == 4 || N == 8)) ||
Chao Liu's avatar
Chao Liu committed
636
            (is_same<T, half_t>::value && (N == 1 || N == 2 || N == 4 || N == 8)) ||
637
            (is_same<T, bhalf_t>::value && (N == 1 || N == 2 || N == 4 || N == 8)) ||
638
            (is_same<T, int32_t>::value && (N == 1 || N == 2 || N == 4)) ||
Chao Liu's avatar
Chao Liu committed
639
            (is_same<T, int8_t>::value && (N == 1 || N == 2 || N == 4 || N == 8 || N == 16)),
640
        "wrong! not implemented");
Chao Liu's avatar
Chao Liu committed
641

Chao Liu's avatar
Chao Liu committed
642
643
644
645
646
    if constexpr(is_same<T, double>::value)
    {
        // use fp32 store to mimic fp64 store
        if constexpr(N == 1)
        {
647
            llvm_amdgcn_raw_buffer_store_fp32x2(bit_cast<float2_t>(src_thread_data),
Chao Liu's avatar
Chao Liu committed
648
649
650
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
651
                                                static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
652
653
654
        }
        else if constexpr(N == 2)
        {
655
            llvm_amdgcn_raw_buffer_store_fp32x4(bit_cast<float4_t>(src_thread_data),
Chao Liu's avatar
Chao Liu committed
656
657
658
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
659
                                                static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
660
661
662
        }
    }
    else if constexpr(is_same<T, float>::value)
Chao Liu's avatar
Chao Liu committed
663
664
665
    {
        if constexpr(N == 1)
        {
666
667
668
669
            llvm_amdgcn_raw_buffer_store_fp32(src_thread_data,
                                              dst_wave_buffer_resource,
                                              dst_thread_addr_offset,
                                              dst_wave_addr_offset,
670
                                              static_cast<index_t>(coherence));
671
672
673
674
        }
        else if constexpr(N == 2)
        {
            llvm_amdgcn_raw_buffer_store_fp32x2(src_thread_data,
Chao Liu's avatar
Chao Liu committed
675
676
677
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
678
                                                static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
679
680
681
        }
        else if constexpr(N == 4)
        {
682
683
684
685
            llvm_amdgcn_raw_buffer_store_fp32x4(src_thread_data,
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
686
                                                static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
687
        }
688
689
690
691
692
693
694
695
696
697
698
699
700
701
        else if constexpr(N == 8)
        {
            vector_type<float, 8> tmp{src_thread_data};
            llvm_amdgcn_raw_buffer_store_fp32x4(tmp.AsType<float4_t>()[Number<0>{}],
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
                                                static_cast<index_t>(coherence));
            llvm_amdgcn_raw_buffer_store_fp32x4(tmp.AsType<float4_t>()[Number<1>{}],
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset + 4 * sizeof(float),
                                                static_cast<index_t>(coherence));
        }
Chao Liu's avatar
Chao Liu committed
702
    }
Chao Liu's avatar
Chao Liu committed
703
704
705
706
707
708
709
710
    else if constexpr(is_same<T, half_t>::value)
    {
        if constexpr(N == 1)
        {
            llvm_amdgcn_raw_buffer_store_fp16(src_thread_data,
                                              dst_wave_buffer_resource,
                                              dst_thread_addr_offset,
                                              dst_wave_addr_offset,
711
                                              static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
712
713
714
715
716
717
718
        }
        else if constexpr(N == 2)
        {
            llvm_amdgcn_raw_buffer_store_fp16x2(src_thread_data,
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
719
                                                static_cast<index_t>(coherence));
720
721
722
723
724
725
726
        }
        else if constexpr(N == 4)
        {
            llvm_amdgcn_raw_buffer_store_fp16x4(src_thread_data,
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
727
                                                static_cast<index_t>(coherence));
728
729
730
        }
        else if constexpr(N == 8)
        {
731
#if 0
732
733
734
735
736
737
            vector_type<half_t, 8> tmp{src_thread_data};

            llvm_amdgcn_raw_buffer_store_fp16x4(tmp.AsType<half4_t>()[Number<0>{}],
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
738
                                                static_cast<index_t>(coherence));
739
740
741
742
743

            llvm_amdgcn_raw_buffer_store_fp16x4(tmp.AsType<half4_t>()[Number<1>{}],
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset + 4 * sizeof(half_t),
744
                                                static_cast<index_t>(coherence));
745
#else
746
            llvm_amdgcn_raw_buffer_store_fp32x4(bit_cast<float4_t>(src_thread_data),
747
748
749
                                                dst_wave_buffer_resource,
                                                dst_thread_addr_offset,
                                                dst_wave_addr_offset,
750
                                                static_cast<index_t>(coherence));
751
#endif
752
753
        }
    }
754
    else if constexpr(is_same<T, bhalf_t>::value)
755
756
757
758
759
760
761
    {
        if constexpr(N == 1)
        {
            llvm_amdgcn_raw_buffer_store_i16(src_thread_data,
                                             dst_wave_buffer_resource,
                                             dst_thread_addr_offset,
                                             dst_wave_addr_offset,
762
                                             static_cast<index_t>(coherence));
763
764
765
        }
        else if constexpr(N == 2)
        {
766
767
768
769
            llvm_amdgcn_raw_buffer_store_i16x2(src_thread_data,
                                               dst_wave_buffer_resource,
                                               dst_thread_addr_offset,
                                               dst_wave_addr_offset,
770
                                               static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
771
772
773
        }
        else if constexpr(N == 4)
        {
774
775
776
777
            llvm_amdgcn_raw_buffer_store_i16x4(src_thread_data,
                                               dst_wave_buffer_resource,
                                               dst_thread_addr_offset,
                                               dst_wave_addr_offset,
778
                                               static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
779
780
781
        }
        else if constexpr(N == 8)
        {
782
            vector_type<bhalf_t, 8> tmp{src_thread_data};
Chao Liu's avatar
Chao Liu committed
783

784
785
786
787
            llvm_amdgcn_raw_buffer_store_i16x4(tmp.AsType<bhalf4_t>()[Number<0>{}],
                                               dst_wave_buffer_resource,
                                               dst_thread_addr_offset,
                                               dst_wave_addr_offset,
788
                                               static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
789

790
791
792
793
            llvm_amdgcn_raw_buffer_store_i16x4(tmp.AsType<bhalf4_t>()[Number<1>{}],
                                               dst_wave_buffer_resource,
                                               dst_thread_addr_offset,
                                               dst_wave_addr_offset + 4 * sizeof(bhalf_t),
794
                                               static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
795
796
        }
    }
Chao Liu's avatar
Chao Liu committed
797
798
799
800
    else if constexpr(is_same<T, int32_t>::value)
    {
        if constexpr(N == 1)
        {
801
802
803
804
            llvm_amdgcn_raw_buffer_store_i32(src_thread_data,
                                             dst_wave_buffer_resource,
                                             dst_thread_addr_offset,
                                             dst_wave_addr_offset,
805
                                             static_cast<index_t>(coherence));
806
807
808
809
        }
        else if constexpr(N == 2)
        {
            llvm_amdgcn_raw_buffer_store_i32x2(src_thread_data,
Chao Liu's avatar
Chao Liu committed
810
811
812
                                               dst_wave_buffer_resource,
                                               dst_thread_addr_offset,
                                               dst_wave_addr_offset,
813
                                               static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
814
815
816
        }
        else if constexpr(N == 4)
        {
817
818
819
820
            llvm_amdgcn_raw_buffer_store_i32x4(src_thread_data,
                                               dst_wave_buffer_resource,
                                               dst_thread_addr_offset,
                                               dst_wave_addr_offset,
821
                                               static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
822
823
824
825
826
827
        }
    }
    else if constexpr(is_same<T, int8_t>::value)
    {
        if constexpr(N == 1)
        {
828
829
830
831
            llvm_amdgcn_raw_buffer_store_i8(src_thread_data,
                                            dst_wave_buffer_resource,
                                            dst_thread_addr_offset,
                                            dst_wave_addr_offset,
832
                                            static_cast<index_t>(coherence));
Chao Liu's avatar
Chao Liu committed
833
834
835
        }
        else if constexpr(N == 2)
        {
836
#if !CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE
837
838
839
840
            llvm_amdgcn_raw_buffer_store_i8x2(src_thread_data,
                                              dst_wave_buffer_resource,
                                              dst_thread_addr_offset,
                                              dst_wave_addr_offset,
841
                                              static_cast<index_t>(coherence));
842
#else
843
            llvm_amdgcn_raw_buffer_store_i16(bit_cast<int16_t>(src_thread_data),
844
845
846
                                             dst_wave_buffer_resource,
                                             dst_thread_addr_offset,
                                             dst_wave_addr_offset,
847
                                             static_cast<index_t>(coherence));
848
#endif
Chao Liu's avatar
Chao Liu committed
849
850
851
        }
        else if constexpr(N == 4)
        {
852
#if !CK_WORKAROUND_SWDEV_XXXXXX_INT8_BUFFER_LOAD_STORE_ISSUE
853
854
855
856
            llvm_amdgcn_raw_buffer_store_i8x4(src_thread_data,
                                              dst_wave_buffer_resource,
                                              dst_thread_addr_offset,
                                              dst_wave_addr_offset,
857
                                              static_cast<index_t>(coherence));
858
#else
859
            llvm_amdgcn_raw_buffer_store_i32(bit_cast<int32_t>(src_thread_data),
860
861
862
                                             dst_wave_buffer_resource,
                                             dst_thread_addr_offset,
                                             dst_wave_addr_offset,
863
                                             static_cast<index_t>(coherence));
864
#endif
Chao Liu's avatar
Chao Liu committed
865
        }
866
867
        else if constexpr(N == 8)
        {
868
            llvm_amdgcn_raw_buffer_store_i32x2(bit_cast<int32x2_t>(src_thread_data),
869
870
871
                                               dst_wave_buffer_resource,
                                               dst_thread_addr_offset,
                                               dst_wave_addr_offset,
872
                                               static_cast<index_t>(coherence));
873
874
875
        }
        else if constexpr(N == 16)
        {
876
            llvm_amdgcn_raw_buffer_store_i32x4(bit_cast<int32x4_t>(src_thread_data),
877
878
879
                                               dst_wave_buffer_resource,
                                               dst_thread_addr_offset,
                                               dst_wave_addr_offset,
880
                                               static_cast<index_t>(coherence));
881
882
        }
    }
Chao Liu's avatar
Chao Liu committed
883
884
}

zjing14's avatar
zjing14 committed
885
886
887
888
889
890
891
template <typename T, index_t N>
__device__ void amd_buffer_atomic_add_impl(const typename vector_type<T, N>::type src_thread_data,
                                           int32x4_t dst_wave_buffer_resource,
                                           index_t dst_thread_addr_offset,
                                           index_t dst_wave_addr_offset)
{
    static_assert((is_same<T, float>::value && (N == 1 || N == 2 || N == 4)) ||
Chao Liu's avatar
Chao Liu committed
892
                      (is_same<T, half_t>::value && (N == 2 || N == 4 || N == 8)) ||
zjing14's avatar
zjing14 committed
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
                      (is_same<T, int32_t>::value && (N == 1 || N == 2 || N == 4)),
                  "wrong! not implemented");

    if constexpr(is_same<T, float>::value)
    {
        if constexpr(N == 1)
        {
            llvm_amdgcn_raw_buffer_atomic_add_fp32(src_thread_data,
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset,
                                                   0);
        }
        else if constexpr(N == 2)
        {
            vector_type<float, 2> tmp{src_thread_data};

            llvm_amdgcn_raw_buffer_atomic_add_fp32(tmp.AsType<float>()[Number<0>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset,
                                                   0);

            llvm_amdgcn_raw_buffer_atomic_add_fp32(tmp.AsType<float>()[Number<1>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset + sizeof(float),
                                                   0);
        }
        else if constexpr(N == 4)
        {
            vector_type<float, 4> tmp{src_thread_data};

            llvm_amdgcn_raw_buffer_atomic_add_fp32(tmp.AsType<float>()[Number<0>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset,
                                                   0);

            llvm_amdgcn_raw_buffer_atomic_add_fp32(tmp.AsType<float>()[Number<1>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset + sizeof(float),
                                                   0);

            llvm_amdgcn_raw_buffer_atomic_add_fp32(tmp.AsType<float>()[Number<2>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset + 2 * sizeof(float),
                                                   0);

            llvm_amdgcn_raw_buffer_atomic_add_fp32(tmp.AsType<float>()[Number<3>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset + 3 * sizeof(float),
                                                   0);
        }
    }
Chao Liu's avatar
Chao Liu committed
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
    else if constexpr(is_same<T, half_t>::value)
    {
        if constexpr(N == 2)
        {
            llvm_amdgcn_raw_buffer_atomic_add_fp16x2(src_thread_data,
                                                     dst_wave_buffer_resource,
                                                     dst_thread_addr_offset,
                                                     dst_wave_addr_offset,
                                                     0);
        }
        else if constexpr(N == 4)
        {
            vector_type<half_t, 4> tmp{src_thread_data};

            static_for<0, 2, 1>{}([&](auto i) {
                llvm_amdgcn_raw_buffer_atomic_add_fp16x2(tmp.AsType<half2_t>()[i],
                                                         dst_wave_buffer_resource,
                                                         dst_thread_addr_offset,
                                                         dst_wave_addr_offset + i * sizeof(half2_t),
                                                         0);
            });
        }
        else if constexpr(N == 8)
        {
            vector_type<half_t, 8> tmp{src_thread_data};

            static_for<0, 4, 1>{}([&](auto i) {
                llvm_amdgcn_raw_buffer_atomic_add_fp16x2(tmp.AsType<half2_t>()[i],
                                                         dst_wave_buffer_resource,
                                                         dst_thread_addr_offset,
                                                         dst_wave_addr_offset + i * sizeof(half2_t),
                                                         0);
            });
        }
    }
zjing14's avatar
zjing14 committed
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
    else if constexpr(is_same<T, int32_t>::value)
    {
        if constexpr(N == 1)
        {
            llvm_amdgcn_raw_buffer_atomic_add_i32(src_thread_data,
                                                  dst_wave_buffer_resource,
                                                  dst_thread_addr_offset,
                                                  dst_wave_addr_offset,
                                                  0);
        }
        else if constexpr(N == 2)
        {
            vector_type<int32_t, 2> tmp{src_thread_data};

            llvm_amdgcn_raw_buffer_atomic_add_i32(tmp.AsType<int32_t>()[Number<0>{}],
                                                  dst_wave_buffer_resource,
                                                  dst_thread_addr_offset,
                                                  dst_wave_addr_offset,
                                                  0);

            llvm_amdgcn_raw_buffer_atomic_add_i32(tmp.AsType<int32_t>()[Number<1>{}],
                                                  dst_wave_buffer_resource,
                                                  dst_thread_addr_offset,
                                                  dst_wave_addr_offset + sizeof(int32_t),
                                                  0);
        }
        else if constexpr(N == 4)
        {
            vector_type<int32_t, 4> tmp{src_thread_data};

            llvm_amdgcn_raw_buffer_atomic_add_i32(tmp.AsType<int32_t>()[Number<0>{}],
                                                  dst_wave_buffer_resource,
                                                  dst_thread_addr_offset,
                                                  dst_wave_addr_offset,
                                                  0);

            llvm_amdgcn_raw_buffer_atomic_add_i32(tmp.AsType<int32_t>()[Number<1>{}],
                                                  dst_wave_buffer_resource,
                                                  dst_thread_addr_offset,
                                                  dst_wave_addr_offset + sizeof(int32_t),
                                                  0);

            llvm_amdgcn_raw_buffer_atomic_add_i32(tmp.AsType<int32_t>()[Number<2>{}],
                                                  dst_wave_buffer_resource,
                                                  dst_thread_addr_offset,
                                                  dst_wave_addr_offset + 2 * sizeof(int32_t),
                                                  0);

            llvm_amdgcn_raw_buffer_atomic_add_i32(tmp.AsType<int32_t>()[Number<3>{}],
                                                  dst_wave_buffer_resource,
                                                  dst_thread_addr_offset,
                                                  dst_wave_addr_offset + 3 * sizeof(int32_t),
                                                  0);
        }
    }
}

rocking5566's avatar
rocking5566 committed
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
template <typename T, index_t N>
__device__ void amd_buffer_atomic_max_impl(const typename vector_type<T, N>::type src_thread_data,
                                           int32x4_t dst_wave_buffer_resource,
                                           index_t dst_thread_addr_offset,
                                           index_t dst_wave_addr_offset)
{
    static_assert((is_same<T, double>::value && (N == 1 || N == 2 || N == 4)),
                  "wrong! not implemented");
    if constexpr(is_same<T, double>::value)
    {
        if constexpr(N == 1)
        {
            llvm_amdgcn_raw_buffer_atomic_max_fp64(src_thread_data,
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset,
                                                   0);
        }
        else if constexpr(N == 2)
        {
            vector_type<double, 2> tmp{src_thread_data};

            llvm_amdgcn_raw_buffer_atomic_max_fp64(tmp.AsType<double>()[Number<0>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset,
                                                   0);

            llvm_amdgcn_raw_buffer_atomic_max_fp64(tmp.AsType<double>()[Number<1>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset + sizeof(double),
                                                   0);
        }
        else if constexpr(N == 4)
        {
            vector_type<double, 4> tmp{src_thread_data};

            llvm_amdgcn_raw_buffer_atomic_max_fp64(tmp.AsType<double>()[Number<0>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset,
                                                   0);

            llvm_amdgcn_raw_buffer_atomic_max_fp64(tmp.AsType<double>()[Number<1>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset + sizeof(double),
                                                   0);

            llvm_amdgcn_raw_buffer_atomic_max_fp64(tmp.AsType<double>()[Number<2>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset + 2 * sizeof(double),
                                                   0);

            llvm_amdgcn_raw_buffer_atomic_max_fp64(tmp.AsType<double>()[Number<3>{}],
                                                   dst_wave_buffer_resource,
                                                   dst_thread_addr_offset,
                                                   dst_wave_addr_offset + 3 * sizeof(double),
                                                   0);
        }
    }
}

Chao Liu's avatar
Chao Liu committed
1108
// buffer_load requires:
Chao Liu's avatar
Chao Liu committed
1109
//   1) p_src_wave must point to global memory space
1110
//   2) p_src_wave must be a wavewise pointer.
Chao Liu's avatar
Chao Liu committed
1111
// It is user's responsibility to make sure that is true.
1112
1113
1114
template <typename T,
          index_t N,
          AmdBufferCoherenceEnum coherence = AmdBufferCoherenceEnum::DefaultCoherence>
1115
__device__ typename vector_type_maker<T, N>::type::type
Jianfeng Yan's avatar
Jianfeng Yan committed
1116
1117
1118
1119
amd_buffer_load_invalid_element_return_zero(const T* p_src_wave,
                                            index_t src_thread_element_offset,
                                            bool src_thread_element_valid,
                                            index_t src_element_space_size)
Chao Liu's avatar
Chao Liu committed
1120
1121
{
    const int32x4_t src_wave_buffer_resource =
1122
        make_wave_buffer_resource(p_src_wave, src_element_space_size);
Chao Liu's avatar
Chao Liu committed
1123

1124
1125
1126
1127
    index_t src_thread_addr_offset = src_thread_element_offset * sizeof(T);

    using vector_t = typename vector_type_maker<T, N>::type::type;
    using scalar_t = typename scalar_type<vector_t>::type;
Chao Liu's avatar
Chao Liu committed
1128

1129
1130
    constexpr index_t vector_size = scalar_type<vector_t>::vector_size;

Chao Liu's avatar
Chao Liu committed
1131
#if CK_EXPERIMENTAL_USE_BUFFER_LOAD_OOB_CHECK_OFFSET_TRICK
1132
    uint32_t src_addr_shift = src_thread_element_valid ? 0 : 0x80000000;
Chao Liu's avatar
Chao Liu committed
1133

1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
    if constexpr(is_same<scalar_t, f8_t>::value)
    {
        auto tmp = amd_buffer_load_impl<int8_t, vector_size, coherence>(
            src_wave_buffer_resource, src_addr_shift + src_thread_addr_offset, 0);
        return bit_cast<vector_t>(tmp);
    }
    else
    {
        return amd_buffer_load_impl<scalar_t, vector_size, coherence>(
            src_wave_buffer_resource, src_addr_shift + src_thread_addr_offset, 0);
    }
Chao Liu's avatar
Chao Liu committed
1145
#else
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
    if constexpr(is_same<scalar_t, f8_t>::value)
    {
        auto tmp = amd_buffer_load_impl<int8_t, vector_size, coherence>(
            src_wave_buffer_resource, src_thread_addr_offset, 0);
        return src_thread_element_valid ? bit_cast<vector_t>(tmp) : vector_t(0);
    }
    else
    {
        vector_t tmp = amd_buffer_load_impl<scalar_t, vector_size, coherence>(
            src_wave_buffer_resource, src_thread_addr_offset, 0);
        return src_thread_element_valid ? tmp : vector_t(0);
    }
Chao Liu's avatar
Chao Liu committed
1158
1159
1160
#endif
}

1161
// buffer_load requires:
Chao Liu's avatar
Chao Liu committed
1162
//   1) p_src_wave must point to global memory space
1163
1164
//   2) p_src_wave must be a wavewise pointer.
// It is user's responsibility to make sure that is true.
1165
1166
1167
template <typename T,
          index_t N,
          AmdBufferCoherenceEnum coherence = AmdBufferCoherenceEnum::DefaultCoherence>
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
__device__ typename vector_type_maker<T, N>::type::type
amd_buffer_load_invalid_element_return_customized_value(const T* p_src_wave,
                                                        index_t src_thread_element_offset,
                                                        bool src_thread_element_valid,
                                                        index_t src_element_space_size,
                                                        T customized_value)
{
    const int32x4_t src_wave_buffer_resource =
        make_wave_buffer_resource(p_src_wave, src_element_space_size);

    index_t src_thread_addr_offset = src_thread_element_offset * sizeof(T);

    using vector_t = typename vector_type_maker<T, N>::type::type;
    using scalar_t = typename scalar_type<vector_t>::type;

    constexpr index_t vector_size = scalar_type<vector_t>::vector_size;

1185
    vector_t tmp = amd_buffer_load_impl<scalar_t, vector_size, coherence>(
1186
1187
1188
1189
1190
        src_wave_buffer_resource, src_thread_addr_offset, 0);

    return src_thread_element_valid ? tmp : vector_t(customized_value);
}

Chao Liu's avatar
Chao Liu committed
1191
// buffer_store requires:
Chao Liu's avatar
Chao Liu committed
1192
//   1) p_dst_wave must point to global memory
zjing14's avatar
zjing14 committed
1193
//   2) p_dst_wave must be a wavewise pointer.
Chao Liu's avatar
Chao Liu committed
1194
// It is user's responsibility to make sure that is true.
1195
1196
1197
template <typename T,
          index_t N,
          AmdBufferCoherenceEnum coherence = AmdBufferCoherenceEnum::DefaultCoherence>
1198
1199
1200
1201
1202
__device__ void amd_buffer_store(const typename vector_type_maker<T, N>::type::type src_thread_data,
                                 T* p_dst_wave,
                                 const index_t dst_thread_element_offset,
                                 const bool dst_thread_element_valid,
                                 const index_t dst_element_space_size)
Chao Liu's avatar
Chao Liu committed
1203
1204
{
    const int32x4_t dst_wave_buffer_resource =
1205
        make_wave_buffer_resource(p_dst_wave, dst_element_space_size);
Chao Liu's avatar
Chao Liu committed
1206

1207
    index_t dst_thread_addr_offset = dst_thread_element_offset * sizeof(T);
Chao Liu's avatar
Chao Liu committed
1208

1209
1210
1211
1212
    using vector_t                = typename vector_type_maker<T, N>::type::type;
    using scalar_t                = typename scalar_type<vector_t>::type;
    constexpr index_t vector_size = scalar_type<vector_t>::vector_size;

Chao Liu's avatar
Chao Liu committed
1213
#if CK_EXPERIMENTAL_USE_BUFFER_STORE_OOB_CHECK_OFFSET_TRICK
1214
    uint32_t dst_addr_shift = dst_thread_element_valid ? 0 : 0x80000000;
Chao Liu's avatar
Chao Liu committed
1215

1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
    if constexpr(is_same<scalar_t, f8_t>::value)
    {
        auto tmp =
            bit_cast<typename vector_type_maker<int8_t, vector_size>::type::type>(src_thread_data);
        amd_buffer_store_impl<int8_t, vector_size, coherence>(
            tmp, dst_wave_buffer_resource, dst_addr_shift + dst_thread_addr_offset, 0);
    }
    else
    {
        amd_buffer_store_impl<scalar_t, vector_size, coherence>(
            src_thread_data, dst_wave_buffer_resource, dst_addr_shift + dst_thread_addr_offset, 0);
    }
Chao Liu's avatar
Chao Liu committed
1228
#else
1229
    if(dst_thread_element_valid)
Chao Liu's avatar
Chao Liu committed
1230
    {
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
        if constexpr(is_same<scalar_t, f8_t>::value)
        {
            auto tmp = bit_cast<typename vector_type_maker<int8_t, vector_size>::type::type>(
                src_thread_data);
            amd_buffer_store_impl<int8_t, vector_size, coherence>(
                tmp, dst_wave_buffer_resource, dst_thread_addr_offset, 0);
        }
        else
        {
            amd_buffer_store_impl<scalar_t, vector_size, coherence>(
                src_thread_data, dst_wave_buffer_resource, dst_thread_addr_offset, 0);
        }
Chao Liu's avatar
Chao Liu committed
1243
1244
1245
1246
    }
#endif
}

zjing14's avatar
zjing14 committed
1247
// buffer_atomic_add requires:
Chao Liu's avatar
Chao Liu committed
1248
//   1) p_dst_wave must point to global memory
zjing14's avatar
zjing14 committed
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
//   2) p_dst_wave must be a wavewise pointer.
// It is user's responsibility to make sure that is true.
template <typename T, index_t N>
__device__ void
amd_buffer_atomic_add(const typename vector_type_maker<T, N>::type::type src_thread_data,
                      T* p_dst_wave,
                      const index_t dst_thread_element_offset,
                      const bool dst_thread_element_valid,
                      const index_t dst_element_space_size)
{
    const int32x4_t dst_wave_buffer_resource =
        make_wave_buffer_resource(p_dst_wave, dst_element_space_size);

    index_t dst_thread_addr_offset = dst_thread_element_offset * sizeof(T);

    using vector_t                = typename vector_type_maker<T, N>::type::type;
    using scalar_t                = typename scalar_type<vector_t>::type;
    constexpr index_t vector_size = scalar_type<vector_t>::vector_size;

#if CK_EXPERIMENTAL_USE_BUFFER_ATOMIC_ADD_OOB_CHECK_OFFSET_TRICK
1269
    uint32_t dst_addr_shift = dst_thread_element_valid ? 0 : 0x80000000;
zjing14's avatar
zjing14 committed
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281

    amd_buffer_atomic_add_impl<scalar_t, vector_size>(
        src_thread_data, dst_wave_buffer_resource, dst_addr_shift + dst_thread_addr_offset, 0);
#else
    if(dst_thread_element_valid)
    {
        amd_buffer_atomic_add_impl<scalar_t, vector_size>(
            src_thread_data, dst_wave_buffer_resource, dst_thread_addr_offset, 0);
    }
#endif
}

rocking5566's avatar
rocking5566 committed
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
// buffer_atomic_max requires:
//   1) p_dst_wave must point to global memory
//   2) p_dst_wave must be a wavewise pointer.
// It is user's responsibility to make sure that is true.
template <typename T, index_t N>
__device__ void
amd_buffer_atomic_max(const typename vector_type_maker<T, N>::type::type src_thread_data,
                      T* p_dst_wave,
                      const index_t dst_thread_element_offset,
                      const bool dst_thread_element_valid,
                      const index_t dst_element_space_size)
{
    const int32x4_t dst_wave_buffer_resource =
        make_wave_buffer_resource(p_dst_wave, dst_element_space_size);

    index_t dst_thread_addr_offset = dst_thread_element_offset * sizeof(T);

    using vector_t                = typename vector_type_maker<T, N>::type::type;
    using scalar_t                = typename scalar_type<vector_t>::type;
    constexpr index_t vector_size = scalar_type<vector_t>::vector_size;

#if CK_EXPERIMENTAL_USE_BUFFER_ATOMIC_MAX_OOB_CHECK_OFFSET_TRICK
1304
    uint32_t dst_addr_shift = dst_thread_element_valid ? 0 : 0x80000000;
rocking5566's avatar
rocking5566 committed
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316

    amd_buffer_atomic_max_impl<scalar_t, vector_size>(
        src_thread_data, dst_wave_buffer_resource, dst_addr_shift + dst_thread_addr_offset, 0);
#else
    if(dst_thread_element_valid)
    {
        amd_buffer_atomic_max_impl<scalar_t, vector_size>(
            src_thread_data, dst_wave_buffer_resource, dst_thread_addr_offset, 0);
    }
#endif
}

Chao Liu's avatar
Chao Liu committed
1317
} // namespace ck
Umang Yadav's avatar
Umang Yadav committed
1318
1319

#pragma clang diagnostic pop