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 // TODO: The version information should be encoded on the LLVM module itself,
37 // not implicitly set here.
38
39 // Mark this module as having debug information.
40 StringRef debugVersionKey = "Debug Info Version";
41 if (!llvmModule.getModuleFlag(Key: debugVersionKey))
42 llvmModule.addModuleFlag(Behavior: llvm::Module::Warning, Key: debugVersionKey,
43 Val: llvm::DEBUG_METADATA_VERSION);
44
45 if (auto targetTripleAttr = module->getDiscardableAttr(
46 LLVM::LLVMDialect::getTargetTripleAttrName())) {
47 auto targetTriple =
48 llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue());
49 if (targetTriple.isKnownWindowsMSVCEnvironment()) {
50 // Dwarf debugging files will be generated by default, unless "CodeView"
51 // is set explicitly. Windows/MSVC should use CodeView instead.
52 llvmModule.addModuleFlag(Behavior: llvm::Module::Warning, Key: "CodeView", Val: 1);
53 }
54 }
55}
56
57/// Finalize the translation of debug information.
58void DebugTranslation::finalize() {}
59
60/// Translate the debug information for the given function.
61void DebugTranslation::translate(LLVMFuncOp func, llvm::Function &llvmFunc) {
62 if (!debugEmissionIsEnabled)
63 return;
64
65 // Look for a sub program attached to the function.
66 auto spLoc =
67 func.getLoc()->findInstanceOf<FusedLocWith<LLVM::DISubprogramAttr>>();
68 if (!spLoc)
69 return;
70 llvmFunc.setSubprogram(translate(spLoc.getMetadata()));
71}
72
73//===----------------------------------------------------------------------===//
74// Attributes
75//===----------------------------------------------------------------------===//
76
77llvm::DIType *DebugTranslation::translateImpl(DINullTypeAttr attr) {
78 // A DINullTypeAttr at the beginning of the subroutine types list models
79 // a void result type. If it is at the end, it models a variadic function.
80 // Translate the explicit DINullTypeAttr to a nullptr since LLVM IR metadata
81 // does not have an explicit void result type nor a variadic type
82 // representation.
83 return nullptr;
84}
85
86llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
87 if (!stringAttr || stringAttr.empty())
88 return nullptr;
89 return llvm::MDString::get(llvmCtx, stringAttr);
90}
91
92llvm::DIBasicType *DebugTranslation::translateImpl(DIBasicTypeAttr attr) {
93 return llvm::DIBasicType::get(
94 llvmCtx, attr.getTag(), getMDStringOrNull(stringAttr: attr.getName()),
95 attr.getSizeInBits(),
96 /*AlignInBits=*/0, attr.getEncoding(), llvm::DINode::FlagZero);
97}
98
99llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
100 llvm::DIBuilder builder(llvmModule);
101 return builder.createCompileUnit(
102 Lang: attr.getSourceLanguage(), File: translate(attr.getFile()),
103 Producer: attr.getProducer() ? attr.getProducer().getValue() : "",
104 isOptimized: attr.getIsOptimized(),
105 /*Flags=*/"", /*RV=*/0, /*SplitName=*/{},
106 Kind: static_cast<llvm::DICompileUnit::DebugEmissionKind>(
107 attr.getEmissionKind()),
108 DWOId: 0, SplitDebugInlining: true, DebugInfoForProfiling: false,
109 NameTableKind: static_cast<llvm::DICompileUnit::DebugNameTableKind>(
110 attr.getNameTableKind()));
111}
112
113/// Returns a new `DINodeT` that is either distinct or not, depending on
114/// `isDistinct`.
115template <class DINodeT, class... Ts>
116static DINodeT *getDistinctOrUnique(bool isDistinct, Ts &&...args) {
117 if (isDistinct)
118 return DINodeT::getDistinct(std::forward<Ts>(args)...);
119 return DINodeT::get(std::forward<Ts>(args)...);
120}
121
122llvm::TempDICompositeType
123DebugTranslation::translateTemporaryImpl(DICompositeTypeAttr attr) {
124 return llvm::DICompositeType::getTemporary(
125 llvmCtx, attr.getTag(), getMDStringOrNull(stringAttr: attr.getName()), nullptr,
126 attr.getLine(), nullptr, nullptr, attr.getSizeInBits(),
127 attr.getAlignInBits(),
128 /*OffsetInBits=*/0,
129 /*Flags=*/static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
130 /*Elements=*/nullptr, /*RuntimeLang=*/0,
131 /*VTableHolder=*/nullptr);
132}
133
134llvm::DICompositeType *
135DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
136 // TODO: Use distinct attributes to model this, once they have landed.
137 // Depending on the tag, composite types must be distinct.
138 bool isDistinct = false;
139 switch (attr.getTag()) {
140 case llvm::dwarf::DW_TAG_class_type:
141 case llvm::dwarf::DW_TAG_enumeration_type:
142 case llvm::dwarf::DW_TAG_structure_type:
143 case llvm::dwarf::DW_TAG_union_type:
144 isDistinct = true;
145 }
146
147 SmallVector<llvm::Metadata *> elements;
148 for (DINodeAttr member : attr.getElements())
149 elements.push_back(translate(member));
150
151 return getDistinctOrUnique<llvm::DICompositeType>(
152 isDistinct, llvmCtx, attr.getTag(), getMDStringOrNull(stringAttr: attr.getName()),
153 translate(attr.getFile()), attr.getLine(), translate(attr.getScope()),
154 translate(attr.getBaseType()), attr.getSizeInBits(),
155 attr.getAlignInBits(),
156 /*OffsetInBits=*/0,
157 /*Flags=*/static_cast<llvm::DINode::DIFlags>(attr.getFlags()),
158 llvm::MDNode::get(Context&: llvmCtx, MDs: elements),
159 /*RuntimeLang=*/0, /*VTableHolder=*/nullptr);
160}
161
162llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
163 return llvm::DIDerivedType::get(
164 llvmCtx, attr.getTag(), getMDStringOrNull(stringAttr: attr.getName()),
165 /*File=*/nullptr, /*Line=*/0,
166 /*Scope=*/nullptr, translate(attr.getBaseType()), attr.getSizeInBits(),
167 attr.getAlignInBits(), attr.getOffsetInBits(),
168 /*DWARFAddressSpace=*/std::nullopt, /*PtrAuthData=*/std::nullopt,
169 /*Flags=*/llvm::DINode::FlagZero, translate(attr.getExtraData()));
170}
171
172llvm::DIFile *DebugTranslation::translateImpl(DIFileAttr attr) {
173 return llvm::DIFile::get(llvmCtx, getMDStringOrNull(stringAttr: attr.getName()),
174 getMDStringOrNull(stringAttr: attr.getDirectory()));
175}
176
177llvm::DILabel *DebugTranslation::translateImpl(DILabelAttr attr) {
178 return llvm::DILabel::get(llvmCtx, translate(attr.getScope()),
179 getMDStringOrNull(stringAttr: attr.getName()),
180 translate(attr.getFile()), attr.getLine());
181}
182
183llvm::DILexicalBlock *DebugTranslation::translateImpl(DILexicalBlockAttr attr) {
184 return llvm::DILexicalBlock::getDistinct(llvmCtx, translate(attr.getScope()),
185 translate(attr.getFile()),
186 attr.getLine(), attr.getColumn());
187}
188
189llvm::DILexicalBlockFile *
190DebugTranslation::translateImpl(DILexicalBlockFileAttr attr) {
191 return llvm::DILexicalBlockFile::getDistinct(
192 llvmCtx, translate(attr.getScope()), translate(attr.getFile()),
193 attr.getDiscriminator());
194}
195
196llvm::DILocalScope *DebugTranslation::translateImpl(DILocalScopeAttr attr) {
197 return cast<llvm::DILocalScope>(Val: translate(attr: DINodeAttr(attr)));
198}
199
200llvm::DILocalVariable *
201DebugTranslation::translateImpl(DILocalVariableAttr attr) {
202 return llvm::DILocalVariable::get(
203 llvmCtx, translate(attr.getScope()), getMDStringOrNull(stringAttr: attr.getName()),
204 translate(attr.getFile()), attr.getLine(), translate(attr.getType()),
205 attr.getArg(),
206 /*Flags=*/llvm::DINode::FlagZero, attr.getAlignInBits(),
207 /*Annotations=*/nullptr);
208}
209
210llvm::DIGlobalVariable *
211DebugTranslation::translateImpl(DIGlobalVariableAttr attr) {
212 return llvm::DIGlobalVariable::getDistinct(
213 llvmCtx, translate(attr.getScope()), getMDStringOrNull(stringAttr: attr.getName()),
214 getMDStringOrNull(stringAttr: attr.getLinkageName()), translate(attr.getFile()),
215 attr.getLine(), translate(attr.getType()), attr.getIsLocalToUnit(),
216 attr.getIsDefined(), nullptr, nullptr, attr.getAlignInBits(), nullptr);
217}
218
219llvm::DIType *
220DebugTranslation::translateRecursive(DIRecursiveTypeAttrInterface attr) {
221 DistinctAttr recursiveId = attr.getRecId();
222 if (auto *iter = recursiveTypeMap.find(Key: recursiveId);
223 iter != recursiveTypeMap.end()) {
224 return iter->second;
225 } else {
226 assert(!attr.isRecSelf() && "unbound DI recursive self type");
227 }
228
229 auto setRecursivePlaceholder = [&](llvm::DIType *placeholder) {
230 recursiveTypeMap.try_emplace(Key: recursiveId, Args&: placeholder);
231 };
232
233 llvm::DIType *result =
234 TypeSwitch<DIRecursiveTypeAttrInterface, llvm::DIType *>(attr)
235 .Case<DICompositeTypeAttr>([&](auto attr) {
236 auto temporary = translateTemporaryImpl(attr);
237 setRecursivePlaceholder(temporary.get());
238 // Must call `translateImpl` directly instead of `translate` to
239 // avoid handling the recursive interface again.
240 auto *concrete = translateImpl(attr);
241 temporary->replaceAllUsesWith(concrete);
242 return concrete;
243 });
244
245 assert(recursiveTypeMap.back().first == recursiveId &&
246 "internal inconsistency: unexpected recursive translation stack");
247 recursiveTypeMap.pop_back();
248
249 return result;
250}
251
252llvm::DIScope *DebugTranslation::translateImpl(DIScopeAttr attr) {
253 return cast<llvm::DIScope>(Val: translate(attr: DINodeAttr(attr)));
254}
255
256llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
257 if (auto iter = distinctAttrToNode.find(attr.getId());
258 iter != distinctAttrToNode.end())
259 return cast<llvm::DISubprogram>(iter->second);
260
261 llvm::DIScope *scope = translate(attr.getScope());
262 llvm::DIFile *file = translate(attr.getFile());
263 llvm::DIType *type = translate(attr.getType());
264 llvm::DICompileUnit *compileUnit = translate(attr.getCompileUnit());
265
266 // Check again after recursive calls in case this distinct node recurses back
267 // to itself.
268 if (auto iter = distinctAttrToNode.find(attr.getId());
269 iter != distinctAttrToNode.end())
270 return cast<llvm::DISubprogram>(iter->second);
271
272 bool isDefinition = static_cast<bool>(attr.getSubprogramFlags() &
273 LLVM::DISubprogramFlags::Definition);
274 llvm::DISubprogram *node = getDistinctOrUnique<llvm::DISubprogram>(
275 isDefinition, llvmCtx, scope, getMDStringOrNull(stringAttr: attr.getName()),
276 getMDStringOrNull(stringAttr: attr.getLinkageName()), file, attr.getLine(), type,
277 attr.getScopeLine(),
278 /*ContainingType=*/nullptr, /*VirtualIndex=*/0,
279 /*ThisAdjustment=*/0, llvm::DINode::FlagZero,
280 static_cast<llvm::DISubprogram::DISPFlags>(attr.getSubprogramFlags()),
281 compileUnit);
282
283 if (attr.getId())
284 distinctAttrToNode.try_emplace(attr.getId(), node);
285 return node;
286}
287
288llvm::DIModule *DebugTranslation::translateImpl(DIModuleAttr attr) {
289 return llvm::DIModule::get(
290 llvmCtx, translate(attr.getFile()), translate(attr.getScope()),
291 getMDStringOrNull(stringAttr: attr.getName()),
292 getMDStringOrNull(stringAttr: attr.getConfigMacros()),
293 getMDStringOrNull(stringAttr: attr.getIncludePath()),
294 getMDStringOrNull(stringAttr: attr.getApinotes()), attr.getLine(), attr.getIsDecl());
295}
296
297llvm::DINamespace *DebugTranslation::translateImpl(DINamespaceAttr attr) {
298 return llvm::DINamespace::get(llvmCtx, translate(attr.getScope()),
299 getMDStringOrNull(stringAttr: attr.getName()),
300 attr.getExportSymbols());
301}
302
303llvm::DISubrange *DebugTranslation::translateImpl(DISubrangeAttr attr) {
304 auto getMetadataOrNull = [&](IntegerAttr attr) -> llvm::Metadata * {
305 if (!attr)
306 return nullptr;
307 return llvm::ConstantAsMetadata::get(C: llvm::ConstantInt::getSigned(
308 llvm::Type::getInt64Ty(C&: llvmCtx), attr.getInt()));
309 };
310 return llvm::DISubrange::get(llvmCtx, getMetadataOrNull(attr.getCount()),
311 getMetadataOrNull(attr.getLowerBound()),
312 getMetadataOrNull(attr.getUpperBound()),
313 getMetadataOrNull(attr.getStride()));
314}
315
316llvm::DISubroutineType *
317DebugTranslation::translateImpl(DISubroutineTypeAttr attr) {
318 // Concatenate the result and argument types into a single array.
319 SmallVector<llvm::Metadata *> types;
320 for (DITypeAttr type : attr.getTypes())
321 types.push_back(translate(type));
322 return llvm::DISubroutineType::get(
323 llvmCtx, llvm::DINode::FlagZero, attr.getCallingConvention(),
324 llvm::DITypeRefArray(llvm::MDNode::get(Context&: llvmCtx, MDs: types)));
325}
326
327llvm::DIType *DebugTranslation::translateImpl(DITypeAttr attr) {
328 return cast<llvm::DIType>(Val: translate(attr: DINodeAttr(attr)));
329}
330
331llvm::DINode *DebugTranslation::translate(DINodeAttr attr) {
332 if (!attr)
333 return nullptr;
334 // Check for a cached instance.
335 if (llvm::DINode *node = attrToNode.lookup(Val: attr))
336 return node;
337
338 llvm::DINode *node = nullptr;
339 // Recursive types go through a dedicated handler. All other types are
340 // dispatched directly to their specific handlers.
341 if (auto recTypeAttr = dyn_cast<DIRecursiveTypeAttrInterface>(attr))
342 if (recTypeAttr.getRecId())
343 node = translateRecursive(recTypeAttr);
344
345 if (!node)
346 node = TypeSwitch<DINodeAttr, llvm::DINode *>(attr)
347 .Case<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
348 DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
349 DILabelAttr, DILexicalBlockAttr, DILexicalBlockFileAttr,
350 DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
351 DINullTypeAttr, DISubprogramAttr, DISubrangeAttr,
352 DISubroutineTypeAttr>(
353 [&](auto attr) { return translateImpl(attr); });
354
355 if (node && !node->isTemporary())
356 attrToNode.insert(KV: {attr, node});
357 return node;
358}
359
360//===----------------------------------------------------------------------===//
361// Locations
362//===----------------------------------------------------------------------===//
363
364/// Translate the given location to an llvm debug location.
365llvm::DILocation *DebugTranslation::translateLoc(Location loc,
366 llvm::DILocalScope *scope) {
367 if (!debugEmissionIsEnabled)
368 return nullptr;
369 return translateLoc(loc, scope, /*inlinedAt=*/nullptr);
370}
371
372llvm::DIExpression *
373DebugTranslation::translateExpression(LLVM::DIExpressionAttr attr) {
374 SmallVector<uint64_t, 1> ops;
375 if (attr) {
376 // Append operations their operands to the list.
377 for (const DIExpressionElemAttr &op : attr.getOperations()) {
378 ops.push_back(op.getOpcode());
379 append_range(ops, op.getArguments());
380 }
381 }
382 return llvm::DIExpression::get(Context&: llvmCtx, Elements: ops);
383}
384
385llvm::DIGlobalVariableExpression *
386DebugTranslation::translateGlobalVariableExpression(
387 LLVM::DIGlobalVariableExpressionAttr attr) {
388 return llvm::DIGlobalVariableExpression::get(
389 llvmCtx, translate(attr.getVar()), translateExpression(attr.getExpr()));
390}
391
392/// Translate the given location to an llvm DebugLoc.
393llvm::DILocation *DebugTranslation::translateLoc(Location loc,
394 llvm::DILocalScope *scope,
395 llvm::DILocation *inlinedAt) {
396 // LLVM doesn't have a representation for unknown.
397 if (isa<UnknownLoc>(Val: loc))
398 return nullptr;
399
400 // Check for a cached instance.
401 auto existingIt = locationToLoc.find(Val: std::make_tuple(args&: loc, args&: scope, args&: inlinedAt));
402 if (existingIt != locationToLoc.end())
403 return existingIt->second;
404
405 llvm::DILocation *llvmLoc = nullptr;
406 if (auto callLoc = dyn_cast<CallSiteLoc>(loc)) {
407 // For callsites, the caller is fed as the inlinedAt for the callee.
408 auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
409 llvmLoc = translateLoc(callLoc.getCallee(), nullptr, callerLoc);
410 // Fallback: Ignore callee if it has no debug scope.
411 if (!llvmLoc)
412 llvmLoc = callerLoc;
413
414 } else if (auto fileLoc = dyn_cast<FileLineColLoc>(loc)) {
415 // A scope of a DILocation cannot be null.
416 if (!scope)
417 return nullptr;
418 llvmLoc =
419 llvm::DILocation::get(llvmCtx, fileLoc.getLine(), fileLoc.getColumn(),
420 scope, const_cast<llvm::DILocation *>(inlinedAt));
421
422 } else if (auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
423 ArrayRef<Location> locations = fusedLoc.getLocations();
424
425 // Check for a scope encoded with the location.
426 if (auto scopedAttr =
427 dyn_cast_or_null<LLVM::DILocalScopeAttr>(fusedLoc.getMetadata()))
428 scope = translate(scopedAttr);
429
430 // For fused locations, merge each of the nodes.
431 llvmLoc = translateLoc(loc: locations.front(), scope, inlinedAt);
432 for (Location locIt : locations.drop_front()) {
433 llvmLoc = llvm::DILocation::getMergedLocation(
434 llvmLoc, translateLoc(locIt, scope, inlinedAt));
435 }
436
437 } else if (auto nameLoc = dyn_cast<NameLoc>(loc)) {
438 llvmLoc = translateLoc(nameLoc.getChildLoc(), scope, inlinedAt);
439
440 } else if (auto opaqueLoc = dyn_cast<OpaqueLoc>(loc)) {
441 llvmLoc = translateLoc(opaqueLoc.getFallbackLocation(), scope, inlinedAt);
442 } else {
443 llvm_unreachable("unknown location kind");
444 }
445
446 locationToLoc.try_emplace(Key: std::make_tuple(args&: loc, args&: scope, args&: inlinedAt), Args&: llvmLoc);
447 return llvmLoc;
448}
449
450/// Create an llvm debug file for the given file path.
451llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
452 auto *&file = fileMap[fileName];
453 if (file)
454 return file;
455
456 // Make sure the current working directory is up-to-date.
457 if (currentWorkingDir.empty())
458 llvm::sys::fs::current_path(result&: currentWorkingDir);
459
460 StringRef directory = currentWorkingDir;
461 SmallString<128> dirBuf;
462 SmallString<128> fileBuf;
463 if (llvm::sys::path::is_absolute(path: fileName)) {
464 // Strip the common prefix (if it is more than just "/") from current
465 // directory and FileName for a more space-efficient encoding.
466 auto fileIt = llvm::sys::path::begin(path: fileName);
467 auto fileE = llvm::sys::path::end(path: fileName);
468 auto curDirIt = llvm::sys::path::begin(path: directory);
469 auto curDirE = llvm::sys::path::end(path: directory);
470 for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
471 llvm::sys::path::append(path&: dirBuf, a: *curDirIt);
472 if (std::distance(first: llvm::sys::path::begin(path: directory), last: curDirIt) == 1) {
473 // Don't strip the common prefix if it is only the root "/" since that
474 // would make LLVM diagnostic locations confusing.
475 directory = StringRef();
476 } else {
477 for (; fileIt != fileE; ++fileIt)
478 llvm::sys::path::append(path&: fileBuf, a: *fileIt);
479 directory = dirBuf;
480 fileName = fileBuf;
481 }
482 }
483 return (file = llvm::DIFile::get(Context&: llvmCtx, Filename: fileName, Directory: directory));
484}
485

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