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
|
/* OpenCL runtime library: clCreateProgramWithBinary()
Copyright (c) 2012 Pekka Jääskeläinen / Tampere University of Technology
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.
*/
#include "pocl_binary.h"
#include "pocl_cache.h"
#include "pocl_cl.h"
#include "pocl_file_util.h"
#include "pocl_llvm.h"
#include "pocl_shared.h"
#include "pocl_util.h"
#include <string.h>
extern unsigned long program_c;
/* creates either a program with binaries, or an empty program. The latter
* is useful for clLinkProgram() which needs an empty program to put the
* compiled results in.
*/
cl_program
create_program_skeleton (cl_context context, cl_uint num_devices,
const cl_device_id *device_list,
const size_t *lengths, const unsigned char **binaries,
cl_int *binary_status, cl_int *errcode_ret,
int allow_empty_binaries)
{
cl_program program;
unsigned i,j;
int errcode;
cl_device_id *unique_devlist = NULL;
POCL_GOTO_ERROR_COND ((!IS_CL_OBJECT_VALID (context)), CL_INVALID_CONTEXT);
POCL_GOTO_ERROR_COND((device_list == NULL), CL_INVALID_VALUE);
POCL_GOTO_ERROR_COND((num_devices == 0), CL_INVALID_VALUE);
if (!allow_empty_binaries)
{
POCL_GOTO_ERROR_COND ((lengths == NULL), CL_INVALID_VALUE);
for (i = 0; i < num_devices; ++i)
{
POCL_GOTO_ERROR_ON ((lengths[i] == 0 || binaries[i] == NULL),
CL_INVALID_VALUE,
"%i-th binary is NULL or its length==0\n", i);
}
}
// check for duplicates in device_list[].
for (i = 0; i < context->num_devices; i++)
{
int count = 0;
for (j = 0; j < num_devices; j++)
{
count += context->devices[i] == device_list[j];
}
// duplicate devices
POCL_GOTO_ERROR_ON((count > 1), CL_INVALID_DEVICE,
"device %s specified multiple times\n", context->devices[i]->long_name);
}
// convert subdevices to devices and remove duplicates
cl_uint real_num_devices = 0;
unique_devlist = pocl_unique_device_list(device_list, num_devices, &real_num_devices);
num_devices = real_num_devices;
device_list = unique_devlist;
// check for invalid devices in device_list[].
for (i = 0; i < num_devices; i++)
{
int found = 0;
for (j = 0; j < context->num_devices; j++)
{
found |= context->devices[j] == device_list[i];
}
POCL_GOTO_ERROR_ON((!found), CL_INVALID_DEVICE,
"device not found in the device list of the context\n");
POCL_GOTO_ERROR_ON (
(*(device_list[i]->available) == CL_FALSE), CL_DEVICE_NOT_AVAILABLE,
"Requested building for device '%s' but it is unavailable.\n",
device_list[i]->long_name);
}
if ((program = (cl_program) calloc (1, sizeof (struct _cl_program))) == NULL)
{
errcode = CL_OUT_OF_HOST_MEMORY;
goto ERROR;
}
POCL_INIT_OBJECT(program);
if ((program->binary_sizes = (size_t *)calloc (num_devices, sizeof (size_t)))
== NULL
|| (program->binaries
= (unsigned char **)calloc (num_devices, sizeof (unsigned char *)))
== NULL
|| (program->pocl_binaries
= (unsigned char **)calloc (num_devices, sizeof (unsigned char *)))
== NULL
|| (program->pocl_binary_sizes
= (size_t *)calloc (num_devices, sizeof (size_t)))
== NULL
|| (program->build_log = (char **)calloc (num_devices, sizeof (char *)))
== NULL
|| ((program->data = (void **)calloc (num_devices, sizeof (void *)))
== NULL)
|| ((program->global_var_total_size
= (size_t *)calloc (num_devices, sizeof (size_t)))
== NULL)
|| ((program->llvm_irs
= (void *)calloc (num_devices, sizeof (void *)))
== NULL)
|| ((program->gvar_storage
= (void *)calloc (num_devices, sizeof (void *)))
== NULL)
|| ((program->build_hash
= (SHA1_digest_t *)calloc (num_devices, sizeof (SHA1_digest_t)))
== NULL))
{
errcode = CL_OUT_OF_HOST_MEMORY;
goto ERROR_CLEAN_PROGRAM_AND_BINARIES;
}
program->context = context;
program->associated_num_devices = num_devices;
program->associated_devices = unique_devlist;
program->num_devices = num_devices;
program->devices = unique_devlist;
program->build_status = CL_BUILD_NONE;
program->binary_type = CL_PROGRAM_BINARY_TYPE_NONE;
TP_CREATE_PROGRAM (context->id, program->id);
char program_bc_path[POCL_MAX_PATHNAME_LENGTH];
if (allow_empty_binaries && (lengths == NULL) && (binaries == NULL))
goto SUCCESS;
for (i = 0; i < num_devices; ++i)
{
/* Poclcc binary */
if (pocl_binary_check_binary(device_list[i], binaries[i]))
{
program->pocl_binary_sizes[i] = lengths[i];
program->pocl_binaries[i] = (unsigned char*) malloc (lengths[i]);
memcpy (program->pocl_binaries[i], binaries[i], lengths[i]);
pocl_binary_set_program_buildhash (program, i, binaries[i]);
int error = pocl_cache_create_program_cachedir
(program, i, NULL, 0, program_bc_path);
POCL_GOTO_ERROR_ON((error != 0), CL_BUILD_PROGRAM_FAILURE,
"Could not create program cachedir");
POCL_GOTO_ERROR_ON(pocl_binary_deserialize (program, i),
CL_INVALID_BINARY,
"Could not unpack a pocl binary\n");
/* read program.bc if present; can be useful later */
if (pocl_exists (program_bc_path))
{
uint64_t size = 0;
pocl_read_file (program_bc_path,
(char **)(&program->binaries[i]), &size);
program->binary_sizes[i] = (size_t)size;
}
if (binary_status != NULL)
binary_status[i] = CL_SUCCESS;
}
/* check if the driver supports that binary */
else
{
cl_device_id device = program->associated_devices[i];
if (device->ops->supports_binary
&& device->ops->supports_binary (device, lengths[i],
(const char *)binaries[i]))
{
program->binary_sizes[i] = lengths[i];
program->binaries[i] = (unsigned char *)malloc (lengths[i]);
memcpy (program->binaries[i], binaries[i], lengths[i]);
if (binary_status != NULL)
binary_status[i] = CL_SUCCESS;
}
else
{
POCL_MSG_WARN ("Could not recognize binary for device %i\n", i);
if (binary_status != NULL)
binary_status[i] = CL_INVALID_BINARY;
errcode = CL_INVALID_BINARY;
goto ERROR_CLEAN_PROGRAM_AND_BINARIES;
}
}
}
SUCCESS:
POname(clRetainContext)(context);
POCL_ATOMIC_INC (program_c);
if (errcode_ret != NULL)
*errcode_ret = CL_SUCCESS;
return program;
ERROR_CLEAN_PROGRAM_AND_BINARIES:
if (program->binaries)
for (i = 0; i < num_devices; ++i)
POCL_MEM_FREE(program->binaries[i]);
POCL_MEM_FREE(program->binaries);
POCL_MEM_FREE(program->binary_sizes);
if (program->pocl_binaries)
for (i = 0; i < num_devices; ++i)
POCL_MEM_FREE(program->pocl_binaries[i]);
POCL_MEM_FREE (program->pocl_binaries);
POCL_MEM_FREE (program->pocl_binary_sizes);
POCL_MEM_FREE (program->data);
POCL_MEM_FREE (program->global_var_total_size);
POCL_MEM_FREE (program->llvm_irs);
POCL_MEM_FREE (program->gvar_storage);
POCL_MEM_FREE (program->build_log);
POCL_MEM_FREE (program->build_hash);
POCL_MEM_FREE (program);
ERROR:
POCL_MEM_FREE(unique_devlist);
if (errcode_ret != NULL)
*errcode_ret = errcode;
return NULL;
}
CL_API_ENTRY cl_program CL_API_CALL POname (clCreateProgramWithBinary) (
cl_context context, cl_uint num_devices, const cl_device_id *device_list,
const size_t *lengths, const unsigned char **binaries,
cl_int *binary_status, cl_int *errcode_ret) CL_API_SUFFIX__VERSION_1_0
{
return create_program_skeleton (context, num_devices, device_list, lengths,
binaries, binary_status, errcode_ret, 0);
}
POsym(clCreateProgramWithBinary)
|