Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / compiler-rt / lib / orc / debug.cpp
blobaf20fa4e6f4e143f0a15962d386baa70d181a193
1 //===- debug.cpp ----------------------------------------------------------===//
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 is a part of the ORC runtime support library.
11 //===----------------------------------------------------------------------===//
13 #include "debug.h"
15 #include <cassert>
16 #include <cstdarg>
17 #include <cstdio>
18 #include <cstdlib>
19 #include <cstring>
22 namespace __orc_rt {
24 #ifndef NDEBUG
26 std::atomic<const char *> DebugTypes;
27 char DebugTypesAll;
28 char DebugTypesNone;
30 /// Sets the DebugState and DebugTypes values -- this function may be called
31 /// concurrently on multiple threads, but will always assign the same values so
32 /// this should be safe.
33 const char *initializeDebug() {
34 if (const char *DT = getenv("ORC_RT_DEBUG")) {
35 // If ORC_RT_DEBUG=1 then log everything.
36 if (strcmp(DT, "1") == 0) {
37 DebugTypes.store(&DebugTypesAll, std::memory_order_relaxed);
38 return &DebugTypesAll;
41 // If ORC_RT_DEBUG is non-empty then record the string for use in
42 // debugTypeEnabled.
43 if (strcmp(DT, "") != 0) {
44 DebugTypes.store(DT, std::memory_order_relaxed);
45 return DT;
49 // If ORT_RT_DEBUG is undefined or defined as empty then log nothing.
50 DebugTypes.store(&DebugTypesNone, std::memory_order_relaxed);
51 return &DebugTypesNone;
54 bool debugTypeEnabled(const char *Type, const char *Types) {
55 assert(Types && Types != &DebugTypesAll && Types != &DebugTypesNone &&
56 "Invalid Types value");
57 size_t TypeLen = strlen(Type);
58 const char *Start = Types;
59 const char *End = Start;
61 do {
62 if (*End == '\0' || *End == ',') {
63 size_t ItemLen = End - Start;
64 if (ItemLen == TypeLen && memcmp(Type, Start, TypeLen) == 0)
65 return true;
66 if (*End == '\0')
67 return false;
68 Start = End + 1;
70 ++End;
71 } while (true);
74 void printdbg(const char *format, ...) {
75 va_list Args;
76 va_start(Args, format);
77 vfprintf(stderr, format, Args);
78 va_end(Args);
81 #endif // !NDEBUG
83 } // end namespace __orc_rt