OpenCL kernel binary cache invalidation strategy across driver updates
0 reputation · 21 Oct 2020, 14:05 UTC
0 reputation · 21 Oct 2020, 14:05 UTC
Applications that cache OpenCL kernel binaries via clGetProgramInfo(CL_PROGRAM_BINARIES) and reload them with clCreateProgramWithBinary face a gap: the specification guarantees binary compatibility only for the exact same CL_DEVICE_NAME and CL_DRIVER_VERSION pair, yet provides no mechanism to detect when a cached binary has become stale.
clCreateProgramWithBinary returns CL_INVALID_BINARY without a reason code (ISA change, compiler version, feature mismatch).Design a portable cache-invalidation policy that avoids silent JIT fallback latency spikes while keeping the cache usable across minor driver patches.
What combination of device-query fields (CL_DEVICE_NAME, CL_DRIVER_VERSION, CL_DRIVER_VERSION build number, vendor-specific extensions) should form the cache key? How can an application distinguish a genuinely incompatible binary from a corrupted cache file when CL_INVALID_BINARY is returned? Is there a vendor-agnostic way to trigger a clean recompile without deleting the entire vendor cache directory?
34775 reputation · 21 Oct 2020, 14:42 UTC
Use an application-managed cache keyed by CL_DEVICE_VENDOR, CL_DEVICE_NAME, and the major.minor portion of CL_DEVICE_DRIVER_VERSION. Optionally append a hash of CL_DEVICE_EXTENSIONS when the kernel depends on optional features. Store the binary with a checksum. On CL_INVALID_BINARY, compare the stored checksum to the cached file: mismatch means corruption, match means incompatibility. In both cases delete only that application-owned cache entry and recompile from source with clCreateProgramWithSource plus clBuildProgram, then refresh the cached binary with clGetProgramInfo(CL_PROGRAM_BINARIES). There is no standard API to purge a single entry from an ICD vendor cache directory, so do not delete files inside opaque vendor directories.
clCreateProgramWithBinary returns CL_INVALID_BINARY with no reason code.A minor driver patch often preserves the same major.minor version, so the cache can survive it. A build-number-only change should not by itself force invalidation. If the driver changes ISA or compiler internals within the same major.minor string, the binary will be rejected even though the key matches; the checksum test cannot detect that, only the CL_INVALID_BINARY result can.
CL_DEVICE_VENDOR, CL_DEVICE_NAME, and CL_DEVICE_DRIVER_VERSION for each device.vendor|name|driver-major.minor; add an extensions hash only if your kernel depends on optional features.clCreateProgramWithBinary. On CL_INVALID_BINARY:clCreateProgramWithSource, clBuildProgram, then clGetProgramInfo(CL_PROGRAM_BINARIES) to write the new binary and checksum.Confirm the key before reuse by re-querying the same device-info fields and comparing them to the stored key. After a failed binary load, retry from source; if clBuildProgram succeeds, the cached binary—not the device—was the problem. There is no portable command to list or clear an ICD cache directory.
Do you own the binary cache files, or are you relying on the ICD vendor cache directory? If you do not own the files, there is no safe portable way to invalidate a single entry, and the recommendation changes to always compiling from source or accepting vendor-specific behavior.
Assumes OpenCL 1.2 or later with an ICD loader. Driver version string parsing is heuristic because formats vary by vendor.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.