1 //===------- Debug.h - Target independent OpenMP target RTL -- C++ --------===//
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 // Routines used to provide debug messages and information from libomptarget
10 // and plugin RTLs to the user.
12 // Each plugin RTL and libomptarget define TARGET_NAME and DEBUG_PREFIX for use
13 // when sending messages to the user. These indicate which RTL sent the message
15 // Debug and information messages are controlled by the environment variables
16 // LIBOMPTARGET_DEBUG and LIBOMPTARGET_INFO which is set upon initialization
17 // of libomptarget or the plugin RTL.
19 // To printf a pointer in hex with a fixed width of 16 digits and a leading 0x,
20 // use printf("ptr=" DPxMOD "...\n", DPxPTR(ptr));
24 // where PRIxPTR expands to an appropriate modifier for the type uintptr_t on a
25 // specific platform, e.g. "lu" if uintptr_t is typedef'd as unsigned long:
28 // Ultimately, the whole statement expands to:
29 // printf("ptr=0x%0*lu...\n", // the 0* modifier expects an extra argument
30 // // specifying the width of the output
31 // (int)(2*sizeof(uintptr_t)), // the extra argument specifying the width
32 // // 8 digits for 32bit systems
33 // // 16 digits for 64bit
36 //===----------------------------------------------------------------------===//
37 #ifndef _OMPTARGET_DEBUG_H
38 #define _OMPTARGET_DEBUG_H
44 /// 32-Bit field data attributes controlling information presented to the user.
45 enum OpenMPInfoType
: uint32_t {
46 // Print data arguments and attributes upon entering an OpenMP device kernel.
47 OMP_INFOTYPE_KERNEL_ARGS
= 0x0001,
48 // Indicate when an address already exists in the device mapping table.
49 OMP_INFOTYPE_MAPPING_EXISTS
= 0x0002,
50 // Dump the contents of the device pointer map at kernel exit or failure.
51 OMP_INFOTYPE_DUMP_TABLE
= 0x0004,
52 // Indicate when an address is added to the device mapping table.
53 OMP_INFOTYPE_MAPPING_CHANGED
= 0x0008,
54 // Print kernel information from target device plugins.
55 OMP_INFOTYPE_PLUGIN_KERNEL
= 0x0010,
56 // Print whenever data is transferred to the device
57 OMP_INFOTYPE_DATA_TRANSFER
= 0x0020,
58 // Print whenever data does not have a viable device counterpart.
59 OMP_INFOTYPE_EMPTY_MAPPING
= 0x0040,
61 OMP_INFOTYPE_ALL
= 0xffffffff,
64 inline std::atomic
<uint32_t> &getInfoLevelInternal() {
65 static std::atomic
<uint32_t> InfoLevel
;
66 static std::once_flag Flag
{};
67 std::call_once(Flag
, []() {
68 if (char *EnvStr
= getenv("LIBOMPTARGET_INFO"))
69 InfoLevel
.store(std::stoi(EnvStr
));
75 inline uint32_t getInfoLevel() { return getInfoLevelInternal().load(); }
77 inline uint32_t getDebugLevel() {
78 static uint32_t DebugLevel
= 0;
79 static std::once_flag Flag
{};
80 std::call_once(Flag
, []() {
81 if (char *EnvStr
= getenv("LIBOMPTARGET_DEBUG"))
82 DebugLevel
= std::stoi(EnvStr
);
91 #ifndef __STDC_FORMAT_MACROS
92 #define __STDC_FORMAT_MACROS
95 #undef __STDC_FORMAT_MACROS
97 #define DPxMOD "0x%0*" PRIxPTR
98 #define DPxPTR(ptr) ((int)(2 * sizeof(uintptr_t))), ((uintptr_t)(ptr))
99 #define GETNAME2(name) #name
100 #define GETNAME(name) GETNAME2(name)
102 /// Print a generic message string from libomptarget or a plugin RTL
103 #define MESSAGE0(_str) \
105 fprintf(stderr, GETNAME(TARGET_NAME) " message: %s\n", _str); \
108 /// Print a printf formatting string message from libomptarget or a plugin RTL
109 #define MESSAGE(_str, ...) \
111 fprintf(stderr, GETNAME(TARGET_NAME) " message: " _str "\n", __VA_ARGS__); \
114 /// Print fatal error message with an error string and error identifier
115 #define FATAL_MESSAGE0(_num, _str) \
117 fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d: %s\n", _num, _str); \
121 /// Print fatal error message with a printf string and error identifier
122 #define FATAL_MESSAGE(_num, _str, ...) \
124 fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d: " _str "\n", _num, \
129 /// Print a generic error string from libomptarget or a plugin RTL
130 #define FAILURE_MESSAGE(...) \
132 fprintf(stderr, GETNAME(TARGET_NAME) " error: "); \
133 fprintf(stderr, __VA_ARGS__); \
136 /// Print a generic information string used if LIBOMPTARGET_INFO=1
137 #define INFO_MESSAGE(_num, ...) \
139 fprintf(stderr, GETNAME(TARGET_NAME) " device %d info: ", (int)_num); \
140 fprintf(stderr, __VA_ARGS__); \
143 // Debugging messages
144 #ifdef OMPTARGET_DEBUG
147 #define DEBUGP(prefix, ...) \
149 fprintf(stderr, "%s --> ", prefix); \
150 fprintf(stderr, __VA_ARGS__); \
153 /// Emit a message for debugging
156 if (getDebugLevel() > 0) { \
157 DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
161 /// Emit a message for debugging or failure if debugging is disabled
162 #define REPORT(...) \
164 if (getDebugLevel() > 0) { \
167 FAILURE_MESSAGE(__VA_ARGS__); \
171 #define DEBUGP(prefix, ...) \
175 #define REPORT(...) FAILURE_MESSAGE(__VA_ARGS__);
176 #endif // OMPTARGET_DEBUG
178 /// Emit a message giving the user extra information about the runtime if
179 #define INFO(_flags, _id, ...) \
181 if (getDebugLevel() > 0) { \
182 DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
183 } else if (getInfoLevel() & _flags) { \
184 INFO_MESSAGE(_id, __VA_ARGS__); \
188 #endif // _OMPTARGET_DEBUG_H