1*fd3907ccSCallum Fare# Offload API definitions 2*fd3907ccSCallum Fare 3*fd3907ccSCallum Fare**Note**: This is a work-in-progress. It is loosely based on equivalent 4*fd3907ccSCallum Faretooling in Unified Runtime. 5*fd3907ccSCallum Fare 6*fd3907ccSCallum FareThe Tablegen files in this directory are used to define the Offload API. They 7*fd3907ccSCallum Fareare used with the `offload-tblgen` tool to generate API headers, print headers, 8*fd3907ccSCallum Fareand other implementation details. 9*fd3907ccSCallum Fare 10*fd3907ccSCallum FareThe root file is `OffloadAPI.td` - additional `.td` files can be included in 11*fd3907ccSCallum Farethis file to add them to the API. 12*fd3907ccSCallum Fare 13*fd3907ccSCallum Fare## API Objects 14*fd3907ccSCallum FareThe API consists of a number of objects, which always have a *name* field and 15*fd3907ccSCallum Fare*description* field, and are one of the following types: 16*fd3907ccSCallum Fare 17*fd3907ccSCallum Fare### Function 18*fd3907ccSCallum FareRepresents an API entry point function. Has a list of returns and parameters. 19*fd3907ccSCallum FareAlso has fields for details (representing a bullet-point list of 20*fd3907ccSCallum Fareinformation about the function that would otherwise be too detailed for the 21*fd3907ccSCallum Faredescription), and analogues (equivalent functions in other APIs). 22*fd3907ccSCallum Fare 23*fd3907ccSCallum Fare#### Parameter 24*fd3907ccSCallum FareRepresents a parameter to a function, has *type*, *name*, and *desc* fields. 25*fd3907ccSCallum FareAlso has a *flags* field containing flags representing whether the parameter is 26*fd3907ccSCallum Farein, out, or optional. 27*fd3907ccSCallum Fare 28*fd3907ccSCallum FareThe *type* field is used to infer if the parameter is a pointer or handle type. 29*fd3907ccSCallum FareA *handle* type is a pointer to an opaque struct, used to abstract over 30*fd3907ccSCallum Fareplugin-specific implementation details. 31*fd3907ccSCallum Fare 32*fd3907ccSCallum FareThere are two special variants of a *parameter*: 33*fd3907ccSCallum Fare* **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)` 34*fd3907ccSCallum Fare* **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. 35*fd3907ccSCallum Fare 36*fd3907ccSCallum Fare#### Return 37*fd3907ccSCallum FareA return represents a possible return code from the function, and optionally a 38*fd3907ccSCallum Farelist of conditions in which this value may be returned. The conditions list is 39*fd3907ccSCallum Farenot expected to be exhaustive. A condition is considered free-form text, but 40*fd3907ccSCallum Fareif it is wrapped in \`backticks\` then it is treated as literal code 41*fd3907ccSCallum Farerepresenting an error condition (e.g. `someParam < 1`). These conditions are 42*fd3907ccSCallum Fareused to automatically create validation checks by the `offload-tblgen` 43*fd3907ccSCallum Farevalidation generator. 44*fd3907ccSCallum Fare 45*fd3907ccSCallum FareReturns are automatically generated for functions with pointer or handle 46*fd3907ccSCallum Fareparameters, so API authors do not need to exhaustively add null checks for 47*fd3907ccSCallum Farethese types of parameters. All functions also get a number of default return 48*fd3907ccSCallum Farevalues automatically. 49*fd3907ccSCallum Fare 50*fd3907ccSCallum Fare 51*fd3907ccSCallum Fare### Struct 52*fd3907ccSCallum FareRepresents a struct. Contains a list of members, which each have a *type*, 53*fd3907ccSCallum Fare*name*, and *desc*. 54*fd3907ccSCallum Fare 55*fd3907ccSCallum FareAlso optionally takes a *base_class* field. If this is either of the special 56*fd3907ccSCallum Fare`offload_base_properties_t` or `offload_base_desc_t` structs, then the struct 57*fd3907ccSCallum Farewill inherit members from those structs. The generated struct does **not** use 58*fd3907ccSCallum Fareactual C++ inheritance, but instead explicitly has those members copied in, 59*fd3907ccSCallum Farewhich preserves ABI compatibility with C. 60*fd3907ccSCallum Fare 61*fd3907ccSCallum Fare### Enum 62*fd3907ccSCallum FareRepresents a C-style enum. Contains a list of `etor` values, which have a name 63*fd3907ccSCallum Fareand description. 64*fd3907ccSCallum Fare 65*fd3907ccSCallum FareA `TaggedEtor` record type also exists which addtionally takes a type. This type 66*fd3907ccSCallum Fareis used when the enum is used as a parameter to a function with a type-tagged 67*fd3907ccSCallum Farefunction parameter (e.g. `olGetDeviceInfo`). 68*fd3907ccSCallum Fare 69*fd3907ccSCallum FareAll enums automatically get a `<enum_name>_FORCE_UINT32 = 0x7fffffff` value, 70*fd3907ccSCallum Farewhich forces the underlying type to be uint32. 71*fd3907ccSCallum Fare 72*fd3907ccSCallum Fare### Handle 73*fd3907ccSCallum FareRepresents a pointer to an opaque struct, as described in the Parameter section. 74*fd3907ccSCallum FareIt does not take any extra fields. 75*fd3907ccSCallum Fare 76*fd3907ccSCallum Fare### Typedef 77*fd3907ccSCallum FareRepresents a typedef, contains only a *value* field. 78*fd3907ccSCallum Fare 79*fd3907ccSCallum Fare### Macro 80*fd3907ccSCallum FareRepresents a C preprocessor `#define`. Contains a *value* field. Optionally 81*fd3907ccSCallum Faretakes a *condition* field, which allows the macro to be conditionally defined, 82*fd3907ccSCallum Fareand an *alt_value* field, which represents the value if the condition is false. 83*fd3907ccSCallum Fare 84*fd3907ccSCallum FareMacro arguments are presented in the *name* field (e.g. name = `mymacro(arg)`). 85*fd3907ccSCallum Fare 86*fd3907ccSCallum FareWhile there may seem little point generating a macro from tablegen, doing this 87*fd3907ccSCallum Fareallows the entire source of the header file to be generated from the tablegen 88*fd3907ccSCallum Farefiles, rather than requiring a mix of C source and tablegen. 89*fd3907ccSCallum Fare 90*fd3907ccSCallum Fare## Generation 91*fd3907ccSCallum Fare 92*fd3907ccSCallum Fare### API header 93*fd3907ccSCallum Fare``` 94*fd3907ccSCallum Fare./offload-tblgen -I <path-to-llvm>/offload/API <path-to-llvm>/offload/API/OffloadAPI.td --gen-api 95*fd3907ccSCallum Fare``` 96*fd3907ccSCallum FareThe comments in the generated header are in Doxygen format, although 97*fd3907ccSCallum Faregenerating documentation from them hasn't been implemented yet. 98*fd3907ccSCallum Fare 99*fd3907ccSCallum FareThe 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. 100*fd3907ccSCallum Fare 101*fd3907ccSCallum Fare### Entry Points 102*fd3907ccSCallum Fare``` 103*fd3907ccSCallum Fare./offload-tblgen -I <path-to-llvm>/offload/API <path-to-llvm>/offload/API/OffloadAPI.td --gen-entry-points 104*fd3907ccSCallum Fare``` 105*fd3907ccSCallum FareThese functions form the actual Offload interface, and are wrappers over the 106*fd3907ccSCallum Farefunctions that contain the actual implementation (see 107*fd3907ccSCallum Fare'Adding a new entry point'). 108*fd3907ccSCallum Fare 109*fd3907ccSCallum FareThey implement automatically generated validation checks, and tracing of 110*fd3907ccSCallum Farefunction calls with arguments and results. The tracing can be enabled with the 111*fd3907ccSCallum Fare`OFFLOAD_TRACE` environment variable. 112*fd3907ccSCallum Fare 113*fd3907ccSCallum Fare### Implementation function declarations 114*fd3907ccSCallum Fare``` 115*fd3907ccSCallum Fare./offload-tblgen -I <path-to-llvm>/offload/API <path-to-llvm>/offload/API/OffloadAPI.td --gen-impl-func-decls 116*fd3907ccSCallum Fare``` 117*fd3907ccSCallum FareGenerates declarations of the implementation of functions of every entry point 118*fd3907ccSCallum Farein the API, e.g. `offloadDeviceFoo_impl` for `offloadDeviceFoo`. 119*fd3907ccSCallum Fare 120*fd3907ccSCallum Fare### Print header 121*fd3907ccSCallum Fare``` 122*fd3907ccSCallum Fare./offload-tblgen -I <path-to-llvm>/offload/API <path-to-llvm>/offload/API/OffloadAPI.td --gen-print-header 123*fd3907ccSCallum Fare``` 124*fd3907ccSCallum FareThis header contains `std::ostream &operator<<(std::ostream&)` definitions for 125*fd3907ccSCallum Farevarious API objects, including function parameters. 126*fd3907ccSCallum Fare 127*fd3907ccSCallum FareAs with the API header, it is expected that this header is part of the installed 128*fd3907ccSCallum Farepackage, so it is entirely generated by Tablegen. 129*fd3907ccSCallum Fare 130*fd3907ccSCallum FareFor ease of implementation, and since it is not strictly part of the API, this 131*fd3907ccSCallum Fareis a C++ header file. If a C version is desirable it could be added. 132*fd3907ccSCallum Fare 133*fd3907ccSCallum Fare### Future Tablegen backends 134*fd3907ccSCallum Fare`RecordTypes.hpp` contains wrappers for all of the API object types, which will 135*fd3907ccSCallum Fareallow more backends to be easily added in future. 136*fd3907ccSCallum Fare 137*fd3907ccSCallum Fare## Adding to the API 138*fd3907ccSCallum Fare 139*fd3907ccSCallum FareA new object can be added to the API by adding to one of the existing `.td` 140*fd3907ccSCallum Farefiles. It is also possible to add a new tablegen file to the API by adding it 141*fd3907ccSCallum Fareto the includes in `OffloadAPI.td`. When the offload target is rebuilt, the 142*fd3907ccSCallum Farenew definition will be included in the generated files. 143*fd3907ccSCallum Fare 144*fd3907ccSCallum Fare### Adding a new entry point 145*fd3907ccSCallum Fare 146*fd3907ccSCallum FareWhen a new entry point is added (e.g. `offloadDeviceFoo`), the actual entry 147*fd3907ccSCallum Farepoint is automatically generated, which contains validation and tracing code. 148*fd3907ccSCallum FareIt expects an implementation function (`offloadDeviceFoo_impl`) to be defined, 149*fd3907ccSCallum Farewhich it will call into. The definition of this implementation function should 150*fd3907ccSCallum Farebe added to `src/offload_impl.cpp` 151