Skip to content

Commit 54f5b32

Browse files
author
walter erquinigo
committed
[mlir][debuginfo] Add support for subroutine annotations
LLVM already supports `DW_TAG_LLVM_annotation` entries for subroutines, but this hasn't been surfaced to the LLVM dialect. I'm doing the minimal amount of work to support string-based annotations, which is useful for attaching metadata to functions, which is useful for debuggers to offer features beyond basic DWARF. As LLVM already supports this, this patch is not controversial.
1 parent e7a43ca commit 54f5b32

File tree

4 files changed

+38
-13
lines changed

4 files changed

+38
-13
lines changed

mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -586,19 +586,20 @@ def LLVM_DISubprogramAttr : LLVM_Attr<"DISubprogram", "di_subprogram",
586586
OptionalParameter<"unsigned">:$scopeLine,
587587
OptionalParameter<"DISubprogramFlags">:$subprogramFlags,
588588
OptionalParameter<"DISubroutineTypeAttr">:$type,
589-
OptionalArrayRefParameter<"DINodeAttr">:$retainedNodes
589+
OptionalArrayRefParameter<"DINodeAttr">:$retainedNodes,
590+
OptionalArrayRefParameter<"DINodeAttr">:$annotations
590591
);
591592
let builders = [
592593
AttrBuilder<(ins
593594
"DistinctAttr":$id, "DICompileUnitAttr":$compileUnit,
594595
"DIScopeAttr":$scope, "StringAttr":$name, "StringAttr":$linkageName,
595596
"DIFileAttr":$file, "unsigned":$line, "unsigned":$scopeLine,
596597
"DISubprogramFlags":$subprogramFlags, "DISubroutineTypeAttr":$type,
597-
"ArrayRef<DINodeAttr>":$retainedNodes
598+
"ArrayRef<DINodeAttr>":$retainedNodes, "ArrayRef<DINodeAttr>":$annotations
598599
), [{
599600
return $_get($_ctxt, /*recId=*/nullptr, /*isRecSelf=*/false, id, compileUnit,
600601
scope, name, linkageName, file, line, scopeLine,
601-
subprogramFlags, type, retainedNodes);
602+
subprogramFlags, type, retainedNodes, annotations);
602603
}]>
603604
];
604605
let assemblyFormat = "`<` struct(params) `>`";
@@ -670,6 +671,21 @@ def LLVM_DIImportedEntityAttr : LLVM_Attr<"DIImportedEntity", "di_imported_entit
670671
let assemblyFormat = "`<` struct(params) `>`";
671672
}
672673

674+
//===----------------------------------------------------------------------===//
675+
// DIStringAnnotationAttr
676+
//===----------------------------------------------------------------------===//
677+
678+
def LLVM_DIStringAnnotationAttr : LLVM_Attr<"DIStringAnnotation",
679+
"di_string_annotation",
680+
/*traits=*/[], "DINodeAttr"> {
681+
let parameters = (ins
682+
"StringAttr":$name,
683+
"StringAttr":$value
684+
);
685+
686+
let assemblyFormat = "`<` struct(params) `>`";
687+
}
688+
673689
//===----------------------------------------------------------------------===//
674690
// DISubrangeAttr
675691
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ bool DINodeAttr::classof(Attribute attr) {
6060
DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
6161
DIImportedEntityAttr, DILabelAttr, DILexicalBlockAttr,
6262
DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
63-
DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
64-
DISubprogramAttr, DISubrangeAttr, DISubroutineTypeAttr>(
65-
attr);
63+
DINamespaceAttr, DINullTypeAttr, DIStringAnnotationAttr,
64+
DIStringTypeAttr, DISubprogramAttr, DISubrangeAttr,
65+
DISubroutineTypeAttr>(attr);
6666
}
6767

6868
//===----------------------------------------------------------------------===//
@@ -221,15 +221,16 @@ DICompositeTypeAttr::getRecSelf(DistinctAttr recId) {
221221
//===----------------------------------------------------------------------===//
222222

223223
DIRecursiveTypeAttrInterface DISubprogramAttr::withRecId(DistinctAttr recId) {
224-
return DISubprogramAttr::get(
225-
getContext(), recId, getIsRecSelf(), getId(), getCompileUnit(),
226-
getScope(), getName(), getLinkageName(), getFile(), getLine(),
227-
getScopeLine(), getSubprogramFlags(), getType(), getRetainedNodes());
224+
return DISubprogramAttr::get(getContext(), recId, getIsRecSelf(), getId(),
225+
getCompileUnit(), getScope(), getName(),
226+
getLinkageName(), getFile(), getLine(),
227+
getScopeLine(), getSubprogramFlags(), getType(),
228+
getRetainedNodes(), getAnnotations());
228229
}
229230

230231
DIRecursiveTypeAttrInterface DISubprogramAttr::getRecSelf(DistinctAttr recId) {
231232
return DISubprogramAttr::get(recId.getContext(), recId, /*isRecSelf=*/true,
232-
{}, {}, {}, {}, {}, 0, 0, {}, {}, {}, {});
233+
{}, {}, {}, {}, {}, 0, 0, {}, {}, {}, {}, {});
233234
}
234235

235236
//===----------------------------------------------------------------------===//

mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ static void addScopeToFunction(LLVM::LLVMFuncOp llvmFunc,
7878
auto subprogramAttr = LLVM::DISubprogramAttr::get(
7979
context, id, compileUnitAttr, fileAttr, funcName, funcName, fileAttr,
8080
/*line=*/line, /*scopeline=*/col, subprogramFlags, subroutineTypeAttr,
81-
/*retainedNodes=*/{});
81+
/*retainedNodes=*/{}, /*annotations=*/{});
8282
llvmFunc->setLoc(FusedLoc::get(context, {loc}, subprogramAttr));
8383
}
8484

mlir/lib/Target/LLVMIR/DebugTranslation.cpp

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,13 @@ DebugTranslation::getMDTupleOrNull(ArrayRef<DINodeAttr> elements) {
102102
return nullptr;
103103
SmallVector<llvm::Metadata *> llvmElements = llvm::to_vector(
104104
llvm::map_range(elements, [&](DINodeAttr attr) -> llvm::Metadata * {
105+
if (DIStringAnnotationAttr annAttr =
106+
dyn_cast<DIStringAnnotationAttr>(attr)) {
107+
llvm::Metadata *ops[2] = {
108+
llvm::MDString::get(llvmCtx, annAttr.getName()),
109+
llvm::MDString::get(llvmCtx, annAttr.getValue())};
110+
return llvm::MDNode::get(llvmCtx, ops);
111+
}
105112
return translate(attr);
106113
}));
107114
return llvm::MDNode::get(llvmCtx, llvmElements);
@@ -332,7 +339,8 @@ llvm::DISubprogram *DebugTranslation::translateImpl(DISubprogramAttr attr) {
332339
/*ThisAdjustment=*/0, llvm::DINode::FlagZero,
333340
static_cast<llvm::DISubprogram::DISPFlags>(attr.getSubprogramFlags()),
334341
compileUnit, /*TemplateParams=*/nullptr, /*Declaration=*/nullptr,
335-
getMDTupleOrNull(attr.getRetainedNodes()));
342+
getMDTupleOrNull(attr.getRetainedNodes()), nullptr,
343+
getMDTupleOrNull(attr.getAnnotations()));
336344
if (attr.getId())
337345
distinctAttrToNode.try_emplace(attr.getId(), node);
338346
return node;

0 commit comments

Comments
 (0)