Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / openmp / libomptarget / include / Debug.h
blob17aa8746afe7ce3cfefd1ecb1dea8d035e6ba1d7
1 //===------- Debug.h - Target independent OpenMP target RTL -- 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 // 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));
22 // DPxMOD expands to:
23 // "0x%0*" PRIxPTR
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:
26 // "0x%0*lu"
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
34 // (uintptr_t) ptr);
36 //===----------------------------------------------------------------------===//
37 #ifndef _OMPTARGET_DEBUG_H
38 #define _OMPTARGET_DEBUG_H
40 #include <atomic>
41 #include <mutex>
42 #include <string>
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,
60 // Enable every flag.
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));
70 });
72 return InfoLevel;
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);
83 });
85 return DebugLevel;
88 #undef USED
89 #undef GCC_VERSION
91 #ifndef __STDC_FORMAT_MACROS
92 #define __STDC_FORMAT_MACROS
93 #endif
94 #include <inttypes.h>
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) \
104 do { \
105 fprintf(stderr, GETNAME(TARGET_NAME) " message: %s\n", _str); \
106 } while (0)
108 /// Print a printf formatting string message from libomptarget or a plugin RTL
109 #define MESSAGE(_str, ...) \
110 do { \
111 fprintf(stderr, GETNAME(TARGET_NAME) " message: " _str "\n", __VA_ARGS__); \
112 } while (0)
114 /// Print fatal error message with an error string and error identifier
115 #define FATAL_MESSAGE0(_num, _str) \
116 do { \
117 fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d: %s\n", _num, _str); \
118 abort(); \
119 } while (0)
121 /// Print fatal error message with a printf string and error identifier
122 #define FATAL_MESSAGE(_num, _str, ...) \
123 do { \
124 fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d: " _str "\n", _num, \
125 __VA_ARGS__); \
126 abort(); \
127 } while (0)
129 /// Print a generic error string from libomptarget or a plugin RTL
130 #define FAILURE_MESSAGE(...) \
131 do { \
132 fprintf(stderr, GETNAME(TARGET_NAME) " error: "); \
133 fprintf(stderr, __VA_ARGS__); \
134 } while (0)
136 /// Print a generic information string used if LIBOMPTARGET_INFO=1
137 #define INFO_MESSAGE(_num, ...) \
138 do { \
139 fprintf(stderr, GETNAME(TARGET_NAME) " device %d info: ", (int)_num); \
140 fprintf(stderr, __VA_ARGS__); \
141 } while (0)
143 // Debugging messages
144 #ifdef OMPTARGET_DEBUG
145 #include <stdio.h>
147 #define DEBUGP(prefix, ...) \
149 fprintf(stderr, "%s --> ", prefix); \
150 fprintf(stderr, __VA_ARGS__); \
153 /// Emit a message for debugging
154 #define DP(...) \
155 do { \
156 if (getDebugLevel() > 0) { \
157 DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
159 } while (false)
161 /// Emit a message for debugging or failure if debugging is disabled
162 #define REPORT(...) \
163 do { \
164 if (getDebugLevel() > 0) { \
165 DP(__VA_ARGS__); \
166 } else { \
167 FAILURE_MESSAGE(__VA_ARGS__); \
169 } while (false)
170 #else
171 #define DEBUGP(prefix, ...) \
173 #define DP(...) \
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, ...) \
180 do { \
181 if (getDebugLevel() > 0) { \
182 DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
183 } else if (getInfoLevel() & _flags) { \
184 INFO_MESSAGE(_id, __VA_ARGS__); \
186 } while (false)
188 #endif // _OMPTARGET_DEBUG_H