[Openmp-commits] [openmp] 277b681 - [OpenMP] Add function tracing debugging to device RTL

Joseph Huber via Openmp-commits openmp-commits at lists.llvm.org
Wed Sep 22 09:25:40 PDT 2021


Author: Joseph Huber
Date: 2021-09-22T12:25:29-04:00
New Revision: 277b681edec2ee4394c7e62a118ad21b2cbbb227

URL: https://github.com/llvm/llvm-project/commit/277b681edec2ee4394c7e62a118ad21b2cbbb227
DIFF: https://github.com/llvm/llvm-project/commit/277b681edec2ee4394c7e62a118ad21b2cbbb227.diff

LOG: [OpenMP] Add function tracing debugging to device RTL

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

Added: 
    

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

Removed: 
    


################################################################################
diff  --git a/openmp/libomptarget/DeviceRTL/include/Configuration.h b/openmp/libomptarget/DeviceRTL/include/Configuration.h
index 97e9449c3880..7e826940cf01 100644
--- 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 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

diff  --git a/openmp/libomptarget/DeviceRTL/include/Debug.h b/openmp/libomptarget/DeviceRTL/include/Debug.h
index 4e2a69937a9f..48ee3e9ef03a 100644
--- a/openmp/libomptarget/DeviceRTL/include/Debug.h
+++ b/openmp/libomptarget/DeviceRTL/include/Debug.h
@@ -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

diff  --git a/openmp/libomptarget/DeviceRTL/src/Configuration.cpp b/openmp/libomptarget/DeviceRTL/src/Configuration.cpp
index dc307071b21f..b725888be2da 100644
--- 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 @@ 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

diff  --git a/openmp/libomptarget/DeviceRTL/src/Debug.cpp b/openmp/libomptarget/DeviceRTL/src/Debug.cpp
index af78836ef0dd..6fa684339c7a 100644
--- 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 @@ 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


        


More information about the Openmp-commits mailing list