[OpenMP] Add function tracing debugging to device RTL
authorJoseph Huber <jhuber6@vols.utk.edu>
Tue, 21 Sep 2021 21:34:11 +0000 (17:34 -0400)
committerJoseph Huber <jhuber6@vols.utk.edu>
Wed, 22 Sep 2021 16:25:29 +0000 (12:25 -0400)
This patch adds support for an RAII struct that will print function
traces when placed inside of a function declaration. Each successive
call will increase the indentation to make it easier to visually
inspect.

Reviewed By: jdoerfert

Differential Revision: https://reviews.llvm.org/D110202

openmp/libomptarget/DeviceRTL/include/Configuration.h
openmp/libomptarget/DeviceRTL/include/Debug.h
openmp/libomptarget/DeviceRTL/src/Configuration.cpp
openmp/libomptarget/DeviceRTL/src/Debug.cpp

index 97e9449..7e82694 100644 (file)
 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 getNumDevices();
 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
index 4e2a699..48ee3e9 100644 (file)
@@ -29,4 +29,19 @@ void __assert_fail(const char *assertion, const char *file, unsigned line,
 #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
index dc30707..b725888 100644 (file)
@@ -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 @@ extern uint32_t __omp_rtl_debug_kind;
 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 @@ uint64_t config::getDynamicMemorySize() {
   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
index af78836..6fa6843 100644 (file)
@@ -12,6 +12,8 @@
 
 #include "Debug.h"
 #include "Configuration.h"
+#include "Mapping.h"
+#include "Types.h"
 
 using namespace _OMP;
 
@@ -19,7 +21,7 @@ using namespace _OMP;
 
 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 @@ void __assert_fail(const char *assertion, const char *file, unsigned line,
 }
 }
 
+/// 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