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 begin declare target device_type(nohost)
22 
23 extern "C" {
__assert_assume(bool condition)24 void __assert_assume(bool condition) { __builtin_assume(condition); }
25 
__assert_fail(const char * assertion,const char * file,unsigned line,const char * function)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 namespace impl {
34 int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t);
35 }
36 
37 #pragma omp begin declare variant match(                                       \
38     device = {arch(nvptx, nvptx64)}, implementation = {extension(match_any)})
39 int32_t vprintf(const char *, void *);
40 namespace impl {
omp_vprintf(const char * Format,void * Arguments,uint32_t)41 int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t) {
42   return vprintf(Format, Arguments);
43 }
44 } // namespace impl
45 #pragma omp end declare variant
46 
47 // We do not have a vprintf implementation for AMD GPU yet so we use a stub.
48 #pragma omp begin declare variant match(device = {arch(amdgcn)})
49 namespace impl {
omp_vprintf(const char * Format,void * Arguments,uint32_t)50 int32_t omp_vprintf(const char *Format, void *Arguments, uint32_t) {
51   return -1;
52 }
53 } // namespace impl
54 #pragma omp end declare variant
55 
__llvm_omp_vprintf(const char * Format,void * Arguments,uint32_t Size)56 int32_t __llvm_omp_vprintf(const char *Format, void *Arguments, uint32_t Size) {
57   return impl::omp_vprintf(Format, Arguments, Size);
58 }
59 }
60 
61 /// Current indentation level for the function trace. Only accessed by thread 0.
62 __attribute__((loader_uninitialized)) static uint32_t Level;
63 #pragma omp allocate(Level) allocator(omp_pteam_mem_alloc)
64 
DebugEntryRAII(const char * File,const unsigned Line,const char * Function)65 DebugEntryRAII::DebugEntryRAII(const char *File, const unsigned Line,
66                                const char *Function) {
67   if (config::isDebugMode(config::DebugKind::FunctionTracing) &&
68       mapping::getThreadIdInBlock() == 0 && mapping::getBlockId() == 0) {
69 
70     for (int I = 0; I < Level; ++I)
71       PRINTF("%s", "  ");
72 
73     PRINTF("%s:%u: Thread %u Entering %s\n", File, Line,
74            mapping::getThreadIdInBlock(), Function);
75     Level++;
76   }
77 }
78 
~DebugEntryRAII()79 DebugEntryRAII::~DebugEntryRAII() {
80   if (config::isDebugMode(config::DebugKind::FunctionTracing) &&
81       mapping::getThreadIdInBlock() == 0 && mapping::getBlockId() == 0)
82     Level--;
83 }
84 
init()85 void DebugEntryRAII::init() { Level = 0; }
86 
87 #pragma omp end declare target
88