Skip to content

Commit

Permalink
[MLIR][LLVM] Add DebugNameTableKind to DICompileUnit (llvm#87974)
Browse files Browse the repository at this point in the history
Add the DebugNameTableKind field to DICompileUnit, along with its
importer & exporter.
  • Loading branch information
zyx-billy authored Apr 9, 2024
1 parent 961d91a commit 6f63368
Show file tree
Hide file tree
Showing 11 changed files with 66 additions and 16 deletions.
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/Transforms/AddDebugFoundation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ void AddDebugFoundationPass::runOnOperation() {
mlir::LLVM::DIFileAttr fileAttr = getFileAttr(inputFilePath);
mlir::StringAttr producer = mlir::StringAttr::get(context, "Flang");
mlir::LLVM::DICompileUnitAttr cuAttr = mlir::LLVM::DICompileUnitAttr::get(
context, mlir::DistinctAttr::create(mlir::UnitAttr::get(context)),
mlir::DistinctAttr::create(mlir::UnitAttr::get(context)),
llvm::dwarf::getLanguage("DW_LANG_Fortran95"), fileAttr, producer,
/*isOptimized=*/false, mlir::LLVM::DIEmissionKind::LineTablesOnly);

Expand Down
10 changes: 9 additions & 1 deletion mlir/include/mlir-c/Dialect/LLVM.h
Original file line number Diff line number Diff line change
Expand Up @@ -257,11 +257,19 @@ enum MlirLLVMDIEmissionKind {
};
typedef enum MlirLLVMDIEmissionKind MlirLLVMDIEmissionKind;

enum MlirLLVMDINameTableKind {
MlirLLVMDINameTableKindDefault = 0,
MlirLLVMDINameTableKindGNU = 1,
MlirLLVMDINameTableKindNone = 2,
MlirLLVMDINameTableKindApple = 3,
};
typedef enum MlirLLVMDINameTableKind MlirLLVMDINameTableKind;

/// Creates a LLVM DICompileUnit attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDICompileUnitAttrGet(
MlirContext ctx, MlirAttribute id, unsigned int sourceLanguage,
MlirAttribute file, MlirAttribute producer, bool isOptimized,
MlirLLVMDIEmissionKind emissionKind);
MlirLLVMDIEmissionKind emissionKind, MlirLLVMDINameTableKind nameTableKind);

/// Creates a LLVM DIFlags attribute.
MLIR_CAPI_EXPORTED MlirAttribute mlirLLVMDIFlagsAttrGet(MlirContext ctx,
Expand Down
14 changes: 13 additions & 1 deletion mlir/include/mlir/Dialect/LLVMIR/LLVMAttrDefs.td
Original file line number Diff line number Diff line change
Expand Up @@ -350,8 +350,20 @@ def LLVM_DICompileUnitAttr : LLVM_Attr<"DICompileUnit", "di_compile_unit",
"DIFileAttr":$file,
OptionalParameter<"StringAttr">:$producer,
"bool":$isOptimized,
"DIEmissionKind":$emissionKind
"DIEmissionKind":$emissionKind,
OptionalParameter<"DINameTableKind">:$nameTableKind
);
let builders = [
AttrBuilderWithInferredContext<(ins
"DistinctAttr":$id, "unsigned":$sourceLanguage, "DIFileAttr":$file,
"StringAttr":$producer, "bool":$isOptimized,
"DIEmissionKind":$emissionKind,
CArg<"DINameTableKind", "DINameTableKind::Default">:$nameTableKind
), [{
return $_get(id.getContext(), id, sourceLanguage, file, producer,
isOptimized, emissionKind, nameTableKind);
}]>
];
let assemblyFormat = "`<` struct(params) `>`";
}

Expand Down
20 changes: 20 additions & 0 deletions mlir/include/mlir/Dialect/LLVMIR/LLVMEnums.td
Original file line number Diff line number Diff line change
Expand Up @@ -393,6 +393,26 @@ def DIFlags : I32BitEnumAttr<
let printBitEnumPrimaryGroups = 1;
}

//===----------------------------------------------------------------------===//
// DINameTableKind
//===----------------------------------------------------------------------===//

def LLVM_DINameTableDefault : I64EnumAttrCase<"Default", 0>;
def LLVM_DINameTableGNU : I64EnumAttrCase<"GNU", 1>;
def LLVM_DINameTableNone : I64EnumAttrCase<"None", 2>;
def LLVM_DINameTableApple : I64EnumAttrCase<"Apple", 3>;

def LLVM_DINameTableKind : I64EnumAttr<
"DINameTableKind",
"LLVM debug name table kind", [
LLVM_DINameTableDefault,
LLVM_DINameTableGNU,
LLVM_DINameTableNone,
LLVM_DINameTableApple,
]> {
let cppNamespace = "::mlir::LLVM";
}

//===----------------------------------------------------------------------===//
// DISubprogramFlags
//===----------------------------------------------------------------------===//
Expand Down
6 changes: 4 additions & 2 deletions mlir/lib/CAPI/Dialect/LLVM.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -206,11 +206,13 @@ MlirAttribute
mlirLLVMDICompileUnitAttrGet(MlirContext ctx, MlirAttribute id,
unsigned int sourceLanguage, MlirAttribute file,
MlirAttribute producer, bool isOptimized,
MlirLLVMDIEmissionKind emissionKind) {
MlirLLVMDIEmissionKind emissionKind,
MlirLLVMDINameTableKind nameTableKind) {
return wrap(DICompileUnitAttr::get(
unwrap(ctx), cast<DistinctAttr>(unwrap(id)), sourceLanguage,
cast<DIFileAttr>(unwrap(file)), cast<StringAttr>(unwrap(producer)),
isOptimized, DIEmissionKind(emissionKind)));
isOptimized, DIEmissionKind(emissionKind),
DINameTableKind(nameTableKind)));
}

MlirAttribute mlirLLVMDIFlagsAttrGet(MlirContext ctx, uint64_t value) {
Expand Down
4 changes: 2 additions & 2 deletions mlir/lib/Dialect/LLVMIR/Transforms/DIScopeForLLVMFuncOp.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -115,8 +115,8 @@ struct DIScopeForLLVMFuncOp
}

compileUnitAttr = LLVM::DICompileUnitAttr::get(
context, DistinctAttr::create(UnitAttr::get(context)),
llvm::dwarf::DW_LANG_C, fileAttr, StringAttr::get(context, "MLIR"),
DistinctAttr::create(UnitAttr::get(context)), llvm::dwarf::DW_LANG_C,
fileAttr, StringAttr::get(context, "MLIR"),
/*isOptimized=*/true, LLVM::DIEmissionKind::LineTablesOnly);
}

Expand Down
6 changes: 5 additions & 1 deletion mlir/lib/Target/LLVMIR/DebugImporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,14 @@ DIBasicTypeAttr DebugImporter::translateImpl(llvm::DIBasicType *node) {
DICompileUnitAttr DebugImporter::translateImpl(llvm::DICompileUnit *node) {
std::optional<DIEmissionKind> emissionKind =
symbolizeDIEmissionKind(node->getEmissionKind());
std::optional<DINameTableKind> nameTableKind = symbolizeDINameTableKind(
static_cast<
std::underlying_type_t<llvm::DICompileUnit::DebugNameTableKind>>(
node->getNameTableKind()));
return DICompileUnitAttr::get(
context, getOrCreateDistinctID(node), node->getSourceLanguage(),
translate(node->getFile()), getStringAttrOrNull(node->getRawProducer()),
node->isOptimized(), emissionKind.value());
node->isOptimized(), emissionKind.value(), nameTableKind.value());
}

DICompositeTypeAttr DebugImporter::translateImpl(llvm::DICompositeType *node) {
Expand Down
5 changes: 4 additions & 1 deletion mlir/lib/Target/LLVMIR/DebugTranslation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,10 @@ llvm::DICompileUnit *DebugTranslation::translateImpl(DICompileUnitAttr attr) {
attr.getIsOptimized(),
/*Flags=*/"", /*RV=*/0, /*SplitName=*/{},
static_cast<llvm::DICompileUnit::DebugEmissionKind>(
attr.getEmissionKind()));
attr.getEmissionKind()),
0, true, false,
static_cast<llvm::DICompileUnit::DebugNameTableKind>(
attr.getNameTableKind()));
}

/// Returns a new `DINodeT` that is either distinct or not, depending on
Expand Down
6 changes: 3 additions & 3 deletions mlir/test/CAPI/llvm.c
Original file line number Diff line number Diff line change
Expand Up @@ -264,9 +264,9 @@ static void testDebugInfoAttributes(MlirContext ctx) {
// CHECK: #llvm.di_file<"foo" in "bar">
mlirAttributeDump(file);

MlirAttribute compile_unit =
mlirLLVMDICompileUnitAttrGet(ctx, id, LLVMDWARFSourceLanguageC99, file,
foo, false, MlirLLVMDIEmissionKindFull);
MlirAttribute compile_unit = mlirLLVMDICompileUnitAttrGet(
ctx, id, LLVMDWARFSourceLanguageC99, file, foo, false,
MlirLLVMDIEmissionKindFull, MlirLLVMDINameTableKindDefault);

// CHECK: #llvm.di_compile_unit<{{.*}}>
mlirAttributeDump(compile_unit);
Expand Down
4 changes: 2 additions & 2 deletions mlir/test/Target/LLVMIR/Import/debug-info.ll
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,7 @@ define void @composite_type() !dbg !3 {
; // -----

; CHECK-DAG: #[[FILE:.+]] = #llvm.di_file<"debug-info.ll" in "/">
; CHECK-DAG: #[[CU:.+]] = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_C, file = #[[FILE]], isOptimized = false, emissionKind = None>
; CHECK-DAG: #[[CU:.+]] = #llvm.di_compile_unit<id = distinct[0]<>, sourceLanguage = DW_LANG_C, file = #[[FILE]], isOptimized = false, emissionKind = None, nameTableKind = None>
; Verify an empty subroutine types list is supported.
; CHECK-DAG: #[[SP_TYPE:.+]] = #llvm.di_subroutine_type<callingConvention = DW_CC_normal>
; CHECK-DAG: #[[SP:.+]] = #llvm.di_subprogram<id = distinct[{{.*}}]<>, compileUnit = #[[CU]], scope = #[[FILE]], name = "subprogram", linkageName = "subprogram", file = #[[FILE]], line = 42, scopeLine = 42, subprogramFlags = Definition, type = #[[SP_TYPE]]>
Expand All @@ -209,7 +209,7 @@ define void @subprogram() !dbg !3 {
!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)
!1 = distinct !DICompileUnit(language: DW_LANG_C, file: !2, nameTableKind: None)
!2 = !DIFile(filename: "debug-info.ll", directory: "/")
!3 = distinct !DISubprogram(name: "subprogram", linkageName: "subprogram", scope: !2, file: !2, line: 42, scopeLine: 42, spFlags: DISPFlagDefinition, unit: !1, type: !4)
!4 = !DISubroutineType(cc: DW_CC_normal, types: !5)
Expand Down
5 changes: 3 additions & 2 deletions mlir/test/Target/LLVMIR/llvmir-debug.mlir
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@ llvm.func @func_no_debug() {
>
#cu = #llvm.di_compile_unit<
id = distinct[0]<>, sourceLanguage = DW_LANG_C, file = #file,
producer = "MLIR", isOptimized = true, emissionKind = Full
producer = "MLIR", isOptimized = true, emissionKind = Full,
nameTableKind = None
>
#composite = #llvm.di_composite_type<
tag = DW_TAG_structure_type, name = "composite", file = #file,
Expand Down Expand Up @@ -127,7 +128,7 @@ llvm.func @empty_types() {
llvm.return
} loc(fused<#sp1>["foo.mlir":2:1])

// CHECK: ![[CU_LOC:.*]] = distinct !DICompileUnit(language: DW_LANG_C, file: ![[CU_FILE_LOC:.*]], producer: "MLIR", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug)
// CHECK: ![[CU_LOC:.*]] = distinct !DICompileUnit(language: DW_LANG_C, file: ![[CU_FILE_LOC:.*]], producer: "MLIR", isOptimized: true, runtimeVersion: 0, emissionKind: FullDebug, nameTableKind: None)
// CHECK: ![[CU_FILE_LOC]] = !DIFile(filename: "foo.mlir", directory: "/test/")

// CHECK: ![[FUNC_LOC]] = distinct !DISubprogram(name: "func_with_debug", linkageName: "func_with_debug", scope: ![[NESTED_NAMESPACE:.*]], file: ![[CU_FILE_LOC]], line: 3, type: ![[FUNC_TYPE:.*]], scopeLine: 3, spFlags: DISPFlagDefinition | DISPFlagOptimized, unit: ![[CU_LOC]])
Expand Down

0 comments on commit 6f63368

Please sign in to comment.