amd_inline_asm.hip.hpp 11.9 KB
Newer Older
Jing Zhang's avatar
Jing Zhang committed
1
#pragma once
Chao Liu's avatar
Chao Liu committed
2
#include "common.hip.hpp"
Jing Zhang's avatar
Jing Zhang committed
3

Jing Zhang's avatar
Jing Zhang committed
4
5
6
7
8
9
#define NO_VM_WAIT 0
#define NO_LGKM_WAIT 0
#define NO_DS_READ 0
#define NO_DS_WRITE 0
#define NO_GLB_READ 0

Chao Liu's avatar
Chao Liu committed
10
11
12
13
// cast a pointer of LDS to its address
extern "C" __attribute__((address_space(3))) void* __to_local(void* p)[[hc]];

__device__ void vmcnt(index_t cnt)
14
{
Jing Zhang's avatar
Jing Zhang committed
15
#if !NO_VM_WAIT
16
17
18
    if(cnt == 0)
    {
        asm volatile("\n \
Jing Zhang's avatar
Jing Zhang committed
19
                s_waitcnt vmcnt(0) \n \
20
                " ::);
Jing Zhang's avatar
Jing Zhang committed
21
    }
22
23
24
    else if(cnt == 1)
    {
        asm volatile("\n \
Jing Zhang's avatar
Jing Zhang committed
25
                s_waitcnt vmcnt(1) \n \
26
                " ::);
Jing Zhang's avatar
Jing Zhang committed
27
    }
28
29
30
    else if(cnt == 2)
    {
        asm volatile("\n \
Jing Zhang's avatar
Jing Zhang committed
31
                s_waitcnt vmcnt(2) \n \
32
                " ::);
Jing Zhang's avatar
Jing Zhang committed
33
    }
34
35
36
    else if(cnt == 4)
    {
        asm volatile("\n \
Jing Zhang's avatar
Jing Zhang committed
37
                s_waitcnt vmcnt(2) \n \
38
                " ::);
Jing Zhang's avatar
Jing Zhang committed
39
    }
40
41
    else
    {
42
        assert(false);
Jing Zhang's avatar
Jing Zhang committed
43
    }
Jing Zhang's avatar
Jing Zhang committed
44
#endif
Jing Zhang's avatar
Jing Zhang committed
45
46
}

Chao Liu's avatar
Chao Liu committed
47
__device__ void lgkmcnt(index_t cnt)
Chao Liu's avatar
Chao Liu committed
48
{
Jing Zhang's avatar
Jing Zhang committed
49
#if 0 
Chao Liu's avatar
Chao Liu committed
50
51
    if(cnt == 0)
    {
Jing Zhang's avatar
Jing Zhang committed
52
53
        asm volatile("\n \
                s_waitcnt lgkmcnt(0) \n \
Chao Liu's avatar
Chao Liu committed
54
                " ::);
Jing Zhang's avatar
Jing Zhang committed
55
    }
Chao Liu's avatar
Chao Liu committed
56
57
    else if(cnt == 1)
    {
Jing Zhang's avatar
Jing Zhang committed
58
59
        asm volatile("\n \
                s_waitcnt lgkmcnt(1) \n \
Chao Liu's avatar
Chao Liu committed
60
                " ::);
Jing Zhang's avatar
Jing Zhang committed
61
    }
Chao Liu's avatar
Chao Liu committed
62
63
    else if(cnt == 2)
    {
Jing Zhang's avatar
Jing Zhang committed
64
65
        asm volatile("\n \
                s_waitcnt lgkmcnt(2) \n \
Chao Liu's avatar
Chao Liu committed
66
                " ::);
Jing Zhang's avatar
Jing Zhang committed
67
    }
Chao Liu's avatar
Chao Liu committed
68
69
    else if(cnt == 3)
    {
Jing Zhang's avatar
Jing Zhang committed
70
71
        asm volatile("\n \
                s_waitcnt lgkmcnt(3) \n \
Chao Liu's avatar
Chao Liu committed
72
                " ::);
Jing Zhang's avatar
Jing Zhang committed
73
    }
Chao Liu's avatar
Chao Liu committed
74
75
    else if(cnt == 4)
    {
Jing Zhang's avatar
Jing Zhang committed
76
77
        asm volatile("\n \
                s_waitcnt lgkmcnt(4) \n \
Chao Liu's avatar
Chao Liu committed
78
                " ::);
Jing Zhang's avatar
Jing Zhang committed
79
    }
Chao Liu's avatar
Chao Liu committed
80
81
    else
    {
82
        assert(false);
Jing Zhang's avatar
Jing Zhang committed
83
    }
Jing Zhang's avatar
Jing Zhang committed
84
#endif
Jing Zhang's avatar
Jing Zhang committed
85
86
}

Chao Liu's avatar
Chao Liu committed
87
__device__ void outerProduct1x4(const float* a, const float* b, float* c)
Chao Liu's avatar
Chao Liu committed
88
{
Jing Zhang's avatar
Jing Zhang committed
89
#if 0
Jing Zhang's avatar
Jing Zhang committed
90
91
92
93
94
95
    asm volatile("\n \
            v_mac_f32 %0, %4, %5 \n \
            v_mac_f32 %1, %4, %6 \n \
            v_mac_f32 %2, %4, %7 \n \
            v_mac_f32 %3, %4, %8 \n \
            "
Chao Liu's avatar
Chao Liu committed
96
97
98
99
100
101
102
103
104
105
                 : "=v"(c[0]), "=v"(c[1]), "=v"(c[2]), "=v"(c[3])
                 : "v"(a[0]),
                   "v"(b[0]),
                   "v"(b[1]),
                   "v"(b[2]),
                   "v"(b[3]),
                   "0"(c[0]),
                   "1"(c[1]),
                   "2"(c[2]),
                   "3"(c[3]));
Jing Zhang's avatar
Jing Zhang committed
106
107
108
109
110
111
#else
    c[0] += a[0] * b[0];
    c[1] += a[0] * b[1];
    c[2] += a[0] * b[2];
    c[3] += a[0] * b[3];
#endif
Jing Zhang's avatar
Jing Zhang committed
112
113
}

Chao Liu's avatar
Chao Liu committed
114
115
116
__device__ void outerProduct1x4(const float& a,
                                const vector_type<float, 4>::MemoryType& b,
                                vector_type<float, 4>::MemoryType& c)
Chao Liu's avatar
Chao Liu committed
117
{
Jing Zhang's avatar
Jing Zhang committed
118
119
120
121
122
123
124
125
126
127
128
129
130
#if 0
    asm volatile(
            "\n \
            v_mac_f32 %0, %4, %5 \n \
            v_mac_f32 %1, %4, %6 \n \
            v_mac_f32 %2, %4, %7 \n \
            v_mac_f32 %3, %4, %8 \n \
            "
            :
            :"v"(c.x),"v"(c.y),"v"(c.z),"v"(c.w), \
            "v"(a.x),"v"(b.x),"v"(b.y),"v"(b.z),"v"(b.w)
            );
#else
Chao Liu's avatar
Chao Liu committed
131
    outerProduct1x4(&a, (float*)&b, (float*)&c);
Jing Zhang's avatar
Jing Zhang committed
132
133
134
#endif
}

Chao Liu's avatar
Chao Liu committed
135
136
137
138
139
140
__device__ void outerProduct4x4(const vector_type<float, 4>::MemoryType& a,
                                const vector_type<float, 4>::MemoryType& b,
                                vector_type<float, 4>::MemoryType& c0,
                                vector_type<float, 4>::MemoryType& c1,
                                vector_type<float, 4>::MemoryType& c2,
                                vector_type<float, 4>::MemoryType& c3)
Chao Liu's avatar
Chao Liu committed
141
{
Jing Zhang's avatar
Jing Zhang committed
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
#if 0
    asm volatile(
            "\n \
            v_mac_f32 %0, %4, %5 \n \
            v_mac_f32 %1, %4, %6 \n \
            v_mac_f32 %2, %4, %7 \n \
            v_mac_f32 %3, %4, %8 \n \
            "
            :
            :"v"(c0.x),"v"(c0.y),"v"(c0.z),"v"(c0.w), \
            "v"(a.x),"v"(b.x),"v"(b.y),"v"(b.z),"v"(b.w)
            );
    asm volatile(
            "\n \
            v_mac_f32 %0, %4, %5 \n \
            v_mac_f32 %1, %4, %6 \n \
            v_mac_f32 %2, %4, %7 \n \
            v_mac_f32 %3, %4, %8 \n \
            "
            :
            :"v"(c1.x),"v"(c1.y),"v"(c1.z),"v"(c1.w), \
            "v"(a.y),"v"(b.x),"v"(b.y),"v"(b.z),"v"(b.w)
            );
    asm volatile(
            "\n \
            v_mac_f32 %0, %4, %5 \n \
            v_mac_f32 %1, %4, %6 \n \
            v_mac_f32 %2, %4, %7 \n \
            v_mac_f32 %3, %4, %8 \n \
            "
            :
            :"v"(c2.x),"v"(c2.y),"v"(c2.z),"v"(c2.w), \
            "v"(a.z),"v"(b.x),"v"(b.y),"v"(b.z),"v"(b.w)
            );
    asm volatile(
            "\n \
            v_mac_f32 %0, %4, %5 \n \
            v_mac_f32 %1, %4, %6 \n \
            v_mac_f32 %2, %4, %7 \n \
            v_mac_f32 %3, %4, %8 \n \
            "
            :
            :"v"(c3.x),"v"(c3.y),"v"(c3.z),"v"(c3.w), \
            "v"(a.w),"v"(b.x),"v"(b.y),"v"(b.z),"v"(b.w)
            );
#else
    outerProduct1x4(a.x, b, c0);
    outerProduct1x4(a.y, b, c1);
    outerProduct1x4(a.z, b, c2);
    outerProduct1x4(a.w, b, c3);
#endif
}

Chao Liu's avatar
Chao Liu committed
195
196
197
__device__ void outerProduct8x8(const vector_type<float, 4>::MemoryType* a,
                                const vector_type<float, 4>::MemoryType* b,
                                vector_type<float, 4>::MemoryType* c)
Jing Zhang's avatar
Jing Zhang committed
198
199
200
201
202
203
204
{
    outerProduct4x4(a[0], b[0], c[0], c[2], c[4], c[6]);
    outerProduct4x4(a[0], b[1], c[1], c[3], c[5], c[7]);
    outerProduct4x4(a[1], b[0], c[8], c[10], c[12], c[14]);
    outerProduct4x4(a[1], b[1], c[9], c[11], c[13], c[15]);
}

Chao Liu's avatar
Chao Liu committed
205
__device__ void ds_read_b128(vector_type<float, 4>::MemoryType& r, void* lds, index_t offset = 0)
Jing Zhang's avatar
Jing Zhang committed
206
{
Jing Zhang's avatar
Jing Zhang committed
207
#if 0 
Jing Zhang's avatar
Jing Zhang committed
208
209
210
211
212
    if(offset == 0)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:0 \n \
                "
Chao Liu's avatar
Chao Liu committed
213
214
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
215
216
217
218
219
220
    }
    else if(offset == 128)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:128 \n \
                "
Chao Liu's avatar
Chao Liu committed
221
222
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
223
224
225
226
227
228
    }
    else if(offset == 256)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:256 \n \
                "
Chao Liu's avatar
Chao Liu committed
229
230
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
231
232
233
234
235
236
    }
    else if(offset == 384)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:384 \n \
                "
Chao Liu's avatar
Chao Liu committed
237
238
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
239
240
241
242
243
244
    }
    else if(offset == 512)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:512 \n \
                "
Chao Liu's avatar
Chao Liu committed
245
246
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
247
248
249
250
251
252
    }
    else if(offset == 640)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:640 \n \
                "
Chao Liu's avatar
Chao Liu committed
253
254
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
255
256
257
258
259
260
    }
    else if(offset == 768)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:768 \n \
                "
Chao Liu's avatar
Chao Liu committed
261
262
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
263
264
265
266
267
268
    }
    else if(offset == 896)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:896 \n \
                "
Chao Liu's avatar
Chao Liu committed
269
270
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
271
272
273
274
275
276
    }
    else if(offset == 1024)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:1024 \n \
                "
Chao Liu's avatar
Chao Liu committed
277
278
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
279
280
281
282
283
284
    }
    else if(offset == 1152)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:1152 \n \
                "
Chao Liu's avatar
Chao Liu committed
285
286
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
287
288
289
290
291
292
    }
    else if(offset == 1280)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:1280 \n \
                "
Chao Liu's avatar
Chao Liu committed
293
294
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
295
296
297
298
299
300
    }
    else if(offset == 1408)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:1408 \n \
                "
Chao Liu's avatar
Chao Liu committed
301
302
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
303
304
305
306
307
308
    }
    else if(offset == 1536)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:1536 \n \
                "
Chao Liu's avatar
Chao Liu committed
309
310
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
311
312
313
314
315
316
    }
    else if(offset == 1664)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:1664 \n \
                "
Chao Liu's avatar
Chao Liu committed
317
318
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
319
320
321
322
323
324
    }
    else if(offset == 1792)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:1792 \n \
                "
Chao Liu's avatar
Chao Liu committed
325
326
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
327
328
329
330
331
332
    }
    else if(offset == 1920)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:1920 \n \
                "
Chao Liu's avatar
Chao Liu committed
333
334
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
335
336
337
338
339
340
    }
    else if(offset == 2048)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:2048 \n \
                "
Chao Liu's avatar
Chao Liu committed
341
342
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
343
344
345
346
347
348
    }
    else if(offset == 2176)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:2176 \n \
                "
Chao Liu's avatar
Chao Liu committed
349
350
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
351
352
353
354
355
356
    }
    else if(offset == 2304)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:2304 \n \
                "
Chao Liu's avatar
Chao Liu committed
357
358
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
359
360
361
362
363
364
    }
    else if(offset == 2560)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:2560 \n \
                "
Chao Liu's avatar
Chao Liu committed
365
366
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
367
368
369
370
371
372
    }
    else if(offset == 2816)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:2816 \n \
                "
Chao Liu's avatar
Chao Liu committed
373
374
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
375
376
377
378
379
380
    }
    else if(offset == 3072)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:3072 \n \
                "
Chao Liu's avatar
Chao Liu committed
381
382
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
383
384
385
386
387
388
    }
    else if(offset == 3328)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:3328 \n \
                "
Chao Liu's avatar
Chao Liu committed
389
390
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
391
392
393
394
395
396
    }
    else if(offset == 3584)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:3584 \n \
                "
Chao Liu's avatar
Chao Liu committed
397
398
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
399
400
401
402
403
404
    }
    else if(offset == 3840)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:3840 \n \
                "
Chao Liu's avatar
Chao Liu committed
405
406
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
407
408
409
410
411
412
    }
    else if(offset == 4096)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:4096 \n \
                "
Chao Liu's avatar
Chao Liu committed
413
414
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
415
416
417
418
419
420
    }
    else if(offset == 4352)
    {
        asm volatile("\n \
                ds_read_b128 %0, %1 offset:4352 \n \
                "
Chao Liu's avatar
Chao Liu committed
421
422
                     : "=v"(r)
                     : "v"(__to_local(lds)));
Jing Zhang's avatar
Jing Zhang committed
423
424
425
    }
    else
    {
426
        assert(false);
Jing Zhang's avatar
Jing Zhang committed
427
    }
Jing Zhang's avatar
Jing Zhang committed
428
429
430
#else
    using Float4 = vector_type<float, 4>::MemoryType;
    r = ((Float4*)lds)[offset]; 
Jing Zhang's avatar
Jing Zhang committed
431
#endif
Jing Zhang's avatar
Jing Zhang committed
432
}
Jing Zhang's avatar
Jing Zhang committed
433

Chao Liu's avatar
Chao Liu committed
434
435
436
__device__ void global_load(vector_type<float, 4>::MemoryType& r,
                            const vector_type<float, 4>::MemoryType* ptr,
                            index_t offset = 0)
437
{
Jing Zhang's avatar
Jing Zhang committed
438
#if !NO_GLB_READ
439
440
441
442
443
444
445
446
447
448
449
450
    if(offset == 0)
    {
        asm volatile("\n \
                global_load_dwordx4 %0, %1, off \n \
                "
                     : "=v"(r)
                     : "v"(ptr));
    }
    else
    {
        assert(false);
    }
Jing Zhang's avatar
Jing Zhang committed
451
#endif
Jing Zhang's avatar
Jing Zhang committed
452
453
}

Chao Liu's avatar
Chao Liu committed
454
455
__device__ void
ds_write_b128(const vector_type<float, 4>::MemoryType& r, void* lds, index_t offset = 0)
Jing Zhang's avatar
Jing Zhang committed
456
{
Jing Zhang's avatar
Jing Zhang committed
457
#if !NO_DS_WRITE
458
459
460
    if(offset == 0)
    {
        asm volatile("\n \
Jing Zhang's avatar
Jing Zhang committed
461
462
            ds_write_b128 %0, %1 \n \
            "
463
464
465
466
467
468
469
                     :
                     : "v"(__to_local(lds)), "v"(r));
    }
    else
    {
        assert(false);
    }
Jing Zhang's avatar
Jing Zhang committed
470
#endif
Jing Zhang's avatar
Jing Zhang committed
471
}