Skip to content

Commit 2965164

Browse files
committed
[OFFLOAD] Add support for more fine grained debug messages control
1 parent b6bbc4b commit 2965164

File tree

4 files changed

+192
-2
lines changed

4 files changed

+192
-2
lines changed

offload/include/Shared/Debug.h

Lines changed: 188 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@
4242
#include <mutex>
4343
#include <string>
4444

45+
#include "llvm/Support/circular_raw_ostream.h"
46+
4547
/// 32-Bit field data attributes controlling information presented to the user.
4648
enum OpenMPInfoType : uint32_t {
4749
// Print data arguments and attributes upon entering an OpenMP device kernel.
@@ -198,4 +200,190 @@ inline uint32_t getDebugLevel() {
198200
} \
199201
} while (false)
200202

203+
// New macros that will allow for more granular control over debugging output
204+
// Each message can be classified by Component, Type and Level
205+
// Component: The broad component of the offload runtime emitting the message.
206+
// Type: A cross-component classification of messages
207+
// Level: The verbosity level of the message
208+
//
209+
// The component is pulled from the TARGET_NAME macro, Type and Level can be
210+
// defined for each debug message but by default they are "default" and "1"
211+
// respectively.
212+
//
213+
// For liboffload and plugins, use OFFLOAD_DEBUG(...)
214+
// For libomptarget, use OPENMP_DEBUG(...)
215+
// Constructing messages should be done using C++ stream style syntax.
216+
//
217+
// Usage examples:
218+
// OFFLOAD_DEBUG("type1", 2, "This is a level 2 message of type1");
219+
// OFFLOAD_DEBUG("Init", "This is a default level of the init type");
220+
// OPENMP_DEBUG("This is a level 1 message of the default type");
221+
// OFFLOAD_DEBUG("Init", 3, NumDevices << " were initialized\n");
222+
// OFFLOAD_DEBUG("Kernel", "Starting kernel " << KernelName << " on device " <<
223+
// DeviceId);
224+
//
225+
// Message output can be controlled by setting LIBOMPTARGET_DEBUG or
226+
// LIBOFFLOAD_DEBUG environment variables. Their syntax is as follows:
227+
// [integer]|all|<type1>[:<level1>][,<type2>[:<level2>],...]
228+
//
229+
// 0 : Disable all debug messages
230+
// all : Enable all debug messages
231+
// integer : Set the default level for all messages
232+
// <type> : Enable only messages of the specified type and level (more than one
233+
// can be specified). Components are also supported as
234+
// types.
235+
// <level> : Set the verbosity level for the specified type (default is 1)
236+
//
237+
// For very specific cases where more control is needed, use OFFLOAD_DEBUG_RAW
238+
// or OFFLOAD_DEBUG_BASE. See below for details.
239+
240+
namespace llvm::offload::debug {
241+
242+
#ifdef OMPTARGET_DEBUG
243+
244+
struct DebugFilter {
245+
StringRef Type;
246+
uint32_t Level;
247+
};
248+
249+
struct DebugSettings {
250+
bool Enabled = false;
251+
uint32_t DefaultLevel = 1;
252+
llvm::SmallVector<DebugFilter> Filters;
253+
};
254+
255+
/// dbgs - Return a circular-buffered debug stream.
256+
inline llvm::raw_ostream &dbgs() {
257+
// Do one-time initialization in a thread-safe way.
258+
static struct dbgstream {
259+
llvm::circular_raw_ostream strm;
260+
261+
dbgstream() : strm(llvm::errs(), "*** Debug Log Output ***\n", 0) {}
262+
} thestrm;
263+
264+
return thestrm.strm;
265+
}
266+
267+
inline DebugFilter parseDebugFilter(StringRef Filter) {
268+
size_t Pos = Filter.find(':');
269+
if (Pos == StringRef::npos)
270+
return {Filter, 1};
271+
272+
StringRef Type = Filter.slice(0, Pos);
273+
uint32_t Level = 1;
274+
if (Filter.slice(Pos + 1, Filter.size()).getAsInteger(10, Level))
275+
Level = 1;
276+
277+
return {Type, Level};
278+
}
279+
280+
inline DebugSettings &getDebugSettings() {
281+
static DebugSettings Settings;
282+
static std::once_flag Flag{};
283+
std::call_once(Flag, []() {
284+
printf("Configuring debug settings\n");
285+
// Eventually, we probably should allow for the upper layers to set
286+
// the debug settings directly according to their own env var
287+
// (or other methods) settings.
288+
// For now mantain compatibility with existing libomptarget env var
289+
// and add a liboffload independent one.
290+
char *Env = getenv("LIBOMPTARGET_DEBUG");
291+
if (!Env) {
292+
Env = getenv("LIBOFFLOAD_DEBUG");
293+
if (!Env)
294+
return;
295+
}
296+
297+
StringRef EnvRef(Env);
298+
if (EnvRef == "0")
299+
return;
300+
301+
Settings.Enabled = true;
302+
if (EnvRef.equals_insensitive("all"))
303+
return;
304+
305+
if (!EnvRef.getAsInteger(10, Settings.DefaultLevel))
306+
return;
307+
308+
Settings.DefaultLevel = 1;
309+
310+
SmallVector<StringRef> DbgTypes;
311+
EnvRef.split(DbgTypes, ',', -1, false);
312+
313+
for (auto &DT : DbgTypes)
314+
Settings.Filters.push_back(parseDebugFilter(DT));
315+
});
316+
317+
return Settings;
318+
}
319+
320+
inline bool isDebugEnabled() {
321+
return getDebugSettings().Enabled;
322+
}
323+
324+
inline bool shouldPrintDebug(const char *Component, const char *Type, uint32_t Level) {
325+
const auto &Settings = getDebugSettings();
326+
if (!Settings.Enabled)
327+
return false;
328+
329+
if (Settings.Filters.empty())
330+
return Level <= Settings.DefaultLevel;
331+
332+
for (const auto &DT : Settings.Filters) {
333+
if (DT.Level < Level)
334+
continue;
335+
if (DT.Type.equals_insensitive(Type))
336+
return true;
337+
if (DT.Type.equals_insensitive(Component))
338+
return true;
339+
}
340+
341+
return false;
342+
}
343+
344+
#define OFFLOAD_DEBUG_BASE(Component, Type, Level, ...) \
345+
do { \
346+
if (llvm::offload::debug::isDebugEnabled() && \
347+
llvm::offload::debug::shouldPrintDebug(Component, Type, Level)) \
348+
__VA_ARGS__; \
349+
} while (0)
350+
351+
#define OFFLOAD_DEBUG_RAW(Type, Level, X) \
352+
OFFLOAD_DEBUG_BASE(GETNAME(TARGET_NAME), Type, Level, X)
353+
354+
#define OFFLOAD_DEBUG_1(X) \
355+
OFFLOAD_DEBUG_BASE(GETNAME(TARGET_NAME), "default", 1, \
356+
llvm::offload::debug::dbgs() \
357+
<< DEBUG_PREFIX << " --> " << X)
358+
359+
#define OFFLOAD_DEBUG_2(Type, X) \
360+
OFFLOAD_DEBUG_BASE(GETNAME(TARGET_NAME), Type, 1, \
361+
llvm::offload::debug::dbgs() \
362+
<< DEBUG_PREFIX << " --> " << X)
363+
364+
#define OFFLOAD_DEBUG_3(Type, Level, X) \
365+
OFFLOAD_DEBUG_BASE(GETNAME(TARGET_NAME), Type, Level, \
366+
llvm::offload::debug::dbgs() \
367+
<< DEBUG_PREFIX << " --> " << X)
368+
369+
#define OFFLOAD_SELECT(Type, Level, X, NArgs, ...) OFFLOAD_DEBUG_##NArgs
370+
371+
// To be used in liboffload and plugins
372+
#define OFFLOAD_DEBUG(...) OFFLOAD_SELECT(__VA_ARGS__, 3, 2, 1)(__VA_ARGS__)
373+
374+
// To be used in libomptarget only
375+
#define OPENMP_DEBUG(...) OFFLOAD_DEBUG(__VA_ARGS__)
376+
377+
#else
378+
379+
// Don't print anything if debugging is disabled
380+
#define OFFLOAD_DEBUG_BASE(Component, Type, Level, ...)
381+
#define OFFLOAD_DEBUG_RAW(Type, Level, X)
382+
#define OFFLOAD_DEBUG(...)
383+
#define OPENMP_DEBUG(...)
384+
385+
#endif
386+
387+
} // namespace llvm::offload::debug
388+
201389
#endif // OMPTARGET_SHARED_DEBUG_H

offload/libomptarget/OffloadRTL.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void initRuntime() {
3535

3636
RefCount++;
3737
if (RefCount == 1) {
38-
DP("Init offload library!\n");
38+
OPENMP_DEBUG("Init offload library!\n");
3939
#ifdef OMPT_SUPPORT
4040
// Initialize OMPT first
4141
llvm::omp::target::ompt::connectLibrary();

offload/libomptarget/PluginManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ void PluginManager::init() {
3636
return;
3737
}
3838

39-
DP("Loading RTLs...\n");
39+
OPENMP_DEBUG("Init", "Loading RTLs\n");
4040

4141
// Attempt to create an instance of each supported plugin.
4242
#define PLUGIN_TARGET(Name) \

offload/plugins-nextgen/host/src/rtl.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -451,6 +451,8 @@ struct GenELF64PluginTy final : public GenericPluginTy {
451451
if (auto Err = Plugin::check(ffi_init(), "failed to initialize libffi"))
452452
return std::move(Err);
453453
#endif
454+
OFFLOAD_DEBUG("Init", 2,
455+
"GenELF64 plugin detected" << NUM_DEVICES << " devices\n");
454456

455457
return NUM_DEVICES;
456458
}

0 commit comments

Comments
 (0)