1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
|
//===- DebugImporter.cpp - LLVM to MLIR Debug conversion ------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
#include "DebugImporter.h"
#include "mlir/Dialect/LLVMIR/LLVMAttrs.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/Location.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/ScopeExit.h"
#include "llvm/IR/Constants.h"
#include "llvm/IR/DebugInfoMetadata.h"
#include "llvm/IR/Metadata.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/ErrorHandling.h"
using namespace mlir;
using namespace mlir::LLVM;
using namespace mlir::LLVM::detail;
Location DebugImporter::translateFuncLocation(llvm::Function *func) {
if (!func->getSubprogram())
return UnknownLoc::get(context);
// Add a fused location to link the subprogram information.
StringAttr name = StringAttr::get(context, func->getSubprogram()->getName());
return FusedLocWith<DISubprogramAttr>::get(
{NameLoc::get(name)}, translate(func->getSubprogram()), context);
}
//===----------------------------------------------------------------------===//
// Attributes
//===----------------------------------------------------------------------===//
DIBasicTypeAttr DebugImporter::translateImpl(llvm::DIBasicType *node) {
return DIBasicTypeAttr::get(context, node->getTag(), node->getName(),
node->getSizeInBits(), node->getEncoding());
}
DICompileUnitAttr DebugImporter::translateImpl(llvm::DICompileUnit *node) {
std::optional<DIEmissionKind> emissionKind =
symbolizeDIEmissionKind(node->getEmissionKind());
return DICompileUnitAttr::get(context, node->getSourceLanguage(),
translate(node->getFile()),
getStringAttrOrNull(node->getRawProducer()),
node->isOptimized(), emissionKind.value());
}
DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
std::optional<DIFlags> flags = symbolizeDIFlags(node->getFlags());
SmallVector<DINodeAttr> elements;
for (llvm::DINode *element : node->getElements()) {
assert(element && "expected a non-null element type");
elements.push_back(translate(element));
}
// Drop the elements parameter if a cyclic dependency is detected. We
// currently cannot model these cycles and thus drop the parameter if
// required. A cyclic dependency is detected if one of the element nodes
// translates to a nullptr since the node is already on the translation stack.
// TODO: Support debug metadata with cyclic dependencies.
if (llvm::is_contained(elements, nullptr))
elements.clear();
return DICompositeTypeAttr::get(
context, node->getTag(), getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine(), translate(node->getScope()),
translate(node->getBaseType()), flags.value_or(DIFlags::Zero),
node->getSizeInBits(), node->getAlignInBits(), elements);
}
DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
// Return nullptr if the base type is a cyclic dependency.
DITypeAttr baseType = translate(node->getBaseType());
if (node->getBaseType() && !baseType)
return nullptr;
return DIDerivedTypeAttr::get(
context, node->getTag(), getStringAttrOrNull(node->getRawName()),
baseType, node->getSizeInBits(), node->getAlignInBits(),
node->getOffsetInBits());
}
DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
}
DILabelAttr DebugImporter::translateImpl(llvm::DILabel *node) {
return DILabelAttr::get(context, translate(node->getScope()),
getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine());
}
DILexicalBlockAttr DebugImporter::translateImpl(llvm::DILexicalBlock *node) {
return DILexicalBlockAttr::get(context, translate(node->getScope()),
translate(node->getFile()), node->getLine(),
node->getColumn());
}
DILexicalBlockFileAttr
DebugImporter::translateImpl(llvm::DILexicalBlockFile *node) {
return DILexicalBlockFileAttr::get(context, translate(node->getScope()),
translate(node->getFile()),
node->getDiscriminator());
}
DILocalVariableAttr DebugImporter::translateImpl(llvm::DILocalVariable *node) {
return DILocalVariableAttr::get(context, translate(node->getScope()),
getStringAttrOrNull(node->getRawName()),
translate(node->getFile()), node->getLine(),
node->getArg(), node->getAlignInBits(),
translate(node->getType()));
}
DIScopeAttr DebugImporter::translateImpl(llvm::DIScope *node) {
return cast<DIScopeAttr>(translate(static_cast<llvm::DINode *>(node)));
}
DINamespaceAttr DebugImporter::translateImpl(llvm::DINamespace *node) {
return DINamespaceAttr::get(context, getStringAttrOrNull(node->getRawName()),
translate(node->getScope()),
node->getExportSymbols());
}
DISubprogramAttr DebugImporter::translateImpl(llvm::DISubprogram *node) {
std::optional<DISubprogramFlags> subprogramFlags =
symbolizeDISubprogramFlags(node->getSubprogram()->getSPFlags());
// Return nullptr if the scope or type is a cyclic dependency.
DIScopeAttr scope = translate(node->getScope());
if (node->getScope() && !scope)
return nullptr;
DISubroutineTypeAttr type = translate(node->getType());
if (node->getType() && !type)
return nullptr;
return DISubprogramAttr::get(context, translate(node->getUnit()), scope,
getStringAttrOrNull(node->getRawName()),
getStringAttrOrNull(node->getRawLinkageName()),
translate(node->getFile()), node->getLine(),
node->getScopeLine(), subprogramFlags.value(),
type);
}
DISubrangeAttr DebugImporter::translateImpl(llvm::DISubrange *node) {
auto getIntegerAttrOrNull = [&](llvm::DISubrange::BoundType data) {
if (auto *constInt = llvm::dyn_cast_or_null<llvm::ConstantInt *>(data))
return IntegerAttr::get(IntegerType::get(context, 64),
constInt->getSExtValue());
return IntegerAttr();
};
return DISubrangeAttr::get(context, getIntegerAttrOrNull(node->getCount()),
getIntegerAttrOrNull(node->getLowerBound()),
getIntegerAttrOrNull(node->getUpperBound()),
getIntegerAttrOrNull(node->getStride()));
}
DISubroutineTypeAttr
DebugImporter::translateImpl(llvm::DISubroutineType *node) {
SmallVector<DITypeAttr> types;
for (llvm::DIType *type : node->getTypeArray()) {
if (!type) {
// A nullptr entry may appear at the beginning or the end of the
// subroutine types list modeling either a void result type or the type of
// a variadic argument. Translate the nullptr to an explicit
// DINullTypeAttr since the attribute list cannot contain a nullptr entry.
types.push_back(DINullTypeAttr::get(context));
continue;
}
types.push_back(translate(type));
}
// Return nullptr if any of the types is a cyclic dependency.
if (llvm::is_contained(types, nullptr))
return nullptr;
return DISubroutineTypeAttr::get(context, node->getCC(), types);
}
DITypeAttr DebugImporter::translateImpl(llvm::DIType *node) {
return cast<DITypeAttr>(translate(static_cast<llvm::DINode *>(node)));
}
DINodeAttr DebugImporter::translate(llvm::DINode *node) {
if (!node)
return nullptr;
// Check for a cached instance.
if (DINodeAttr attr = nodeToAttr.lookup(node))
return attr;
// Return nullptr if a cyclic dependency is detected since the same node is
// being traversed twice. This check avoids infinite recursion if the debug
// metadata contains cycles.
if (!translationStack.insert(node))
return nullptr;
auto guard = llvm::make_scope_exit([&]() { translationStack.pop_back(); });
// Convert the debug metadata if possible.
auto translateNode = [this](llvm::DINode *node) -> DINodeAttr {
if (auto *casted = dyn_cast<llvm::DIBasicType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DICompileUnit>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DICompositeType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIDerivedType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIFile>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DILabel>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DILexicalBlock>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DILexicalBlockFile>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DILocalVariable>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubprogram>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DINamespace>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubrange>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DISubroutineType>(node))
return translateImpl(casted);
return nullptr;
};
if (DINodeAttr attr = translateNode(node)) {
nodeToAttr.insert({node, attr});
return attr;
}
return nullptr;
}
//===----------------------------------------------------------------------===//
// Locations
//===----------------------------------------------------------------------===//
Location DebugImporter::translateLoc(llvm::DILocation *loc) {
if (!loc)
return UnknownLoc::get(context);
// Get the file location of the instruction.
Location result = FileLineColLoc::get(context, loc->getFilename(),
loc->getLine(), loc->getColumn());
// Add call site information, if available.
if (llvm::DILocation *inlinedAt = loc->getInlinedAt())
result = CallSiteLoc::get(result, translateLoc(inlinedAt));
// Add scope information.
assert(loc->getScope() && "expected non-null scope");
result = FusedLocWith<DIScopeAttr>::get({result}, translate(loc->getScope()),
context);
return result;
}
StringAttr DebugImporter::getStringAttrOrNull(llvm::MDString *stringNode) {
if (!stringNode)
return StringAttr();
return StringAttr::get(context, stringNode->getString());
}
|