1 //===--- Debug.cpp -------- Debug utilities ----------------------- 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 // This file contains debug utilities
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "Debug.h"
14 #include "Configuration.h"
15 #include "Interface.h"
16 #include "Mapping.h"
17 #include "Types.h"
18 
19 using namespace _OMP;
20 
21 #pragma omp declare target
22 
23 extern "C" {
24 void __assert_assume(bool condition) { __builtin_assume(condition); }
25 
26 void __assert_fail(const char *assertion, const char *file, unsigned line,
27                    const char *function) {
28   PRINTF("%s:%u: %s: Assertion `%s' failed.\n", file, line, function,
29          assertion);
30   __builtin_trap();
31 }
32 
33 #pragma omp begin declare variant match(                                       \
34     device = {arch(nvptx, nvptx64)}, implementation = {extension(match_any)})
35 int32_t vprintf(const char *, void *);
36 namespace impl {
37 static int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t) {
38   return vprintf(Format, Arguments);
39 }
40 } // namespace impl
41 #pragma omp end declare variant
42 
43 // We do not have a vprintf implementation for AMD GPU yet so we use a stub.
44 #pragma omp begin declare variant match(device = {arch(amdgcn)})
45 namespace impl {
46 static int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t) {
47   return -1;
48 }
49 } // namespace impl
50 #pragma omp end declare variant
51 
52 int32_t __llvm_omp_vprintf(const char *Format, void *Arguments, uint32_t Size) {
53   return impl::omp_vprintf(Format, Arguments, Size);
54 }
55 }
56 
57 /// Current indentation level for the function trace. Only accessed by thread 0.
58 static uint32_t Level = 0;
59 #pragma omp allocate(Level) allocator(omp_pteam_mem_alloc)
60 
61 DebugEntryRAII::DebugEntryRAII(const char *File, const unsigned Line,
62                                const char *Function) {
63   if (config::isDebugMode(config::DebugKind::FunctionTracing) &&
64       mapping::getThreadIdInBlock() == 0 && mapping::getBlockId() == 0) {
65 
66     for (int I = 0; I < Level; ++I)
67       PRINTF("%s", "  ");
68 
69     PRINTF("%s:%u: Thread %u Entering %s\n", File, Line,
70            mapping::getThreadIdInBlock(), Function);
71     Level++;
72   }
73 }
74 
75 DebugEntryRAII::~DebugEntryRAII() {
76   if (config::isDebugMode(config::DebugKind::FunctionTracing) &&
77       mapping::getThreadIdInBlock() == 0 && mapping::getBlockId() == 0)
78     Level--;
79 }
80 
81 #pragma omp end declare target
82