64 Commits

Author SHA1 Message Date
Ross Brunton
2e74cc6c04
[Offload][NFC] Use a sensible order for APIGen (#154518)
The order entries in the tablegen API files are iterated is not the
order
they appear in the file. To avoid any issues with the order changing
in future, we now generate all definitions of a certain class before
class that can use them.

This is a NFC; the definitions don't actually change, just the order
they exist in in the OffloadAPI.h header.
2025-08-21 09:38:21 +01:00
Ross Brunton
273ca1f77b
[Offload] Fix OL_DEVICE_INFO_MAX_MEM_ALLOC_SIZE on AMD (#154521)
This wasn't handled with the normal info API, so needs special handling.
2025-08-21 09:37:58 +01:00
Ross Brunton
c8986d1ecb
[Offload] Guard olMemAlloc/Free with a mutex (#153786)
Both these functions update an `AllocInfoMap` structure in the context,
however they did not use any locks, causing random failures in threaded
code. Now they use a mutex.
2025-08-20 13:23:57 +01:00
Ross Brunton
2c11a83691
[Offload] Add olCalculateOptimalOccupancy (#142950)
This is equivalent to `cuOccupancyMaxPotentialBlockSize`. It is
currently
only implemented on Cuda; AMDGPU and Host return unsupported.

---------

Co-authored-by: Callum Fare <callum@codeplay.com>
2025-08-19 15:16:47 +01:00
Rafal Bielski
9c9d9e4cb6
[Offload] Define additional device info properties (#152533)
Add the following properties in Offload device info:
* VENDOR_ID
* NUM_COMPUTE_UNITS
* [SINGLE|DOUBLE|HALF]_FP_CONFIG
* NATIVE_VECTOR_WIDTH_[CHAR|SHORT|INT|LONG|FLOAT|DOUBLE|HALF]
* MAX_CLOCK_FREQUENCY
* MEMORY_CLOCK_RATE
* ADDRESS_BITS
* MAX_MEM_ALLOC_SIZE
* GLOBAL_MEM_SIZE

Add a bitfield option to enumerators, allowing the values to be
bit-shifted instead of incremented. Generate the per-type enums using
`foreach` to reduce code duplication.

Use macros in unit test definitions to reduce code duplication.
2025-08-19 13:02:01 +01:00
Ross Brunton
30c7951136
[Offload] olLaunchHostFunction (#152482)
Add an `olLaunchHostFunction` method that allows enqueueing host work
to the stream.
2025-08-15 09:39:48 +01:00
Ross Brunton
3e9f29cfee
[Offload] Store globals in the program's global list rather than the kernel list (#153441) 2025-08-13 17:18:25 +01:00
Ross Brunton
910d7e90bf
[Offload] Make olLaunchKernel test thread safe (#149497)
This sprinkles a few mutexes around the plugin interface so that the
olLaunchKernel CTS test now passes when ran on multiple threads.

Part of this also involved changing the interface for device synchronise
so that it can optionally not free the underlying queue (which
introduced a race condition in liboffload).
2025-08-08 10:57:04 +01:00
Ross Brunton
197d1c1570
[Offload] OL_QUEUE_INFO_EMPTY (#152473)
Add a queue query that (if possible) reports whether the queue is empty
2025-08-08 10:20:45 +01:00
Ross Brunton
a44532544b
[Offload] Don't create events for empty queues (#152304)
Add a device function to check if a device queue is empty. If liboffload
tries to create an event for an empty queue, we create an "empty" event
that is already complete.

This allows `olCreateEvent`, `olSyncEvent` and `olWaitEvent` to run
quickly for empty queues.
2025-08-07 10:16:33 +01:00
Ross Brunton
ca13c44bbc
[NFC][Offload] Clarify olDestroyQueue (#152132)
This has no code changes.
2025-08-06 15:34:31 +01:00
Ross Brunton
d03692a00e
[Offload] Rework MAX_WORK_GROUP_SIZE (#151926)
`MAX_WORK_GROUP_SIZE` now represents the maximum total number of work
groups the device can allocate, rather than the maximum per dimension.
`MAX_WORK_GROUP_SIZE_PER_DIMENSION` has been added, which has the old
behaviour.
2025-08-04 15:21:24 +01:00
Ross Brunton
adb2421202
[Offload] Refactor device information queries to use new tagging (#147318)
Instead using strings to look up device information (which is brittle
and slow), use the new tags that the plugins specify when building the
nodes.
2025-07-25 14:51:51 +01:00
Ross Brunton
690c3ee5be
[Offload] Replace "EventOut" parameters with olCreateEvent (#150217)
Rather than having every "enqueue"-type function have an output pointer
specifically for an output event, just provide an `olCreateEvent`
entrypoint which pushes an event to the queue.

For example, replace:
```cpp
olMemcpy(Queue, ..., EventOut);
```
with
```cpp
olMemcpy(Queue, ...);
olCreateEvent(Queue, EventOut);
```
2025-07-24 14:31:06 +01:00
Ross Brunton
081b74caf5
[Offload] Add olWaitEvents (#150036)
This function causes a queue to wait until all the provided events have
completed before running any future scheduled work.
2025-07-23 14:12:16 +01:00
Ross Brunton
2726b7fb1c
[Offload] Rename olWaitEvent/Queue to olSyncEvent/Queue (#150023)
This more closely matches the nomenclature used by CUDA, AMDGPU and
the plugin interface.
2025-07-23 10:52:13 +01:00
Ross Brunton
55b417a75f
[Offload] Cache symbols in program (#148209)
When creating a new symbol, check that it already exists. If it does,
return that pointer rather than building a new symbol structure.
2025-07-16 18:32:47 +01:00
Callum Fare
47c9609a86
[Offload] Check plugins aren't already deinitialized when tearing down (#148642)
This is a hotfix for #148615 - it fixes the issue for me locally.

I think a broader issue is that in the test environment we're calling
olShutDown from a global destructor in the test binaries. We should do
something more controlled, either calling olInit/olShutDown in every
test, or move those to a GTest global environment. I didn't do that
originally because it looked like it needed changes to LLVM's GTest
wrapper.
2025-07-14 16:17:10 +01:00
Ross Brunton
2fdeeefacf
[Offload] Add global variable address/size queries (#147972)
Add two new symbol info types for getting the bounds of a global
variable. As well as a number of tests for reading/writing to it.
2025-07-11 16:12:48 +01:00
Ross Brunton
84e15d08c2
[Offload] Add olGetSymbolInfo[Size] (#147962)
This mirrors the similar functions for other handles. The only
implemented info at the moment is the symbol's kind.
2025-07-11 15:29:53 +01:00
Ross Brunton
eee723f928
[Offload] Replace GetKernel with GetSymbol with global support (#148221)
`olGetKernel` has been replaced by `olGetSymbol` which accepts a
`Kind` parameter. As well as loading information about kernels, it
can now also load information about global variables.
2025-07-11 14:48:10 +01:00
Ross Brunton
466357ab51
[Offload] Change ol_kernel_handle_t -> ol_symbol_handle_t (#147943)
In the future, we want `ol_symbol_handle_t` to represent both kernels
and global variables The first step in this process is a rename and
promotion to a "typed handle".
2025-07-10 14:54:10 +01:00
Kenneth Benzie (Benie)
cea33304c0
[Offload] Add Offload API Sphinx documentation (#147323)
* Add spec generation to offload-tblgen tool
* This patch adds generation of Sphinx compatible reStructuedText
utilizing the C domain to document the Offload API directly from the
spec definition `.td` files.
* Add Sphinx HTML documentation target
* Introduces the `docs-offload-html` target when CMake is configured
with `LLVM_ENABLE_SPHINX=ON` and `SPHINX_OUTPUT_HTML=ON`. Utilized
`offload-tblgen -gen-spen` to generate Offload API specification docs.
2025-07-10 11:50:51 +01:00
Callum Fare
7c6edf4a05
[Offload] Implement olGetQueueInfo, olGetEventInfo (#142947)
Add info queries for queues and events.

`olGetQueueInfo` only supports getting the associated device. We were
already tracking this so we can implement this for free. We will likely
add other queries to it in the future (whether the queue is empty, what
flags it was created with, etc)

`olGetEventInfo` only supports getting the associated queue. This is
another thing we were already storing in the handle. We'll be able to
add other queries in future (the event type, status, etc)
2025-07-09 17:09:31 +01:00
Ross Brunton
8c06d0e547
[Offload] Generate OffloadInfo.inc (#147316)
This is a generated file which contains a macro for all Device Info
keys. This is visible to the plugin interface so that it can use the
definitions in a future patch.
2025-07-09 11:35:22 +01:00
Callum Fare
3c0571a749
[Offload] Add missing license header to Common.td (#146737)
All other tablegen files in this directory have the license header, but
`Common.td` is missing it
2025-07-02 17:17:30 +01:00
Ross Brunton
7d52b0983e
[Offload] Add MAX_WORK_GROUP_SIZE device info query (#143718)
This adds a new device info query for the maximum workgroup/block size
for each dimension.
2025-07-02 16:33:54 +01:00
Callum Fare
acb52a8a98
[Offload] Improve liboffload documentation (#142403)
- Update the main README to reflect the current project status
- Rework the main API generation documentation. General fixes/tidying,
but also spell out explicitly how to make API changes at the top of the
document since this is what most people will care about.

---------

Co-authored-by: Martin Grant <martingrant@outlook.com>
2025-07-02 13:52:27 +01:00
Ross Brunton
67e73ba605
[Offload] Refactor device/platform info queries (#146345)
This makes several small changes to how the platform and device info
queries are handled:
* ReturnHelper has been replaced with InfoWriter which is more explicit
  in how it is invoked.
* InfoWriter consumes `llvm::Expected` rather than values directly, and
  will early exit if it returns an error.
* As a result of the above, `GetInfoString` now correctly returns errors
  rather than empty strings.
* The host device now has its own dedicated "getInfo" function rather
  than being checked in multiple places.
2025-06-30 15:00:43 +01:00
Ross Brunton
003145d0c8
[Offload] Implement olShutDown (#144055)
`olShutDown` was not properly calling deinit on the platforms, resulting
in random segfaults on AMD devices.

As part of this, `olInit` and `olShutDown` now alloc and free the
offload context rather than it being static. This
allows `olShutDown` to be called within a destructor of a static object
(like the tests do) without having to worry about destructor ordering.
2025-06-30 12:14:00 +01:00
Ross Brunton
39f19f2f1f
[Offload] Store device info tree in device handle (#145913)
Rather than creating a new device info tree for each call to
`olGetDeviceInfo`, we instead do it on device initialisation. As well
as improving performance, this fixes a few lifetime issues with returned
strings.

This does unfortunately mean that device information is immutable,
but hopefully that shouldn't be a problem for any queries we want to
implement.

This also meant allowing offload initialization to fail, which it can
now do.
2025-06-27 15:10:43 +01:00
Ross Brunton
0870c8838b
[Offload] Add an unloadBinary interface to PluginInterface (#143873)
This allows removal of a specific Image from a Device, rather than
requiring all image data to outlive the device they were created for.

This is required for `ol_program_handle_t`s, which now specify the
lifetime of the buffer used to create the program.
2025-06-25 14:53:18 +01:00
Ross Brunton
4359e55838
[Offload] Properly report errors when jit compiling (#145498)
Previously, if a binary failed to load due to failures when jit
compiling, the function would return success with nullptr. Now it
returns a new plugin error, `COMPILE_FAILURE`.
2025-06-24 16:27:12 +01:00
Ross Brunton
f242360e15
[Offload] Add type information to device info nodes (#144535)
Rather than being "stringly typed", store values as a std::variant that
can hold various types. This means that liboffload doesn't have to do
any string parsing for integer/bool device info keys.
2025-06-20 09:05:05 -05:00
Ross Brunton
e0633d59b9
[Offload] Check for initialization (#144370)
All entry points (except olInit) now check that offload has been
initialized. If not, a new `OL_ERRC_UNINITIALIZED` error is returned.
2025-06-20 09:04:50 -05:00
Ross Brunton
53336ad488
[Offload] Move (most) global state to an OffloadContext struct (#144494)
Rather than having a number of static local variables, we now use
a single `OffloadContext` struct to store global state. This is
initialised by `olInit`, but is never deleted (de-initialization of
Offload isn't yet implemented).

The error reporting mechanism has not been moved to the struct, since
that's going to cause issues with teardown (error messages must outlive
liboffload).
2025-06-19 16:02:03 -05:00
Ross Brunton
e6a3579653
[Offload] Replace device info queue with a tree (#144050)
Previously, device info was returned as a queue with each element having
a "Level" field indicating its nesting level. This replaces this queue
with a more traditional tree-like structure.

This should not result in a change to the output of
`llvm-offload-device-info`.
2025-06-13 09:22:47 -05:00
Ross Brunton
4f60321ca1
[Offload] Add ol_dimensions_t and convert ranges from size_t -> uint32_t (#143901)
This is a three element x, y, z size_t vector that can be used any place
where a 3D vector is required. This ensures that all vectors across
liboffload are the same and don't require any resizing/reordering
dances.
2025-06-12 09:59:59 -05:00
Callum Fare
835497a4dc
[Offload] Make olMemcpy src parameter const (#143161) 2025-06-06 10:25:00 -05:00
Ross Brunton
269c29ae67
[Offload] Allow setting null arguments in olLaunchKernel (#141958) 2025-06-06 07:05:11 -05:00
Callum Fare
f44df93a9c
[Offload] Explicitly create directories that contain tablegen output (#142817)
This isn't required when building with Ninja, but with the Makefile
generator these directories don't get implicitly created.
2025-06-04 13:46:19 -05:00
Callum Fare
817af2ddf2
[Offload] Fix missing dependencies in Offload API generation (#142776)
Thanks to @RossBrunton for spotting this.

We attempt to clang-format the generated Offload header files, but if
clang-format isn't available we just copy the generated files instead.
That fallback path was missing the correct dependencies.

Fixes #142756
2025-06-04 08:51:50 -05:00
Callum Fare
b78bc35d16
[Offload] Don't check in generated files (#141982)
Previously we decided to check in files that we generate with tablegen.
The justification at the time was that it helped reviewers unfamiliar
with `offload-tblgen` see the actual changes to the headers in PRs.
After trying it for a while, it's ended up causing some headaches and is
also not how tablegen is used elsewhere in LLVM.

This changes our use of tablegen to be more conventional. Where
possible, files are still clang-formatted, but this is no longer a hard
requirement. Because `OffloadErrcodes.inc` is shared with libomptarget
it now gets generated in a more appropriate place.
2025-06-03 10:39:04 -05:00
Ross Brunton
7efb79b705
[Offload] Fix Error checking (#141939)
All errors must be checked - this includes the local variable we were
using to increase the lifetime of `Res`. As we were not explicitly
checking it, it resulted in an `abort` in debug builds.
2025-05-29 08:17:08 -05:00
Joseph Huber
0ebe5557d9
[Offload] Add specifier for the host type (#141635)
Summary:
We use this sepcial type to indicate a host value, this will be refined
later but for now it's used as a stand-in device for transfers and
queues. It needs a special kind because it is not a device target as the
other ones so we need to differentiate it between a CPU and GPU type.

Fixes: https://github.com/llvm/llvm-project/issues/141436
2025-05-28 08:51:14 -05:00
Joseph Huber
a9b64bb318
[Offload] Fix segfault when looking for host device name (#141632)
Summary:
This is done using the generic device into pointe, but no such thing
exists for the host device, leading to a segfault. This patch fixes that
for now, but in the future we should probably be more careful in general
handling the possibility that the handle is null everywhere.

Fixes: https://github.com/llvm/llvm-project/issues/141434
2025-05-27 13:43:29 -05:00
Joseph Huber
20f9f1fc02
[Offload][NFCI] Remove coupling to omp target for version scripting (#141637)
Summary:
This is a weird dependency on libomp just for testing if version scripts
work. We shouldn't need to do  this because LLVM already checks for
this. I believe this should be available as well in standalone when we
call `addLLVM` but I did not test that directly.
2025-05-27 13:43:07 -05:00
Ross Brunton
7e9d708be0
[Offload] Use llvm::Error throughout liboffload internals (#140879)
This removes the `ol_impl_result_t` helper class, replacing it with
`llvm::Error`. In addition, some internal functions that returned
`ol_errc_t` now return `llvm::Error` (with a fancy message).
2025-05-27 13:42:56 -05:00
Ross Brunton
050892d2f8
[Offload] Use new error code handling mechanism and lower-case messages (#139275)
[Offload] Use new error code handling mechanism

This removes the old ErrorCode-less error method and requires
every user to provide a concrete error code. All calls have been
updated.

In addition, for consistency with error messages elsewhere in LLVM, all
messages have been made to start lower case.
2025-05-20 08:50:20 -05:00
Ross Brunton
1532ee6916
[Offload] Add Error Codes to PluginInterface (#138258)
A new ErrorCode enumeration is present in PluginInterface which can
be used when returning an llvm::Error from offload and PluginInterface
functions.

This enum must be kept up to sync with liboffload's ol_errc_t enum, so
both are automatically generated from liboffload's enum definition.

Some error codes have also been shuffled around to allow for future
work. Note that this patch only adds the machinery; actual error codes
will be added in a future patch.

~~Depends on #137339 , please ignore first commit of this MR.~~ This has
been merged.
2025-05-19 09:38:34 -05:00