Skip to content

Commit 952bb44

Browse files
committed
Update and address comments
1 parent 4a3348e commit 952bb44

File tree

4 files changed

+642
-141
lines changed

4 files changed

+642
-141
lines changed

clang/lib/Headers/amdgpuintrin.h

Lines changed: 34 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -10,178 +10,144 @@
1010
#define __AMDGPUINTRIN_H
1111

1212
#ifndef __AMDGPU__
13-
#error "This file is intended for AMDGPU targets or offloading to AMDGPU
13+
#error "This file is intended for AMDGPU targets or offloading to AMDGPU"
1414
#endif
1515

1616
#include <stdbool.h>
1717
#include <stdint.h>
1818

1919
#if defined(__HIP__) || defined(__CUDA__)
20-
#define _DEFAULT_ATTRS __attribute__((device)) __attribute__((always_inline))
21-
#else
22-
#define _DEFAULT_ATTRS __attribute__((always_inline))
20+
#define _DEFAULT_ATTRS __attribute__((device))
21+
#elif !defined(_DEFAULT_ATTRS)
22+
#define _DEFAULT_ATTRS
2323
#endif
2424

2525
#pragma omp begin declare target device_type(nohost)
2626
#pragma omp begin declare variant match(device = {arch(amdgcn)})
2727

2828
// Type aliases to the address spaces used by the AMDGPU backend.
29-
#define _private __attribute__((opencl_private))
30-
#define _constant __attribute__((opencl_constant))
31-
#define _local __attribute__((opencl_local))
32-
#define _global __attribute__((opencl_global))
29+
#define _Private __attribute__((opencl_private))
30+
#define _Constant __attribute__((opencl_constant))
31+
#define _Local __attribute__((opencl_local))
32+
#define _Global __attribute__((opencl_global))
3333

3434
// Attribute to declare a function as a kernel.
35-
#define _kernel __attribute__((amdgpu_kernel, visibility("protected")))
35+
#define _Kernel __attribute__((amdgpu_kernel, visibility("protected")))
3636

3737
// Returns the number of workgroups in the 'x' dimension of the grid.
38-
_DEFAULT_ATTRS static inline uint32_t _get_num_blocks_x() {
38+
_DEFAULT_ATTRS static inline uint32_t __gpu_num_blocks_x() {
3939
return __builtin_amdgcn_grid_size_x() / __builtin_amdgcn_workgroup_size_x();
4040
}
4141

4242
// Returns the number of workgroups in the 'y' dimension of the grid.
43-
_DEFAULT_ATTRS static inline uint32_t _get_num_blocks_y() {
43+
_DEFAULT_ATTRS static inline uint32_t __gpu_num_blocks_y() {
4444
return __builtin_amdgcn_grid_size_y() / __builtin_amdgcn_workgroup_size_y();
4545
}
4646

4747
// Returns the number of workgroups in the 'z' dimension of the grid.
48-
_DEFAULT_ATTRS static inline uint32_t _get_num_blocks_z() {
48+
_DEFAULT_ATTRS static inline uint32_t __gpu_num_blocks_z() {
4949
return __builtin_amdgcn_grid_size_z() / __builtin_amdgcn_workgroup_size_z();
5050
}
5151

52-
// Returns the total number of workgruops in the grid.
53-
_DEFAULT_ATTRS static inline uint64_t _get_num_blocks() {
54-
return _get_num_blocks_x() * _get_num_blocks_y() * _get_num_blocks_z();
55-
}
56-
5752
// Returns the 'x' dimension of the current AMD workgroup's id.
58-
_DEFAULT_ATTRS static inline uint32_t _get_block_id_x() {
53+
_DEFAULT_ATTRS static inline uint32_t __gpu_block_id_x() {
5954
return __builtin_amdgcn_workgroup_id_x();
6055
}
6156

6257
// Returns the 'y' dimension of the current AMD workgroup's id.
63-
_DEFAULT_ATTRS static inline uint32_t _get_block_id_y() {
58+
_DEFAULT_ATTRS static inline uint32_t __gpu_block_id_y() {
6459
return __builtin_amdgcn_workgroup_id_y();
6560
}
6661

6762
// Returns the 'z' dimension of the current AMD workgroup's id.
68-
_DEFAULT_ATTRS static inline uint32_t _get_block_id_z() {
63+
_DEFAULT_ATTRS static inline uint32_t __gpu_block_id_z() {
6964
return __builtin_amdgcn_workgroup_id_z();
7065
}
7166

72-
// Returns the absolute id of the AMD workgroup.
73-
_DEFAULT_ATTRS static inline uint64_t _get_block_id() {
74-
return _get_block_id_x() + _get_num_blocks_x() * _get_block_id_y() +
75-
_get_num_blocks_x() * _get_num_blocks_y() * _get_block_id_z();
76-
}
77-
7867
// Returns the number of workitems in the 'x' dimension.
79-
_DEFAULT_ATTRS static inline uint32_t _get_num_threads_x() {
68+
_DEFAULT_ATTRS static inline uint32_t __gpu_num_threads_x() {
8069
return __builtin_amdgcn_workgroup_size_x();
8170
}
8271

8372
// Returns the number of workitems in the 'y' dimension.
84-
_DEFAULT_ATTRS static inline uint32_t _get_num_threads_y() {
73+
_DEFAULT_ATTRS static inline uint32_t __gpu_num_threads_y() {
8574
return __builtin_amdgcn_workgroup_size_y();
8675
}
8776

8877
// Returns the number of workitems in the 'z' dimension.
89-
_DEFAULT_ATTRS static inline uint32_t _get_num_threads_z() {
78+
_DEFAULT_ATTRS static inline uint32_t __gpu_num_threads_z() {
9079
return __builtin_amdgcn_workgroup_size_z();
9180
}
9281

93-
// Returns the total number of workitems in the workgroup.
94-
_DEFAULT_ATTRS static inline uint64_t _get_num_threads() {
95-
return _get_num_threads_x() * _get_num_threads_y() * _get_num_threads_z();
96-
}
97-
9882
// Returns the 'x' dimension id of the workitem in the current AMD workgroup.
99-
_DEFAULT_ATTRS static inline uint32_t _get_thread_id_x() {
83+
_DEFAULT_ATTRS static inline uint32_t __gpu_thread_id_x() {
10084
return __builtin_amdgcn_workitem_id_x();
10185
}
10286

10387
// Returns the 'y' dimension id of the workitem in the current AMD workgroup.
104-
_DEFAULT_ATTRS static inline uint32_t _get_thread_id_y() {
88+
_DEFAULT_ATTRS static inline uint32_t __gpu_thread_id_y() {
10589
return __builtin_amdgcn_workitem_id_y();
10690
}
10791

10892
// Returns the 'z' dimension id of the workitem in the current AMD workgroup.
109-
_DEFAULT_ATTRS static inline uint32_t _get_thread_id_z() {
93+
_DEFAULT_ATTRS static inline uint32_t __gpu_thread_id_z() {
11094
return __builtin_amdgcn_workitem_id_z();
11195
}
11296

113-
// Returns the absolute id of the thread in the current AMD workgroup.
114-
_DEFAULT_ATTRS static inline uint64_t _get_thread_id() {
115-
return _get_thread_id_x() + _get_num_threads_x() * _get_thread_id_y() +
116-
_get_num_threads_x() * _get_num_threads_y() * _get_thread_id_z();
117-
}
118-
11997
// Returns the size of an AMD wavefront, either 32 or 64 depending on hardware
12098
// and compilation options.
121-
_DEFAULT_ATTRS static inline uint32_t _get_lane_size() {
99+
_DEFAULT_ATTRS static inline uint32_t __gpu_num_lanes() {
122100
return __builtin_amdgcn_wavefrontsize();
123101
}
124102

125103
// Returns the id of the thread inside of an AMD wavefront executing together.
126-
_DEFAULT_ATTRS [[clang::convergent]] static inline uint32_t _get_lane_id() {
104+
_DEFAULT_ATTRS [[clang::convergent]] static inline uint32_t __gpu_lane_id() {
127105
return __builtin_amdgcn_mbcnt_hi(~0u, __builtin_amdgcn_mbcnt_lo(~0u, 0u));
128106
}
129107

130108
// Returns the bit-mask of active threads in the current wavefront.
131-
_DEFAULT_ATTRS [[clang::convergent]] static inline uint64_t _get_lane_mask() {
109+
_DEFAULT_ATTRS [[clang::convergent]] static inline uint64_t __gpu_lane_mask() {
132110
return __builtin_amdgcn_read_exec();
133111
}
134112

135113
// Copies the value from the first active thread in the wavefront to the rest.
136114
_DEFAULT_ATTRS [[clang::convergent]] static inline uint32_t
137-
_broadcast_value(uint64_t, uint32_t x) {
138-
return __builtin_amdgcn_readfirstlane(x);
115+
__gpu_broadcast(uint64_t __lane_mask, uint32_t __x) {
116+
return __builtin_amdgcn_readfirstlane(__x);
139117
}
140118

141119
// Returns a bitmask of threads in the current lane for which \p x is true.
142120
_DEFAULT_ATTRS [[clang::convergent]] static inline uint64_t
143-
_ballot(uint64_t lane_mask, bool x) {
121+
__gpu_ballot(uint64_t __lane_mask, bool __x) {
144122
// The lane_mask & gives the nvptx semantics when lane_mask is a subset of
145123
// the active threads
146-
return lane_mask & __builtin_amdgcn_ballot_w64(x);
124+
return __lane_mask & __builtin_amdgcn_ballot_w64(__x);
147125
}
148126

149127
// Waits for all the threads in the block to converge and issues a fence.
150-
_DEFAULT_ATTRS [[clang::convergent]] static inline void _sync_threads() {
128+
_DEFAULT_ATTRS [[clang::convergent]] static inline void __gpu_sync_threads() {
151129
__builtin_amdgcn_s_barrier();
152130
__builtin_amdgcn_fence(__ATOMIC_ACQUIRE, "workgroup");
153131
}
154132

155133
// Wait for all threads in the wavefront to converge, this is a noop on AMDGPU.
156-
_DEFAULT_ATTRS [[clang::convergent]] static inline void _sync_lane(uint64_t) {
134+
_DEFAULT_ATTRS [[clang::convergent]] static inline void
135+
__gpu_sync_lane(uint64_t __lane_mask) {
157136
__builtin_amdgcn_wave_barrier();
158137
}
159138

160139
// Shuffles the the lanes inside the wavefront according to the given index.
161140
_DEFAULT_ATTRS [[clang::convergent]] static inline uint32_t
162-
_shuffle(uint64_t, uint32_t idx, uint32_t x) {
163-
return __builtin_amdgcn_ds_bpermute(idx << 2, x);
164-
}
165-
166-
// Returns the current value of the GPU's processor clock.
167-
// NOTE: The RDNA3 and RDNA2 architectures use a 20-bit cycle counter.
168-
_DEFAULT_ATTRS static inline uint64_t _processor_clock() {
169-
return __builtin_readcyclecounter();
170-
}
171-
172-
// Returns a fixed-frequency timestamp. The actual frequency is dependent on
173-
// the card and can only be queried via the driver.
174-
_DEFAULT_ATTRS static inline uint64_t _fixed_frequency_clock() {
175-
return __builtin_readsteadycounter();
141+
__gpu_shuffle_idx(uint64_t __lane_mask, uint32_t __idx, uint32_t __x) {
142+
return __builtin_amdgcn_ds_bpermute(__idx << 2, __x);
176143
}
177144

178145
// Terminates execution of the associated wavefront.
179-
_DEFAULT_ATTRS [[noreturn]] static inline void _end_program() {
146+
_DEFAULT_ATTRS [[noreturn]] static inline void __gpu_exit() {
180147
__builtin_amdgcn_endpgm();
181148
}
182149

183150
#pragma omp end declare variant
184151
#pragma omp end declare target
185-
#undef _DEFAULT_ATTRS
186152

187153
#endif // __AMDGPUINTRIN_H

clang/lib/Headers/gpuintrin.h

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,4 +15,62 @@
1515
#include <amdgpuintrin.h>
1616
#endif
1717

18+
// Returns the total number of blocks / workgroups.
19+
_DEFAULT_ATTRS static inline uint64_t __gpu_num_blocks() {
20+
return __gpu_num_blocks_x() * __gpu_num_blocks_y() * __gpu_num_blocks_z();
21+
}
22+
23+
// Returns the absolute id of the block / workgroup.
24+
_DEFAULT_ATTRS static inline uint64_t __gpu_block_id() {
25+
return __gpu_block_id_x() +
26+
(uint64_t)__gpu_num_blocks_x() * __gpu_block_id_y() +
27+
(uint64_t)__gpu_num_blocks_x() * __gpu_num_blocks_y() *
28+
__gpu_block_id_z();
29+
}
30+
31+
// Returns the total number of threads in the block / workgroup.
32+
_DEFAULT_ATTRS static inline uint32_t __gpu_num_threads() {
33+
return __gpu_num_threads_x() * __gpu_num_threads_y() * __gpu_num_threads_z();
34+
}
35+
36+
// Returns the absolute id of the thread in the current block / workgroup.
37+
_DEFAULT_ATTRS static inline uint32_t __gpu_thread_id() {
38+
return __gpu_thread_id_x() + __gpu_num_threads_x() * __gpu_thread_id_y() +
39+
__gpu_num_threads_x() * __gpu_num_threads_y() * __gpu_thread_id_z();
40+
}
41+
42+
// Get the first active thread inside the lane.
43+
_DEFAULT_ATTRS static inline uint64_t
44+
__gpu_first_lane_id(uint64_t __lane_mask) {
45+
return __builtin_ffsll(__lane_mask) - 1;
46+
}
47+
48+
// Conditional that is only true for a single thread in a lane.
49+
_DEFAULT_ATTRS static inline bool __gpu_is_first_lane(uint64_t __lane_mask) {
50+
return __gpu_lane_id() == __gpu_first_lane_id(__lane_mask);
51+
}
52+
53+
// Gets the sum of all lanes inside the warp or wavefront.
54+
_DEFAULT_ATTRS static inline uint32_t __gpu_lane_reduce(uint64_t __lane_mask,
55+
uint32_t x) {
56+
for (uint32_t step = __gpu_num_lanes() / 2; step > 0; step /= 2) {
57+
uint32_t index = step + __gpu_lane_id();
58+
x += __gpu_shuffle_idx(__lane_mask, index, x);
59+
}
60+
return __gpu_broadcast(__lane_mask, x);
61+
}
62+
63+
// Gets the accumulator scan of the threads in the warp or wavefront.
64+
_DEFAULT_ATTRS static inline uint32_t __gpu_lane_scan(uint64_t __lane_mask,
65+
uint32_t x) {
66+
for (uint32_t step = 1; step < __gpu_num_lanes(); step *= 2) {
67+
uint32_t index = __gpu_lane_id() - step;
68+
uint32_t bitmask = __gpu_lane_id() >= step;
69+
x += -bitmask & __gpu_shuffle_idx(__lane_mask, index, x);
70+
}
71+
return x;
72+
}
73+
74+
#undef _DEFAULT_ATTRS
75+
1876
#endif // __GPUINTRIN_H

0 commit comments

Comments
 (0)