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