1 //===-- omptargetplugin.h - Target dependent OpenMP Plugin API --*- 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 an interface between target independent OpenMP offload
10 // runtime library libomptarget and target dependent plugin.
11 //
12 //===----------------------------------------------------------------------===//
13 
14 #ifndef _OMPTARGETPLUGIN_H_
15 #define _OMPTARGETPLUGIN_H_
16 
17 #include <omptarget.h>
18 
19 #ifdef __cplusplus
20 extern "C" {
21 #endif
22 
23 // Return the number of available devices of the type supported by the
24 // target RTL.
25 int32_t __tgt_rtl_number_of_devices(void);
26 
27 // Return an integer different from zero if the provided device image can be
28 // supported by the runtime. The functionality is similar to comparing the
29 // result of __tgt__rtl__load__binary to NULL. However, this is meant to be a
30 // lightweight query to determine if the RTL is suitable for an image without
31 // having to load the library, which can be expensive.
32 int32_t __tgt_rtl_is_valid_binary(__tgt_device_image *Image);
33 
34 // Return an integer other than zero if the data can be exchaned from SrcDevId
35 // to DstDevId. If it is data exchangable, the device plugin should provide
36 // function to move data from source device to destination device directly.
37 int32_t __tgt_rtl_is_data_exchangable(int32_t SrcDevId, int32_t DstDevId);
38 
39 // Return an integer other than zero if the plugin can handle images which do
40 // not contain target regions and global variables (but can contain other
41 // functions)
42 int32_t __tgt_rtl_supports_empty_images();
43 
44 // Initialize the requires flags for the device.
45 int64_t __tgt_rtl_init_requires(int64_t RequiresFlags);
46 
47 // Initialize the specified device. In case of success return 0; otherwise
48 // return an error code.
49 int32_t __tgt_rtl_init_device(int32_t ID);
50 
51 // Pass an executable image section described by image to the specified
52 // device and prepare an address table of target entities. In case of error,
53 // return NULL. Otherwise, return a pointer to the built address table.
54 // Individual entries in the table may also be NULL, when the corresponding
55 // offload region is not supported on the target device.
56 __tgt_target_table *__tgt_rtl_load_binary(int32_t ID,
57                                           __tgt_device_image *Image);
58 
59 // Allocate data on the particular target device, of the specified size.
60 // HostPtr is a address of the host data the allocated target data
61 // will be associated with (HostPtr may be NULL if it is not known at
62 // allocation time, like for example it would be for target data that
63 // is allocated by omp_target_alloc() API). Return address of the
64 // allocated data on the target that will be used by libomptarget.so to
65 // initialize the target data mapping structures. These addresses are
66 // used to generate a table of target variables to pass to
67 // __tgt_rtl_run_region(). The __tgt_rtl_data_alloc() returns NULL in
68 // case an error occurred on the target device. Kind dictates what allocator
69 // to use (e.g. shared, host, device).
70 void *__tgt_rtl_data_alloc(int32_t ID, int64_t Size, void *HostPtr,
71                            int32_t Kind);
72 
73 // Pass the data content to the target device using the target address. In case
74 // of success, return zero. Otherwise, return an error code.
75 int32_t __tgt_rtl_data_submit(int32_t ID, void *TargetPtr, void *HostPtr,
76                               int64_t Size);
77 
78 int32_t __tgt_rtl_data_submit_async(int32_t ID, void *TargetPtr, void *HostPtr,
79                                     int64_t Size, __tgt_async_info *AsyncInfo);
80 
81 // Retrieve the data content from the target device using its address. In case
82 // of success, return zero. Otherwise, return an error code.
83 int32_t __tgt_rtl_data_retrieve(int32_t ID, void *HostPtr, void *TargetPtr,
84                                 int64_t Size);
85 
86 // Asynchronous version of __tgt_rtl_data_retrieve
87 int32_t __tgt_rtl_data_retrieve_async(int32_t ID, void *HostPtr,
88                                       void *TargetPtr, int64_t Size,
89                                       __tgt_async_info *AsyncInfo);
90 
91 // Copy the data content from one target device to another target device using
92 // its address. This operation does not need to copy data back to host and then
93 // from host to another device. In case of success, return zero. Otherwise,
94 // return an error code.
95 int32_t __tgt_rtl_data_exchange(int32_t SrcID, void *SrcPtr, int32_t DstID,
96                                 void *DstPtr, int64_t Size);
97 
98 // Asynchronous version of __tgt_rtl_data_exchange
99 int32_t __tgt_rtl_data_exchange_async(int32_t SrcID, void *SrcPtr,
100                                       int32_t DesID, void *DstPtr, int64_t Size,
101                                       __tgt_async_info *AsyncInfo);
102 
103 // De-allocate the data referenced by target ptr on the device. In case of
104 // success, return zero. Otherwise, return an error code.
105 int32_t __tgt_rtl_data_delete(int32_t ID, void *TargetPtr);
106 
107 // Transfer control to the offloaded entry Entry on the target device.
108 // Args and Offsets are arrays of NumArgs size of target addresses and
109 // offsets. An offset should be added to the target address before passing it
110 // to the outlined function on device side. If AsyncInfo is nullptr, it is
111 // synchronous; otherwise it is asynchronous. However, AsyncInfo may be
112 // ignored on some platforms, like x86_64. In that case, it is synchronous. In
113 // case of success, return zero. Otherwise, return an error code.
114 int32_t __tgt_rtl_run_target_region(int32_t ID, void *Entry, void **Args,
115                                     ptrdiff_t *Offsets, int32_t NumArgs);
116 
117 // Asynchronous version of __tgt_rtl_run_target_region
118 int32_t __tgt_rtl_run_target_region_async(int32_t ID, void *Entry, void **Args,
119                                           ptrdiff_t *Offsets, int32_t NumArgs,
120                                           __tgt_async_info *AsyncInfo);
121 
122 // Similar to __tgt_rtl_run_target_region, but additionally specify the
123 // number of teams to be created and a number of threads in each team. If
124 // AsyncInfo is nullptr, it is synchronous; otherwise it is asynchronous.
125 // However, AsyncInfo may be ignored on some platforms, like x86_64. In that
126 // case, it is synchronous.
127 int32_t __tgt_rtl_run_target_team_region(int32_t ID, void *Entry, void **Args,
128                                          ptrdiff_t *Offsets, int32_t NumArgs,
129                                          int32_t NumTeams, int32_t ThreadLimit,
130                                          uint64_t loop_tripcount);
131 
132 // Asynchronous version of __tgt_rtl_run_target_team_region
133 int32_t __tgt_rtl_run_target_team_region_async(
134     int32_t ID, void *Entry, void **Args, ptrdiff_t *Offsets, int32_t NumArgs,
135     int32_t NumTeams, int32_t ThreadLimit, uint64_t loop_tripcount,
136     __tgt_async_info *AsyncInfo);
137 
138 // Device synchronization. In case of success, return zero. Otherwise, return an
139 // error code.
140 int32_t __tgt_rtl_synchronize(int32_t ID, __tgt_async_info *AsyncInfo);
141 
142 // Set plugin's internal information flag externally.
143 void __tgt_rtl_set_info_flag(uint32_t);
144 
145 #ifdef __cplusplus
146 }
147 #endif
148 
149 #endif // _OMPTARGETPLUGIN_H_
150