Skip to content
Merged
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
53 changes: 53 additions & 0 deletions offload/include/Shared/Debug.h
Original file line number Diff line number Diff line change
Expand Up @@ -430,6 +430,52 @@ static inline raw_ostream &operator<<(raw_ostream &Os,
#define ODBG_RESET_LEVEL() \
static_cast<llvm::offload::debug::odbg_ostream::IfLevel>(0)

// helper templates to support lambdas with different number of arguments
template <typename LambdaTy> struct LambdaHelper {
template <typename T, typename = std::void_t<>>
struct has_two_args : std::false_type {};
template <typename T>
struct has_two_args<T,
std::void_t<decltype(std::declval<T>().operator()(1, 2))>>
: std::true_type {};

static void dispatch(LambdaTy func, llvm::raw_ostream &Os, uint32_t Level) {
if constexpr (has_two_args<LambdaTy>::value)
func(Os, Level);
else
func(Os);
}
};

#define ODBG_OS_BASE(Stream, Component, Prefix, Type, Level, Callback) \
if (::llvm::offload::debug::isDebugEnabled()) { \
uint32_t RealLevel = (Level); \
if (::llvm::offload::debug::shouldPrintDebug((Component), (Type), \
RealLevel)) { \
::llvm::offload::debug::odbg_ostream OS{ \
::llvm::offload::debug::computePrefix((Prefix), (Type)), (Stream), \
RealLevel, /*ShouldPrefixNextString=*/true, \
/*ShouldEmitNewLineOnDestruction=*/true}; \
auto F = Callback; \
::llvm::offload::debug::LambdaHelper<decltype(F)>::dispatch(F, OS, \
RealLevel); \
} \
}

#define ODBG_OS_STREAM(Stream, Type, Level, Callback) \
ODBG_OS_BASE(Stream, GETNAME(TARGET_NAME), DEBUG_PREFIX, Type, Level, \
Callback)
#define ODBG_OS_3(Type, Level, Callback) \
ODBG_OS_STREAM(llvm::offload::debug::dbgs(), Type, Level, Callback)
#define ODBG_OS_2(Type, Callback) ODBG_OS_3(Type, 1, Callback)
#define ODBG_OS_1(Callback) ODBG_OS_2("default", Callback)
#define ODBG_OS_SELECT(Type, Level, Callback, NArgs, ...) ODBG_OS_##NArgs
// Print a debug message of a certain type and verbosity level using a callback
// to emit the message. If no type or level is provided, "default" and "1 are
// assumed respectively.
#define ODBG_OS(...) \
ODBG_OS_SELECT(__VA_ARGS__ __VA_OPT__(, ) 3, 2, 1)(__VA_ARGS__)

#else

inline bool isDebugEnabled() { return false; }
Expand All @@ -446,6 +492,10 @@ inline bool isDebugEnabled() { return false; }
#define ODBG_RESET_LEVEL() 0
#define ODBG(...) ODBG_NULL

#define ODBG_OS_BASE(Stream, Component, Prefix, Type, Level, Callback)
#define ODBG_OS_STREAM(Stream, Type, Level, Callback)
#define ODBG_OS(...)

#endif

} // namespace llvm::offload::debug
Expand Down Expand Up @@ -476,6 +526,9 @@ constexpr const char *ODT_DumpTable = "DumpTable";
constexpr const char *ODT_MappingChanged = "MappingChanged";
constexpr const char *ODT_PluginKernel = "PluginKernel";
constexpr const char *ODT_EmptyMapping = "EmptyMapping";
constexpr const char *ODT_Device = "Device";
constexpr const char *ODT_Interface = "Interface";
constexpr const char *ODT_Alloc = "Alloc";

static inline odbg_ostream reportErrorStream() {
#ifdef OMPTARGET_DEBUG
Expand Down
85 changes: 45 additions & 40 deletions offload/libomptarget/interface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
#include "Utils/ExponentialBackoff.h"

#include "llvm/Frontend/OpenMP/OMPConstants.h"
#include "llvm/Support/Format.h"

#include <cassert>
#include <cstdint>
Expand All @@ -35,6 +36,7 @@
#ifdef OMPT_SUPPORT
using namespace llvm::omp::target::ompt;
#endif
using namespace llvm::omp::target::debug;

// If offload is enabled, ensure that device DeviceID has been initialized.
//
Expand All @@ -49,25 +51,25 @@ using namespace llvm::omp::target::ompt;
// This step might be skipped if offload is disabled.
bool checkDevice(int64_t &DeviceID, ident_t *Loc) {
if (OffloadPolicy::get(*PM).Kind == OffloadPolicy::DISABLED) {
DP("Offload is disabled\n");
ODBG(ODT_Device) << "Offload is disabled";
return true;
}

if (DeviceID == OFFLOAD_DEVICE_DEFAULT) {
DeviceID = omp_get_default_device();
DP("Use default device id %" PRId64 "\n", DeviceID);
ODBG(ODT_Device) << "Use default device id " << DeviceID;
}

// Proposed behavior for OpenMP 5.2 in OpenMP spec github issue 2669.
if (omp_get_num_devices() == 0) {
DP("omp_get_num_devices() == 0 but offload is manadatory\n");
ODBG(ODT_Device) << "omp_get_num_devices() == 0 but offload is manadatory";
handleTargetOutcome(false, Loc);
return true;
}

if (DeviceID == omp_get_initial_device()) {
DP("Device is host (%" PRId64 "), returning as if offload is disabled\n",
DeviceID);
ODBG(ODT_Device) << "Device is host (" << DeviceID
<< "), returning as if offload is disabled";
return true;
}
return false;
Expand Down Expand Up @@ -123,25 +125,25 @@ targetData(ident_t *Loc, int64_t DeviceId, int32_t ArgNum, void **ArgsBase,
TIMESCOPE_WITH_DETAILS_AND_IDENT("Runtime: Data Copy",
"NumArgs=" + std::to_string(ArgNum), Loc);

DP("Entering data %s region for device %" PRId64 " with %d mappings\n",
RegionName, DeviceId, ArgNum);
ODBG(ODT_Interface) << "Entering data " << RegionName << " region for device "
<< DeviceId << " with " << ArgNum << " mappings";

if (checkDevice(DeviceId, Loc)) {
DP("Not offloading to device %" PRId64 "\n", DeviceId);
ODBG(ODT_Interface) << "Not offloading to device " << DeviceId;
return;
}

if (getInfoLevel() & OMP_INFOTYPE_KERNEL_ARGS)
printKernelArguments(Loc, DeviceId, ArgNum, ArgSizes, ArgTypes, ArgNames,
RegionTypeMsg);
#ifdef OMPTARGET_DEBUG
for (int I = 0; I < ArgNum; ++I) {
DP("Entry %2d: Base=" DPxMOD ", Begin=" DPxMOD ", Size=%" PRId64
", Type=0x%" PRIx64 ", Name=%s\n",
I, DPxPTR(ArgsBase[I]), DPxPTR(Args[I]), ArgSizes[I], ArgTypes[I],
(ArgNames) ? getNameFromMapping(ArgNames[I]).c_str() : "unknown");
}
#endif
ODBG_OS(ODT_Kernel, [&](llvm::raw_ostream &Os) {
for (int I = 0; I < ArgNum; ++I) {
Os << "Entry " << llvm::format_decimal(I, 2) << ": Base=" << ArgsBase[I]
<< ", Begin=" << Args[I] << ", Size=" << ArgSizes[I]
<< ", Type=" << llvm::format_hex(ArgTypes[I], 8) << ", Name="
<< ((ArgNames) ? getNameFromMapping(ArgNames[I]) : "unknown") << "\n";
}
});

auto DeviceOrErr = PM->getDevice(DeviceId);
if (!DeviceOrErr)
Expand Down Expand Up @@ -274,7 +276,7 @@ static KernelArgsTy *upgradeKernelArgs(KernelArgsTy *KernelArgs,
KernelArgsTy &LocalKernelArgs,
int32_t NumTeams, int32_t ThreadLimit) {
if (KernelArgs->Version > OMP_KERNEL_ARG_VERSION)
DP("Unexpected ABI version: %u\n", KernelArgs->Version);
ODBG(ODT_Interface) << "Unexpected ABI version: " << KernelArgs->Version;

uint32_t UpgradedVersion = KernelArgs->Version;
if (KernelArgs->Version < OMP_KERNEL_ARG_VERSION) {
Expand Down Expand Up @@ -326,12 +328,11 @@ static inline int targetKernel(ident_t *Loc, int64_t DeviceId, int32_t NumTeams,
assert(PM && "Runtime not initialized");
static_assert(std::is_convertible_v<TargetAsyncInfoTy &, AsyncInfoTy &>,
"Target AsyncInfoTy must be convertible to AsyncInfoTy.");
DP("Entering target region for device %" PRId64 " with entry point " DPxMOD
"\n",
DeviceId, DPxPTR(HostPtr));
ODBG(ODT_Interface) << "Entering target region for device " << DeviceId
<< " with entry point " << HostPtr;

if (checkDevice(DeviceId, Loc)) {
DP("Not offloading to device %" PRId64 "\n", DeviceId);
ODBG(ODT_Interface) << "Not offloading to device " << DeviceId;
return OMP_TGT_FAIL;
}

Expand All @@ -354,17 +355,21 @@ static inline int targetKernel(ident_t *Loc, int64_t DeviceId, int32_t NumTeams,
printKernelArguments(Loc, DeviceId, KernelArgs->NumArgs,
KernelArgs->ArgSizes, KernelArgs->ArgTypes,
KernelArgs->ArgNames, "Entering OpenMP kernel");
#ifdef OMPTARGET_DEBUG
for (uint32_t I = 0; I < KernelArgs->NumArgs; ++I) {
DP("Entry %2d: Base=" DPxMOD ", Begin=" DPxMOD ", Size=%" PRId64
", Type=0x%" PRIx64 ", Name=%s\n",
I, DPxPTR(KernelArgs->ArgBasePtrs[I]), DPxPTR(KernelArgs->ArgPtrs[I]),
KernelArgs->ArgSizes[I], KernelArgs->ArgTypes[I],
(KernelArgs->ArgNames)
? getNameFromMapping(KernelArgs->ArgNames[I]).c_str()
: "unknown");
}
#endif

ODBG_OS(ODT_Kernel, [&](llvm::raw_ostream &Os) {
for (uint32_t I = 0; I < KernelArgs->NumArgs; ++I) {
Os << "Entry " << llvm::format_decimal(I, 2)
<< " Base=" << KernelArgs->ArgBasePtrs[I]
<< ", Begin=" << KernelArgs->ArgPtrs[I]
<< ", Size=" << KernelArgs->ArgSizes[I]
<< ", Type=" << llvm::format_hex(KernelArgs->ArgTypes[I], 8)
<< ", Name="
<< (KernelArgs->ArgNames
? getNameFromMapping(KernelArgs->ArgNames[I]).c_str()
: "unknown")
<< "\n";
}
});

auto DeviceOrErr = PM->getDevice(DeviceId);
if (!DeviceOrErr)
Expand Down Expand Up @@ -463,7 +468,7 @@ EXTERN int __tgt_target_kernel_replay(ident_t *Loc, int64_t DeviceId,
assert(PM && "Runtime not initialized");
OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));
if (checkDevice(DeviceId, Loc)) {
DP("Not offloading to device %" PRId64 "\n", DeviceId);
ODBG(ODT_Interface) << "Not offloading to device " << DeviceId;
return OMP_TGT_FAIL;
}
auto DeviceOrErr = PM->getDevice(DeviceId);
Expand Down Expand Up @@ -491,20 +496,20 @@ EXTERN int __tgt_target_kernel_replay(ident_t *Loc, int64_t DeviceId,
EXTERN int64_t __tgt_mapper_num_components(void *RtMapperHandle) {
auto *MapperComponentsPtr = (struct MapperComponentsTy *)RtMapperHandle;
int64_t Size = MapperComponentsPtr->Components.size();
DP("__tgt_mapper_num_components(Handle=" DPxMOD ") returns %" PRId64 "\n",
DPxPTR(RtMapperHandle), Size);
ODBG(ODT_Interface) << "__tgt_mapper_num_components(Handle=" << RtMapperHandle
<< ") returns " << Size;
return Size;
}

// Push back one component for a user-defined mapper.
EXTERN void __tgt_push_mapper_component(void *RtMapperHandle, void *Base,
void *Begin, int64_t Size, int64_t Type,
void *Name) {
DP("__tgt_push_mapper_component(Handle=" DPxMOD
") adds an entry (Base=" DPxMOD ", Begin=" DPxMOD ", Size=%" PRId64
", Type=0x%" PRIx64 ", Name=%s).\n",
DPxPTR(RtMapperHandle), DPxPTR(Base), DPxPTR(Begin), Size, Type,
(Name) ? getNameFromMapping(Name).c_str() : "unknown");
ODBG(ODT_Interface) << "__tgt_push_mapper_component(Handle=" << RtMapperHandle
<< ") adds an entry (Base=" << Base << ", Begin=" << Begin
<< ", Size=" << Size
<< ", Type=" << llvm::format_hex(Type, 8) << ", Name="
<< ((Name) ? getNameFromMapping(Name) : "unknown") << ")";
auto *MapperComponentsPtr = (struct MapperComponentsTy *)RtMapperHandle;
MapperComponentsPtr->Components.push_back(
MapComponentInfoTy(Base, Begin, Size, Type, Name));
Expand Down
Loading
Loading