Skip to content

Commit 180147e

Browse files
introduce SPIR-V target for 'libc'
1 parent 8657165 commit 180147e

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

118 files changed

+3786
-2
lines changed

clang/include/clang/Basic/BuiltinsSPIRV.td

Lines changed: 85 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,8 +32,92 @@ def SPIRVSmoothStep : Builtin {
3232
let Prototype = "void(...)";
3333
}
3434

35-
def SPIRVFaceForward : Builtin {
35+
def SPIRVSmoothStep : Builtin {
3636
let Spellings = ["__builtin_spirv_faceforward"];
3737
let Attributes = [NoThrow, Const, CustomTypeChecking];
3838
let Prototype = "void(...)";
3939
}
40+
41+
def SPIRVGetNumWrkgrpX : Builtin {
42+
let Spellings = ["__builtin_spirv_get_num_workgroups_x"];
43+
let Attributes = [NoThrow, Const];
44+
let Prototype = "void(...)";
45+
}
46+
47+
def SPIRVGetNumWrkgrpY : Builtin {
48+
let Spellings = ["__builtin_spirv_get_num_workgroups_y"];
49+
let Attributes = [NoThrow, Const];
50+
let Prototype = "void(...)";
51+
}
52+
53+
def SPIRVGetNumWrkgrpZ : Builtin {
54+
let Spellings = ["__builtin_spirv_get_num_workgroups_z"];
55+
let Attributes = [NoThrow, Const];
56+
let Prototype = "void(...)";
57+
}
58+
59+
def SPIRVGetWrkgrpIdX : Builtin {
60+
let Spellings = ["__builtin_spirv_get_workgroup_id_x"];
61+
let Attributes = [NoThrow, Const];
62+
let Prototype = "void(...)";
63+
}
64+
65+
def SPIRVGetWrkgrpIdY : Builtin {
66+
let Spellings = ["__builtin_spirv_get_workgroup_id_y"];
67+
let Attributes = [NoThrow, Const];
68+
let Prototype = "void(...)";
69+
}
70+
71+
def SPIRVGetWrkgrpIdZ : Builtin {
72+
let Spellings = ["__builtin_spirv_get_workgroup_id_z"];
73+
let Attributes = [NoThrow, Const];
74+
let Prototype = "void(...)";
75+
}
76+
77+
def SPIRVGetWrkgrpSizeX : Builtin {
78+
let Spellings = ["__builtin_spirv_workgroup_size_x"];
79+
let Attributes = [NoThrow, Const];
80+
let Prototype = "void(...)";
81+
}
82+
83+
def SPIRVGetWrkgrpSizeY : Builtin {
84+
let Spellings = ["__builtin_spirv_workgroup_size_y"];
85+
let Attributes = [NoThrow, Const];
86+
let Prototype = "void(...)";
87+
}
88+
89+
def SPIRVGetWrkgrpSizeZ : Builtin {
90+
let Spellings = ["__builtin_spirv_workgroup_size_z"];
91+
let Attributes = [NoThrow, Const];
92+
let Prototype = "void(...)";
93+
}
94+
95+
def SPIRVGetWrkitemIdX : Builtin {
96+
let Spellings = ["__builtin_spirv_workitem_id_x"];
97+
let Attributes = [NoThrow, Const];
98+
let Prototype = "void(...)";
99+
}
100+
101+
def SPIRVGetWrkitemIdY : Builtin {
102+
let Spellings = ["__builtin_spirv_workitem_id_y"];
103+
let Attributes = [NoThrow, Const];
104+
let Prototype = "void(...)";
105+
}
106+
107+
def SPIRVGetWrkitemIdZ : Builtin {
108+
let Spellings = ["__builtin_spirv_workitem_id_z"];
109+
let Attributes = [NoThrow, Const];
110+
let Prototype = "void(...)";
111+
}
112+
113+
def SPIRVBallot : Builtin {
114+
let Spellings = ["__builtin_spirv_ballot"];
115+
let Attributes = [NoThrow, Const];
116+
let Prototype = "void(...)";
117+
}
118+
119+
def SPIRVSyncThreads : Builtin {
120+
let Spellings = ["__builtin_spirv_sync_threads"];
121+
let Attributes = [NoThrow, Const];
122+
let Prototype = "void(...)";
123+
}

clang/lib/Headers/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -289,6 +289,7 @@ set(gpu_files
289289
gpuintrin.h
290290
nvptxintrin.h
291291
amdgpuintrin.h
292+
spirvintrin.h
292293
)
293294

294295
set(windows_only_files

clang/lib/Headers/gpuintrin.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,8 @@ _Pragma("omp end declare target");
6060
#include <nvptxintrin.h>
6161
#elif defined(__AMDGPU__)
6262
#include <amdgpuintrin.h>
63+
#elif defined(__SPIRV__)
64+
#include <spirvintrin.h>
6365
#elif !defined(_OPENMP)
6466
#error "This header is only meant to be used on GPU architectures."
6567
#endif

clang/lib/Headers/spirvintrin.h

Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
1+
//===-- spirvintrin.h - SPIR-V intrinsic functions ------------------------===//
2+
//
3+
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4+
// See https://llvm.org/LICENSE.txt for license information.
5+
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6+
//
7+
//===----------------------------------------------------------------------===//
8+
9+
#ifndef __SPIRVINTRIN_H
10+
#define __SPIRVINTRIN_H
11+
12+
#ifndef __SPIRV__
13+
#error "This file is intended for SPIRV targets"
14+
#endif
15+
16+
#ifndef __GPUINTRIN_H
17+
#error "Never use <spirvintrin.h> directly; include <gpuintrin.h> instead"
18+
#endif
19+
20+
_Pragma("omp begin declare target device_type(nohost)");
21+
_Pragma("omp begin declare variant match(device = {arch(amdgcn)})");
22+
23+
// Type aliases to the address spaces used by the SPIRV backend.
24+
#define __gpu_private __attribute__((address_space(5)))
25+
#define __gpu_constant __attribute__((address_space(4)))
26+
#define __gpu_local __attribute__((address_space(3)))
27+
#define __gpu_global __attribute__((address_space(1)))
28+
#define __gpu_generic __attribute__((address_space(0)))
29+
30+
// Attribute to declare a function as a kernel.
31+
#define __gpu_kernel __attribute__((spir_kernel, visibility("protected")))
32+
33+
// Returns the number of workgroups in the 'x' dimension of the grid.
34+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_x(void) {
35+
return __builtin_spirv_get_num_workgroups_x();
36+
}
37+
38+
// Returns the number of workgroups in the 'y' dimension of the grid.
39+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_y(void) {
40+
return __builtin_spirv_get_num_workgroups_y();
41+
}
42+
43+
// Returns the number of workgroups in the 'z' dimension of the grid.
44+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_z(void) {
45+
return __builtin_spirv_get_num_workgroups_z();
46+
}
47+
48+
// Returns the 'x' dimension of the current SPIR-V workgroup's id.
49+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_x(void) {
50+
return __builtin_spirv_get_workgroup_id_x();
51+
}
52+
53+
// Returns the 'y' dimension of the current SPIR-V workgroup's id.
54+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_y(void) {
55+
return __builtin_spirv_get_workgroup_id_y();
56+
}
57+
58+
// Returns the 'z' dimension of the current SPIR-V workgroup's id.
59+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_block_id_z(void) {
60+
return __builtin_spirv_get_workgroup_id_z();
61+
}
62+
63+
// Returns the number of workitems in the 'x' dimension.
64+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_x(void) {
65+
return __builtin_spirv_workgroup_size_x();
66+
}
67+
68+
// Returns the number of workitems in the 'y' dimension.
69+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_y(void) {
70+
return __builtin_spirv_workgroup_size_y();
71+
}
72+
73+
// Returns the number of workitems in the 'z' dimension.
74+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_threads_z(void) {
75+
return __builtin_spirv_workgroup_size_z();
76+
}
77+
78+
// Returns the 'x' dimension id of the workitem in the current SPIR-V workgroup.
79+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_x(void) {
80+
return __builtin_spirv_workitem_id_x();
81+
}
82+
83+
// Returns the 'y' dimension id of the workitem in the current SPIR-V workgroup.
84+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_y(void) {
85+
return __builtin_spirv_workitem_id_y();
86+
}
87+
88+
// Returns the 'z' dimension id of the workitem in the current SPIR-V workgroup.
89+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_thread_id_z(void) {
90+
return __builtin_spirv_workitem_id_z();
91+
}
92+
93+
// Returns the size of a wavefront
94+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_lanes(void) {
95+
__builtin_unreachable();
96+
}
97+
98+
// Returns the id of the thread inside of a wavefront executing together.
99+
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_lane_id(void) {
100+
__builtin_unreachable();
101+
}
102+
103+
// Returns the bit-mask of active threads in the current wavefront.
104+
_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_lane_mask(void) {
105+
__builtin_unreachable();
106+
}
107+
108+
// Copies the value from the first active thread in the wavefront to the rest.
109+
_DEFAULT_FN_ATTRS static __inline__ uint32_t
110+
__gpu_read_first_lane_u32(uint64_t __lane_mask, uint32_t __x) {
111+
__builtin_unreachable();
112+
}
113+
114+
// Returns a bitmask of threads in the current lane for which \p x is true.
115+
_DEFAULT_FN_ATTRS static __inline__ uint64_t __gpu_ballot(uint64_t __lane_mask,
116+
bool __x) {
117+
return __lane_mask & __builtin_spirv_ballot(__x);
118+
}
119+
120+
// Waits for all the threads in the block to converge and issues a fence.
121+
_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_threads(void) {
122+
__builtin_spirv_sync_threads();
123+
}
124+
125+
// Wait for all threads in the wavefront to converge.
126+
_DEFAULT_FN_ATTRS static __inline__ void __gpu_sync_lane(uint64_t __lane_mask) {
127+
__builtin_unreachable();
128+
}
129+
130+
// Shuffles the the lanes inside the wavefront according to the given index.
131+
_DEFAULT_FN_ATTRS static __inline__ uint32_t
132+
__gpu_shuffle_idx_u32(uint64_t __lane_mask, uint32_t __idx, uint32_t __x,
133+
uint32_t __width) {
134+
__builtin_unreachable();
135+
}
136+
137+
// Returns a bitmask marking all lanes that have the same value of __x.
138+
_DEFAULT_FN_ATTRS static __inline__ uint64_t
139+
__gpu_match_any_u32(uint64_t __lane_mask, uint32_t __x) {
140+
return __gpu_match_any_u32_impl(__lane_mask, __x);
141+
}
142+
143+
// Returns a bitmask marking all lanes that have the same value of __x.
144+
_DEFAULT_FN_ATTRS static __inline__ uint64_t
145+
__gpu_match_any_u64(uint64_t __lane_mask, uint64_t __x) {
146+
return __gpu_match_any_u64_impl(__lane_mask, __x);
147+
}
148+
149+
// Returns the current lane mask if every lane contains __x.
150+
_DEFAULT_FN_ATTRS static __inline__ uint64_t
151+
__gpu_match_all_u32(uint64_t __lane_mask, uint32_t __x) {
152+
return __gpu_match_all_u32_impl(__lane_mask, __x);
153+
}
154+
155+
// Returns the current lane mask if every lane contains __x.
156+
_DEFAULT_FN_ATTRS static __inline__ uint64_t
157+
__gpu_match_all_u64(uint64_t __lane_mask, uint64_t __x) {
158+
return __gpu_match_all_u64_impl(__lane_mask, __x);
159+
}
160+
161+
// Returns true if the flat pointer points to SPIRV 'shared' memory.
162+
_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_local(void *ptr) {
163+
return __builtin_spirv_is_shared((void [[clang::address_space(0)]] *)((
164+
void [[clang::opencl_generic]] *)ptr));
165+
}
166+
167+
// Returns true if the flat pointer points to SPIRV 'private' memory.
168+
_DEFAULT_FN_ATTRS static __inline__ bool __gpu_is_ptr_private(void *ptr) {
169+
return __builtin_spirv_is_private((void [[clang::address_space(0)]] *)((
170+
void [[clang::opencl_generic]] *)ptr));
171+
}
172+
173+
// Terminates execution of the associated wavefront.
174+
_DEFAULT_FN_ATTRS [[noreturn]] static __inline__ void __gpu_exit(void) {
175+
__builtin_unreachable();
176+
}
177+
178+
// Suspend the thread briefly to assist the scheduler during busy loops.
179+
_DEFAULT_FN_ATTRS static __inline__ void __gpu_thread_suspend(void) {
180+
// no op
181+
}
182+
183+
_Pragma("omp end declare variant");
184+
_Pragma("omp end declare target");
185+
186+
#endif // __SPIRVINTRIN_H
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
add_header_library(
2+
spirv64_timing
3+
HDRS
4+
timing.h
5+
DEPENDS
6+
libc.src.__support.common
7+
libc.src.__support.macros.config
8+
libc.src.__support.macros.attributes
9+
libc.src.__support.CPP.type_traits
10+
libc.src.__support.CPP.array
11+
)

0 commit comments

Comments
 (0)