diff --git a/openmp/libomptarget/include/Debug.h b/openmp/libomptarget/include/Debug.h new file mode 100644 --- /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 +#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 + +#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 --- a/openmp/libomptarget/include/omptarget.h +++ b/openmp/libomptarget/include/omptarget.h @@ -261,45 +261,6 @@ } #endif -#ifndef __STDC_FORMAT_MACROS -#define __STDC_FORMAT_MACROS -#endif - -#include -#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 -#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 --- 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 @@ #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 @@ } 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 --- 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) && defined(DP)) +#error Include elf_common.c in the plugin source AFTER Debug.h has\ + been included and macro DP(...) has been defined. #endif #include diff --git a/openmp/libomptarget/plugins/cuda/src/rtl.cpp b/openmp/libomptarget/plugins/cuda/src/rtl.cpp --- a/openmp/libomptarget/plugins/cuda/src/rtl.cpp +++ b/openmp/libomptarget/plugins/cuda/src/rtl.cpp @@ -19,35 +19,23 @@ #include #include +#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 @@ 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 --- 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 #include +#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 @@ } 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 --- 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 @@ -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 @@ } 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 --- a/openmp/libomptarget/src/api.cpp +++ b/openmp/libomptarget/src/api.cpp @@ -10,8 +10,6 @@ // //===----------------------------------------------------------------------===// -#include - #include "device.h" #include "private.h" #include "rtl.h" diff --git a/openmp/libomptarget/src/interface.cpp b/openmp/libomptarget/src/interface.cpp --- a/openmp/libomptarget/src/interface.cpp +++ b/openmp/libomptarget/src/interface.cpp @@ -11,8 +11,6 @@ // //===----------------------------------------------------------------------===// -#include - #include "device.h" #include "private.h" #include "rtl.h" @@ -62,7 +60,7 @@ 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 --- a/openmp/libomptarget/src/omptarget.cpp +++ b/openmp/libomptarget/src/omptarget.cpp @@ -11,8 +11,6 @@ // //===----------------------------------------------------------------------===// -#include - #include "device.h" #include "private.h" #include "rtl.h" @@ -20,11 +18,6 @@ #include #include -#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 --- a/openmp/libomptarget/src/private.h +++ b/openmp/libomptarget/src/private.h @@ -14,6 +14,7 @@ #define _OMPTARGET_PRIVATE_H #include +#include #include @@ -79,39 +80,6 @@ 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 @@ } #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 --- a/openmp/libomptarget/src/rtl.cpp +++ b/openmp/libomptarget/src/rtl.cpp @@ -61,16 +61,6 @@ } 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) {