Skip to content

[MLIR] Translate DIStringType. #94480

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 5 commits into from
Jun 7, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions mlir/include/mlir-c/Dialect/LLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,12 @@ MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIDerivedTypeAttrGet(
MlirAttribute baseType, uint64_t sizeInBits, uint32_t alignInBits,
uint64_t offsetInBits, int64_t dwarfAddressSpace, MlirAttribute extraData);

MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIStringTypeAttrGet(
MlirContext ctx, unsigned int tag, MlirAttribute name, uint64_t sizeInBits,
uint32_t alignInBits, MlirAttribute stringLength,
MlirAttribute stringLengthExp, MlirAttribute stringLocationExp,
MlirLLVMTypeEncoding encoding);

/// Constant to represent std::nullopt for dwarfAddressSpace to omit the field.
#define MLIR_CAPI_DWARF_ADDRESS_SPACE_NULL -1

Expand Down
15 changes: 15 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -676,6 +676,21 @@ def LLVM_DILabelAttr : LLVM_Attr<"DILabel", "di_label",
let assemblyFormat = "`<` struct(params) `>`";
}

def LLVM_DIStringTypeAttr : LLVM_Attr<"DIStringType", "di_string_type",
/*traits=*/[], "DITypeAttr"> {
let parameters = (ins
LLVM_DITagParameter:$tag,
"StringAttr":$name,
OptionalParameter<"uint64_t">:$sizeInBits,
OptionalParameter<"uint64_t">:$alignInBits,
OptionalParameter<"DINodeAttr">:$stringLength,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it would make sense to introduce DIVariableAttr in LLVMAttrs.h. Especially since this seems to exist in the LLVM class hierarchy.

When adding it is best to take DIScopeAttr, DITypeAttr, or a similar attribute as an example. There are a couple of places that need to be updated such as the DebugImport / DebugTranslation and LLVMAttrs.h / LLVMAttrs.cpp

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the pointers. I have added DIVariableAttr and I think it makes the patch cleaner. With DIVariableAttr now available, there may be some cleanup possible in the DISubrangeAttr changes that I did last week. I will handle that in a separate PR.

OptionalParameter<"DIExpressionAttr">:$stringLengthExp,
OptionalParameter<"DIExpressionAttr">:$stringLocationExp,
LLVM_DIEncodingParameter:$encoding
);
let assemblyFormat = "`<` struct(params) `>`";
}

//===----------------------------------------------------------------------===//
// MemoryEffectsAttr
//===----------------------------------------------------------------------===//
Expand Down
12 changes: 12 additions & 0 deletions mlir/lib/CAPI/Dialect/LLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -195,6 +195,18 @@ MlirAttribute mlirLLVMDIDerivedTypeAttrGet(
addressSpace, cast<DINodeAttr>(unwrap(extraData))));
}

MlirAttribute mlirLLVMDIStringTypeAttrGet(
MlirContext ctx, unsigned int tag, MlirAttribute name, uint64_t sizeInBits,
uint32_t alignInBits, MlirAttribute stringLength,
MlirAttribute stringLengthExp, MlirAttribute stringLocationExp,
MlirLLVMTypeEncoding encoding) {
return wrap(DIStringTypeAttr::get(
unwrap(ctx), tag, cast<StringAttr>(unwrap(name)), sizeInBits, alignInBits,
cast<DINodeAttr>(unwrap(stringLength)),
cast<DIExpressionAttr>(unwrap(stringLengthExp)),
cast<DIExpressionAttr>(unwrap(stringLocationExp)), encoding));
}

MlirAttribute
mlirLLVMDIDerivedTypeAttrGetBaseType(MlirAttribute diDerivedType) {
return wrap(cast<DIDerivedTypeAttr>(unwrap(diDerivedType)).getBaseType());
Expand Down
7 changes: 4 additions & 3 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMAttrs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ bool DINodeAttr::classof(Attribute attr) {
DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
DILabelAttr, DILexicalBlockAttr, DILexicalBlockFileAttr,
DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
DINullTypeAttr, DISubprogramAttr, DISubrangeAttr,
DISubroutineTypeAttr>(attr);
DINullTypeAttr, DIStringTypeAttr, DISubprogramAttr,
DISubrangeAttr, DISubroutineTypeAttr>(attr);
}

//===----------------------------------------------------------------------===//
Expand All @@ -88,7 +88,8 @@ bool DILocalScopeAttr::classof(Attribute attr) {

bool DITypeAttr::classof(Attribute attr) {
return llvm::isa<DINullTypeAttr, DIBasicTypeAttr, DICompositeTypeAttr,
DIDerivedTypeAttr, DISubroutineTypeAttr>(attr);
DIDerivedTypeAttr, DIStringTypeAttr, DISubroutineTypeAttr>(
attr);
}

//===----------------------------------------------------------------------===//
Expand Down
12 changes: 6 additions & 6 deletions mlir/lib/Dialect/LLVMIR/IR/LLVMDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3031,12 +3031,12 @@ struct LLVMOpAsmDialectInterface : public OpAsmDialectInterface {
DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
DIGlobalVariableExpressionAttr, DILabelAttr, DILexicalBlockAttr,
DILexicalBlockFileAttr, DILocalVariableAttr, DIModuleAttr,
DINamespaceAttr, DINullTypeAttr, DISubprogramAttr,
DISubroutineTypeAttr, LoopAnnotationAttr, LoopVectorizeAttr,
LoopInterleaveAttr, LoopUnrollAttr, LoopUnrollAndJamAttr,
LoopLICMAttr, LoopDistributeAttr, LoopPipelineAttr,
LoopPeeledAttr, LoopUnswitchAttr, TBAARootAttr, TBAATagAttr,
TBAATypeDescriptorAttr>([&](auto attr) {
DINamespaceAttr, DINullTypeAttr, DIStringTypeAttr,
DISubprogramAttr, DISubroutineTypeAttr, LoopAnnotationAttr,
LoopVectorizeAttr, LoopInterleaveAttr, LoopUnrollAttr,
LoopUnrollAndJamAttr, LoopLICMAttr, LoopDistributeAttr,
LoopPipelineAttr, LoopPeeledAttr, LoopUnswitchAttr, TBAARootAttr,
TBAATagAttr, TBAATypeDescriptorAttr>([&](auto attr) {
os << decltype(attr)::getMnemonic();
return AliasResult::OverridableAlias;
})
Expand Down
15 changes: 15 additions & 0 deletions mlir/lib/Target/LLVMIR/DebugImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,19 @@ DIDerivedTypeAttr DebugImporter::translateImpl(llvm::DIDerivedType *node) {
node->getOffsetInBits(), node->getDWARFAddressSpace(), extraData);
}

DIStringTypeAttr DebugImporter::translateImpl(llvm::DIStringType *node) {
llvm::DINode *stringLength =
TypeSwitch<llvm::DIVariable *, llvm::DINode *>(node->getStringLength())
.Case([&](llvm::DILocalVariable *local) { return local; })
.Case([&](llvm::DIGlobalVariable *global) { return global; })
.Default([&](llvm::DIVariable *) { return nullptr; });
return DIStringTypeAttr::get(
context, node->getTag(), getStringAttrOrNull(node->getRawName()),
node->getSizeInBits(), node->getAlignInBits(), translate(stringLength),
translateExpression(node->getStringLengthExp()),
translateExpression(node->getStringLocationExp()), node->getEncoding());
}

DIFileAttr DebugImporter::translateImpl(llvm::DIFile *node) {
return DIFileAttr::get(context, node->getFilename(), node->getDirectory());
}
Expand Down Expand Up @@ -295,6 +308,8 @@ DINodeAttr DebugImporter::translate(llvm::DINode *node) {
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIDerivedType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIStringType>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIFile>(node))
return translateImpl(casted);
if (auto *casted = dyn_cast<llvm::DIGlobalVariable>(node))
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Target/LLVMIR/DebugImporter.h
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ class DebugImporter {
DICompileUnitAttr translateImpl(llvm::DICompileUnit *node);
DICompositeTypeAttr translateImpl(llvm::DICompositeType *node);
DIDerivedTypeAttr translateImpl(llvm::DIDerivedType *node);
DIStringTypeAttr translateImpl(llvm::DIStringType *node);
DIFileAttr translateImpl(llvm::DIFile *node);
DILabelAttr translateImpl(llvm::DILabel *node);
DILexicalBlockAttr translateImpl(llvm::DILexicalBlock *node);
Expand Down
13 changes: 11 additions & 2 deletions mlir/lib/Target/LLVMIR/DebugTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,15 @@ llvm::DIDerivedType *DebugTranslation::translateImpl(DIDerivedTypeAttr attr) {
/*Flags=*/llvm::DINode::FlagZero, translate(attr.getExtraData()));
}

llvm::DIStringType *DebugTranslation::translateImpl(DIStringTypeAttr attr) {
return llvm::DIStringType::get(
llvmCtx, attr.getTag(), getMDStringOrNull(attr.getName()),
translate(attr.getStringLength()),
getExpressionAttrOrNull(attr.getStringLengthExp()),
getExpressionAttrOrNull(attr.getStringLocationExp()),
attr.getSizeInBits(), attr.getAlignInBits(), attr.getEncoding());
}

llvm::DIFile *DebugTranslation::translateImpl(DIFileAttr attr) {
return llvm::DIFile::get(llvmCtx, getMDStringOrNull(attr.getName()),
getMDStringOrNull(attr.getDirectory()));
Expand Down Expand Up @@ -377,8 +386,8 @@ llvm::DINode *DebugTranslation::translate(DINodeAttr attr) {
DIDerivedTypeAttr, DIFileAttr, DIGlobalVariableAttr,
DILabelAttr, DILexicalBlockAttr, DILexicalBlockFileAttr,
DILocalVariableAttr, DIModuleAttr, DINamespaceAttr,
DINullTypeAttr, DISubprogramAttr, DISubrangeAttr,
DISubroutineTypeAttr>(
DINullTypeAttr, DIStringTypeAttr, DISubprogramAttr,
DISubrangeAttr, DISubroutineTypeAttr>(
[&](auto attr) { return translateImpl(attr); });

if (node && !node->isTemporary())
Expand Down
1 change: 1 addition & 0 deletions mlir/lib/Target/LLVMIR/DebugTranslation.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ class DebugTranslation {
llvm::DICompileUnit *translateImpl(DICompileUnitAttr attr);
llvm::DICompositeType *translateImpl(DICompositeTypeAttr attr);
llvm::DIDerivedType *translateImpl(DIDerivedTypeAttr attr);
llvm::DIStringType *translateImpl(DIStringTypeAttr attr);
llvm::DIFile *translateImpl(DIFileAttr attr);
llvm::DILabel *translateImpl(DILabelAttr attr);
llvm::DILexicalBlock *translateImpl(DILexicalBlockAttr attr);
Expand Down
11 changes: 9 additions & 2 deletions mlir/test/CAPI/llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -293,8 +293,9 @@ static void testDebugInfoAttributes(MlirContext ctx) {
mlirLLVMDILexicalBlockFileAttrGet(ctx, compile_unit, file, 3));

// CHECK: #llvm.di_local_variable<{{.*}}>
mlirAttributeDump(mlirLLVMDILocalVariableAttrGet(ctx, compile_unit, foo, file,
1, 0, 8, di_type));
MlirAttribute local_var = mlirLLVMDILocalVariableAttrGet(
ctx, compile_unit, foo, file, 1, 0, 8, di_type);
mlirAttributeDump(local_var);
// CHECK: #llvm.di_derived_type<{{.*}}>
// CHECK-NOT: dwarfAddressSpace
mlirAttributeDump(mlirLLVMDIDerivedTypeAttrGet(
Expand Down Expand Up @@ -337,6 +338,12 @@ static void testDebugInfoAttributes(MlirContext ctx) {
// CHECK: #llvm.di_expression<[(1)]>
mlirAttributeDump(expression);

MlirAttribute string_type =
mlirLLVMDIStringTypeAttrGet(ctx, 0x0, foo, 16, 0, local_var, expression,
expression, MlirLLVMTypeEncodingSigned);
// CHECK: #llvm.di_string_type<{{.*}}>
mlirAttributeDump(string_type);

// CHECK: #llvm.di_composite_type<{{.*}}>
mlirAttributeDump(mlirLLVMDICompositeTypeAttrGet(
ctx, 0, id, foo, file, 1, compile_unit, di_type, 0, 64, 8, 1, &di_type,
Expand Down
32 changes: 32 additions & 0 deletions mlir/test/Target/LLVMIR/Import/debug-info.ll
Original file line number Diff line number Diff line change
Expand Up @@ -761,3 +761,35 @@ define void @class_field(ptr %arg1) !dbg !18 {
!11 = !{!6, !7, !8} ; C -> A, B, C

!18 = distinct !DISubprogram(name: "SP", scope: !3, file: !2, spFlags: DISPFlagDefinition, unit: !1)

; // -----

; Verify the string type is handled correctly

define void @string_type(ptr %arg1) {
call void @llvm.dbg.value(metadata ptr %arg1, metadata !4, metadata !DIExpression()), !dbg !10
call void @llvm.dbg.value(metadata ptr %arg1, metadata !9, metadata !DIExpression()), !dbg !10
ret void
}

!llvm.dbg.cu = !{!1}
!llvm.module.flags = !{!0}
!0 = !{i32 2, !"Debug Info Version", i32 3}
!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2)
!2 = !DIFile(filename: "debug-info.ll", directory: "/")
!3 = !DIBasicType(name: "int", size: 32, encoding: DW_ATE_signed)
!4 = !DILocalVariable(scope: !5, name: "string_size", file: !2, type: !3);
!5 = distinct !DISubprogram(name: "class_field", scope: !2, file: !2, spFlags: DISPFlagDefinition, unit: !1)
!6 = !DIStringType(name: "character(*)", stringLength: !4, size: 32, align: 8, stringLengthExpression: !8, stringLocationExpression: !7)
!7 = !DIExpression(DW_OP_push_object_address, DW_OP_deref)
!8 = !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 8)
!9 = !DILocalVariable(scope: !5, name: "str", file: !2, type: !6);
!10 = !DILocation(line: 1, column: 2, scope: !5)

; CHECK: #[[VAR:.+]] = #llvm.di_local_variable<{{.*}}name = "string_size"{{.*}}>
; CHECK: #llvm.di_string_type<tag = DW_TAG_string_type, name = "character(*)"
; CHECK-SAME: sizeInBits = 32
; CHECK-SAME: alignInBits = 8
; CHECK-SAME: stringLength = #[[VAR]]
; CHECK-SAME: stringLengthExp = <[DW_OP_push_object_address, DW_OP_plus_uconst(8)]>
; CHECK-SAME: stringLocationExp = <[DW_OP_push_object_address, DW_OP_deref]>>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ultra nit: missing newline

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching it. Fixed now.

27 changes: 27 additions & 0 deletions mlir/test/Target/LLVMIR/llvmir-debug.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -564,3 +564,30 @@ llvm.func @subranges(%arg: !llvm.ptr) {
// CHECK: ![[ELEMENTS2]] = !{![[ELEMENT2:[0-9]+]]}
// CHECK: ![[ELEMENT2]] = !DISubrange(count: ![[LV:[0-9]+]], stride: ![[GV:[0-9]+]])
// CHECK: ![[LV]] = !DILocalVariable(name: "size"{{.*}})

// -----

#bt = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "int", sizeInBits = 32>
#file = #llvm.di_file<"debug-info.ll" in "/">
#cu = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_C,
file = #file, isOptimized = false, emissionKind = Full>
#sp = #llvm.di_subprogram<compileUnit = #cu, scope = #file, name = "test",
file = #file, subprogramFlags = Definition>
#var = #llvm.di_local_variable<scope = #sp, name = "string_size", type = #bt>
#ty = #llvm.di_string_type<tag = DW_TAG_string_type, name = "character(*)",
sizeInBits = 32, alignInBits = 8, stringLength = #var,
stringLengthExp = <[DW_OP_push_object_address, DW_OP_plus_uconst(8)]>,
stringLocationExp = <[DW_OP_push_object_address, DW_OP_deref]>>
#var1 = #llvm.di_local_variable<scope = #sp, name = "str", type = #ty>

llvm.func @string_ty(%arg0: !llvm.ptr) {
llvm.intr.dbg.value #var1 = %arg0 : !llvm.ptr
llvm.intr.dbg.value #var = %arg0 : !llvm.ptr
llvm.return
} loc(#loc2)

#loc1 = loc("test.f90":1:1)
#loc2 = loc(fused<#sp>[#loc1])

// CHECK-DAG: !DIStringType(name: "character(*)", stringLength: ![[VAR:[0-9]+]], stringLengthExpression: !DIExpression(DW_OP_push_object_address, DW_OP_plus_uconst, 8), stringLocationExpression: !DIExpression(DW_OP_push_object_address, DW_OP_deref), size: 32, align: 8)
// CHECK-DAG: ![[VAR]] = !DILocalVariable(name: "string_size"{{.*}})
Loading