1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
|
//===- ErrorReporting.h - Helper to provide nice error messages ----- 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
//
//===----------------------------------------------------------------------===//
//
//===----------------------------------------------------------------------===//
#ifndef OFFLOAD_PLUGINS_NEXTGEN_COMMON_ERROR_REPORTING_H
#define OFFLOAD_PLUGINS_NEXTGEN_COMMON_ERROR_REPORTING_H
#include "PluginInterface.h"
#include "Shared/EnvironmentVar.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Frontend/OpenMP/OMP.h"
#include "llvm/Support/ErrorHandling.h"
#include "llvm/Support/WithColor.h"
#include "llvm/Support/raw_ostream.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <functional>
#include <optional>
#include <string>
#include <unistd.h>
namespace llvm {
namespace omp {
namespace target {
namespace plugin {
class ErrorReporter {
enum ColorTy {
Yellow = int(HighlightColor::Address),
Green = int(HighlightColor::String),
DarkBlue = int(HighlightColor::Tag),
Cyan = int(HighlightColor::Attribute),
DarkPurple = int(HighlightColor::Enumerator),
DarkRed = int(HighlightColor::Macro),
BoldRed = int(HighlightColor::Error),
BoldLightPurple = int(HighlightColor::Warning),
BoldDarkGrey = int(HighlightColor::Note),
BoldLightBlue = int(HighlightColor::Remark),
};
/// The banner printed at the beginning of an error report.
static constexpr auto ErrorBanner = "OFFLOAD ERROR: ";
/// Return the device id as string, or n/a if not available.
static std::string getDeviceIdStr(GenericDeviceTy *Device) {
return Device ? std::to_string(Device->getDeviceId()) : "n/a";
}
/// Return a nice name for an TargetAllocTy.
static StringRef getAllocTyName(TargetAllocTy Kind) {
switch (Kind) {
case TARGET_ALLOC_DEFAULT:
case TARGET_ALLOC_DEVICE:
return "device memory";
case TARGET_ALLOC_HOST:
return "pinned host memory";
case TARGET_ALLOC_SHARED:
return "managed memory";
break;
}
llvm_unreachable("Unknown target alloc kind");
}
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wgcc-compat"
#pragma clang diagnostic ignored "-Wformat-security"
/// Print \p Format, instantiated with \p Args to stderr.
/// TODO: Allow redirection into a file stream.
template <typename... ArgsTy>
#ifdef __clang__ // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77958
[[gnu::format(__printf__, 1, 2)]]
#endif
static void print(const char *Format, ArgsTy &&...Args) {
raw_fd_ostream OS(STDERR_FILENO, false);
OS << llvm::format(Format, Args...);
}
/// Print \p Format, instantiated with \p Args to stderr, but colored.
/// TODO: Allow redirection into a file stream.
template <typename... ArgsTy>
#ifdef __clang__ // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77958
[[gnu::format(__printf__, 2, 3)]]
#endif
static void print(ColorTy Color, const char *Format, ArgsTy &&...Args) {
raw_fd_ostream OS(STDERR_FILENO, false);
WithColor(OS, HighlightColor(Color)) << llvm::format(Format, Args...);
}
/// Print \p Format, instantiated with \p Args to stderr, but colored and with
/// a banner.
/// TODO: Allow redirection into a file stream.
template <typename... ArgsTy>
#ifdef __clang__ // https://gcc.gnu.org/bugzilla/show_bug.cgi?id=77958
[[gnu::format(__printf__, 1, 2)]]
#endif
static void reportError(const char *Format, ArgsTy &&...Args) {
print(BoldRed, "%s", ErrorBanner);
print(BoldRed, Format, Args...);
print("\n");
}
#pragma clang diagnostic pop
static void reportError(const char *Str) { reportError("%s", Str); }
static void print(const char *Str) { print("%s", Str); }
static void print(StringRef Str) { print("%s", Str.str().c_str()); }
static void print(ColorTy Color, const char *Str) { print(Color, "%s", Str); }
static void print(ColorTy Color, StringRef Str) {
print(Color, "%s", Str.str().c_str());
}
/// Pretty print a stack trace.
static void reportStackTrace(StringRef StackTrace) {
if (StackTrace.empty())
return;
SmallVector<StringRef> Lines, Parts;
StackTrace.split(Lines, "\n", /*MaxSplit=*/-1, /*KeepEmpty=*/false);
int Start = Lines.empty() || !Lines[0].contains("PrintStackTrace") ? 0 : 1;
unsigned NumDigits =
(int)(floor(log10(Lines.size() - Start - /*0*/ 1)) + 1);
for (int I = Start, E = Lines.size(); I < E; ++I) {
auto Line = Lines[I];
Parts.clear();
Line = Line.drop_while([](char C) { return std::isspace(C); });
Line.split(Parts, " ", /*MaxSplit=*/2);
if (Parts.size() != 3 || Parts[0].size() < 2 || Parts[0][0] != '#') {
print("%s\n", Line.str().c_str());
continue;
}
unsigned FrameIdx = std::stoi(Parts[0].drop_front(1).str());
if (Start)
FrameIdx -= 1;
print(DarkPurple, " %s", Parts[0].take_front().str().c_str());
print(Green, "%*u", NumDigits, FrameIdx);
print(BoldLightBlue, " %s", Parts[1].str().c_str());
print(" %s\n", Parts[2].str().c_str());
}
print("\n");
}
/// Report information about an allocation associated with \p ATI.
static void reportAllocationInfo(AllocationTraceInfoTy *ATI) {
if (!ATI)
return;
if (!ATI->DeallocationTrace.empty()) {
print(BoldLightPurple, "Last deallocation:\n");
reportStackTrace(ATI->DeallocationTrace);
}
if (ATI->HostPtr)
print(BoldLightPurple,
"Last allocation of size %lu for host pointer %p -> device pointer "
"%p:\n",
ATI->Size, ATI->HostPtr, ATI->DevicePtr);
else
print(BoldLightPurple,
"Last allocation of size %lu -> device pointer %p:\n", ATI->Size,
ATI->DevicePtr);
reportStackTrace(ATI->AllocationTrace);
if (!ATI->LastAllocationInfo)
return;
unsigned I = 0;
print(BoldLightPurple, "Prior allocations with the same base pointer:");
while (ATI->LastAllocationInfo) {
print("\n");
ATI = ATI->LastAllocationInfo;
print(BoldLightPurple, " #%u Prior deallocation of size %lu:\n", I,
ATI->Size);
reportStackTrace(ATI->DeallocationTrace);
if (ATI->HostPtr)
print(
BoldLightPurple,
" #%u Prior allocation for host pointer %p -> device pointer %p:\n",
I, ATI->HostPtr, ATI->DevicePtr);
else
print(BoldLightPurple, " #%u Prior allocation -> device pointer %p:\n",
I, ATI->DevicePtr);
reportStackTrace(ATI->AllocationTrace);
++I;
}
}
/// End the execution of the program.
static void abortExecution() { abort(); }
public:
#define DEALLOCATION_ERROR(Format, ...) \
reportError(Format, __VA_ARGS__); \
reportStackTrace(StackTrace); \
reportAllocationInfo(ATI); \
abortExecution();
static void reportDeallocationOfNonAllocatedPtr(void *DevicePtr,
TargetAllocTy Kind,
AllocationTraceInfoTy *ATI,
std::string &StackTrace) {
DEALLOCATION_ERROR("deallocation of non-allocated %s: %p",
getAllocTyName(Kind).data(), DevicePtr);
}
static void reportDeallocationOfDeallocatedPtr(void *DevicePtr,
TargetAllocTy Kind,
AllocationTraceInfoTy *ATI,
std::string &StackTrace) {
DEALLOCATION_ERROR("double-free of %s: %p", getAllocTyName(Kind).data(),
DevicePtr);
}
static void reportDeallocationOfWrongPtrKind(void *DevicePtr,
TargetAllocTy Kind,
AllocationTraceInfoTy *ATI,
std::string &StackTrace) {
DEALLOCATION_ERROR("deallocation requires %s but allocation was %s: %p",
getAllocTyName(Kind).data(),
getAllocTyName(ATI->Kind).data(), DevicePtr);
#undef DEALLOCATION_ERROR
}
static void reportMemoryAccessError(GenericDeviceTy &Device, void *DevicePtr,
std::string &ErrorStr, bool Abort) {
reportError(ErrorStr.c_str());
if (!Device.OMPX_TrackAllocationTraces) {
print(Yellow, "Use '%s=true' to track device allocations\n",
Device.OMPX_TrackAllocationTraces.getName().data());
if (Abort)
abortExecution();
return;
}
uintptr_t Distance = false;
auto *ATI =
Device.getClosestAllocationTraceInfoForAddr(DevicePtr, Distance);
if (!ATI) {
print(Cyan,
"No host-issued allocations; device pointer %p might be "
"a global, stack, or shared location\n",
DevicePtr);
if (Abort)
abortExecution();
return;
}
if (!Distance) {
print(Cyan, "Device pointer %p points into%s host-issued allocation:\n",
DevicePtr, ATI->DeallocationTrace.empty() ? "" : " prior");
reportAllocationInfo(ATI);
if (Abort)
abortExecution();
return;
}
bool IsClose = Distance < (1L << 29L /*512MB=*/);
print(Cyan,
"Device pointer %p does not point into any (current or prior) "
"host-issued allocation%s.\n",
DevicePtr,
IsClose ? "" : " (might be a global, stack, or shared location)");
if (IsClose) {
print(Cyan,
"Closest host-issued allocation (distance %" PRIuPTR
" byte%s; might be by page):\n",
Distance, Distance > 1 ? "s" : "");
reportAllocationInfo(ATI);
}
if (Abort)
abortExecution();
}
/// Report that a kernel encountered a trap instruction.
static void reportTrapInKernel(
GenericDeviceTy &Device, KernelTraceInfoRecordTy &KTIR,
std::function<bool(__tgt_async_info &)> AsyncInfoWrapperMatcher) {
assert(AsyncInfoWrapperMatcher && "A matcher is required");
uint32_t Idx = 0;
for (uint32_t I = 0, E = KTIR.size(); I < E; ++I) {
auto KTI = KTIR.getKernelTraceInfo(I);
if (KTI.Kernel == nullptr)
break;
// Skip kernels issued in other queues.
if (KTI.AsyncInfo && !(AsyncInfoWrapperMatcher(*KTI.AsyncInfo)))
continue;
Idx = I;
break;
}
auto KTI = KTIR.getKernelTraceInfo(Idx);
if (KTI.AsyncInfo && (AsyncInfoWrapperMatcher(*KTI.AsyncInfo))) {
auto PrettyKernelName =
llvm::omp::prettifyFunctionName(KTI.Kernel->getName());
reportError("Kernel '%s'", PrettyKernelName.c_str());
}
reportError("execution interrupted by hardware trap instruction");
if (KTI.AsyncInfo && (AsyncInfoWrapperMatcher(*KTI.AsyncInfo))) {
if (!KTI.LaunchTrace.empty())
reportStackTrace(KTI.LaunchTrace);
else
print(Yellow, "Use '%s=1' to show the stack trace of the kernel\n",
Device.OMPX_TrackNumKernelLaunches.getName().data());
}
abort();
}
/// Report the kernel traces taken from \p KTIR, up to
/// OFFLOAD_TRACK_NUM_KERNEL_LAUNCH_TRACES many.
static void reportKernelTraces(GenericDeviceTy &Device,
KernelTraceInfoRecordTy &KTIR) {
uint32_t NumKTIs = 0;
for (uint32_t I = 0, E = KTIR.size(); I < E; ++I) {
auto KTI = KTIR.getKernelTraceInfo(I);
if (KTI.Kernel == nullptr)
break;
++NumKTIs;
}
if (NumKTIs == 0) {
print(BoldRed, "No kernel launches known\n");
return;
}
uint32_t TracesToShow =
std::min(Device.OMPX_TrackNumKernelLaunches.get(), NumKTIs);
if (TracesToShow == 0) {
if (NumKTIs == 1)
print(BoldLightPurple, "Display only launched kernel:\n");
else
print(BoldLightPurple, "Display last %u kernels launched:\n", NumKTIs);
} else {
if (NumKTIs == 1)
print(BoldLightPurple, "Display kernel launch trace:\n");
else
print(BoldLightPurple,
"Display %u of the %u last kernel launch traces:\n", TracesToShow,
NumKTIs);
}
for (uint32_t Idx = 0, I = 0; I < NumKTIs; ++Idx) {
auto KTI = KTIR.getKernelTraceInfo(Idx);
auto PrettyKernelName =
llvm::omp::prettifyFunctionName(KTI.Kernel->getName());
if (NumKTIs == 1)
print(BoldLightPurple, "Kernel '%s'\n", PrettyKernelName.c_str());
else
print(BoldLightPurple, "Kernel %d: '%s'\n", I,
PrettyKernelName.c_str());
reportStackTrace(KTI.LaunchTrace);
++I;
}
if (NumKTIs != 1) {
print(Yellow,
"Use '%s=<num>' to adjust the number of shown stack traces (%u "
"now, up to %zu)\n",
Device.OMPX_TrackNumKernelLaunches.getName().data(),
Device.OMPX_TrackNumKernelLaunches.get(), KTIR.size());
}
// TODO: Let users know how to serialize kernels
}
};
} // namespace plugin
} // namespace target
} // namespace omp
} // namespace llvm
#endif // OFFLOAD_PLUGINS_NEXTGEN_COMMON_ERROR_REPORTING_H
|