29class FIRToMemRefTypeConverter :
public mlir::TypeConverter {
32 bool convertComplexTypes =
false;
33 bool convertScalarTypesOnly =
false;
35 mlir::MemRefType convertMemrefBaseType(mlir::Type baseTy)
const {
36 if (
auto charTy = mlir::dyn_cast<fir::CharacterType>(baseTy)) {
37 unsigned kind = charTy.getFKind();
38 unsigned bitWidth = kindMapping.getCharacterBitsize(kind);
39 mlir::Type elTy = mlir::IntegerType::get(charTy.getContext(), bitWidth);
41 if (charTy.hasConstantLen() && charTy.getLen() == 1)
42 return mlir::MemRefType::get({}, elTy);
43 if (charTy.hasConstantLen())
44 return mlir::MemRefType::get({charTy.getLen()}, elTy);
45 return mlir::MemRefType::get({mlir::ShapedType::kDynamic}, elTy);
48 if (
auto seqTy = mlir::dyn_cast<fir::SequenceType>(baseTy)) {
49 mlir::Type ty = convertType(seqTy.getElementType());
52 for (
auto it = firShape.rbegin(); it != firShape.rend(); ++it)
54 assert(mlir::BaseMemRefType::isValidElementType(ty) &&
55 "got invalid memref element type from array fir type");
56 return mlir::MemRefType::get(shape, ty);
59 mlir::Type ty = convertType(baseTy);
60 assert(mlir::BaseMemRefType::isValidElementType(ty) &&
61 "got invalid memref element type from scalar fir type");
62 return mlir::MemRefType::get({}, ty);
66 explicit FIRToMemRefTypeConverter(mlir::ModuleOp mod)
68 addConversion([](mlir::Type type) {
return type; });
70 addConversion([&](fir::LogicalType type) -> mlir::Type {
71 return mlir::IntegerType::get(
72 type.getContext(), kindMapping.getLogicalBitsize(type.getFKind()));
75 addSourceMaterialization([](mlir::OpBuilder &builder, mlir::Type type,
76 mlir::ValueRange inputs,
77 mlir::Location loc) -> mlir::Value {
78 assert(!inputs.empty() &&
"expected a single input for materialization");
79 builder.setInsertionPointAfter(inputs[0].getDefiningOp());
80 return fir::ConvertOp::create(builder, loc, type, inputs[0]);
83 addTargetMaterialization([](mlir::OpBuilder &builder, mlir::Type type,
84 mlir::ValueRange inputs,
85 mlir::Location loc) -> mlir::Value {
86 return fir::ConvertOp::create(builder, loc, type, inputs[0]);
104 else if (
auto boxTy = mlir::dyn_cast<fir::BoxType>(ty))
112 if (
auto seqTy = mlir::dyn_cast<fir::SequenceType>(ty))
113 ty = seqTy.getElementType();
126 if (
auto refTy = mlir::dyn_cast<fir::ReferenceType>(ty))
128 else if (
auto pointerTy = mlir::dyn_cast<fir::PointerType>(ty))
130 else if (
auto heapTy = mlir::dyn_cast<fir::HeapType>(ty))
132 else if (
auto seqTy = mlir::dyn_cast<fir::SequenceType>(ty)) {
134 for (
auto shape : firShape)
145 if (!convertScalarTypesOnly) {
146 if (
auto refTy = mlir::dyn_cast<fir::ReferenceType>(type)) {
147 auto elTy = refTy.getElementType();
148 if (mlir::isa<fir::SequenceType>(elTy))
153 if (
auto seqTy = mlir::dyn_cast<fir::SequenceType>(type))
158 if (mlir::isa<fir::LogicalType>(type))
163 if (type.isUnsignedInteger())
166 if (mlir::isa<mlir::ComplexType>(type))
167 return convertComplexTypes;
169 if (mlir::isa<mlir::FunctionType>(type))
172 if (mlir::isa<mlir::TupleType>(type))
181 return convertMemrefBaseType(pointee);
183 if (
auto boxTy = mlir::dyn_cast<fir::BoxType>(firTy)) {
185 return mlir::MemRefType::Builder(memRefTy).setLayout(
186 mlir::StridedLayoutAttr::get(
187 memRefTy.getContext(), mlir::ShapedType::kDynamic,
189 mlir::ShapedType::kDynamic)));
192 return convertMemrefBaseType(firTy);
bool isa_fir_type(mlir::Type t)
Is t any of the FIR dialect types?
Definition FIRType.cpp:218