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
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
|
//===- DebugImporter.cpp - LLVM to MLIR Debug conversion ------------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "DebugImporter.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Location.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/TypeSwitch.h"
#include "llvm/BinaryFormat/Dwarf.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Metadata.h"
using namespace mlir;
using namespace mlir::LLVM;
using namespace mlir::LLVM::detail;
DebugImporter::DebugImporter(ModuleOp mlirModule,
bool dropDICompositeTypeElements)
: cache([&](llvm::DINode *node) { return createRecSelf(node); }),
context(mlirModule.getContext()), mlirModule(mlirModule),
dropDICompositeTypeElements(dropDICompositeTypeElements) {}
Location DebugImporter::translateFuncLocation(llvm::Function *func) {
llvm::DISubprogram *subprogram = func->getSubprogram();
if (!subprogram)
return UnknownLoc::get(context);
// Add a fused location to link the subprogram information.
StringAttr funcName = StringAttr::get(context, subprogram->getName());
StringAttr fileName = StringAttr::get(context, subprogram->getFilename());
return FusedLocWith<DISubprogramAttr>::get(
{NameLoc::get(funcName),
FileLineColLoc::get(fileName, subprogram->getLine(), /*column=*/0)},
translate(subprogram), context);
}
//===----------------------------------------------------------------------===//
// Attributes
//===----------------------------------------------------------------------===//
DIBasicTypeAttr DebugImporter::translateImpl(llvm::DIBasicType *node) {
return DIBasicTypeAttr::get(context, node->getTag(), node->getName(),
node->getSizeInBits(), node->getEncoding());
}
DICompileUnitAttr DebugImporter::translateImpl(llvm::DICompileUnit *node) {
std::optional<DIEmissionKind> emissionKind =
symbolizeDIEmissionKind(node->getEmissionKind());
std::optional<DINameTableKind> nameTableKind = symbolizeDINameTableKind(
static_cast<
std::underlying_type_t<llvm::DICompileUnit::DebugNameTableKind>>(
node->getNameTableKind()));
return DICompileUnitAttr::get(
context, getOrCreateDistinctID(node), node->getSourceLanguage(),
translate(node->getFile()), getStringAttrOrNull(node->getRawProducer()),
node->isOptimized(), emissionKind.value(), nameTableKind.value(),
getStringAttrOrNull(node->getRawSplitDebugFilename()));
}
DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
std::optional<DIFlags> flags = symbolizeDIFlags(node->getFlags());
SmallVector<DINodeAttr> elements;
// A vector always requires an element.
bool isVectorType = flags && bitEnumContainsAll(*flags, DIFlags::Vector);
if (isVectorType || !dropDICompositeTypeElements) {
for (llvm::DINode *element : node->getElements()) {
assert(element && "expected a non-null element type");
elements.push_back(translate(element));
}
}
// Drop the elements parameter if any of the elements are invalid.
if (llvm::is_contained(elements, nullptr))
elements.clear();
DITypeAttr baseType = translate(node->getBaseType());
// Arrays require a base type, otherwise the debug metadata is considered to
// be malformed.
if (node->getTag() == llvm::dwarf::DW_TAG_array_type && !baseType)
return nullptr;
return DICompositeTypeAttr::get(
context, node->getTag(), getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine(), translate(node->getScope()),
baseType, flags.value_or(DIFlags::Zero), node->getSizeInBits(),
node->getAlignInBits(), translateExpression(node->getDataLocationExp()),
translateExpression(node->getRankExp()),
translateExpression(node->getAllocatedExp()),
translateExpression(node->getAssociatedExp()), elements);
}
DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
// Return nullptr if the base type is invalid.
DITypeAttr baseType = translate(node->getBaseType());
if (node->getBaseType() && !baseType)
return nullptr;
DINodeAttr extraData =
translate(dyn_cast_or_null<llvm::DINode>(node->getExtraData()));
return DIDerivedTypeAttr::get(
context, node->getTag(), getStringAttrOrNull(node->getRawName()),
baseType, node->getSizeInBits(), node->getAlignInBits(),
node->getOffsetInBits(), node->getDWARFAddressSpace(), extraData);
}
DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
return DIStringTypeAttr::get(
context, node->getTag(), getStringAttrOrNull(node->getRawName()),
node->getSizeInBits(), node->getAlignInBits(),
translate(node->getStringLength()),
translateExpression(node->getStringLengthExp()),
translateExpression(node->getStringLocationExp()), node->getEncoding());
}
DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
}
DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
return DILabelAttr::get(context, scope,
getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine());
}
DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
return DILexicalBlockAttr::get(context, scope, translate(node->getFile()),
node->getLine(), node->getColumn());
}
DILexicalBlockFileAttr
DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
return DILexicalBlockFileAttr::get(context, scope, translate(node->getFile()),
node->getDiscriminator());
}
DIGlobalVariableAttr
DebugImporter::translateImpl(llvm::DIGlobalVariable *node) {
// Names of DIGlobalVariables can be empty. MLIR models them as null, instead
// of empty strings, so this special handling is necessary.
auto convertToStringAttr = [&](StringRef name) -> StringAttr {
if (name.empty())
return {};
return StringAttr::get(context, node->getName());
};
return DIGlobalVariableAttr::get(
context, translate(node->getScope()),
convertToStringAttr(node->getName()),
convertToStringAttr(node->getLinkageName()), translate(node->getFile()),
node->getLine(), translate(node->getType()), node->isLocalToUnit(),
node->isDefinition(), node->getAlignInBits());
}
DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
return DILocalVariableAttr::get(
context, scope, getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine(), node->getArg(),
node->getAlignInBits(), translate(node->getType()),
symbolizeDIFlags(node->getFlags()).value_or(DIFlags::Zero));
}
DIVariableAttr DebugImporter::translateImpl(llvm::DIVariable *node) {
return cast<DIVariableAttr>(translate(static_cast<llvm::DINode *>(node)));
}
DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
return cast<DIScopeAttr>(translate(static_cast<llvm::DINode *>(node)));
}
DIModuleAttr DebugImporter::translateImpl(llvm::DIModule *node) {
return DIModuleAttr::get(
context, translate(node->getFile()), translate(node->getScope()),
getStringAttrOrNull(node->getRawName()),
getStringAttrOrNull(node->getRawConfigurationMacros()),
getStringAttrOrNull(node->getRawIncludePath()),
getStringAttrOrNull(node->getRawAPINotesFile()), node->getLineNo(),
node->getIsDecl());
}
DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
return DINamespaceAttr::get(context, getStringAttrOrNull(node->getRawName()),
translate(node->getScope()),
node->getExportSymbols());
}
DIImportedEntityAttr
DebugImporter::translateImpl(llvm::DIImportedEntity *node) {
SmallVector<DINodeAttr> elements;
for (llvm::DINode *element : node->getElements()) {
assert(element && "expected a non-null element type");
elements.push_back(translate(element));
}
return DIImportedEntityAttr::get(
context, node->getTag(), translate(node->getScope()),
translate(node->getEntity()), translate(node->getFile()), node->getLine(),
getStringAttrOrNull(node->getRawName()), elements);
}
DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
// Only definitions require a distinct identifier.
mlir::DistinctAttr id;
if (node->isDistinct())
id = getOrCreateDistinctID(node);
// Return nullptr if the scope or type is invalid.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
std::optional<DISubprogramFlags> subprogramFlags =
symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
assert(subprogramFlags && "expected valid subprogram flags");
DISubroutineTypeAttr type = translate(node->getType());
if (node->getType() && !type)
return nullptr;
// Convert the retained nodes but drop all of them if one of them is invalid.
SmallVector<DINodeAttr> retainedNodes;
for (llvm::DINode *retainedNode : node->getRetainedNodes())
retainedNodes.push_back(translate(retainedNode));
if (llvm::is_contained(retainedNodes, nullptr))
retainedNodes.clear();
SmallVector<DINodeAttr> annotations;
// We currently only support `string` values for annotations on the MLIR side.
// Theoretically we could support other primitives, but LLVM is not using
// other types in practice.
if (llvm::DINodeArray rawAnns = node->getAnnotations(); rawAnns) {
for (size_t i = 0, e = rawAnns->getNumOperands(); i < e; ++i) {
const llvm::MDTuple *tuple = cast<llvm::MDTuple>(rawAnns->getOperand(i));
if (tuple->getNumOperands() != 2)
continue;
const llvm::MDString *name = cast<llvm::MDString>(tuple->getOperand(0));
const llvm::MDString *value =
dyn_cast<llvm::MDString>(tuple->getOperand(1));
if (name && value) {
annotations.push_back(DIAnnotationAttr::get(
context, StringAttr::get(context, name->getString()),
StringAttr::get(context, value->getString())));
}
}
}
return DISubprogramAttr::get(context, id, translate(node->getUnit()), scope,
getStringAttrOrNull(node->getRawName()),
getStringAttrOrNull(node->getRawLinkageName()),
translate(node->getFile()), node->getLine(),
node->getScopeLine(), *subprogramFlags, type,
retainedNodes, annotations);
}
DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
auto getAttrOrNull = [&](llvm::DISubrange::BoundType data) -> Attribute {
if (data.isNull())
return nullptr;
if (auto *constInt = dyn_cast<llvm::ConstantInt *>(data))
return IntegerAttr::get(IntegerType::get(context, 64),
constInt->getSExtValue());
if (auto *expr = dyn_cast<llvm::DIExpression *>(data))
return translateExpression(expr);
if (auto *var = dyn_cast<llvm::DIVariable *>(data)) {
if (auto *local = dyn_cast<llvm::DILocalVariable>(var))
return translate(local);
if (auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
return translate(global);
return nullptr;
}
return nullptr;
};
Attribute count = getAttrOrNull(node->getCount());
Attribute upperBound = getAttrOrNull(node->getUpperBound());
// Either count or the upper bound needs to be present. Otherwise, the
// metadata is invalid. The conversion might fail due to unsupported DI nodes.
if (!count && !upperBound)
return {};
return DISubrangeAttr::get(context, count,
getAttrOrNull(node->getLowerBound()), upperBound,
getAttrOrNull(node->getStride()));
}
DICommonBlockAttr DebugImporter::translateImpl(llvm::DICommonBlock *node) {
return DICommonBlockAttr::get(context, translate(node->getScope()),
translate(node->getDecl()),
getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLineNo());
}
DIGenericSubrangeAttr
DebugImporter::translateImpl(llvm::DIGenericSubrange *node) {
auto getAttrOrNull =
[&](llvm::DIGenericSubrange::BoundType data) -> Attribute {
if (data.isNull())
return nullptr;
if (auto *expr = dyn_cast<llvm::DIExpression *>(data))
return translateExpression(expr);
if (auto *var = dyn_cast<llvm::DIVariable *>(data)) {
if (auto *local = dyn_cast<llvm::DILocalVariable>(var))
return translate(local);
if (auto *global = dyn_cast<llvm::DIGlobalVariable>(var))
return translate(global);
return nullptr;
}
return nullptr;
};
Attribute count = getAttrOrNull(node->getCount());
Attribute upperBound = getAttrOrNull(node->getUpperBound());
Attribute lowerBound = getAttrOrNull(node->getLowerBound());
Attribute stride = getAttrOrNull(node->getStride());
// Either count or the upper bound needs to be present. Otherwise, the
// metadata is invalid.
if (!count && !upperBound)
return {};
return DIGenericSubrangeAttr::get(context, count, lowerBound, upperBound,
stride);
}
DISubroutineTypeAttr
DebugImporter::translateImpl(llvm::DISubroutineType *node) {
SmallVector<DITypeAttr> types;
for (llvm::DIType *type : node->getTypeArray()) {
if (!type) {
// A nullptr entry may appear at the beginning or the end of the
// subroutine types list modeling either a void result type or the type of
// a variadic argument. Translate the nullptr to an explicit
// DINullTypeAttr since the attribute list cannot contain a nullptr entry.
types.push_back(DINullTypeAttr::get(context));
continue;
}
types.push_back(translate(type));
}
// Return nullptr if any of the types is invalid.
if (llvm::is_contained(types, nullptr))
return nullptr;
return DISubroutineTypeAttr::get(context, node->getCC(), types);
}
DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
return cast<DITypeAttr>(translate(static_cast<llvm::DINode *>(node)));
}
DINodeAttr DebugImporter::translate(llvm::DINode *node) {
if (!node)
return nullptr;
// Check for a cached instance.
auto cacheEntry = cache.lookupOrInit(node);
if (std::optional<DINodeAttr> result = cacheEntry.get())
return *result;
// Convert the debug metadata if possible.
auto translateNode = [this](llvm::DINode *node) -> DINodeAttr {
if (auto *casted = dyn_cast<llvm::DIBasicType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DICommonBlock>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DICompileUnit>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DICompositeType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIDerivedType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIStringType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIFile>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIImportedEntity>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DILabel>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DILocalVariable>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIModule>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DINamespace>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubprogram>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubrange>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIGenericSubrange>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubroutineType>(node))
return translateImpl(casted);
return nullptr;
};
if (DINodeAttr attr = translateNode(node)) {
// If this node was repeated, lookup its recursive ID and assign it to the
// base result.
if (cacheEntry.wasRepeated()) {
DistinctAttr recId = nodeToRecId.lookup(node);
auto recType = cast<DIRecursiveTypeAttrInterface>(attr);
attr = cast<DINodeAttr>(recType.withRecId(recId));
}
cacheEntry.resolve(attr);
return attr;
}
cacheEntry.resolve(nullptr);
return nullptr;
}
/// Get the `getRecSelf` constructor for the translated type of `node` if its
/// translated DITypeAttr supports recursion. Otherwise, returns nullptr.
static function_ref<DIRecursiveTypeAttrInterface(DistinctAttr)>
getRecSelfConstructor(llvm::DINode *node) {
using CtorType = function_ref<DIRecursiveTypeAttrInterface(DistinctAttr)>;
return TypeSwitch<llvm::DINode *, CtorType>(node)
.Case([&](llvm::DICompositeType *) {
return CtorType(DICompositeTypeAttr::getRecSelf);
})
.Case([&](llvm::DISubprogram *) {
return CtorType(DISubprogramAttr::getRecSelf);
})
.Default(CtorType());
}
std::optional<DINodeAttr> DebugImporter::createRecSelf(llvm::DINode *node) {
auto recSelfCtor = getRecSelfConstructor(node);
if (!recSelfCtor)
return std::nullopt;
// The original node may have already been assigned a recursive ID from
// a different self-reference. Use that if possible.
DistinctAttr recId = nodeToRecId.lookup(node);
if (!recId) {
recId = DistinctAttr::create(UnitAttr::get(context));
nodeToRecId[node] = recId;
}
DIRecursiveTypeAttrInterface recSelf = recSelfCtor(recId);
return cast<DINodeAttr>(recSelf);
}
//===----------------------------------------------------------------------===//
// Locations
//===----------------------------------------------------------------------===//
Location DebugImporter::translateLoc(llvm::DILocation *loc) {
if (!loc)
return UnknownLoc::get(context);
// Get the file location of the instruction.
Location result = FileLineColLoc::get(context, loc->getFilename(),
loc->getLine(), loc->getColumn());
// Add scope information.
assert(loc->getScope() && "expected non-null scope");
result = FusedLocWith<DIScopeAttr>::get({result}, translate(loc->getScope()),
context);
// Add call site information, if available.
if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
result = CallSiteLoc::get(result, translateLoc(inlinedAt));
return result;
}
DIExpressionAttr DebugImporter::translateExpression(llvm::DIExpression *node) {
if (!node)
return nullptr;
SmallVector<DIExpressionElemAttr> ops;
// Begin processing the operations.
for (const llvm::DIExpression::ExprOperand &op : node->expr_ops()) {
SmallVector<uint64_t> operands;
operands.reserve(op.getNumArgs());
for (const auto &i : llvm::seq(op.getNumArgs()))
operands.push_back(op.getArg(i));
const auto attr = DIExpressionElemAttr::get(context, op.getOp(), operands);
ops.push_back(attr);
}
return DIExpressionAttr::get(context, ops);
}
DIGlobalVariableExpressionAttr DebugImporter::translateGlobalVariableExpression(
llvm::DIGlobalVariableExpression *node) {
return DIGlobalVariableExpressionAttr::get(
context, translate(node->getVariable()),
translateExpression(node->getExpression()));
}
StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
if (!stringNode)
return StringAttr();
return StringAttr::get(context, stringNode->getString());
}
DistinctAttr DebugImporter::getOrCreateDistinctID(llvm::DINode *node) {
DistinctAttr &id = nodeToDistinctAttr[node];
if (!id)
id = DistinctAttr::create(UnitAttr::get(context));
return id;
}
|