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"
29 : debugEmissionIsEnabled(false), llvmModule(llvmModule),
34 debugEmissionIsEnabled =
true;
40 StringRef debugVersionKey =
"Debug Info Version";
41 if (!llvmModule.getModuleFlag(debugVersionKey))
42 llvmModule.addModuleFlag(llvm::Module::Warning, debugVersionKey,
43 llvm::DEBUG_METADATA_VERSION);
46 LLVM::LLVMDialect::getTargetTripleAttrName())) {
48 llvm::Triple(cast<StringAttr>(targetTripleAttr).getValue());
49 if (targetTriple.isKnownWindowsMSVCEnvironment()) {
52 llvmModule.addModuleFlag(llvm::Module::Warning,
"CodeView", 1);
62 if (!debugEmissionIsEnabled)
70 llvmFunc.setSubprogram(
translate(spLoc.getMetadata()));
77 llvm::DIType *DebugTranslation::translateImpl(DINullTypeAttr attr) {
87 DebugTranslation::getExpressionAttrOrNull(DIExpressionAttr attr) {
93 llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
94 if (!stringAttr || stringAttr.empty())
99 llvm::DIBasicType *DebugTranslation::translateImpl(DIBasicTypeAttr attr) {
101 llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
102 attr.getSizeInBits(),
103 0, attr.getEncoding(), llvm::DINode::FlagZero);
106 llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
107 llvm::DIBuilder builder(llvmModule);
108 return builder.createCompileUnit(
109 attr.getSourceLanguage(),
translate(attr.getFile()),
110 attr.getProducer() ? attr.getProducer().getValue() :
"",
111 attr.getIsOptimized(),
113 static_cast<llvm::DICompileUnit::DebugEmissionKind
>(
114 attr.getEmissionKind()),
116 static_cast<llvm::DICompileUnit::DebugNameTableKind
>(
117 attr.getNameTableKind()));
122 template <
class DINodeT,
class... Ts>
125 return DINodeT::getDistinct(std::forward<Ts>(args)...);
129 llvm::TempDICompositeType
130 DebugTranslation::translateTemporaryImpl(DICompositeTypeAttr attr) {
131 return llvm::DICompositeType::getTemporary(
132 llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
nullptr,
133 attr.getLine(),
nullptr,
nullptr, attr.getSizeInBits(),
134 attr.getAlignInBits(),
136 static_cast<llvm::DINode::DIFlags
>(attr.getFlags()),
141 llvm::DICompositeType *
142 DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
145 bool isDistinct =
false;
146 switch (attr.getTag()) {
147 case llvm::dwarf::DW_TAG_class_type:
148 case llvm::dwarf::DW_TAG_enumeration_type:
149 case llvm::dwarf::DW_TAG_structure_type:
150 case llvm::dwarf::DW_TAG_union_type:
158 return getDistinctOrUnique<llvm::DICompositeType>(
159 isDistinct, llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
161 translate(attr.getBaseType()), attr.getSizeInBits(),
162 attr.getAlignInBits(),
164 static_cast<llvm::DINode::DIFlags
>(attr.getFlags()),
169 getExpressionAttrOrNull(attr.getDataLocation()),
170 getExpressionAttrOrNull(attr.getAssociated()),
171 getExpressionAttrOrNull(attr.getAllocated()),
172 getExpressionAttrOrNull(attr.getRank()));
175 llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
177 llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
179 nullptr,
translate(attr.getBaseType()), attr.getSizeInBits(),
180 attr.getAlignInBits(), attr.getOffsetInBits(),
181 attr.getDwarfAddressSpace(), std::nullopt,
182 llvm::DINode::FlagZero,
translate(attr.getExtraData()));
185 llvm::DIStringType *DebugTranslation::translateImpl(DIStringTypeAttr attr) {
187 llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
189 getExpressionAttrOrNull(attr.getStringLengthExp()),
190 getExpressionAttrOrNull(attr.getStringLocationExp()),
191 attr.getSizeInBits(), attr.getAlignInBits(), attr.getEncoding());
194 llvm::DIFile *DebugTranslation::translateImpl(DIFileAttr attr) {
196 getMDStringOrNull(attr.getDirectory()));
199 llvm::DILabel *DebugTranslation::translateImpl(DILabelAttr attr) {
201 getMDStringOrNull(attr.getName()),
202 translate(attr.getFile()), attr.getLine());
205 llvm::DILexicalBlock *DebugTranslation::translateImpl(DILexicalBlockAttr attr) {
206 return llvm::DILexicalBlock::getDistinct(llvmCtx,
translate(attr.getScope()),
208 attr.getLine(), attr.getColumn());
211 llvm::DILexicalBlockFile *
212 DebugTranslation::translateImpl(DILexicalBlockFileAttr attr) {
213 return llvm::DILexicalBlockFile::getDistinct(
215 attr.getDiscriminator());
218 llvm::DILocalScope *DebugTranslation::translateImpl(
DILocalScopeAttr attr) {
222 llvm::DIVariable *DebugTranslation::translateImpl(
DIVariableAttr attr) {
226 llvm::DILocalVariable *
227 DebugTranslation::translateImpl(DILocalVariableAttr attr) {
229 llvmCtx,
translate(attr.getScope()), getMDStringOrNull(attr.getName()),
231 attr.getArg(),
static_cast<llvm::DINode::DIFlags
>(attr.getFlags()),
232 attr.getAlignInBits(),
236 llvm::DIGlobalVariable *
237 DebugTranslation::translateImpl(DIGlobalVariableAttr attr) {
238 return llvm::DIGlobalVariable::getDistinct(
239 llvmCtx,
translate(attr.getScope()), getMDStringOrNull(attr.getName()),
240 getMDStringOrNull(attr.getLinkageName()),
translate(attr.getFile()),
241 attr.getLine(),
translate(attr.getType()), attr.getIsLocalToUnit(),
242 attr.getIsDefined(),
nullptr,
nullptr, attr.getAlignInBits(),
nullptr);
246 DebugTranslation::translateRecursive(DIRecursiveTypeAttrInterface attr) {
248 if (
auto *iter = recursiveTypeMap.find(recursiveId);
249 iter != recursiveTypeMap.end()) {
252 assert(!attr.isRecSelf() &&
"unbound DI recursive self type");
255 auto setRecursivePlaceholder = [&](llvm::DIType *placeholder) {
256 recursiveTypeMap.try_emplace(recursiveId, placeholder);
259 llvm::DIType *result =
261 .Case<DICompositeTypeAttr>([&](
auto attr) {
262 auto temporary = translateTemporaryImpl(attr);
263 setRecursivePlaceholder(temporary.get());
266 auto *concrete = translateImpl(attr);
267 temporary->replaceAllUsesWith(concrete);
271 assert(recursiveTypeMap.back().first == recursiveId &&
272 "internal inconsistency: unexpected recursive translation stack");
273 recursiveTypeMap.pop_back();
278 llvm::DIScope *DebugTranslation::translateImpl(
DIScopeAttr attr) {
282 llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
283 if (
auto iter = distinctAttrToNode.find(attr.getId());
284 iter != distinctAttrToNode.end())
285 return cast<llvm::DISubprogram>(iter->second);
287 llvm::DIScope *scope =
translate(attr.getScope());
288 llvm::DIFile *file =
translate(attr.getFile());
289 llvm::DIType *type =
translate(attr.getType());
290 llvm::DICompileUnit *compileUnit =
translate(attr.getCompileUnit());
294 if (
auto iter = distinctAttrToNode.find(attr.getId());
295 iter != distinctAttrToNode.end())
296 return cast<llvm::DISubprogram>(iter->second);
298 bool isDefinition =
static_cast<bool>(attr.getSubprogramFlags() &
299 LLVM::DISubprogramFlags::Definition);
300 llvm::DISubprogram *node = getDistinctOrUnique<llvm::DISubprogram>(
301 isDefinition, llvmCtx, scope, getMDStringOrNull(attr.getName()),
302 getMDStringOrNull(attr.getLinkageName()), file, attr.getLine(), type,
305 0, llvm::DINode::FlagZero,
306 static_cast<llvm::DISubprogram::DISPFlags
>(attr.getSubprogramFlags()),
310 distinctAttrToNode.try_emplace(attr.getId(), node);
314 llvm::DIModule *DebugTranslation::translateImpl(DIModuleAttr attr) {
317 getMDStringOrNull(attr.getName()),
318 getMDStringOrNull(attr.getConfigMacros()),
319 getMDStringOrNull(attr.getIncludePath()),
320 getMDStringOrNull(attr.getApinotes()), attr.getLine(), attr.getIsDecl());
323 llvm::DINamespace *DebugTranslation::translateImpl(DINamespaceAttr attr) {
325 getMDStringOrNull(attr.getName()),
326 attr.getExportSymbols());
329 llvm::DISubrange *DebugTranslation::translateImpl(DISubrangeAttr attr) {
330 auto getMetadataOrNull = [&](
Attribute attr) -> llvm::Metadata * {
334 llvm::Metadata *metadata =
336 .Case([&](IntegerAttr intAttr) {
338 llvm::Type::getInt64Ty(llvmCtx), intAttr.getInt()));
340 .Case([&](LLVM::DIExpressionAttr expr) {
343 .Case([&](LLVM::DILocalVariableAttr local) {
346 .Case<>([&](LLVM::DIGlobalVariableAttr global) {
349 .Default([&](
Attribute attr) {
return nullptr; });
353 getMetadataOrNull(attr.getLowerBound()),
354 getMetadataOrNull(attr.getUpperBound()),
355 getMetadataOrNull(attr.getStride()));
358 llvm::DISubroutineType *
359 DebugTranslation::translateImpl(DISubroutineTypeAttr attr) {
365 llvmCtx, llvm::DINode::FlagZero, attr.getCallingConvention(),
369 llvm::DIType *DebugTranslation::translateImpl(
DITypeAttr attr) {
377 if (llvm::DINode *node = attrToNode.lookup(attr))
380 llvm::DINode *node =
nullptr;
383 if (
auto recTypeAttr = dyn_cast<DIRecursiveTypeAttrInterface>(attr))
384 if (recTypeAttr.getRecId())
385 node = translateRecursive(recTypeAttr);
389 .Case<DIBasicTypeAttr, DICompileUnitAttr, DICompositeTypeAttr,
390 DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
391 DILabelAttr, DILexicalBlockAttr, DILexicalBlockFileAttr,
392 DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
393 DINullTypeAttr, DIStringTypeAttr, DISubprogramAttr,
394 DISubrangeAttr, DISubroutineTypeAttr>(
395 [&](
auto attr) {
return translateImpl(attr); });
397 if (node && !node->isTemporary())
398 attrToNode.insert({attr, node});
407 llvm::DILocation *DebugTranslation::translateLoc(
Location loc,
408 llvm::DILocalScope *scope) {
409 if (!debugEmissionIsEnabled)
411 return translateLoc(loc, scope,
nullptr);
415 DebugTranslation::translateExpression(LLVM::DIExpressionAttr attr) {
419 for (
const DIExpressionElemAttr &op : attr.getOperations()) {
420 ops.push_back(op.getOpcode());
421 append_range(ops, op.getArguments());
427 llvm::DIGlobalVariableExpression *
428 DebugTranslation::translateGlobalVariableExpression(
429 LLVM::DIGlobalVariableExpressionAttr attr) {
431 llvmCtx, translate(attr.getVar()), translateExpression(attr.getExpr()));
435 llvm::DILocation *DebugTranslation::translateLoc(
Location loc,
436 llvm::DILocalScope *scope,
437 llvm::DILocation *inlinedAt) {
439 if (isa<UnknownLoc>(loc))
443 auto existingIt = locationToLoc.find(std::make_tuple(loc, scope, inlinedAt));
444 if (existingIt != locationToLoc.end())
445 return existingIt->second;
447 llvm::DILocation *llvmLoc =
nullptr;
448 if (
auto callLoc = dyn_cast<CallSiteLoc>(loc)) {
450 auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
458 callerLoc = inlinedAt;
460 llvmLoc = translateLoc(callLoc.getCallee(),
nullptr, callerLoc);
465 }
else if (
auto fileLoc = dyn_cast<FileLineColLoc>(loc)) {
471 scope,
const_cast<llvm::DILocation *
>(inlinedAt));
473 }
else if (
auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
477 if (
auto scopedAttr =
478 dyn_cast_or_null<LLVM::DILocalScopeAttr>(fusedLoc.getMetadata()))
479 scope = translate(scopedAttr);
482 llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
483 for (
Location locIt : locations.drop_front()) {
484 llvmLoc = llvm::DILocation::getMergedLocation(
485 llvmLoc, translateLoc(locIt, scope, inlinedAt));
488 }
else if (
auto nameLoc = dyn_cast<NameLoc>(loc)) {
489 llvmLoc = translateLoc(nameLoc.getChildLoc(), scope, inlinedAt);
491 }
else if (
auto opaqueLoc = dyn_cast<OpaqueLoc>(loc)) {
492 llvmLoc = translateLoc(opaqueLoc.getFallbackLocation(), scope, inlinedAt);
494 llvm_unreachable(
"unknown location kind");
497 locationToLoc.try_emplace(std::make_tuple(loc, scope, inlinedAt), llvmLoc);
502 llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
503 auto *&file = fileMap[fileName];
508 if (currentWorkingDir.empty())
509 llvm::sys::fs::current_path(currentWorkingDir);
511 StringRef directory = currentWorkingDir;
514 if (llvm::sys::path::is_absolute(fileName)) {
517 auto fileIt = llvm::sys::path::begin(fileName);
518 auto fileE = llvm::sys::path::end(fileName);
519 auto curDirIt = llvm::sys::path::begin(directory);
520 auto curDirE = llvm::sys::path::end(directory);
521 for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
522 llvm::sys::path::append(dirBuf, *curDirIt);
523 if (std::distance(llvm::sys::path::begin(directory), curDirIt) == 1) {
526 directory = StringRef();
528 for (; fileIt != fileE; ++fileIt)
529 llvm::sys::path::append(fileBuf, *fileIt);
static DINodeT * getDistinctOrUnique(bool isDistinct, Ts &&...args)
Returns a new DINodeT that is either distinct or not, depending on isDistinct.
static WalkResult interruptIfValidLocation(Operation *op)
A utility walker that interrupts if the operation has valid debug information.
static MLIRContext * getContext(OpFoldResult val)
Attributes are known-constant values of operations.
An attribute that associates a referenced attribute with a unique identifier.
This class represents a fused location whose metadata is known to be an instance of the given type.
This class represents a LLVM attribute that describes a local debug info scope.
This class represents the base attribute for all debug info attributes.
This class represents a LLVM attribute that describes a debug info scope.
This class represents a LLVM attribute that describes a debug info type.
This class represents a LLVM attribute that describes a debug info variable.
void finalize()
Finalize the translation of debug information.
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.
DebugTranslation(Operation *module, llvm::Module &llvmModule)
This class defines the main interface for locations in MLIR and acts as a non-nullable wrapper around...
Operation is the basic unit of execution within MLIR.
Attribute getDiscardableAttr(StringRef name)
Access a discardable attribute by name, returns an null Attribute if the discardable attribute does n...
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),...
Location getLoc()
The source location the operation was defined or derived from.
A utility result that is used to signal how to proceed with an ongoing walk:
static WalkResult advance()
static WalkResult interrupt()
Include the generated interface declarations.
auto get(MLIRContext *context, Ts &&...params)
Helper method that injects context only if needed, this helps unify some of the attribute constructio...