Skip to content
This repository was archived by the owner on Oct 11, 2025. It is now read-only.

Commit 4540f92

Browse files
[mlir][debuginfo] Add support for subprogram annotations (#110946)
LLVM already supports `DW_TAG_LLVM_annotation` entries for subprograms, 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 a4d175b commit 4540f92

File tree

2 files changed

+21
-2
lines changed

2 files changed

+21
-2
lines changed

mlir/include/mlir-c/Dialect/LLVM.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,12 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDISubprogramAttrGet(
325325
MlirAttribute compileUnit, MlirAttribute scope, MlirAttribute name,
326326
MlirAttribute linkageName, MlirAttribute file, unsigned int line,
327327
unsigned int scopeLine, uint64_t subprogramFlags, MlirAttribute type,
328-
intptr_t nRetainedNodes, MlirAttribute const *retainedNodes);
328+
intptr_t nRetainedNodes, MlirAttribute const *retainedNodes,
329+
intptr_t nAnnotations, MlirAttribute const *annotations);
330+
331+
/// Creates a LLVM DIAnnotation attribute.
332+
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIAnnotationAttrGet(
333+
MlirContext ctx, MlirAttribute name, MlirAttribute value);
329334

330335
/// Gets the scope from this DISubprogramAttr.
331336
MLIR_CAPI_EXPORTED MlirAttribute

mlir/lib/CAPI/Dialect/LLVM.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,9 +303,14 @@ MlirAttribute mlirLLVMDISubprogramAttrGet(
303303
MlirAttribute compileUnit, MlirAttribute scope, MlirAttribute name,
304304
MlirAttribute linkageName, MlirAttribute file, unsigned int line,
305305
unsigned int scopeLine, uint64_t subprogramFlags, MlirAttribute type,
306-
intptr_t nRetainedNodes, MlirAttribute const *retainedNodes) {
306+
intptr_t nRetainedNodes, MlirAttribute const *retainedNodes,
307+
intptr_t nAnnotations, MlirAttribute const *annotations) {
307308
SmallVector<Attribute> nodesStorage;
308309
nodesStorage.reserve(nRetainedNodes);
310+
311+
SmallVector<Attribute> annotationsStorage;
312+
annotationsStorage.reserve(nAnnotations);
313+
309314
return wrap(DISubprogramAttr::get(
310315
unwrap(ctx), cast<DistinctAttr>(unwrap(recId)), isRecSelf,
311316
cast<DistinctAttr>(unwrap(id)),
@@ -316,6 +321,9 @@ MlirAttribute mlirLLVMDISubprogramAttrGet(
316321
cast<DISubroutineTypeAttr>(unwrap(type)),
317322
llvm::map_to_vector(
318323
unwrapList(nRetainedNodes, retainedNodes, nodesStorage),
324+
[](Attribute a) { return cast<DINodeAttr>(a); }),
325+
llvm::map_to_vector(
326+
unwrapList(nAnnotations, annotations, annotationsStorage),
319327
[](Attribute a) { return cast<DINodeAttr>(a); })));
320328
}
321329

@@ -375,3 +383,9 @@ MlirAttribute mlirLLVMDIImportedEntityAttrGet(
375383
llvm::map_to_vector(unwrapList(nElements, elements, elementsStorage),
376384
[](Attribute a) { return cast<DINodeAttr>(a); })));
377385
}
386+
387+
MlirAttribute mlirLLVMDIAnnotationAttrGet(MlirContext ctx, MlirAttribute name,
388+
MlirAttribute value) {
389+
return wrap(DIAnnotationAttr::get(unwrap(ctx), cast<StringAttr>(unwrap(name)),
390+
cast<StringAttr>(unwrap(value))));
391+
}

0 commit comments

Comments
 (0)