1//===- DebugTranslation.cpp - MLIR to LLVM Debug conversion ---------------===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8
9#include "DebugTranslation.h"
10#include "mlir/Dialect/LLVMIR/LLVMDialect.h"
11#include "llvm/ADT/TypeSwitch.h"
12#include "llvm/IR/Metadata.h"
13#include "llvm/IR/Module.h"
14#include "llvm/Support/FileSystem.h"
15#include "llvm/Support/Path.h"
16
17using namespace mlir;
18using namespace mlir::LLVM;
19using namespace mlir::LLVM::detail;
20
21/// A utility walker that interrupts if the operation has valid debug
22/// information.
23static WalkResult interruptIfValidLocation(Operation *op) {
24 return isa<UnknownLoc>(Val: op->getLoc()) ? WalkResult::advance()
25 : WalkResult::interrupt();
26}
27
28DebugTranslation::DebugTranslation(Operation *module, llvm::Module &llvmModule)
29 : debugEmissionIsEnabled(false), llvmModule(llvmModule),
30 llvmCtx(llvmModule.getContext()) {
31 // If the module has no location information, there is nothing to do.
32 if (!module->walk(callback&: interruptIfValidLocation).wasInterrupted())
33 return;
34 debugEmissionIsEnabled = true;
35}
36
37static constexpr StringRef kDebugVersionKey = "Debug Info Version";
38static constexpr StringRef kCodeViewKey = "CodeView";
39
40void DebugTranslation::addModuleFlagsIfNotPresent() {
41 // TODO: The version information should be encoded on the LLVM module itself,
42 // not implicitly set here.
43
44 // Mark this module as having debug information.
45 if (!llvmModule.getModuleFlag(Key: kDebugVersionKey))
46 llvmModule.addModuleFlag(Behavior: llvm::Module::Warning, Key: kDebugVersionKey,
47 Val: llvm::DEBUG_METADATA_VERSION);
48
49 const llvm::Triple &targetTriple = llvmModule.getTargetTriple();
50 if (targetTriple.isKnownWindowsMSVCEnvironment()) {
51 // Dwarf debugging files will be generated by default, unless "CodeView"
52 // is set explicitly. Windows/MSVC should use CodeView instead.
53 if (!llvmModule.getModuleFlag(Key: kCodeViewKey))
54 llvmModule.addModuleFlag(Behavior: llvm::Module::Warning, Key: kCodeViewKey, Val: 1);
55 }
56}
57
58/// Translate the debug information for the given function.
59void DebugTranslation::translate(LLVMFuncOp func, llvm::Function &llvmFunc) {
60 if (!debugEmissionIsEnabled)
61 return;
62
63 // Look for a sub program attached to the function.
64 auto spLoc =
65 func.getLoc()->findInstanceOf<FusedLocWith<LLVM::DISubprogramAttr>>();
66 if (!spLoc)
67 return;
68 llvmFunc.setSubprogram(translate(attr: spLoc.getMetadata()));
69}
70
71//===----------------------------------------------------------------------===//
72// Attributes
73//===----------------------------------------------------------------------===//
74
75llvm::DIType *DebugTranslation::translateImpl(DINullTypeAttr attr) {
76 // A DINullTypeAttr at the beginning of the subroutine types list models
77 // a void result type. If it is at the end, it models a variadic function.
78 // Translate the explicit DINullTypeAttr to a nullptr since LLVM IR metadata
79 // does not have an explicit void result type nor a variadic type
80 // representation.
81 return nullptr;
82}
83
84llvm::DIExpression *
85DebugTranslation::getExpressionAttrOrNull(DIExpressionAttr attr) {
86 if (!attr)
87 return nullptr;
88 return translateExpression(attr);
89}
90
91llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
92 if (!stringAttr || stringAttr.empty())
93 return nullptr;
94 return llvm::MDString::get(Context&: llvmCtx, Str: stringAttr);
95}
96
97llvm::MDTuple *
98DebugTranslation::getMDTupleOrNull(ArrayRef<DINodeAttr> elements) {
99 if (elements.empty())
100 return nullptr;
101 SmallVector<llvm::Metadata *> llvmElements = llvm::to_vector(
102 Range: llvm::map_range(C&: elements, F: [&](DINodeAttr attr) -> llvm::Metadata * {
103 if (DIAnnotationAttr annAttr = dyn_cast<DIAnnotationAttr>(Val&: attr)) {
104 llvm::Metadata *ops[2] = {
105 llvm::MDString::get(Context&: llvmCtx, Str: annAttr.getName()),
106 llvm::MDString::get(Context&: llvmCtx, Str: annAttr.getValue())};
107 return llvm::MDNode::get(Context&: llvmCtx, MDs: ops);
108 }
109 return translate(attr);
110 }));
111 return llvm::MDNode::get(Context&: llvmCtx, MDs: llvmElements);
112}
113
114llvm::DIBasicType *DebugTranslation::translateImpl(DIBasicTypeAttr attr) {
115 return llvm::DIBasicType::get(
116 Context&: llvmCtx, Tag: attr.getTag(), Name: getMDStringOrNull(stringAttr: attr.getName()),
117 SizeInBits: attr.getSizeInBits(),
118 /*AlignInBits=*/0, Encoding: attr.getEncoding(), Flags: llvm::DINode::FlagZero);
119}
120
121llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
122 llvm::DIBuilder builder(llvmModule);
123 return builder.createCompileUnit(
124 Lang: attr.getSourceLanguage(), File: translate(attr: attr.getFile()),
125 Producer: attr.getProducer() ? attr.getProducer().getValue() : "",
126 isOptimized: attr.getIsOptimized(),
127 /*Flags=*/"", /*RV=*/0, /*SplitName=*/{},
128 Kind: static_cast<llvm::DICompileUnit::DebugEmissionKind>(
129 attr.getEmissionKind()),
130 DWOId: 0, SplitDebugInlining: true, DebugInfoForProfiling: false,
131 NameTableKind: static_cast<llvm::DICompileUnit::DebugNameTableKind>(
132 attr.getNameTableKind()));
133}
134
135/// Returns a new `DINodeT` that is either distinct or not, depending on
136/// `isDistinct`.
137template <class DINodeT, class... Ts>
138static DINodeT *getDistinctOrUnique(bool isDistinct, Ts &&...args) {
139 if (isDistinct)
140 return DINodeT::getDistinct(std::forward<Ts>(args)...);
141 return DINodeT::get(std::forward<Ts>(args)...);
142}
143
144llvm::TempDICompositeType
145DebugTranslation::translateTemporaryImpl(DICompositeTypeAttr attr) {
146 return llvm::DICompositeType::getTemporary(
147 Context&: llvmCtx, Tag: attr.getTag(), Name: getMDStringOrNull(stringAttr: attr.getName()), File: nullptr,
148 Line: attr.getLine(), Scope: nullptr, BaseType: nullptr, SizeInBits: attr.getSizeInBits(),
149 AlignInBits: attr.getAlignInBits(),
150 /*OffsetInBits=*/0,
151 /*Flags=*/static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
152 /*Elements=*/nullptr, /*RuntimeLang=*/0, /*EnumKind=*/std::nullopt,
153 /*VTableHolder=*/nullptr);
154}
155
156llvm::TempDISubprogram
157DebugTranslation::translateTemporaryImpl(DISubprogramAttr attr) {
158 return llvm::DISubprogram::getTemporary(
159 Context&: llvmCtx, /*Scope=*/nullptr, /*Name=*/{}, /*LinkageName=*/{},
160 /*File=*/nullptr, Line: attr.getLine(), /*Type=*/nullptr,
161 /*ScopeLine=*/0, /*ContainingType=*/nullptr, /*VirtualIndex=*/0,
162 /*ThisAdjustment=*/0, Flags: llvm::DINode::FlagZero,
163 SPFlags: static_cast<llvm::DISubprogram::DISPFlags>(attr.getSubprogramFlags()),
164 /*Unit=*/nullptr);
165}
166
167llvm::DICompositeType *
168DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
169 // TODO: Use distinct attributes to model this, once they have landed.
170 // Depending on the tag, composite types must be distinct.
171 bool isDistinct = false;
172 switch (attr.getTag()) {
173 case llvm::dwarf::DW_TAG_class_type:
174 case llvm::dwarf::DW_TAG_enumeration_type:
175 case llvm::dwarf::DW_TAG_structure_type:
176 case llvm::dwarf::DW_TAG_union_type:
177 isDistinct = true;
178 }
179
180 return getDistinctOrUnique<llvm::DICompositeType>(
181 isDistinct, args&: llvmCtx, args: attr.getTag(), args: getMDStringOrNull(stringAttr: attr.getName()),
182 args: translate(attr: attr.getFile()), args: attr.getLine(), args: translate(attr: attr.getScope()),
183 args: translate(attr: attr.getBaseType()), args: attr.getSizeInBits(),
184 args: attr.getAlignInBits(),
185 /*OffsetInBits=*/args: 0,
186 /*Flags=*/args: static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
187 args: getMDTupleOrNull(elements: attr.getElements()),
188 /*RuntimeLang=*/args: 0, /*EnumKind*/ args: std::nullopt, /*VTableHolder=*/args: nullptr,
189 /*TemplateParams=*/args: nullptr, /*Identifier=*/args: nullptr,
190 /*Discriminator=*/args: nullptr,
191 args: getExpressionAttrOrNull(attr: attr.getDataLocation()),
192 args: getExpressionAttrOrNull(attr: attr.getAssociated()),
193 args: getExpressionAttrOrNull(attr: attr.getAllocated()),
194 args: getExpressionAttrOrNull(attr: attr.getRank()));
195}
196
197llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
198 return llvm::DIDerivedType::get(
199 Context&: llvmCtx, Tag: attr.getTag(), Name: getMDStringOrNull(stringAttr: attr.getName()),
200 /*File=*/nullptr, /*Line=*/0,
201 /*Scope=*/nullptr, BaseType: translate(attr: attr.getBaseType()), SizeInBits: attr.getSizeInBits(),
202 AlignInBits: attr.getAlignInBits(), OffsetInBits: attr.getOffsetInBits(),
203 DWARFAddressSpace: attr.getDwarfAddressSpace(), /*PtrAuthData=*/std::nullopt,
204 /*Flags=*/llvm::DINode::FlagZero, ExtraData: translate(attr: attr.getExtraData()));
205}
206
207llvm::DIStringType *DebugTranslation::translateImpl(DIStringTypeAttr attr) {
208 return llvm::DIStringType::get(
209 Context&: llvmCtx, Tag: attr.getTag(), Name: getMDStringOrNull(stringAttr: attr.getName()),
210 StringLength: translate(attr: attr.getStringLength()),
211 StringLengthExp: getExpressionAttrOrNull(attr: attr.getStringLengthExp()),
212 StringLocationExp: getExpressionAttrOrNull(attr: attr.getStringLocationExp()),
213 SizeInBits: attr.getSizeInBits(), AlignInBits: attr.getAlignInBits(), Encoding: attr.getEncoding());
214}
215
216llvm::DIFile *DebugTranslation::translateImpl(DIFileAttr attr) {
217 return llvm::DIFile::get(Context&: llvmCtx, Filename: getMDStringOrNull(stringAttr: attr.getName()),
218 Directory: getMDStringOrNull(stringAttr: attr.getDirectory()));
219}
220
221llvm::DILabel *DebugTranslation::translateImpl(DILabelAttr attr) {
222 return llvm::DILabel::get(Context&: llvmCtx, Scope: translate(attr: attr.getScope()),
223 Name: getMDStringOrNull(stringAttr: attr.getName()),
224 File: translate(attr: attr.getFile()), Line: attr.getLine(),
225 /*Column=*/0, /*IsArtificial=*/false,
226 /*CoroSuspendIdx=*/std::nullopt);
227}
228
229llvm::DILexicalBlock *DebugTranslation::translateImpl(DILexicalBlockAttr attr) {
230 return llvm::DILexicalBlock::getDistinct(Context&: llvmCtx, Scope: translate(attr: attr.getScope()),
231 File: translate(attr: attr.getFile()),
232 Line: attr.getLine(), Column: attr.getColumn());
233}
234
235llvm::DILexicalBlockFile *
236DebugTranslation::translateImpl(DILexicalBlockFileAttr attr) {
237 return llvm::DILexicalBlockFile::getDistinct(
238 Context&: llvmCtx, Scope: translate(attr: attr.getScope()), File: translate(attr: attr.getFile()),
239 Discriminator: attr.getDiscriminator());
240}
241
242llvm::DILocalScope *DebugTranslation::translateImpl(DILocalScopeAttr attr) {
243 return cast<llvm::DILocalScope>(Val: translate(attr: DINodeAttr(attr)));
244}
245
246llvm::DIVariable *DebugTranslation::translateImpl(DIVariableAttr attr) {
247 return cast<llvm::DIVariable>(Val: translate(attr: DINodeAttr(attr)));
248}
249
250llvm::DILocalVariable *
251DebugTranslation::translateImpl(DILocalVariableAttr attr) {
252 return llvm::DILocalVariable::get(
253 Context&: llvmCtx, Scope: translate(attr: attr.getScope()), Name: getMDStringOrNull(stringAttr: attr.getName()),
254 File: translate(attr: attr.getFile()), Line: attr.getLine(), Type: translate(attr: attr.getType()),
255 Arg: attr.getArg(), Flags: static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
256 AlignInBits: attr.getAlignInBits(),
257 /*Annotations=*/nullptr);
258}
259
260llvm::DIGlobalVariable *
261DebugTranslation::translateImpl(DIGlobalVariableAttr attr) {
262 return llvm::DIGlobalVariable::getDistinct(
263 Context&: llvmCtx, Scope: translate(attr: attr.getScope()), Name: getMDStringOrNull(stringAttr: attr.getName()),
264 LinkageName: getMDStringOrNull(stringAttr: attr.getLinkageName()), File: translate(attr: attr.getFile()),
265 Line: attr.getLine(), Type: translate(attr: attr.getType()), IsLocalToUnit: attr.getIsLocalToUnit(),
266 IsDefinition: attr.getIsDefined(), StaticDataMemberDeclaration: nullptr, TemplateParams: nullptr, AlignInBits: attr.getAlignInBits(), Annotations: nullptr);
267}
268
269llvm::DINode *
270DebugTranslation::translateRecursive(DIRecursiveTypeAttrInterface attr) {
271 DistinctAttr recursiveId = attr.getRecId();
272 if (auto *iter = recursiveNodeMap.find(Key: recursiveId);
273 iter != recursiveNodeMap.end()) {
274 return iter->second;
275 }
276 assert(!attr.getIsRecSelf() && "unbound DI recursive self reference");
277
278 auto setRecursivePlaceholder = [&](llvm::DINode *placeholder) {
279 recursiveNodeMap.try_emplace(Key: recursiveId, Args&: placeholder);
280 };
281
282 llvm::DINode *result =
283 TypeSwitch<DIRecursiveTypeAttrInterface, llvm::DINode *>(attr)
284 .Case<DICompositeTypeAttr>(caseFn: [&](auto attr) {
285 auto temporary = translateTemporaryImpl(attr);
286 setRecursivePlaceholder(temporary.get());
287 // Must call `translateImpl` directly instead of `translate` to
288 // avoid handling the recursive interface again.
289 auto *concrete = translateImpl(attr);
290 temporary->replaceAllUsesWith(concrete);
291 return concrete;
292 })
293 .Case<DISubprogramAttr>(caseFn: [&](auto attr) {
294 auto temporary = translateTemporaryImpl(attr);
295 setRecursivePlaceholder(temporary.get());
296 // Must call `translateImpl` directly instead of `translate` to
297 // avoid handling the recursive interface again.
298 auto *concrete = translateImpl(attr);
299 temporary->replaceAllUsesWith(concrete);
300 return concrete;
301 });
302
303 assert(recursiveNodeMap.back().first == recursiveId &&
304 "internal inconsistency: unexpected recursive translation stack");
305 recursiveNodeMap.pop_back();
306
307 return result;
308}
309
310llvm::DIScope *DebugTranslation::translateImpl(DIScopeAttr attr) {
311 return cast<llvm::DIScope>(Val: translate(attr: DINodeAttr(attr)));
312}
313
314llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
315 if (auto iter = distinctAttrToNode.find(Val: attr.getId());
316 iter != distinctAttrToNode.end())
317 return cast<llvm::DISubprogram>(Val: iter->second);
318
319 llvm::DIScope *scope = translate(attr: attr.getScope());
320 llvm::DIFile *file = translate(attr: attr.getFile());
321 llvm::DIType *type = translate(attr: attr.getType());
322 llvm::DICompileUnit *compileUnit = translate(attr: attr.getCompileUnit());
323
324 // Check again after recursive calls in case this distinct node recurses back
325 // to itself.
326 if (auto iter = distinctAttrToNode.find(Val: attr.getId());
327 iter != distinctAttrToNode.end())
328 return cast<llvm::DISubprogram>(Val: iter->second);
329
330 bool isDefinition = static_cast<bool>(attr.getSubprogramFlags() &
331 LLVM::DISubprogramFlags::Definition);
332
333 llvm::DISubprogram *node = getDistinctOrUnique<llvm::DISubprogram>(
334 isDistinct: isDefinition, args&: llvmCtx, args&: scope, args: getMDStringOrNull(stringAttr: attr.getName()),
335 args: getMDStringOrNull(stringAttr: attr.getLinkageName()), args&: file, args: attr.getLine(), args&: type,
336 args: attr.getScopeLine(),
337 /*ContainingType=*/args: nullptr, /*VirtualIndex=*/args: 0,
338 /*ThisAdjustment=*/args: 0, args: llvm::DINode::FlagZero,
339 args: static_cast<llvm::DISubprogram::DISPFlags>(attr.getSubprogramFlags()),
340 args&: compileUnit, /*TemplateParams=*/args: nullptr, /*Declaration=*/args: nullptr,
341 args: getMDTupleOrNull(elements: attr.getRetainedNodes()), args: nullptr,
342 args: getMDTupleOrNull(elements: attr.getAnnotations()));
343 if (attr.getId())
344 distinctAttrToNode.try_emplace(Key: attr.getId(), Args&: node);
345 return node;
346}
347
348llvm::DIModule *DebugTranslation::translateImpl(DIModuleAttr attr) {
349 return llvm::DIModule::get(
350 Context&: llvmCtx, File: translate(attr: attr.getFile()), Scope: translate(attr: attr.getScope()),
351 Name: getMDStringOrNull(stringAttr: attr.getName()),
352 ConfigurationMacros: getMDStringOrNull(stringAttr: attr.getConfigMacros()),
353 IncludePath: getMDStringOrNull(stringAttr: attr.getIncludePath()),
354 APINotesFile: getMDStringOrNull(stringAttr: attr.getApinotes()), LineNo: attr.getLine(), IsDecl: attr.getIsDecl());
355}
356
357llvm::DINamespace *DebugTranslation::translateImpl(DINamespaceAttr attr) {
358 return llvm::DINamespace::get(Context&: llvmCtx, Scope: translate(attr: attr.getScope()),
359 Name: getMDStringOrNull(stringAttr: attr.getName()),
360 ExportSymbols: attr.getExportSymbols());
361}
362
363llvm::DIImportedEntity *
364DebugTranslation::translateImpl(DIImportedEntityAttr attr) {
365 return llvm::DIImportedEntity::get(
366 Context&: llvmCtx, Tag: attr.getTag(), Scope: translate(attr: attr.getScope()),
367 Entity: translate(attr: attr.getEntity()), File: translate(attr: attr.getFile()), Line: attr.getLine(),
368 Name: getMDStringOrNull(stringAttr: attr.getName()), Elements: getMDTupleOrNull(elements: attr.getElements()));
369}
370
371llvm::DISubrange *DebugTranslation::translateImpl(DISubrangeAttr attr) {
372 auto getMetadataOrNull = [&](Attribute attr) -> llvm::Metadata * {
373 if (!attr)
374 return nullptr;
375
376 llvm::Metadata *metadata =
377 llvm::TypeSwitch<Attribute, llvm::Metadata *>(attr)
378 .Case(caseFn: [&](IntegerAttr intAttr) {
379 return llvm::ConstantAsMetadata::get(C: llvm::ConstantInt::getSigned(
380 Ty: llvm::Type::getInt64Ty(C&: llvmCtx), V: intAttr.getInt()));
381 })
382 .Case(caseFn: [&](LLVM::DIExpressionAttr expr) {
383 return translateExpression(attr: expr);
384 })
385 .Case(caseFn: [&](LLVM::DILocalVariableAttr local) {
386 return translate(attr: local);
387 })
388 .Case<>(caseFn: [&](LLVM::DIGlobalVariableAttr global) {
389 return translate(attr: global);
390 })
391 .Default(defaultFn: [&](Attribute attr) { return nullptr; });
392 return metadata;
393 };
394 return llvm::DISubrange::get(Context&: llvmCtx, CountNode: getMetadataOrNull(attr.getCount()),
395 LowerBound: getMetadataOrNull(attr.getLowerBound()),
396 UpperBound: getMetadataOrNull(attr.getUpperBound()),
397 Stride: getMetadataOrNull(attr.getStride()));
398}
399
400llvm::DICommonBlock *DebugTranslation::translateImpl(DICommonBlockAttr attr) {
401 return llvm::DICommonBlock::get(Context&: llvmCtx, Scope: translate(attr: attr.getScope()),
402 Decl: translate(attr: attr.getDecl()),
403 Name: getMDStringOrNull(stringAttr: attr.getName()),
404 File: translate(attr: attr.getFile()), LineNo: attr.getLine());
405}
406
407llvm::DIGenericSubrange *
408DebugTranslation::translateImpl(DIGenericSubrangeAttr attr) {
409 auto getMetadataOrNull = [&](Attribute attr) -> llvm::Metadata * {
410 if (!attr)
411 return nullptr;
412
413 llvm::Metadata *metadata =
414 llvm::TypeSwitch<Attribute, llvm::Metadata *>(attr)
415 .Case(caseFn: [&](LLVM::DIExpressionAttr expr) {
416 return translateExpression(attr: expr);
417 })
418 .Case(caseFn: [&](LLVM::DILocalVariableAttr local) {
419 return translate(attr: local);
420 })
421 .Case<>(caseFn: [&](LLVM::DIGlobalVariableAttr global) {
422 return translate(attr: global);
423 })
424 .Default(defaultFn: [&](Attribute attr) { return nullptr; });
425 return metadata;
426 };
427 return llvm::DIGenericSubrange::get(Context&: llvmCtx,
428 CountNode: getMetadataOrNull(attr.getCount()),
429 LowerBound: getMetadataOrNull(attr.getLowerBound()),
430 UpperBound: getMetadataOrNull(attr.getUpperBound()),
431 Stride: getMetadataOrNull(attr.getStride()));
432}
433
434llvm::DISubroutineType *
435DebugTranslation::translateImpl(DISubroutineTypeAttr attr) {
436 // Concatenate the result and argument types into a single array.
437 SmallVector<llvm::Metadata *> types;
438 for (DITypeAttr type : attr.getTypes())
439 types.push_back(Elt: translate(attr: type));
440 return llvm::DISubroutineType::get(
441 Context&: llvmCtx, Flags: llvm::DINode::FlagZero, CC: attr.getCallingConvention(),
442 TypeArray: llvm::DITypeRefArray(llvm::MDNode::get(Context&: llvmCtx, MDs: types)));
443}
444
445llvm::DIType *DebugTranslation::translateImpl(DITypeAttr attr) {
446 return cast<llvm::DIType>(Val: translate(attr: DINodeAttr(attr)));
447}
448
449llvm::DINode *DebugTranslation::translate(DINodeAttr attr) {
450 if (!attr)
451 return nullptr;
452 // Check for a cached instance.
453 if (llvm::DINode *node = attrToNode.lookup(Val: attr))
454 return node;
455
456 llvm::DINode *node = nullptr;
457 // Recursive types go through a dedicated handler. All other types are
458 // dispatched directly to their specific handlers.
459 if (auto recTypeAttr = dyn_cast<DIRecursiveTypeAttrInterface>(Val&: attr))
460 if (recTypeAttr.getRecId())
461 node = translateRecursive(attr: recTypeAttr);
462
463 if (!node)
464 node = TypeSwitch<DINodeAttr, llvm::DINode *>(attr)
465 .Case<DIBasicTypeAttr, DICommonBlockAttr, DICompileUnitAttr,
466 DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr,
467 DIGenericSubrangeAttr, DIGlobalVariableAttr,
468 DIImportedEntityAttr, DILabelAttr, DILexicalBlockAttr,
469 DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
470 DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
471 DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
472 caseFn: [&](auto attr) { return translateImpl(attr); });
473
474 if (node && !node->isTemporary())
475 attrToNode.insert(KV: {attr, node});
476 return node;
477}
478
479//===----------------------------------------------------------------------===//
480// Locations
481//===----------------------------------------------------------------------===//
482
483/// Translate the given location to an llvm debug location.
484llvm::DILocation *DebugTranslation::translateLoc(Location loc,
485 llvm::DILocalScope *scope) {
486 if (!debugEmissionIsEnabled)
487 return nullptr;
488 return translateLoc(loc, scope, /*inlinedAt=*/nullptr);
489}
490
491llvm::DIExpression *
492DebugTranslation::translateExpression(LLVM::DIExpressionAttr attr) {
493 SmallVector<uint64_t, 1> ops;
494 if (attr) {
495 // Append operations their operands to the list.
496 for (const DIExpressionElemAttr &op : attr.getOperations()) {
497 ops.push_back(Elt: op.getOpcode());
498 append_range(C&: ops, R: op.getArguments());
499 }
500 }
501 return llvm::DIExpression::get(Context&: llvmCtx, Elements: ops);
502}
503
504llvm::DIGlobalVariableExpression *
505DebugTranslation::translateGlobalVariableExpression(
506 LLVM::DIGlobalVariableExpressionAttr attr) {
507 return llvm::DIGlobalVariableExpression::get(
508 Context&: llvmCtx, Variable: translate(attr: attr.getVar()), Expression: translateExpression(attr: attr.getExpr()));
509}
510
511/// Translate the given location to an llvm DebugLoc.
512llvm::DILocation *DebugTranslation::translateLoc(Location loc,
513 llvm::DILocalScope *scope,
514 llvm::DILocation *inlinedAt) {
515 // LLVM doesn't have a representation for unknown.
516 if (isa<UnknownLoc>(Val: loc))
517 return nullptr;
518
519 // Check for a cached instance.
520 auto existingIt = locationToLoc.find(Val: std::make_tuple(args&: loc, args&: scope, args&: inlinedAt));
521 if (existingIt != locationToLoc.end())
522 return existingIt->second;
523
524 llvm::DILocation *llvmLoc = nullptr;
525 if (auto callLoc = dyn_cast<CallSiteLoc>(Val&: loc)) {
526 // For callsites, the caller is fed as the inlinedAt for the callee.
527 auto *callerLoc = translateLoc(loc: callLoc.getCaller(), scope, inlinedAt);
528 // If the caller scope is not translatable, the overall callsite cannot be
529 // represented in LLVM (the callee scope may not match the parent function).
530 if (!callerLoc) {
531 // If there is an inlinedAt scope (an outer caller), skip to that
532 // directly. Otherwise, cannot translate.
533 if (!inlinedAt)
534 return nullptr;
535 callerLoc = inlinedAt;
536 }
537 llvmLoc = translateLoc(loc: callLoc.getCallee(), scope: nullptr, inlinedAt: callerLoc);
538 // Fallback: Ignore callee if it has no debug scope.
539 if (!llvmLoc)
540 llvmLoc = callerLoc;
541
542 } else if (auto fileLoc = dyn_cast<FileLineColLoc>(Val&: loc)) {
543 // A scope of a DILocation cannot be null.
544 if (!scope)
545 return nullptr;
546 llvmLoc =
547 llvm::DILocation::get(Context&: llvmCtx, Line: fileLoc.getLine(), Column: fileLoc.getColumn(),
548 Scope: scope, InlinedAt: const_cast<llvm::DILocation *>(inlinedAt));
549
550 } else if (auto fusedLoc = dyn_cast<FusedLoc>(Val&: loc)) {
551 ArrayRef<Location> locations = fusedLoc.getLocations();
552
553 // Check for a scope encoded with the location.
554 if (auto scopedAttr =
555 dyn_cast_or_null<LLVM::DILocalScopeAttr>(Val: fusedLoc.getMetadata()))
556 scope = translate(attr: scopedAttr);
557
558 // For fused locations, merge each of the nodes.
559 llvmLoc = translateLoc(loc: locations.front(), scope, inlinedAt);
560 for (Location locIt : locations.drop_front()) {
561 llvmLoc = llvm::DILocation::getMergedLocation(
562 LocA: llvmLoc, LocB: translateLoc(loc: locIt, scope, inlinedAt));
563 }
564
565 } else if (auto nameLoc = dyn_cast<NameLoc>(Val&: loc)) {
566 llvmLoc = translateLoc(loc: nameLoc.getChildLoc(), scope, inlinedAt);
567
568 } else if (auto opaqueLoc = dyn_cast<OpaqueLoc>(Val&: loc)) {
569 llvmLoc = translateLoc(loc: opaqueLoc.getFallbackLocation(), scope, inlinedAt);
570 } else {
571 llvm_unreachable("unknown location kind");
572 }
573
574 locationToLoc.try_emplace(Key: std::make_tuple(args&: loc, args&: scope, args&: inlinedAt), Args&: llvmLoc);
575 return llvmLoc;
576}
577
578/// Create an llvm debug file for the given file path.
579llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
580 auto *&file = fileMap[fileName];
581 if (file)
582 return file;
583
584 // Make sure the current working directory is up-to-date.
585 if (currentWorkingDir.empty())
586 llvm::sys::fs::current_path(result&: currentWorkingDir);
587
588 StringRef directory = currentWorkingDir;
589 SmallString<128> dirBuf;
590 SmallString<128> fileBuf;
591 if (llvm::sys::path::is_absolute(path: fileName)) {
592 // Strip the common prefix (if it is more than just "/") from current
593 // directory and FileName for a more space-efficient encoding.
594 auto fileIt = llvm::sys::path::begin(path: fileName);
595 auto fileE = llvm::sys::path::end(path: fileName);
596 auto curDirIt = llvm::sys::path::begin(path: directory);
597 auto curDirE = llvm::sys::path::end(path: directory);
598 for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
599 llvm::sys::path::append(path&: dirBuf, a: *curDirIt);
600 if (std::distance(first: llvm::sys::path::begin(path: directory), last: curDirIt) == 1) {
601 // Don't strip the common prefix if it is only the root "/" since that
602 // would make LLVM diagnostic locations confusing.
603 directory = StringRef();
604 } else {
605 for (; fileIt != fileE; ++fileIt)
606 llvm::sys::path::append(path&: fileBuf, a: *fileIt);
607 directory = dirBuf;
608 fileName = fileBuf;
609 }
610 }
611 return (file = llvm::DIFile::get(Context&: llvmCtx, Filename: fileName, Directory: directory));
612}
613

source code of mlir/lib/Target/LLVMIR/DebugTranslation.cpp