[llvm] [Offload] Generate `OffloadTypedGetInfo.inc` (PR #168615)

Andrei Elovikov via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 19 09:04:55 PST 2025


aelovikov-intel wrote:

I've been thinking if there is a smaller/cheaper alternative.

* This approach, fullest, provides the following usage
```c++
// get_info is provided/tblgen'ed by liboffload:
auto result = get_info<OL_DEVICE_INFO_SMTH>(device_handle);
```
but it mandates the mapping of how C types get translated into C++ type/error handling (e.g., choosing `std::varaint`/`std::expected` vs using C++ exceptions) and generates lots of similar code.
* Smallest approach
```c++
// Technically, we can outline that helper in the "full" approach inside generated code as well.
// On the consumer side, pseudocode:
namespace detail {
template <auto InfoKind>
auto get_info(auto &InfoGetter, auto &InfoSizeGetter, auto Handle) {
  // ol_ret_type is provided/tblgen'ed by liboffload
  using ret_ty = typename ol_ret_type<decltype(InfoKind), InfoKind>::type;
  // generic implementation using InfoGetter, InfoSizeGetter, ret_ty
}
} // namespace detail
...
auto result = detail::get_info<OL_DEVICE_INFO_SMTH>(olGetDeviceInfo, olGetDeviceInfoSize, device_handle);
```
consumer can choose what types to use and how to handler errors. The actual logic is implemented in a single template instead of being duplicated in multiple template specializations. Minimal amount of tblgen'ed code inside liboffload. Maybe we can even put that into `OffloadAPI.h` under `#if defined(__cplusplus)`, like this:
```c++
///////////////////////////////////////////////////////////////////////////////
/// @brief Variant of olWaitEvents that also sets source code location
/// information
/// @details See also ::olWaitEvents
OL_APIEXPORT ol_result_t OL_APICALL
olWaitEventsWithCodeLoc(ol_queue_handle_t Queue, ol_event_handle_t *Events,
                        size_t NumEvents, ol_code_location_t *CodeLocation);

#if defined(__cplusplus)
} // extern "C"
#endif

#if defined(__cplusplus)
template <typename, auto> struct ol_info_ret_type;
template <> struct ol_info_ret_type<ol_device_info_t, OL_DEVICE_INFO_TYPE> {
  using type = ol_device_type_t;
};
template <> struct ol_info_ret_type<ol_device_info_t, OL_DEVICE_INFO_PLATFORM> {
  using type = ol_platform_handle_t;
};
...
#endif
```

* Middle ground, provide something like
```c++
template <typename, auto> struct info_traits;
template <> struct info_traits<ol_device_info_t, OL_DEVICE_INFO_SMTH> {
  using return_type = <smth>;
  // Maybe split these into a separate trait with just typename template param:
  static constexpr auto InfoGetter = &olGetDeviceInfo;
  static constexpr auto InfoSizeGetter = &olGetDeviceInfoSize;
};
```

@jhuber6 any preference between those three?

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


More information about the llvm-commits mailing list