[Openmp-commits] [openmp] ae95cee - [OpenMP] Consolidate error handling and debug messages in Libomptarget
via Openmp-commits
openmp-commits at lists.llvm.org
Tue Sep 1 12:28:52 PDT 2020
Author: Joseph Huber
Date: 2020-09-01T15:28:19-04:00
New Revision: ae95ceeb8f98d81f615c69da02f73b5ee6b1519a
URL: https://github.com/llvm/llvm-project/commit/ae95ceeb8f98d81f615c69da02f73b5ee6b1519a
DIFF: https://github.com/llvm/llvm-project/commit/ae95ceeb8f98d81f615c69da02f73b5ee6b1519a.diff
LOG: [OpenMP] Consolidate error handling and debug messages in Libomptarget
Summary:
This patch consolidates the error handling and messaging routines to a single
file omptargetmessage. The goal is to simplify the error handling interface
prior to adding more error handling support
Reviewers: jdoerfert grokos ABataev AndreyChurbanov ronlieb JonChesterfield ye-luo tianshilei1992
Subscribers: danielkiss guansong jvesely kerbowa nhaehnle openmp-commits sstefan1 yaxunl
Added:
openmp/libomptarget/include/Debug.h
Modified:
openmp/libomptarget/include/omptarget.h
openmp/libomptarget/plugins/amdgpu/src/rtl.cpp
openmp/libomptarget/plugins/common/elf_common.c
openmp/libomptarget/plugins/cuda/src/rtl.cpp
openmp/libomptarget/plugins/generic-elf-64bit/src/rtl.cpp
openmp/libomptarget/plugins/ve/src/rtl.cpp
openmp/libomptarget/src/api.cpp
openmp/libomptarget/src/interface.cpp
openmp/libomptarget/src/omptarget.cpp
openmp/libomptarget/src/private.h
openmp/libomptarget/src/rtl.cpp
Removed:
################################################################################
diff --git a/openmp/libomptarget/include/Debug.h b/openmp/libomptarget/include/Debug.h
new file mode 100644
index 000000000000..b7092dd61a3d
--- /dev/null
+++ b/openmp/libomptarget/include/Debug.h
@@ -0,0 +1,136 @@
+//===------- Debug.h - Target independent OpenMP target RTL -- C++ --------===//
+//
+// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
+// See https://llvm.org/LICENSE.txt for license information.
+// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
+//
+//===----------------------------------------------------------------------===//
+//
+// Routines used to provide debug messages and information from libomptarget
+// and plugin RTLs to the user.
+//
+// Each plugin RTL and libomptarget define TARGET_NAME and DEBUG_PREFIX for use
+// when sending messages to the user. These indicate which RTL sent the message
+//
+// Debug and information messages are controlled by the environment variables
+// LIBOMPTARGET_DEBUG and LIBOMPTARGET_INFO which is set upon initialization
+// of libomptarget or the plugin RTL.
+//
+// To printf a pointer in hex with a fixed width of 16 digits and a leading 0x,
+// use printf("ptr=" DPxMOD "...\n", DPxPTR(ptr));
+//
+// DPxMOD expands to:
+// "0x%0*" PRIxPTR
+// where PRIxPTR expands to an appropriate modifier for the type uintptr_t on a
+// specific platform, e.g. "lu" if uintptr_t is typedef'd as unsigned long:
+// "0x%0*lu"
+//
+// Ultimately, the whole statement expands to:
+// printf("ptr=0x%0*lu...\n", // the 0* modifier expects an extra argument
+// // specifying the width of the output
+// (int)(2*sizeof(uintptr_t)), // the extra argument specifying the width
+// // 8 digits for 32bit systems
+// // 16 digits for 64bit
+// (uintptr_t) ptr);
+//
+//===----------------------------------------------------------------------===//
+#ifndef _OMPTARGET_DEBUG_H
+#define _OMPTARGET_DEBUG_H
+
+static inline int getInfoLevel() {
+ static int InfoLevel = -1;
+ if (InfoLevel >= 0)
+ return InfoLevel;
+
+ if (char *EnvStr = getenv("LIBOMPTARGET_INFO"))
+ InfoLevel = std::stoi(EnvStr);
+
+ return InfoLevel;
+}
+
+static inline int getDebugLevel() {
+ static int DebugLevel = -1;
+ if (DebugLevel >= 0)
+ return DebugLevel;
+
+ if (char *EnvStr = getenv("LIBOMPTARGET_DEBUG"))
+ DebugLevel = std::stoi(EnvStr);
+
+ return DebugLevel;
+}
+
+#ifndef __STDC_FORMAT_MACROS
+#define __STDC_FORMAT_MACROS
+#endif
+#include <inttypes.h>
+#undef __STDC_FORMAT_MACROS
+
+#define DPxMOD "0x%0*" PRIxPTR
+#define DPxPTR(ptr) ((int)(2 * sizeof(uintptr_t))), ((uintptr_t)(ptr))
+#define GETNAME2(name) #name
+#define GETNAME(name) GETNAME2(name)
+
+// Messaging interface
+#define MESSAGE0(_str) \
+ do { \
+ fprintf(stderr, GETNAME(TARGET_NAME) " message: %s\n", _str); \
+ } while (0)
+
+#define MESSAGE(_str, ...) \
+ do { \
+ fprintf(stderr, GETNAME(TARGET_NAME) " message: " _str "\n", __VA_ARGS__); \
+ } while (0)
+
+#define FATAL_MESSAGE0(_num, _str) \
+ do { \
+ fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d: %s\n", _num, _str); \
+ abort(); \
+ } while (0)
+
+#define FATAL_MESSAGE(_num, _str, ...) \
+ do { \
+ fprintf(stderr, GETNAME(TARGET_NAME) " fatal error %d:" _str "\n", _num, \
+ __VA_ARGS__); \
+ abort(); \
+ } while (0)
+
+#define FAILURE_MESSAGE(...) \
+ do { \
+ fprintf(stderr, GETNAME(TARGET_NAME) " error: "); \
+ fprintf(stderr, __VA_ARGS__); \
+ } while (0)
+
+// Debugging messages
+#ifdef OMPTARGET_DEBUG
+#include <stdio.h>
+
+#define DEBUGP(prefix, ...) \
+ { \
+ fprintf(stderr, "%s --> ", prefix); \
+ fprintf(stderr, __VA_ARGS__); \
+ }
+
+#define DP(...) \
+ do { \
+ if (getDebugLevel() > 0) { \
+ DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
+ } \
+ } while (false)
+
+#define REPORT(...) \
+ do { \
+ if (getDebugLevel() > 0) { \
+ DP(__VA_ARGS__); \
+ } else { \
+ FAILURE_MESSAGE(__VA_ARGS__); \
+ } \
+ } while (false)
+#else
+#define DEBUGP(prefix, ...) \
+ {}
+#define DP(...) \
+ {}
+#define REPORT(...) FAILURE_MESSAGE(__VA_ARGS__);
+#endif // OMPTARGET_DEBUG
+
+#endif // _OMPTARGET_DEBUG_H
diff --git a/openmp/libomptarget/include/omptarget.h b/openmp/libomptarget/include/omptarget.h
index 0751816cd9d9..11d112159dc7 100644
--- a/openmp/libomptarget/include/omptarget.h
+++ b/openmp/libomptarget/include/omptarget.h
@@ -261,45 +261,6 @@ void __kmpc_push_target_tripcount(int64_t device_id, uint64_t loop_tripcount);
}
#endif
-#ifndef __STDC_FORMAT_MACROS
-#define __STDC_FORMAT_MACROS
-#endif
-
-#include <inttypes.h>
-#define DPxMOD "0x%0*" PRIxPTR
-#define DPxPTR(ptr) ((int)(2*sizeof(uintptr_t))), ((uintptr_t) (ptr))
-
-/*
- * To printf a pointer in hex with a fixed width of 16 digits and a leading 0x,
- * use printf("ptr=" DPxMOD "...\n", DPxPTR(ptr));
- *
- * DPxMOD expands to:
- * "0x%0*" PRIxPTR
- * where PRIxPTR expands to an appropriate modifier for the type uintptr_t on a
- * specific platform, e.g. "lu" if uintptr_t is typedef'd as unsigned long:
- * "0x%0*lu"
- *
- * Ultimately, the whole statement expands to:
- * printf("ptr=0x%0*lu...\n", // the 0* modifier expects an extra argument
- * // specifying the width of the output
- * (int)(2*sizeof(uintptr_t)), // the extra argument specifying the width
- * // 8 digits for 32bit systems
- * // 16 digits for 64bit
- * (uintptr_t) ptr);
- */
-
-#ifdef OMPTARGET_DEBUG
-#include <stdio.h>
-#define DEBUGP(prefix, ...) \
- { \
- fprintf(stderr, "%s --> ", prefix); \
- fprintf(stderr, __VA_ARGS__); \
- }
-#else
-#define DEBUGP(prefix, ...) \
- {}
-#endif
-
#ifdef __cplusplus
#define EXTERN extern "C"
#else
diff --git a/openmp/libomptarget/plugins/amdgpu/src/rtl.cpp b/openmp/libomptarget/plugins/amdgpu/src/rtl.cpp
index 0ec4c2ac918a..9ba27560d140 100644
--- a/openmp/libomptarget/plugins/amdgpu/src/rtl.cpp
+++ b/openmp/libomptarget/plugins/amdgpu/src/rtl.cpp
@@ -35,6 +35,7 @@
#include "internal.h"
+#include "Debug.h"
#include "omptargetplugin.h"
// Get static gpu grid values from clang target-specific constants managed
@@ -101,28 +102,13 @@ static constexpr unsigned AMDGPUGpuGridValues[] = {
#ifndef TARGET_NAME
#define TARGET_NAME AMDHSA
#endif
+#define DEBUG_PREFIX "Target " GETNAME(TARGET_NAME) " RTL"
int print_kernel_trace;
// Size of the target call stack struture
uint32_t TgtStackItemSize = 0;
-#ifdef OMPTARGET_DEBUG
-static int DebugLevel = 0;
-
-#define GETNAME2(name) #name
-#define GETNAME(name) GETNAME2(name)
-#define DP(...) \
- do { \
- if (DebugLevel > 0) { \
- DEBUGP("Target " GETNAME(TARGET_NAME) " RTL", __VA_ARGS__); \
- } \
- } while (false)
-#else // OMPTARGET_DEBUG
-#define DP(...) \
- {}
-#endif // OMPTARGET_DEBUG
-
#undef check // Drop definition from internal.h
#ifdef OMPTARGET_DEBUG
#define check(msg, status) \
@@ -476,11 +462,6 @@ class RTLDeviceInfoTy {
}
RTLDeviceInfoTy() {
-#ifdef OMPTARGET_DEBUG
- if (char *envStr = getenv("LIBOMPTARGET_DEBUG"))
- DebugLevel = std::stoi(envStr);
-#endif // OMPTARGET_DEBUG
-
// LIBOMPTARGET_KERNEL_TRACE provides a kernel launch trace to stderr
// anytime. You do not need a debug library build.
// 0 => no tracing
diff --git a/openmp/libomptarget/plugins/common/elf_common.c b/openmp/libomptarget/plugins/common/elf_common.c
index 8cbf8aadca28..ba63a5fd9f2b 100644
--- a/openmp/libomptarget/plugins/common/elf_common.c
+++ b/openmp/libomptarget/plugins/common/elf_common.c
@@ -13,9 +13,9 @@
//
//===----------------------------------------------------------------------===//
-#if !(defined(_OMPTARGET_H_) && defined(DP))
-#error Include elf_common.c in the plugin source AFTER omptarget.h has been\
- included and macro DP(...) has been defined.
+#if !(defined(_OMPTARGET_DEBUG_H))
+#error Include elf_common.c in the plugin source AFTER Debug.h has\
+ been included.
#endif
#include <elf.h>
diff --git a/openmp/libomptarget/plugins/cuda/src/rtl.cpp b/openmp/libomptarget/plugins/cuda/src/rtl.cpp
index a0060dbb22b8..2675f83ae28f 100644
--- a/openmp/libomptarget/plugins/cuda/src/rtl.cpp
+++ b/openmp/libomptarget/plugins/cuda/src/rtl.cpp
@@ -19,35 +19,23 @@
#include <string>
#include <vector>
+#include "Debug.h"
#include "omptargetplugin.h"
-#ifndef TARGET_NAME
#define TARGET_NAME CUDA
-#endif
-
-#ifdef OMPTARGET_DEBUG
-static int DebugLevel = 0;
-
-#define GETNAME2(name) #name
-#define GETNAME(name) GETNAME2(name)
-#define DP(...) \
- do { \
- if (DebugLevel > 0) { \
- DEBUGP("Target " GETNAME(TARGET_NAME) " RTL", __VA_ARGS__); \
- } \
- } while (false)
+#define DEBUG_PREFIX "Target " GETNAME(TARGET_NAME) " RTL"
// Utility for retrieving and printing CUDA error string.
-#define CUDA_ERR_STRING(err) \
- do { \
- if (DebugLevel > 0) { \
- const char *errStr; \
- cuGetErrorString(err, &errStr); \
- DEBUGP("Target " GETNAME(TARGET_NAME) " RTL", "CUDA error is: %s\n", errStr); \
- } \
+#ifdef OMPTARGET_DEBUG
+#define CUDA_ERR_STRING(err) \
+ do { \
+ if (getDebugLevel() > 0) { \
+ const char *errStr; \
+ cuGetErrorString(err, &errStr); \
+ DP("CUDA error is: %s\n", errStr); \
+ } \
} while (false)
#else // OMPTARGET_DEBUG
-#define DP(...) {}
#define CUDA_ERR_STRING(err) {}
#endif // OMPTARGET_DEBUG
@@ -338,10 +326,6 @@ class DeviceRTLTy {
DeviceRTLTy()
: NumberOfDevices(0), EnvNumTeams(-1), EnvTeamLimit(-1),
RequiresFlags(OMP_REQ_UNDEFINED) {
-#ifdef OMPTARGET_DEBUG
- if (const char *EnvStr = getenv("LIBOMPTARGET_DEBUG"))
- DebugLevel = std::stoi(EnvStr);
-#endif // OMPTARGET_DEBUG
DP("Start initializing CUDA\n");
diff --git a/openmp/libomptarget/plugins/generic-elf-64bit/src/rtl.cpp b/openmp/libomptarget/plugins/generic-elf-64bit/src/rtl.cpp
index 8a6e085d3f75..3390aed08403 100644
--- a/openmp/libomptarget/plugins/generic-elf-64bit/src/rtl.cpp
+++ b/openmp/libomptarget/plugins/generic-elf-64bit/src/rtl.cpp
@@ -22,31 +22,18 @@
#include <string>
#include <vector>
+#include "Debug.h"
#include "omptargetplugin.h"
#ifndef TARGET_NAME
#define TARGET_NAME Generic ELF - 64bit
#endif
+#define DEBUG_PREFIX "TARGET " GETNAME(TARGET_NAME) " RTL"
#ifndef TARGET_ELF_ID
#define TARGET_ELF_ID 0
#endif
-#ifdef OMPTARGET_DEBUG
-static int DebugLevel = 0;
-
-#define GETNAME2(name) #name
-#define GETNAME(name) GETNAME2(name)
-#define DP(...) \
- do { \
- if (DebugLevel > 0) { \
- DEBUGP("Target " GETNAME(TARGET_NAME) " RTL", __VA_ARGS__); \
- } \
- } while (false)
-#else // OMPTARGET_DEBUG
-#define DP(...) {}
-#endif // OMPTARGET_DEBUG
-
#include "../../common/elf_common.c"
#define NUMBER_OF_DEVICES 4
@@ -107,11 +94,6 @@ class RTLDeviceInfoTy {
}
RTLDeviceInfoTy(int32_t num_devices) {
-#ifdef OMPTARGET_DEBUG
- if (char *envStr = getenv("LIBOMPTARGET_DEBUG")) {
- DebugLevel = std::stoi(envStr);
- }
-#endif // OMPTARGET_DEBUG
FuncGblEntries.resize(num_devices);
}
diff --git a/openmp/libomptarget/plugins/ve/src/rtl.cpp b/openmp/libomptarget/plugins/ve/src/rtl.cpp
index ec89932a76e1..414fc62650fb 100644
--- a/openmp/libomptarget/plugins/ve/src/rtl.cpp
+++ b/openmp/libomptarget/plugins/ve/src/rtl.cpp
@@ -11,6 +11,7 @@
//
//===----------------------------------------------------------------------===//
+#include "Debug.h"
#include "omptargetplugin.h"
#include <algorithm>
@@ -29,21 +30,9 @@
#define TARGET_ELF_ID 0
#endif
-#ifdef OMPTARGET_DEBUG
-static int DebugLevel = 0;
-
-#define GETNAME2(name) #name
-#define GETNAME(name) GETNAME2(name)
-#define DP(...) \
- do { \
- if (DebugLevel > 0) { \
- DEBUGP("Target " GETNAME(TARGET_NAME) " RTL", __VA_ARGS__); \
- } \
- } while (false)
-#else // OMPTARGET_DEBUG
-#define DP(...) \
- {}
-#endif // OMPTARGET_DEBUG
+#define TARGET_NAME VE
+
+#define DEBUG_PREFIX "Target " GETNAME(TARGET_NAME) " RTL"
#include "../../common/elf_common.c"
@@ -111,11 +100,6 @@ class RTLDeviceInfoTy {
}
RTLDeviceInfoTy() {
-#ifdef OMPTARGET_DEBUG
- if (char *envStr = getenv("LIBOMPTARGET_DEBUG")) {
- DebugLevel = std::stoi(envStr);
- }
-#endif // OMPTARGET_DEBUG
struct ve_nodeinfo node_info;
ve_node_info(&node_info);
diff --git a/openmp/libomptarget/src/api.cpp b/openmp/libomptarget/src/api.cpp
index d9258b4bff5d..7e5f49a8b398 100644
--- a/openmp/libomptarget/src/api.cpp
+++ b/openmp/libomptarget/src/api.cpp
@@ -10,8 +10,6 @@
//
//===----------------------------------------------------------------------===//
-#include <omptarget.h>
-
#include "device.h"
#include "private.h"
#include "rtl.h"
diff --git a/openmp/libomptarget/src/interface.cpp b/openmp/libomptarget/src/interface.cpp
index d15e4f321fc9..d22e5978c20a 100644
--- a/openmp/libomptarget/src/interface.cpp
+++ b/openmp/libomptarget/src/interface.cpp
@@ -11,8 +11,6 @@
//
//===----------------------------------------------------------------------===//
-#include <omptarget.h>
-
#include "device.h"
#include "private.h"
#include "rtl.h"
@@ -62,7 +60,7 @@ static void HandleTargetOutcome(bool success) {
break;
case tgt_mandatory:
if (!success) {
- if (InfoLevel > 0)
+ if (getInfoLevel() > 0)
MESSAGE0("LIBOMPTARGET_INFO is not supported yet");
FATAL_MESSAGE0(1, "failure of target construct while offloading is mandatory");
}
diff --git a/openmp/libomptarget/src/omptarget.cpp b/openmp/libomptarget/src/omptarget.cpp
index c4f781d46959..e44b9aad6202 100644
--- a/openmp/libomptarget/src/omptarget.cpp
+++ b/openmp/libomptarget/src/omptarget.cpp
@@ -11,8 +11,6 @@
//
//===----------------------------------------------------------------------===//
-#include <omptarget.h>
-
#include "device.h"
#include "private.h"
#include "rtl.h"
@@ -20,11 +18,6 @@
#include <cassert>
#include <vector>
-#ifdef OMPTARGET_DEBUG
-int DebugLevel = 0;
-#endif // OMPTARGET_DEBUG
-int InfoLevel = 0;
-
/* All begin addresses for partially mapped structs must be 8-aligned in order
* to ensure proper alignment of members. E.g.
*
diff --git a/openmp/libomptarget/src/private.h b/openmp/libomptarget/src/private.h
index a7091c5b746c..f01714808dd4 100644
--- a/openmp/libomptarget/src/private.h
+++ b/openmp/libomptarget/src/private.h
@@ -14,6 +14,7 @@
#define _OMPTARGET_PRIVATE_H
#include <omptarget.h>
+#include <Debug.h>
#include <cstdint>
@@ -79,39 +80,6 @@ typedef int (*TargetDataFuncPtrTy)(DeviceTy &, int32_t, void **, void **,
int64_t *, int64_t *, void **,
__tgt_async_info *);
-////////////////////////////////////////////////////////////////////////////////
-// implementation for messages
-////////////////////////////////////////////////////////////////////////////////
-
-#define MESSAGE0(_str) \
- do { \
- fprintf(stderr, "Libomptarget message: %s\n", _str); \
- } while (0)
-
-#define MESSAGE(_str, ...) \
- do { \
- fprintf(stderr, "Libomptarget message: " _str "\n", __VA_ARGS__); \
- } while (0)
-
-#define FATAL_MESSAGE0(_num, _str) \
- do { \
- fprintf(stderr, "Libomptarget fatal error %d: %s\n", _num, _str); \
- abort(); \
- } while (0)
-
-#define FATAL_MESSAGE(_num, _str, ...) \
- do { \
- fprintf(stderr, "Libomptarget fatal error %d:" _str "\n", _num, \
- __VA_ARGS__); \
- abort(); \
- } while (0)
-
-#define FAILURE_MESSAGE(...) \
- do { \
- fprintf(stderr, "Libomptarget error: "); \
- fprintf(stderr, __VA_ARGS__); \
- } while (0)
-
// Implemented in libomp, they are called from within __tgt_* functions.
#ifdef __cplusplus
extern "C" {
@@ -125,32 +93,7 @@ int __kmpc_get_target_offload(void) __attribute__((weak));
}
#endif
-extern int InfoLevel;
-#ifdef OMPTARGET_DEBUG
-extern int DebugLevel;
-
-#define DP(...) \
- do { \
- if (DebugLevel > 0) { \
- DEBUGP("Libomptarget", __VA_ARGS__); \
- } \
- } while (false)
-#else // OMPTARGET_DEBUG
-#define DP(...) {}
-#endif // OMPTARGET_DEBUG
-
-// Report debug messages that result in offload failure always
-#ifdef OMPTARGET_DEBUG
-#define REPORT(...) \
- do { \
- if (DebugLevel > 0) { \
- DP(__VA_ARGS__); \
- } else { \
- FAILURE_MESSAGE(__VA_ARGS__); \
- } \
- } while (false)
-#else
-#define REPORT(...) FAILURE_MESSAGE(__VA_ARGS__);
-#endif
+#define TARGET_NAME Libomptarget
+#define DEBUG_PREFIX GETNAME(TARGET_NAME)
#endif
diff --git a/openmp/libomptarget/src/rtl.cpp b/openmp/libomptarget/src/rtl.cpp
index 620451baaa0b..5699ba8da503 100644
--- a/openmp/libomptarget/src/rtl.cpp
+++ b/openmp/libomptarget/src/rtl.cpp
@@ -61,16 +61,6 @@ __attribute__((destructor(101))) void deinit() {
}
void RTLsTy::LoadRTLs() {
-
- if (char *envStr = getenv("LIBOMPTARGET_INFO")) {
- InfoLevel = std::stoi(envStr);
- }
-#ifdef OMPTARGET_DEBUG
- if (char *envStr = getenv("LIBOMPTARGET_DEBUG")) {
- DebugLevel = std::stoi(envStr);
- }
-#endif // OMPTARGET_DEBUG
-
// Parse environment variable OMP_TARGET_OFFLOAD (if set)
TargetOffloadPolicy = (kmp_target_offload_kind_t) __kmpc_get_target_offload();
if (TargetOffloadPolicy == tgt_disabled) {
More information about the Openmp-commits
mailing list