xref: /llvm-project/offload/include/Shared/APITypes.h (revision 6518b121f037717fd211c36659f7b25266424719)
1 //===-- Shared/APITypes.h - Offload and plugin API types --------*- C++ -*-===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file defines types used in the interface between the user code, the
10 // target independent offload runtime library, and target dependent plugins.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef OMPTARGET_SHARED_API_TYPES_H
15 #define OMPTARGET_SHARED_API_TYPES_H
16 
17 #include "Environment.h"
18 
19 #include "llvm/ADT/SmallVector.h"
20 #include "llvm/Frontend/Offloading/Utility.h"
21 
22 #include <cstddef>
23 #include <cstdint>
24 
25 extern "C" {
26 
27 /// This struct is a record of the device image information
28 struct __tgt_device_image {
29   void *ImageStart; // Pointer to the target code start
30   void *ImageEnd;   // Pointer to the target code end
31   llvm::offloading::EntryTy
32       *EntriesBegin; // Begin of table with all target entries
33   llvm::offloading::EntryTy *EntriesEnd; // End of table (non inclusive)
34 };
35 
36 struct __tgt_device_info {
37   void *Context = nullptr;
38   void *Device = nullptr;
39 };
40 
41 /// This struct is a record of all the host code that may be offloaded to a
42 /// target.
43 struct __tgt_bin_desc {
44   int32_t NumDeviceImages;          // Number of device types supported
45   __tgt_device_image *DeviceImages; // Array of device images (1 per dev. type)
46   llvm::offloading::EntryTy
47       *HostEntriesBegin; // Begin of table with all host entries
48   llvm::offloading::EntryTy *HostEntriesEnd; // End of table (non inclusive)
49 };
50 
51 /// This struct contains the offload entries identified by the target runtime
52 struct __tgt_target_table {
53   llvm::offloading::EntryTy
54       *EntriesBegin; // Begin of the table with all the entries
55   llvm::offloading::EntryTy
56       *EntriesEnd; // End of the table with all the entries (non inclusive)
57 };
58 
59 /// This struct contains a handle to a loaded binary in the plugin device.
60 struct __tgt_device_binary {
61   uintptr_t handle;
62 };
63 
64 // clang-format on
65 
66 /// This struct contains information exchanged between different asynchronous
67 /// operations for device-dependent optimization and potential synchronization
68 struct __tgt_async_info {
69   // A pointer to a queue-like structure where offloading operations are issued.
70   // We assume to use this structure to do synchronization. In CUDA backend, it
71   // is CUstream.
72   void *Queue = nullptr;
73 
74   /// A collection of allocations that are associated with this stream and that
75   /// should be freed after finalization.
76   llvm::SmallVector<void *, 2> AssociatedAllocations;
77 
78   /// The kernel launch environment used to issue a kernel. Stored here to
79   /// ensure it is a valid location while the transfer to the device is
80   /// happening.
81   KernelLaunchEnvironmentTy KernelLaunchEnvironment;
82 };
83 
84 /// This struct contains all of the arguments to a target kernel region launch.
85 struct KernelArgsTy {
86   uint32_t Version = 0; // Version of this struct for ABI compatibility.
87   uint32_t NumArgs = 0; // Number of arguments in each input pointer.
88   void **ArgBasePtrs =
89       nullptr;                 // Base pointer of each argument (e.g. a struct).
90   void **ArgPtrs = nullptr;    // Pointer to the argument data.
91   int64_t *ArgSizes = nullptr; // Size of the argument data in bytes.
92   int64_t *ArgTypes = nullptr; // Type of the data (e.g. to / from).
93   void **ArgNames = nullptr;   // Name of the data for debugging, possibly null.
94   void **ArgMappers = nullptr; // User-defined mappers, possibly null.
95   uint64_t Tripcount =
96       0; // Tripcount for the teams / distribute loop, 0 otherwise.
97   struct {
98     uint64_t NoWait : 1; // Was this kernel spawned with a `nowait` clause.
99     uint64_t IsCUDA : 1; // Was this kernel spawned via CUDA.
100     uint64_t Unused : 62;
101   } Flags = {0, 0, 0};
102   // The number of teams (for x,y,z dimension).
103   uint32_t NumTeams[3] = {0, 0, 0};
104   // The number of threads (for x,y,z dimension).
105   uint32_t ThreadLimit[3] = {0, 0, 0};
106   uint32_t DynCGroupMem = 0; // Amount of dynamic cgroup memory requested.
107 };
108 static_assert(sizeof(KernelArgsTy().Flags) == sizeof(uint64_t),
109               "Invalid struct size");
110 static_assert(sizeof(KernelArgsTy) ==
111                   (8 * sizeof(int32_t) + 3 * sizeof(int64_t) +
112                    4 * sizeof(void **) + 2 * sizeof(int64_t *)),
113               "Invalid struct size");
114 
115 /// Flat array of kernel launch parameters and their total size.
116 struct KernelLaunchParamsTy {
117   /// Size of the Data array.
118   size_t Size = 0;
119   /// Flat array of kernel parameters.
120   void *Data = nullptr;
121   /// Ptrs to the Data entries. Only strictly required for the host plugin.
122   void **Ptrs = nullptr;
123 };
124 }
125 
126 #endif // OMPTARGET_SHARED_API_TYPES_H
127