Skip to content

Commit 29fa88e

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

File tree

3 files changed

+49
-15
lines changed

3 files changed

+49
-15
lines changed

clang/include/clang/Basic/BuiltinsSPIRV.td

Lines changed: 26 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -41,83 +41,95 @@ def SPIRVSmoothStep : Builtin {
4141
def SPIRVGetNumWrkgrpX : Builtin {
4242
let Spellings = ["__builtin_spirv_get_num_workgroups_x"];
4343
let Attributes = [NoThrow, Const];
44-
let Prototype = "void(...)";
44+
let Prototype = "uint32_t()";
4545
}
4646

4747
def SPIRVGetNumWrkgrpY : Builtin {
4848
let Spellings = ["__builtin_spirv_get_num_workgroups_y"];
4949
let Attributes = [NoThrow, Const];
50-
let Prototype = "void(...)";
50+
let Prototype = "uint32_t()";
5151
}
5252

5353
def SPIRVGetNumWrkgrpZ : Builtin {
5454
let Spellings = ["__builtin_spirv_get_num_workgroups_z"];
5555
let Attributes = [NoThrow, Const];
56-
let Prototype = "void(...)";
56+
let Prototype = "uint32_t()";
5757
}
5858

5959
def SPIRVGetWrkgrpIdX : Builtin {
6060
let Spellings = ["__builtin_spirv_get_workgroup_id_x"];
6161
let Attributes = [NoThrow, Const];
62-
let Prototype = "void(...)";
62+
let Prototype = "uint32_t()";
6363
}
6464

6565
def SPIRVGetWrkgrpIdY : Builtin {
6666
let Spellings = ["__builtin_spirv_get_workgroup_id_y"];
6767
let Attributes = [NoThrow, Const];
68-
let Prototype = "void(...)";
68+
let Prototype = "uint32_t()";
6969
}
7070

7171
def SPIRVGetWrkgrpIdZ : Builtin {
7272
let Spellings = ["__builtin_spirv_get_workgroup_id_z"];
7373
let Attributes = [NoThrow, Const];
74-
let Prototype = "void(...)";
74+
let Prototype = "uint32_t()";
7575
}
7676

7777
def SPIRVGetWrkgrpSizeX : Builtin {
7878
let Spellings = ["__builtin_spirv_workgroup_size_x"];
7979
let Attributes = [NoThrow, Const];
80-
let Prototype = "void(...)";
80+
let Prototype = "uint32_t()";
8181
}
8282

8383
def SPIRVGetWrkgrpSizeY : Builtin {
8484
let Spellings = ["__builtin_spirv_workgroup_size_y"];
8585
let Attributes = [NoThrow, Const];
86-
let Prototype = "void(...)";
86+
let Prototype = "uint32_t()";
8787
}
8888

8989
def SPIRVGetWrkgrpSizeZ : Builtin {
9090
let Spellings = ["__builtin_spirv_workgroup_size_z"];
9191
let Attributes = [NoThrow, Const];
92-
let Prototype = "void(...)";
92+
let Prototype = "uint32_t()";
9393
}
9494

9595
def SPIRVGetWrkitemIdX : Builtin {
9696
let Spellings = ["__builtin_spirv_workitem_id_x"];
9797
let Attributes = [NoThrow, Const];
98-
let Prototype = "void(...)";
98+
let Prototype = "uint32_t()";
9999
}
100100

101101
def SPIRVGetWrkitemIdY : Builtin {
102102
let Spellings = ["__builtin_spirv_workitem_id_y"];
103103
let Attributes = [NoThrow, Const];
104-
let Prototype = "void(...)";
104+
let Prototype = "uint32_t()";
105105
}
106106

107107
def SPIRVGetWrkitemIdZ : Builtin {
108108
let Spellings = ["__builtin_spirv_workitem_id_z"];
109109
let Attributes = [NoThrow, Const];
110-
let Prototype = "void(...)";
110+
let Prototype = "uint32_t()";
111111
}
112112

113113
def SPIRVBallot : Builtin {
114114
let Spellings = ["__builtin_spirv_ballot"];
115115
let Attributes = [NoThrow, Const];
116-
let Prototype = "void(...)";
116+
let Prototype = "uint64_t(bool)";
117117
}
118118

119119
def SPIRVSyncThreads : Builtin {
120120
let Spellings = ["__builtin_spirv_sync_threads"];
121121
let Attributes = [NoThrow, Const];
122-
let Prototype = "void(...)";
122+
let Prototype = "void()";
123+
}
124+
125+
def SPIRVIsShared : Builtin {
126+
let Spellings = ["__builtin_spirv_is_shared"];
127+
let Attributes = [NoThrow, Const];
128+
let Prototype = "bool(void*)";
129+
}
130+
131+
def SPIRVIsPrivate : Builtin {
132+
let Spellings = ["__builtin_spirv_is_private"];
133+
let Attributes = [NoThrow, Const];
134+
let Prototype = "bool(void*)";
123135
}

clang/lib/Headers/spirvintrin.h

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
_Pragma("omp begin declare target device_type(nohost)");
2121
_Pragma("omp begin declare variant match(device = {arch(amdgcn)})");
2222

23-
// Type aliases to the address spaces used by the SPIRV backend.
23+
// Type aliases to the address spaces used by the SPIR-V backend.
2424
#define __gpu_private __attribute__((address_space(5)))
2525
#define __gpu_constant __attribute__((address_space(4)))
2626
#define __gpu_local __attribute__((address_space(3)))
@@ -30,6 +30,25 @@ _Pragma("omp begin declare variant match(device = {arch(amdgcn)})");
3030
// Attribute to declare a function as a kernel.
3131
#define __gpu_kernel __attribute__((spir_kernel, visibility("protected")))
3232

33+
extern "C" {
34+
uint32_t __builtin_spirv_get_num_workgroups_x();
35+
uint32_t __builtin_spirv_get_num_workgroups_y();
36+
uint32_t __builtin_spirv_get_num_workgroups_z();
37+
uint32_t __builtin_spirv_get_workgroup_id_x();
38+
uint32_t __builtin_spirv_get_workgroup_id_y();
39+
uint32_t __builtin_spirv_get_workgroup_id_z();
40+
uint32_t __builtin_spirv_workgroup_size_x();
41+
uint32_t __builtin_spirv_workgroup_size_y();
42+
uint32_t __builtin_spirv_workgroup_size_z();
43+
uint32_t __builtin_spirv_workitem_id_x();
44+
uint32_t __builtin_spirv_workitem_id_y();
45+
uint32_t __builtin_spirv_workitem_id_z();
46+
uint64_t __builtin_spirv_ballot(bool);
47+
void __builtin_spirv_sync_threads();
48+
bool __builtin_spirv_is_shared(void [[clang::address_space(0)]] *);
49+
bool __builtin_spirv_is_private(void [[clang::address_space(0)]] *);
50+
}
51+
3352
// Returns the number of workgroups in the 'x' dimension of the grid.
3453
_DEFAULT_FN_ATTRS static __inline__ uint32_t __gpu_num_blocks_x(void) {
3554
return __builtin_spirv_get_num_workgroups_x();

libc/src/__support/time/gpu/time_utils.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,9 @@ extern gpu::Constant<uint64_t> __llvm_libc_clock_freq;
3434
#elif defined(LIBC_TARGET_ARCH_IS_NVPTX)
3535
// NPVTX uses a single 1 GHz fixed frequency clock for all target architectures.
3636
#define GPU_CLOCKS_PER_SEC static_cast<clock_t>(1000000000UL)
37+
#elif defined(LIBC_TARGET_ARCH_IS_SPIRV)
38+
// NPVTX uses a single 1 GHz fixed frequency clock for all target architectures.
39+
#define GPU_CLOCKS_PER_SEC static_cast<clock_t>(1000000000UL)
3740
#else
3841
#error "Unsupported target"
3942
#endif

0 commit comments

Comments
 (0)