Reland #118503: [Offload] Introduce offload-tblgen and initial new API implementation (#118614)

Reland #118503. Added a fix for builds with `-DBUILD_SHARED_LIBS=ON`
(see last commit). Otherwise the changes are identical.

---


### New API

Previous discussions at the LLVM/Offload meeting have brought up the
need for a new API for exposing the functionality of the plugins. This
change introduces a very small subset of a new API, which is primarily
for testing the offload tooling and demonstrating how a new API can fit
into the existing code base without being too disruptive. Exact designs
for these entry points and future additions can be worked out over time.

The new API does however introduce the bare minimum functionality to
implement device discovery for Unified Runtime and SYCL. This means that
the `urinfo` and `sycl-ls` tools can be used on top of Offload. A
(rough) implementation of a Unified Runtime adapter (aka plugin) for
Offload is available
[here](https://github.com/callumfare/unified-runtime/tree/offload_adapter).
Our intention is to maintain this and use it to implement and test
Offload API changes with SYCL.

### Demoing the new API

```sh
# From the runtime build directory
$ ninja LibomptUnitTests
$ OFFLOAD_TRACE=1 ./offload/unittests/OffloadAPI/offload.unittests 
```


### Open questions and future work
* Only some of the available device info is exposed, and not all the
possible device queries needed for SYCL are implemented by the plugins.
A sensible next step would be to refactor and extend the existing device
info queries in the plugins. The existing info queries are all strings,
but the new API introduces the ability to return any arbitrary type.
* It may be sensible at some point for the plugins to implement the new
API directly, and the higher level code on top of it could be made
generic, but this is more of a long-term possibility.
This commit is contained in:
Callum Fare
2024-12-05 08:34:04 +00:00
committed by GitHub
parent 636beb6a28
commit fd3907ccb5
56 changed files with 4928 additions and 2 deletions

View File

@@ -377,6 +377,9 @@ add_subdirectory(tools)
# Build target agnostic offloading library.
add_subdirectory(src)
add_subdirectory(tools/offload-tblgen)
add_subdirectory(liboffload)
# Add tests.
add_subdirectory(test)

View File

@@ -37,6 +37,17 @@ function(find_standalone_test_dependencies)
return()
endif()
find_program(OFFLOAD_TBLGEN_EXECUTABLE
NAMES offload-tblgen
PATHS ${OPENMP_LLVM_TOOLS_DIR})
if (NOT OFFLOAD_TBLGEN_EXECUTABLE)
message(STATUS "Cannot find 'offload-tblgen'.")
message(STATUS "Please put 'not' in your PATH, set OFFLOAD_TBLGEN_EXECUTABLE to its full path, or point OPENMP_LLVM_TOOLS_DIR to its directory.")
message(WARNING "The check targets will not be available!")
set(ENABLE_CHECK_TARGETS FALSE PARENT_SCOPE)
return()
endif()
find_program(OPENMP_NOT_EXECUTABLE
NAMES not
PATHS ${OPENMP_LLVM_TOOLS_DIR})
@@ -73,6 +84,7 @@ else()
set(OPENMP_NOT_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/not)
endif()
set(OFFLOAD_DEVICE_INFO_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/llvm-offload-device-info)
set(OFFLOAD_TBLGEN_EXECUTABLE ${LLVM_RUNTIME_OUTPUT_INTDIR}/offload-tblgen)
# Macro to extract information about compiler from file. (no own scope)
macro(extract_test_compiler_information lang file)

View File

@@ -0,0 +1,212 @@
//===-- APIDefs.td - Base definitions for Offload tablegen -*- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file contains the class definitions used to implement the Offload API,
// as well as helper functions used to help populate relevant records.
// See offload/API/README.md for more detailed documentation.
//
//===----------------------------------------------------------------------===//
// Prefix for API naming. This could be hard-coded in the future when a value
// is agreed upon.
defvar PREFIX = "OL";
defvar prefix = !tolower(PREFIX);
// Parameter flags
defvar PARAM_IN = 0x1;
defvar PARAM_OUT = 0x2;
defvar PARAM_OPTIONAL = 0x4;
defvar PARAM_IN_OPTIONAL = !or(PARAM_IN, PARAM_OPTIONAL);
defvar PARAM_OUT_OPTIONAL = !or(PARAM_OUT, PARAM_OPTIONAL);
// Does the type end with '_handle_t'?
class IsHandleType<string Type> {
// size("_handle_t") == 9
bit ret = !if(!lt(!size(Type), 9), 0,
!ne(!find(Type, "_handle_t", !sub(!size(Type), 9)), -1));
}
// Does the type end with '*'?
class IsPointerType<string Type> {
bit ret = !ne(!find(Type, "*", !sub(!size(Type), 1)), -1);
}
// Describes the valid range of a pointer parameter that reperesents an array
class Range<string Begin, string End> {
string begin = Begin;
string end = End;
}
// Names the parameters that indicate the type and size of the data pointed to
// by an opaque pointer parameter
class TypeInfo<string TypeEnum, string TypeSize> {
string enum = TypeEnum;
string size = TypeSize;
}
class Param<string Type, string Name, string Desc, bits<3> Flags = 0> {
string type = Type;
string name = Name;
string desc = Desc;
bits<3> flags = Flags;
Range range = Range<"", "">;
TypeInfo type_info = TypeInfo<"", "">;
bit IsHandle = IsHandleType<type>.ret;
bit IsPointer = IsPointerType<type>.ret;
}
// A parameter whose range is described by other parameters in the function.
class RangedParam<string Type, string Name, string Desc, bits<3> Flags, Range ParamRange> : Param<Type, Name, Desc, Flags> {
let range = ParamRange;
}
// A parameter (normally of type void*) which has its pointee type and size
// described by other parameters in the function.
class TypeTaggedParam<string Type, string Name, string Desc, bits<3> Flags, TypeInfo ParamTypeInfo> : Param<Type, Name, Desc, Flags> {
let type_info = ParamTypeInfo;
}
class Return<string Value, list<string> Conditions = []> {
string value = Value;
list<string> conditions = Conditions;
}
class ShouldCheckHandle<Param P> {
bit ret = !and(P.IsHandle, !eq(!and(PARAM_OPTIONAL, P.flags), 0));
}
class ShouldCheckPointer<Param P> {
bit ret = !and(P.IsPointer, !eq(!and(PARAM_OPTIONAL, P.flags), 0));
}
// For a list of returns that contains a specific return code, find and append
// new conditions to that return
class AppendConditionsToReturn<list<Return> Returns, string ReturnValue,
list<string> Conditions> {
list<Return> ret =
!foreach(Ret, Returns,
!if(!eq(Ret.value, ReturnValue),
Return<Ret.value, Ret.conditions#Conditions>, Ret));
}
// Add null handle checks to a function's return value descriptions
class AddHandleChecksToReturns<list<Param> Params, list<Return> Returns> {
list<string> handle_params =
!foreach(P, Params, !if(ShouldCheckHandle<P>.ret, P.name, ""));
list<string> handle_params_filt =
!filter(param, handle_params, !ne(param, ""));
list<string> handle_param_conds =
!foreach(handle, handle_params_filt, "`NULL == "#handle#"`");
// Does the list of returns already contain ERROR_INVALID_NULL_HANDLE?
bit returns_has_inv_handle = !foldl(
0, Returns, HasErr, Ret,
!or(HasErr, !eq(Ret.value, PREFIX#"_ERRC_INVALID_NULL_HANDLE")));
list<Return> returns_out = !if(returns_has_inv_handle,
AppendConditionsToReturn<Returns, PREFIX # "_ERRC_INVALID_NULL_HANDLE", handle_param_conds>.ret,
!listconcat(Returns, [Return<PREFIX # "_ERRC_INVALID_NULL_HANDLE", handle_param_conds>])
);
}
// Add null pointer checks to a function's return value descriptions
class AddPointerChecksToReturns<list<Param> Params, list<Return> Returns> {
list<string> ptr_params =
!foreach(P, Params, !if(ShouldCheckPointer<P>.ret, P.name, ""));
list<string> ptr_params_filt = !filter(param, ptr_params, !ne(param, ""));
list<string> ptr_param_conds =
!foreach(ptr, ptr_params_filt, "`NULL == "#ptr#"`");
// Does the list of returns already contain ERROR_INVALID_NULL_POINTER?
bit returns_has_inv_ptr = !foldl(
0, Returns, HasErr, Ret,
!or(HasErr, !eq(Ret.value, PREFIX#"_ERRC_INVALID_NULL_POINTER")));
list<Return> returns_out = !if(returns_has_inv_ptr,
AppendConditionsToReturn<Returns, PREFIX # "_ERRC_INVALID_NULL_POINTER", ptr_param_conds>.ret,
!listconcat(Returns, [Return<PREFIX # "_ERRC_INVALID_NULL_POINTER", ptr_param_conds>])
);
}
defvar DefaultReturns = [Return<PREFIX#"_RESULT_SUCCESS">,
Return<PREFIX#"_ERRC_UNINITIALIZED">,
Return<PREFIX#"_ERRC_DEVICE_LOST">];
class APIObject {
string name;
string desc;
}
class Function : APIObject {
list<Param> params;
list<Return> returns;
list<string> details = [];
list<string> analogues = [];
list<Return> returns_with_def = !listconcat(DefaultReturns, returns);
list<Return> all_returns = AddPointerChecksToReturns<params,
AddHandleChecksToReturns<params, returns_with_def>.returns_out>.returns_out;
}
class Etor<string Name, string Desc> {
string name = Name;
string desc = Desc;
string tagged_type;
}
class TaggedEtor<string Name, string Type, string Desc> : Etor<Name, Desc> {
let tagged_type = Type;
}
class Enum : APIObject {
// This refers to whether the enumerator descriptions specify a return
// type for functions where this enum may be used as an output type. If set,
// all Etor values must be TaggedEtor records
bit is_typed = 0;
list<Etor> etors = [];
}
class StructMember<string Type, string Name, string Desc> {
string type = Type;
string name = Name;
string desc = Desc;
}
defvar DefaultPropStructMembers =
[StructMember<prefix#"_structure_type_t", "stype",
"type of this structure">,
StructMember<"void*", "pNext", "pointer to extension-specific structure">];
class StructHasInheritedMembers<string BaseClass> {
bit ret = !or(!eq(BaseClass, prefix#"_base_properties_t"),
!eq(BaseClass, prefix#"_base_desc_t"));
}
class Struct : APIObject {
string base_class = "";
list<StructMember> members;
list<StructMember> all_members =
!if(StructHasInheritedMembers<base_class>.ret,
DefaultPropStructMembers, [])#members;
}
class Typedef : APIObject { string value; }
class FptrTypedef : APIObject {
list<Param> params;
list<Return> returns;
}
class Macro : APIObject {
string value;
string condition;
string alt_value;
}
class Handle : APIObject;

View File

@@ -0,0 +1,25 @@
# 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.
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)
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)
set(OFFLOAD_GENERATED_FILES ${TABLEGEN_OUTPUT})
add_public_tablegen_target(OffloadGenerate)
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CLANG_FORMAT}
-i ${OFFLOAD_GENERATED_FILES})
add_custom_command(TARGET OffloadGenerate POST_BUILD COMMAND ${CMAKE_COMMAND}
-E copy_if_different ${OFFLOAD_GENERATED_FILES} "${CMAKE_CURRENT_SOURCE_DIR}/../include/generated")
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.")
endif()

View File

@@ -0,0 +1,141 @@
def : Macro {
let name = "OL_VERSION_MAJOR";
let desc = "Major version of the Offload API";
let value = "0";
}
def : Macro {
let name = "OL_VERSION_MINOR";
let desc = "Minor version of the Offload API";
let value = "0";
}
def : Macro {
let name = "OL_VERSION_PATCH";
let desc = "Patch version of the Offload API";
let value = "1";
}
def : Macro {
let name = "OL_APICALL";
let desc = "Calling convention for all API functions";
let condition = "defined(_WIN32)";
let value = "__cdecl";
let alt_value = "";
}
def : Macro {
let name = "OL_APIEXPORT";
let desc = "Microsoft-specific dllexport storage-class attribute";
let condition = "defined(_WIN32)";
let value = "__declspec(dllexport)";
let alt_value = "";
}
def : Macro {
let name = "OL_DLLEXPORT";
let desc = "Microsoft-specific dllexport storage-class attribute";
let condition = "defined(_WIN32)";
let value = "__declspec(dllexport)";
}
def : Macro {
let name = "OL_DLLEXPORT";
let desc = "GCC-specific dllexport storage-class attribute";
let condition = "__GNUC__ >= 4";
let value = "__attribute__ ((visibility (\"default\")))";
let alt_value = "";
}
def : Handle {
let name = "ol_platform_handle_t";
let desc = "Handle of a platform instance";
}
def : Handle {
let name = "ol_device_handle_t";
let desc = "Handle of platform's device object";
}
def : Handle {
let name = "ol_context_handle_t";
let desc = "Handle of context object";
}
def : Enum {
let name = "ol_errc_t";
let desc = "Defines Return/Error codes";
let etors =[
Etor<"SUCCESS", "Success">,
Etor<"INVALID_VALUE", "Invalid Value">,
Etor<"INVALID_PLATFORM", "Invalid platform">,
Etor<"DEVICE_NOT_FOUND", "Device not found">,
Etor<"INVALID_DEVICE", "Invalid device">,
Etor<"DEVICE_LOST", "Device hung, reset, was removed, or driver update occurred">,
Etor<"UNINITIALIZED", "plugin is not initialized or specific entry-point is not implemented">,
Etor<"OUT_OF_RESOURCES", "Out of resources">,
Etor<"UNSUPPORTED_VERSION", "generic error code for unsupported versions">,
Etor<"UNSUPPORTED_FEATURE", "generic error code for unsupported features">,
Etor<"INVALID_ARGUMENT", "generic error code for invalid arguments">,
Etor<"INVALID_NULL_HANDLE", "handle argument is not valid">,
Etor<"INVALID_NULL_POINTER", "pointer argument may not be nullptr">,
Etor<"INVALID_SIZE", "invalid size or dimensions (e.g., must not be zero, or is out of bounds)">,
Etor<"INVALID_ENUMERATION", "enumerator argument is not valid">,
Etor<"UNSUPPORTED_ENUMERATION", "enumerator argument is not supported by the device">,
Etor<"UNKNOWN", "Unknown or internal error">
];
}
def : Struct {
let name = "ol_error_struct_t";
let desc = "Details of the error condition returned by an API call";
let members = [
StructMember<"ol_errc_t", "Code", "The error code">,
StructMember<"const char*", "Details", "String containing error details">
];
}
def : Typedef {
let name = "ol_result_t";
let desc = "Result type returned by all entry points.";
let value = "const ol_error_struct_t*";
}
def : Macro {
let name = "OL_SUCCESS";
let desc = "Success condition";
let value = "NULL";
}
def : Struct {
let name = "ol_code_location_t";
let desc = "Code location information that can optionally be associated with an API call";
let members = [
StructMember<"const char*", "FunctionName", "Function name">,
StructMember<"const char*", "SourceFile", "Source code file">,
StructMember<"uint32_t", "LineNumber", "Source code line number">,
StructMember<"uint32_t", "ColumnNumber", "Source code column number">
];
}
def : Function {
let name = "olInit";
let desc = "Perform initialization of the Offload library and plugins";
let 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`"
];
let params = [];
let returns = [];
}
def : Function {
let name = "olShutDown";
let desc = "Release the resources in use by Offload";
let 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"
];
let params = [];
let returns = [];
}

View File

@@ -0,0 +1,106 @@
//===-- Device.td - Device definitions for Offload ---------*- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file contains Offload API definitions related to the Device handle
//
//===----------------------------------------------------------------------===//
def : Enum {
let name = "ol_device_type_t";
let desc = "Supported device types";
let etors =[
Etor<"DEFAULT", "The default device type as preferred by the runtime">,
Etor<"ALL", "Devices of all types">,
Etor<"GPU", "GPU device type">,
Etor<"CPU", "CPU device type">,
];
}
def : Enum {
let name = "ol_device_info_t";
let desc = "Supported device info";
let is_typed = 1;
let etors =[
TaggedEtor<"TYPE", "ol_device_type_t", "type of the device">,
TaggedEtor<"PLATFORM", "ol_platform_handle_t", "the platform associated with the device">,
TaggedEtor<"NAME", "char[]", "Device name">,
TaggedEtor<"VENDOR", "char[]", "Device vendor">,
TaggedEtor<"DRIVER_VERSION", "char[]", "Driver version">
];
}
def : Function {
let name = "olGetDeviceCount";
let desc = "Retrieves the number of available devices within a platform";
let params = [
Param<"ol_platform_handle_t", "Platform", "handle of the platform instance", PARAM_IN>,
Param<"uint32_t*", "NumDevices", "pointer to the number of devices.", PARAM_OUT>
];
let returns = [];
}
def : Function {
let name = "olGetDevice";
let desc = "Retrieves devices within a platform";
let details = [
"Multiple calls to this function will return identical device handles, in the same order.",
];
let params = [
Param<"ol_platform_handle_t", "Platform", "handle of the platform instance", PARAM_IN>,
Param<"uint32_t", "NumEntries", "the number of devices to be added to phDevices, which must be greater than zero", PARAM_IN>,
RangedParam<"ol_device_handle_t*", "Devices", "Array of device handles. "
"If NumEntries is less than the number of devices available, then this function shall only retrieve that number of devices.", PARAM_OUT,
Range<"0", "NumEntries">>
];
let returns = [
Return<"OL_ERRC_INVALID_SIZE", [
"`NumEntries == 0`"
]>
];
}
def : Function {
let name = "olGetDeviceInfo";
let desc = "Queries the given property of the device";
let details = [];
let params = [
Param<"ol_device_handle_t", "Device", "handle of the device instance", PARAM_IN>,
Param<"ol_device_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 device."
]>,
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 = "olGetDeviceInfoSize";
let desc = "Returns the storage size of the given device query";
let details = [];
let params = [
Param<"ol_device_handle_t", "Device", "handle of the device instance", PARAM_IN>,
Param<"ol_device_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 device."
]>,
Return<"OL_ERRC_INVALID_DEVICE">
];
}

View File

@@ -0,0 +1,15 @@
//===-- OffloadAPI.td - Root tablegen file for Offload -----*- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
// Always include this file first
include "APIDefs.td"
// Add API definition files here
include "Common.td"
include "Platform.td"
include "Device.td"

View File

@@ -0,0 +1,112 @@
//===-- Platform.td - Platform definitions for Offload -----*- tablegen -*-===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This file contains Offload API definitions related to the Platform handle
//
//===----------------------------------------------------------------------===//
def : Function {
let name = "olGetPlatform";
let desc = "Retrieves all available platforms";
let details = [
"Multiple calls to this function will return identical platforms handles, in the same order.",
];
let params = [
Param<"uint32_t", "NumEntries",
"The number of platforms to be added to Platforms. NumEntries must be "
"greater than zero.",
PARAM_IN>,
RangedParam<"ol_platform_handle_t*", "Platforms",
"Array of handle of platforms. If NumEntries is less than the number of "
"platforms available, then olGetPlatform shall only retrieve that "
"number of platforms.",
PARAM_OUT, Range<"0", "NumEntries">>
];
let returns = [
Return<"OL_ERRC_INVALID_SIZE", [
"`NumEntries == 0`"
]>
];
}
def : Function {
let name = "olGetPlatformCount";
let desc = "Retrieves the number of available platforms";
let params = [
Param<"uint32_t*",
"NumPlatforms", "returns the total number of platforms available.",
PARAM_OUT>
];
let returns = [];
}
def : Enum {
let name = "ol_platform_info_t";
let desc = "Supported platform info";
let is_typed = 1;
let etors = [
TaggedEtor<"NAME", "char[]", "The string denoting name of the platform. The size of the info needs to be dynamically queried.">,
TaggedEtor<"VENDOR_NAME", "char[]", "The string denoting name of the vendor of the platform. The size of the info needs to be dynamically queried.">,
TaggedEtor<"VERSION", "char[]", "The string denoting the version of the platform. The size of the info needs to be dynamically queried.">,
TaggedEtor<"BACKEND", "ol_platform_backend_t", "The native backend of the platform.">
];
}
def : Enum {
let name = "ol_platform_backend_t";
let desc = "Identifies the native backend of the platform";
let etors =[
Etor<"UNKNOWN", "The backend is not recognized">,
Etor<"CUDA", "The backend is CUDA">,
Etor<"AMDGPU", "The backend is AMDGPU">,
];
}
def : Function {
let name = "olGetPlatformInfo";
let desc = "Queries the given property of the platform";
let details = [
"`olGetPlatformInfoSize` can be used to query the storage size "
"required for the given query."
];
let params = [
Param<"ol_platform_handle_t", "Platform", "handle of the platform", PARAM_IN>,
Param<"ol_platform_info_t", "PropName", "type of the info to retrieve", PARAM_IN>,
Param<"size_t", "PropSize", "the number of bytes pointed to by pPlatformInfo.", PARAM_IN>,
TypeTaggedParam<"void*", "PropValue", "array of bytes holding the info. "
"If Size is not equal to or greater to the real number of bytes needed to return the info "
"then the OL_ERRC_INVALID_SIZE error is returned and pPlatformInfo is not used.", PARAM_OUT,
TypeInfo<"PropName" , "PropSize">>
];
let returns = [
Return<"OL_ERRC_UNSUPPORTED_ENUMERATION", [
"If `PropName` is not supported by the platform."
]>,
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_PLATFORM">
];
}
def : Function {
let name = "olGetPlatformInfoSize";
let desc = "Returns the storage size of the given platform query";
let details = [];
let params = [
Param<"ol_platform_handle_t", "Platform", "handle of the platform", PARAM_IN>,
Param<"ol_platform_info_t", "PropName", "type of the info to query", 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 platform."
]>,
Return<"OL_ERRC_INVALID_PLATFORM">
];
}

View File

@@ -0,0 +1,150 @@
# Offload API definitions
**Note**: This is a work-in-progress. It is loosely based on equivalent
tooling in Unified Runtime.
The Tablegen files in this directory are used to define the Offload API. They
are used with the `offload-tblgen` tool to generate API headers, print headers,
and other implementation details.
The root file is `OffloadAPI.td` - additional `.td` files can be included in
this file to add them to the API.
## API Objects
The API consists of a number of objects, which always have a *name* field and
*description* field, and are one of the following types:
### Function
Represents an API entry point function. Has a list of returns and parameters.
Also has fields for details (representing a bullet-point list of
information about the function that would otherwise be too detailed for the
description), and analogues (equivalent functions in other APIs).
#### Parameter
Represents a parameter to a function, has *type*, *name*, and *desc* fields.
Also has a *flags* field containing flags representing whether the parameter is
in, out, or optional.
The *type* field is used to infer if the parameter is a pointer or handle type.
A *handle* type is a pointer to an opaque struct, used to abstract over
plugin-specific implementation details.
There are two special variants of a *parameter*:
* **RangedParameter** - Represents a parameter that has a range described by other parameters. Generally these are pointers to an arbitrary number of objects. The range is used for generating validation and printing code. E.g, a range might be between `(0, NumDevices)`
* **TypeTaggedParameter** - Represents a parameter (usually of `void*` type) that has the type and size of its pointee data described by other function parameters. The type is usually described by a type-tagged enum. This allows functions (e.g. `olGetDeviceInfo`) to return data of an arbitrary type.
#### Return
A return represents a possible return code from the function, and optionally a
list of conditions in which this value may be returned. The conditions list is
not expected to be exhaustive. A condition is considered free-form text, but
if it is wrapped in \`backticks\` then it is treated as literal code
representing an error condition (e.g. `someParam < 1`). These conditions are
used to automatically create validation checks by the `offload-tblgen`
validation generator.
Returns are automatically generated for functions with pointer or handle
parameters, so API authors do not need to exhaustively add null checks for
these types of parameters. All functions also get a number of default return
values automatically.
### Struct
Represents a struct. Contains a list of members, which each have a *type*,
*name*, and *desc*.
Also optionally takes a *base_class* field. If this is either of the special
`offload_base_properties_t` or `offload_base_desc_t` structs, then the struct
will inherit members from those structs. The generated struct does **not** use
actual C++ inheritance, but instead explicitly has those members copied in,
which preserves ABI compatibility with C.
### Enum
Represents a C-style enum. Contains a list of `etor` values, which have a name
and description.
A `TaggedEtor` record type also exists which addtionally takes a type. This type
is used when the enum is used as a parameter to a function with a type-tagged
function parameter (e.g. `olGetDeviceInfo`).
All enums automatically get a `<enum_name>_FORCE_UINT32 = 0x7fffffff` value,
which forces the underlying type to be uint32.
### Handle
Represents a pointer to an opaque struct, as described in the Parameter section.
It does not take any extra fields.
### Typedef
Represents a typedef, contains only a *value* field.
### Macro
Represents a C preprocessor `#define`. Contains a *value* field. Optionally
takes a *condition* field, which allows the macro to be conditionally defined,
and an *alt_value* field, which represents the value if the condition is false.
Macro arguments are presented in the *name* field (e.g. name = `mymacro(arg)`).
While there may seem little point generating a macro from tablegen, doing this
allows the entire source of the header file to be generated from the tablegen
files, rather than requiring a mix of C source and tablegen.
## Generation
### API header
```
./offload-tblgen -I <path-to-llvm>/offload/API <path-to-llvm>/offload/API/OffloadAPI.td --gen-api
```
The comments in the generated header are in Doxygen format, although
generating documentation from them hasn't been implemented yet.
The entirety of this header is generated by Tablegen, rather than having a predefined header file that includes one or more `.inc` files. This is because this header is expected to be part of the installation and distributed to end-users, so should be self-contained.
### Entry Points
```
./offload-tblgen -I <path-to-llvm>/offload/API <path-to-llvm>/offload/API/OffloadAPI.td --gen-entry-points
```
These functions form the actual Offload interface, and are wrappers over the
functions that contain the actual implementation (see
'Adding a new entry point').
They implement automatically generated validation checks, and tracing of
function calls with arguments and results. The tracing can be enabled with the
`OFFLOAD_TRACE` environment variable.
### Implementation function declarations
```
./offload-tblgen -I <path-to-llvm>/offload/API <path-to-llvm>/offload/API/OffloadAPI.td --gen-impl-func-decls
```
Generates declarations of the implementation of functions of every entry point
in the API, e.g. `offloadDeviceFoo_impl` for `offloadDeviceFoo`.
### Print header
```
./offload-tblgen -I <path-to-llvm>/offload/API <path-to-llvm>/offload/API/OffloadAPI.td --gen-print-header
```
This header contains `std::ostream &operator<<(std::ostream&)` definitions for
various API objects, including function parameters.
As with the API header, it is expected that this header is part of the installed
package, so it is entirely generated by Tablegen.
For ease of implementation, and since it is not strictly part of the API, this
is a C++ header file. If a C version is desirable it could be added.
### Future Tablegen backends
`RecordTypes.hpp` contains wrappers for all of the API object types, which will
allow more backends to be easily added in future.
## Adding to the API
A new object can be added to the API by adding to one of the existing `.td`
files. It is also possible to add a new tablegen file to the API by adding it
to the includes in `OffloadAPI.td`. When the offload target is rebuilt, the
new definition will be included in the generated files.
### Adding a new entry point
When a new entry point is added (e.g. `offloadDeviceFoo`), the actual entry
point is automatically generated, which contains validation and tracing code.
It expects an implementation function (`offloadDeviceFoo_impl`) to be defined,
which it will call into. The definition of this implementation function should
be added to `src/offload_impl.cpp`

View File

@@ -0,0 +1,43 @@
add_subdirectory(API)
add_llvm_library(
LLVMOffload SHARED
src/OffloadLib.cpp
src/OffloadImpl.cpp
LINK_COMPONENTS
FrontendOpenMP
Support
)
foreach(plugin IN LISTS LIBOMPTARGET_PLUGINS_TO_BUILD)
target_link_libraries(LLVMOffload PRIVATE omptarget.rtl.${plugin})
endforeach()
if(LIBOMP_HAVE_VERSION_SCRIPT_FLAG)
target_link_libraries(LLVMOffload PRIVATE "-Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/exports")
endif()
target_include_directories(LLVMOffload PUBLIC
${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)
target_compile_options(LLVMOffload PRIVATE ${offload_compile_flags})
target_link_options(LLVMOffload PRIVATE ${offload_link_flags})
target_compile_definitions(LLVMOffload PRIVATE
TARGET_NAME="Liboffload"
DEBUG_PREFIX="Liboffload"
)
set_target_properties(LLVMOffload PROPERTIES
POSITION_INDEPENDENT_CODE ON
INSTALL_RPATH "$ORIGIN"
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)

View File

@@ -0,0 +1,8 @@
# Offload New API
This directory contains the implementation of the experimental work-in-progress
new API for Offload. It builds on top of the existing plugin implementations but
provides a single level of abstraction suitable for runtimes for languages other
than OpenMP to be built on top of.
See the [API definition readme](API/README.md) for implementation details.

View File

@@ -0,0 +1,6 @@
VERS1.0 {
global:
ol*;
local:
*;
};

View File

@@ -0,0 +1,94 @@
//===- offload_impl.hpp- Implementation helpers for the Offload library ---===//
//
// 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
//
//===----------------------------------------------------------------------===//
#pragma once
#include <OffloadAPI.h>
#include <iostream>
#include <memory>
#include <optional>
#include <set>
#include <string>
#include <unordered_set>
#include <vector>
#include "llvm/ADT/DenseSet.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/ADT/StringSet.h"
struct OffloadConfig {
bool TracingEnabled = false;
};
OffloadConfig &offloadConfig();
// Use the StringSet container to efficiently deduplicate repeated error
// strings (e.g. if the same error is hit constantly in a long running program)
llvm::StringSet<> &errorStrs();
// Use an unordered_set to avoid duplicates of error structs themselves.
// We cannot store the structs directly as returned pointers to them must always
// be valid, and a rehash of the set may invalidate them. This requires
// custom hash and equal_to function objects.
using ErrPtrT = std::unique_ptr<ol_error_struct_t>;
struct ErrPtrEqual {
bool operator()(const ErrPtrT &lhs, const ErrPtrT &rhs) const {
if (!lhs && !rhs) {
return true;
}
if (!lhs || !rhs) {
return false;
}
bool StrsEqual = false;
if (lhs->Details == NULL && rhs->Details == NULL) {
StrsEqual = true;
} else if (lhs->Details != NULL && rhs->Details != NULL) {
StrsEqual = (std::strcmp(lhs->Details, rhs->Details) == 0);
}
return (lhs->Code == rhs->Code) && StrsEqual;
}
};
struct ErrPtrHash {
size_t operator()(const ErrPtrT &e) const {
if (!e) {
// We shouldn't store empty errors (i.e. success), but just in case
return 0lu;
} else {
return std::hash<int>{}(e->Code);
}
}
};
using ErrSetT = std::unordered_set<ErrPtrT, ErrPtrHash, ErrPtrEqual>;
ErrSetT &errors();
struct ol_impl_result_t {
ol_impl_result_t(std::nullptr_t) : Result(OL_SUCCESS) {}
ol_impl_result_t(ol_errc_t Code) {
if (Code == OL_ERRC_SUCCESS) {
Result = nullptr;
} else {
auto Err = std::unique_ptr<ol_error_struct_t>(
new ol_error_struct_t{Code, nullptr});
Result = errors().emplace(std::move(Err)).first->get();
}
}
ol_impl_result_t(ol_errc_t Code, llvm::StringRef Details) {
assert(Code != OL_ERRC_SUCCESS);
Result = nullptr;
auto DetailsStr = errorStrs().insert(Details).first->getKeyData();
auto Err = std::unique_ptr<ol_error_struct_t>(
new ol_error_struct_t{Code, DetailsStr});
Result = errors().emplace(std::move(Err)).first->get();
}
operator ol_result_t() { return Result; }
private:
ol_result_t Result;
};

File diff suppressed because it is too large Load Diff

View File

@@ -0,0 +1,441 @@
//===- 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
//
//===----------------------------------------------------------------------===//
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olInit_val() {
if (true /*enableParameterValidation*/) {
}
return olInit_impl();
}
OL_APIEXPORT ol_result_t OL_APICALL olInit() {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olInit";
}
ol_result_t Result = olInit_val();
if (offloadConfig().TracingEnabled) {
std::cout << "()";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olInitWithCodeLoc(ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olInit();
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olShutDown_val() {
if (true /*enableParameterValidation*/) {
}
return olShutDown_impl();
}
OL_APIEXPORT ol_result_t OL_APICALL olShutDown() {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olShutDown";
}
ol_result_t Result = olShutDown_val();
if (offloadConfig().TracingEnabled) {
std::cout << "()";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olShutDownWithCodeLoc(ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olShutDown();
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olGetPlatform_val(uint32_t NumEntries,
ol_platform_handle_t *Platforms) {
if (true /*enableParameterValidation*/) {
if (NumEntries == 0) {
return OL_ERRC_INVALID_SIZE;
}
if (NULL == Platforms) {
return OL_ERRC_INVALID_NULL_POINTER;
}
}
return olGetPlatform_impl(NumEntries, Platforms);
}
OL_APIEXPORT ol_result_t OL_APICALL
olGetPlatform(uint32_t NumEntries, ol_platform_handle_t *Platforms) {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olGetPlatform";
}
ol_result_t Result = olGetPlatform_val(NumEntries, Platforms);
if (offloadConfig().TracingEnabled) {
ol_get_platform_params_t Params = {&NumEntries, &Platforms};
std::cout << "(" << &Params << ")";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olGetPlatformWithCodeLoc(uint32_t NumEntries,
ol_platform_handle_t *Platforms,
ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olGetPlatform(NumEntries, Platforms);
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olGetPlatformCount_val(uint32_t *NumPlatforms) {
if (true /*enableParameterValidation*/) {
if (NULL == NumPlatforms) {
return OL_ERRC_INVALID_NULL_POINTER;
}
}
return olGetPlatformCount_impl(NumPlatforms);
}
OL_APIEXPORT ol_result_t OL_APICALL olGetPlatformCount(uint32_t *NumPlatforms) {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olGetPlatformCount";
}
ol_result_t Result = olGetPlatformCount_val(NumPlatforms);
if (offloadConfig().TracingEnabled) {
ol_get_platform_count_params_t Params = {&NumPlatforms};
std::cout << "(" << &Params << ")";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olGetPlatformCountWithCodeLoc(uint32_t *NumPlatforms,
ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olGetPlatformCount(NumPlatforms);
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olGetPlatformInfo_val(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t PropSize, void *PropValue) {
if (true /*enableParameterValidation*/) {
if (PropSize == 0) {
return OL_ERRC_INVALID_SIZE;
}
if (NULL == Platform) {
return OL_ERRC_INVALID_NULL_HANDLE;
}
if (NULL == PropValue) {
return OL_ERRC_INVALID_NULL_POINTER;
}
}
return olGetPlatformInfo_impl(Platform, PropName, PropSize, PropValue);
}
OL_APIEXPORT ol_result_t OL_APICALL
olGetPlatformInfo(ol_platform_handle_t Platform, ol_platform_info_t PropName,
size_t PropSize, void *PropValue) {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olGetPlatformInfo";
}
ol_result_t Result =
olGetPlatformInfo_val(Platform, PropName, PropSize, PropValue);
if (offloadConfig().TracingEnabled) {
ol_get_platform_info_params_t Params = {&Platform, &PropName, &PropSize,
&PropValue};
std::cout << "(" << &Params << ")";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olGetPlatformInfoWithCodeLoc(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t PropSize, void *PropValue,
ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result =
olGetPlatformInfo(Platform, PropName, PropSize, PropValue);
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olGetPlatformInfoSize_val(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t *PropSizeRet) {
if (true /*enableParameterValidation*/) {
if (NULL == Platform) {
return OL_ERRC_INVALID_NULL_HANDLE;
}
if (NULL == PropSizeRet) {
return OL_ERRC_INVALID_NULL_POINTER;
}
}
return olGetPlatformInfoSize_impl(Platform, PropName, PropSizeRet);
}
OL_APIEXPORT ol_result_t OL_APICALL
olGetPlatformInfoSize(ol_platform_handle_t Platform,
ol_platform_info_t PropName, size_t *PropSizeRet) {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olGetPlatformInfoSize";
}
ol_result_t Result =
olGetPlatformInfoSize_val(Platform, PropName, PropSizeRet);
if (offloadConfig().TracingEnabled) {
ol_get_platform_info_size_params_t Params = {&Platform, &PropName,
&PropSizeRet};
std::cout << "(" << &Params << ")";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olGetPlatformInfoSizeWithCodeLoc(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t *PropSizeRet,
ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olGetPlatformInfoSize(Platform, PropName, PropSizeRet);
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olGetDeviceCount_val(ol_platform_handle_t Platform,
uint32_t *NumDevices) {
if (true /*enableParameterValidation*/) {
if (NULL == Platform) {
return OL_ERRC_INVALID_NULL_HANDLE;
}
if (NULL == NumDevices) {
return OL_ERRC_INVALID_NULL_POINTER;
}
}
return olGetDeviceCount_impl(Platform, NumDevices);
}
OL_APIEXPORT ol_result_t OL_APICALL
olGetDeviceCount(ol_platform_handle_t Platform, uint32_t *NumDevices) {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olGetDeviceCount";
}
ol_result_t Result = olGetDeviceCount_val(Platform, NumDevices);
if (offloadConfig().TracingEnabled) {
ol_get_device_count_params_t Params = {&Platform, &NumDevices};
std::cout << "(" << &Params << ")";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olGetDeviceCountWithCodeLoc(ol_platform_handle_t Platform,
uint32_t *NumDevices,
ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olGetDeviceCount(Platform, NumDevices);
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olGetDevice_val(ol_platform_handle_t Platform,
uint32_t NumEntries,
ol_device_handle_t *Devices) {
if (true /*enableParameterValidation*/) {
if (NumEntries == 0) {
return OL_ERRC_INVALID_SIZE;
}
if (NULL == Platform) {
return OL_ERRC_INVALID_NULL_HANDLE;
}
if (NULL == Devices) {
return OL_ERRC_INVALID_NULL_POINTER;
}
}
return olGetDevice_impl(Platform, NumEntries, Devices);
}
OL_APIEXPORT ol_result_t OL_APICALL olGetDevice(ol_platform_handle_t Platform,
uint32_t NumEntries,
ol_device_handle_t *Devices) {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olGetDevice";
}
ol_result_t Result = olGetDevice_val(Platform, NumEntries, Devices);
if (offloadConfig().TracingEnabled) {
ol_get_device_params_t Params = {&Platform, &NumEntries, &Devices};
std::cout << "(" << &Params << ")";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olGetDeviceWithCodeLoc(ol_platform_handle_t Platform,
uint32_t NumEntries,
ol_device_handle_t *Devices,
ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olGetDevice(Platform, NumEntries, Devices);
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olGetDeviceInfo_val(ol_device_handle_t Device,
ol_device_info_t PropName, size_t PropSize,
void *PropValue) {
if (true /*enableParameterValidation*/) {
if (PropSize == 0) {
return OL_ERRC_INVALID_SIZE;
}
if (NULL == Device) {
return OL_ERRC_INVALID_NULL_HANDLE;
}
if (NULL == PropValue) {
return OL_ERRC_INVALID_NULL_POINTER;
}
}
return olGetDeviceInfo_impl(Device, PropName, PropSize, PropValue);
}
OL_APIEXPORT ol_result_t OL_APICALL olGetDeviceInfo(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t PropSize,
void *PropValue) {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olGetDeviceInfo";
}
ol_result_t Result =
olGetDeviceInfo_val(Device, PropName, PropSize, PropValue);
if (offloadConfig().TracingEnabled) {
ol_get_device_info_params_t Params = {&Device, &PropName, &PropSize,
&PropValue};
std::cout << "(" << &Params << ")";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olGetDeviceInfoWithCodeLoc(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t PropSize, void *PropValue,
ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olGetDeviceInfo(Device, PropName, PropSize, PropValue);
currentCodeLocation() = nullptr;
return Result;
}
///////////////////////////////////////////////////////////////////////////////
ol_impl_result_t olGetDeviceInfoSize_val(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t *PropSizeRet) {
if (true /*enableParameterValidation*/) {
if (NULL == Device) {
return OL_ERRC_INVALID_NULL_HANDLE;
}
if (NULL == PropSizeRet) {
return OL_ERRC_INVALID_NULL_POINTER;
}
}
return olGetDeviceInfoSize_impl(Device, PropName, PropSizeRet);
}
OL_APIEXPORT ol_result_t OL_APICALL olGetDeviceInfoSize(
ol_device_handle_t Device, ol_device_info_t PropName, size_t *PropSizeRet) {
if (offloadConfig().TracingEnabled) {
std::cout << "---> olGetDeviceInfoSize";
}
ol_result_t Result = olGetDeviceInfoSize_val(Device, PropName, PropSizeRet);
if (offloadConfig().TracingEnabled) {
ol_get_device_info_size_params_t Params = {&Device, &PropName,
&PropSizeRet};
std::cout << "(" << &Params << ")";
std::cout << "-> " << Result << "\n";
if (Result && Result->Details) {
std::cout << " *Error Details* " << Result->Details << " \n";
}
}
return Result;
}
ol_result_t olGetDeviceInfoSizeWithCodeLoc(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t *PropSizeRet,
ol_code_location_t *CodeLocation) {
currentCodeLocation() = CodeLocation;
ol_result_t Result = olGetDeviceInfoSize(Device, PropName, PropSizeRet);
currentCodeLocation() = nullptr;
return Result;
}

View File

@@ -0,0 +1,34 @@
//===- 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_FUNC
#error Please define the macro OFFLOAD_FUNC(Function)
#endif
OFFLOAD_FUNC(olInit)
OFFLOAD_FUNC(olShutDown)
OFFLOAD_FUNC(olGetPlatform)
OFFLOAD_FUNC(olGetPlatformCount)
OFFLOAD_FUNC(olGetPlatformInfo)
OFFLOAD_FUNC(olGetPlatformInfoSize)
OFFLOAD_FUNC(olGetDeviceCount)
OFFLOAD_FUNC(olGetDevice)
OFFLOAD_FUNC(olGetDeviceInfo)
OFFLOAD_FUNC(olGetDeviceInfoSize)
OFFLOAD_FUNC(olInitWithCodeLoc)
OFFLOAD_FUNC(olShutDownWithCodeLoc)
OFFLOAD_FUNC(olGetPlatformWithCodeLoc)
OFFLOAD_FUNC(olGetPlatformCountWithCodeLoc)
OFFLOAD_FUNC(olGetPlatformInfoWithCodeLoc)
OFFLOAD_FUNC(olGetPlatformInfoSizeWithCodeLoc)
OFFLOAD_FUNC(olGetDeviceCountWithCodeLoc)
OFFLOAD_FUNC(olGetDeviceWithCodeLoc)
OFFLOAD_FUNC(olGetDeviceInfoWithCodeLoc)
OFFLOAD_FUNC(olGetDeviceInfoSizeWithCodeLoc)
#undef OFFLOAD_FUNC

View File

@@ -0,0 +1,38 @@
//===- 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
//
//===----------------------------------------------------------------------===//
ol_impl_result_t olInit_impl();
ol_impl_result_t olShutDown_impl();
ol_impl_result_t olGetPlatform_impl(uint32_t NumEntries,
ol_platform_handle_t *Platforms);
ol_impl_result_t olGetPlatformCount_impl(uint32_t *NumPlatforms);
ol_impl_result_t olGetPlatformInfo_impl(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t PropSize, void *PropValue);
ol_impl_result_t olGetPlatformInfoSize_impl(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t *PropSizeRet);
ol_impl_result_t olGetDeviceCount_impl(ol_platform_handle_t Platform,
uint32_t *NumDevices);
ol_impl_result_t olGetDevice_impl(ol_platform_handle_t Platform,
uint32_t NumEntries,
ol_device_handle_t *Devices);
ol_impl_result_t olGetDeviceInfo_impl(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t PropSize, void *PropValue);
ol_impl_result_t olGetDeviceInfoSize_impl(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t *PropSizeRet);

View File

@@ -0,0 +1,428 @@
//===- 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 <OffloadAPI.h>
#include <ostream>
template <typename T>
inline ol_result_t printPtr(std::ostream &os, const T *ptr);
template <typename T>
inline void printTagged(std::ostream &os, const void *ptr, T value,
size_t size);
template <typename T> struct is_handle : std::false_type {};
template <> struct is_handle<ol_platform_handle_t> : std::true_type {};
template <> struct is_handle<ol_device_handle_t> : std::true_type {};
template <> struct is_handle<ol_context_handle_t> : std::true_type {};
template <typename T> inline constexpr bool is_handle_v = is_handle<T>::value;
inline std::ostream &operator<<(std::ostream &os, enum ol_errc_t value);
inline std::ostream &operator<<(std::ostream &os,
enum ol_platform_info_t value);
inline std::ostream &operator<<(std::ostream &os,
enum ol_platform_backend_t value);
inline std::ostream &operator<<(std::ostream &os, enum ol_device_type_t value);
inline std::ostream &operator<<(std::ostream &os, enum ol_device_info_t value);
///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ol_errc_t type
/// @returns std::ostream &
inline std::ostream &operator<<(std::ostream &os, enum ol_errc_t value) {
switch (value) {
case OL_ERRC_SUCCESS:
os << "OL_ERRC_SUCCESS";
break;
case OL_ERRC_INVALID_VALUE:
os << "OL_ERRC_INVALID_VALUE";
break;
case OL_ERRC_INVALID_PLATFORM:
os << "OL_ERRC_INVALID_PLATFORM";
break;
case OL_ERRC_DEVICE_NOT_FOUND:
os << "OL_ERRC_DEVICE_NOT_FOUND";
break;
case OL_ERRC_INVALID_DEVICE:
os << "OL_ERRC_INVALID_DEVICE";
break;
case OL_ERRC_DEVICE_LOST:
os << "OL_ERRC_DEVICE_LOST";
break;
case OL_ERRC_UNINITIALIZED:
os << "OL_ERRC_UNINITIALIZED";
break;
case OL_ERRC_OUT_OF_RESOURCES:
os << "OL_ERRC_OUT_OF_RESOURCES";
break;
case OL_ERRC_UNSUPPORTED_VERSION:
os << "OL_ERRC_UNSUPPORTED_VERSION";
break;
case OL_ERRC_UNSUPPORTED_FEATURE:
os << "OL_ERRC_UNSUPPORTED_FEATURE";
break;
case OL_ERRC_INVALID_ARGUMENT:
os << "OL_ERRC_INVALID_ARGUMENT";
break;
case OL_ERRC_INVALID_NULL_HANDLE:
os << "OL_ERRC_INVALID_NULL_HANDLE";
break;
case OL_ERRC_INVALID_NULL_POINTER:
os << "OL_ERRC_INVALID_NULL_POINTER";
break;
case OL_ERRC_INVALID_SIZE:
os << "OL_ERRC_INVALID_SIZE";
break;
case OL_ERRC_INVALID_ENUMERATION:
os << "OL_ERRC_INVALID_ENUMERATION";
break;
case OL_ERRC_UNSUPPORTED_ENUMERATION:
os << "OL_ERRC_UNSUPPORTED_ENUMERATION";
break;
case OL_ERRC_UNKNOWN:
os << "OL_ERRC_UNKNOWN";
break;
default:
os << "unknown enumerator";
break;
}
return os;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ol_platform_info_t type
/// @returns std::ostream &
inline std::ostream &operator<<(std::ostream &os,
enum ol_platform_info_t value) {
switch (value) {
case OL_PLATFORM_INFO_NAME:
os << "OL_PLATFORM_INFO_NAME";
break;
case OL_PLATFORM_INFO_VENDOR_NAME:
os << "OL_PLATFORM_INFO_VENDOR_NAME";
break;
case OL_PLATFORM_INFO_VERSION:
os << "OL_PLATFORM_INFO_VERSION";
break;
case OL_PLATFORM_INFO_BACKEND:
os << "OL_PLATFORM_INFO_BACKEND";
break;
default:
os << "unknown enumerator";
break;
}
return os;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Print type-tagged ol_platform_info_t enum value
/// @returns std::ostream &
template <>
inline void printTagged(std::ostream &os, const void *ptr,
ol_platform_info_t value, size_t size) {
if (ptr == NULL) {
printPtr(os, ptr);
return;
}
switch (value) {
case OL_PLATFORM_INFO_NAME: {
printPtr(os, (const char *)ptr);
break;
}
case OL_PLATFORM_INFO_VENDOR_NAME: {
printPtr(os, (const char *)ptr);
break;
}
case OL_PLATFORM_INFO_VERSION: {
printPtr(os, (const char *)ptr);
break;
}
case OL_PLATFORM_INFO_BACKEND: {
const ol_platform_backend_t *const tptr =
(const ol_platform_backend_t *const)ptr;
os << (const void *)tptr << " (";
os << *tptr;
os << ")";
break;
}
default:
os << "unknown enumerator";
break;
}
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ol_platform_backend_t type
/// @returns std::ostream &
inline std::ostream &operator<<(std::ostream &os,
enum ol_platform_backend_t value) {
switch (value) {
case OL_PLATFORM_BACKEND_UNKNOWN:
os << "OL_PLATFORM_BACKEND_UNKNOWN";
break;
case OL_PLATFORM_BACKEND_CUDA:
os << "OL_PLATFORM_BACKEND_CUDA";
break;
case OL_PLATFORM_BACKEND_AMDGPU:
os << "OL_PLATFORM_BACKEND_AMDGPU";
break;
default:
os << "unknown enumerator";
break;
}
return os;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ol_device_type_t type
/// @returns std::ostream &
inline std::ostream &operator<<(std::ostream &os, enum ol_device_type_t value) {
switch (value) {
case OL_DEVICE_TYPE_DEFAULT:
os << "OL_DEVICE_TYPE_DEFAULT";
break;
case OL_DEVICE_TYPE_ALL:
os << "OL_DEVICE_TYPE_ALL";
break;
case OL_DEVICE_TYPE_GPU:
os << "OL_DEVICE_TYPE_GPU";
break;
case OL_DEVICE_TYPE_CPU:
os << "OL_DEVICE_TYPE_CPU";
break;
default:
os << "unknown enumerator";
break;
}
return os;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Print operator for the ol_device_info_t type
/// @returns std::ostream &
inline std::ostream &operator<<(std::ostream &os, enum ol_device_info_t value) {
switch (value) {
case OL_DEVICE_INFO_TYPE:
os << "OL_DEVICE_INFO_TYPE";
break;
case OL_DEVICE_INFO_PLATFORM:
os << "OL_DEVICE_INFO_PLATFORM";
break;
case OL_DEVICE_INFO_NAME:
os << "OL_DEVICE_INFO_NAME";
break;
case OL_DEVICE_INFO_VENDOR:
os << "OL_DEVICE_INFO_VENDOR";
break;
case OL_DEVICE_INFO_DRIVER_VERSION:
os << "OL_DEVICE_INFO_DRIVER_VERSION";
break;
default:
os << "unknown enumerator";
break;
}
return os;
}
///////////////////////////////////////////////////////////////////////////////
/// @brief Print type-tagged ol_device_info_t enum value
/// @returns std::ostream &
template <>
inline void printTagged(std::ostream &os, const void *ptr,
ol_device_info_t value, size_t size) {
if (ptr == NULL) {
printPtr(os, ptr);
return;
}
switch (value) {
case OL_DEVICE_INFO_TYPE: {
const ol_device_type_t *const tptr = (const ol_device_type_t *const)ptr;
os << (const void *)tptr << " (";
os << *tptr;
os << ")";
break;
}
case OL_DEVICE_INFO_PLATFORM: {
const ol_platform_handle_t *const tptr =
(const ol_platform_handle_t *const)ptr;
os << (const void *)tptr << " (";
os << *tptr;
os << ")";
break;
}
case OL_DEVICE_INFO_NAME: {
printPtr(os, (const char *)ptr);
break;
}
case OL_DEVICE_INFO_VENDOR: {
printPtr(os, (const char *)ptr);
break;
}
case OL_DEVICE_INFO_DRIVER_VERSION: {
printPtr(os, (const char *)ptr);
break;
}
default:
os << "unknown enumerator";
break;
}
}
inline std::ostream &operator<<(std::ostream &os,
const ol_error_struct_t *Err) {
if (Err == nullptr) {
os << "OL_SUCCESS";
} else {
os << Err->Code;
}
return os;
}
inline std::ostream &operator<<(std::ostream &os,
const struct ol_get_platform_params_t *params) {
os << ".NumEntries = ";
os << *params->pNumEntries;
os << ", ";
os << ".Platforms = ";
os << "{";
for (size_t i = 0; i < *params->pNumEntries; i++) {
if (i > 0) {
os << ", ";
}
printPtr(os, (*params->pPlatforms)[i]);
}
os << "}";
return os;
}
inline std::ostream &
operator<<(std::ostream &os,
const struct ol_get_platform_count_params_t *params) {
os << ".NumPlatforms = ";
printPtr(os, *params->pNumPlatforms);
return os;
}
inline std::ostream &
operator<<(std::ostream &os,
const struct ol_get_platform_info_params_t *params) {
os << ".Platform = ";
printPtr(os, *params->pPlatform);
os << ", ";
os << ".PropName = ";
os << *params->pPropName;
os << ", ";
os << ".PropSize = ";
os << *params->pPropSize;
os << ", ";
os << ".PropValue = ";
printTagged(os, *params->pPropValue, *params->pPropName, *params->pPropSize);
return os;
}
inline std::ostream &
operator<<(std::ostream &os,
const struct ol_get_platform_info_size_params_t *params) {
os << ".Platform = ";
printPtr(os, *params->pPlatform);
os << ", ";
os << ".PropName = ";
os << *params->pPropName;
os << ", ";
os << ".PropSizeRet = ";
printPtr(os, *params->pPropSizeRet);
return os;
}
inline std::ostream &
operator<<(std::ostream &os,
const struct ol_get_device_count_params_t *params) {
os << ".Platform = ";
printPtr(os, *params->pPlatform);
os << ", ";
os << ".NumDevices = ";
printPtr(os, *params->pNumDevices);
return os;
}
inline std::ostream &operator<<(std::ostream &os,
const struct ol_get_device_params_t *params) {
os << ".Platform = ";
printPtr(os, *params->pPlatform);
os << ", ";
os << ".NumEntries = ";
os << *params->pNumEntries;
os << ", ";
os << ".Devices = ";
os << "{";
for (size_t i = 0; i < *params->pNumEntries; i++) {
if (i > 0) {
os << ", ";
}
printPtr(os, (*params->pDevices)[i]);
}
os << "}";
return os;
}
inline std::ostream &
operator<<(std::ostream &os, const struct ol_get_device_info_params_t *params) {
os << ".Device = ";
printPtr(os, *params->pDevice);
os << ", ";
os << ".PropName = ";
os << *params->pPropName;
os << ", ";
os << ".PropSize = ";
os << *params->pPropSize;
os << ", ";
os << ".PropValue = ";
printTagged(os, *params->pPropValue, *params->pPropName, *params->pPropSize);
return os;
}
inline std::ostream &
operator<<(std::ostream &os,
const struct ol_get_device_info_size_params_t *params) {
os << ".Device = ";
printPtr(os, *params->pDevice);
os << ", ";
os << ".PropName = ";
os << *params->pPropName;
os << ", ";
os << ".PropSizeRet = ";
printPtr(os, *params->pPropSizeRet);
return os;
}
///////////////////////////////////////////////////////////////////////////////
// @brief Print pointer value
template <typename T>
inline ol_result_t printPtr(std::ostream &os, const T *ptr) {
if (ptr == nullptr) {
os << "nullptr";
} else if constexpr (std::is_pointer_v<T>) {
os << (const void *)(ptr) << " (";
printPtr(os, *ptr);
os << ")";
} else if constexpr (std::is_void_v<T> || is_handle_v<T *>) {
os << (const void *)ptr;
} else if constexpr (std::is_same_v<std::remove_cv_t<T>, char>) {
os << (const void *)(ptr) << " (";
os << ptr;
os << ")";
} else {
os << (const void *)(ptr) << " (";
os << *ptr;
os << ")";
}
return OL_SUCCESS;
}

View File

@@ -0,0 +1,95 @@
//===- helpers.hpp- GetInfo return helpers for the new LLVM/Offload API ---===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// The getInfo*/ReturnHelper facilities provide shortcut way of writing return
// data + size for the various getInfo APIs. Based on the equivalent
// implementations in Unified Runtime.
//
//===----------------------------------------------------------------------===//
#include "OffloadAPI.h"
#include <cstring>
template <typename T, typename Assign>
ol_errc_t getInfoImpl(size_t ParamValueSize, void *ParamValue,
size_t *ParamValueSizeRet, T Value, size_t ValueSize,
Assign &&AssignFunc) {
if (!ParamValue && !ParamValueSizeRet) {
return OL_ERRC_INVALID_NULL_POINTER;
}
if (ParamValue != nullptr) {
if (ParamValueSize < ValueSize) {
return OL_ERRC_INVALID_SIZE;
}
AssignFunc(ParamValue, Value, ValueSize);
}
if (ParamValueSizeRet != nullptr) {
*ParamValueSizeRet = ValueSize;
}
return OL_ERRC_SUCCESS;
}
template <typename T>
ol_errc_t getInfo(size_t ParamValueSize, void *ParamValue,
size_t *ParamValueSizeRet, T Value) {
auto Assignment = [](void *ParamValue, T Value, size_t) {
*static_cast<T *>(ParamValue) = Value;
};
return getInfoImpl(ParamValueSize, ParamValue, ParamValueSizeRet, Value,
sizeof(T), Assignment);
}
template <typename T>
ol_errc_t getInfoArray(size_t array_length, size_t ParamValueSize,
void *ParamValue, size_t *ParamValueSizeRet,
const T *Value) {
return getInfoImpl(ParamValueSize, ParamValue, ParamValueSizeRet, Value,
array_length * sizeof(T), memcpy);
}
template <>
inline ol_errc_t getInfo<const char *>(size_t ParamValueSize, void *ParamValue,
size_t *ParamValueSizeRet,
const char *Value) {
return getInfoArray(strlen(Value) + 1, ParamValueSize, ParamValue,
ParamValueSizeRet, Value);
}
class ReturnHelper {
public:
ReturnHelper(size_t ParamValueSize, void *ParamValue,
size_t *ParamValueSizeRet)
: ParamValueSize(ParamValueSize), ParamValue(ParamValue),
ParamValueSizeRet(ParamValueSizeRet) {}
// A version where in/out info size is represented by a single pointer
// to a value which is updated on return
ReturnHelper(size_t *ParamValueSize, void *ParamValue)
: ParamValueSize(*ParamValueSize), ParamValue(ParamValue),
ParamValueSizeRet(ParamValueSize) {}
// Scalar return Value
template <class T> ol_errc_t operator()(const T &t) {
return getInfo(ParamValueSize, ParamValue, ParamValueSizeRet, t);
}
// Array return Value
template <class T> ol_errc_t operator()(const T *t, size_t s) {
return getInfoArray(s, ParamValueSize, ParamValue, ParamValueSizeRet, t);
}
protected:
size_t ParamValueSize;
void *ParamValue;
size_t *ParamValueSizeRet;
};

View File

@@ -0,0 +1,247 @@
//===- ol_impl.cpp - Implementation of the new LLVM/Offload API ------===//
//
// 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
//
//===----------------------------------------------------------------------===//
//
// This contains the definitions of the new LLVM/Offload API entry points. See
// new-api/API/README.md for more information.
//
//===----------------------------------------------------------------------===//
#include "OffloadImpl.hpp"
#include "Helpers.hpp"
#include "PluginManager.h"
#include "llvm/Support/FormatVariadic.h"
#include <OffloadAPI.h>
#include <mutex>
using namespace llvm;
using namespace llvm::omp::target::plugin;
// Handle type definitions. Ideally these would be 1:1 with the plugins
struct ol_device_handle_t_ {
int DeviceNum;
GenericDeviceTy &Device;
ol_platform_handle_t Platform;
};
struct ol_platform_handle_t_ {
std::unique_ptr<GenericPluginTy> Plugin;
std::vector<ol_device_handle_t_> Devices;
};
using PlatformVecT = SmallVector<ol_platform_handle_t_, 4>;
PlatformVecT &Platforms() {
static PlatformVecT Platforms;
return Platforms;
}
// TODO: Some plugins expect to be linked into libomptarget which defines these
// symbols to implement ompt callbacks. The least invasive workaround here is to
// define them in libLLVMOffload as false/null so they are never used. In future
// it would be better to allow the plugins to implement callbacks without
// pulling in details from libomptarget.
#ifdef OMPT_SUPPORT
namespace llvm::omp::target {
namespace ompt {
bool Initialized = false;
ompt_get_callback_t lookupCallbackByCode = nullptr;
ompt_function_lookup_t lookupCallbackByName = nullptr;
} // namespace ompt
} // namespace llvm::omp::target
#endif
// Every plugin exports this method to create an instance of the plugin type.
#define PLUGIN_TARGET(Name) extern "C" GenericPluginTy *createPlugin_##Name();
#include "Shared/Targets.def"
void initPlugins() {
// Attempt to create an instance of each supported plugin.
#define PLUGIN_TARGET(Name) \
do { \
Platforms().emplace_back(ol_platform_handle_t_{ \
std::unique_ptr<GenericPluginTy>(createPlugin_##Name()), {}}); \
} while (false);
#include "Shared/Targets.def"
// Preemptively initialize all devices in the plugin so we can just return
// them from deviceGet
for (auto &Platform : Platforms()) {
auto Err = Platform.Plugin->init();
[[maybe_unused]] std::string InfoMsg = toString(std::move(Err));
for (auto DevNum = 0; DevNum < Platform.Plugin->number_of_devices();
DevNum++) {
if (Platform.Plugin->init_device(DevNum) == OFFLOAD_SUCCESS) {
Platform.Devices.emplace_back(ol_device_handle_t_{
DevNum, Platform.Plugin->getDevice(DevNum), &Platform});
}
}
}
offloadConfig().TracingEnabled = std::getenv("OFFLOAD_TRACE");
}
// TODO: We can properly reference count here and manage the resources in a more
// clever way
ol_impl_result_t olInit_impl() {
static std::once_flag InitFlag;
std::call_once(InitFlag, initPlugins);
return OL_SUCCESS;
}
ol_impl_result_t olShutDown_impl() { return OL_SUCCESS; }
ol_impl_result_t olGetPlatformCount_impl(uint32_t *NumPlatforms) {
*NumPlatforms = Platforms().size();
return OL_SUCCESS;
}
ol_impl_result_t olGetPlatform_impl(uint32_t NumEntries,
ol_platform_handle_t *PlatformsOut) {
if (NumEntries > Platforms().size()) {
return {OL_ERRC_INVALID_SIZE,
std::string{formatv("{0} platform(s) available but {1} requested.",
Platforms().size(), NumEntries)}};
}
for (uint32_t PlatformIndex = 0; PlatformIndex < NumEntries;
PlatformIndex++) {
PlatformsOut[PlatformIndex] = &(Platforms())[PlatformIndex];
}
return OL_SUCCESS;
}
ol_impl_result_t olGetPlatformInfoImplDetail(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t PropSize, void *PropValue,
size_t *PropSizeRet) {
ReturnHelper ReturnValue(PropSize, PropValue, PropSizeRet);
switch (PropName) {
case OL_PLATFORM_INFO_NAME:
return ReturnValue(Platform->Plugin->getName());
case OL_PLATFORM_INFO_VENDOR_NAME:
// TODO: Implement this
return ReturnValue("Unknown platform vendor");
case OL_PLATFORM_INFO_VERSION: {
return ReturnValue(formatv("v{0}.{1}.{2}", OL_VERSION_MAJOR,
OL_VERSION_MINOR, OL_VERSION_PATCH)
.str()
.c_str());
}
case OL_PLATFORM_INFO_BACKEND: {
auto PluginName = Platform->Plugin->getName();
if (PluginName == StringRef("CUDA")) {
return ReturnValue(OL_PLATFORM_BACKEND_CUDA);
} else if (PluginName == StringRef("AMDGPU")) {
return ReturnValue(OL_PLATFORM_BACKEND_AMDGPU);
} else {
return ReturnValue(OL_PLATFORM_BACKEND_UNKNOWN);
}
}
default:
return OL_ERRC_INVALID_ENUMERATION;
}
return OL_SUCCESS;
}
ol_impl_result_t olGetPlatformInfo_impl(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t PropSize, void *PropValue) {
return olGetPlatformInfoImplDetail(Platform, PropName, PropSize, PropValue,
nullptr);
}
ol_impl_result_t olGetPlatformInfoSize_impl(ol_platform_handle_t Platform,
ol_platform_info_t PropName,
size_t *PropSizeRet) {
return olGetPlatformInfoImplDetail(Platform, PropName, 0, nullptr,
PropSizeRet);
}
ol_impl_result_t olGetDeviceCount_impl(ol_platform_handle_t Platform,
uint32_t *pNumDevices) {
*pNumDevices = static_cast<uint32_t>(Platform->Devices.size());
return OL_SUCCESS;
}
ol_impl_result_t olGetDevice_impl(ol_platform_handle_t Platform,
uint32_t NumEntries,
ol_device_handle_t *Devices) {
if (NumEntries > Platform->Devices.size()) {
return OL_ERRC_INVALID_SIZE;
}
for (uint32_t DeviceIndex = 0; DeviceIndex < NumEntries; DeviceIndex++) {
Devices[DeviceIndex] = &(Platform->Devices[DeviceIndex]);
}
return OL_SUCCESS;
}
ol_impl_result_t olGetDeviceInfoImplDetail(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t PropSize, void *PropValue,
size_t *PropSizeRet) {
ReturnHelper ReturnValue(PropSize, PropValue, PropSizeRet);
InfoQueueTy DevInfo;
if (auto Err = Device->Device.obtainInfoImpl(DevInfo))
return OL_ERRC_OUT_OF_RESOURCES;
// Find the info if it exists under any of the given names
auto GetInfo = [&DevInfo](std::vector<std::string> Names) {
for (auto Name : Names) {
auto InfoKeyMatches = [&](const InfoQueueTy::InfoQueueEntryTy &Info) {
return Info.Key == Name;
};
auto Item = std::find_if(DevInfo.getQueue().begin(),
DevInfo.getQueue().end(), InfoKeyMatches);
if (Item != std::end(DevInfo.getQueue())) {
return Item->Value;
}
}
return std::string("");
};
switch (PropName) {
case OL_DEVICE_INFO_PLATFORM:
return ReturnValue(Device->Platform);
case OL_DEVICE_INFO_TYPE:
return ReturnValue(OL_DEVICE_TYPE_GPU);
case OL_DEVICE_INFO_NAME:
return ReturnValue(GetInfo({"Device Name"}).c_str());
case OL_DEVICE_INFO_VENDOR:
return ReturnValue(GetInfo({"Vendor Name"}).c_str());
case OL_DEVICE_INFO_DRIVER_VERSION:
return ReturnValue(
GetInfo({"CUDA Driver Version", "HSA Runtime Version"}).c_str());
default:
return OL_ERRC_INVALID_ENUMERATION;
}
return OL_SUCCESS;
}
ol_impl_result_t olGetDeviceInfo_impl(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t PropSize, void *PropValue) {
return olGetDeviceInfoImplDetail(Device, PropName, PropSize, PropValue,
nullptr);
}
ol_impl_result_t olGetDeviceInfoSize_impl(ol_device_handle_t Device,
ol_device_info_t PropName,
size_t *PropSizeRet) {
return olGetDeviceInfoImplDetail(Device, PropName, 0, nullptr, PropSizeRet);
}

Some files were not shown because too many files have changed in this diff Show More