CCCC
C bytecode compiler + VM interpreter
Loading...
Searching...
No Matches
opencl.h
Go to the documentation of this file.
1/*
2 CCCC: Comprehensiev C Compensation Compiler - OpenCL C Prelude
3
4 Copyright (C) 2025 George Watson
5
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, either version 3 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program. If not, see <https://www.gnu.org/licenses/>.
18*/
19
27#ifndef CCCC_OPENCL_H
28#define CCCC_OPENCL_H
29
30#include <cccc/kernel.h>
31
34#define __OPENCL_C_VERSION__ 120
35#define __OPENCL_VERSION__ 120
36#define CL_VERSION_1_0 100
37#define CL_VERSION_1_1 110
38#define CL_VERSION_1_2 120
39#define __ENDIAN_LITTLE__ 1
44// The bare spellings are reserved words in OpenCL C, so they are
45// macros here and cannot be used as identifiers. The bodies use the
46// underscored attribute names so that the macros do not expand themselves.
47#define __kernel [[cccc::__kernel__]]
48#define kernel [[cccc::__kernel__]]
49#define __global [[cccc::__global__]]
50#define global [[cccc::__global__]]
51#define __local [[cccc::__local__]]
52#define local [[cccc::__local__]]
53#define __constant [[cccc::__constant__]]
54#define constant [[cccc::__constant__]]
55#define __private [[cccc::__private__]]
56#define private [[cccc::__private__]]
61typedef unsigned char uchar;
62typedef unsigned short ushort;
63typedef unsigned int uint;
64typedef unsigned long ulong;
67// A cast's type-name cannot start with an address-space attribute
68typedef [[cccc::__generic__]] unsigned __cl_generic_uint;
69
70// Vector types of 16, 32 and 64 bytes. A 3-lane type is stored as four lanes
71// and exposes three. Narrower ones (float2, int2, char4, ...) do not exist
72// yet. char is signed here whatever the target's char is.
73#define __CL_VECTOR(base, name, bytes) \
74 typedef base name __attribute__((vector_size(bytes)))
75#define __CL_VECTOR3(base, name, bytes) \
76 typedef base name __attribute__((vector_size(bytes), cccc_visible_lanes(3)))
77
78__CL_VECTOR(signed char, char16, 16);
79__CL_VECTOR(unsigned char, uchar16, 16);
80__CL_VECTOR(short, short8, 16);
81__CL_VECTOR(short, short16, 32);
82__CL_VECTOR(unsigned short, ushort8, 16);
83__CL_VECTOR(unsigned short, ushort16, 32);
84__CL_VECTOR3(int, int3, 16);
85__CL_VECTOR(int, int4, 16);
86__CL_VECTOR(int, int8, 32);
87__CL_VECTOR(int, int16, 64);
88__CL_VECTOR3(unsigned int, uint3, 16);
89__CL_VECTOR(unsigned int, uint4, 16);
90__CL_VECTOR(unsigned int, uint8, 32);
91__CL_VECTOR(unsigned int, uint16, 64);
92__CL_VECTOR(long, long2, 16);
93__CL_VECTOR3(long, long3, 32);
94__CL_VECTOR(long, long4, 32);
95__CL_VECTOR(long, long8, 64);
96__CL_VECTOR(unsigned long, ulong2, 16);
97__CL_VECTOR3(unsigned long, ulong3, 32);
98__CL_VECTOR(unsigned long, ulong4, 32);
99__CL_VECTOR(unsigned long, ulong8, 64);
100__CL_VECTOR3(float, float3, 16);
101__CL_VECTOR(float, float4, 16);
102__CL_VECTOR(float, float8, 32);
103__CL_VECTOR(float, float16, 64);
104
108#define get_work_dim() cccc_work_dim()
109#define get_global_id(d) cccc_global_id(d)
110#define get_local_id(d) cccc_local_id(d)
111#define get_group_id(d) cccc_group_id(d)
112#define get_global_size(d) cccc_global_size(d)
113#define get_local_size(d) cccc_local_size(d)
114#define get_num_groups(d) cccc_num_groups(d)
115// TODO: cccc_launch has no offset, so this is always 0; #1413
116#define get_global_offset(d) ((size_t)0)
121#define CLK_LOCAL_MEM_FENCE CCCC_LOCAL_FENCE
122#define CLK_GLOBAL_MEM_FENCE CCCC_GLOBAL_FENCE
123
124#define barrier(flags) cccc_barrier(flags)
125#define mem_fence(flags) __atomic_thread_fence(__ATOMIC_SEQ_CST)
126#define read_mem_fence(flags) __atomic_thread_fence(__ATOMIC_SEQ_CST)
127#define write_mem_fence(flags) __atomic_thread_fence(__ATOMIC_SEQ_CST)
134#define atomic_add(p, v) __atomic_fetch_add(p, v, __ATOMIC_SEQ_CST)
135#define atomic_sub(p, v) __atomic_fetch_sub(p, v, __ATOMIC_SEQ_CST)
136#define atomic_and(p, v) __atomic_fetch_and(p, v, __ATOMIC_SEQ_CST)
137#define atomic_or(p, v) __atomic_fetch_or(p, v, __ATOMIC_SEQ_CST)
138#define atomic_xor(p, v) __atomic_fetch_xor(p, v, __ATOMIC_SEQ_CST)
139#define atomic_inc(p) __atomic_fetch_add(p, 1, __ATOMIC_SEQ_CST)
140#define atomic_dec(p) __atomic_fetch_sub(p, 1, __ATOMIC_SEQ_CST)
141
142// Return the old value. typeof_unqual keeps the temporary out of the pointee's
143// address space.
144//
145// atomic_xchg also takes a float, which no atomic builtin does: the float case
146// exchanges its bits through an unsigned view of the pointer. Only the
147// selected _Generic branch is compiled.
148#define __CL_XCHG_F32(p, val) \
149 ({ \
150 union { \
151 float f; \
152 unsigned u; \
153 } __cl_in = {.f = (val)}, __cl_out; \
154 __cl_out.u = __atomic_exchange_n((__cl_generic_uint *)(p), __cl_in.u, \
155 __ATOMIC_SEQ_CST); \
156 __cl_out.f; \
157 })
158#define atomic_xchg(p, val) \
159 _Generic(*(p), \
160 float: __CL_XCHG_F32(p, val), \
161 default: __atomic_exchange_n(p, val, __ATOMIC_SEQ_CST))
162#define atomic_cmpxchg(p, cmp, val) \
163 ({ \
164 auto __cl_p = (p); \
165 typeof_unqual(*__cl_p) __cl_e = (cmp); \
166 __atomic_compare_exchange_n(__cl_p, &__cl_e, (val), 0, \
167 __ATOMIC_SEQ_CST, __ATOMIC_SEQ_CST); \
168 __cl_e; \
169 })
170#define __CL_ATOMIC_EXTREMUM(p, val, stops) \
171 ({ \
172 auto __cl_p = (p); \
173 typeof_unqual(*__cl_p) __cl_v = (val); \
174 typeof_unqual(*__cl_p) __cl_o = \
175 __atomic_load_n(__cl_p, __ATOMIC_SEQ_CST); \
176 while (stops && !__atomic_compare_exchange_n(__cl_p, &__cl_o, __cl_v, \
177 0, __ATOMIC_SEQ_CST, \
178 __ATOMIC_SEQ_CST)) { \
179 } \
180 __cl_o; \
181 })
182#define atomic_min(p, val) __CL_ATOMIC_EXTREMUM(p, val, __cl_v < __cl_o)
183#define atomic_max(p, val) __CL_ATOMIC_EXTREMUM(p, val, __cl_v > __cl_o)
184
185// OpenCL 1.0 extension spellings
186#define atom_add atomic_add
187#define atom_sub atomic_sub
188#define atom_and atomic_and
189#define atom_or atomic_or
190#define atom_xor atomic_xor
191#define atom_xchg atomic_xchg
192#define atom_inc atomic_inc
193#define atom_dec atomic_dec
194#define atom_cmpxchg atomic_cmpxchg
195#define atom_min atomic_min
196#define atom_max atomic_max
199#endif
Work-item builtins and the launch builtin for [[cccc::kernel]] functions. See docs/kernels....
#define __CL_VECTOR3(base, name, bytes)
Definition opencl.h:75
unsigned __cl_generic_uint
Definition opencl.h:68
unsigned char uchar
Definition opencl.h:61
#define __CL_VECTOR(base, name, bytes)
Definition opencl.h:73
unsigned long ulong
Definition opencl.h:64
unsigned int uint
Definition opencl.h:63
unsigned short ushort
Definition opencl.h:62