[llvm] [offload][sycl] add context parameter to olCreateProgram (PR #218387)

Jan Trusiłło via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 27 05:23:33 PDT 2026


================
@@ -144,7 +144,8 @@ ProgramAndKernelManager::getOrCreateKernel(DeviceKernelInfo &KernelInfo,
                         KernelInfo.getName().data() + " was found");
 
   auto DeviceHandle = Device.getOLHandle();
-  auto Program = DeviceImage.getOrCreateProgram(DeviceHandle);
+  auto Program =
+      DeviceImage.getOrCreateProgram(Context.getOLHandleRef(), DeviceHandle);
----------------
311Volt wrote:

Pasting Codex analysis below for reference:

---

The key point: none of these caches is currently SYCL-context-scoped. They belong to a runtime-global `ProgramAndKernelManager` singleton.

| Cache | Owner/scope | Key | Stored value | Purpose |
|---|---|---|---|---|
| `MDeviceImageManagers` | Global manager | Fat-binary start address | Vector of `DeviceImageManager`s | Own parsed device images and tie them to library registration |
| `MDeviceKernelInfoMap` | Global manager | Kernel name | `DeviceKernelInfo` | Find the image containing a named kernel |
| `DeviceImageManager::MPrograms` | One device-image entry | Device handle | Owning `ProgramWrapper`/`ol_program_handle_t` | Load each image only once per device |
| `DeviceKernelInfo::MBuiltKernels` | One named kernel | Device handle | `ol_symbol_handle_t` | Resolve each kernel symbol only once per device |
| Template static `Info` | One C++ kernel-name instantiation | Implicit template type | Reference to `DeviceKernelInfo` | Avoid repeated kernel-name map lookup |

The hierarchy is approximately:

```text
ProgramAndKernelManager (runtime-global singleton)
├── fat-binary address
│   └── device image
│       └── device handle → ol_program_handle_t
└── kernel name
    └── device handle → ol_symbol_handle_t
```

On first submission:

1. Kernel name selects a `DeviceKernelInfo`.
2. Its `(kernel name, device)` symbol cache is checked.
3. On a miss, the image’s `device → program` cache is checked.
4. On a miss, `olCreateProgram(context, device, image)` creates the program.
5. `olGetSymbol` creates the kernel handle and caches it by device.

On subsequent submissions, the context is bypassed:

```cpp
if (auto Kernel = KernelInfo.getKernel(Device.getOLHandle()))
  return Kernel;
```

That cache is in [device_kernel_info.hpp](libsycl/src/detail/device_kernel_info.hpp:47). The program cache is likewise keyed only by device in [device_image_wrapper.hpp](libsycl/src/detail/device_image_wrapper.hpp:93).

Consequences:

- Same kernel, same device, different context: the kernel-symbol cache immediately returns the first context’s symbol.
- Different kernel from the same image, same device, different context: the program cache returns the first context’s program.

Both should effectively be keyed by `(context, device)`—with image and kernel identity already supplied by their owning objects.

The caches live until the fat binary is unregistered or libsycl shuts down. Removing an image destroys its `ProgramWrapper`s, which call `olDestroyProgram` ([device_image_wrapper.cpp](libsycl/src/detail/device_image_wrapper.cpp:27)).

https://github.com/llvm/llvm-project/pull/218387


More information about the llvm-commits mailing list