1 //===---------- private.h - Target independent OpenMP target RTL ----------===//
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 // Private function declarations and helper macros for debugging output.
10 //
11 //===----------------------------------------------------------------------===//
12
13 #ifndef _OMPTARGET_PRIVATE_H
14 #define _OMPTARGET_PRIVATE_H
15
16 #include <omptarget.h>
17 #include <Debug.h>
18
19 #include <cstdint>
20
21 extern int targetDataBegin(DeviceTy &Device, int32_t arg_num, void **args_base,
22 void **args, int64_t *arg_sizes, int64_t *arg_types,
23 map_var_info_t *arg_names, void **arg_mappers,
24 __tgt_async_info *async_info_ptr);
25
26 extern int targetDataEnd(DeviceTy &Device, int32_t ArgNum, void **ArgBases,
27 void **Args, int64_t *ArgSizes, int64_t *ArgTypes,
28 map_var_info_t *arg_names, void **ArgMappers,
29 __tgt_async_info *AsyncInfo);
30
31 extern int targetDataUpdate(DeviceTy &Device, int32_t arg_num, void **args_base,
32 void **args, int64_t *arg_sizes, int64_t *arg_types,
33 map_var_info_t *arg_names, void **arg_mappers,
34 __tgt_async_info *async_info_ptr = nullptr);
35
36 extern int target(int64_t DeviceId, void *HostPtr, int32_t ArgNum,
37 void **ArgBases, void **Args, int64_t *ArgSizes,
38 int64_t *ArgTypes, map_var_info_t *arg_names,
39 void **ArgMappers, int32_t TeamNum, int32_t ThreadLimit,
40 int IsTeamConstruct);
41
42 extern int CheckDeviceAndCtors(int64_t device_id);
43
44 // This structure stores information of a mapped memory region.
45 struct MapComponentInfoTy {
46 void *Base;
47 void *Begin;
48 int64_t Size;
49 int64_t Type;
50 MapComponentInfoTy() = default;
MapComponentInfoTyMapComponentInfoTy51 MapComponentInfoTy(void *Base, void *Begin, int64_t Size, int64_t Type)
52 : Base(Base), Begin(Begin), Size(Size), Type(Type) {}
53 };
54
55 // This structure stores all components of a user-defined mapper. The number of
56 // components are dynamically decided, so we utilize C++ STL vector
57 // implementation here.
58 struct MapperComponentsTy {
59 std::vector<MapComponentInfoTy> Components;
sizeMapperComponentsTy60 int32_t size() { return Components.size(); }
61 };
62
63 // The mapper function pointer type. It follows the signature below:
64 // void .omp_mapper.<type_name>.<mapper_id>.(void *rt_mapper_handle,
65 // void *base, void *begin,
66 // size_t size, int64_t type);
67 typedef void (*MapperFuncPtrTy)(void *, void *, void *, int64_t, int64_t);
68
69 // Function pointer type for target_data_* functions (targetDataBegin,
70 // targetDataEnd and targetDataUpdate).
71 typedef int (*TargetDataFuncPtrTy)(DeviceTy &, int32_t, void **, void **,
72 int64_t *, int64_t *, map_var_info_t *,
73 void **, __tgt_async_info *);
74
75 // Implemented in libomp, they are called from within __tgt_* functions.
76 #ifdef __cplusplus
77 extern "C" {
78 #endif
79 // functions that extract info from libomp; keep in sync
80 int omp_get_default_device(void) __attribute__((weak));
81 int32_t __kmpc_omp_taskwait(void *loc_ref, int32_t gtid) __attribute__((weak));
82 int32_t __kmpc_global_thread_num(void *) __attribute__((weak));
83 int __kmpc_get_target_offload(void) __attribute__((weak));
84 #ifdef __cplusplus
85 }
86 #endif
87
88 #define TARGET_NAME Libomptarget
89 #define DEBUG_PREFIX GETNAME(TARGET_NAME)
90
91 ////////////////////////////////////////////////////////////////////////////////
92 /// dump a table of all the host-target pointer pairs on failure
dumpTargetPointerMappings(const DeviceTy & Device)93 static inline void dumpTargetPointerMappings(const DeviceTy &Device) {
94 if (Device.HostDataToTargetMap.empty())
95 return;
96
97 fprintf(stderr, "Device %d Host-Device Pointer Mappings:\n", Device.DeviceID);
98 fprintf(stderr, "%-18s %-18s %s %s\n", "Host Ptr", "Target Ptr", "Size (B)",
99 "Declaration");
100 for (const auto &HostTargetMap : Device.HostDataToTargetMap) {
101 SourceInfo info(HostTargetMap.HstPtrName);
102 fprintf(stderr, DPxMOD " " DPxMOD " %-8lu %s at %s:%d:%d\n",
103 DPxPTR(HostTargetMap.HstPtrBegin),
104 DPxPTR(HostTargetMap.TgtPtrBegin),
105 HostTargetMap.HstPtrEnd - HostTargetMap.HstPtrBegin, info.getName(),
106 info.getFilename(), info.getLine(), info.getColumn());
107 }
108 }
109
110 #endif
111