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