Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions offload/include/OffloadPolicy.h
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,12 @@ class OffloadPolicy {
return;
default:
if (PM.getNumDevices()) {
DP("Default TARGET OFFLOAD policy is now mandatory "
"(devices were found)\n");
DPIF(RTL, "Default TARGET OFFLOAD policy is now mandatory "
"(devices were found)\n");
Kind = MANDATORY;
} else {
DP("Default TARGET OFFLOAD policy is now disabled "
"(no devices were found)\n");
DPIF(RTL, "Default TARGET OFFLOAD policy is now disabled "
"(no devices were found)\n");
Kind = DISABLED;
}
return;
Expand Down
8 changes: 4 additions & 4 deletions offload/include/OpenMP/OMPT/Connector.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ class OmptLibraryConnectorTy {
std::string LibName = LibIdent;
LibName += ".so";

DP("OMPT: Trying to load library %s\n", LibName.c_str());
DPIF(TOOL, "OMPT: Trying to load library %s\n", LibName.c_str());
auto DynLibHandle = std::make_unique<llvm::sys::DynamicLibrary>(
llvm::sys::DynamicLibrary::getPermanentLibrary(LibName.c_str(),
&ErrMsg));
Expand All @@ -85,12 +85,12 @@ class OmptLibraryConnectorTy {
LibConnHandle = nullptr;
} else {
auto LibConnRtn = "ompt_" + LibIdent + "_connect";
DP("OMPT: Trying to get address of connection routine %s\n",
LibConnRtn.c_str());
DPIF(TOOL, "OMPT: Trying to get address of connection routine %s\n",
LibConnRtn.c_str());
LibConnHandle = reinterpret_cast<OmptConnectRtnTy>(
DynLibHandle->getAddressOfSymbol(LibConnRtn.c_str()));
}
DP("OMPT: Library connection handle = %p\n", LibConnHandle);
DPIF(TOOL, "OMPT: Library connection handle = %p\n", LibConnHandle);
IsInitialized = true;
}

Expand Down
126 changes: 112 additions & 14 deletions offload/include/Shared/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@

#include <atomic>
#include <mutex>
#include <sstream>
#include <string>

/// 32-Bit field data attributes controlling information presented to the user.
Expand All @@ -62,6 +63,38 @@ enum OpenMPInfoType : uint32_t {
OMP_INFOTYPE_ALL = 0xffffffff,
};

/// 32-bit field attributes controlling debug trace/dump
enum DebugInfoType : uint32_t {
/// Generic plugin/runtime interface/management
DEBUG_INFOTYPE_RTL = 0x0001,
/// Generic device activity
DEBUG_INFOTYPE_DEVICE = 0x0002,
/// Module preparation
DEBUG_INFOTYPE_MODULE = 0x0004,
/// Kernel preparation and invocation
DEBUG_INFOTYPE_KERNEL = 0x0008,
/// Memory allocation/deallocation or related activities
DEBUG_INFOTYPE_MEMORY = 0x0010,
/// Data-mapping activities
DEBUG_INFOTYPE_MAP = 0x0020,
/// Data-copying or similar activities
DEBUG_INFOTYPE_COPY = 0x0040,
/// OpenMP interop
DEBUG_INFOTYPE_INTEROP = 0x0080,
/// Tool interface
DEBUG_INFOTYPE_TOOL = 0x0100,
/// Backend API tracing
DEBUG_INFOTYPE_API = 0x0200,
/// All
DEBUG_INFOTYPE_ALL = 0xffffffff,
};

/// Debug option struct to support both numeric and string value
struct DebugOptionTy {
uint32_t Level;
uint32_t Type;
};

inline std::atomic<uint32_t> &getInfoLevelInternal() {
static std::atomic<uint32_t> InfoLevel;
static std::once_flag Flag{};
Expand All @@ -75,17 +108,45 @@ inline std::atomic<uint32_t> &getInfoLevelInternal() {

inline uint32_t getInfoLevel() { return getInfoLevelInternal().load(); }

inline uint32_t getDebugLevel() {
static uint32_t DebugLevel = 0;
static std::once_flag Flag{};
std::call_once(Flag, []() {
if (char *EnvStr = getenv("LIBOMPTARGET_DEBUG"))
DebugLevel = std::stoi(EnvStr);
});

return DebugLevel;
inline DebugOptionTy &getDebugOption() {
static DebugOptionTy DebugOption = []() {
DebugOptionTy OptVal{0, 0};
char *EnvStr = getenv("LIBOMPTARGET_DEBUG");
if (!EnvStr || *EnvStr == '0')
return OptVal; // undefined or explicitly defined as zero
OptVal.Level = std::atoi(EnvStr);
if (OptVal.Level)
return OptVal; // defined as numeric value
struct DebugStrToBitTy {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

static const

const char *Str;
uint32_t Bit;
} DebugStrToBit[] = {
{"rtl", DEBUG_INFOTYPE_RTL}, {"device", DEBUG_INFOTYPE_DEVICE},
{"module", DEBUG_INFOTYPE_MODULE}, {"kernel", DEBUG_INFOTYPE_KERNEL},
{"memory", DEBUG_INFOTYPE_MEMORY}, {"map", DEBUG_INFOTYPE_MAP},
{"copy", DEBUG_INFOTYPE_COPY}, {"interop", DEBUG_INFOTYPE_INTEROP},
{"tool", DEBUG_INFOTYPE_TOOL}, {"api", DEBUG_INFOTYPE_API},
{"all", DEBUG_INFOTYPE_ALL}, {nullptr, 0},
};
// Check string value of the option. Comma-separated list of the known
// keywords are accepted.
std::istringstream Tokens(EnvStr);
for (std::string Token; std::getline(Tokens, Token, ',');) {
for (int I = 0; DebugStrToBit[I].Str; I++) {
if (Token == DebugStrToBit[I].Str) {
OptVal.Type |= DebugStrToBit[I].Bit;
break;
}
}
}
return OptVal;
}();
return DebugOption;
}

inline uint32_t getDebugLevel() { return getDebugOption().Level; }
inline uint32_t getDebugType() { return getDebugOption().Type; }

#undef USED
#undef GCC_VERSION

Expand Down Expand Up @@ -154,18 +215,25 @@ inline uint32_t getDebugLevel() {
fprintf(stderr, __VA_ARGS__); \
}

/// Emit a message for debugging
#define DP(...) \
/// Check if debug option is turned on for `Type`
#define DPSET(Type) \
((getDebugType() & DEBUG_INFOTYPE_##Type) || getDebugLevel() > 0)

/// Emit a message for debugging if related to `Type`
#define DPIF(Type, ...) \
do { \
if (getDebugLevel() > 0) { \
if (DPSET(Type)) { \
DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
} \
} while (false)

/// Emit a message for debugging
#define DP(...) DPIF(ALL, __VA_ARGS__);

/// Emit a message for debugging or failure if debugging is disabled
#define REPORT(...) \
do { \
if (getDebugLevel() > 0) { \
if (DPSET(ALL)) { \
DP(__VA_ARGS__); \
} else { \
FAILURE_MESSAGE(__VA_ARGS__); \
Expand All @@ -174,15 +242,45 @@ inline uint32_t getDebugLevel() {
#else
#define DEBUGP(prefix, ...) \
{}
#define DPSET(Type) false
#define DPIF(Type, ...) \
{ \
}
#define DP(...) \
{}
#define REPORT(...) FAILURE_MESSAGE(__VA_ARGS__);
#endif // OMPTARGET_DEBUG

#ifdef OMPTARGET_DEBUG
// Convert `OpenMPInfoType` to corresponding `DebugInfoType`
inline bool debugInfoEnabled(OpenMPInfoType InfoType) {
switch (InfoType) {
case OMP_INFOTYPE_KERNEL_ARGS:
[[fallthrough]];
case OMP_INFOTYPE_PLUGIN_KERNEL:
return DPSET(KERNEL);
case OMP_INFOTYPE_MAPPING_EXISTS:
[[fallthrough]];
case OMP_INFOTYPE_DUMP_TABLE:
[[fallthrough]];
case OMP_INFOTYPE_MAPPING_CHANGED:
[[fallthrough]];
case OMP_INFOTYPE_EMPTY_MAPPING:
return DPSET(MAP);
case OMP_INFOTYPE_DATA_TRANSFER:
return DPSET(COPY);
case OMP_INFOTYPE_ALL:
return DPSET(ALL);
}
}
#else
#define debugInfoEnabled(InfoType) false
#endif // OMPTARGET_DEBUG

/// Emit a message giving the user extra information about the runtime if
#define INFO(_flags, _id, ...) \
do { \
if (getDebugLevel() > 0) { \
if (debugInfoEnabled(_flags)) { \
DEBUGP(DEBUG_PREFIX, __VA_ARGS__); \
} else if (getInfoLevel() & _flags) { \
INFO_MESSAGE(_id, __VA_ARGS__); \
Expand Down
10 changes: 6 additions & 4 deletions offload/include/Shared/EnvironmentVar.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,8 @@ template <typename Ty> class Envar {
IsPresent = StringParser::parse<Ty>(EnvStr, Data);

if (!IsPresent) {
DP("Ignoring invalid value %s for envar %s\n", EnvStr, Name.data());
DPIF(RTL, "Ignoring invalid value %s for envar %s\n", EnvStr,
Name.data());
Data = Default;
}
}
Expand Down Expand Up @@ -180,12 +181,13 @@ inline llvm::Error Envar<Ty>::init(llvm::StringRef Name, GetterFunctor Getter,
// not present and reset to the getter value (default).
IsPresent = false;
Data = Default;
DP("Setter of envar %s failed, resetting to %s\n", Name.data(),
std::to_string(Data).data());
DPIF(RTL, "Setter of envar %s failed, resetting to %s\n", Name.data(),
std::to_string(Data).data());
consumeError(std::move(Err));
}
} else {
DP("Ignoring invalid value %s for envar %s\n", EnvStr, Name.data());
DPIF(RTL, "Ignoring invalid value %s for envar %s\n", EnvStr,
Name.data());
Data = Default;
}
} else {
Expand Down
3 changes: 2 additions & 1 deletion offload/libomptarget/LegacyAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,8 @@ EXTERN int __tgt_target_teams_nowait_mapper(
EXTERN void __kmpc_push_target_tripcount_mapper(ident_t *Loc, int64_t DeviceId,
uint64_t LoopTripcount) {
TIMESCOPE_WITH_IDENT(Loc);
DP("WARNING: __kmpc_push_target_tripcount has been deprecated and is a noop");
DPIF(RTL, "WARNING: __kmpc_push_target_tripcount has been deprecated and is "
"a noop");
}

EXTERN void __kmpc_push_target_tripcount(int64_t DeviceId,
Expand Down
8 changes: 4 additions & 4 deletions offload/libomptarget/OffloadRTL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ void initRuntime() {

RefCount++;
if (RefCount == 1) {
DP("Init offload library!\n");
DPIF(RTL, "Init offload library!\n");
#ifdef OMPT_SUPPORT
// Initialize OMPT first
llvm::omp::target::ompt::connectLibrary();
Expand All @@ -54,12 +54,12 @@ void deinitRuntime() {
assert(PM && "Runtime not initialized");

if (RefCount == 1) {
DP("Deinit offload library!\n");
DPIF(RTL, "Deinit offload library!\n");
// RTL deinitialization has started
RTLAlive = false;
while (RTLOngoingSyncs > 0) {
DP("Waiting for ongoing syncs to finish, count: %d\n",
RTLOngoingSyncs.load());
DPIF(RTL, "Waiting for ongoing syncs to finish, count: %d\n",
RTLOngoingSyncs.load());
std::this_thread::sleep_for(std::chrono::milliseconds(100));
}
PM->deinit();
Expand Down
Loading
Loading