1 //===- DebugTranslation.h - MLIR to LLVM Debug conversion -------*- C++ -*-===//
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
7 //===----------------------------------------------------------------------===//
9 // This file implements the translation between an MLIR debug information and
10 // the corresponding LLVMIR representation.
12 //===----------------------------------------------------------------------===//
14 #ifndef MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
15 #define MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_
17 #include "mlir/Dialect/LLVMIR/LLVMDialect.h"
18 #include "mlir/IR/Location.h"
19 #include "llvm/ADT/SmallString.h"
20 #include "llvm/ADT/StringMap.h"
21 #include "llvm/IR/DIBuilder.h"
30 class DebugTranslation
{
32 DebugTranslation(Operation
*module
, llvm::Module
&llvmModule
);
34 /// Finalize the translation of debug information.
37 /// Translate the given location to an llvm debug location.
38 llvm::DILocation
*translateLoc(Location loc
, llvm::DILocalScope
*scope
);
40 /// Translates the given DWARF expression metadata to to LLVM.
41 llvm::DIExpression
*translateExpression(LLVM::DIExpressionAttr attr
);
43 /// Translates the given DWARF global variable expression to LLVM.
44 llvm::DIGlobalVariableExpression
*
45 translateGlobalVariableExpression(LLVM::DIGlobalVariableExpressionAttr attr
);
47 /// Translate the debug information for the given function.
48 void translate(LLVMFuncOp func
, llvm::Function
&llvmFunc
);
50 /// Translate the given LLVM debug metadata to LLVM.
51 llvm::DINode
*translate(DINodeAttr attr
);
53 /// Translate the given derived LLVM debug metadata to LLVM.
54 template <typename DIAttrT
>
55 auto translate(DIAttrT attr
) {
56 // Infer the LLVM type from the attribute type.
57 using LLVMTypeT
= std::remove_pointer_t
<decltype(translateImpl(attr
))>;
58 return cast_or_null
<LLVMTypeT
>(translate(DINodeAttr(attr
)));
62 /// Translate the given location to an llvm debug location with the given
63 /// scope and inlinedAt parameters.
64 llvm::DILocation
*translateLoc(Location loc
, llvm::DILocalScope
*scope
,
65 llvm::DILocation
*inlinedAt
);
67 /// Create an llvm debug file for the given file path.
68 llvm::DIFile
*translateFile(StringRef fileName
);
70 /// Translate the given attribute to the corresponding llvm debug metadata.
71 llvm::DIType
*translateImpl(DINullTypeAttr attr
);
72 llvm::DIBasicType
*translateImpl(DIBasicTypeAttr attr
);
73 llvm::DICompileUnit
*translateImpl(DICompileUnitAttr attr
);
74 llvm::DICompositeType
*translateImpl(DICompositeTypeAttr attr
);
75 llvm::DIDerivedType
*translateImpl(DIDerivedTypeAttr attr
);
76 llvm::DIFile
*translateImpl(DIFileAttr attr
);
77 llvm::DILabel
*translateImpl(DILabelAttr attr
);
78 llvm::DILexicalBlock
*translateImpl(DILexicalBlockAttr attr
);
79 llvm::DILexicalBlockFile
*translateImpl(DILexicalBlockFileAttr attr
);
80 llvm::DILocalScope
*translateImpl(DILocalScopeAttr attr
);
81 llvm::DILocalVariable
*translateImpl(DILocalVariableAttr attr
);
82 llvm::DIGlobalVariable
*translateImpl(DIGlobalVariableAttr attr
);
83 llvm::DIModule
*translateImpl(DIModuleAttr attr
);
84 llvm::DINamespace
*translateImpl(DINamespaceAttr attr
);
85 llvm::DIScope
*translateImpl(DIScopeAttr attr
);
86 llvm::DISubprogram
*translateImpl(DISubprogramAttr attr
);
87 llvm::DISubrange
*translateImpl(DISubrangeAttr attr
);
88 llvm::DISubroutineType
*translateImpl(DISubroutineTypeAttr attr
);
89 llvm::DIType
*translateImpl(DITypeAttr attr
);
91 /// Attributes that support self recursion need to implement an additional
92 /// method to hook into `translateRecursive`.
93 /// - `<temp llvm type> translateTemporaryImpl(<mlir type>)`:
94 /// Create a temporary translation of the DI attr without recursively
95 /// translating any nested DI attrs.
96 llvm::DIType
*translateRecursive(DIRecursiveTypeAttrInterface attr
);
98 /// Translate the given attribute to a temporary llvm debug metadata of the
99 /// corresponding type.
100 llvm::TempDICompositeType
translateTemporaryImpl(DICompositeTypeAttr attr
);
102 /// Constructs a string metadata node from the string attribute. Returns
103 /// nullptr if `stringAttr` is null or contains and empty string.
104 llvm::MDString
*getMDStringOrNull(StringAttr stringAttr
);
106 /// A mapping between mlir location+scope and the corresponding llvm debug
108 DenseMap
<std::tuple
<Location
, llvm::DILocalScope
*, const llvm::DILocation
*>,
112 /// A mapping between debug attribute and the corresponding llvm debug
114 DenseMap
<Attribute
, llvm::DINode
*> attrToNode
;
116 /// A mapping between recursive ID and the translated DIType.
117 llvm::MapVector
<DistinctAttr
, llvm::DIType
*> recursiveTypeMap
;
119 /// A mapping between a distinct ID and the translated LLVM metadata node.
120 /// This helps identify attrs that should translate into the same LLVM debug
122 DenseMap
<DistinctAttr
, llvm::DINode
*> distinctAttrToNode
;
124 /// A mapping between filename and llvm debug file.
125 /// TODO: Change this to DenseMap<Identifier, ...> when we can
126 /// access the Identifier filename in FileLineColLoc.
127 llvm::StringMap
<llvm::DIFile
*> fileMap
;
129 /// A string containing the current working directory of the compiler.
130 SmallString
<256> currentWorkingDir
;
132 /// Flag indicating if debug information should be emitted.
133 bool debugEmissionIsEnabled
;
135 /// Debug information fields.
136 llvm::Module
&llvmModule
;
137 llvm::LLVMContext
&llvmCtx
;
140 } // namespace detail
144 #endif // MLIR_LIB_TARGET_LLVMIR_DEBUGTRANSLATION_H_