Skip to content

[flang][debug] Support complex types. #92559

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 4 commits into from
May 23, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
12 changes: 12 additions & 0 deletions flang/lib/Optimizer/Transforms/DebugTypeGenerator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,18 @@ DebugTypeGenerator::convertType(mlir::Type Ty, mlir::LLVM::DIFileAttr fileAttr,
mlir::StringAttr::get(context, logTy.getMnemonic()),
kindMapping.getLogicalBitsize(logTy.getFKind()),
llvm::dwarf::DW_ATE_boolean);
} else if (fir::isa_complex(Ty)) {
Copy link
Contributor

Choose a reason for hiding this comment

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

LLVM has some guidelines regarding else after return. https://llvm.org/docs/CodingStandards.html#don-t-use-else-after-a-return

unsigned bitWidth;
Copy link
Contributor

Choose a reason for hiding this comment

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

Would it make sense to initialize this to some value to avoid an uninitialized warning or alternatively add an else case below to assert or llvm unreachable?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think it is better to make the intention explicit. I have added an llvm_unreachable.

if (auto cplxTy = mlir::dyn_cast_or_null<mlir::ComplexType>(Ty)) {
auto floatTy = mlir::cast<mlir::FloatType>(cplxTy.getElementType());
bitWidth = floatTy.getWidth();
} else if (auto cplxTy = mlir::dyn_cast_or_null<fir::ComplexType>(Ty)) {
bitWidth = kindMapping.getRealBitsize(cplxTy.getFKind());
} else {
llvm_unreachable("Unhandled complex type");
}
return genBasicType(context, mlir::StringAttr::get(context, "complex"),
bitWidth * 2, llvm::dwarf::DW_ATE_complex_float);
} else {
// FIXME: These types are currently unhandled. We are generating a
// placeholder type to allow us to test supported bits.
Expand Down
26 changes: 26 additions & 0 deletions flang/test/Integration/debug-complex-1.f90
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
! RUN: %flang_fc1 -emit-llvm -debug-info-kind=standalone %s -o - | FileCheck %s

program mn
complex(kind=4) :: c4
complex(kind=8) :: c8
complex(kind=16) :: r
r = fn1(c4, c8)
print *, r
contains
function fn1(a, b) result (c)
complex(kind=4), intent(in) :: a
complex(kind=8), intent(in) :: b
complex(kind=16) :: c
c = a + b
end function
end program

! CHECK-DAG: ![[C4:.*]] = !DIBasicType(name: "complex", size: 64, encoding: DW_ATE_complex_float)
! CHECK-DAG: ![[C8:.*]] = !DIBasicType(name: "complex", size: 128, encoding: DW_ATE_complex_float)
! CHECK-DAG: ![[C16:.*]] = !DIBasicType(name: "complex", size: 256, encoding: DW_ATE_complex_float)
! CHECK-DAG: !DILocalVariable(name: "c4"{{.*}}type: ![[C4]])
! CHECK-DAG: !DILocalVariable(name: "c8"{{.*}}type: ![[C8]])
! CHECK-DAG: !DILocalVariable(name: "r"{{.*}}type: ![[C16]])
! CHECK-DAG: !DILocalVariable(name: "a"{{.*}}type: ![[C4]])
! CHECK-DAG: !DILocalVariable(name: "b"{{.*}}type: ![[C8]])
! CHECK-DAG: !DILocalVariable(name: "c"{{.*}}type: ![[C16]])
39 changes: 39 additions & 0 deletions flang/test/Transforms/debug-complex-1.fir
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// RUN: fir-opt --add-debug-info --mlir-print-debuginfo %s | FileCheck %s

// check conversion of complex type of different size. Both fir and mlir
// variants are checked.

module attributes {fir.defaultkind = "a1c4d8i4l4r4", fir.kindmap = "", llvm.target_triple = "native"} {
func.func @test1(%x : !fir.complex<4>) -> !fir.complex<8> {
%1 = fir.convert %x : (!fir.complex<4>) -> !fir.complex<8>
return %1 : !fir.complex<8>
}loc(#loc1)
func.func @test2(%x : !fir.complex<4>) -> complex<f64> {
%1 = fir.convert %x : (!fir.complex<4>) -> complex<f64>
return %1 : complex<f64>
}loc(#loc2)
func.func @test3(%x : !fir.complex<4>) -> !fir.complex<16> {
%1 = fir.convert %x : (!fir.complex<4>) -> !fir.complex<16>
return %1 : !fir.complex<16>
}loc(#loc3)
func.func @test4(%x : !fir.complex<4>) -> complex<f128> {
%1 = fir.convert %x : (!fir.complex<4>) -> complex<f128>
return %1 : complex<f128>
}loc(#loc4)
}
#loc1 = loc("./simple.f90":2:1)
#loc2 = loc("./simple.f90":5:1)
#loc3 = loc("./simple.f90":8:1)
#loc4 = loc("./simple.f90":11:1)

// CHECK-DAG: #[[CMPX8:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "complex", sizeInBits = 128, encoding = DW_ATE_complex_float>
// CHECK-DAG: #[[CMPX4:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "complex", sizeInBits = 64, encoding = DW_ATE_complex_float>
// CHECK-DAG: #[[CMPX16:.*]] = #llvm.di_basic_type<tag = DW_TAG_base_type, name = "complex", sizeInBits = 256, encoding = DW_ATE_complex_float>

// CHECK-DAG: #[[TY1:.*]] = #llvm.di_subroutine_type<{{.*}}types = #[[CMPX8]], #[[CMPX4]]>
// CHECK-DAG: #[[TY2:.*]] = #llvm.di_subroutine_type<{{.*}}types = #[[CMPX16]], #[[CMPX4]]>

// CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "test1"{{.*}}type = #[[TY1]]>
// CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "test2"{{.*}}type = #[[TY1]]>
// CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "test3"{{.*}}type = #[[TY2]]>
// CHECK-DAG: #llvm.di_subprogram<{{.*}}name = "test4"{{.*}}type = #[[TY2]]>
Loading