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;
47 llvm::DEBUG_METADATA_VERSION);
49 const llvm::Triple &targetTriple = llvmModule.getTargetTriple();
50 if (targetTriple.isKnownWindowsMSVCEnvironment()) {
54 llvmModule.addModuleFlag(llvm::Module::Warning,
kCodeViewKey, 1);
60 if (!debugEmissionIsEnabled)
68 llvmFunc.setSubprogram(
translate(spLoc.getMetadata()));
75 llvm::DIType *DebugTranslation::translateImpl(DINullTypeAttr attr) {
85 DebugTranslation::getExpressionAttrOrNull(DIExpressionAttr attr) {
91 llvm::MDString *DebugTranslation::getMDStringOrNull(StringAttr stringAttr) {
92 if (!stringAttr || stringAttr.empty())
102 llvm::map_range(elements, [&](
DINodeAttr attr) -> llvm::Metadata * {
103 if (DIAnnotationAttr annAttr = dyn_cast<DIAnnotationAttr>(attr)) {
104 llvm::Metadata *ops[2] = {
114 llvm::DIBasicType *DebugTranslation::translateImpl(DIBasicTypeAttr attr) {
116 llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
117 attr.getSizeInBits(),
118 0, attr.getEncoding(), llvm::DINode::FlagZero);
121 llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
122 llvm::DIBuilder builder(llvmModule);
123 return builder.createCompileUnit(
124 attr.getSourceLanguage(),
translate(attr.getFile()),
125 attr.getProducer() ? attr.getProducer().getValue() :
"",
126 attr.getIsOptimized(),
128 attr.getSplitDebugFilename() ? attr.getSplitDebugFilename().getValue()
130 static_cast<llvm::DICompileUnit::DebugEmissionKind
>(
131 attr.getEmissionKind()),
133 static_cast<llvm::DICompileUnit::DebugNameTableKind
>(
134 attr.getNameTableKind()));
139 template <
class DINodeT,
class... Ts>
142 return DINodeT::getDistinct(std::forward<Ts>(args)...);
146 llvm::TempDICompositeType
147 DebugTranslation::translateTemporaryImpl(DICompositeTypeAttr attr) {
148 return llvm::DICompositeType::getTemporary(
149 llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
nullptr,
150 attr.getLine(),
nullptr,
nullptr, attr.getSizeInBits(),
151 attr.getAlignInBits(),
153 static_cast<llvm::DINode::DIFlags
>(attr.getFlags()),
154 nullptr, 0, std::nullopt,
158 llvm::TempDISubprogram
159 DebugTranslation::translateTemporaryImpl(DISubprogramAttr attr) {
160 return llvm::DISubprogram::getTemporary(
161 llvmCtx,
nullptr, {}, {},
162 nullptr, attr.getLine(),
nullptr,
164 0, llvm::DINode::FlagZero,
165 static_cast<llvm::DISubprogram::DISPFlags
>(attr.getSubprogramFlags()),
169 llvm::DICompositeType *
170 DebugTranslation::translateImpl(DICompositeTypeAttr attr) {
173 bool isDistinct =
false;
174 switch (attr.getTag()) {
175 case llvm::dwarf::DW_TAG_class_type:
176 case llvm::dwarf::DW_TAG_enumeration_type:
177 case llvm::dwarf::DW_TAG_structure_type:
178 case llvm::dwarf::DW_TAG_union_type:
182 return getDistinctOrUnique<llvm::DICompositeType>(
183 isDistinct, llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
185 translate(attr.getBaseType()), attr.getSizeInBits(),
186 attr.getAlignInBits(),
188 static_cast<llvm::DINode::DIFlags
>(attr.getFlags()),
189 getMDTupleOrNull(attr.getElements()),
190 0, std::nullopt,
nullptr,
193 getExpressionAttrOrNull(attr.getDataLocation()),
194 getExpressionAttrOrNull(attr.getAssociated()),
195 getExpressionAttrOrNull(attr.getAllocated()),
196 getExpressionAttrOrNull(attr.getRank()));
199 llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
201 llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
203 nullptr,
translate(attr.getBaseType()), attr.getSizeInBits(),
204 attr.getAlignInBits(), attr.getOffsetInBits(),
205 attr.getDwarfAddressSpace(), std::nullopt,
206 llvm::DINode::FlagZero,
translate(attr.getExtraData()));
209 llvm::DIStringType *DebugTranslation::translateImpl(DIStringTypeAttr attr) {
211 llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
213 getExpressionAttrOrNull(attr.getStringLengthExp()),
214 getExpressionAttrOrNull(attr.getStringLocationExp()),
215 attr.getSizeInBits(), attr.getAlignInBits(), attr.getEncoding());
218 llvm::DIFile *DebugTranslation::translateImpl(DIFileAttr attr) {
220 getMDStringOrNull(attr.getDirectory()));
223 llvm::DILabel *DebugTranslation::translateImpl(DILabelAttr attr) {
225 getMDStringOrNull(attr.getName()),
226 translate(attr.getFile()), attr.getLine(),
231 llvm::DILexicalBlock *DebugTranslation::translateImpl(DILexicalBlockAttr attr) {
232 return llvm::DILexicalBlock::getDistinct(llvmCtx,
translate(attr.getScope()),
234 attr.getLine(), attr.getColumn());
237 llvm::DILexicalBlockFile *
238 DebugTranslation::translateImpl(DILexicalBlockFileAttr attr) {
239 return llvm::DILexicalBlockFile::getDistinct(
241 attr.getDiscriminator());
244 llvm::DILocalScope *DebugTranslation::translateImpl(
DILocalScopeAttr attr) {
248 llvm::DIVariable *DebugTranslation::translateImpl(
DIVariableAttr attr) {
252 llvm::DILocalVariable *
253 DebugTranslation::translateImpl(DILocalVariableAttr attr) {
255 llvmCtx,
translate(attr.getScope()), getMDStringOrNull(attr.getName()),
257 attr.getArg(),
static_cast<llvm::DINode::DIFlags
>(attr.getFlags()),
258 attr.getAlignInBits(),
262 llvm::DIGlobalVariable *
263 DebugTranslation::translateImpl(DIGlobalVariableAttr attr) {
264 return llvm::DIGlobalVariable::getDistinct(
265 llvmCtx,
translate(attr.getScope()), getMDStringOrNull(attr.getName()),
266 getMDStringOrNull(attr.getLinkageName()),
translate(attr.getFile()),
267 attr.getLine(),
translate(attr.getType()), attr.getIsLocalToUnit(),
268 attr.getIsDefined(),
nullptr,
nullptr, attr.getAlignInBits(),
nullptr);
272 DebugTranslation::translateRecursive(DIRecursiveTypeAttrInterface attr) {
274 if (
auto *iter = recursiveNodeMap.find(recursiveId);
275 iter != recursiveNodeMap.end()) {
278 assert(!attr.getIsRecSelf() &&
"unbound DI recursive self reference");
280 auto setRecursivePlaceholder = [&](llvm::DINode *placeholder) {
281 recursiveNodeMap.try_emplace(recursiveId, placeholder);
284 llvm::DINode *result =
286 .Case<DICompositeTypeAttr>([&](
auto attr) {
287 auto temporary = translateTemporaryImpl(attr);
288 setRecursivePlaceholder(temporary.get());
291 auto *concrete = translateImpl(attr);
292 temporary->replaceAllUsesWith(concrete);
295 .Case<DISubprogramAttr>([&](
auto attr) {
296 auto temporary = translateTemporaryImpl(attr);
297 setRecursivePlaceholder(temporary.get());
300 auto *concrete = translateImpl(attr);
301 temporary->replaceAllUsesWith(concrete);
305 assert(recursiveNodeMap.back().first == recursiveId &&
306 "internal inconsistency: unexpected recursive translation stack");
307 recursiveNodeMap.pop_back();
312 llvm::DIScope *DebugTranslation::translateImpl(
DIScopeAttr attr) {
316 llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
317 if (
auto iter = distinctAttrToNode.find(attr.getId());
318 iter != distinctAttrToNode.end())
319 return cast<llvm::DISubprogram>(iter->second);
321 llvm::DIScope *scope =
translate(attr.getScope());
322 llvm::DIFile *file =
translate(attr.getFile());
323 llvm::DIType *type =
translate(attr.getType());
324 llvm::DICompileUnit *compileUnit =
translate(attr.getCompileUnit());
328 if (
auto iter = distinctAttrToNode.find(attr.getId());
329 iter != distinctAttrToNode.end())
330 return cast<llvm::DISubprogram>(iter->second);
332 bool isDefinition =
static_cast<bool>(attr.getSubprogramFlags() &
333 LLVM::DISubprogramFlags::Definition);
335 llvm::DISubprogram *node = getDistinctOrUnique<llvm::DISubprogram>(
336 isDefinition, llvmCtx, scope, getMDStringOrNull(attr.getName()),
337 getMDStringOrNull(attr.getLinkageName()), file, attr.getLine(), type,
340 0, llvm::DINode::FlagZero,
341 static_cast<llvm::DISubprogram::DISPFlags
>(attr.getSubprogramFlags()),
342 compileUnit,
nullptr,
nullptr,
343 getMDTupleOrNull(attr.getRetainedNodes()),
nullptr,
344 getMDTupleOrNull(attr.getAnnotations()));
346 distinctAttrToNode.try_emplace(attr.getId(), node);
350 llvm::DIModule *DebugTranslation::translateImpl(DIModuleAttr attr) {
353 getMDStringOrNull(attr.getName()),
354 getMDStringOrNull(attr.getConfigMacros()),
355 getMDStringOrNull(attr.getIncludePath()),
356 getMDStringOrNull(attr.getApinotes()), attr.getLine(), attr.getIsDecl());
359 llvm::DINamespace *DebugTranslation::translateImpl(DINamespaceAttr attr) {
361 getMDStringOrNull(attr.getName()),
362 attr.getExportSymbols());
365 llvm::DIImportedEntity *
366 DebugTranslation::translateImpl(DIImportedEntityAttr attr) {
368 llvmCtx, attr.getTag(),
translate(attr.getScope()),
370 getMDStringOrNull(attr.getName()), getMDTupleOrNull(attr.getElements()));
373 llvm::DISubrange *DebugTranslation::translateImpl(DISubrangeAttr attr) {
374 auto getMetadataOrNull = [&](
Attribute attr) -> llvm::Metadata * {
378 llvm::Metadata *metadata =
380 .Case([&](IntegerAttr intAttr) {
382 llvm::Type::getInt64Ty(llvmCtx), intAttr.getInt()));
384 .Case([&](LLVM::DIExpressionAttr expr) {
387 .Case([&](LLVM::DILocalVariableAttr local) {
390 .Case<>([&](LLVM::DIGlobalVariableAttr global) {
393 .Default([&](
Attribute attr) {
return nullptr; });
397 getMetadataOrNull(attr.getLowerBound()),
398 getMetadataOrNull(attr.getUpperBound()),
399 getMetadataOrNull(attr.getStride()));
402 llvm::DICommonBlock *DebugTranslation::translateImpl(DICommonBlockAttr attr) {
405 getMDStringOrNull(attr.getName()),
406 translate(attr.getFile()), attr.getLine());
409 llvm::DIGenericSubrange *
410 DebugTranslation::translateImpl(DIGenericSubrangeAttr attr) {
411 auto getMetadataOrNull = [&](
Attribute attr) -> llvm::Metadata * {
415 llvm::Metadata *metadata =
417 .Case([&](LLVM::DIExpressionAttr expr) {
420 .Case([&](LLVM::DILocalVariableAttr local) {
423 .Case<>([&](LLVM::DIGlobalVariableAttr global) {
426 .Default([&](
Attribute attr) {
return nullptr; });
430 getMetadataOrNull(attr.getCount()),
431 getMetadataOrNull(attr.getLowerBound()),
432 getMetadataOrNull(attr.getUpperBound()),
433 getMetadataOrNull(attr.getStride()));
436 llvm::DISubroutineType *
437 DebugTranslation::translateImpl(DISubroutineTypeAttr attr) {
443 llvmCtx, llvm::DINode::FlagZero, attr.getCallingConvention(),
447 llvm::DIType *DebugTranslation::translateImpl(
DITypeAttr attr) {
455 if (llvm::DINode *node = attrToNode.lookup(attr))
458 llvm::DINode *node =
nullptr;
461 if (
auto recTypeAttr = dyn_cast<DIRecursiveTypeAttrInterface>(attr))
462 if (recTypeAttr.getRecId())
463 node = translateRecursive(recTypeAttr);
467 .Case<DIBasicTypeAttr, DICommonBlockAttr, DICompileUnitAttr,
468 DICompositeTypeAttr, DIDerivedTypeAttr, DIFileAttr,
469 DIGenericSubrangeAttr, DIGlobalVariableAttr,
470 DIImportedEntityAttr, DILabelAttr, DILexicalBlockAttr,
471 DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
472 DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
473 DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
474 [&](
auto attr) {
return translateImpl(attr); });
476 if (node && !node->isTemporary())
477 attrToNode.insert({attr, node});
486 llvm::DILocation *DebugTranslation::translateLoc(
Location loc,
487 llvm::DILocalScope *scope) {
488 if (!debugEmissionIsEnabled)
490 return translateLoc(loc, scope,
nullptr);
494 DebugTranslation::translateExpression(LLVM::DIExpressionAttr attr) {
498 for (
const DIExpressionElemAttr &op : attr.getOperations()) {
499 ops.push_back(op.getOpcode());
500 append_range(ops, op.getArguments());
506 llvm::DIGlobalVariableExpression *
507 DebugTranslation::translateGlobalVariableExpression(
508 LLVM::DIGlobalVariableExpressionAttr attr) {
510 llvmCtx, translate(attr.getVar()), translateExpression(attr.getExpr()));
514 llvm::DILocation *DebugTranslation::translateLoc(
Location loc,
515 llvm::DILocalScope *scope,
516 llvm::DILocation *inlinedAt) {
518 if (isa<UnknownLoc>(loc))
522 auto existingIt = locationToLoc.find(std::make_tuple(loc, scope, inlinedAt));
523 if (existingIt != locationToLoc.end())
524 return existingIt->second;
526 llvm::DILocation *llvmLoc =
nullptr;
527 if (
auto callLoc = dyn_cast<CallSiteLoc>(loc)) {
529 auto *callerLoc = translateLoc(callLoc.getCaller(), scope, inlinedAt);
537 callerLoc = inlinedAt;
539 llvmLoc = translateLoc(callLoc.getCallee(),
nullptr, callerLoc);
544 }
else if (
auto fileLoc = dyn_cast<FileLineColLoc>(loc)) {
550 scope,
const_cast<llvm::DILocation *
>(inlinedAt));
552 }
else if (
auto fusedLoc = dyn_cast<FusedLoc>(loc)) {
556 if (
auto scopedAttr =
557 dyn_cast_or_null<LLVM::DILocalScopeAttr>(fusedLoc.getMetadata()))
558 scope = translate(scopedAttr);
561 llvmLoc = translateLoc(locations.front(), scope, inlinedAt);
562 for (
Location locIt : locations.drop_front()) {
563 llvmLoc = llvm::DILocation::getMergedLocation(
564 llvmLoc, translateLoc(locIt, scope, inlinedAt));
567 }
else if (
auto nameLoc = dyn_cast<NameLoc>(loc)) {
568 llvmLoc = translateLoc(nameLoc.getChildLoc(), scope, inlinedAt);
570 }
else if (
auto opaqueLoc = dyn_cast<OpaqueLoc>(loc)) {
571 llvmLoc = translateLoc(opaqueLoc.getFallbackLocation(), scope, inlinedAt);
573 llvm_unreachable(
"unknown location kind");
576 locationToLoc.try_emplace(std::make_tuple(loc, scope, inlinedAt), llvmLoc);
581 llvm::DIFile *DebugTranslation::translateFile(StringRef fileName) {
582 auto *&file = fileMap[fileName];
587 if (currentWorkingDir.empty())
588 llvm::sys::fs::current_path(currentWorkingDir);
590 StringRef directory = currentWorkingDir;
593 if (llvm::sys::path::is_absolute(fileName)) {
596 auto fileIt = llvm::sys::path::begin(fileName);
597 auto fileE = llvm::sys::path::end(fileName);
598 auto curDirIt = llvm::sys::path::begin(directory);
599 auto curDirE = llvm::sys::path::end(directory);
600 for (; curDirIt != curDirE && *curDirIt == *fileIt; ++curDirIt, ++fileIt)
601 llvm::sys::path::append(dirBuf, *curDirIt);
602 if (std::distance(llvm::sys::path::begin(directory), curDirIt) == 1) {
605 directory = StringRef();
607 for (; fileIt != fileE; ++fileIt)
608 llvm::sys::path::append(fileBuf, *fileIt);
static constexpr StringRef kDebugVersionKey
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 constexpr StringRef kCodeViewKey
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 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)
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.
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...