[sanitizer] Improve FreeBSD ASLR detection
[llvm-project.git] / openmp / libomptarget / deviceRTLs / common / debug.h
blob4ca1e5563fb36d0879ea22b17a0a603a2238eb69
1 //===------------- debug.h - NVPTX OpenMP debug macros ----------- CUDA -*-===//
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 // This file contains debug macros to be used in the application.
11 // Usage guide
13 // PRINT0(flag, str) : if debug flag is on, print (no arguments)
14 // PRINT(flag, str, args) : if debug flag is on, print (arguments)
15 // DON(flag) : return true if debug flag is on
17 // ASSERT(flag, cond, str, args): if test flag is on, test the condition
18 // if the condition is false, print str+args
19 // and assert.
20 // CAUTION: cond may be evaluate twice
21 // AON(flag) : return true if test flag is on
23 // WARNING(flag, str, args) : if warning flag is on, print the warning
24 // WON(flag) : return true if warning flag is on
26 //===----------------------------------------------------------------------===//
28 #ifndef _OMPTARGET_NVPTX_DEBUG_H_
29 #define _OMPTARGET_NVPTX_DEBUG_H_
31 #include "target_interface.h"
33 ////////////////////////////////////////////////////////////////////////////////
34 // set desired level of debugging
35 ////////////////////////////////////////////////////////////////////////////////
37 #define LD_SET_NONE 0ULL /* none */
38 #define LD_SET_ALL -1ULL /* all */
40 // pos 1
41 #define LD_SET_LOOP 0x1ULL /* basic loop */
42 #define LD_SET_LOOPD 0x2ULL /* basic loop */
43 #define LD_SET_PAR 0x4ULL /* basic parallel */
44 #define LD_SET_PARD 0x8ULL /* basic parallel */
46 // pos 2
47 #define LD_SET_SYNC 0x10ULL /* sync info */
48 #define LD_SET_SYNCD 0x20ULL /* sync info */
49 #define LD_SET_WAIT 0x40ULL /* state when waiting */
50 #define LD_SET_TASK 0x80ULL /* print task info (high level) */
52 // pos 3
53 #define LD_SET_IO 0x100ULL /* big region io (excl atomic) */
54 #define LD_SET_IOD 0x200ULL /* big region io (excl atomic) */
55 #define LD_SET_ENV 0x400ULL /* env info */
56 #define LD_SET_CANCEL 0x800ULL /* print cancel info */
58 // pos 4
59 #define LD_SET_MEM 0x1000ULL /* malloc / free */
61 ////////////////////////////////////////////////////////////////////////////////
62 // set the desired flags to print selected output.
64 // these are some examples of possible definitions that can be used for
65 // debugging.
66 //#define OMPTARGET_NVPTX_DEBUG (LD_SET_ALL)
67 //#define OMPTARGET_NVPTX_DEBUG (LD_SET_LOOP) // limit to loop printfs to save
68 // on cuda buffer
69 //#define OMPTARGET_NVPTX_DEBUG (LD_SET_IO)
70 //#define OMPTARGET_NVPTX_DEBUG (LD_SET_IO | LD_SET_ENV)
71 //#define OMPTARGET_NVPTX_DEBUG (LD_SET_PAR)
73 #ifndef OMPTARGET_NVPTX_DEBUG
74 #define OMPTARGET_NVPTX_DEBUG LD_SET_NONE
75 #elif OMPTARGET_NVPTX_DEBUG
76 #warning debug is used, not good for measurements
77 #endif
79 ////////////////////////////////////////////////////////////////////////////////
80 // set desired level of asserts
81 ////////////////////////////////////////////////////////////////////////////////
83 ////////////////////////////////////////////////////////////////////////////////
84 // available flags
86 #define LT_SET_NONE 0x0 /* unsafe */
87 #define LT_SET_SAFETY \
88 0x1 /* check malloc type of stuff, input at creation, cheap */
89 #define LT_SET_INPUT 0x2 /* check also all runtime inputs */
90 #define LT_SET_FUSSY 0x4 /* fussy checks, expensive */
92 ////////////////////////////////////////////////////////////////////////////////
93 // set the desired flags
95 #ifndef OMPTARGET_NVPTX_TEST
96 #if OMPTARGET_NVPTX_DEBUG
97 #define OMPTARGET_NVPTX_TEST (LT_SET_FUSSY)
98 #else
99 #define OMPTARGET_NVPTX_TEST (LT_SET_SAFETY)
100 #endif
101 #endif
103 ////////////////////////////////////////////////////////////////////////////////
104 // set desired level of warnings
105 ////////////////////////////////////////////////////////////////////////////////
107 ////////////////////////////////////////////////////////////////////////////////
108 // available flags
110 #define LW_SET_ALL -1
111 #define LW_SET_NONE 0x0
112 #define LW_SET_ENV 0x1
113 #define LW_SET_INPUT 0x2
114 #define LW_SET_FUSSY 0x4
116 ////////////////////////////////////////////////////////////////////////////////
117 // set the desired flags
119 #if OMPTARGET_NVPTX_DEBUG
120 #define OMPTARGET_NVPTX_WARNING (LW_SET_NONE)
121 #else
122 #define OMPTARGET_NVPTX_WARNING (LW_SET_FUSSY)
123 #endif
125 ////////////////////////////////////////////////////////////////////////////////
126 // implementation for debug
127 ////////////////////////////////////////////////////////////////////////////////
129 #if OMPTARGET_NVPTX_DEBUG || OMPTARGET_NVPTX_TEST || OMPTARGET_NVPTX_WARNING
130 #include "common/support.h"
132 template <typename... Arguments>
133 NOINLINE static void log(const char *fmt, Arguments... parameters) {
134 printf(fmt, (int)GetBlockIdInKernel(),
135 (int)__kmpc_get_hardware_thread_id_in_block(), (int)GetWarpId(),
136 (int)GetLaneId(), parameters...);
139 #endif
140 #if OMPTARGET_NVPTX_TEST
142 template <typename... Arguments>
143 NOINLINE static void check(bool cond, const char *fmt,
144 Arguments... parameters) {
145 if (!cond) {
146 printf(fmt, (int)GetBlockIdInKernel(),
147 (int)__kmpc_get_hardware_thread_id_in_block(), (int)GetWarpId(),
148 (int)GetLaneId(), parameters...);
149 __builtin_trap();
153 NOINLINE static void check(bool cond) {
154 if (!cond)
155 __builtin_trap();
157 #endif
159 // set flags that are tested (inclusion properties)
161 #define LD_ALL (LD_SET_ALL)
163 #define LD_LOOP (LD_SET_LOOP | LD_SET_LOOPD)
164 #define LD_LOOPD (LD_SET_LOOPD)
165 #define LD_PAR (LD_SET_PAR | LD_SET_PARD)
166 #define LD_PARD (LD_SET_PARD)
168 // pos 2
169 #define LD_SYNC (LD_SET_SYNC | LD_SET_SYNCD)
170 #define LD_SYNCD (LD_SET_SYNCD)
171 #define LD_WAIT (LD_SET_WAIT)
172 #define LD_TASK (LD_SET_TASK)
174 // pos 3
175 #define LD_IO (LD_SET_IO | LD_SET_IOD)
176 #define LD_IOD (LD_SET_IOD)
177 #define LD_ENV (LD_SET_ENV)
178 #define LD_CANCEL (LD_SET_CANCEL)
180 // pos 3
181 #define LD_MEM (LD_SET_MEM)
183 // implement
184 #if OMPTARGET_NVPTX_DEBUG
186 #define DON(_flag) ((unsigned)(OMPTARGET_NVPTX_DEBUG) & (_flag))
188 #define PRINT0(_flag, _str) \
190 if (omptarget_device_environment.debug_level && DON(_flag)) { \
191 log("<b %2d, t %4d, w %2d, l %2d>: " _str); \
195 #define PRINT(_flag, _str, _args...) \
197 if (omptarget_device_environment.debug_level && DON(_flag)) { \
198 log("<b %2d, t %4d, w %2d, l %2d>: " _str, _args); \
201 #else
203 #define DON(_flag) (0)
204 #define PRINT0(flag, str)
205 #define PRINT(flag, str, _args...)
207 #endif
209 // for printing without worrying about precision, pointers...
210 #define P64(_x) ((unsigned long long)(_x))
212 ////////////////////////////////////////////////////////////////////////////////
213 // early defs for test
214 ////////////////////////////////////////////////////////////////////////////////
216 #define LT_SAFETY (LT_SET_SAFETY | LT_SET_INPUT | LT_SET_FUSSY)
217 #define LT_INPUT (LT_SET_INPUT | LT_SET_FUSSY)
218 #define LT_FUSSY (LT_SET_FUSSY)
220 #if OMPTARGET_NVPTX_TEST == LT_SET_SAFETY
222 #define TON(_flag) ((OMPTARGET_NVPTX_TEST) & (_flag))
223 #define ASSERT0(_flag, _cond, _str) \
225 if (TON(_flag)) { \
226 check(_cond); \
229 #define ASSERT(_flag, _cond, _str, _args...) \
231 if (TON(_flag)) { \
232 check(_cond); \
236 #elif OMPTARGET_NVPTX_TEST >= LT_SET_INPUT
238 #define TON(_flag) ((OMPTARGET_NVPTX_TEST) & (_flag))
239 #define ASSERT0(_flag, _cond, _str) \
241 if (TON(_flag)) { \
242 check((_cond), "<b %3d, t %4d, w %2d, l %2d> ASSERT: " _str "\n"); \
245 #define ASSERT(_flag, _cond, _str, _args...) \
247 if (TON(_flag)) { \
248 check((_cond), "<b %3d, t %4d, w %2d, l %d2> ASSERT: " _str "\n", \
249 _args); \
253 #else
255 #define TON(_flag) (0)
256 #define ASSERT0(_flag, _cond, _str)
257 #define ASSERT(_flag, _cond, _str, _args...)
259 #endif
261 ////////////////////////////////////////////////////////////////////////////////
262 // early defs for warning
264 #define LW_ALL (LW_SET_ALL)
265 #define LW_ENV (LW_SET_FUSSY | LW_SET_INPUT | LW_SET_ENV)
266 #define LW_INPUT (LW_SET_FUSSY | LW_SET_INPUT)
267 #define LW_FUSSY (LW_SET_FUSSY)
269 #if OMPTARGET_NVPTX_WARNING
271 #define WON(_flag) ((OMPTARGET_NVPTX_WARNING) & (_flag))
272 #define WARNING0(_flag, _str) \
274 if (WON(_flag)) { \
275 log("<b %2d, t %4d, w %2d, l %2d> WARNING: " _str); \
278 #define WARNING(_flag, _str, _args...) \
280 if (WON(_flag)) { \
281 log("<b %2d, t %4d, w %2d, l %2d> WARNING: " _str, _args); \
285 #else
287 #define WON(_flag) (0)
288 #define WARNING0(_flag, _str)
289 #define WARNING(_flag, _str, _args...)
291 #endif
293 #endif