diff --git a/openmp/libomptarget/DeviceRTL/include/Configuration.h b/openmp/libomptarget/DeviceRTL/include/Configuration.h --- a/openmp/libomptarget/DeviceRTL/include/Configuration.h +++ b/openmp/libomptarget/DeviceRTL/include/Configuration.h @@ -18,7 +18,10 @@ namespace _OMP { namespace config { -enum DebugLevel : int32_t { Assertion }; +enum DebugKind : uint32_t { + Assertion = 1U << 0, + FunctionTracing = 1U << 1, +}; /// Return the number of devices in the system, same number as returned on the /// host by omp_get_num_devices. @@ -29,12 +32,12 @@ uint32_t getDeviceNum(); /// Return the user choosen debug level. -uint32_t getDebugLevel(); +uint32_t getDebugKind(); /// Return the amount of dynamic shared memory that was allocated at launch. uint64_t getDynamicMemorySize(); -bool isDebugMode(DebugLevel Level); +bool isDebugMode(DebugKind Level); } // namespace config } // namespace _OMP diff --git a/openmp/libomptarget/DeviceRTL/include/Debug.h b/openmp/libomptarget/DeviceRTL/include/Debug.h --- a/openmp/libomptarget/DeviceRTL/include/Debug.h +++ b/openmp/libomptarget/DeviceRTL/include/Debug.h @@ -29,4 +29,19 @@ #define PRINTF(fmt, ...) (void)fmt; #define PRINT(str) PRINTF("%s", str) +///} + +/// Enter a debugging scope for performing function traces. Enabled with +/// FunctionTracting set in the debug kind. +#define FunctionTracingRAII() \ + DebugEntryRAII Entry(__LINE__, __PRETTY_FUNCTION__); + +/// An RAII class for handling entries to debug locations. The current location +/// and function will be printed on entry. Nested levels increase the +/// indentation shown in the debugging output. +struct DebugEntryRAII { + DebugEntryRAII(const unsigned Line, const char *Function); + ~DebugEntryRAII(); +}; + #endif diff --git a/openmp/libomptarget/DeviceRTL/src/Configuration.cpp b/openmp/libomptarget/DeviceRTL/src/Configuration.cpp --- a/openmp/libomptarget/DeviceRTL/src/Configuration.cpp +++ b/openmp/libomptarget/DeviceRTL/src/Configuration.cpp @@ -18,7 +18,7 @@ using namespace _OMP; struct DeviceEnvironmentTy { - uint32_t DebugLevel; + uint32_t DebugKind; uint32_t NumDevices; uint32_t DeviceNum; uint64_t DynamicMemSize; @@ -32,8 +32,8 @@ DeviceEnvironmentTy CONSTANT(omptarget_device_environment) __attribute__((used)); -uint32_t config::getDebugLevel() { - return __omp_rtl_debug_kind & omptarget_device_environment.DebugLevel; +uint32_t config::getDebugKind() { + return __omp_rtl_debug_kind & omptarget_device_environment.DebugKind; } uint32_t config::getNumDevices() { @@ -48,8 +48,8 @@ return omptarget_device_environment.DynamicMemSize; } -bool config::isDebugMode(config::DebugLevel Level) { - return config::getDebugLevel() > Level; +bool config::isDebugMode(config::DebugKind Kind) { + return config::getDebugKind() & Kind; } #pragma omp end declare target diff --git a/openmp/libomptarget/DeviceRTL/src/Debug.cpp b/openmp/libomptarget/DeviceRTL/src/Debug.cpp --- a/openmp/libomptarget/DeviceRTL/src/Debug.cpp +++ b/openmp/libomptarget/DeviceRTL/src/Debug.cpp @@ -12,6 +12,8 @@ #include "Debug.h" #include "Configuration.h" +#include "Mapping.h" +#include "Types.h" using namespace _OMP; @@ -19,7 +21,7 @@ extern "C" { void __assert_assume(bool cond, const char *exp, const char *file, int line) { - if (!cond && config::isDebugMode(config::DebugLevel::Assertion)) { + if (!cond && config::isDebugMode(config::DebugKind::Assertion)) { PRINTF("ASSERTION failed: %s at %s, line %d\n", exp, file, line); __builtin_trap(); } @@ -35,4 +37,27 @@ } } +/// Current indentation level for the function trace. Only accessed by thread 0. +static uint32_t Level = 0; +#pragma omp allocate(Level) allocator(omp_pteam_mem_alloc) + +DebugEntryRAII::DebugEntryRAII(const unsigned Line, const char *Function) { + if (config::isDebugMode(config::DebugKind::FunctionTracing) && + mapping::getThreadIdInBlock() == 0) { + + for (int I = 0; I < Level; ++I) + PRINTF("%s", " "); + + PRINTF("Line %u: Thread %u Entering %s:%u\n", Line, + mapping::getThreadIdInBlock(), Function); + Level++; + } +} + +DebugEntryRAII::~DebugEntryRAII() { + if (config::isDebugMode(config::DebugKind::FunctionTracing) && + mapping::getThreadIdInBlock() == 0) + Level--; +} + #pragma omp end declare target