1 //===---------- private.h - Target independent OpenMP target RTL ----------===//
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
7 //===----------------------------------------------------------------------===//
9 // Private function declarations and helper macros for debugging output.
11 //===----------------------------------------------------------------------===//
13 #ifndef _OMPTARGET_PRIVATE_H
14 #define _OMPTARGET_PRIVATE_H
18 #include <SourceInfo.h>
19 #include <omptarget.h>
23 extern int targetDataBegin(ident_t
*loc
, DeviceTy
&Device
, int32_t arg_num
,
24 void **args_base
, void **args
, int64_t *arg_sizes
,
25 int64_t *arg_types
, map_var_info_t
*arg_names
,
26 void **arg_mappers
, AsyncInfoTy
&AsyncInfo
,
27 bool FromMapper
= false);
29 extern int targetDataEnd(ident_t
*loc
, DeviceTy
&Device
, int32_t ArgNum
,
30 void **ArgBases
, void **Args
, int64_t *ArgSizes
,
31 int64_t *ArgTypes
, map_var_info_t
*arg_names
,
32 void **ArgMappers
, AsyncInfoTy
&AsyncInfo
,
33 bool FromMapper
= false);
35 extern int targetDataUpdate(ident_t
*loc
, DeviceTy
&Device
, int32_t arg_num
,
36 void **args_base
, void **args
, int64_t *arg_sizes
,
37 int64_t *arg_types
, map_var_info_t
*arg_names
,
38 void **arg_mappers
, AsyncInfoTy
&AsyncInfo
,
39 bool FromMapper
= false);
41 extern int target(ident_t
*loc
, DeviceTy
&Device
, void *HostPtr
, int32_t ArgNum
,
42 void **ArgBases
, void **Args
, int64_t *ArgSizes
,
43 int64_t *ArgTypes
, map_var_info_t
*arg_names
,
44 void **ArgMappers
, int32_t TeamNum
, int32_t ThreadLimit
,
45 int IsTeamConstruct
, AsyncInfoTy
&AsyncInfo
);
47 extern void handleTargetOutcome(bool Success
, ident_t
*Loc
);
48 extern bool checkDeviceAndCtors(int64_t &DeviceID
, ident_t
*Loc
);
49 extern void *targetAllocExplicit(size_t size
, int device_num
, int kind
,
52 // This structure stores information of a mapped memory region.
53 struct MapComponentInfoTy
{
59 MapComponentInfoTy() = default;
60 MapComponentInfoTy(void *Base
, void *Begin
, int64_t Size
, int64_t Type
,
62 : Base(Base
), Begin(Begin
), Size(Size
), Type(Type
), Name(Name
) {}
65 // This structure stores all components of a user-defined mapper. The number of
66 // components are dynamically decided, so we utilize C++ STL vector
67 // implementation here.
68 struct MapperComponentsTy
{
69 std::vector
<MapComponentInfoTy
> Components
;
70 int32_t size() { return Components
.size(); }
73 // The mapper function pointer type. It follows the signature below:
74 // void .omp_mapper.<type_name>.<mapper_id>.(void *rt_mapper_handle,
75 // void *base, void *begin,
76 // size_t size, int64_t type,
78 typedef void (*MapperFuncPtrTy
)(void *, void *, void *, int64_t, int64_t,
81 // Function pointer type for targetData* functions (targetDataBegin,
82 // targetDataEnd and targetDataUpdate).
83 typedef int (*TargetDataFuncPtrTy
)(ident_t
*, DeviceTy
&, int32_t, void **,
84 void **, int64_t *, int64_t *,
85 map_var_info_t
*, void **, AsyncInfoTy
&,
88 // Implemented in libomp, they are called from within __tgt_* functions.
92 // functions that extract info from libomp; keep in sync
93 int omp_get_default_device(void) __attribute__((weak
));
94 int32_t __kmpc_global_thread_num(void *) __attribute__((weak
));
95 int __kmpc_get_target_offload(void) __attribute__((weak
));
100 #define TARGET_NAME Libomptarget
101 #define DEBUG_PREFIX GETNAME(TARGET_NAME)
103 ////////////////////////////////////////////////////////////////////////////////
104 /// dump a table of all the host-target pointer pairs on failure
105 static inline void dumpTargetPointerMappings(const ident_t
*Loc
,
107 if (Device
.HostDataToTargetMap
.empty())
110 SourceInfo
Kernel(Loc
);
111 INFO(OMP_INFOTYPE_ALL
, Device
.DeviceID
,
112 "OpenMP Host-Device pointer mappings after block at %s:%d:%d:\n",
113 Kernel
.getFilename(), Kernel
.getLine(), Kernel
.getColumn());
114 INFO(OMP_INFOTYPE_ALL
, Device
.DeviceID
, "%-18s %-18s %s %s %s %s\n",
115 "Host Ptr", "Target Ptr", "Size (B)", "DynRefCount", "HoldRefCount",
117 Device
.DataMapMtx
.lock();
118 for (const auto &HostTargetMap
: Device
.HostDataToTargetMap
) {
119 SourceInfo
Info(HostTargetMap
.HstPtrName
);
120 INFO(OMP_INFOTYPE_ALL
, Device
.DeviceID
,
121 DPxMOD
" " DPxMOD
" %-8" PRIuPTR
" %-11s %-12s %s at %s:%d:%d\n",
122 DPxPTR(HostTargetMap
.HstPtrBegin
), DPxPTR(HostTargetMap
.TgtPtrBegin
),
123 HostTargetMap
.HstPtrEnd
- HostTargetMap
.HstPtrBegin
,
124 HostTargetMap
.dynRefCountToStr().c_str(),
125 HostTargetMap
.holdRefCountToStr().c_str(), Info
.getName(),
126 Info
.getFilename(), Info
.getLine(), Info
.getColumn());
128 Device
.DataMapMtx
.unlock();
131 ////////////////////////////////////////////////////////////////////////////////
132 /// Print out the names and properties of the arguments to each kernel
134 printKernelArguments(const ident_t
*Loc
, const int64_t DeviceId
,
135 const int32_t ArgNum
, const int64_t *ArgSizes
,
136 const int64_t *ArgTypes
, const map_var_info_t
*ArgNames
,
137 const char *RegionType
) {
138 SourceInfo
info(Loc
);
139 INFO(OMP_INFOTYPE_ALL
, DeviceId
, "%s at %s:%d:%d with %d arguments:\n",
140 RegionType
, info
.getFilename(), info
.getLine(), info
.getColumn(),
143 for (int32_t i
= 0; i
< ArgNum
; ++i
) {
144 const map_var_info_t varName
= (ArgNames
) ? ArgNames
[i
] : nullptr;
145 const char *type
= nullptr;
146 const char *implicit
=
147 (ArgTypes
[i
] & OMP_TGT_MAPTYPE_IMPLICIT
) ? "(implicit)" : "";
148 if (ArgTypes
[i
] & OMP_TGT_MAPTYPE_TO
&& ArgTypes
[i
] & OMP_TGT_MAPTYPE_FROM
)
150 else if (ArgTypes
[i
] & OMP_TGT_MAPTYPE_TO
)
152 else if (ArgTypes
[i
] & OMP_TGT_MAPTYPE_FROM
)
154 else if (ArgTypes
[i
] & OMP_TGT_MAPTYPE_PRIVATE
)
156 else if (ArgTypes
[i
] & OMP_TGT_MAPTYPE_LITERAL
)
157 type
= "firstprivate";
158 else if (ArgSizes
[i
] != 0)
161 type
= "use_address";
163 INFO(OMP_INFOTYPE_ALL
, DeviceId
, "%s(%s)[%" PRId64
"] %s\n", type
,
164 getNameFromMapping(varName
).c_str(), ArgSizes
[i
], implicit
);
168 #ifdef OMPTARGET_PROFILE_ENABLED
169 #include "llvm/Support/TimeProfiler.h"
170 #define TIMESCOPE() llvm::TimeTraceScope TimeScope(__FUNCTION__)
171 #define TIMESCOPE_WITH_IDENT(IDENT) \
172 SourceInfo SI(IDENT); \
173 llvm::TimeTraceScope TimeScope(__FUNCTION__, SI.getProfileLocation())
174 #define TIMESCOPE_WITH_NAME_AND_IDENT(NAME, IDENT) \
175 SourceInfo SI(IDENT); \
176 llvm::TimeTraceScope TimeScope(NAME, SI.getProfileLocation())
179 #define TIMESCOPE_WITH_IDENT(IDENT)
180 #define TIMESCOPE_WITH_NAME_AND_IDENT(NAME, IDENT)