Skip to content

Commit 02a908c

Browse files
authored
[OpenMP][Offload] Continue to update libomptarget debug messages (llvm#170425)
* Add support to use lambdas to output debug messages (like LDBG_OS) * Update messages for interface.cpp and omptarget.cpp
1 parent 130fa98 commit 02a908c

File tree

3 files changed

+336
-253
lines changed

3 files changed

+336
-253
lines changed

offload/include/Shared/Debug.h

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -430,6 +430,52 @@ static inline raw_ostream &operator<<(raw_ostream &Os,
430430
#define ODBG_RESET_LEVEL() \
431431
static_cast<llvm::offload::debug::odbg_ostream::IfLevel>(0)
432432

433+
// helper templates to support lambdas with different number of arguments
434+
template <typename LambdaTy> struct LambdaHelper {
435+
template <typename T, typename = std::void_t<>>
436+
struct has_two_args : std::false_type {};
437+
template <typename T>
438+
struct has_two_args<T,
439+
std::void_t<decltype(std::declval<T>().operator()(1, 2))>>
440+
: std::true_type {};
441+
442+
static void dispatch(LambdaTy func, llvm::raw_ostream &Os, uint32_t Level) {
443+
if constexpr (has_two_args<LambdaTy>::value)
444+
func(Os, Level);
445+
else
446+
func(Os);
447+
}
448+
};
449+
450+
#define ODBG_OS_BASE(Stream, Component, Prefix, Type, Level, Callback) \
451+
if (::llvm::offload::debug::isDebugEnabled()) { \
452+
uint32_t RealLevel = (Level); \
453+
if (::llvm::offload::debug::shouldPrintDebug((Component), (Type), \
454+
RealLevel)) { \
455+
::llvm::offload::debug::odbg_ostream OS{ \
456+
::llvm::offload::debug::computePrefix((Prefix), (Type)), (Stream), \
457+
RealLevel, /*ShouldPrefixNextString=*/true, \
458+
/*ShouldEmitNewLineOnDestruction=*/true}; \
459+
auto F = Callback; \
460+
::llvm::offload::debug::LambdaHelper<decltype(F)>::dispatch(F, OS, \
461+
RealLevel); \
462+
} \
463+
}
464+
465+
#define ODBG_OS_STREAM(Stream, Type, Level, Callback) \
466+
ODBG_OS_BASE(Stream, GETNAME(TARGET_NAME), DEBUG_PREFIX, Type, Level, \
467+
Callback)
468+
#define ODBG_OS_3(Type, Level, Callback) \
469+
ODBG_OS_STREAM(llvm::offload::debug::dbgs(), Type, Level, Callback)
470+
#define ODBG_OS_2(Type, Callback) ODBG_OS_3(Type, 1, Callback)
471+
#define ODBG_OS_1(Callback) ODBG_OS_2("default", Callback)
472+
#define ODBG_OS_SELECT(Type, Level, Callback, NArgs, ...) ODBG_OS_##NArgs
473+
// Print a debug message of a certain type and verbosity level using a callback
474+
// to emit the message. If no type or level is provided, "default" and "1 are
475+
// assumed respectively.
476+
#define ODBG_OS(...) \
477+
ODBG_OS_SELECT(__VA_ARGS__ __VA_OPT__(, ) 3, 2, 1)(__VA_ARGS__)
478+
433479
#else
434480

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

495+
#define ODBG_OS_BASE(Stream, Component, Prefix, Type, Level, Callback)
496+
#define ODBG_OS_STREAM(Stream, Type, Level, Callback)
497+
#define ODBG_OS(...)
498+
449499
#endif
450500

451501
} // namespace llvm::offload::debug
@@ -476,6 +526,9 @@ constexpr const char *ODT_DumpTable = "DumpTable";
476526
constexpr const char *ODT_MappingChanged = "MappingChanged";
477527
constexpr const char *ODT_PluginKernel = "PluginKernel";
478528
constexpr const char *ODT_EmptyMapping = "EmptyMapping";
529+
constexpr const char *ODT_Device = "Device";
530+
constexpr const char *ODT_Interface = "Interface";
531+
constexpr const char *ODT_Alloc = "Alloc";
479532

480533
static inline odbg_ostream reportErrorStream() {
481534
#ifdef OMPTARGET_DEBUG

offload/libomptarget/interface.cpp

Lines changed: 45 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "Utils/ExponentialBackoff.h"
2626

2727
#include "llvm/Frontend/OpenMP/OMPConstants.h"
28+
#include "llvm/Support/Format.h"
2829

2930
#include <cassert>
3031
#include <cstdint>
@@ -35,6 +36,7 @@
3536
#ifdef OMPT_SUPPORT
3637
using namespace llvm::omp::target::ompt;
3738
#endif
39+
using namespace llvm::omp::target::debug;
3840

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

5658
if (DeviceID == OFFLOAD_DEVICE_DEFAULT) {
5759
DeviceID = omp_get_default_device();
58-
DP("Use default device id %" PRId64 "\n", DeviceID);
60+
ODBG(ODT_Device) << "Use default device id " << DeviceID;
5961
}
6062

6163
// Proposed behavior for OpenMP 5.2 in OpenMP spec github issue 2669.
6264
if (omp_get_num_devices() == 0) {
63-
DP("omp_get_num_devices() == 0 but offload is manadatory\n");
65+
ODBG(ODT_Device) << "omp_get_num_devices() == 0 but offload is manadatory";
6466
handleTargetOutcome(false, Loc);
6567
return true;
6668
}
6769

6870
if (DeviceID == omp_get_initial_device()) {
69-
DP("Device is host (%" PRId64 "), returning as if offload is disabled\n",
70-
DeviceID);
71+
ODBG(ODT_Device) << "Device is host (" << DeviceID
72+
<< "), returning as if offload is disabled";
7173
return true;
7274
}
7375
return false;
@@ -123,25 +125,25 @@ targetData(ident_t *Loc, int64_t DeviceId, int32_t ArgNum, void **ArgsBase,
123125
TIMESCOPE_WITH_DETAILS_AND_IDENT("Runtime: Data Copy",
124126
"NumArgs=" + std::to_string(ArgNum), Loc);
125127

126-
DP("Entering data %s region for device %" PRId64 " with %d mappings\n",
127-
RegionName, DeviceId, ArgNum);
128+
ODBG(ODT_Interface) << "Entering data " << RegionName << " region for device "
129+
<< DeviceId << " with " << ArgNum << " mappings";
128130

129131
if (checkDevice(DeviceId, Loc)) {
130-
DP("Not offloading to device %" PRId64 "\n", DeviceId);
132+
ODBG(ODT_Interface) << "Not offloading to device " << DeviceId;
131133
return;
132134
}
133135

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

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

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

333334
if (checkDevice(DeviceId, Loc)) {
334-
DP("Not offloading to device %" PRId64 "\n", DeviceId);
335+
ODBG(ODT_Interface) << "Not offloading to device " << DeviceId;
335336
return OMP_TGT_FAIL;
336337
}
337338

@@ -354,17 +355,21 @@ static inline int targetKernel(ident_t *Loc, int64_t DeviceId, int32_t NumTeams,
354355
printKernelArguments(Loc, DeviceId, KernelArgs->NumArgs,
355356
KernelArgs->ArgSizes, KernelArgs->ArgTypes,
356357
KernelArgs->ArgNames, "Entering OpenMP kernel");
357-
#ifdef OMPTARGET_DEBUG
358-
for (uint32_t I = 0; I < KernelArgs->NumArgs; ++I) {
359-
DP("Entry %2d: Base=" DPxMOD ", Begin=" DPxMOD ", Size=%" PRId64
360-
", Type=0x%" PRIx64 ", Name=%s\n",
361-
I, DPxPTR(KernelArgs->ArgBasePtrs[I]), DPxPTR(KernelArgs->ArgPtrs[I]),
362-
KernelArgs->ArgSizes[I], KernelArgs->ArgTypes[I],
363-
(KernelArgs->ArgNames)
364-
? getNameFromMapping(KernelArgs->ArgNames[I]).c_str()
365-
: "unknown");
366-
}
367-
#endif
358+
359+
ODBG_OS(ODT_Kernel, [&](llvm::raw_ostream &Os) {
360+
for (uint32_t I = 0; I < KernelArgs->NumArgs; ++I) {
361+
Os << "Entry " << llvm::format_decimal(I, 2)
362+
<< " Base=" << KernelArgs->ArgBasePtrs[I]
363+
<< ", Begin=" << KernelArgs->ArgPtrs[I]
364+
<< ", Size=" << KernelArgs->ArgSizes[I]
365+
<< ", Type=" << llvm::format_hex(KernelArgs->ArgTypes[I], 8)
366+
<< ", Name="
367+
<< (KernelArgs->ArgNames
368+
? getNameFromMapping(KernelArgs->ArgNames[I]).c_str()
369+
: "unknown")
370+
<< "\n";
371+
}
372+
});
368373

369374
auto DeviceOrErr = PM->getDevice(DeviceId);
370375
if (!DeviceOrErr)
@@ -463,7 +468,7 @@ EXTERN int __tgt_target_kernel_replay(ident_t *Loc, int64_t DeviceId,
463468
assert(PM && "Runtime not initialized");
464469
OMPT_IF_BUILT(ReturnAddressSetterRAII RA(__builtin_return_address(0)));
465470
if (checkDevice(DeviceId, Loc)) {
466-
DP("Not offloading to device %" PRId64 "\n", DeviceId);
471+
ODBG(ODT_Interface) << "Not offloading to device " << DeviceId;
467472
return OMP_TGT_FAIL;
468473
}
469474
auto DeviceOrErr = PM->getDevice(DeviceId);
@@ -491,20 +496,20 @@ EXTERN int __tgt_target_kernel_replay(ident_t *Loc, int64_t DeviceId,
491496
EXTERN int64_t __tgt_mapper_num_components(void *RtMapperHandle) {
492497
auto *MapperComponentsPtr = (struct MapperComponentsTy *)RtMapperHandle;
493498
int64_t Size = MapperComponentsPtr->Components.size();
494-
DP("__tgt_mapper_num_components(Handle=" DPxMOD ") returns %" PRId64 "\n",
495-
DPxPTR(RtMapperHandle), Size);
499+
ODBG(ODT_Interface) << "__tgt_mapper_num_components(Handle=" << RtMapperHandle
500+
<< ") returns " << Size;
496501
return Size;
497502
}
498503

499504
// Push back one component for a user-defined mapper.
500505
EXTERN void __tgt_push_mapper_component(void *RtMapperHandle, void *Base,
501506
void *Begin, int64_t Size, int64_t Type,
502507
void *Name) {
503-
DP("__tgt_push_mapper_component(Handle=" DPxMOD
504-
") adds an entry (Base=" DPxMOD ", Begin=" DPxMOD ", Size=%" PRId64
505-
", Type=0x%" PRIx64 ", Name=%s).\n",
506-
DPxPTR(RtMapperHandle), DPxPTR(Base), DPxPTR(Begin), Size, Type,
507-
(Name) ? getNameFromMapping(Name).c_str() : "unknown");
508+
ODBG(ODT_Interface) << "__tgt_push_mapper_component(Handle=" << RtMapperHandle
509+
<< ") adds an entry (Base=" << Base << ", Begin=" << Begin
510+
<< ", Size=" << Size
511+
<< ", Type=" << llvm::format_hex(Type, 8) << ", Name="
512+
<< ((Name) ? getNameFromMapping(Name) : "unknown") << ")";
508513
auto *MapperComponentsPtr = (struct MapperComponentsTy *)RtMapperHandle;
509514
MapperComponentsPtr->Components.push_back(
510515
MapComponentInfoTy(Base, Begin, Size, Type, Name));

0 commit comments

Comments
 (0)