Skip to content

Conversation

@RossBrunton
Copy link
Contributor

@RossBrunton RossBrunton commented Jun 3, 2025

Added new entrypoints GetKernelInfo and GetKernelInfoSize for querying
info about a kernel. Currently, the only supported info is the program
handle which was used to create the program.

As part of implementing this, ol_kernel_handle_t has been "promoted"
to a real handle type rather than a bitcast.

This is a stacked MR based on #141982 , since I figure people don't want to
stare at generated code. Ignore the first commit.
Merged.

@llvmbot llvmbot added the offload label Jun 3, 2025
@llvmbot
Copy link
Member

llvmbot commented Jun 3, 2025

@llvm/pr-subscribers-offload

Author: Ross Brunton (RossBrunton)

Changes

Added new entrypoints GetKernelInfo and GetKernelInfoSize for querying
info about a kernel. Currently, the only supported info is the program
handle which was used to create the program.

As part of implementing this, ol_kernel_handle_t has been "promoted"
to a real handle type rather than a bitcast.

This is a stacked MR based on #141982 , since I figure people don't want to
stare at generated code. Ignore the first commit.


Patch is 115.66 KiB, truncated to 20.00 KiB below, full version: https://github.com/llvm/llvm-project/pull/142632.diff

17 Files Affected:

  • (modified) offload/CMakeLists.txt (+2-1)
  • (removed) offload/include/Shared/OffloadErrcodes.inc (-51)
  • (modified) offload/liboffload/API/CMakeLists.txt (+41-24)
  • (modified) offload/liboffload/API/Common.td (+1-2)
  • (modified) offload/liboffload/API/Kernel.td (+50)
  • (modified) offload/liboffload/CMakeLists.txt (+7-3)
  • (removed) offload/liboffload/include/generated/OffloadAPI.h (-1021)
  • (removed) offload/liboffload/include/generated/OffloadEntryPoints.inc (-903)
  • (removed) offload/liboffload/include/generated/OffloadFuncs.inc (-52)
  • (removed) offload/liboffload/include/generated/OffloadImplFuncDecls.inc (-60)
  • (removed) offload/liboffload/include/generated/OffloadPrint.hpp (-645)
  • (modified) offload/liboffload/src/OffloadImpl.cpp (+37-2)
  • (modified) offload/libomptarget/CMakeLists.txt (+7)
  • (modified) offload/plugins-nextgen/CMakeLists.txt (+2)
  • (modified) offload/unittests/OffloadAPI/CMakeLists.txt (+3-1)
  • (added) offload/unittests/OffloadAPI/kernel/olGetKernelInfo.cpp (+55)
  • (added) offload/unittests/OffloadAPI/kernel/olGetKernelInfoSize.cpp (+20)
diff --git a/offload/CMakeLists.txt b/offload/CMakeLists.txt
index c7cafd105f52a..09eae0f5d3aea 100644
--- a/offload/CMakeLists.txt
+++ b/offload/CMakeLists.txt
@@ -363,6 +363,8 @@ set(LIBOMPTARGET_LLVM_LIBRARY_DIR "${LLVM_LIBRARY_DIR}" CACHE STRING
 set(LIBOMPTARGET_LLVM_LIBRARY_INTDIR "${LIBOMPTARGET_INTDIR}" CACHE STRING
   "Path to folder where intermediate libraries will be output")
 
+add_subdirectory(tools/offload-tblgen)
+
 # Build offloading plugins and device RTLs if they are available.
 add_subdirectory(plugins-nextgen)
 add_subdirectory(DeviceRTL)
@@ -371,7 +373,6 @@ add_subdirectory(tools)
 # Build target agnostic offloading library.
 add_subdirectory(libomptarget)
 
-add_subdirectory(tools/offload-tblgen)
 add_subdirectory(liboffload)
 
 # Add tests.
diff --git a/offload/include/Shared/OffloadErrcodes.inc b/offload/include/Shared/OffloadErrcodes.inc
deleted file mode 100644
index 130e553aa70a4..0000000000000
--- a/offload/include/Shared/OffloadErrcodes.inc
+++ /dev/null
@@ -1,51 +0,0 @@
-//===- Auto-generated file, part of the LLVM/Offload project --------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-#ifndef OFFLOAD_ERRC
-#error Please define the macro OFFLOAD_ERRCODE(Name, Desc, Value)
-#endif
-
-// Error codes are shared between PluginInterface and liboffload.
-// To add new error codes, add them to offload/liboffload/API/Common.td and run
-// the GenerateOffload target.
-
-OFFLOAD_ERRC(SUCCESS, "success", 0)
-OFFLOAD_ERRC(UNKNOWN, "unknown or internal error", 1)
-OFFLOAD_ERRC(HOST_IO, "I/O error on host", 2)
-OFFLOAD_ERRC(INVALID_BINARY, "a provided binary image is malformed", 3)
-OFFLOAD_ERRC(INVALID_NULL_POINTER,
-             "a pointer argument is null when it should not be", 4)
-OFFLOAD_ERRC(INVALID_ARGUMENT, "an argument is invalid", 5)
-OFFLOAD_ERRC(NOT_FOUND, "requested object was not found in the binary image", 6)
-OFFLOAD_ERRC(OUT_OF_RESOURCES, "out of resources", 7)
-OFFLOAD_ERRC(
-    INVALID_SIZE,
-    "invalid size or dimensions (e.g., must not be zero, or is out of bounds)",
-    8)
-OFFLOAD_ERRC(INVALID_ENUMERATION, "enumerator argument is not valid", 9)
-OFFLOAD_ERRC(HOST_TOOL_NOT_FOUND,
-             "a required binary (linker, etc.) was not found on the host", 10)
-OFFLOAD_ERRC(INVALID_VALUE, "invalid value", 11)
-OFFLOAD_ERRC(UNIMPLEMENTED,
-             "generic error code for features currently unimplemented by the "
-             "device/backend",
-             12)
-OFFLOAD_ERRC(
-    UNSUPPORTED,
-    "generic error code for features unsupported by the device/backend", 13)
-OFFLOAD_ERRC(ASSEMBLE_FAILURE,
-             "assembler failure while processing binary image", 14)
-OFFLOAD_ERRC(LINK_FAILURE, "linker failure while processing binary image", 15)
-OFFLOAD_ERRC(BACKEND_FAILURE,
-             "the plugin backend is in an invalid or unsupported state", 16)
-OFFLOAD_ERRC(INVALID_NULL_HANDLE,
-             "a handle argument is null when it should not be", 17)
-OFFLOAD_ERRC(INVALID_PLATFORM, "invalid platform", 18)
-OFFLOAD_ERRC(INVALID_DEVICE, "invalid device", 19)
-OFFLOAD_ERRC(INVALID_QUEUE, "invalid queue", 20)
-OFFLOAD_ERRC(INVALID_EVENT, "invalid event", 21)
diff --git a/offload/liboffload/API/CMakeLists.txt b/offload/liboffload/API/CMakeLists.txt
index 5f8d1435d141f..cf6e132aa57a9 100644
--- a/offload/liboffload/API/CMakeLists.txt
+++ b/offload/liboffload/API/CMakeLists.txt
@@ -1,29 +1,46 @@
-# The OffloadGenerate target is used to regenerate the generated files in the
-# include directory. These files are checked in with the rest of the source,
-# therefore it is only needed when making changes to the API.
+# We want to clang-format the generated files if possible, since OffloadAPI.h is
+# the main public header for liboffload. Generate them in a temporary location,
+# then clang-format and copy them to the proper location. If clang-format is
+# missing just copy them.
+# Ideally we'd just clang-format them in place and avoid the copy but cmake
+# gets confused about the same path being a byproduct of two custom commands.
 
-find_program(CLANG_FORMAT clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
-if (CLANG_FORMAT)
-    set(LLVM_TARGET_DEFINITIONS ${CMAKE_CURRENT_SOURCE_DIR}/OffloadAPI.td)
+set(LLVM_TARGET_DEFINITIONS ${CMAKE_CURRENT_SOURCE_DIR}/OffloadAPI.td)
+set(files_to_copy "")
 
-    tablegen(OFFLOAD OffloadAPI.h -gen-api)
-    tablegen(OFFLOAD OffloadEntryPoints.inc -gen-entry-points)
-    tablegen(OFFLOAD OffloadFuncs.inc -gen-func-names)
-    tablegen(OFFLOAD OffloadImplFuncDecls.inc -gen-impl-func-decls)
-    tablegen(OFFLOAD OffloadPrint.hpp -gen-print-header)
-    tablegen(OFFLOAD OffloadErrcodes.inc -gen-errcodes)
+macro(offload_tablegen file)
+    tablegen(OFFLOAD generated/${file}.gen ${ARGN})
+    list(APPEND files_to_copy ${file})
+endmacro()
 
-    set(FILES_TO_COPY "OffloadAPI.h;OffloadEntryPoints.inc;OffloadFuncs.inc;OffloadImplFuncDecls.inc;OffloadPrint.hpp")
-    set(GEN_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../include/generated)
-    add_public_tablegen_target(OffloadGenerate)
-    add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CLANG_FORMAT}
-        -i ${TABLEGEN_OUTPUT})
-    add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-        -E copy_if_different ${FILES_TO_COPY} ${GEN_DIR})
-    add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-        -E copy_if_different OffloadErrcodes.inc "${LIBOMPTARGET_INCLUDE_DIR}/Shared/OffloadErrcodes.inc")
+offload_tablegen(OffloadAPI.h -gen-api)
+offload_tablegen(OffloadEntryPoints.inc -gen-entry-points)
+offload_tablegen(OffloadFuncs.inc -gen-func-names)
+offload_tablegen(OffloadImplFuncDecls.inc -gen-impl-func-decls)
+offload_tablegen(OffloadPrint.hpp -gen-print-header)
+
+add_public_tablegen_target(OffloadGenerate)
+
+add_custom_target(OffloadAPI DEPENDS OffloadGenerate)
+find_program(clang_format clang-format PATHS ${LLVM_TOOLS_BINARY_DIR} NO_DEFAULT_PATH)
+if (clang_format)
+    foreach(file IN LISTS files_to_copy)
+      add_custom_command(
+          OUTPUT ${file}
+          COMMAND ${clang_format} -i generated/${file}.gen
+          COMMAND ${CMAKE_COMMAND} -E copy_if_different generated/${file}.gen ${CMAKE_CURRENT_BINARY_DIR}/${file}
+          DEPENDS generated/${file}.gen
+      )
+      add_custom_target(OffloadAPI.${file} DEPENDS ${file})
+      add_dependencies(OffloadAPI OffloadAPI.${file})
+    endforeach()
 else()
-    message(WARNING "clang-format was not found, so the OffloadGenerate target\
-        will not be available. Offload will still build, but you will not be\
-        able to make changes to the API.")
+    message(WARNING "clang-format not found, the generated Offload API headers will not be formatted")
+    foreach(file IN LISTS files_to_copy)
+        add_custom_command(
+            OUTPUT ${file}
+            COMMAND ${CMAKE_COMMAND} -E copy_if_different generated/${file}.gen ${CMAKE_CURRENT_BINARY_DIR}/${file}
+            DEPENDS generated/${file}.gen
+        )
+    endforeach()
 endif()
diff --git a/offload/liboffload/API/Common.td b/offload/liboffload/API/Common.td
index 7674da0438c29..680396e379f16 100644
--- a/offload/liboffload/API/Common.td
+++ b/offload/liboffload/API/Common.td
@@ -77,10 +77,9 @@ def : Handle {
   let desc = "Handle of program object";
 }
 
-def : Typedef {
+def : Handle {
   let name = "ol_kernel_handle_t";
   let desc = "Handle of kernel object";
-  let value = "void *";
 }
 
 def ErrorCode : Enum {
diff --git a/offload/liboffload/API/Kernel.td b/offload/liboffload/API/Kernel.td
index 247f9c1bf5b6a..e61916d91f2dd 100644
--- a/offload/liboffload/API/Kernel.td
+++ b/offload/liboffload/API/Kernel.td
@@ -10,6 +10,15 @@
 //
 //===----------------------------------------------------------------------===//
 
+def : Enum {
+  let name = "ol_kernel_info_t";
+  let desc = "Supported kernel info.";
+  let is_typed = 1;
+  let etors =[
+    TaggedEtor<"PROGRAM", "ol_program_handle_t", "the program associated with this kernel">,
+  ];
+}
+
 def : Function {
     let name = "olGetKernel";
     let desc = "Get a kernel from the function identified by `KernelName` in the given program.";
@@ -59,3 +68,44 @@ def : Function {
         Return<"OL_ERRC_INVALID_DEVICE", ["If Queue is non-null but does not belong to Device"]>,
     ];
 }
+
+def : Function {
+  let name = "olGetKernelInfo";
+  let desc = "Queries the given property of the device.";
+  let details = [];
+  let params = [
+    Param<"ol_kernel_handle_t", "Kernel", "handle of the kernel instance", PARAM_IN>,
+    Param<"ol_kernel_info_t", "PropName", "type of the info to retrieve", PARAM_IN>,
+    Param<"size_t", "PropSize", "the number of bytes pointed to by PropValue.", PARAM_IN>,
+    TypeTaggedParam<"void*", "PropValue", "array of bytes holding the info. If PropSize is not equal to or greater than the real "
+                    "number of bytes needed to return the info then the OL_ERRC_INVALID_SIZE error is returned and "
+                    "PropValue is not used.", PARAM_OUT, TypeInfo<"PropName" , "PropSize">>
+  ];
+  let returns = [
+    Return<"OL_ERRC_UNSUPPORTED_ENUMERATION", [
+      "If `PropName` is not supported by the kernel."
+    ]>,
+    Return<"OL_ERRC_INVALID_SIZE", [
+      "`PropSize == 0`",
+      "If `PropSize` is less than the real number of bytes needed to return the info."
+    ]>,
+    Return<"OL_ERRC_INVALID_DEVICE">
+  ];
+}
+
+def : Function {
+  let name = "olGetKernelInfoSize";
+  let desc = "Returns the storage size of the given device query.";
+  let details = [];
+  let params = [
+    Param<"ol_kernel_handle_t", "Kernel", "handle of the kernel instance", PARAM_IN>,
+    Param<"ol_kernel_info_t", "PropName", "type of the info to retrieve", PARAM_IN>,
+    Param<"size_t*", "PropSizeRet", "pointer to the number of bytes required to store the query", PARAM_OUT>
+  ];
+  let returns = [
+    Return<"OL_ERRC_UNSUPPORTED_ENUMERATION", [
+      "If `PropName` is not supported by the kernel."
+    ]>,
+    Return<"OL_ERRC_INVALID_DEVICE">
+  ];
+}
diff --git a/offload/liboffload/CMakeLists.txt b/offload/liboffload/CMakeLists.txt
index 1b098bc01e218..815785ab23718 100644
--- a/offload/liboffload/CMakeLists.txt
+++ b/offload/liboffload/CMakeLists.txt
@@ -8,6 +8,10 @@ add_llvm_library(
   LINK_COMPONENTS
   FrontendOpenMP
   Support
+
+  DEPENDS
+  OffloadAPI
+  LibomptargetOffloadErrcodes
   )
 
 foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)
@@ -19,9 +23,9 @@ if(LLVM_HAVE_LINK_VERSION_SCRIPT)
 endif()
 
 target_include_directories(LLVMOffload PUBLIC
+                            ${CMAKE_CURRENT_BINARY_DIR}/API
                             ${CMAKE_CURRENT_BINARY_DIR}/../include
                             ${CMAKE_CURRENT_SOURCE_DIR}/include
-                            ${CMAKE_CURRENT_SOURCE_DIR}/include/generated
                             ${CMAKE_CURRENT_SOURCE_DIR}/../include
                             ${CMAKE_CURRENT_SOURCE_DIR}/../plugins-nextgen/common/include)
 
@@ -39,5 +43,5 @@ set_target_properties(LLVMOffload PROPERTIES
                       BUILD_RPATH "$ORIGIN:${CMAKE_CURRENT_BINARY_DIR}/..")
 install(TARGETS LLVMOffload LIBRARY COMPONENT LLVMOffload DESTINATION "${OFFLOAD_INSTALL_LIBDIR}")
 
-install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/generated/OffloadAPI.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/offload)
-install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/include/generated/OffloadPrint.hpp DESTINATION ${CMAKE_INSTALL_PREFIX}/include/offload)
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/API/OffloadAPI.h DESTINATION ${CMAKE_INSTALL_PREFIX}/include/offload)
+install(FILES ${CMAKE_CURRENT_BINARY_DIR}/API/OffloadPrint.hpp DESTINATION ${CMAKE_INSTALL_PREFIX}/include/offload)
diff --git a/offload/liboffload/include/generated/OffloadAPI.h b/offload/liboffload/include/generated/OffloadAPI.h
deleted file mode 100644
index a1d7540519e32..0000000000000
--- a/offload/liboffload/include/generated/OffloadAPI.h
+++ /dev/null
@@ -1,1021 +0,0 @@
-//===- Auto-generated file, part of the LLVM/Offload project --------------===//
-//
-// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
-// See https://llvm.org/LICENSE.txt for license information.
-// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
-//
-//===----------------------------------------------------------------------===//
-
-// Auto-generated file, do not manually edit.
-
-#pragma once
-
-#include <stddef.h>
-#include <stdint.h>
-
-#if defined(__cplusplus)
-extern "C" {
-#endif
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Defines Return/Error codes
-typedef enum ol_errc_t {
-  /// success
-  OL_ERRC_SUCCESS = 0,
-  /// unknown or internal error
-  OL_ERRC_UNKNOWN = 1,
-  /// I/O error on host
-  OL_ERRC_HOST_IO = 2,
-  /// a provided binary image is malformed
-  OL_ERRC_INVALID_BINARY = 3,
-  /// a pointer argument is null when it should not be
-  OL_ERRC_INVALID_NULL_POINTER = 4,
-  /// an argument is invalid
-  OL_ERRC_INVALID_ARGUMENT = 5,
-  /// requested object was not found in the binary image
-  OL_ERRC_NOT_FOUND = 6,
-  /// out of resources
-  OL_ERRC_OUT_OF_RESOURCES = 7,
-  /// invalid size or dimensions (e.g., must not be zero, or is out of bounds)
-  OL_ERRC_INVALID_SIZE = 8,
-  /// enumerator argument is not valid
-  OL_ERRC_INVALID_ENUMERATION = 9,
-  /// a required binary (linker, etc.) was not found on the host
-  OL_ERRC_HOST_TOOL_NOT_FOUND = 10,
-  /// invalid value
-  OL_ERRC_INVALID_VALUE = 11,
-  /// generic error code for features currently unimplemented by the
-  /// device/backend
-  OL_ERRC_UNIMPLEMENTED = 12,
-  /// generic error code for features unsupported by the device/backend
-  OL_ERRC_UNSUPPORTED = 13,
-  /// assembler failure while processing binary image
-  OL_ERRC_ASSEMBLE_FAILURE = 14,
-  /// linker failure while processing binary image
-  OL_ERRC_LINK_FAILURE = 15,
-  /// the plugin backend is in an invalid or unsupported state
-  OL_ERRC_BACKEND_FAILURE = 16,
-  /// a handle argument is null when it should not be
-  OL_ERRC_INVALID_NULL_HANDLE = 17,
-  /// invalid platform
-  OL_ERRC_INVALID_PLATFORM = 18,
-  /// invalid device
-  OL_ERRC_INVALID_DEVICE = 19,
-  /// invalid queue
-  OL_ERRC_INVALID_QUEUE = 20,
-  /// invalid event
-  OL_ERRC_INVALID_EVENT = 21,
-  /// @cond
-  OL_ERRC_FORCE_UINT32 = 0x7fffffff
-  /// @endcond
-
-} ol_errc_t;
-
-///////////////////////////////////////////////////////////////////////////////
-#ifndef OL_VERSION_MAJOR
-/// @brief Major version of the Offload API
-#define OL_VERSION_MAJOR 0
-#endif // OL_VERSION_MAJOR
-
-///////////////////////////////////////////////////////////////////////////////
-#ifndef OL_VERSION_MINOR
-/// @brief Minor version of the Offload API
-#define OL_VERSION_MINOR 0
-#endif // OL_VERSION_MINOR
-
-///////////////////////////////////////////////////////////////////////////////
-#ifndef OL_VERSION_PATCH
-/// @brief Patch version of the Offload API
-#define OL_VERSION_PATCH 1
-#endif // OL_VERSION_PATCH
-
-///////////////////////////////////////////////////////////////////////////////
-#ifndef OL_APICALL
-#if defined(_WIN32)
-/// @brief Calling convention for all API functions
-#define OL_APICALL __cdecl
-#else
-#define OL_APICALL
-#endif // defined(_WIN32)
-#endif // OL_APICALL
-
-///////////////////////////////////////////////////////////////////////////////
-#ifndef OL_APIEXPORT
-#if defined(_WIN32)
-/// @brief Microsoft-specific dllexport storage-class attribute
-#define OL_APIEXPORT __declspec(dllexport)
-#else
-#define OL_APIEXPORT
-#endif // defined(_WIN32)
-#endif // OL_APIEXPORT
-
-///////////////////////////////////////////////////////////////////////////////
-#ifndef OL_DLLEXPORT
-#if defined(_WIN32)
-/// @brief Microsoft-specific dllexport storage-class attribute
-#define OL_DLLEXPORT __declspec(dllexport)
-#endif // defined(_WIN32)
-#endif // OL_DLLEXPORT
-
-///////////////////////////////////////////////////////////////////////////////
-#ifndef OL_DLLEXPORT
-#if __GNUC__ >= 4
-/// @brief GCC-specific dllexport storage-class attribute
-#define OL_DLLEXPORT __attribute__((visibility("default")))
-#else
-#define OL_DLLEXPORT
-#endif // __GNUC__ >= 4
-#endif // OL_DLLEXPORT
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Handle of a platform instance
-typedef struct ol_platform_impl_t *ol_platform_handle_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Handle of platform's device object
-typedef struct ol_device_impl_t *ol_device_handle_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Handle of context object
-typedef struct ol_context_impl_t *ol_context_handle_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Handle of queue object
-typedef struct ol_queue_impl_t *ol_queue_handle_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Handle of event object
-typedef struct ol_event_impl_t *ol_event_handle_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Handle of program object
-typedef struct ol_program_impl_t *ol_program_handle_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Handle of kernel object
-typedef void *ol_kernel_handle_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Details of the error condition returned by an API call
-typedef struct ol_error_struct_t {
-  ol_errc_t Code;      /// The error code
-  const char *Details; /// String containing error details
-} ol_error_struct_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Result type returned by all entry points.
-typedef const ol_error_struct_t *ol_result_t;
-
-///////////////////////////////////////////////////////////////////////////////
-#ifndef OL_SUCCESS
-/// @brief Success condition
-#define OL_SUCCESS NULL
-#endif // OL_SUCCESS
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Code location information that can optionally be associated with an
-/// API call
-typedef struct ol_code_location_t {
-  const char *FunctionName; /// Function name
-  const char *SourceFile;   /// Source code file
-  uint32_t LineNumber;      /// Source code line number
-  uint32_t ColumnNumber;    /// Source code column number
-} ol_code_location_t;
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Perform initialization of the Offload library and plugins
-///
-/// @details
-///    - This must be the first API call made by a user of the Offload library
-///    - Each call will increment an internal reference count that is
-///    decremented by `olShutDown`
-///
-/// @returns
-///     - ::OL_RESULT_SUCCESS
-///     - ::OL_ERRC_UNINITIALIZED
-///     - ::OL_ERRC_DEVICE_LOST
-///     - ::OL_ERRC_INVALID_NULL_HANDLE
-///     - ::OL_ERRC_INVALID_NULL_POINTER
-OL_APIEXPORT ol_result_t OL_APICALL olInit();
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Release the resources in use by Offload
-///
-/// @details
-///    - This decrements an internal reference count. When this reaches 0, all
-///    resources will be released
-///    - Subsequent API calls made after this are not valid
-///
-/// @returns
-///     - ::OL_RESULT_SUCCESS
-///     - ::OL_ERRC_UNINITIALIZED
-///     - ::OL_ERRC_DEVICE_LOST
-///     - ::OL_ERRC_INVALID_NULL_HANDLE
-///     - ::OL_ERRC_INVALID_NULL_POINTER
-OL_APIEXPORT ol_result_t OL_APICALL olShutDown();
-
-///////////////////////////////////////////////////////////////////////////////
-/// @brief Supported platform info.
-typedef enum ol_platform_info_t {
-  /// [char[]] The string denoting name of the platform. The size of the info
-  /// needs to be dynamically queried.
-  OL_PLATFORM_INFO_NAME = 0,
-  /// [char[]] The string denoting name of the vendor of the platform. The size
-  /// of the info needs to be dynamically...
[truncated]

Added new entrypoints GetKernelInfo and GetKernelInfoSize for querying
info about a kernel. Currently, the only supported info is the program
handle which was used to create the program.

As part of implementing this, `ol_kernel_handle_t` has been "promoted"
to a real handle type rather than a bitcast.
Copy link
Contributor

@callumfare callumfare left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The change looks good, but I recall when originally adding the kernel handle the feedback was to keep it as a plain void * rather than a proper handle. I think the justification was that we didn't want to abstract over kernels just being symbols fetched from programs.

I'm happy if that changes but just FYI.

@jhuber6
Copy link
Contributor

jhuber6 commented Jun 3, 2025

Is there a specific reason we need to query the kernel? Usually that's up to the higher level runtime to track which kernels it mapped to some other stub. I'd need to double check how this is actually used, but a kernel is just a pointer to the loaded executable. Depends if we want to totally change those semantics and make it opaque.

@RossBrunton
Copy link
Contributor Author

RossBrunton commented Jun 4, 2025

@jhuber6 @callumfare I'm looking at a kernel information query for supporting cu/hipOccupancyMaxPotentialBlockSize (or equivalents) which is a property of the kernel itself rather than the program. Hence the need for getKernelInfo. Getting the program handle seemed to be a simple enough thing to implement for the purpose of testing. I could use OL_KERNEL_INFO_NAME instead though, which would sidestep the handle changes. Since adding ol_kernel_handle_t was more controversial than I thought, I'll make a new MR with NAME instead of PROGRAM.

FUTURE EDIT: I'm wrong here; we can't implement blockSize as a normal query since it requires the dynamic memory size.

@RossBrunton RossBrunton marked this pull request as draft June 4, 2025 10:43
@RossBrunton RossBrunton closed this Jul 9, 2025
@RossBrunton RossBrunton reopened this Jul 9, 2025
@RossBrunton
Copy link
Contributor Author

@jhuber6 Looking at it again and discussing internally, is this change what you're looking for? Specifically, with extending ol_kernel_t to be a handle type like program, event, etc.

If it's not, and the kernel handle should just stay as a void *, I'm not sure what kernel queries I could actually implement. In UR at least ( https://oneapi-src.github.io/unified-runtime/core/api.html#ur-kernel-info-t ), all the properties are either not implementable on Cuda/Hip (arg count) or could be tracked by the offload user (name, context, program).

@jhuber6
Copy link
Contributor

jhuber6 commented Jul 9, 2025

At a minimum we'd want symbol address and size, then you can add stuff to that like whether or not it's a kernel or a global, you can probably refer to what CUDA / HSA provides as well.

@RossBrunton
Copy link
Contributor Author

@jhuber6 I don't think it's practical to have a single type for both kernels and global variables; the set of legal operations for them is just very different. Even in HSA, which has a shared "symbol" type, the only operation useful to us is querying the symbol's name. I do like the idea of ol_symbol_t, but I don't think it maps to GPUs where kernels are not simply function pointers.

@jhuber6
Copy link
Contributor

jhuber6 commented Jul 10, 2025

@jhuber6 I don't think it's practical to have a single type for both kernels and global variables; the set of legal operations for them is just very different. Even in HSA, which has a shared "symbol" type, the only operation useful to us is querying the symbol's name. I do like the idea of ol_symbol_t, but I don't think it maps to GPUs where kernels are not simply function pointers.

I figured you'd just have an info you can look up that tells the user if it's a kernel or global and only have certain infos supported depending on that.

@RossBrunton
Copy link
Contributor Author

@jhuber6 I think I'm warming up to that idea. Let me go and implement something and see if it works.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants