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