Skip to content

Commit 499a259

Browse files
committed
[Flang][LoongArch] Add support for complex16 params/returns.
In LoongArch64, the passing and returning of type `complex16` is similar to that of structure type like `struct {fp128, fp128}`, meaning they are passed and returned by reference. This behavior is similar to clang, so it can implement conveniently `iso_c_binding`. Additionally, this patch fixes the failure in flang test Integration/debug-complex-1.f90: ``` llvm-project/flang/lib/Optimizer/codeGen/Target.cpp:56: not yet implemented: complex for this precision for return type ```
1 parent 29dfc18 commit 499a259

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

flang/lib/Optimizer/CodeGen/Target.cpp

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1087,11 +1087,17 @@ struct TargetLoongArch64 : public GenericTarget<TargetLoongArch64> {
10871087
CodeGenSpecifics::Marshalling marshal;
10881088
const auto *sem = &floatToSemantics(kindMap, eleTy);
10891089
if (sem == &llvm::APFloat::IEEEsingle() ||
1090-
sem == &llvm::APFloat::IEEEdouble() ||
1091-
sem == &llvm::APFloat::IEEEquad()) {
1090+
sem == &llvm::APFloat::IEEEdouble()) {
10921091
// Two distinct element type arguments (re, im)
10931092
marshal.emplace_back(eleTy, AT{});
10941093
marshal.emplace_back(eleTy, AT{});
1094+
} else if (sem == &llvm::APFloat::IEEEquad()) {
1095+
// Use a type that will be translated into LLVM as:
1096+
// { fp128, fp128 } struct of 2 fp128, byval
1097+
marshal.emplace_back(
1098+
fir::ReferenceType::get(mlir::TupleType::get(
1099+
eleTy.getContext(), mlir::TypeRange{eleTy, eleTy})),
1100+
AT{/*align=*/16, /*byval=*/true});
10951101
} else {
10961102
typeTodo(sem, loc, "argument");
10971103
}
@@ -1103,13 +1109,19 @@ struct TargetLoongArch64 : public GenericTarget<TargetLoongArch64> {
11031109
CodeGenSpecifics::Marshalling marshal;
11041110
const auto *sem = &floatToSemantics(kindMap, eleTy);
11051111
if (sem == &llvm::APFloat::IEEEsingle() ||
1106-
sem == &llvm::APFloat::IEEEdouble() ||
1107-
sem == &llvm::APFloat::IEEEquad()) {
1112+
sem == &llvm::APFloat::IEEEdouble()) {
11081113
// Use a type that will be translated into LLVM as:
11091114
// { t, t } struct of 2 eleTy, byVal
11101115
marshal.emplace_back(mlir::TupleType::get(eleTy.getContext(),
11111116
mlir::TypeRange{eleTy, eleTy}),
11121117
AT{/*alignment=*/0, /*byval=*/true});
1118+
} else if (sem == &llvm::APFloat::IEEEquad()) {
1119+
// Use a type that will be translated into LLVM as:
1120+
// { fp128, fp128 } struct of 2 fp128, sret, align 16
1121+
marshal.emplace_back(
1122+
fir::ReferenceType::get(mlir::TupleType::get(
1123+
eleTy.getContext(), mlir::TypeRange{eleTy, eleTy})),
1124+
AT{/*align=*/16, /*byval=*/false, /*sret=*/true});
11131125
} else {
11141126
typeTodo(sem, loc, "return");
11151127
}

flang/test/Fir/target-rewrite-complex16.fir

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
// RUN: fir-opt --target-rewrite="target=x86_64-unknown-linux-gnu" %s | FileCheck %s
2+
// RUN: fir-opt --target-rewrite="target=loongarch64-unknown-linux-gnu" %s | FileCheck %s
23

34
// Test that we rewrite the signature and body of a func.function that returns a
45
// complex<16>.

0 commit comments

Comments
 (0)