Debian Codegraph @

tensilelite/Tensile/Source/memory_gfx.h

raw symbols SCIP
1 /******************************************************************************
2 * Copyright (C) 2021-2025 Advanced Micro Devices, Inc. All rights reserved.
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a copy
5 * of this software and associated documentation files (the "Software"), to deal
6 * in the Software without restriction, including without limitation the rights
7 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 * copies of the Software, and to permit persons to whom the Software is
9 * furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 * THE SOFTWARE.
21 *****************************************************************************/
22 /*! \file
23 \brief Architecture-specific operators on memory added for GFX9
24 */
25 // reference:
26 // https://github.com/llvm/llvm-project/blob/main/llvm/test/CodeGen/AMDGPU/llvm.amdgcn.raw.buffer.load.ll
27
28 #ifndef INTRINSIC_MEM_ACCESS_H
29 #define INTRINSIC_MEM_ACCESS_H
30
31 #if defined(__clang__) && defined(__HIP__)
32
33 #if(defined(__NVCC__) || defined(__HIPCC__)) \
34 || (defined(__clang__) && (defined(__CUDA__)) || defined(__HIP__))
35 #define INLINEDEVICE __forceinline__ __device__
36 #elif defined(__CUDACC_RTC__)
37 #define INLINEDEVICE __forceinline__ __device__
38 #else
39 #define INLINEDEVICE inline
40 #endif
41
42 #if defined(__gfx803__) || defined(__gfx900__) || defined(__gfx906__) || defined(__gfx908__) \
43 || defined(__gfx90a__) || defined(__gfx942__) || defined(__gfx950__) // test device
44 #define USE_GFX_BUFFER_INTRINSIC
45 #define BUFFER_RESOURCE_3RD_DWORD 0x00020000
46 #elif defined(__gfx1030__) // special device
47 #define USE_GFX_BUFFER_INTRINSIC
48 #define BUFFER_RESOURCE_3RD_DWORD 0x31014000
49 #elif defined(__gfx1100__) || defined(__gfx1101__) || defined(__gfx1102__) || defined(__gfx1103__) \
50 || defined(__gfx1150__) || defined(__gfx1151__)
51 #define USE_GFX_BUFFER_INTRINSIC
52 #define BUFFER_RESOURCE_3RD_DWORD 0x31004000
53 #elif defined(__gfx1200__) || defined(__gfx1201__)
54 #define USE_GFX_BUFFER_INTRINSIC
55 #define BUFFER_RESOURCE_3RD_DWORD 0x30020000
56 #else // not support
57 #define BUFFER_RESOURCE_3RD_DWORD -1
58 #endif
59
60 /// Controls AMD gfx arch cache operations
61 struct CacheOperation
62 {
63 enum Kind
64 {
65 /// Cache at all levels - accessed again
66 Always,
67 /// Cache at global level; glc = 1
68 Global,
69 /// Streaming - likely to be accessed once; slc = 1
70 Streaming,
71 /// Indicates the line will not be used again, glc = 1; slc = 1
72 LastUse
73 };
74 };
75
76 using float16_t = _Float16;
77 using float32_t = float;
78
79 template <typename T, int N>
80 struct NativeVector
81 {
82 using type = T __attribute__((ext_vector_type(N)));
83 };
84
85 using float32x2_t = NativeVector<float, 2>::type;
86 using float32x4_t = NativeVector<float, 4>::type;
87
88 using int32x4_t = NativeVector<int, 4>::type;
89 using int32x2_t = NativeVector<int, 2>::type;
90
91 ////////////////////////////////////////////////////////////////////////////////////////////////////
92
93 struct alignas(16) BufferResource
94 {
95 union Desc
96 {
97 int32x4_t d128;
98 void* d64[2];
99 uint32_t d32[4];
100 };
101
102 INLINEDEVICE
103 BufferResource(void const* base_addr, uint32_t num_records = (0xFFFFFFFF - 1))
104 {
105 // Reference:
106 // For CDNA: see section 9.1.8 in the AMD resources
107 // https://developer.amd.com/wp-content/resources/CDNA1_Shader_ISA_14December2020.pdf
108 // For RDNA: see section 8.1.8 in the AMD resources
109 // https://developer.amd.com/wp-content/resources/RDNA2_Shader_ISA_November2020.pdf
110 // The d32[3] field represents the 0x[127] ~ [96]
111
112 // 64-bit base address
113 desc_.d64[0] = const_cast<void*>(base_addr);
114 // 32-bit number of records in bytes which is used to guard against out-of-range access
115 desc_.d32[2] = num_records;
116 // 32-bit buffer resource descriptor
117 desc_.d32[3] = BUFFER_RESOURCE_3RD_DWORD;
118 }
119
120 INLINEDEVICE
121 operator int32x4_t()
122 {
123 // return desc_.d128; // NOTE HIP: Crashes compiler; see below
124
125 /// This hack is to enforce scalarization of the variable "base_addr", where in some
126 /// circumstances it becomes vectorized and then in turn causes illegal lowering to GCN ISA
127 /// since compiler effectively tries to stuff VGPRs in slots where it only accepts SGPRs
128 Desc ret;
129 ret.d32[0] = __builtin_amdgcn_readfirstlane(desc_.d32[0]);
130 ret.d32[1] = __builtin_amdgcn_readfirstlane(desc_.d32[1]);
131 ret.d64[1] = desc_.d64[1];
132 return ret.d128;
133 ///
134 }
135
136 Desc desc_;
137 };
138
139 ////////////////////////////////////////////////////////////////////////////////////////////////////
140
141 ///
142 /// Load
143 ///
144
145 // 1 byte
146 __device__ char
147 llvm_amdgcn_raw_buffer_load_i8(int32x4_t buffer_resource,
148 uint32_t voffset,
149 uint32_t soffset,
150 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.load.i8");
151
152 // 2 bytes
153 __device__ float16_t
154 llvm_amdgcn_raw_buffer_load_f16(int32x4_t buffer_resource,
155 uint32_t voffset,
156 uint32_t soffset,
157 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.load.f16");
158
159 // 4 bytes
160 __device__ float32_t
161 llvm_amdgcn_raw_buffer_load_f32(int32x4_t buffer_resource,
162 uint32_t voffset,
163 uint32_t soffset,
164 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.load.f32");
165
166 // 8 bytes
167 __device__ float32x2_t
168 llvm_amdgcn_raw_buffer_load_f32x2(int32x4_t buffer_resource,
169 uint32_t voffset,
170 uint32_t soffset,
171 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.load.v2f32");
172
173 // 16 bytes
174 __device__ float32x4_t
175 llvm_amdgcn_raw_buffer_load_f32x4(int32x4_t buffer_resource,
176 uint32_t voffset,
177 uint32_t soffset,
178 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.load.v4f32");
179
180 // 4 bytes
181 __device__ int32_t
182 llvm_amdgcn_s_buffer_load_i32(int32x4_t buffer_resource,
183 uint32_t soffset,
184 int32_t cache_op) __asm("llvm.amdgcn.s.buffer.load.i32");
185
186 // 8 bytes
187 __device__ int32x2_t
188 llvm_amdgcn_s_buffer_load_i32x2(int32x4_t buffer_resource,
189 uint32_t soffset,
190 int32_t cache_op) __asm("llvm.amdgcn.s.buffer.load.v2i32");
191
192 // 16 bytes
193 __device__ int32x4_t
194 llvm_amdgcn_s_buffer_load_i32x4(int32x4_t buffer_resource,
195 uint32_t soffset,
196 int32_t cache_op) __asm("llvm.amdgcn.s.buffer.load.v4i32");
197
198 ///
199 /// Store
200 ///
201
202 // 1 byte
203 __device__ void
204 llvm_amdgcn_raw_buffer_store_i8(char data,
205 int32x4_t buffer_resource,
206 uint32_t voffset,
207 uint32_t soffset,
208 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.store.i8");
209
210 // 2 bytes
211 __device__ void
212 llvm_amdgcn_raw_buffer_store_f16(float16_t data,
213 int32x4_t buffer_resource,
214 uint32_t voffset,
215 uint32_t soffset,
216 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.store.f16");
217
218 // 4 bytes
219 __device__ void
220 llvm_amdgcn_raw_buffer_store_f32(float32_t data,
221 int32x4_t buffer_resource,
222 uint32_t voffset,
223 uint32_t soffset,
224 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.store.f32");
225
226 // 8 bytes
227 __device__ void llvm_amdgcn_raw_buffer_store_f32x2(
228 float32x2_t data,
229 int32x4_t buffer_resource,
230 uint32_t voffset,
231 uint32_t soffset,
232 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.store.v2f32");
233
234 // 16 bytes
235 __device__ void llvm_amdgcn_raw_buffer_store_f32x4(
236 float32x4_t data,
237 int32x4_t buffer_resource,
238 uint32_t voffset,
239 uint32_t soffset,
240 int32_t cache_op) __asm("llvm.amdgcn.raw.buffer.store.v4f32");
241
242 ////////////////////////////////////////////////////////////////////////////////////////////////////
243
244 template <
245 /// Fragment type to store loaded data
246 typename AccessType,
247 /// The bytes of loading
248 int LoadBytes,
249 /// Cache operation
250 CacheOperation::Kind cache_op = CacheOperation::Always>
251 struct buffer_load;
252
253 template <typename AccessType, CacheOperation::Kind cache_op>
254 struct buffer_load<AccessType, 1, cache_op>
255 {
256 INLINEDEVICE
257 buffer_load() {}
258
259 INLINEDEVICE
260 buffer_load(AccessType& D,
261 void const* base_ptr,
262 uint32_t voffset,
263 uint32_t soffset,
264 uint32_t num_records = (0xFFFFFFFF - 1))
265 {
266 BufferResource buffer_rsc(base_ptr, num_records);
267 char ret = llvm_amdgcn_raw_buffer_load_i8(
268 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
269 D = *reinterpret_cast<AccessType*>(&ret);
270 }
271
272 INLINEDEVICE
273 AccessType load(void const* base_ptr,
274 uint32_t voffset,
275 uint32_t soffset,
276 uint32_t num_records = (0xFFFFFFFF - 1))
277 {
278 BufferResource buffer_rsc(base_ptr, num_records);
279 char ret = llvm_amdgcn_raw_buffer_load_i8(
280 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
281 return *reinterpret_cast<AccessType*>(&ret);
282 }
283 };
284
285 template <typename AccessType, CacheOperation::Kind cache_op>
286 struct buffer_load<AccessType, 2, cache_op>
287 {
288 INLINEDEVICE
289 buffer_load() {}
290
291 INLINEDEVICE
292 buffer_load(AccessType& D,
293 void const* base_ptr,
294 uint32_t voffset,
295 uint32_t soffset,
296 uint32_t num_records = (0xFFFFFFFF - 1))
297 {
298 BufferResource buffer_rsc(base_ptr, num_records);
299 float16_t ret = llvm_amdgcn_raw_buffer_load_f16(
300 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
301 D = *reinterpret_cast<AccessType*>(&ret);
302 }
303
304 INLINEDEVICE
305 AccessType load(void const* base_ptr,
306 uint32_t voffset,
307 uint32_t soffset,
308 uint32_t num_records = (0xFFFFFFFF - 1))
309 {
310 BufferResource buffer_rsc(base_ptr, num_records);
311 float16_t ret = llvm_amdgcn_raw_buffer_load_f16(
312 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
313 return *reinterpret_cast<AccessType*>(&ret);
314 }
315 };
316
317 template <typename AccessType, CacheOperation::Kind cache_op>
318 struct buffer_load<AccessType, 4, cache_op>
319 {
320 INLINEDEVICE
321 buffer_load() {}
322
323 INLINEDEVICE
324 buffer_load(AccessType& D,
325 void const* base_ptr,
326 uint32_t voffset,
327 uint32_t soffset,
328 uint32_t num_records = (0xFFFFFFFF - 1))
329 {
330 BufferResource buffer_rsc(base_ptr, num_records);
331 float32_t ret = llvm_amdgcn_raw_buffer_load_f32(
332 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
333 D = *reinterpret_cast<AccessType*>(&ret);
334 }
335
336 INLINEDEVICE
337 AccessType load(void const* base_ptr,
338 uint32_t voffset,
339 uint32_t soffset,
340 uint32_t num_records = (0xFFFFFFFF - 1))
341 {
342 BufferResource buffer_rsc(base_ptr, num_records);
343 float32_t ret = llvm_amdgcn_raw_buffer_load_f32(
344 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
345 return *reinterpret_cast<AccessType*>(&ret);
346 }
347 };
348
349 template <typename AccessType, CacheOperation::Kind cache_op>
350 struct buffer_load<AccessType, 8, cache_op>
351 {
352 INLINEDEVICE
353 buffer_load() {}
354
355 INLINEDEVICE
356 buffer_load(AccessType& D,
357 void const* base_ptr,
358 uint32_t voffset,
359 uint32_t soffset,
360 uint32_t num_records = (0xFFFFFFFF - 1))
361 {
362 BufferResource buffer_rsc(base_ptr, num_records);
363 float32x2_t ret = llvm_amdgcn_raw_buffer_load_f32x2(
364 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
365 D = *reinterpret_cast<AccessType*>(&ret);
366 }
367
368 INLINEDEVICE
369 AccessType load(void const* base_ptr,
370 uint32_t voffset,
371 uint32_t soffset,
372 uint32_t num_records = (0xFFFFFFFF - 1))
373 {
374 BufferResource buffer_rsc(base_ptr, num_records);
375 float32x2_t ret = llvm_amdgcn_raw_buffer_load_f32x2(
376 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
377 return *reinterpret_cast<AccessType*>(&ret);
378 }
379 };
380
381 template <typename AccessType, CacheOperation::Kind cache_op>
382 struct buffer_load<AccessType, 16, cache_op>
383 {
384 INLINEDEVICE
385 buffer_load() {}
386
387 INLINEDEVICE
388 buffer_load(AccessType& D,
389 void const* base_ptr,
390 uint32_t voffset,
391 uint32_t soffset,
392 uint32_t num_records = (0xFFFFFFFF - 1))
393 {
394 BufferResource buffer_rsc(base_ptr, num_records);
395 float32x4_t ret = llvm_amdgcn_raw_buffer_load_f32x4(
396 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
397 D = *reinterpret_cast<AccessType*>(&ret);
398 }
399
400 INLINEDEVICE
401 AccessType load(void const* base_ptr,
402 uint32_t voffset,
403 uint32_t soffset,
404 uint32_t num_records = (0xFFFFFFFF - 1))
405 {
406 BufferResource buffer_rsc(base_ptr, num_records);
407 float32x4_t ret = llvm_amdgcn_raw_buffer_load_f32x4(
408 buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
409 return *reinterpret_cast<AccessType*>(&ret);
410 }
411 };
412
413 template <
414 /// Fragment type to store loaded data
415 typename AccessType,
416 /// The bytes of loading
417 int LoadBytes,
418 /// Cache operation
419 CacheOperation::Kind cache_op = CacheOperation::Always>
420 struct s_buffer_load;
421
422 template <typename AccessType, CacheOperation::Kind cache_op>
423 struct s_buffer_load<AccessType, 4, cache_op>
424 {
425 INLINEDEVICE
426 s_buffer_load() {}
427
428 INLINEDEVICE
429 s_buffer_load(AccessType& D, void const* base_ptr, uint32_t soffset)
430 {
431 BufferResource buffer_rsc(base_ptr);
432 int32_t ret = llvm_amdgcn_s_buffer_load_i32(
433 buffer_rsc, __builtin_amdgcn_readfirstlane(soffset), cache_op);
434 D = *reinterpret_cast<AccessType*>(&ret);
435 }
436
437 INLINEDEVICE
438 AccessType load(void const* base_ptr, uint32_t soffset)
439 {
440 BufferResource buffer_rsc(base_ptr);
441 int32_t ret = llvm_amdgcn_s_buffer_load_i32(
442 buffer_rsc, __builtin_amdgcn_readfirstlane(soffset), cache_op);
443 return *reinterpret_cast<AccessType*>(&ret);
444 }
445 };
446
447 template <typename AccessType, CacheOperation::Kind cache_op>
448 struct s_buffer_load<AccessType, 8, cache_op>
449 {
450 INLINEDEVICE
451 s_buffer_load() {}
452
453 INLINEDEVICE
454 s_buffer_load(AccessType& D, void const* base_ptr, uint32_t soffset)
455 {
456 BufferResource buffer_rsc(base_ptr);
457 int32x2_t ret = llvm_amdgcn_s_buffer_load_i32x2(
458 buffer_rsc, __builtin_amdgcn_readfirstlane(soffset), cache_op);
459 D = *reinterpret_cast<AccessType*>(&ret);
460 }
461
462 INLINEDEVICE
463 AccessType load(void const* base_ptr, uint32_t soffset)
464 {
465 BufferResource buffer_rsc(base_ptr);
466 int32x2_t ret = llvm_amdgcn_s_buffer_load_i32x2(
467 buffer_rsc, __builtin_amdgcn_readfirstlane(soffset), cache_op);
468 return *reinterpret_cast<AccessType*>(&ret);
469 }
470 };
471
472 template <typename AccessType, CacheOperation::Kind cache_op>
473 struct s_buffer_load<AccessType, 16, cache_op>
474 {
475 INLINEDEVICE
476 s_buffer_load() {}
477
478 INLINEDEVICE
479 s_buffer_load(AccessType& D, void const* base_ptr, uint32_t soffset)
480 {
481 BufferResource buffer_rsc(base_ptr);
482 int32x4_t ret = llvm_amdgcn_s_buffer_load_i32x4(
483 buffer_rsc, __builtin_amdgcn_readfirstlane(soffset), cache_op);
484 D = *reinterpret_cast<AccessType*>(&ret);
485 }
486
487 INLINEDEVICE
488 AccessType load(void const* base_ptr, uint32_t soffset)
489 {
490 BufferResource buffer_rsc(base_ptr);
491 int32x4_t ret = llvm_amdgcn_s_buffer_load_i32x4(
492 buffer_rsc, __builtin_amdgcn_readfirstlane(soffset), cache_op);
493 return *reinterpret_cast<AccessType*>(&ret);
494 }
495 };
496
497 ////////////////////////////////////////////////////////////////////////////////////////////////////
498
499 template <
500 /// Fragment type to store loaded data
501 typename AccessType,
502 /// The width of loading
503 int NumElements,
504 /// Cache operation
505 CacheOperation::Kind cache_op = CacheOperation::Always>
506 struct buffer_store;
507
508 template <typename AccessType, CacheOperation::Kind cache_op>
509 struct buffer_store<AccessType, 1, cache_op>
510 {
511 INLINEDEVICE
512 buffer_store(const AccessType& D,
513 void const* base_ptr,
514 uint32_t voffset,
515 uint32_t soffset,
516 uint32_t num_records = (0xFFFFFFFF - 1))
517 {
518 BufferResource buffer_rsc(base_ptr, num_records);
519 char data = *reinterpret_cast<char const*>(&D);
520 llvm_amdgcn_raw_buffer_store_i8(
521 data, buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
522 }
523 };
524
525 template <typename AccessType, CacheOperation::Kind cache_op>
526 struct buffer_store<AccessType, 2, cache_op>
527 {
528 INLINEDEVICE
529 buffer_store(const AccessType& D,
530 void const* base_ptr,
531 uint32_t voffset,
532 uint32_t soffset,
533 uint32_t num_records = (0xFFFFFFFF - 1))
534 {
535 BufferResource buffer_rsc(base_ptr, num_records);
536 float16_t data = *reinterpret_cast<float16_t const*>(&D);
537 llvm_amdgcn_raw_buffer_store_f16(
538 data, buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
539 }
540 };
541
542 template <typename AccessType, CacheOperation::Kind cache_op>
543 struct buffer_store<AccessType, 4, cache_op>
544 {
545 INLINEDEVICE
546 buffer_store(const AccessType& D,
547 void const* base_ptr,
548 uint32_t voffset,
549 uint32_t soffset,
550 uint32_t num_records = (0xFFFFFFFF - 1))
551 {
552 BufferResource buffer_rsc(base_ptr, num_records);
553 float32_t data = *reinterpret_cast<float32_t const*>(&D);
554 llvm_amdgcn_raw_buffer_store_f32(
555 data, buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
556 }
557 };
558
559 template <typename AccessType, CacheOperation::Kind cache_op>
560 struct buffer_store<AccessType, 8, cache_op>
561 {
562 INLINEDEVICE
563 buffer_store(const AccessType& D,
564 void const* base_ptr,
565 uint32_t voffset,
566 uint32_t soffset,
567 uint32_t num_records = (0xFFFFFFFF - 1))
568 {
569 BufferResource buffer_rsc(base_ptr, num_records);
570 float32x2_t data = *reinterpret_cast<float32x2_t const*>(&D);
571 llvm_amdgcn_raw_buffer_store_f32x2(
572 data, buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
573 }
574 };
575
576 template <typename AccessType, CacheOperation::Kind cache_op>
577 struct buffer_store<AccessType, 16, cache_op>
578 {
579 INLINEDEVICE
580 buffer_store(const AccessType& D,
581 void const* base_ptr,
582 uint32_t voffset,
583 uint32_t soffset,
584 uint32_t num_records = (0xFFFFFFFF - 1))
585 {
586 BufferResource buffer_rsc(base_ptr, num_records);
587 float32x4_t data = *reinterpret_cast<float32x4_t const*>(&D);
588 llvm_amdgcn_raw_buffer_store_f32x4(
589 data, buffer_rsc, voffset, __builtin_amdgcn_readfirstlane(soffset), cache_op);
590 }
591 };
592
593 ////////////////////////////////////////////////////////////////////////////////////////////////////
594
595 #endif // defined(__clang__) && defined(__HIP__)
596
597 #endif // INTRINSIC_MEM_ACCESS_H