Skip to content

Commit 1753de2

Browse files
authored
[flang][FIR] remove fir.complex type and its fir.real element type (llvm#111025)
Final patch of https://discourse.llvm.org/t/rfc-flang-replace-usages-of-fir-complex-by-mlir-complex-type/82292 Since fir.real was only still used as fir.complex element type, this patch removes it at the same time.
1 parent bba3849 commit 1753de2

21 files changed

+46
-305
lines changed

flang/include/flang/Optimizer/CodeGen/TypeConverter.h

Lines changed: 0 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -89,25 +89,10 @@ class LLVMTypeConverter : public mlir::LLVMTypeConverter {
8989
// fir.char<k,n> --> llvm.array<n x "ix">
9090
mlir::Type convertCharType(fir::CharacterType charTy) const;
9191

92-
// Use the target specifics to figure out how to map complex to LLVM IR. The
93-
// use of complex values in function signatures is handled before conversion
94-
// to LLVM IR dialect here.
95-
//
96-
// fir.complex<T> | std.complex<T> --> llvm<"{t,t}">
97-
template <typename C>
98-
mlir::Type convertComplexType(C cmplx) const {
99-
auto eleTy = cmplx.getElementType();
100-
return convertType(specifics->complexMemoryType(eleTy));
101-
}
102-
10392
template <typename A> mlir::Type convertPointerLike(A &ty) const {
10493
return mlir::LLVM::LLVMPointerType::get(ty.getContext());
10594
}
10695

107-
// convert a front-end kind value to either a std or LLVM IR dialect type
108-
// fir.real<n> --> llvm.anyfloat where anyfloat is a kind mapping
109-
mlir::Type convertRealType(fir::KindTy kind) const;
110-
11196
// fir.array<c ... :any> --> llvm<"[...[c x any]]">
11297
mlir::Type convertSequenceType(SequenceType seq) const;
11398

flang/include/flang/Optimizer/Dialect/FIROps.td

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2735,8 +2735,8 @@ def fir_ConvertOp : fir_SimpleOneResultOp<"convert", [NoMemoryEffect]> {
27352735
}
27362736

27372737
def FortranTypeAttr : Attr<And<[CPred<"mlir::isa<mlir::TypeAttr>($_self)">,
2738-
Or<[CPred<"mlir::isa<fir::CharacterType, fir::ComplexType, "
2739-
"fir::IntegerType, fir::LogicalType, fir::RealType, "
2738+
Or<[CPred<"mlir::isa<fir::CharacterType, fir::IntegerType,"
2739+
"fir::LogicalType, mlir::FloatType, mlir::ComplexType,"
27402740
"fir::RecordType>(mlir::cast<mlir::TypeAttr>($_self).getValue())"
27412741
>]>]>, "Fortran surface type"> {
27422742
let storageType = [{ ::mlir::TypeAttr }];

flang/include/flang/Optimizer/Dialect/FIRType.h

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,10 +159,8 @@ mlir::Type dyn_cast_ptrEleTy(mlir::Type t);
159159
/// `t` is not a memory reference or box type, then returns a null `Type`.
160160
mlir::Type dyn_cast_ptrOrBoxEleTy(mlir::Type t);
161161

162-
/// Is `t` a FIR Real or MLIR Float type?
163-
inline bool isa_real(mlir::Type t) {
164-
return mlir::isa<fir::RealType, mlir::FloatType>(t);
165-
}
162+
/// Is `t` a real type?
163+
inline bool isa_real(mlir::Type t) { return mlir::isa<mlir::FloatType>(t); }
166164

167165
/// Is `t` an integral type?
168166
inline bool isa_integer(mlir::Type t) {

flang/include/flang/Optimizer/Dialect/FIRTypes.td

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -158,26 +158,6 @@ def fir_ClassType : FIR_Type<"Class", "class", [], "BaseBoxType"> {
158158
let assemblyFormat = "`<` $eleTy `>`";
159159
}
160160

161-
def fir_ComplexType : FIR_Type<"Complex", "complex"> {
162-
let summary = "Complex type";
163-
164-
let description = [{
165-
Model of a Fortran COMPLEX intrinsic type, including the KIND type
166-
parameter. COMPLEX is a floating point type with a real and imaginary
167-
member.
168-
}];
169-
170-
let parameters = (ins "KindTy":$fKind);
171-
let hasCustomAssemblyFormat = 1;
172-
173-
let extraClassDeclaration = [{
174-
using KindTy = unsigned;
175-
176-
mlir::Type getElementType() const;
177-
mlir::Type getEleType(const fir::KindMapping &kindMap) const;
178-
}];
179-
}
180-
181161
def fir_FieldType : FIR_Type<"Field", "field"> {
182162
let summary = "A field (in a RecordType) argument's type";
183163

@@ -313,26 +293,6 @@ def fir_PointerType : FIR_Type<"Pointer", "ptr"> {
313293
}];
314294
}
315295

316-
def fir_RealType : FIR_Type<"Real", "real"> {
317-
let summary = "FIR real type";
318-
319-
let description = [{
320-
Model of a Fortran REAL (and DOUBLE PRECISION) intrinsic type, including the
321-
KIND type parameter.
322-
}];
323-
324-
let parameters = (ins "KindTy":$fKind);
325-
let hasCustomAssemblyFormat = 1;
326-
327-
let extraClassDeclaration = [{
328-
using KindTy = unsigned;
329-
// Get MLIR float type with same semantics.
330-
mlir::Type getFloatType(const fir::KindMapping &kindMap) const;
331-
}];
332-
333-
let genVerifyDecl = 1;
334-
}
335-
336296
def fir_RecordType : FIR_Type<"Record", "type"> {
337297
let summary = "FIR derived type";
338298

@@ -597,8 +557,7 @@ def AnyIntegerLike : TypeConstraint<Or<[SignlessIntegerLike.predicate,
597557
AnySignedInteger.predicate, fir_IntegerType.predicate]>, "any integer">;
598558
def AnyLogicalLike : TypeConstraint<Or<[BoolLike.predicate,
599559
fir_LogicalType.predicate]>, "any logical">;
600-
def AnyRealLike : TypeConstraint<Or<[FloatLike.predicate,
601-
fir_RealType.predicate]>, "any real">;
560+
def AnyRealLike : TypeConstraint<FloatLike.predicate, "any real">;
602561
def AnyIntegerType : Type<AnyIntegerLike.predicate, "any integer">;
603562

604563
def AnyFirComplexLike : TypeConstraint<CPred<"::fir::isa_complex($_self)">,

flang/include/flang/Optimizer/HLFIR/HLFIROpBase.td

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ def AnyFortranVariable : Type<IsFortranVariablePred, "any HLFIR variable type">;
9393

9494
def AnyFortranValue : TypeConstraint<Or<[AnyLogicalLike.predicate,
9595
AnyIntegerLike.predicate, AnyRealLike.predicate,
96-
fir_ComplexType.predicate, AnyComplex.predicate,
96+
AnyFirComplexLike.predicate,
9797
hlfir_ExprType.predicate]>, "any Fortran value type">;
9898

9999

flang/lib/Lower/ConvertExpr.cpp

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5710,10 +5710,6 @@ class ArrayExprLowering {
57105710
fir::applyPathToType(seqTy.getEleTy(), components.suffixComponents);
57115711
if (!eleTy)
57125712
fir::emitFatalError(loc, "slicing path is ill-formed");
5713-
if (auto realTy = mlir::dyn_cast<fir::RealType>(eleTy))
5714-
eleTy = Fortran::lower::convertReal(realTy.getContext(),
5715-
realTy.getFKind());
5716-
57175713
// create the type of the projected array.
57185714
arrTy = fir::SequenceType::get(seqTy.getShape(), eleTy);
57195715
LLVM_DEBUG(llvm::dbgs()

flang/lib/Optimizer/Builder/FIRBuilder.cpp

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,8 +150,6 @@ mlir::Value
150150
fir::FirOpBuilder::createRealConstant(mlir::Location loc, mlir::Type fltTy,
151151
llvm::APFloat::integerPart val) {
152152
auto apf = [&]() -> llvm::APFloat {
153-
if (auto ty = mlir::dyn_cast<fir::RealType>(fltTy))
154-
return llvm::APFloat(kindMap.getFloatSemantics(ty.getFKind()), val);
155153
if (fltTy.isF16())
156154
return llvm::APFloat(llvm::APFloat::IEEEhalf(), val);
157155
if (fltTy.isBF16())

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -589,9 +589,7 @@ struct CallOpConversion : public fir::FIROpConversion<fir::CallOp> {
589589
} // namespace
590590

591591
static mlir::Type getComplexEleTy(mlir::Type complex) {
592-
if (auto cc = mlir::dyn_cast<mlir::ComplexType>(complex))
593-
return cc.getElementType();
594-
return mlir::cast<fir::ComplexType>(complex).getElementType();
592+
return mlir::cast<mlir::ComplexType>(complex).getElementType();
595593
}
596594

597595
namespace {

flang/lib/Optimizer/CodeGen/Target.cpp

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,6 @@ llvm::StringRef Attributes::getIntExtensionAttrName() const {
4141
static const llvm::fltSemantics &floatToSemantics(const KindMapping &kindMap,
4242
mlir::Type type) {
4343
assert(isa_real(type));
44-
if (auto ty = mlir::dyn_cast<fir::RealType>(type))
45-
return kindMap.getFloatSemantics(ty.getFKind());
4644
return mlir::cast<mlir::FloatType>(type).getFloatSemantics();
4745
}
4846

@@ -356,7 +354,7 @@ struct TargetX86_64 : public GenericTarget<TargetX86_64> {
356354
else
357355
current = ArgClass::Integer;
358356
})
359-
.template Case<mlir::FloatType, fir::RealType>([&](mlir::Type floatTy) {
357+
.template Case<mlir::FloatType>([&](mlir::Type floatTy) {
360358
const auto *sem = &floatToSemantics(kindMap, floatTy);
361359
if (sem == &llvm::APFloat::x87DoubleExtended()) {
362360
Lo = ArgClass::X87;
@@ -540,9 +538,16 @@ struct TargetX86_64 : public GenericTarget<TargetX86_64> {
540538
if (typeList.size() != 1)
541539
return {};
542540
mlir::Type fieldType = typeList[0].second;
543-
if (mlir::isa<mlir::FloatType, mlir::IntegerType, fir::RealType,
544-
fir::CharacterType, fir::LogicalType>(fieldType))
541+
if (mlir::isa<mlir::FloatType, mlir::IntegerType, fir::LogicalType>(
542+
fieldType))
545543
return fieldType;
544+
if (mlir::isa<fir::CharacterType>(fieldType)) {
545+
// Only CHARACTER(1) are expected in BIND(C) contexts, which is the only
546+
// contexts where derived type may be passed in registers.
547+
assert(mlir::cast<fir::CharacterType>(fieldType).getLen() == 1 &&
548+
"fir.type value arg character components must have length 1");
549+
return fieldType;
550+
}
546551
// Complex field that needs to be split, or array.
547552
return {};
548553
}

flang/lib/Optimizer/CodeGen/TargetRewrite.cpp

Lines changed: 12 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -351,11 +351,6 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
351351
if (fnTy.getResults().size() == 1) {
352352
mlir::Type ty = fnTy.getResult(0);
353353
llvm::TypeSwitch<mlir::Type>(ty)
354-
.template Case<fir::ComplexType>([&](fir::ComplexType cmplx) {
355-
wrap = rewriteCallComplexResultType(loc, cmplx, newResTys,
356-
newInTyAndAttrs, newOpers,
357-
savedStackPtr);
358-
})
359354
.template Case<mlir::ComplexType>([&](mlir::ComplexType cmplx) {
360355
wrap = rewriteCallComplexResultType(loc, cmplx, newResTys,
361356
newInTyAndAttrs, newOpers,
@@ -414,10 +409,6 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
414409
}
415410
}
416411
})
417-
.template Case<fir::ComplexType>([&](fir::ComplexType cmplx) {
418-
rewriteCallComplexInputType(loc, cmplx, oper, newInTyAndAttrs,
419-
newOpers, savedStackPtr);
420-
})
421412
.template Case<mlir::ComplexType>([&](mlir::ComplexType cmplx) {
422413
rewriteCallComplexInputType(loc, cmplx, oper, newInTyAndAttrs,
423414
newOpers, savedStackPtr);
@@ -538,10 +529,10 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
538529
}
539530
}
540531

541-
// Result type fixup for fir::ComplexType and mlir::ComplexType
542-
template <typename A, typename B>
532+
// Result type fixup for ComplexType.
533+
template <typename Ty>
543534
void lowerComplexSignatureRes(
544-
mlir::Location loc, A cmplx, B &newResTys,
535+
mlir::Location loc, mlir::ComplexType cmplx, Ty &newResTys,
545536
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs) {
546537
if (noComplexConversion) {
547538
newResTys.push_back(cmplx);
@@ -557,10 +548,9 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
557548
}
558549
}
559550

560-
// Argument type fixup for fir::ComplexType and mlir::ComplexType
561-
template <typename A>
551+
// Argument type fixup for ComplexType.
562552
void lowerComplexSignatureArg(
563-
mlir::Location loc, A cmplx,
553+
mlir::Location loc, mlir::ComplexType cmplx,
564554
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs) {
565555
if (noComplexConversion) {
566556
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(cmplx));
@@ -602,9 +592,6 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
602592
auto loc = addrOp.getLoc();
603593
for (mlir::Type ty : addrTy.getResults()) {
604594
llvm::TypeSwitch<mlir::Type>(ty)
605-
.Case<fir::ComplexType>([&](fir::ComplexType ty) {
606-
lowerComplexSignatureRes(loc, ty, newResTys, newInTyAndAttrs);
607-
})
608595
.Case<mlir::ComplexType>([&](mlir::ComplexType ty) {
609596
lowerComplexSignatureRes(loc, ty, newResTys, newInTyAndAttrs);
610597
})
@@ -628,9 +615,6 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
628615
}
629616
}
630617
})
631-
.Case<fir::ComplexType>([&](fir::ComplexType ty) {
632-
lowerComplexSignatureArg(loc, ty, newInTyAndAttrs);
633-
})
634618
.Case<mlir::ComplexType>([&](mlir::ComplexType ty) {
635619
lowerComplexSignatureArg(loc, ty, newInTyAndAttrs);
636620
})
@@ -766,12 +750,6 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
766750
// Convert return value(s)
767751
for (auto ty : funcTy.getResults())
768752
llvm::TypeSwitch<mlir::Type>(ty)
769-
.Case<fir::ComplexType>([&](fir::ComplexType cmplx) {
770-
if (noComplexConversion)
771-
newResTys.push_back(cmplx);
772-
else
773-
doComplexReturn(func, cmplx, newResTys, newInTyAndAttrs, fixups);
774-
})
775753
.Case<mlir::ComplexType>([&](mlir::ComplexType cmplx) {
776754
if (noComplexConversion)
777755
newResTys.push_back(cmplx);
@@ -835,9 +813,6 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
835813
}
836814
}
837815
})
838-
.Case<fir::ComplexType>([&](fir::ComplexType cmplx) {
839-
doComplexArg(func, cmplx, newInTyAndAttrs, fixups);
840-
})
841816
.Case<mlir::ComplexType>([&](mlir::ComplexType cmplx) {
842817
doComplexArg(func, cmplx, newInTyAndAttrs, fixups);
843818
})
@@ -1090,10 +1065,11 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
10901065
/// Convert a complex return value. This can involve converting the return
10911066
/// value to a "hidden" first argument or packing the complex into a wide
10921067
/// GPR.
1093-
template <typename A, typename B, typename C>
1094-
void doComplexReturn(mlir::func::FuncOp func, A cmplx, B &newResTys,
1068+
template <typename Ty, typename FIXUPS>
1069+
void doComplexReturn(mlir::func::FuncOp func, mlir::ComplexType cmplx,
1070+
Ty &newResTys,
10951071
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs,
1096-
C &fixups) {
1072+
FIXUPS &fixups) {
10971073
if (noComplexConversion) {
10981074
newResTys.push_back(cmplx);
10991075
return;
@@ -1194,10 +1170,10 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
11941170
/// Convert a complex argument value. This can involve storing the value to
11951171
/// a temporary memory location or factoring the value into two distinct
11961172
/// arguments.
1197-
template <typename A, typename B>
1198-
void doComplexArg(mlir::func::FuncOp func, A cmplx,
1173+
template <typename FIXUPS>
1174+
void doComplexArg(mlir::func::FuncOp func, mlir::ComplexType cmplx,
11991175
fir::CodeGenSpecifics::Marshalling &newInTyAndAttrs,
1200-
B &fixups) {
1176+
FIXUPS &fixups) {
12011177
if (noComplexConversion) {
12021178
newInTyAndAttrs.push_back(fir::CodeGenSpecifics::getTypeAndAttr(cmplx));
12031179
return;

flang/lib/Optimizer/CodeGen/TypeConverter.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,6 @@ LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
5757
[&](fir::ClassType classTy) { return convertBoxType(classTy); });
5858
addConversion(
5959
[&](fir::CharacterType charTy) { return convertCharType(charTy); });
60-
addConversion(
61-
[&](fir::ComplexType cmplx) { return convertComplexType(cmplx); });
6260
addConversion([&](fir::FieldType field) {
6361
// Convert to i32 because of LLVM GEP indexing restriction.
6462
return mlir::IntegerType::get(field.getContext(), 32);
@@ -86,8 +84,6 @@ LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
8684
[&](fir::RecordType derived, llvm::SmallVectorImpl<mlir::Type> &results) {
8785
return convertRecordType(derived, results);
8886
});
89-
addConversion(
90-
[&](fir::RealType real) { return convertRealType(real.getFKind()); });
9187
addConversion(
9288
[&](fir::ReferenceType ref) { return convertPointerLike(ref); });
9389
addConversion([&](fir::SequenceType sequence) {
@@ -277,13 +273,6 @@ mlir::Type LLVMTypeConverter::convertCharType(fir::CharacterType charTy) const {
277273
return mlir::LLVM::LLVMArrayType::get(iTy, charTy.getLen());
278274
}
279275

280-
// convert a front-end kind value to either a std or LLVM IR dialect type
281-
// fir.real<n> --> llvm.anyfloat where anyfloat is a kind mapping
282-
mlir::Type LLVMTypeConverter::convertRealType(fir::KindTy kind) const {
283-
return fir::fromRealTypeID(&getContext(), kindMapping.getRealTypeID(kind),
284-
kind);
285-
}
286-
287276
// fir.array<c ... :any> --> llvm<"[...[c x any]]">
288277
mlir::Type LLVMTypeConverter::convertSequenceType(SequenceType seq) const {
289278
auto baseTy = convertType(seq.getEleTy());

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1347,7 +1347,7 @@ bool fir::ConvertOp::isIntegerCompatible(mlir::Type ty) {
13471347
}
13481348

13491349
bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) {
1350-
return mlir::isa<mlir::FloatType, fir::RealType>(ty);
1350+
return mlir::isa<mlir::FloatType>(ty);
13511351
}
13521352

13531353
bool fir::ConvertOp::isPointerCompatible(mlir::Type ty) {
@@ -1533,8 +1533,6 @@ llvm::LogicalResult fir::CoordinateOp::verify() {
15331533
} else if (auto t = mlir::dyn_cast<fir::RecordType>(eleTy)) {
15341534
// FIXME: This is the same as the tuple case.
15351535
return mlir::success();
1536-
} else if (auto t = mlir::dyn_cast<fir::ComplexType>(eleTy)) {
1537-
eleTy = t.getElementType();
15381536
} else if (auto t = mlir::dyn_cast<mlir::ComplexType>(eleTy)) {
15391537
eleTy = t.getElementType();
15401538
} else if (auto t = mlir::dyn_cast<fir::CharacterType>(eleTy)) {
@@ -4389,14 +4387,6 @@ mlir::Type fir::applyPathToType(mlir::Type eleTy, mlir::ValueRange path) {
43894387
return ty.getType(fir::toInt(off));
43904388
return mlir::Type{};
43914389
})
4392-
.Case<fir::ComplexType>([&](fir::ComplexType ty) {
4393-
auto x = *i;
4394-
if (auto *op = (*i++).getDefiningOp())
4395-
if (fir::isa_integer(x.getType()))
4396-
return ty.getEleType(fir::getKindMapping(
4397-
op->getParentOfType<mlir::ModuleOp>()));
4398-
return mlir::Type{};
4399-
})
44004390
.Case<mlir::ComplexType>([&](mlir::ComplexType ty) {
44014391
if (fir::isa_integer((*i++).getType()))
44024392
return ty.getElementType();

0 commit comments

Comments
 (0)