Skip to content

Commit 21addb2

Browse files
abidhNoumanAmir657
authored andcommitted
[flang][debug] Support fir.vector type. (llvm#112951)
This PR converts the `fir.vector<>` to `DICompositeTypeAttr(DW_TAG_array_type)` with `vector` flag set.
1 parent 3090d5e commit 21addb2

File tree

3 files changed

+62
-0
lines changed

3 files changed

+62
-0
lines changed

flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -431,6 +431,39 @@ mlir::LLVM::DITypeAttr DebugTypeGenerator::convertSequenceType(
431431
/*associated=*/nullptr);
432432
}
433433

434+
mlir::LLVM::DITypeAttr DebugTypeGenerator::convertVectorType(
435+
fir::VectorType vecTy, mlir::LLVM::DIFileAttr fileAttr,
436+
mlir::LLVM::DIScopeAttr scope, fir::cg::XDeclareOp declOp) {
437+
mlir::MLIRContext *context = module.getContext();
438+
439+
llvm::SmallVector<mlir::LLVM::DINodeAttr> elements;
440+
mlir::LLVM::DITypeAttr elemTy =
441+
convertType(vecTy.getEleTy(), fileAttr, scope, declOp);
442+
auto intTy = mlir::IntegerType::get(context, 64);
443+
auto countAttr =
444+
mlir::IntegerAttr::get(intTy, llvm::APInt(64, vecTy.getLen()));
445+
auto subrangeTy = mlir::LLVM::DISubrangeAttr::get(
446+
context, countAttr, /*lowerBound=*/nullptr, /*upperBound=*/nullptr,
447+
/*stride=*/nullptr);
448+
elements.push_back(subrangeTy);
449+
mlir::Type llvmTy = llvmTypeConverter.convertType(vecTy.getEleTy());
450+
uint64_t sizeInBits = dataLayout->getTypeSize(llvmTy) * vecTy.getLen() * 8;
451+
std::string name("vector");
452+
// The element type of the vector must be integer or real so it will be a
453+
// DIBasicTypeAttr.
454+
if (auto ty = mlir::dyn_cast_if_present<mlir::LLVM::DIBasicTypeAttr>(elemTy))
455+
name += " " + ty.getName().str();
456+
457+
name += " (" + std::to_string(vecTy.getLen()) + ")";
458+
return mlir::LLVM::DICompositeTypeAttr::get(
459+
context, llvm::dwarf::DW_TAG_array_type,
460+
mlir::StringAttr::get(context, name),
461+
/*file=*/nullptr, /*line=*/0, /*scope=*/nullptr, elemTy,
462+
mlir::LLVM::DIFlags::Vector, sizeInBits, /*alignInBits=*/0, elements,
463+
/*dataLocation=*/nullptr, /*rank=*/nullptr, /*allocated=*/nullptr,
464+
/*associated=*/nullptr);
465+
}
466+
434467
mlir::LLVM::DITypeAttr DebugTypeGenerator::convertCharacterType(
435468
fir::CharacterType charTy, mlir::LLVM::DIFileAttr fileAttr,
436469
mlir::LLVM::DIScopeAttr scope, fir::cg::XDeclareOp declOp,
@@ -546,6 +579,8 @@ DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
546579
return convertPointerLikeType(elTy, fileAttr, scope, declOp,
547580
/*genAllocated=*/false,
548581
/*genAssociated=*/false);
582+
} else if (auto vecTy = mlir::dyn_cast_or_null<fir::VectorType>(Ty)) {
583+
return convertVectorType(vecTy, fileAttr, scope, declOp);
549584
} else if (auto boxTy = mlir::dyn_cast_or_null<fir::BoxType>(Ty)) {
550585
auto elTy = boxTy.getElementType();
551586
if (auto seqTy = mlir::dyn_cast_or_null<fir::SequenceType>(elTy))

flang/lib/Optimizer/Transforms/DebugTypeGenerator.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@ class DebugTypeGenerator {
4343
mlir::LLVM::DIFileAttr fileAttr,
4444
mlir::LLVM::DIScopeAttr scope,
4545
fir::cg::XDeclareOp declOp);
46+
mlir::LLVM::DITypeAttr convertVectorType(fir::VectorType vecTy,
47+
mlir::LLVM::DIFileAttr fileAttr,
48+
mlir::LLVM::DIScopeAttr scope,
49+
fir::cg::XDeclareOp declOp);
4650

4751
/// The 'genAllocated' is true when we want to generate 'allocated' field
4852
/// in the DICompositeType. It is needed for the allocatable arrays.
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
2+
3+
module attributes {dlti.dl_spec = #dlti.dl_spec<>} {
4+
func.func private @foo1(%arg0: !fir.vector<20:bf16>)
5+
// CHECK-DAG: #[[F16:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "real", sizeInBits = 16, encoding = DW_ATE_float>
6+
// CHECK-DAG: #llvm.di_composite_type<tag = DW_TAG_array_type, name = "vector real (20)", baseType = #[[F16]], flags = Vector, sizeInBits = 320, elements = #llvm.di_subrange<count = 20 : i64>>
7+
8+
func.func private @foo2(%arg0: !fir.vector<30:f32>)
9+
// CHECK-DAG: #[[F32:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "real", sizeInBits = 32, encoding = DW_ATE_float>
10+
// CHECK-DAG: #llvm.di_composite_type<tag = DW_TAG_array_type, name = "vector real (30)", baseType = #[[F32]], flags = Vector, sizeInBits = 960, elements = #llvm.di_subrange<count = 30 : i64>>
11+
12+
func.func private @foo3(%arg0: !fir.vector<10:f64>)
13+
// CHECK-DAG: #[[F64:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "real", sizeInBits = 64, encoding = DW_ATE_float>
14+
// CHECK-DAG: #llvm.di_composite_type<tag = DW_TAG_array_type, name = "vector real (10)", baseType = #[[F64]], flags = Vector, sizeInBits = 640, elements = #llvm.di_subrange<count = 10 : i64>>
15+
16+
func.func private @foo4(%arg0: !fir.vector<5:i32>)
17+
// CHECK-DAG: #[[I32:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "integer", sizeInBits = 32, encoding = DW_ATE_signed>
18+
// CHECK-DAG: #llvm.di_composite_type<tag = DW_TAG_array_type, name = "vector integer (5)", baseType = #[[I32]], flags = Vector, sizeInBits = 160, elements = #llvm.di_subrange<count = 5 : i64>>
19+
20+
func.func private @foo5(%arg0: !fir.vector<2:i64>)
21+
// CHECK-DAG: #[[I64:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "integer", sizeInBits = 64, encoding = DW_ATE_signed>
22+
// CHECK-DAG: #llvm.di_composite_type<tag = DW_TAG_array_type, name = "vector integer (2)", baseType = #[[I64]], flags = Vector, sizeInBits = 128, elements = #llvm.di_subrange<count = 2 : i64>>
23+
}

0 commit comments

Comments
 (0)