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
|
/* OpenCL runtime library: caching functions
Copyright (c) 2015-2023 pocl developers
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 <errno.h>
#include <stddef.h>
#include <stdio.h>
#include <string.h>
#include "config.h"
#include "common.h"
#include "pocl_build_timestamp.h"
#include "pocl_version.h"
#ifdef ENABLE_LLVM
#include "kernellib_hash.h"
#endif
#include "pocl_hash.h"
#include "pocl_cache.h"
#include "pocl_file_util.h"
#include "pocl_llvm.h"
#include "pocl_cl.h"
#include "pocl_runtime_config.h"
#define POCL_LAST_ACCESSED_FILENAME "/last_accessed"
/* The filename in which the program's build log is stored */
#define POCL_BUILDLOG_FILENAME "/build.log"
/* The filename in which the program source is stored in the program's temp
* dir. */
#define POCL_PROGRAM_CL_FILENAME "/program.cl"
/* The filename in which the program LLVM bc is stored in the program's temp
* dir. */
#define POCL_PROGRAM_BC_FILENAME "/program.bc"
#define POCL_PROGRAM_SPV_FILENAME "/program.spv"
static char cache_topdir[POCL_MAX_PATHNAME_LENGTH];
static char tempfile_pattern[POCL_MAX_PATHNAME_LENGTH];
static char tempdir_pattern[POCL_MAX_PATHNAME_LENGTH];
static int cache_topdir_initialized = 0;
static int use_kernel_cache = 0;
/* sanity check on SHA1 digest emptiness */
unsigned pocl_cache_buildhash_is_valid(cl_program program, unsigned device_i)
{
unsigned i, sum = 0;
for(i=0; i<sizeof(SHA1_digest_t); i++)
sum += program->build_hash[device_i][i];
return sum;
}
static void program_device_dir(char *path,
cl_program program,
unsigned device_i,
const char* append_path)
{
assert(path);
assert(program);
assert(device_i < program->num_devices);
assert(pocl_cache_buildhash_is_valid(program, device_i));
int bytes_written
= snprintf (path, POCL_MAX_PATHNAME_LENGTH, "%s/%s%s", cache_topdir,
program->build_hash[device_i], append_path);
assert (bytes_written > 0 && bytes_written < POCL_MAX_PATHNAME_LENGTH);
}
void pocl_cache_program_path(char* path,
cl_program program,
unsigned device_i)
{
program_device_dir (path, program, device_i, "");
}
POCL_EXPORT
void
pocl_cache_program_bc_path(char* program_bc_path,
cl_program program,
unsigned device_i) {
program_device_dir (program_bc_path, program,
device_i, POCL_PROGRAM_BC_FILENAME);
}
POCL_EXPORT
void
pocl_cache_program_spv_path (char *program_bc_path, cl_program program,
unsigned device_i)
{
program_device_dir (program_bc_path, program, device_i,
POCL_PROGRAM_SPV_FILENAME);
}
/**
* \brief Return a string clipped to a given length with a hash appended.
*
* Returns the string as is if it is not too long. Clips the string and appends
* a hash of the original string to make it uniqueish, if it's too long.
*
* \param str [in] the original string
* \param max_length [in] maximum size of the generated string in chars
* \param new_str [out] storage where the generated zero terminated
* string will be written (must have at least max_length + 1 of storage).
*/
static void
pocl_hash_clipped_name (const char *str, char *new_str)
{
size_t max_length = POCL_HASH_FILENAME_LENGTH;
if (strlen (str) > max_length)
{
SHA1_CTX hash_ctx;
uint8_t digest[SHA1_DIGEST_SIZE];
int i = 0;
char *new_str_pos = new_str;
pocl_SHA1_Init (&hash_ctx);
pocl_SHA1_Update (&hash_ctx, (uint8_t *)str, strlen (str));
pocl_SHA1_Final (&hash_ctx, digest);
strncpy (new_str, str, max_length - SHA1_DIGEST_SIZE * 2 - 1);
new_str_pos += max_length - SHA1_DIGEST_SIZE * 2 - 1;
#ifndef _WIN32
*new_str_pos++ = '.';
#else
*new_str_pos++ = ' ';
#endif
/* Convert the digest to an alphabetic string. */
for (i = 0; i < SHA1_DIGEST_SIZE; i++)
{
*new_str_pos++ = (digest[i] & 0x0F) + 65;
*new_str_pos++ = ((digest[i] & 0xF0) >> 4) + 65;
}
*new_str_pos = 0;
POCL_MSG_PRINT_GENERAL ("Generated a shortened name '%s'\n", new_str);
}
else
{
strncpy (new_str, str, strlen (str) + 1);
}
}
/* Return the cache directory for the given work-group function.
If specialized = 1, specialization parameters are derived from run_cmd,
otherwise a generic directory name is returned.
The current specialization parameters are:
- local size
- if the global offset is zero (in all dimensions) or not
- if the grid size in any dimension is smaller than a device
specified limit ("smallgrid" specialization)
*/
void
pocl_cache_kernel_cachedir_path (char *kernel_cachedir_path,
cl_program program, unsigned program_device_i,
cl_kernel kernel, const char *append_str,
_cl_command_node *command, int specialized)
{
int bytes_written;
_cl_command_run *run_cmd = &command->command.run;
char tempstring[POCL_MAX_PATHNAME_LENGTH];
cl_device_id dev = command->device;
size_t max_grid_width = pocl_cmd_max_grid_dim_width (run_cmd);
char kernel_dir_name[POCL_MAX_DIRNAME_LENGTH + 1];
pocl_hash_clipped_name (kernel->name, &kernel_dir_name[0]);
bytes_written = snprintf (
tempstring, POCL_MAX_PATHNAME_LENGTH, "/%s/%zu-%zu-%zu%s%s%s",
kernel_dir_name, !specialized ? 0 : run_cmd->pc.local_size[0],
!specialized ? 0 : run_cmd->pc.local_size[1],
!specialized ? 0 : run_cmd->pc.local_size[2],
(specialized && run_cmd->pc.global_offset[0] == 0
&& run_cmd->pc.global_offset[1] == 0
&& run_cmd->pc.global_offset[2] == 0)
? "-goffs0"
: "",
specialized && !run_cmd->force_large_grid_wg_func
&& max_grid_width < dev->grid_width_specialization_limit
? "-smallgrid"
: "",
append_str);
assert (bytes_written > 0 && bytes_written < POCL_MAX_PATHNAME_LENGTH);
program_device_dir (kernel_cachedir_path, program, program_device_i,
tempstring);
}
void
pocl_cache_kernel_cachedir (char *kernel_cachedir_path, cl_program program,
unsigned device_i, const char *kernel_name)
{
int bytes_written;
char tempstring[POCL_MAX_PATHNAME_LENGTH];
char file_name[POCL_MAX_FILENAME_LENGTH + 1];
pocl_hash_clipped_name (kernel_name, &file_name[0]);
bytes_written
= snprintf (tempstring, POCL_MAX_PATHNAME_LENGTH, "/%s", file_name);
assert (bytes_written > 0 && bytes_written < POCL_MAX_PATHNAME_LENGTH);
program_device_dir (kernel_cachedir_path, program, device_i, tempstring);
}
// required in llvm API
void
pocl_cache_work_group_function_path (char *parallel_bc_path,
cl_program program, unsigned device_i,
cl_kernel kernel,
_cl_command_node *command, int specialize)
{
assert (kernel->name);
pocl_cache_kernel_cachedir_path (parallel_bc_path, program, device_i, kernel,
POCL_PARALLEL_BC_FILENAME, command,
specialize);
}
/* Return the final binary path for the given work-group function.
If specialized is 1, find the WG function specialized for the
given command's properties, if 0, return the path to a generic version. */
void
pocl_cache_final_binary_path (char *final_binary_path, cl_program program,
unsigned device_i, cl_kernel kernel,
_cl_command_node *command, int specialized)
{
assert (kernel->name);
/* TODO: This should be probably refactored to either get the binary name
from the device itself, or let the device ops call
pocl_llvm_generate_workgroup_function() on their own */
int bytes_written;
char final_binary_name[POCL_MAX_PATHNAME_LENGTH];
/* FIXME: Why different naming for SPMD and why the .brig suffix? */
if (kernel->program->devices[device_i]->spmd)
bytes_written = snprintf (final_binary_name, POCL_MAX_PATHNAME_LENGTH,
"%s.brig", POCL_PARALLEL_BC_FILENAME);
else
{
char file_name[POCL_MAX_FILENAME_LENGTH + 1];
pocl_hash_clipped_name (kernel->name, &file_name[0]);
bytes_written = snprintf (final_binary_name, POCL_MAX_PATHNAME_LENGTH,
#ifdef _WIN32
"/%s.dll",
#else
"/%s.so",
#endif
file_name);
}
assert (bytes_written > 0 && bytes_written < POCL_MAX_PATHNAME_LENGTH);
pocl_cache_kernel_cachedir_path (final_binary_path, program, device_i,
kernel, final_binary_name, command,
specialized);
}
/******************************************************************************/
/******************************************************************************/
int
pocl_cache_create_tempdir (char *path)
{
return pocl_mk_tempdir (path, tempdir_pattern);
}
POCL_EXPORT
int
pocl_cache_tempname (char *path, const char *suffix, int *fd)
{
assert (cache_topdir_initialized);
assert (path);
return pocl_mk_tempname (path, tempfile_pattern, suffix, fd);
}
int
pocl_cache_write_program_source (char *program_cl_path, cl_program program)
{
return pocl_write_tempfile (program_cl_path, tempfile_pattern, ".cl",
program->source, strlen (program->source));
}
int
pocl_cache_write_kernel_objfile (char *objfile_path,
const char *objfile_content,
uint64_t objfile_size)
{
return pocl_write_tempfile (objfile_path, tempfile_pattern,
OBJ_EXT,
objfile_content, objfile_size);
}
int
pocl_cache_write_spirv (char *spirv_path,
const char *spirv_content,
uint64_t file_size)
{
return pocl_write_tempfile (spirv_path, tempfile_pattern, ".spirv",
spirv_content, file_size);
}
int
pocl_cache_write_kernel_asmfile (char *asmfile_path,
const char *asmfile_content,
uint64_t asmfile_size)
{
return pocl_write_tempfile (asmfile_path, tempfile_pattern, ASM_EXT,
asmfile_content, asmfile_size);
}
int
pocl_cache_write_generic_objfile (char *objfile_path,
const char *objfile_content,
uint64_t objfile_size)
{
return pocl_write_tempfile (objfile_path, tempfile_pattern, ".binary",
objfile_content, objfile_size);
}
int
pocl_cache_write_header (char *header_path,
const char *header_name,
const char *header_content,
uint64_t header_size)
{
int bytes_written = snprintf (header_path, POCL_MAX_PATHNAME_LENGTH, "%s/%s",
cache_topdir, header_name);
assert (bytes_written > 0 && bytes_written < POCL_MAX_PATHNAME_LENGTH);
return pocl_write_file (header_path, header_content, header_size, 0);
}
/******************************************************************************/
int pocl_cache_update_program_last_access(cl_program program,
unsigned device_i) {
if (!use_kernel_cache)
return 0;
char last_accessed_path[POCL_MAX_PATHNAME_LENGTH];
program_device_dir (last_accessed_path, program, device_i,
POCL_LAST_ACCESSED_FILENAME);
return pocl_touch_file (last_accessed_path);
}
/******************************************************************************/
int pocl_cache_device_cachedir_exists(cl_program program,
unsigned device_i) {
char device_cachedir_path[POCL_MAX_PATHNAME_LENGTH];
program_device_dir (device_cachedir_path, program, device_i, "");
return pocl_exists (device_cachedir_path);
}
/******************************************************************************/
int
pocl_cache_write_descriptor (_cl_command_node *command, cl_kernel kernel,
int specialize, const char *content, size_t size)
{
char dirr[POCL_MAX_PATHNAME_LENGTH];
pocl_cache_kernel_cachedir_path (dirr, kernel->program, command->program_device_i,
kernel, "", command, specialize);
char descriptor[POCL_MAX_PATHNAME_LENGTH];
pocl_cache_kernel_cachedir_path (
descriptor, kernel->program, command->program_device_i, kernel,
"/../descriptor.so.kernel_obj.c", command, specialize);
if (pocl_exists (descriptor))
return 0;
if (pocl_mkdir_p (dirr))
return -1;
return pocl_write_file (descriptor, content, size, 0);
}
/******************************************************************************/
char* pocl_cache_read_buildlog(cl_program program,
unsigned device_i) {
char buildlog_path[POCL_MAX_PATHNAME_LENGTH];
if (program->build_hash[device_i][0] == 0)
return NULL;
program_device_dir (buildlog_path, program, device_i,
POCL_BUILDLOG_FILENAME);
if (!pocl_exists (buildlog_path))
return NULL;
char *res = NULL;
uint64_t filesize;
if (pocl_read_file (buildlog_path, &res, &filesize))
return NULL;
return res;
}
int pocl_cache_append_to_buildlog(cl_program program,
unsigned device_i,
const char *content,
size_t size) {
if (!pocl_cache_buildhash_is_valid (program, device_i))
return -1;
char buildlog_path[POCL_MAX_PATHNAME_LENGTH];
program_device_dir(buildlog_path, program,
device_i, POCL_BUILDLOG_FILENAME);
return pocl_write_file(buildlog_path, content, size, 1);
}
/******************************************************************************/
#ifdef ENABLE_LLVM
int
pocl_cache_write_kernel_parallel_bc (void *bc, cl_program program,
int device_i, cl_kernel kernel,
_cl_command_node *command, int specialize)
{
assert (bc);
char kernel_parallel_path[POCL_MAX_PATHNAME_LENGTH];
pocl_cache_kernel_cachedir_path (kernel_parallel_path, program, device_i,
kernel, "", command, specialize);
int err = pocl_mkdir_p (kernel_parallel_path);
if (err)
{
POCL_MSG_PRINT_GENERAL ("Unable to create directory %s.\n",
kernel_parallel_path);
return err;
}
assert (strlen (kernel_parallel_path)
< (POCL_MAX_PATHNAME_LENGTH - strlen (POCL_PARALLEL_BC_FILENAME)));
strcat (kernel_parallel_path, POCL_PARALLEL_BC_FILENAME);
return pocl_write_module (bc, kernel_parallel_path);
}
#endif
/******************************************************************************/
static inline void
build_program_compute_hash (cl_program program, unsigned device_i,
const char *hash_source, size_t source_len)
{
SHA1_CTX hash_ctx;
unsigned i;
cl_device_id device = program->devices[device_i];
static const char *builtin_seed = POCL_VERSION_BASE POCL_BUILD_TIMESTAMP
#ifdef ENABLE_LLVM
LLVM_VERSION POCL_KERNELLIB_SHA1
#endif
;
pocl_SHA1_Init(&hash_ctx);
pocl_SHA1_Update (&hash_ctx, (uint8_t *)builtin_seed,
strlen (builtin_seed));
assert (hash_source);
assert (source_len > 0);
pocl_SHA1_Update (&hash_ctx, (uint8_t *)hash_source, source_len);
if (program->compiler_options)
pocl_SHA1_Update(&hash_ctx, (uint8_t*) program->compiler_options,
strlen(program->compiler_options));
pocl_SHA1_Update (&hash_ctx,
(uint8_t *)&program->binary_type,
sizeof(cl_program_binary_type));
#ifdef ENABLE_LLVM
/* The kernel compiler work-group function method affects the
produced binary heavily. */
if (device->llvm_target_triplet)
{
const char *wg_method
= pocl_get_string_option ("POCL_WORK_GROUP_METHOD", NULL);
if (wg_method)
pocl_SHA1_Update (&hash_ctx, (uint8_t *)wg_method,
strlen (wg_method));
}
#endif
#ifdef ENABLE_SPIRV
for (size_t i = 0; i < program->num_spec_consts; ++i)
{
if (program->spec_const_is_set[i])
{
pocl_SHA1_Update (&hash_ctx,
(uint8_t *)&program->spec_const_ids[i],
sizeof (cl_uint));
pocl_SHA1_Update (&hash_ctx,
(uint8_t *)&program->spec_const_values[i],
program->spec_const_sizes[i]);
}
}
#endif
/*devices may include their own information to hash */
if (device->ops->build_hash)
{
char *dev_hash = device->ops->build_hash(device);
pocl_SHA1_Update(&hash_ctx, (const uint8_t *)dev_hash, strlen(dev_hash));
free(dev_hash);
}
uint8_t digest[SHA1_DIGEST_SIZE];
pocl_SHA1_Final(&hash_ctx, digest);
unsigned char* hashstr = program->build_hash[device_i];
for (i=0; i < SHA1_DIGEST_SIZE; i++)
{
*hashstr++ = (digest[i] & 0x0F) + 65;
*hashstr++ = ((digest[i] & 0xF0) >> 4) + 65;
}
*hashstr = 0;
program->build_hash[device_i][2] = '/';
}
/******************************************************************************/
int
pocl_cache_init_topdir ()
{
if (cache_topdir_initialized)
return 0;
use_kernel_cache
= pocl_get_bool_option ("POCL_KERNEL_CACHE", POCL_KERNEL_CACHE_DEFAULT);
const char *tmp_path = pocl_get_string_option ("POCL_CACHE_DIR", NULL);
int needed;
if (tmp_path)
{
needed = snprintf (cache_topdir, POCL_MAX_PATHNAME_LENGTH, "%s", tmp_path);
} else {
#ifdef __ANDROID__
POCL_MSG_ERR ("Please set the POCL_CACHE_DIR env var to your app's "
"cache directory (Context.getCacheDir())\n");
return CL_FAILED;
#elif defined(_WIN32)
tmp_path = pocl_get_string_option ("LOCALAPPDATA", NULL);
if (!tmp_path) {
tmp_path = pocl_get_string_option ("TEMP", NULL);
}
if (tmp_path == NULL)
return CL_FAILED;
needed = snprintf (cache_topdir, POCL_MAX_PATHNAME_LENGTH, "%s\\pocl",
tmp_path);
#else
// "If $XDG_CACHE_HOME is either not set or empty, a default equal to
// $HOME/.cache should be used."
// https://standards.freedesktop.org/basedir-spec/latest/
tmp_path = getenv("XDG_CACHE_HOME");
const char *p;
if (use_kernel_cache)
p = "pocl/kcache";
else
p = "pocl/uncached";
if (tmp_path && tmp_path[0] != '\0') {
needed = snprintf (cache_topdir, POCL_MAX_PATHNAME_LENGTH, "%s/%s",
tmp_path, p);
}
else if ((tmp_path = getenv("HOME")) != NULL) {
needed = snprintf (cache_topdir, POCL_MAX_PATHNAME_LENGTH,
"%s/.cache/%s", tmp_path, p);
}
else {
needed = snprintf (cache_topdir, POCL_MAX_PATHNAME_LENGTH, "/tmp/%s",
p);
}
#endif
}
if (needed >= POCL_MAX_PATHNAME_LENGTH)
{
POCL_MSG_ERR ("pocl: cache path longer than maximum filename length\n");
return CL_FAILED;
}
assert(strlen(cache_topdir) > 0);
if (pocl_mkdir_p(cache_topdir))
{
POCL_MSG_ERR (
"Could not create top directory (%s) for cache. \n\nNote: "
"if you have proper rights to create that directory, and still "
"get the error, then most likely pocl and the program you're "
"trying to run are linked to different versions of libstdc++ "
"library. \nThis is not a bug in pocl and there's nothing we "
"can do to fix it - you need both pocl and your program to be"
" compiled for your system. This is known to happen with "
"Luxmark benchmark binaries downloaded from website; Luxmark "
"installed from your linux distribution's packages should "
"work.\n",
cache_topdir);
return CL_FAILED;
}
strncpy (tempfile_pattern, cache_topdir, POCL_MAX_PATHNAME_LENGTH);
size_t len = strlen (tempfile_pattern);
strncpy (tempfile_pattern + len, "/tempfile",
(POCL_MAX_PATHNAME_LENGTH - len));
tempfile_pattern[POCL_MAX_PATHNAME_LENGTH - 1] = 0;
assert (strlen (tempfile_pattern) < POCL_MAX_PATHNAME_LENGTH);
int bytes_written;
if (use_kernel_cache)
{
bytes_written = snprintf (tempdir_pattern, POCL_MAX_PATHNAME_LENGTH,
"%s/tempdir", cache_topdir);
assert (bytes_written > 0 && bytes_written < POCL_MAX_PATHNAME_LENGTH);
}
else
{
bytes_written = snprintf (tempdir_pattern, POCL_MAX_PATHNAME_LENGTH,
"%s/_UNCACHED", cache_topdir);
assert (bytes_written > 0 && bytes_written < POCL_MAX_PATHNAME_LENGTH);
}
cache_topdir_initialized = 1;
return CL_SUCCESS;
}
/* Create the new program cachedir, invalidating the old program
* binaries and IRs if the new computed hash is different from the old
* one. The source hash is computed from the preprocessed source
* if present, from the original source otherwise: this is to ensure
* that cache-related functions (which include log retrieval) still
* work correctly even if preprocessing fails
*/
POCL_EXPORT
int
pocl_cache_create_program_cachedir (cl_program program, unsigned device_i,
const char *hash_source,
size_t hash_source_len,
char *program_bc_path)
{
assert(cache_topdir_initialized);
assert (program_bc_path);
/* NULL is used only in one place, clCreateWithBinary,
* and we want to keep the original hash value in that case */
if (hash_source == NULL)
{
assert (pocl_cache_buildhash_is_valid (program, device_i));
program_device_dir (program_bc_path, program, device_i, "");
if (pocl_mkdir_p (program_bc_path))
return 1;
}
else if (use_kernel_cache)
{
build_program_compute_hash (program, device_i, hash_source,
hash_source_len);
assert (pocl_cache_buildhash_is_valid (program, device_i));
program_device_dir (program_bc_path, program, device_i, "");
if (pocl_mkdir_p (program_bc_path))
return 1;
}
else
{
/* if kernel cache is disabled, use a random dir. */
char random_dir[POCL_MAX_PATHNAME_LENGTH];
if (pocl_cache_create_tempdir (random_dir))
return 1;
size_t s = strlen (cache_topdir) + 1;
assert (strlen (random_dir) == (s + 16));
memcpy (program->build_hash[device_i], random_dir + s, 16);
}
pocl_cache_program_bc_path (program_bc_path, program, device_i);
return 0;
}
void pocl_cache_cleanup_cachedir(cl_program program) {
/* only rm -rf if kernel cache is disabled */
if (use_kernel_cache)
return;
unsigned i;
for (i = 0; i < program->num_devices; i++)
{
if (!pocl_cache_buildhash_is_valid (program, i))
continue;
char cachedir[POCL_MAX_PATHNAME_LENGTH];
program_device_dir (cachedir, program, i, "");
pocl_rm_rf (cachedir);
}
}
/******************************************************************************/
|