|
42 | 42 | #include <mutex> |
43 | 43 | #include <string> |
44 | 44 |
|
| 45 | +#include "llvm/Support/circular_raw_ostream.h" |
| 46 | + |
45 | 47 | /// 32-Bit field data attributes controlling information presented to the user. |
46 | 48 | enum OpenMPInfoType : uint32_t { |
47 | 49 | // Print data arguments and attributes upon entering an OpenMP device kernel. |
@@ -198,4 +200,190 @@ inline uint32_t getDebugLevel() { |
198 | 200 | } \ |
199 | 201 | } while (false) |
200 | 202 |
|
| 203 | +// New macros that will allow for more granular control over debugging output |
| 204 | +// Each message can be classified by Component, Type and Level |
| 205 | +// Component: The broad component of the offload runtime emitting the message. |
| 206 | +// Type: A cross-component classification of messages |
| 207 | +// Level: The verbosity level of the message |
| 208 | +// |
| 209 | +// The component is pulled from the TARGET_NAME macro, Type and Level can be |
| 210 | +// defined for each debug message but by default they are "default" and "1" |
| 211 | +// respectively. |
| 212 | +// |
| 213 | +// For liboffload and plugins, use OFFLOAD_DEBUG(...) |
| 214 | +// For libomptarget, use OPENMP_DEBUG(...) |
| 215 | +// Constructing messages should be done using C++ stream style syntax. |
| 216 | +// |
| 217 | +// Usage examples: |
| 218 | +// OFFLOAD_DEBUG("type1", 2, "This is a level 2 message of type1"); |
| 219 | +// OFFLOAD_DEBUG("Init", "This is a default level of the init type"); |
| 220 | +// OPENMP_DEBUG("This is a level 1 message of the default type"); |
| 221 | +// OFFLOAD_DEBUG("Init", 3, NumDevices << " were initialized\n"); |
| 222 | +// OFFLOAD_DEBUG("Kernel", "Starting kernel " << KernelName << " on device " << |
| 223 | +// DeviceId); |
| 224 | +// |
| 225 | +// Message output can be controlled by setting LIBOMPTARGET_DEBUG or |
| 226 | +// LIBOFFLOAD_DEBUG environment variables. Their syntax is as follows: |
| 227 | +// [integer]|all|<type1>[:<level1>][,<type2>[:<level2>],...] |
| 228 | +// |
| 229 | +// 0 : Disable all debug messages |
| 230 | +// all : Enable all debug messages |
| 231 | +// integer : Set the default level for all messages |
| 232 | +// <type> : Enable only messages of the specified type and level (more than one |
| 233 | +// can be specified). Components are also supported as |
| 234 | +// types. |
| 235 | +// <level> : Set the verbosity level for the specified type (default is 1) |
| 236 | +// |
| 237 | +// For very specific cases where more control is needed, use OFFLOAD_DEBUG_RAW |
| 238 | +// or OFFLOAD_DEBUG_BASE. See below for details. |
| 239 | + |
| 240 | +namespace llvm::offload::debug { |
| 241 | + |
| 242 | +#ifdef OMPTARGET_DEBUG |
| 243 | + |
| 244 | +struct DebugFilter { |
| 245 | + StringRef Type; |
| 246 | + uint32_t Level; |
| 247 | +}; |
| 248 | + |
| 249 | +struct DebugSettings { |
| 250 | + bool Enabled = false; |
| 251 | + uint32_t DefaultLevel = 1; |
| 252 | + llvm::SmallVector<DebugFilter> Filters; |
| 253 | +}; |
| 254 | + |
| 255 | +/// dbgs - Return a circular-buffered debug stream. |
| 256 | +inline llvm::raw_ostream &dbgs() { |
| 257 | + // Do one-time initialization in a thread-safe way. |
| 258 | + static struct dbgstream { |
| 259 | + llvm::circular_raw_ostream strm; |
| 260 | + |
| 261 | + dbgstream() : strm(llvm::errs(), "*** Debug Log Output ***\n", 0) {} |
| 262 | + } thestrm; |
| 263 | + |
| 264 | + return thestrm.strm; |
| 265 | +} |
| 266 | + |
| 267 | +inline DebugFilter parseDebugFilter(StringRef Filter) { |
| 268 | + size_t Pos = Filter.find(':'); |
| 269 | + if (Pos == StringRef::npos) |
| 270 | + return {Filter, 1}; |
| 271 | + |
| 272 | + StringRef Type = Filter.slice(0, Pos); |
| 273 | + uint32_t Level = 1; |
| 274 | + if (Filter.slice(Pos + 1, Filter.size()).getAsInteger(10, Level)) |
| 275 | + Level = 1; |
| 276 | + |
| 277 | + return {Type, Level}; |
| 278 | +} |
| 279 | + |
| 280 | +inline DebugSettings &getDebugSettings() { |
| 281 | + static DebugSettings Settings; |
| 282 | + static std::once_flag Flag{}; |
| 283 | + std::call_once(Flag, []() { |
| 284 | + printf("Configuring debug settings\n"); |
| 285 | + // Eventually, we probably should allow for the upper layers to set |
| 286 | + // the debug settings directly according to their own env var |
| 287 | + // (or other methods) settings. |
| 288 | + // For now mantain compatibility with existing libomptarget env var |
| 289 | + // and add a liboffload independent one. |
| 290 | + char *Env = getenv("LIBOMPTARGET_DEBUG"); |
| 291 | + if (!Env) { |
| 292 | + Env = getenv("LIBOFFLOAD_DEBUG"); |
| 293 | + if (!Env) |
| 294 | + return; |
| 295 | + } |
| 296 | + |
| 297 | + StringRef EnvRef(Env); |
| 298 | + if (EnvRef == "0") |
| 299 | + return; |
| 300 | + |
| 301 | + Settings.Enabled = true; |
| 302 | + if (EnvRef.equals_insensitive("all")) |
| 303 | + return; |
| 304 | + |
| 305 | + if (!EnvRef.getAsInteger(10, Settings.DefaultLevel)) |
| 306 | + return; |
| 307 | + |
| 308 | + Settings.DefaultLevel = 1; |
| 309 | + |
| 310 | + SmallVector<StringRef> DbgTypes; |
| 311 | + EnvRef.split(DbgTypes, ',', -1, false); |
| 312 | + |
| 313 | + for (auto &DT : DbgTypes) |
| 314 | + Settings.Filters.push_back(parseDebugFilter(DT)); |
| 315 | + }); |
| 316 | + |
| 317 | + return Settings; |
| 318 | +} |
| 319 | + |
| 320 | +inline bool isDebugEnabled() { |
| 321 | + return getDebugSettings().Enabled; |
| 322 | +} |
| 323 | + |
| 324 | +inline bool shouldPrintDebug(const char *Component, const char *Type, uint32_t Level) { |
| 325 | + const auto &Settings = getDebugSettings(); |
| 326 | + if (!Settings.Enabled) |
| 327 | + return false; |
| 328 | + |
| 329 | + if (Settings.Filters.empty()) |
| 330 | + return Level <= Settings.DefaultLevel; |
| 331 | + |
| 332 | + for (const auto &DT : Settings.Filters) { |
| 333 | + if (DT.Level < Level) |
| 334 | + continue; |
| 335 | + if (DT.Type.equals_insensitive(Type)) |
| 336 | + return true; |
| 337 | + if (DT.Type.equals_insensitive(Component)) |
| 338 | + return true; |
| 339 | + } |
| 340 | + |
| 341 | + return false; |
| 342 | +} |
| 343 | + |
| 344 | +#define OFFLOAD_DEBUG_BASE(Component, Type, Level, ...) \ |
| 345 | + do { \ |
| 346 | + if (llvm::offload::debug::isDebugEnabled() && \ |
| 347 | + llvm::offload::debug::shouldPrintDebug(Component, Type, Level)) \ |
| 348 | + __VA_ARGS__; \ |
| 349 | + } while (0) |
| 350 | + |
| 351 | +#define OFFLOAD_DEBUG_RAW(Type, Level, X) \ |
| 352 | + OFFLOAD_DEBUG_BASE(GETNAME(TARGET_NAME), Type, Level, X) |
| 353 | + |
| 354 | +#define OFFLOAD_DEBUG_1(X) \ |
| 355 | + OFFLOAD_DEBUG_BASE(GETNAME(TARGET_NAME), "default", 1, \ |
| 356 | + llvm::offload::debug::dbgs() \ |
| 357 | + << DEBUG_PREFIX << " --> " << X) |
| 358 | + |
| 359 | +#define OFFLOAD_DEBUG_2(Type, X) \ |
| 360 | + OFFLOAD_DEBUG_BASE(GETNAME(TARGET_NAME), Type, 1, \ |
| 361 | + llvm::offload::debug::dbgs() \ |
| 362 | + << DEBUG_PREFIX << " --> " << X) |
| 363 | + |
| 364 | +#define OFFLOAD_DEBUG_3(Type, Level, X) \ |
| 365 | + OFFLOAD_DEBUG_BASE(GETNAME(TARGET_NAME), Type, Level, \ |
| 366 | + llvm::offload::debug::dbgs() \ |
| 367 | + << DEBUG_PREFIX << " --> " << X) |
| 368 | + |
| 369 | +#define OFFLOAD_SELECT(Type, Level, X, NArgs, ...) OFFLOAD_DEBUG_##NArgs |
| 370 | + |
| 371 | +// To be used in liboffload and plugins |
| 372 | +#define OFFLOAD_DEBUG(...) OFFLOAD_SELECT(__VA_ARGS__, 3, 2, 1)(__VA_ARGS__) |
| 373 | + |
| 374 | +// To be used in libomptarget only |
| 375 | +#define OPENMP_DEBUG(...) OFFLOAD_DEBUG(__VA_ARGS__) |
| 376 | + |
| 377 | +#else |
| 378 | + |
| 379 | +// Don't print anything if debugging is disabled |
| 380 | +#define OFFLOAD_DEBUG_BASE(Component, Type, Level, ...) |
| 381 | +#define OFFLOAD_DEBUG_RAW(Type, Level, X) |
| 382 | +#define OFFLOAD_DEBUG(...) |
| 383 | +#define OPENMP_DEBUG(...) |
| 384 | + |
| 385 | +#endif |
| 386 | + |
| 387 | +} // namespace llvm::offload::debug |
| 388 | + |
201 | 389 | #endif // OMPTARGET_SHARED_DEBUG_H |
0 commit comments