Skip to content

Commit 63a4133

Browse files
authored
[flang][debug] Support complex types. (#92559)
This PR adds supports for conversion of complex type to corresponding DITypeAttr. Both fir and mlir types are supported. Apart from lit testing, I have also tested the types in debugger and they work correctly. An exception is 128 bit complex which somehow requires that its name be different from `complex`. I am going to open a separate PR to add (kind=n) in the type names similar to what gfortran does.
1 parent fbd643f commit 63a4133

File tree

3 files changed

+77
-0
lines changed

3 files changed

+77
-0
lines changed

flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,18 @@ DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
5757
mlir::StringAttr::get(context, logTy.getMnemonic()),
5858
kindMapping.getLogicalBitsize(logTy.getFKind()),
5959
llvm::dwarf::DW_ATE_boolean);
60+
} else if (fir::isa_complex(Ty)) {
61+
unsigned bitWidth;
62+
if (auto cplxTy = mlir::dyn_cast_or_null<mlir::ComplexType>(Ty)) {
63+
auto floatTy = mlir::cast<mlir::FloatType>(cplxTy.getElementType());
64+
bitWidth = floatTy.getWidth();
65+
} else if (auto cplxTy = mlir::dyn_cast_or_null<fir::ComplexType>(Ty)) {
66+
bitWidth = kindMapping.getRealBitsize(cplxTy.getFKind());
67+
} else {
68+
llvm_unreachable("Unhandled complex type");
69+
}
70+
return genBasicType(context, mlir::StringAttr::get(context, "complex"),
71+
bitWidth * 2, llvm::dwarf::DW_ATE_complex_float);
6072
} else {
6173
// FIXME: These types are currently unhandled. We are generating a
6274
// placeholder type to allow us to test supported bits.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o - | FileCheck %s
2+
3+
program mn
4+
complex(kind=4) :: c4
5+
complex(kind=8) :: c8
6+
complex(kind=16) :: r
7+
r = fn1(c4, c8)
8+
print *, r
9+
contains
10+
function fn1(a, b) result (c)
11+
complex(kind=4), intent(in) :: a
12+
complex(kind=8), intent(in) :: b
13+
complex(kind=16) :: c
14+
c = a + b
15+
end function
16+
end program
17+
18+
! CHECK-DAG: ![[C4:.*]] = !DIBasicType(name: "complex", size: 64, encoding: DW_ATE_complex_float)
19+
! CHECK-DAG: ![[C8:.*]] = !DIBasicType(name: "complex", size: 128, encoding: DW_ATE_complex_float)
20+
! CHECK-DAG: ![[C16:.*]] = !DIBasicType(name: "complex", size: 256, encoding: DW_ATE_complex_float)
21+
! CHECK-DAG: !DILocalVariable(name: "c4"{{.*}}type: ![[C4]])
22+
! CHECK-DAG: !DILocalVariable(name: "c8"{{.*}}type: ![[C8]])
23+
! CHECK-DAG: !DILocalVariable(name: "r"{{.*}}type: ![[C16]])
24+
! CHECK-DAG: !DILocalVariable(name: "a"{{.*}}type: ![[C4]])
25+
! CHECK-DAG: !DILocalVariable(name: "b"{{.*}}type: ![[C8]])
26+
! CHECK-DAG: !DILocalVariable(name: "c"{{.*}}type: ![[C16]])
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s
2+
3+
// check conversion of complex type of different size. Both fir and mlir
4+
// variants are checked.
5+
6+
module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "native"} {
7+
func.func @test1(%x : !fir.complex<4>) -> !fir.complex<8> {
8+
%1 = fir.convert %x : (!fir.complex<4>) -> !fir.complex<8>
9+
return %1 : !fir.complex<8>
10+
}loc(#loc1)
11+
func.func @test2(%x : !fir.complex<4>) -> complex<f64> {
12+
%1 = fir.convert %x : (!fir.complex<4>) -> complex<f64>
13+
return %1 : complex<f64>
14+
}loc(#loc2)
15+
func.func @test3(%x : !fir.complex<4>) -> !fir.complex<16> {
16+
%1 = fir.convert %x : (!fir.complex<4>) -> !fir.complex<16>
17+
return %1 : !fir.complex<16>
18+
}loc(#loc3)
19+
func.func @test4(%x : !fir.complex<4>) -> complex<f128> {
20+
%1 = fir.convert %x : (!fir.complex<4>) -> complex<f128>
21+
return %1 : complex<f128>
22+
}loc(#loc4)
23+
}
24+
#loc1 = loc("./simple.f90":2:1)
25+
#loc2 = loc("./simple.f90":5:1)
26+
#loc3 = loc("./simple.f90":8:1)
27+
#loc4 = loc("./simple.f90":11:1)
28+
29+
// CHECK-DAG: #[[CMPX8:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "complex", sizeInBits = 128, encoding = DW_ATE_complex_float>
30+
// CHECK-DAG: #[[CMPX4:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "complex", sizeInBits = 64, encoding = DW_ATE_complex_float>
31+
// CHECK-DAG: #[[CMPX16:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "complex", sizeInBits = 256, encoding = DW_ATE_complex_float>
32+
33+
// CHECK-DAG: #[[TY1:.*]] = #llvm.di_subroutine_type<{{.*}}types = #[[CMPX8]], #[[CMPX4]]>
34+
// CHECK-DAG: #[[TY2:.*]] = #llvm.di_subroutine_type<{{.*}}types = #[[CMPX16]], #[[CMPX4]]>
35+
36+
// CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "test1"{{.*}}type = #[[TY1]]>
37+
// CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "test2"{{.*}}type = #[[TY1]]>
38+
// CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "test3"{{.*}}type = #[[TY2]]>
39+
// CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "test4"{{.*}}type = #[[TY2]]>

0 commit comments

Comments
 (0)