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,119 @@ +//===------- 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 libmomptarget +// and plugin RTLs to the user. +// +// Each plugin RTL and libomp target define TARGET_NAME and DEBUG_PREFIX for use +// when sending messages to the user. These indicate which RTL sent themessage +// +// 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 + +// Enables debugging output if env LIBOMPTARGET_DEBUG > 0 +extern int DebugLevel; +// Enables extra runtime information output if env LIBOMPTARGET_INFO > 0 +extern int InfoLevel; + +#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 (DebugLevel > 0) { \ + DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \ + } \ + } while (false) + +#define REPORT(...) \ + do { \ + if (DebugLevel > 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,27 +102,14 @@ #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 +int DebugLevel = 0; #undef check // Drop definition from internal.h #ifdef OMPTARGET_DEBUG 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(DEBUG_PREFIX) && defined(DP)) +#error Include elf_common.c in the plugin source AFTER DEBUG_PREFIX has\ + been defined 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,25 @@ #include #include +#include "Debug.h" #include "omptargetplugin.h" -#ifndef TARGET_NAME -#define TARGET_NAME CUDA -#endif +int DebugLevel = 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) +#define TARGET_NAME CUDA +#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 (DebugLevel > 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 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,20 @@ #include #include +#include "Debug.h" #include "omptargetplugin.h" +int DebugLevel = 0; + #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 @@ -108,9 +97,8 @@ RTLDeviceInfoTy(int32_t num_devices) { #ifdef OMPTARGET_DEBUG - if (char *envStr = getenv("LIBOMPTARGET_DEBUG")) { - DebugLevel = std::stoi(envStr); - } + 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,11 @@ #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" + +int DebugLevel = 0; #include "../../common/elf_common.c" 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" 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 @@ -21,6 +21,9 @@ #include #include +int DebugLevel = 0; +int InfoLevel = 0; + // List of all plugins that can support offloading. static const char *RTLNames[] = { /* PowerPC target */ "libomptarget.rtl.ppc64.so", @@ -61,14 +64,11 @@ } void RTLsTy::LoadRTLs() { - - if (char *envStr = getenv("LIBOMPTARGET_INFO")) { - InfoLevel = std::stoi(envStr); - } + if (char *EnvStr = getenv("LIBOMPTARGET_INFO")) + InfoLevel = std::stoi(EnvStr); #ifdef OMPTARGET_DEBUG - if (char *envStr = getenv("LIBOMPTARGET_DEBUG")) { - DebugLevel = std::stoi(envStr); - } + if (char *EnvStr = getenv("LIBOMPTARGET_DEBUG")) + DebugLevel = std::stoi(EnvStr); #endif // OMPTARGET_DEBUG // Parse environment variable OMP_TARGET_OFFLOAD (if set)