File: misc.c

package info (click to toggle)
pocl 6.0-7
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 25,320 kB
  • sloc: lisp: 149,513; ansic: 103,778; cpp: 54,947; python: 1,513; sh: 949; ruby: 255; pascal: 226; tcl: 180; makefile: 175; java: 72; xml: 49
file content (669 lines) | stat: -rw-r--r-- 20,235 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
/**
 * \brief poclu_misc - misc generic OpenCL helper functions

   Copyright (c) 2013 Pekka Jääskeläinen / Tampere University of Technology
                 2014 Kalle Raiskila
                 2023-2024 Pekka Jääskeläinen / Intel Finland Oy

   Permission is hereby granted, free of charge, to any person obtaining a copy
   of this software and associated documentation files (the "Software"), to
 deal in the Software without restriction, including without limitation the
 rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
 sell copies of the Software, and to permit persons to whom the Software is
   furnished to do so, subject to the following conditions:

   The above copyright notice and this permission notice shall be included in
   all copies or substantial portions of the Software.

   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
   IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
   FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
   AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
   LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 IN THE SOFTWARE.

   \file
*/

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>

#include "config.h"

#include "pocl_opencl.h"

cl_context
poclu_create_any_context2 (cl_platform_id *platform)
{
  cl_uint i;

  clGetPlatformIDs (1, platform, &i);
  if (i == 0)
    return (cl_context) 0;

  cl_context_properties properties[]
    = { CL_CONTEXT_PLATFORM, (cl_context_properties)(*platform), 0 };

  // create the OpenCL context on any available OCL device
  cl_context context = clCreateContextFromType (properties,
                                                CL_DEVICE_TYPE_ALL,
                                                NULL, NULL, NULL);

  return context;
}

cl_context
poclu_create_any_context ()
{
  cl_platform_id platform = NULL;
  return poclu_create_any_context2 (&platform);
}

cl_int
poclu_get_any_device (cl_context *context, cl_device_id *device,
                      cl_command_queue *queue)
{
  cl_platform_id temp;
  return poclu_get_any_device2 (context, device, queue, &temp);
}

cl_int
poclu_get_any_device2 (cl_context *context, cl_device_id *device,
                       cl_command_queue *queue, cl_platform_id *platform)
{
  cl_int err;

  if (context == NULL ||
      device  == NULL ||
      queue   == NULL ||
      platform == NULL)
    return CL_INVALID_VALUE;

  err = clGetPlatformIDs (1, platform, NULL);
  if (err != CL_SUCCESS)
    return err;

  err = clGetDeviceIDs (*platform, CL_DEVICE_TYPE_ALL, 1, device, NULL);
  if (err != CL_SUCCESS)
    {

      err = clGetDeviceIDs (*platform, CL_DEVICE_TYPE_CUSTOM, 1, device, NULL);
      if (err != CL_SUCCESS)
        {
          return err;
        }
    }

  *context = clCreateContext (NULL, 1, device, NULL, NULL, &err);
  if (err != CL_SUCCESS)
    return err;

  *queue = clCreateCommandQueue (*context, *device,
                                 CL_QUEUE_PROFILING_ENABLE, &err);
  if (err != CL_SUCCESS)
    return err;

  return CL_SUCCESS;
}

cl_int
poclu_get_multiple_devices (cl_platform_id *platform, cl_context *context,
                            cl_char include_custom_dev, cl_uint *num_devices,
                            cl_device_id **devices, cl_command_queue **queues,
                            int ooo_queues)
{
  cl_int err;
  cl_uint num_dev_all = 0;
  cl_uint num_dev_custom = 0;
  cl_device_id *devs = NULL;
  cl_command_queue *ques = NULL;
  size_t i;

  if (context == NULL || devices == NULL || queues == NULL || platform == NULL)
    return CL_INVALID_VALUE;

  err = clGetPlatformIDs (1, platform, NULL);
  if (err != CL_SUCCESS)
    goto ERROR;

  err = clGetDeviceIDs (*platform, CL_DEVICE_TYPE_ALL, 0, NULL, &num_dev_all);

  // if custom devices are excluded, continue as normal
  if (include_custom_dev != 1)
    {
      if (err != CL_SUCCESS)
        goto ERROR;
    }
  else
    {
      cl_int err_custom;
      err_custom = clGetDeviceIDs (*platform, CL_DEVICE_TYPE_CUSTOM, 0, NULL, &num_dev_custom);

      if (err != CL_SUCCESS && err_custom != CL_SUCCESS)
        {
          err = err_custom;
          goto ERROR;
        }
      if (err != CL_SUCCESS)
        {
          num_dev_all = 0;
        }
      if (err_custom != CL_SUCCESS)
        {
          num_dev_custom = 0;
        }

    }

  *num_devices = num_dev_all + num_dev_custom;

  devs = (cl_device_id *)calloc (*num_devices, sizeof (cl_device_id));
  ques = (cl_command_queue *)calloc (*num_devices, sizeof (cl_command_queue));

  if (num_dev_all != 0)
    {
      err = clGetDeviceIDs (*platform, CL_DEVICE_TYPE_ALL, num_dev_all, devs,
                            NULL);
      if (err != CL_SUCCESS)
        goto ERROR;
    }

  if (num_dev_custom != 0)
    {
      // add devs to the end of the array
      err = clGetDeviceIDs (*platform, CL_DEVICE_TYPE_CUSTOM, num_dev_custom, &devs[num_dev_all],
                            NULL);
      if (err != CL_SUCCESS)
        goto ERROR;
    }

  *context = clCreateContext (NULL, *num_devices, devs, NULL, NULL, &err);
  if (err != CL_SUCCESS)
    goto ERROR;

  cl_command_queue_properties props = CL_QUEUE_PROFILING_ENABLE;
  if (ooo_queues)
    props |= CL_QUEUE_OUT_OF_ORDER_EXEC_MODE_ENABLE;
  for (i = 0; i < *num_devices; ++i)
    {
      ques[i] = clCreateCommandQueue (*context, devs[i], props, &err);
      if (err != CL_SUCCESS)
        goto ERROR;
    }

  *devices = devs;
  *queues = ques;
  return CL_SUCCESS;

ERROR:
  free (devs);
  free (ques);
  return err;
}

char *
poclu_read_binfile (const char *filename, size_t *len)
{
  FILE *file;
  char *src;

  file = fopen (filename, "r");
  if (file == NULL)
    return NULL;

  fseek (file, 0, SEEK_END);
  *len = ftell (file);
  src = (char*)malloc (*len + 1);
  if (src == NULL)
    {
      fclose (file);
      return NULL;
    }

  fseek (file, 0, SEEK_SET);
  fread (src, *len, 1, file);
  fclose (file);

  return src;
}

char *
poclu_read_file (const char *filename)
{
  size_t size;
  char *res = poclu_read_binfile (filename, &size);
  if (res)
    res[size] = 0;
  return res;
}

int
poclu_write_file (const char *filename, char *content, size_t size)
{
  FILE *file;

  file = fopen (filename, "w");
  if (file == NULL)
    return -1;

  if (fwrite (content, sizeof (char), size, file) < size)
  {
    fclose (file);
    return -1;
  }

  if (fclose (file))
    return -1;

  return 0;
}

int
poclu_supports_opencl_30 (cl_device_id *devices, unsigned num_devices)
{
  if (num_devices == 0)
    return 0;

  unsigned supported = 0;
  char dev_version[256];
  size_t string_len;
  for (unsigned i = 0; i < num_devices; ++i)
    {
    int err = clGetDeviceInfo (devices[i], CL_DEVICE_VERSION,
                               sizeof (dev_version), dev_version, &string_len);
    TEST_ASSERT (err == CL_SUCCESS);
    if ((string_len >= 15)
        && (strncmp (dev_version, "OpenCL 3.0 PoCL", 15) == 0))
        ++supported;
    }
  return (supported == num_devices);
}

int
poclu_supports_extension (cl_device_id dev, const char *ext)
{
  char extensions[8192];
  int err;
  size_t real_size = 0;
  err = clGetDeviceInfo (dev, CL_DEVICE_EXTENSIONS, 0, NULL, &real_size);
  TEST_ASSERT (err == CL_SUCCESS);
  TEST_ASSERT (real_size > 0);
  TEST_ASSERT (real_size < 8192);
  err = clGetDeviceInfo (dev, CL_DEVICE_EXTENSIONS, real_size, extensions,
                         NULL);
  TEST_ASSERT (err == CL_SUCCESS);
  extensions[real_size] = 0;
  return (strstr (extensions, ext) != NULL);
}

cl_int
poclu_show_program_build_log (cl_program program)
{
  cl_int err;
  cl_uint num_devices;
  cl_device_id *devices;

  err = clGetProgramInfo (program, CL_PROGRAM_NUM_DEVICES,
                          sizeof (num_devices), &num_devices, NULL);
  if (err != CL_SUCCESS)
    goto fail;

  devices = (cl_device_id *)malloc (sizeof (cl_device_id) * num_devices);
  if (!devices)
    {
      err = CL_OUT_OF_HOST_MEMORY;
      goto fail;
    }

  err = clGetProgramInfo (program, CL_PROGRAM_DEVICES,
                          sizeof (cl_device_id) * num_devices, devices, NULL);
  if (err != CL_SUCCESS)
    goto fail2;

  for (cl_uint i = 0; i < num_devices; ++i)
    {
      char *log;
      size_t log_size;

      err = clGetProgramBuildInfo (program, devices[i], CL_PROGRAM_BUILD_LOG,
                                   0, NULL, &log_size);
      if (err != CL_SUCCESS)
        goto fail2;

      log = (char *)malloc (log_size);
      if (!log)
        {
          err = CL_OUT_OF_HOST_MEMORY;
          goto fail2;
        }

      err = clGetProgramBuildInfo (program, devices[i], CL_PROGRAM_BUILD_LOG,
                                   log_size, log, NULL);
      if (err != CL_SUCCESS)
        {
          free (log);
          goto fail2;
        }

      if (num_devices > 1)
        fprintf (stderr, "device %d/%d:\n", i, num_devices);
      fprintf (stderr, "%s\n", log);

      free (log);
    }

  err = CL_SUCCESS;

fail2:
  free (devices);

fail:
  check_cl_error (err, __LINE__, __PRETTY_FUNCTION__);
  return err;
}

/**
 * \brief private macro to check errors in check_cl_error function
 */
#define OPENCL_ERROR_CASE(ERR) \
  case ERR:                                                             \
  { fprintf (stderr, "" #ERR " in %s on line %i\n", func_name, line);   \
    return 1; }

int
check_cl_error (cl_int cl_err, int line, const char* func_name) {

  switch (cl_err)
    {
    case CL_SUCCESS: return 0;

      OPENCL_ERROR_CASE (CL_DEVICE_NOT_FOUND)
        OPENCL_ERROR_CASE (CL_DEVICE_NOT_AVAILABLE)
        OPENCL_ERROR_CASE (CL_COMPILER_NOT_AVAILABLE)
        OPENCL_ERROR_CASE (CL_MEM_OBJECT_ALLOCATION_FAILURE)
        OPENCL_ERROR_CASE (CL_OUT_OF_RESOURCES)
        OPENCL_ERROR_CASE (CL_OUT_OF_HOST_MEMORY)
        OPENCL_ERROR_CASE (CL_PROFILING_INFO_NOT_AVAILABLE)
        OPENCL_ERROR_CASE (CL_MEM_COPY_OVERLAP)
        OPENCL_ERROR_CASE (CL_IMAGE_FORMAT_MISMATCH)
        OPENCL_ERROR_CASE (CL_IMAGE_FORMAT_NOT_SUPPORTED)
        OPENCL_ERROR_CASE (CL_BUILD_PROGRAM_FAILURE)
        OPENCL_ERROR_CASE (CL_MAP_FAILURE)
        OPENCL_ERROR_CASE (CL_MISALIGNED_SUB_BUFFER_OFFSET)
        OPENCL_ERROR_CASE (CL_EXEC_STATUS_ERROR_FOR_EVENTS_IN_WAIT_LIST)
        OPENCL_ERROR_CASE (CL_COMPILE_PROGRAM_FAILURE)
        OPENCL_ERROR_CASE (CL_LINKER_NOT_AVAILABLE)
        OPENCL_ERROR_CASE (CL_LINK_PROGRAM_FAILURE)
        OPENCL_ERROR_CASE (CL_DEVICE_PARTITION_FAILED)
        OPENCL_ERROR_CASE (CL_KERNEL_ARG_INFO_NOT_AVAILABLE)
        OPENCL_ERROR_CASE (CL_INVALID_VALUE)
        OPENCL_ERROR_CASE (CL_INVALID_DEVICE_TYPE)
        OPENCL_ERROR_CASE (CL_INVALID_PLATFORM)
        OPENCL_ERROR_CASE (CL_INVALID_DEVICE)
        OPENCL_ERROR_CASE (CL_INVALID_CONTEXT)
        OPENCL_ERROR_CASE (CL_INVALID_QUEUE_PROPERTIES)
        OPENCL_ERROR_CASE (CL_INVALID_COMMAND_QUEUE)
        OPENCL_ERROR_CASE (CL_INVALID_HOST_PTR)
        OPENCL_ERROR_CASE (CL_INVALID_MEM_OBJECT)
        OPENCL_ERROR_CASE (CL_INVALID_IMAGE_FORMAT_DESCRIPTOR)
        OPENCL_ERROR_CASE (CL_INVALID_IMAGE_SIZE)
        OPENCL_ERROR_CASE (CL_INVALID_SAMPLER)
        OPENCL_ERROR_CASE (CL_INVALID_BINARY)
        OPENCL_ERROR_CASE (CL_INVALID_BUILD_OPTIONS)
        OPENCL_ERROR_CASE (CL_INVALID_PROGRAM)
        OPENCL_ERROR_CASE (CL_INVALID_PROGRAM_EXECUTABLE)
        OPENCL_ERROR_CASE (CL_INVALID_KERNEL_NAME)
        OPENCL_ERROR_CASE (CL_INVALID_KERNEL_DEFINITION)
        OPENCL_ERROR_CASE (CL_INVALID_KERNEL)
        OPENCL_ERROR_CASE (CL_INVALID_ARG_INDEX)
        OPENCL_ERROR_CASE (CL_INVALID_ARG_VALUE)
        OPENCL_ERROR_CASE (CL_INVALID_ARG_SIZE)
        OPENCL_ERROR_CASE (CL_INVALID_KERNEL_ARGS)
        OPENCL_ERROR_CASE (CL_INVALID_WORK_DIMENSION)
        OPENCL_ERROR_CASE (CL_INVALID_WORK_GROUP_SIZE)
        OPENCL_ERROR_CASE (CL_INVALID_WORK_ITEM_SIZE)
        OPENCL_ERROR_CASE (CL_INVALID_GLOBAL_OFFSET)
        OPENCL_ERROR_CASE (CL_INVALID_EVENT_WAIT_LIST)
        OPENCL_ERROR_CASE (CL_INVALID_EVENT)
        OPENCL_ERROR_CASE (CL_INVALID_OPERATION)
        OPENCL_ERROR_CASE (CL_INVALID_GL_OBJECT)
        OPENCL_ERROR_CASE (CL_INVALID_BUFFER_SIZE)
        OPENCL_ERROR_CASE (CL_INVALID_MIP_LEVEL)
        OPENCL_ERROR_CASE (CL_INVALID_GLOBAL_WORK_SIZE)
        OPENCL_ERROR_CASE (CL_INVALID_PROPERTY)
        OPENCL_ERROR_CASE (CL_INVALID_IMAGE_DESCRIPTOR)
        OPENCL_ERROR_CASE (CL_INVALID_COMPILER_OPTIONS)
        OPENCL_ERROR_CASE (CL_INVALID_LINKER_OPTIONS)
        OPENCL_ERROR_CASE (CL_INVALID_DEVICE_PARTITION_COUNT)
        OPENCL_ERROR_CASE (CL_PLATFORM_NOT_FOUND_KHR)

    default:
      printf ("Unknown OpenCL error %i in %s on line %i\n", cl_err, func_name,
              line);
      return 1;
    }
}

/**
 * \brief get the path of a source file.
 *
 * can be either used by passing the explicit_binary option
 * or using basename + ext arguments.
 * if basename + ext is not found in current working directory,
 * it will check the build or source directories for said file.
 * @param path [out] the return string of the path.
 * @param len [in] the lenght of the destination path.
 * @param explicit_binary [in] string to the file.
 * @param basename [in] relative name of the program c program running.
 * @param ext [in] extension of the file.
 * @return CL_SUCCESS or CL_BUILD_PROGRAM_FAILURE on error.
 */
static int
pocl_getpath (char *path, size_t len, const char *explicit_binary,
              const char *basename, const char *ext)
{
  if (explicit_binary)
    {
      snprintf (path, len, "%s", explicit_binary);
      return CL_SUCCESS;
    }

  snprintf (path, len, "%s%s", basename, ext);
  if (access (path, F_OK) == 0)
    return CL_SUCCESS;

  snprintf (path, len, "%s/%s%s", BUILDDIR, basename, ext);
  if (access (path, F_OK) == 0)
    return CL_SUCCESS;

  snprintf (path, len, "%s/%s%s", SRCDIR, basename, ext);
  if (access (path, F_OK) == 0)
    return CL_SUCCESS;

  snprintf (path, len, "%s/tests/%s%s", SRCDIR, basename, ext);
  if (access (path, F_OK) == 0)
    return CL_SUCCESS;

  snprintf (path, len, "%s/examples/%s/%s%s", SRCDIR, basename, basename, ext);
  if (access (path, F_OK) == 0)
    return CL_SUCCESS;

  fprintf (stderr,
           "Can't find %s%s SPIR / PoCLbin file in BUILDDIR, SRCDIR/examples "
           "or current dir.\n",
           basename, ext);
  return CL_BUILD_PROGRAM_FAILURE;
}

int
poclu_load_program_multidev (cl_context context, cl_device_id *devices,
                             cl_uint num_devices, const char *basename,
                             int spirv, int poclbin,
                             const char *explicit_binary,
                             const char *extra_build_opts, cl_program *p)
{
  cl_bool little_endian = 0;
  cl_uint address_bits = 0;
  char extensions[1024];
  char path[1024];
  const char *ext;
  char final_opts[2048];
  size_t binary_size = 0;
  char *binary = NULL;
  cl_program program;
  cl_int err = CL_SUCCESS;

  int from_source = (!spirv && !poclbin);
  TEST_ASSERT (num_devices > 0);
  cl_device_id device = devices[0];
  if (!from_source)
    {
      // Check that all the devices have the same name.
      // This is to prevent loading the same binary to
      // different device types.
      size_t device0_name_length = 0;
      err = clGetDeviceInfo (devices[0], CL_DEVICE_NAME, 0, NULL,
                             &device0_name_length);
      CHECK_OPENCL_ERROR_IN ("clGetDeviceInfo name size");
      char *device0_name = malloc (device0_name_length);
      TEST_ASSERT (device0_name);
      err = clGetDeviceInfo (devices[0], CL_DEVICE_NAME, device0_name_length,
                             device0_name, NULL);
      CHECK_OPENCL_ERROR_IN ("clGetDeviceInfo name");
      for (unsigned i = 1; i < num_devices; i++)
        {
          size_t deviceN_name_length = 0;
          err = clGetDeviceInfo (devices[i], CL_DEVICE_NAME, 0, NULL,
                                 &deviceN_name_length);
          CHECK_OPENCL_ERROR_IN ("clGetDeviceInfo name size");
          char *deviceN_name = malloc (deviceN_name_length);
          TEST_ASSERT (deviceN_name);
          err = clGetDeviceInfo (devices[i], CL_DEVICE_NAME,
                                 deviceN_name_length, deviceN_name, NULL);
          CHECK_OPENCL_ERROR_IN ("clGetDeviceInfo name");

          TEST_ASSERT (!strcmp (device0_name, deviceN_name)
                       && "Trying to load the same binary/IL for different types of devices");
          free (deviceN_name);
        }
      free (device0_name);
    }

  *p = NULL;
  final_opts[0] = 0;
  if (extra_build_opts != NULL)
    strncat(final_opts, extra_build_opts, 2047);

  if (spirv)
    {
      TEST_ASSERT (device != NULL);
      err = clGetDeviceInfo (device, CL_DEVICE_EXTENSIONS, 1024, extensions,
                             NULL);
      CHECK_OPENCL_ERROR_IN ("clGetDeviceInfo extensions");

      if (spirv && strstr (extensions, "cl_khr_il_program") == NULL)
        {
          printf ("SPIR-V not supported, cannot run the test\n");
          return -1;
        }

      err = clGetDeviceInfo (device, CL_DEVICE_ENDIAN_LITTLE, sizeof (cl_bool),
                             &little_endian, NULL);
      CHECK_OPENCL_ERROR_IN ("clGetDeviceInfo endianness");

      if (little_endian == CL_FALSE)
        {
          fprintf (stderr,
                   "SPIR-V can only be tested on little-endian devices\n");
          return 1;
        }

      err = clGetDeviceInfo (device, CL_DEVICE_ADDRESS_BITS, sizeof (cl_uint),
                             &address_bits, NULL);
      CHECK_OPENCL_ERROR_IN ("clGetDeviceInfo addr bits");

      if (address_bits < 64)
        ext = ".spirv32";
      else
        ext = ".spirv64";
    }

  if (poclbin)
    {
      ext = ".poclbin";
    }

  if (from_source)
    {
      ext = ".cl";
    }

  err = pocl_getpath (path, 1024, explicit_binary, basename, ext);
  if (err != CL_SUCCESS)
    return err;

  if (from_source)
    {
      char *src = poclu_read_file (path);
      TEST_ASSERT (src != NULL);

      program = clCreateProgramWithSource (context, 1, (const char **)&src,
                                           NULL, &err);
      CHECK_OPENCL_ERROR_IN ("clCreateProgramWithSource");

      err = clBuildProgram (program, num_devices, devices, final_opts, NULL,
                            NULL);
      CHECK_OPENCL_ERROR_IN ("clBuildProgram");
      free (src);
    }
  else if (spirv)
    {
#ifdef CL_VERSION_2_1
      TEST_ASSERT (device != NULL);
      binary = poclu_read_binfile (path, &binary_size);
      TEST_ASSERT (binary != NULL);

      program = clCreateProgramWithIL (context, (const void *)binary,
                                       binary_size, &err);
      CHECK_OPENCL_ERROR_IN ("clCreateProgramWithIL");

      err = clBuildProgram (program, 0, NULL, final_opts, NULL, NULL);
      CHECK_OPENCL_ERROR_IN ("clBuildProgram");
      free (binary);
#else
      TEST_ASSERT (0 && "test compiled without OpenCL 2.1 can't use clCreateProgramWithIL");
#endif
    }
  else
    {
      TEST_ASSERT (device != NULL);
      binary = poclu_read_binfile (path, &binary_size);
      TEST_ASSERT (binary != NULL);

      program = clCreateProgramWithBinary (context, 1, &device, &binary_size,
                                           (const unsigned char **)&binary,
                                           NULL, &err);
      CHECK_OPENCL_ERROR_IN ("clCreateProgramWithBinary");

      err = clBuildProgram (program, 0, NULL, final_opts, NULL, NULL);
      CHECK_OPENCL_ERROR_IN ("clBuildProgram");
      free (binary);
    }

  *p = program;
  return err;
}

int
poclu_load_program (cl_context context, cl_device_id device,
                    const char *basename, int spirv, int poclbin,
                    const char *explicit_binary, const char *extra_build_opts,
                    cl_program *p)
{
  return poclu_load_program_multidev (context, &device, 1, basename,
                                      spirv, poclbin, explicit_binary,
                                      extra_build_opts, p);
}