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.
11 //
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
14 //
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.
18 //
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));
21 //
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"
27 //
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);
35 //
36 //===----------------------------------------------------------------------===//
37 #ifndef _OMPTARGET_DEBUG_H
38 #define _OMPTARGET_DEBUG_H
39 
40 #include <atomic>
41 #include <mutex>
42 
43 /// 32-Bit field data attributes controlling information presented to the user.
44 enum OpenMPInfoType : uint32_t {
45   // Print data arguments and attributes upon entering an OpenMP device kernel.
46   OMP_INFOTYPE_KERNEL_ARGS = 0x0001,
47   // Indicate when an address already exists in the device mapping table.
48   OMP_INFOTYPE_MAPPING_EXISTS = 0x0002,
49   // Dump the contents of the device pointer map at kernel exit or failure.
50   OMP_INFOTYPE_DUMP_TABLE = 0x0004,
51   // Indicate when an address is added to the device mapping table.
52   OMP_INFOTYPE_MAPPING_CHANGED = 0x0008,
53   // Print kernel information from target device plugins.
54   OMP_INFOTYPE_PLUGIN_KERNEL = 0x0010,
55   // Enable every flag.
56   OMP_INFOTYPE_ALL = 0xffffffff,
57 };
58 
59 #define GCC_VERSION                                                            \
60   (__GNUC__ * 10000 + __GNUC_MINOR__ * 100 + __GNUC_PATCHLEVEL__)
61 
62 #if !defined(__clang__) && defined(__GNUC__) && GCC_VERSION < 70100
63 #define USED __attribute__((used))
64 #else
65 #define USED
66 #endif
67 
68 // Add __attribute__((used)) to work around a bug in gcc 5/6.
69 USED inline std::atomic<uint32_t> &getInfoLevelInternal() {
70   static std::atomic<uint32_t> InfoLevel;
71   static std::once_flag Flag{};
72   std::call_once(Flag, []() {
73     if (char *EnvStr = getenv("LIBOMPTARGET_INFO"))
74       InfoLevel.store(std::stoi(EnvStr));
75   });
76 
77   return InfoLevel;
78 }
79 
80 USED inline uint32_t getInfoLevel() { return getInfoLevelInternal().load(); }
81 
82 // Add __attribute__((used)) to work around a bug in gcc 5/6.
83 USED inline uint32_t getDebugLevel() {
84   static uint32_t DebugLevel = 0;
85   static std::once_flag Flag{};
86   std::call_once(Flag, []() {
87     if (char *EnvStr = getenv("LIBOMPTARGET_DEBUG"))
88       DebugLevel = std::stoi(EnvStr);
89   });
90 
91   return DebugLevel;
92 }
93 
94 #undef USED
95 #undef GCC_VERSION
96 
97 #ifndef __STDC_FORMAT_MACROS
98 #define __STDC_FORMAT_MACROS
99 #endif
100 #include <inttypes.h>
101 #undef __STDC_FORMAT_MACROS
102 
103 #define DPxMOD "0x%0*" PRIxPTR
104 #define DPxPTR(ptr) ((int)(2 * sizeof(uintptr_t))), ((uintptr_t)(ptr))
105 #define GETNAME2(name) #name
106 #define GETNAME(name) GETNAME2(name)
107 
108 /// Print a generic message string from libomptarget or a plugin RTL
109 #define MESSAGE0(_str)                                                         \
110   do {                                                                         \
111     fprintf(stderr, GETNAME(TARGET_NAME) " message: %s\n", _str);              \
112   } while (0)
113 
114 /// Print a printf formatting string message from libomptarget or a plugin RTL
115 #define MESSAGE(_str, ...)                                                     \
116   do {                                                                         \
117     fprintf(stderr, GETNAME(TARGET_NAME) " message: " _str "\n", __VA_ARGS__); \
118   } while (0)
119 
120 /// Print fatal error message with an error string and error identifier
121 #define FATAL_MESSAGE0(_num, _str)                                             \
122   do {                                                                         \
123     fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d: %s\n", _num, _str); \
124     abort();                                                                   \
125   } while (0)
126 
127 /// Print fatal error message with a printf string and error identifier
128 #define FATAL_MESSAGE(_num, _str, ...)                                         \
129   do {                                                                         \
130     fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d:" _str "\n", _num,   \
131             __VA_ARGS__);                                                      \
132     abort();                                                                   \
133   } while (0)
134 
135 /// Print a generic error string from libomptarget or a plugin RTL
136 #define FAILURE_MESSAGE(...)                                                   \
137   do {                                                                         \
138     fprintf(stderr, GETNAME(TARGET_NAME) " error: ");                          \
139     fprintf(stderr, __VA_ARGS__);                                              \
140   } while (0)
141 
142 /// Print a generic information string used if LIBOMPTARGET_INFO=1
143 #define INFO_MESSAGE(_num, ...)                                                \
144   do {                                                                         \
145     fprintf(stderr, GETNAME(TARGET_NAME) " device %d info: ", (int)_num);      \
146     fprintf(stderr, __VA_ARGS__);                                              \
147   } while (0)
148 
149 // Debugging messages
150 #ifdef OMPTARGET_DEBUG
151 #include <stdio.h>
152 
153 #define DEBUGP(prefix, ...)                                                    \
154   {                                                                            \
155     fprintf(stderr, "%s --> ", prefix);                                        \
156     fprintf(stderr, __VA_ARGS__);                                              \
157   }
158 
159 /// Emit a message for debugging
160 #define DP(...)                                                                \
161   do {                                                                         \
162     if (getDebugLevel() > 0) {                                                 \
163       DEBUGP(DEBUG_PREFIX, __VA_ARGS__);                                       \
164     }                                                                          \
165   } while (false)
166 
167 /// Emit a message for debugging or failure if debugging is disabled
168 #define REPORT(...)                                                            \
169   do {                                                                         \
170     if (getDebugLevel() > 0) {                                                 \
171       DP(__VA_ARGS__);                                                         \
172     } else {                                                                   \
173       FAILURE_MESSAGE(__VA_ARGS__);                                            \
174     }                                                                          \
175   } while (false)
176 #else
177 #define DEBUGP(prefix, ...)                                                    \
178   {}
179 #define DP(...)                                                                \
180   {}
181 #define REPORT(...) FAILURE_MESSAGE(__VA_ARGS__);
182 #endif // OMPTARGET_DEBUG
183 
184 /// Emit a message giving the user extra information about the runtime if
185 #define INFO(_flags, _id, ...)                                                 \
186   do {                                                                         \
187     if (getDebugLevel() > 0) {                                                 \
188       DEBUGP(DEBUG_PREFIX, __VA_ARGS__);                                       \
189     } else if (getInfoLevel() & _flags) {                                      \
190       INFO_MESSAGE(_id, __VA_ARGS__);                                          \
191     }                                                                          \
192   } while (false)
193 
194 #endif // _OMPTARGET_DEBUG_H
195