interplane.Get Omega
Read in your language

omega-mv

Omega: a language for verified programs, semantic identity and machine realization. Seed-born work and existing implementations have distinct provenance.

Brought by drakestapleton

branch main Branches Tags History

omega-mv / seed / spirv / opencl-host.c

354 lines · 11438 bytes Raw Blame History
1// SPDX-License-Identifier: Apache-2.0
2// Verification/demo host only: runs one irspirv kernel on a PoCL CPU device.
3// It never generates IR or SPIR-V and is not part of the translator build.
4//
5// usage: opencl-host MODULE.spv KERNEL [ARG ...] (up to 6 canonical u64 decimals)
6// stdout: "VALUE STATUS\n" read back from outputs[0] and outputs[1].
7// Exit 0 only when every OpenCL call succeeded; any usage, environment, API or
8// runtime failure exits nonzero with a named stderr line and prints nothing on
9// stdout. Device and toolchain identity are recorded on stderr.
10//
11// GPU isolation: the build pins one absolute CPU OpenCL shared-library path and the
12// platform name it must report (default PoCL: libpocl.so, "Portable Computing
13// Language"; a vendor CPU runtime such as Intel's libintelocl.so may be pinned
14// instead with IRSPIRV_OCL_LIBRARY_BASE and IRSPIRV_OCL_PLATFORM). Inherited
15// vendor/layer discovery settings are overwritten before the first OpenCL call.
16// Only CL_DEVICE_TYPE_CPU devices are enumerated; POCL_DEVICES is forced to cpu.
17
18#define _POSIX_C_SOURCE 200809L
19#define CL_TARGET_OPENCL_VERSION 300
20#include <CL/cl.h>
21
22#include <errno.h>
23#include <stdint.h>
24#include <stdio.h>
25#include <stdlib.h>
26#include <string.h>
27#include <sys/stat.h>
28
29enum {
30 EXIT_USAGE = 2,
31 EXIT_ENVIRONMENT = 3,
32 EXIT_MODULE = 4,
33 EXIT_DEVICE = 5,
34 EXIT_PROGRAM = 6,
35 EXIT_KERNEL = 7,
36 EXIT_RUN = 8,
37};
38
39#define MAX_ARGS 6
40#define INFO_MAX 1024
41#define POISON UINT64_C(0xa5a5a5a5a5a5a5a5)
42#ifndef IRSPIRV_POCL_LIBRARY
43#define IRSPIRV_POCL_LIBRARY ""
44#endif
45#ifndef IRSPIRV_OCL_LIBRARY_BASE
46#define IRSPIRV_OCL_LIBRARY_BASE "libpocl.so"
47#endif
48#ifndef IRSPIRV_OCL_PLATFORM
49#define IRSPIRV_OCL_PLATFORM "Portable Computing Language"
50#endif
51
52static int fail(int code, const char *what, cl_int err)
53{
54 fprintf(stderr, "opencl-host: %s failed (cl error %d)\n", what, (int)err);
55 return code;
56}
57
58static int refuse(int code, const char *what)
59{
60 fprintf(stderr, "opencl-host: %s\n", what);
61 return code;
62}
63
64// Canonical unsigned decimal: digits only, no sign, no leading zero, fits 64 bits.
65static int parse_u64(const char *text, cl_ulong *out)
66{
67 size_t length = strlen(text);
68 if (length == 0 || length > 20 || (text[0] == '0' && length > 1))
69 return -1;
70 for (size_t i = 0; i < length; i++)
71 if (text[i] < '0' || text[i] > '9')
72 return -1;
73 errno = 0;
74 char *end = NULL;
75 unsigned long long value = strtoull(text, &end, 10);
76 if (errno != 0 || end != text + length)
77 return -1;
78 *out = (cl_ulong)value;
79 return 0;
80}
81
82// ocl-icd 2.3.2 accepts an absolute shared-library path in OCL_ICD_VENDORS.
83// A fixed library avoids both untrusted manifest paths and manifest re-open races.
84// The configured library and its dependency directories must be trusted installs.
85static int pin_loader(void)
86{
87 const char *library = IRSPIRV_POCL_LIBRARY;
88 if (library[0] != '/')
89 return refuse(EXIT_ENVIRONMENT, "build requires an absolute IRSPIRV_POCL_LIBRARY");
90 const char *base = strrchr(library, '/');
91 size_t base_len = strlen(IRSPIRV_OCL_LIBRARY_BASE);
92 if (base == NULL || base_len == 0 || strncmp(base + 1, IRSPIRV_OCL_LIBRARY_BASE, base_len) != 0)
93 return refuse(EXIT_ENVIRONMENT, "configured library is not " IRSPIRV_OCL_LIBRARY_BASE);
94 if (setenv("OCL_ICD_VENDORS", library, 1) != 0 ||
95 unsetenv("OCL_ICD_FILENAMES") != 0 || unsetenv("OPENCL_LAYERS") != 0 ||
96 unsetenv("OPENCL_VENDOR_PATH") != 0 ||
97 setenv("OPENCL_LAYER_PATH", "/dev/null", 1) != 0 ||
98 setenv("POCL_DEVICES", "cpu", 1) != 0)
99 return refuse(EXIT_ENVIRONMENT, "cannot pin loader environment");
100 fprintf(stderr, "opencl-host: library=%s POCL_DEVICES=cpu layers=disabled\n", library);
101 return 0;
102}
103
104static int platform_text(cl_platform_id platform, cl_platform_info key, char *out)
105{
106 size_t size = 0;
107 cl_int err = clGetPlatformInfo(platform, key, INFO_MAX, out, &size);
108 if (err != CL_SUCCESS || size == 0 || size > INFO_MAX)
109 return -1;
110 out[size - 1] = '\0';
111 return 0;
112}
113
114static int device_text(cl_device_id device, cl_device_info key, char *out)
115{
116 size_t size = 0;
117 cl_int err = clGetDeviceInfo(device, key, INFO_MAX, out, &size);
118 if (err != CL_SUCCESS || size == 0 || size > INFO_MAX)
119 return -1;
120 out[size - 1] = '\0';
121 return 0;
122}
123
124// Reads the whole module; SPIR-V is a nonempty sequence of 32-bit words.
125static unsigned char *read_module(const char *path, size_t *size)
126{
127 FILE *file = fopen(path, "rb");
128 if (file == NULL)
129 return NULL;
130 struct stat info;
131 if (fstat(fileno(file), &info) != 0 || !S_ISREG(info.st_mode) ||
132 info.st_size < 20 || info.st_size > 8388608 || info.st_size % 4 != 0) {
133 fclose(file);
134 return NULL;
135 }
136 size_t used = (size_t)info.st_size;
137 unsigned char *bytes = malloc(used);
138 if (bytes == NULL) {
139 fclose(file);
140 return NULL;
141 }
142 size_t got = fread(bytes, 1, used, file);
143 int bad = got != used || ferror(file) || fgetc(file) != EOF;
144 fclose(file);
145 if (bad) {
146 free(bytes);
147 return NULL;
148 }
149 *size = used;
150 return bytes;
151}
152
153static void print_build_log(cl_program program, cl_device_id device, int always)
154{
155 size_t size = 0;
156 if (clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, 0, NULL, &size) != CL_SUCCESS || size == 0)
157 return;
158 char *log = malloc(size);
159 if (log == NULL)
160 return;
161 if (clGetProgramBuildInfo(program, device, CL_PROGRAM_BUILD_LOG, size, log, NULL) == CL_SUCCESS) {
162 log[size - 1] = '\0';
163 if (always || log[strspn(log, " \t\r\n")] != '\0')
164 fprintf(stderr, "opencl-host: build log:\n%s\n", log);
165 }
166 free(log);
167}
168
169int main(int argc, char **argv)
170{
171 if (argc < 3 || argc > 3 + MAX_ARGS)
172 return refuse(EXIT_USAGE, "usage: opencl-host MODULE.spv KERNEL [u64 ...] (at most 6 arguments)");
173 cl_ulong inputs[MAX_ARGS] = { 0 };
174 for (int i = 3; i < argc; i++)
175 if (parse_u64(argv[i], &inputs[i - 3]) != 0) {
176 fprintf(stderr, "opencl-host: argument %d is not a canonical u64 decimal: %s\n", i - 2, argv[i]);
177 return EXIT_USAGE;
178 }
179 if (argv[2][0] == '\0')
180 return refuse(EXIT_USAGE, "empty kernel name");
181
182 int rc = pin_loader();
183 if (rc != 0)
184 return rc;
185
186 size_t il_size = 0;
187 unsigned char *il = read_module(argv[1], &il_size);
188 if (il == NULL)
189 return refuse(EXIT_MODULE, "cannot read module or size is not a nonzero multiple of 4 bytes");
190
191 cl_context context = NULL;
192 cl_command_queue queue = NULL;
193 cl_program program = NULL;
194 cl_kernel kernel = NULL;
195 cl_mem in_buffer = NULL, out_buffer = NULL;
196 cl_event done = NULL;
197 char text[INFO_MAX];
198 cl_int err;
199
200 cl_uint platforms = 0;
201 cl_platform_id platform;
202 err = clGetPlatformIDs(1, &platform, &platforms);
203 if (err != CL_SUCCESS) {
204 rc = fail(EXIT_DEVICE, "clGetPlatformIDs", err);
205 goto out;
206 }
207 if (platforms != 1) {
208 rc = refuse(EXIT_DEVICE, "expected exactly one (pinned) platform");
209 goto out;
210 }
211 if (platform_text(platform, CL_PLATFORM_NAME, text) != 0 || strcmp(text, IRSPIRV_OCL_PLATFORM) != 0) {
212 rc = refuse(EXIT_DEVICE, "platform is not " IRSPIRV_OCL_PLATFORM);
213 goto out;
214 }
215 fprintf(stderr, "opencl-host: platform=%s", text);
216 if (platform_text(platform, CL_PLATFORM_VERSION, text) == 0)
217 fprintf(stderr, " version=\"%s\"", text);
218 fputc('\n', stderr);
219
220 cl_uint devices = 0;
221 cl_device_id device;
222 err = clGetDeviceIDs(platform, CL_DEVICE_TYPE_CPU, 1, &device, &devices);
223 if (err != CL_SUCCESS) {
224 rc = fail(EXIT_DEVICE, "clGetDeviceIDs(CL_DEVICE_TYPE_CPU)", err);
225 goto out;
226 }
227 cl_device_type type = 0;
228 err = clGetDeviceInfo(device, CL_DEVICE_TYPE, sizeof type, &type, NULL);
229 if (err != CL_SUCCESS || type != CL_DEVICE_TYPE_CPU) {
230 rc = refuse(EXIT_DEVICE, "selected device is not a CPU");
231 goto out;
232 }
233 if (device_text(device, CL_DEVICE_IL_VERSION, text) != 0 || strstr(text, "SPIR-V_1.0") == NULL) {
234 rc = refuse(EXIT_DEVICE, "CPU device does not accept SPIR-V 1.0 IL");
235 goto out;
236 }
237 fprintf(stderr, "opencl-host: il=\"%s\"", text);
238 if (device_text(device, CL_DEVICE_NAME, text) == 0)
239 fprintf(stderr, " device=%s", text);
240 if (device_text(device, CL_DEVICE_VERSION, text) == 0)
241 fprintf(stderr, " device-version=\"%s\"", text);
242 if (device_text(device, CL_DRIVER_VERSION, text) == 0)
243 fprintf(stderr, " driver=%s", text);
244 fprintf(stderr, " cpu-devices=%u host-cc=\"%s\"\n", (unsigned)devices, __VERSION__);
245
246 context = clCreateContext(NULL, 1, &device, NULL, NULL, &err);
247 if (err != CL_SUCCESS) {
248 rc = fail(EXIT_DEVICE, "clCreateContext", err);
249 goto out;
250 }
251 queue = clCreateCommandQueueWithProperties(context, device, NULL, &err);
252 if (err != CL_SUCCESS) {
253 rc = fail(EXIT_DEVICE, "clCreateCommandQueueWithProperties", err);
254 goto out;
255 }
256
257 program = clCreateProgramWithIL(context, il, il_size, &err);
258 if (err != CL_SUCCESS) {
259 rc = fail(EXIT_PROGRAM, "clCreateProgramWithIL", err);
260 goto out;
261 }
262 err = clBuildProgram(program, 1, &device, "", NULL, NULL);
263 print_build_log(program, device, err != CL_SUCCESS);
264 if (err != CL_SUCCESS) {
265 rc = fail(EXIT_PROGRAM, "clBuildProgram", err);
266 goto out;
267 }
268
269 kernel = clCreateKernel(program, argv[2], &err);
270 if (err != CL_SUCCESS) {
271 rc = fail(EXIT_KERNEL, "clCreateKernel", err);
272 goto out;
273 }
274 cl_uint kernel_args = 0;
275 err = clGetKernelInfo(kernel, CL_KERNEL_NUM_ARGS, sizeof kernel_args, &kernel_args, NULL);
276 if (err != CL_SUCCESS) {
277 rc = fail(EXIT_KERNEL, "clGetKernelInfo(CL_KERNEL_NUM_ARGS)", err);
278 goto out;
279 }
280 if (kernel_args != 2) {
281 rc = refuse(EXIT_KERNEL, "kernel signature is not (inputs, outputs)");
282 goto out;
283 }
284
285 // All six input slots are always backed; unused slots are zero.
286 cl_ulong outputs[2] = { POISON, POISON };
287 in_buffer = clCreateBuffer(context, CL_MEM_READ_ONLY | CL_MEM_COPY_HOST_PTR, sizeof inputs, inputs, &err);
288 if (err != CL_SUCCESS) {
289 rc = fail(EXIT_RUN, "clCreateBuffer(inputs)", err);
290 goto out;
291 }
292 out_buffer = clCreateBuffer(context, CL_MEM_READ_WRITE | CL_MEM_COPY_HOST_PTR, sizeof outputs, outputs, &err);
293 if (err != CL_SUCCESS) {
294 rc = fail(EXIT_RUN, "clCreateBuffer(outputs)", err);
295 goto out;
296 }
297 err = clSetKernelArg(kernel, 0, sizeof in_buffer, &in_buffer);
298 if (err != CL_SUCCESS) {
299 rc = fail(EXIT_KERNEL, "clSetKernelArg(0)", err);
300 goto out;
301 }
302 err = clSetKernelArg(kernel, 1, sizeof out_buffer, &out_buffer);
303 if (err != CL_SUCCESS) {
304 rc = fail(EXIT_KERNEL, "clSetKernelArg(1)", err);
305 goto out;
306 }
307
308 size_t one = 1;
309 err = clEnqueueNDRangeKernel(queue, kernel, 1, NULL, &one, &one, 0, NULL, &done);
310 if (err != CL_SUCCESS) {
311 rc = fail(EXIT_RUN, "clEnqueueNDRangeKernel", err);
312 goto out;
313 }
314 err = clEnqueueReadBuffer(queue, out_buffer, CL_TRUE, 0, sizeof outputs, outputs, 1, &done, NULL);
315 if (err != CL_SUCCESS) {
316 rc = fail(EXIT_RUN, "clEnqueueReadBuffer", err);
317 goto out;
318 }
319 cl_int status = 0;
320 err = clGetEventInfo(done, CL_EVENT_COMMAND_EXECUTION_STATUS, sizeof status, &status, NULL);
321 if (err != CL_SUCCESS) {
322 rc = fail(EXIT_RUN, "clGetEventInfo", err);
323 goto out;
324 }
325 if (status != CL_COMPLETE) {
326 rc = fail(EXIT_RUN, "kernel execution", status);
327 goto out;
328 }
329
330 if (printf("%llu %llu\n", (unsigned long long)outputs[0], (unsigned long long)outputs[1]) < 0 ||
331 fflush(stdout) != 0) {
332 rc = refuse(EXIT_RUN, "cannot write result");
333 goto out;
334 }
335 rc = 0;
336
337out:
338 if (done)
339 clReleaseEvent(done);
340 if (out_buffer)
341 clReleaseMemObject(out_buffer);
342 if (in_buffer)
343 clReleaseMemObject(in_buffer);
344 if (kernel)
345 clReleaseKernel(kernel);
346 if (program)
347 clReleaseProgram(program);
348 if (queue)
349 clReleaseCommandQueue(queue);
350 if (context)
351 clReleaseContext(context);
352 free(il);
353 return rc;
354}