/
pocl_cl.h
1902 lines (1619 loc) · 72.8 KB
/
pocl_cl.h
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
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* pocl_cl.h - local runtime library declarations.
Copyright (c) 2011 Universidad Rey Juan Carlos
2011-2019 Pekka Jääskeläinen
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.
*/
#ifndef POCL_CL_H
#define POCL_CL_H
#include "config.h"
#include <assert.h>
#include <errno.h>
#include <stdio.h>
#ifdef ENABLE_VALGRIND
#include <valgrind/helgrind.h>
#endif
#ifdef _WIN32
# include "vccompat.hpp"
#endif
#include "common.h"
#include "pocl.h"
#include "pocl_debug.h"
#include "pocl_hash.h"
#include "pocl_runtime_config.h"
#include "pocl_threads.h"
#include "pocl_tracing.h"
#ifdef BUILD_ICD
# include "pocl_icd.h"
#endif
#include <CL/cl_egl.h>
#include <CL/opencl.h>
#if __STDC_VERSION__ < 199901L
# if __GNUC__ >= 2
# define __func__ __PRETTY_FUNCTION__
# else
# define __func__ UNKNOWN_FUNCTION
# endif
#endif
#ifdef ENABLE_VALGRIND
#define VG_REFC_ZERO(var) \
ANNOTATE_HAPPENS_AFTER (&var->pocl_refcount); \
ANNOTATE_HAPPENS_BEFORE_FORGET_ALL (&var->pocl_refcount)
#define VG_REFC_NONZERO(var) ANNOTATE_HAPPENS_BEFORE (&var->pocl_refcount)
#else
#define VG_REFC_ZERO(var) (void)0
#define VG_REFC_NONZERO(var) (void)0
#endif
/*
* a workaround for a detection problem in Valgrind, which causes
* false positives. Valgrind manual states:
*
* Helgrind only partially correctly handles POSIX condition variables. This is
* because Helgrind can see inter-thread dependencies between a
* pthread_cond_wait call and a pthread_cond_signal/ pthread_cond_broadcast
* call only if the waiting thread actually gets to the rendezvous first (so
* that it actually calls pthread_cond_wait). It can't see dependencies between
* the threads if the signaller arrives first. In the latter case, POSIX
* guidelines imply that the associated boolean condition still provides an
* inter-thread synchronisation event, but one which is invisible to Helgrind.
*
* ... this macro explicitly associates the cond var with the mutex, by
* calling pthread_cond_wait with a short timeout (100 usec)
*/
#ifdef ENABLE_VALGRIND
#define VG_ASSOC_COND_VAR(cond_var, mutex) \
do \
{ \
struct timespec time_to_wait; \
clock_gettime (CLOCK_MONOTONIC, &time_to_wait); \
time_to_wait.tv_nsec += 100000; \
if (time_to_wait.tv_nsec > 1000000000) \
{ \
time_to_wait.tv_nsec -= 1000000000; \
time_to_wait.tv_sec += 1; \
} \
POCL_TIMEDWAIT_COND (cond_var, mutex, time_to_wait); \
} \
while (0)
#else
#define VG_ASSOC_COND_VAR(var, mutex) (void)0
#endif
#ifdef __linux__
#define ALIGN_CACHE(x) x __attribute__ ((aligned (HOST_CPU_CACHELINE_SIZE)))
#else
#define ALIGN_CACHE(x) x
#endif
//############################################################################
#ifdef ENABLE_EXTRA_VALIDITY_CHECKS
// 13_729_512_230_397_775_139
#define POCL_MAGIC_1 0xBE8906A1A83D8D23ULL
// 511_941_616_703_887_367
#define POCL_MAGIC_2 0x071AC830215FD807ULL
#define IS_CL_OBJECT_VALID(__OBJ__) \
(((__OBJ__) != NULL) && ((__OBJ__)->magic_1 == POCL_MAGIC_1) \
&& ((__OBJ__)->magic_2 == POCL_MAGIC_2))
#define CHECK_VALIDITY_MARKERS(__OBJ__) \
assert ((__OBJ__)->magic_1 == POCL_MAGIC_1); \
assert ((__OBJ__)->magic_2 == POCL_MAGIC_2);
#define SET_VALIDITY_MARKERS(__OBJ__) \
(__OBJ__)->magic_1 = POCL_MAGIC_1; \
(__OBJ__)->magic_2 = POCL_MAGIC_2;
#define UNSET_VALIDITY_MARKERS(__OBJ__) \
(__OBJ__)->magic_1 = 0; \
(__OBJ__)->magic_2 = 0;
#else
#define IS_CL_OBJECT_VALID(__OBJ__) ((__OBJ__) != NULL)
#define CHECK_VALIDITY_MARKERS(__OBJ__)
#define SET_VALIDITY_MARKERS(__OBJ__)
#define UNSET_VALIDITY_MARKERS(__OBJ__)
#endif
#define POCL_LOCK_OBJ(__OBJ__) \
do \
{ \
CHECK_VALIDITY_MARKERS(__OBJ__); \
POCL_LOCK ((__OBJ__)->pocl_lock); \
assert ((__OBJ__)->pocl_refcount > 0); \
} \
while (0)
#define POCL_UNLOCK_OBJ(__OBJ__) \
do \
{ \
CHECK_VALIDITY_MARKERS(__OBJ__); \
assert ((__OBJ__)->pocl_refcount >= 0); \
POCL_UNLOCK ((__OBJ__)->pocl_lock); \
} \
while (0)
#define POCL_RELEASE_OBJECT(__OBJ__, __NEW_REFCOUNT__) \
do \
{ \
POCL_LOCK_OBJ (__OBJ__); \
__NEW_REFCOUNT__ = --(__OBJ__)->pocl_refcount; \
POCL_UNLOCK_OBJ (__OBJ__); \
} \
while (0)
#define POCL_RELEASE_OBJECT_UNLOCKED(__OBJ__, __NEW_REFCOUNT__) \
__NEW_REFCOUNT__ = --(__OBJ__)->pocl_refcount;
#define POCL_RETAIN_OBJECT_UNLOCKED(__OBJ__) \
++((__OBJ__)->pocl_refcount)
#define POCL_RETAIN_OBJECT_REFCOUNT(__OBJ__, R) \
do { \
POCL_LOCK_OBJ (__OBJ__); \
R = POCL_RETAIN_OBJECT_UNLOCKED (__OBJ__); \
POCL_UNLOCK_OBJ (__OBJ__); \
} while (0)
#define POCL_RETAIN_OBJECT(__OBJ__) \
do { \
POCL_LOCK_OBJ (__OBJ__); \
POCL_RETAIN_OBJECT_UNLOCKED (__OBJ__); \
POCL_UNLOCK_OBJ (__OBJ__); \
} while (0)
extern uint64_t last_object_id;
/* The reference counter is initialized to 1,
when it goes to 0 object can be freed. */
#define POCL_INIT_OBJECT_NO_ICD(__OBJ__) \
do \
{ \
SET_VALIDITY_MARKERS(__OBJ__); \
(__OBJ__)->pocl_refcount = 1; \
POCL_INIT_LOCK ((__OBJ__)->pocl_lock); \
(__OBJ__)->id = POCL_ATOMIC_INC (last_object_id); \
} \
while (0)
#define POCL_MEM_FREE(F_PTR) \
do { \
free((F_PTR)); \
(F_PTR) = NULL; \
} while (0)
#ifdef BUILD_ICD
/* Most (all?) object must also initialize the ICD field */
# define POCL_INIT_OBJECT(__OBJ__) \
do { \
POCL_INIT_OBJECT_NO_ICD(__OBJ__); \
POCL_INIT_ICD_OBJECT(__OBJ__); \
} while (0)
#else
# define POCL_INIT_OBJECT(__OBJ__) \
POCL_INIT_OBJECT_NO_ICD(__OBJ__)
#endif
#define POCL_DESTROY_OBJECT(__OBJ__) \
do \
{ \
UNSET_VALIDITY_MARKERS(__OBJ__); \
POCL_DESTROY_LOCK ((__OBJ__)->pocl_lock); \
} \
while (0)
/* Declares the generic pocl object attributes inside a struct. */
#ifdef ENABLE_EXTRA_VALIDITY_CHECKS
#define POCL_OBJECT \
uint64_t magic_1; \
uint64_t id; \
pocl_lock_t pocl_lock; \
uint64_t magic_2; \
int pocl_refcount
#else
#define POCL_OBJECT \
uint64_t id; \
pocl_lock_t pocl_lock; \
int pocl_refcount
#endif
#ifdef __APPLE__
/* Note: OSX doesn't support aliases because it doesn't use ELF */
# define POname(name) name
# define POdeclsym(name)
# define POdeclsymExport(name)
# define POsym(name)
# define POsymAlways(name)
#elif defined(_WIN32)
/* Visual Studio does not support this magic either */
# define POname(name) name
# define POdeclsym(name)
# define POdeclsymExport(name)
# define POsym(name)
# define POsymAlways(name)
#else
/* Symbol aliases are supported */
# define POname(name) PO##name
#define POdeclsym(name) __typeof__ (name) PO##name;
#define POdeclsymExport(name) POCL_EXPORT POdeclsym(name)
# define POCL_ALIAS_OPENCL_SYMBOL(name) \
__typeof__(name) name __attribute__((alias ("PO" #name), visibility("default")));
#if !defined(BUILD_ICD) && !defined(RENAME_POCL)
# define POsym(name) POCL_ALIAS_OPENCL_SYMBOL(name)
# else
# define POsym(name)
# endif
#if !defined(RENAME_POCL)
# define POsymAlways(name) POCL_ALIAS_OPENCL_SYMBOL(name)
# else
# define POsymAlways(name)
# endif
#endif
/* The ICD compatibility part. This must be first in the objects where
* it is used (as the ICD loader assumes that)*/
#ifdef BUILD_ICD
# define POCL_ICD_OBJECT struct _cl_icd_dispatch *dispatch;
# define POCL_ICD_OBJECT_PLATFORM_ID POCL_ICD_OBJECT
# define POsymICD(name) POsym(name)
# define POdeclsymICD(name) POdeclsym(name)
#else
# define POCL_ICD_OBJECT
# define POCL_ICD_OBJECT_PLATFORM_ID unsigned long id;
# define POsymICD(name)
# define POdeclsymICD(name)
#endif
#include "pocl_intfn.h"
/* fields for cl_kernel -> has_arg_metadata */
#define POCL_HAS_KERNEL_ARG_ADDRESS_QUALIFIER 1
#define POCL_HAS_KERNEL_ARG_ACCESS_QUALIFIER 2
#define POCL_HAS_KERNEL_ARG_TYPE_NAME 4
#define POCL_HAS_KERNEL_ARG_TYPE_QUALIFIER 8
#define POCL_HAS_KERNEL_ARG_NAME 16
/* pocl specific flag, for "hidden" default queues allocated in each context */
#define CL_QUEUE_HIDDEN (1 << 10)
#define POCL_GVAR_INIT_KERNEL_NAME "pocl.gvar.init"
typedef struct pocl_argument {
uint64_t size;
/* The "offset" is used to simplify subbuffer handling.
* At enqueue time, subbuffers are converted to buffers + offset into them.
*/
uint64_t offset;
void *value;
/* 1 if this argument has been set by clSetKernelArg */
char is_set;
/* 1 if the argument is read-only according to kernel metadata. So either
* a buffer with "const" qualifier, or an image with read_only qualifier */
char is_readonly;
/* 1 if the argument pointer is SVM direct pointer, not a cl_mem */
char is_svm;
} pocl_argument;
typedef struct event_node event_node;
/**
* Enumeration for kernel argument types
*/
typedef enum
{
POCL_ARG_TYPE_NONE = 0,
POCL_ARG_TYPE_POINTER = 1,
POCL_ARG_TYPE_IMAGE = 2,
POCL_ARG_TYPE_SAMPLER = 3,
POCL_ARG_TYPE_PIPE = 4,
} pocl_argument_type;
#define ARG_IS_LOCAL(a) (a.address_qualifier == CL_KERNEL_ARG_ADDRESS_LOCAL)
#define ARGP_IS_LOCAL(a) (a->address_qualifier == CL_KERNEL_ARG_ADDRESS_LOCAL)
typedef struct pocl_argument_info {
char* type_name;
char* name;
cl_kernel_arg_address_qualifier address_qualifier;
cl_kernel_arg_access_qualifier access_qualifier;
cl_kernel_arg_type_qualifier type_qualifier;
pocl_argument_type type;
unsigned type_size;
} pocl_argument_info;
struct pocl_device_ops {
const char *device_name;
/* New driver api extension for out-of-order execution and
asynchronous devices.
See this for reference: http://URN.fi/URN:NBN:fi:tty-201412051583
See basic and pthread driver for reference. */
/* submit gives the command for the device. The command may be left in the cq
or stored to the device driver owning the cq. submit is called
with node->sync.event.event locked, and must return with it unlocked. */
void (*submit) (_cl_command_node *node, cl_command_queue cq);
/* join is called by clFinish and this function blocks until all the enqueued
commands are finished. Called by the user thread; see notify_cmdq_finished
for the driver thread counterpart. */
void (*join) (cl_device_id device, cl_command_queue cq);
/* flush is called when clFlush is called. This function ensures that
commands will be eventually executed. It is up to the device what happens
here, if anything. See basic and pthread for reference.*/
void (*flush) (cl_device_id device, cl_command_queue cq);
/* notify is used to communicate to a device driver that an event, it has
been waiting, has been completed. Upon call, both events are locked, and
must be locked also on return.*/
void (*notify) (cl_device_id device, cl_event event, cl_event finished);
/* broadcast is(has to be) called by the device driver when a command is
completed.
It is used to broadcast notifications to device drivers waiting
this event to complete.
There is a default implementation for this. Use it if there is no need
to do anything special here.
The default implementation calls notify(event, target_event) for the
list of events waiting on 'event'. */
void (*broadcast) (cl_event event);
/* wait_event is called by clWaitForEvents() and blocks the execution until
* the waited event is complete or failed. Called by user threads; see the
* notify_event_finished() callback for driver thread counterpart.
* Called (and must return) with unlocked event. */
void (*wait_event) (cl_device_id device, cl_event event);
/* update_event is an extra callback called during handling of event status
* changes, useful if something device specific needs to be done. May be
* NULL; no need to implement if not needed.
*
* Called via pocl_update_event_* functions in pocl_util.c
* All pocl_update_event_* (except COMPLETE) must be called (and return)
* with LOCKED event.
*/
void (*update_event) (cl_device_id device, cl_event event);
/* free_event_data may be called when event is freed. Event data may only be
used by the device driver owning the corresponding command.
No need to implement this if the device does not need any event data. */
void (*free_event_data) (cl_event event);
/* Called from driver threads to notify every user thread waiting on
* command queue finish. See join() for user counterpart.
* Driver may chose to not implement this, which will result in
* undefined behaviour in multi-threaded user programs. */
void (*notify_cmdq_finished) (cl_command_queue cq);
/* Called from driver threads to notify every user thread waiting on
* a specific event. See wait_event() for user counterpart.
* Driver may chose to not implement this, which will result in
* undefined behaviour in multi-threaded user programs. */
void (*notify_event_finished) (cl_event event);
/* /New driver api extension */
/* Detects & returns the number of available devices the driver finds on the system. */
unsigned int (*probe) (struct pocl_device_ops *ops);
/* Device initialization. Parameters:
* j : progressive index for the devices of the same type
* device : struct to initialize
* parameters : optional environment with device-specific parameters
*/
cl_int (*init) (unsigned j, cl_device_id device, const char *parameters);
/* Device type initialization after all devices have been initialized */
cl_int (*post_init) (struct pocl_device_ops *ops);
/* uninitializes the driver for a particular device. May free hardware resources. */
cl_int (*uninit) (unsigned j, cl_device_id device);
/* reinitializes the driver for a particular device. Called after uninit;
* the first initialization is done by 'init'. May be NULL */
cl_int (*reinit) (unsigned j, cl_device_id device, const char *parameters);
/* allocate a buffer in device memory */
cl_int (*alloc_mem_obj) (cl_device_id device, cl_mem mem_obj, void* host_ptr);
/* free a device buffer */
void (*free) (cl_device_id device, cl_mem mem_obj);
/* return >0 if driver can migrate directly between devices.
* Priority between devices signalled by larger numbers. */
int (*can_migrate_d2d) (cl_device_id dest, cl_device_id source);
/* migrate buffer content directly between devices */
int (*migrate_d2d) (cl_device_id src_dev,
cl_device_id dst_dev,
cl_mem mem,
pocl_mem_identifier *src_mem_id,
pocl_mem_identifier *dst_mem_id);
/* SVM Ops */
void (*svm_free) (cl_device_id dev, void *svm_ptr);
void *(*svm_alloc) (cl_device_id dev, cl_svm_mem_flags flags, size_t size);
void (*svm_map) (cl_device_id dev, void *svm_ptr);
void (*svm_unmap) (cl_device_id dev, void *svm_ptr);
/* these are optional. If the driver needs to do anything to be able
* to use host memory, it should do it (and undo it) in these callbacks.
* Currently used by HSA.
* See pocl_driver_alloc_mem_obj and pocl_driver_free for details. */
void (*svm_register) (cl_device_id dev, void *host_ptr, size_t size);
void (*svm_unregister) (cl_device_id dev, void *host_ptr, size_t size);
/* we can use restrict here, because Spec says overlapping copy should return
* with CL_MEM_COPY_OVERLAP error. */
void (*svm_copy) (cl_device_id dev, void *__restrict__ dst,
const void *__restrict__ src, size_t size);
void (*svm_fill) (cl_device_id dev, void *__restrict__ svm_ptr, size_t size,
void *__restrict__ pattern, size_t pattern_size);
void (*svm_migrate) (cl_device_id dev, size_t num_svm_pointers,
void *__restrict__ svm_pointers,
size_t *__restrict__ sizes);
void (*svm_advise) (cl_device_id dev, const void *svm_ptr, size_t size,
cl_mem_advice_intel advice);
/* required for PoCL's command buffer extensions */
void (*svm_copy_rect) (cl_device_id dev,
void *__restrict__ dst,
const void *__restrict__ src,
const size_t *dst_origin,
const size_t *src_origin,
const size_t *region,
size_t dst_row_pitch,
size_t dst_slice_pitch,
size_t src_row_pitch,
size_t src_slice_pitch);
void (*svm_fill_rect) (cl_device_id dev,
void *__restrict__ svm_ptr,
const size_t *origin,
const size_t *region,
size_t row_pitch,
size_t slice_pitch,
void *__restrict__ pattern,
size_t pattern_size);
/* USM Ops (Intel) */
void *(*usm_alloc) (cl_device_id dev, unsigned alloc_type,
cl_mem_alloc_flags_intel flags, size_t size, cl_int *errcode);
void (*usm_free) (cl_device_id dev, void *svm_ptr);
/* this one is separate, because the device might choose to not support it in
* the driver. in that case, the runtime will create an event of usm_free
* type, which has a wait on all CQs in the context. */
void (*usm_free_blocking) (cl_device_id dev, void *svm_ptr);
/* the following callbacks only deal with buffers (and IMAGE1D_BUFFER which
* is backed by a buffer), not images. */
/* clEnqReadBuffer */
void (*read) (void *data,
void *__restrict__ dst_host_ptr,
pocl_mem_identifier * src_mem_id,
cl_mem src_buf,
size_t offset,
size_t size);
/* clEnqReadBufferRect */
void (*read_rect) (void *data, void *__restrict__ dst_host_ptr,
pocl_mem_identifier *src_mem_id, cl_mem src_buf,
const size_t *buffer_origin, const size_t *host_origin,
const size_t *region, size_t buffer_row_pitch,
size_t buffer_slice_pitch, size_t host_row_pitch,
size_t host_slice_pitch);
/* clEnqWriteBuffer */
void (*write) (void *data,
const void *__restrict__ src_host_ptr,
pocl_mem_identifier * dst_mem_id,
cl_mem dst_buf,
size_t offset,
size_t size);
/* clEnqWriteBufferRect */
void (*write_rect) (void *data, const void *__restrict__ src_host_ptr,
pocl_mem_identifier *dst_mem_id, cl_mem dst_buf,
const size_t *buffer_origin, const size_t *host_origin,
const size_t *region, size_t buffer_row_pitch,
size_t buffer_slice_pitch, size_t host_row_pitch,
size_t host_slice_pitch);
/* clEnqCopyBuffer */
void (*copy) (void *data,
pocl_mem_identifier * dst_mem_id,
cl_mem dst_buf,
pocl_mem_identifier * src_mem_id,
cl_mem src_buf,
size_t dst_offset,
size_t src_offset,
size_t size);
/* clEnqCopyBufferRect */
void (*copy_rect) (void *data,
pocl_mem_identifier * dst_mem_id,
cl_mem dst_buf,
pocl_mem_identifier * src_mem_id,
cl_mem src_buf,
const size_t *dst_origin,
const size_t *src_origin,
const size_t *region,
size_t dst_row_pitch,
size_t dst_slice_pitch,
size_t src_row_pitch,
size_t src_slice_pitch);
/* clEnqCopyBuffer with the cl_pocl_content_size extension. This callback is optional */
void (*copy_with_size) (void *data,
pocl_mem_identifier *dst_mem_id,
cl_mem dst_buf,
pocl_mem_identifier *src_mem_id,
cl_mem src_buf,
pocl_mem_identifier *content_size_buf_mem_id,
cl_mem content_size_buf,
size_t dst_offset,
size_t src_offset,
size_t size);
/* clEnqFillBuffer */
void (*memfill) (void *data,
pocl_mem_identifier * dst_mem_id,
cl_mem dst_buf,
size_t size,
size_t offset,
const void *__restrict__ pattern,
size_t pattern_size);
/* Maps 'size' bytes of device global memory at + offset to
host-accessible memory. This might or might not involve copying
the block from the device. */
cl_int (*map_mem) (void *data,
pocl_mem_identifier * src_mem_id,
cl_mem src_buf,
mem_mapping_t *map);
cl_int (*unmap_mem) (void *data,
pocl_mem_identifier * dst_mem_id,
cl_mem dst_buf,
mem_mapping_t *map);
/* these don't actually do the mapping, only return a pointer
* where the driver will map in future. Separate API from map/unmap
* because 1) unlike other driver ops, this is called from the user thread,
* so it can be called in parallel with map/unmap or any command executing
* in the driver; 2) most drivers can share the code for these */
cl_int (*get_mapping_ptr) (void *data, pocl_mem_identifier *mem_id,
cl_mem mem, mem_mapping_t *map);
cl_int (*free_mapping_ptr) (void *data, pocl_mem_identifier *mem_id,
cl_mem mem, mem_mapping_t *map);
/* if the driver needs to do something at kernel create/destroy time */
int (*create_kernel) (cl_device_id device, cl_program program,
cl_kernel kernel, unsigned program_device_i);
int (*free_kernel) (cl_device_id device, cl_program program,
cl_kernel kernel, unsigned program_device_i);
/* program building callbacks */
int (*build_source) (
cl_program program, cl_uint device_i,
/* these are filled by clCompileProgram(), otherwise NULLs */
cl_uint num_input_headers, const cl_program *input_headers,
const char **header_include_names,
/* 1 = compile & link, 0 = compile only, linked later via clLinkProgram*/
int link_program);
int (*build_binary) (
cl_program program, cl_uint device_i,
/* 1 = compile & link, 0 = compile only, linked later via clLinkProgram*/
int link_program, int spir_build);
/* build a program with builtin kernels. */
int (*build_builtin) (cl_program program, cl_uint device_i);
int (*link_program) (cl_program program, cl_uint device_i,
cl_uint num_input_programs,
const cl_program *input_programs,
/* 1 = create library, 0 = create executable*/
int create_library);
/* optional. called after build/link and after metadata setup. */
int (*post_build_program) (cl_program program, cl_uint device_i);
/* optional. Ensures that everything is built for
* returning a poclbinary to the user. E.g. for CPU driver this means
* building a dynamic WG sized parallel.bc */
int (*build_poclbinary) (cl_program program, cl_uint device_i);
/* Optional. If the driver uses the default build_poclbinary implementation
* from common_driver.c, that implementation calls this to compile a
* "dynamic WG size" kernel. */
void (*compile_kernel) (_cl_command_node *cmd, cl_kernel kernel,
cl_device_id device, int specialize);
/* Optional. driver should free the content of "program->data" here,
* if it fills it. */
int (*free_program) (cl_device_id device, cl_program program,
unsigned program_device_i);
/* Driver should setup kernel metadata here, if it can, and return non-zero
* on success. This is called after compilation/build/link. E.g. CPU driver
* parses the LLVM metadata. */
int (*setup_metadata) (cl_device_id device, cl_program program,
unsigned program_device_i);
/* Driver should examine the binary and return non-zero if it can load it.
* Note that it's never called with pocl-binaries; those are automatically
* accepted if device-hash in the binary's header matches the device. */
int (*supports_binary) (cl_device_id device, const size_t length,
const char *binary);
/* Optional. if the driver needs to use hardware resources
* for command queues, it should use these callbacks */
int (*init_queue) (cl_device_id device, cl_command_queue queue);
int (*free_queue) (cl_device_id device, cl_command_queue queue);
/* Optional. if the driver needs to use per-context resources,
* it should use these callbacks for management. */
int (*init_context) (cl_device_id device, cl_context context);
int (*free_context) (cl_device_id device, cl_context context);
/* clEnqueueNDRangeKernel */
void (*run) (void *data, _cl_command_node *cmd);
/* for clEnqueueNativeKernel. may be NULL */
void (*run_native) (void *data, _cl_command_node *cmd);
/* Perform initialization steps and can return additional
build options that are required for the device. The caller
owns the returned string. may be NULL */
char* (*init_build) (void *data);
/* may be NULL */
void (*init_target_machine) (void *data, void *target_machine);
/* returns a hash string that should identify the device. This string
* is used when writing/loading pocl binaries to decide compatibility. */
char* (*build_hash) (cl_device_id device);
/* the following callbacks deal with images ONLY, with the exception of
* IMAGE1D_BUFFER type (which is implemented as a buffer).
* If the device does not support images, all of these may be NULL. */
/* creates a device-specific hardware resource for sampler. May be NULL */
int (*create_sampler) (cl_device_id device,
cl_sampler samp,
unsigned context_device_i);
int (*free_sampler) (cl_device_id device,
cl_sampler samp,
unsigned context_device_i);
/* copies image to image, on the same device (or same global memory). */
cl_int (*copy_image_rect) (void *data,
cl_mem src_image,
cl_mem dst_image,
pocl_mem_identifier *src_mem_id,
pocl_mem_identifier *dst_mem_id,
const size_t *src_origin,
const size_t *dst_origin,
const size_t *region);
/* copies a region from host OR device buffer to device image.
* clEnqueueCopyBufferToImage: src_mem_id = buffer,
* src_host_ptr = NULL, src_row_pitch = src_slice_pitch = 0
* clEnqueueWriteImage: src_mem_id = NULL,
* src_host_ptr = host pointer, src_offset = 0
*/
cl_int (*write_image_rect ) (void *data,
cl_mem dst_image,
pocl_mem_identifier *dst_mem_id,
const void *__restrict__ src_host_ptr,
pocl_mem_identifier *src_mem_id,
const size_t *origin,
const size_t *region,
size_t src_row_pitch,
size_t src_slice_pitch,
size_t src_offset);
/* copies a region from device image to host or device buffer
* clEnqueueCopyImageToBuffer: dst_mem_id = buffer,
* dst_host_ptr = NULL, dst_row_pitch = dst_slice_pitch = 0
* clEnqueueReadImage: dst_mem_id = NULL,
* dst_host_ptr = host pointer, dst_offset = 0
*/
cl_int (*read_image_rect) (void *data,
cl_mem src_image,
pocl_mem_identifier *src_mem_id,
void *__restrict__ dst_host_ptr,
pocl_mem_identifier *dst_mem_id,
const size_t *origin,
const size_t *region,
size_t dst_row_pitch,
size_t dst_slice_pitch,
size_t dst_offset);
/* maps the entire image from device to host */
cl_int (*map_image) (void *data,
pocl_mem_identifier *mem_id,
cl_mem src_image,
mem_mapping_t *map);
/* unmaps the entire image from host to device */
cl_int (*unmap_image) (void *data,
pocl_mem_identifier *mem_id,
cl_mem dst_image,
mem_mapping_t *map);
/* fill image with pattern */
cl_int (*fill_image) (void *data, cl_mem image, pocl_mem_identifier *mem_id,
const size_t *origin, const size_t *region,
cl_uint4 orig_pixel, pixel_t fill_pixel,
size_t pixel_size);
/* custom device functionality */
/* The device can override this function to perform driver-specific
* optimizations to the local size dimensions, whenever the decision
* is left to the runtime. */
void (*compute_local_size) (cl_device_id dev, cl_kernel kernel,
unsigned device_i,
size_t global_x, size_t global_y,
size_t global_z, size_t *local_x,
size_t *local_y, size_t *local_z);
cl_int (*get_device_info_ext) (cl_device_id dev,
cl_device_info param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret);
cl_int (*get_mem_info_ext) (cl_device_id dev,
const void *ptr,
cl_uint param_name,
size_t param_value_size,
void *param_value,
size_t *param_value_size_ret);
cl_int (*set_kernel_exec_info_ext) (cl_device_id dev,
unsigned program_device_i,
cl_kernel kernel,
cl_uint param_name,
size_t param_value_size,
const void *param_value);
/* optional. Return CL_SUCCESS if the device can be, or is associated with
* the GL context described in properties. */
cl_int (*get_gl_context_assoc) (cl_device_id device, cl_gl_context_info type,
const cl_context_properties *properties);
/* cl_khr_command_buffer extension */
cl_int (*create_finalized_command_buffer) (
cl_device_id device, cl_command_buffer_khr command_buffer);
cl_int (*free_command_buffer) (cl_device_id device,
cl_command_buffer_khr command_buffer);
cl_int (*run_command_buffer) (void *data, cl_command_buffer_khr cmd);
};
typedef struct pocl_global_mem_t {
pocl_lock_t pocl_lock;
cl_ulong max_ever_allocated;
cl_ulong currently_allocated;
cl_ulong total_alloc_limit;
} pocl_global_mem_t;
#define NUM_OPENCL_IMAGE_TYPES 6
struct _cl_device_id {
POCL_ICD_OBJECT
POCL_OBJECT;
/* queries */
cl_device_type type;
cl_uint vendor_id;
cl_uint max_compute_units;
/* for subdevice support */
cl_device_id parent_device;
unsigned core_start;
unsigned core_count;
cl_uint max_work_item_dimensions;
/* when enabled, Workgroup LLVM pass will replace all printf() calls
* with calls to __pocl_printf and recursively change functions to
* add printf buffer arguments from pocl_context.
* Currently the pthread/basic devices require this; other devices
* implement printf their own way. */
int device_side_printf;
size_t max_work_item_sizes[3];
size_t max_work_group_size;
size_t preferred_wg_size_multiple;
cl_bool non_uniform_work_group_support;
cl_bool generic_as_support;
cl_bool wg_collective_func_support;
cl_uint preferred_vector_width_char;
cl_uint preferred_vector_width_short;
cl_uint preferred_vector_width_int;
cl_uint preferred_vector_width_long;
cl_uint preferred_vector_width_float;
cl_uint preferred_vector_width_double;
cl_uint preferred_vector_width_half;
cl_uint native_vector_width_char;
cl_uint native_vector_width_short;
cl_uint native_vector_width_int;
cl_uint native_vector_width_long;
cl_uint native_vector_width_float;
cl_uint native_vector_width_double;
cl_uint native_vector_width_half;
cl_uint max_clock_frequency;
cl_uint address_bits;
cl_ulong max_mem_alloc_size;
cl_bool image_support;
cl_uint max_read_image_args;
cl_uint max_write_image_args;
cl_uint max_read_write_image_args;
size_t image2d_max_width;
size_t image2d_max_height;
size_t image3d_max_width;
size_t image3d_max_height;
size_t image3d_max_depth;
size_t image_max_buffer_size;
size_t image_max_array_size;
cl_uint max_samplers;
size_t max_parameter_size;
cl_uint mem_base_addr_align;
cl_uint min_data_type_align_size;
cl_device_fp_config half_fp_config;
cl_device_fp_config single_fp_config;
cl_device_fp_config double_fp_config;
cl_device_fp_atomic_capabilities_ext single_fp_atomic_caps;
cl_device_fp_atomic_capabilities_ext half_fp_atomic_caps;
cl_device_fp_atomic_capabilities_ext double_fp_atomic_caps;
cl_device_mem_cache_type global_mem_cache_type;
cl_uint global_mem_cacheline_size;
cl_ulong global_mem_cache_size;
cl_ulong global_mem_size;
size_t global_var_pref_size;
size_t global_var_max_size;
cl_ulong max_constant_buffer_size;
cl_uint max_constant_args;
cl_device_local_mem_type local_mem_type;
cl_ulong local_mem_size;
cl_bool error_correction_support;
cl_bool host_unified_memory;
size_t profiling_timer_resolution;
cl_bool endian_little;
cl_bool *available;
cl_bool compiler_available;
cl_bool linker_available;
/* Is the target a Single Program Multiple Data machine? If not,
we need to generate work-item loops to execute all the work-items
in the WG. For SPMD machines, the hardware spawns the WIs. */
cl_bool spmd;
/* The device uses an HSA-like kernel ABI with a single argument buffer as
an input. */
cl_bool arg_buffer_launcher;
/* The device uses a GRID launcher */
cl_bool grid_launcher;
/* The Workgroup pass creates launcher functions and replaces work-item
placeholder global variables (e.g. _local_size_, _global_offset_ etc) with
loads from the context struct passed as a kernel argument. This flag
enables or disables this pass. */
cl_bool run_workgroup_pass;
/* The program scope variable pass takes program-scope variables and replaces
them by references into a buffer, and creates an initializer kernel. */
cl_bool run_program_scope_variables_pass;
/* if CL_TRUE, pocl_llvm_build_program will ignore pocl's OpenCL headers
* and rely purely on Clang's OpenCL headers. For most drivers,
* this should default to CL_FALSE. */
cl_bool use_only_clang_opencl_headers;
cl_device_exec_capabilities execution_capabilities;
cl_command_queue_properties queue_properties;
cl_platform_id platform;
cl_uint max_sub_devices;
size_t num_partition_properties;
cl_device_partition_property *partition_properties;
size_t num_partition_types;
cl_device_partition_property *partition_type;
size_t printf_buffer_size;
const char *short_name;
const char *long_name;
const char *vendor;
const char *driver_version;
const char *profile;
const char *extensions;
/* these are Device versions, not OpenCL C versions */
const char *version;
unsigned version_as_int; /* e.g. 200 */
cl_version version_as_cl; /* cl_version format */
/* highest OpenCL C version supported by the compiler */
const char *opencl_c_version_as_opt;
cl_version opencl_c_version_as_cl;
void *data;
const char* llvm_target_triplet; /* the llvm target triplet to use */
const char* kernellib_name; /* bitcode kernel library name */
const char* kernellib_fallback_name; /* bitcode kernel fallback library name */
const char* kernellib_subdir; /* bitcode kernel library subdir */
const char* llvm_cpu; /* the llvm CPU variant to use */
const char *llvm_abi; /* the ABI to use */
const char* llvm_fp_contract_mode; /* the floating point contract mde to use */
/* A running number (starting from zero) across all the device instances.
Used for indexing arrays in data structures with device specific
entries. */
int dev_id;
/* Identifier for a physical device global memory. */
int global_mem_id;
/* Pointer to an accounting struct for global memory */
pocl_global_mem_t *global_memory;
/* Does the device have 64bit longs */
int has_64bit_long;
/* Does the device set the event times in update_event() callback ?
* if zero, the default event change handlers set the event times based on
* the host's system time (pocl_gettimemono_ns). */
int has_own_timer;
/* whether this device supports OpenGL / EGL interop */
int has_gl_interop;
/* Convert automatic local variables to kernel arguments? */
pocl_autolocals_to_args_strategy autolocals_to_args;
/* Allocate local buffers device side in the work-group launcher instead of
having a disjoint physical local memory per work-group or having the
runtime/driver allocate the local space. */
int device_alloca_locals;
/* If > 0, specialized versions of the work-group functions are generated
which assume each grid dimension is of at most the given width. This
assumption can be then taken in account in IR optimization and codegen
to reduce address computation overheads etc. */
size_t grid_width_specialization_limit;
/* Device-specific linker flags that should be appended to the clang's