@@ -70,7 +70,11 @@ pi_result OCL(piextDeviceSelectBinary)(
70
70
pi_device_binary *images, pi_uint32 num_images,
71
71
pi_device_binary *selected_image) {
72
72
73
- // TODO dummy implementation.
73
+ // TODO: this is a bare-bones implementation for choosing a device image
74
+ // that would be compatible with the targeted device. An AOT-compiled
75
+ // image is preferred over SPIRV for known devices (i.e. Intel devices)
76
+ // The implementation makes no effort to differentiate between multiple images
77
+ // for the given device, and simply picks the first one compatible
74
78
// Real implementaion will use the same mechanism OpenCL ICD dispatcher
75
79
// uses. Something like:
76
80
// PI_VALIDATE_HANDLE_RETURN_HANDLE(ctx, PI_INVALID_CONTEXT);
@@ -79,8 +83,56 @@ pi_result OCL(piextDeviceSelectBinary)(
79
83
// where context->dispatch is set to the dispatch table provided by PI
80
84
// plugin for platform/device the ctx was created for.
81
85
82
- *selected_image = num_images > 0 ? images[0 ] : nullptr ;
83
- return PI_SUCCESS;
86
+ // Choose the binary target for the provided device
87
+ const char *image_target = nullptr ;
88
+ // Get the type of the device
89
+ cl_device_type device_type;
90
+ cl_int ret_err = clGetDeviceInfo (cast<cl_device_id >(device), CL_DEVICE_TYPE,
91
+ sizeof (cl_device_type), &device_type, nullptr );
92
+ if (ret_err != CL_SUCCESS) {
93
+ *selected_image = nullptr ;
94
+ return cast<pi_result>(ret_err);
95
+ }
96
+
97
+ switch (device_type) {
98
+ // TODO: Factor out vendor specifics into a separate source
99
+ // E.g. sycl/source/detail/vendor/intel/detail/pi_opencl.cpp?
100
+
101
+ // We'll attempt to find an image that was AOT-compiled
102
+ // from a SPIR-V image into an image specific for:
103
+
104
+ case CL_DEVICE_TYPE_CPU: // OpenCL 64-bit CPU
105
+ image_target = PI_DEVICE_BINARY_TARGET_SPIRV64_X86_64;
106
+ break ;
107
+ case CL_DEVICE_TYPE_GPU: // OpenCL 64-bit GEN GPU
108
+ image_target = PI_DEVICE_BINARY_TARGET_SPIRV64_GEN;
109
+ break ;
110
+ case CL_DEVICE_TYPE_ACCELERATOR: // OpenCL 64-bit FPGA
111
+ image_target = PI_DEVICE_BINARY_TARGET_SPIRV64_FPGA;
112
+ break ;
113
+ default :
114
+ // Otherwise, we'll attempt to find and JIT-compile
115
+ // a device-independent SPIR-V image
116
+ image_target = PI_DEVICE_BINARY_TARGET_SPIRV64;
117
+ break ;
118
+ }
119
+
120
+ // Find the appropriate device image, fallback to spirv if not found
121
+ pi_device_binary fallback = nullptr ;
122
+ for (size_t i = 0 ; i < num_images; ++i) {
123
+ if (strcmp (images[i]->DeviceTargetSpec , image_target) == 0 ) {
124
+ *selected_image = images[i];
125
+ return PI_SUCCESS;
126
+ }
127
+ if (strcmp (images[i]->DeviceTargetSpec , PI_DEVICE_BINARY_TARGET_SPIRV64) ==
128
+ 0 )
129
+ fallback = images[i];
130
+ }
131
+ // Points to a spirv image, if such indeed was found
132
+ if ((*selected_image = fallback))
133
+ return PI_SUCCESS;
134
+ // No image can be loaded for the given device
135
+ return PI_INVALID_BINARY;
84
136
}
85
137
86
138
pi_result OCL (piQueueCreate)(pi_context context, pi_device device,
0 commit comments