107 changes: 53 additions & 54 deletions flang/lib/Optimizer/Builder/FIRBuilder.cpp

Large diffs are not rendered by default.

78 changes: 40 additions & 38 deletions flang/lib/Optimizer/Builder/HLFIRTools.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,10 +38,10 @@ hlfir::getExplicitExtentsFromShape(mlir::Value shape,
} else if (mlir::dyn_cast_or_null<fir::ShiftOp>(shapeOp)) {
return {};
} else if (auto s = mlir::dyn_cast_or_null<hlfir::ShapeOfOp>(shapeOp)) {
hlfir::ExprType expr = mlir::cast<hlfir::ExprType>(s.getExpr().getType());
hlfir::ExprType expr = s.getExpr().getType().cast<hlfir::ExprType>();
llvm::ArrayRef<int64_t> exprShape = expr.getShape();
mlir::Type indexTy = builder.getIndexType();
fir::ShapeType shapeTy = mlir::cast<fir::ShapeType>(shape.getType());
fir::ShapeType shapeTy = shape.getType().cast<fir::ShapeType>();
result.reserve(shapeTy.getRank());
for (unsigned i = 0; i < shapeTy.getRank(); ++i) {
int64_t extent = exprShape[i];
Expand Down Expand Up @@ -99,7 +99,7 @@ genLboundsAndExtentsFromBox(mlir::Location loc, fir::FirOpBuilder &builder,
hlfir::Entity boxEntity,
llvm::SmallVectorImpl<mlir::Value> &lbounds,
llvm::SmallVectorImpl<mlir::Value> *extents) {
assert(mlir::isa<fir::BaseBoxType>(boxEntity.getType()) && "must be a box");
assert(boxEntity.getType().isa<fir::BaseBoxType>() && "must be a box");
mlir::Type idxTy = builder.getIndexType();
const int rank = boxEntity.getRank();
for (int i = 0; i < rank; ++i) {
Expand Down Expand Up @@ -154,7 +154,7 @@ static mlir::Value genCharacterVariableLength(mlir::Location loc,
hlfir::Entity var) {
if (mlir::Value len = tryGettingNonDeferredCharLen(var))
return len;
auto charType = mlir::cast<fir::CharacterType>(var.getFortranElementType());
auto charType = var.getFortranElementType().cast<fir::CharacterType>();
if (charType.hasConstantLen())
return builder.createIntegerConstant(loc, builder.getIndexType(),
charType.getLen());
Expand All @@ -172,7 +172,7 @@ static fir::CharBoxValue genUnboxChar(mlir::Location loc,
if (auto emboxChar = boxChar.getDefiningOp<fir::EmboxCharOp>())
return {emboxChar.getMemref(), emboxChar.getLen()};
mlir::Type refType = fir::ReferenceType::get(
mlir::cast<fir::BoxCharType>(boxChar.getType()).getEleTy());
boxChar.getType().cast<fir::BoxCharType>().getEleTy());
auto unboxed = builder.create<fir::UnboxCharOp>(
loc, refType, builder.getIndexType(), boxChar);
mlir::Value addr = unboxed.getResult(0);
Expand Down Expand Up @@ -252,8 +252,8 @@ hlfir::genAssociateExpr(mlir::Location loc, fir::FirOpBuilder &builder,
// and the other static).
mlir::Type varEleTy = getFortranElementType(variableType);
mlir::Type valueEleTy = getFortranElementType(value.getType());
if (varEleTy != valueEleTy && !(mlir::isa<fir::CharacterType>(valueEleTy) &&
mlir::isa<fir::CharacterType>(varEleTy))) {
if (varEleTy != valueEleTy && !(valueEleTy.isa<fir::CharacterType>() &&
varEleTy.isa<fir::CharacterType>())) {
assert(value.isScalar() && fir::isa_trivial(value.getType()));
source = builder.createConvert(loc, fir::unwrapPassByRefType(variableType),
value);
Expand All @@ -278,9 +278,9 @@ mlir::Value hlfir::genVariableRawAddress(mlir::Location loc,
if (var.isMutableBox())
baseAddr = builder.create<fir::LoadOp>(loc, baseAddr);
// Get raw address.
if (mlir::isa<fir::BoxCharType>(var.getType()))
if (var.getType().isa<fir::BoxCharType>())
baseAddr = genUnboxChar(loc, builder, var.getBase()).getAddr();
if (mlir::isa<fir::BaseBoxType>(baseAddr.getType()))
if (baseAddr.getType().isa<fir::BaseBoxType>())
baseAddr = builder.create<fir::BoxAddrOp>(loc, baseAddr);
return baseAddr;
}
Expand All @@ -289,13 +289,13 @@ mlir::Value hlfir::genVariableBoxChar(mlir::Location loc,
fir::FirOpBuilder &builder,
hlfir::Entity var) {
assert(var.isVariable() && "only address of variables can be taken");
if (mlir::isa<fir::BoxCharType>(var.getType()))
if (var.getType().isa<fir::BoxCharType>())
return var;
mlir::Value addr = genVariableRawAddress(loc, builder, var);
llvm::SmallVector<mlir::Value> lengths;
genLengthParameters(loc, builder, var, lengths);
assert(lengths.size() == 1);
auto charType = mlir::cast<fir::CharacterType>(var.getFortranElementType());
auto charType = var.getFortranElementType().cast<fir::CharacterType>();
auto boxCharType =
fir::BoxCharType::get(builder.getContext(), charType.getFKind());
auto scalarAddr =
Expand All @@ -309,19 +309,19 @@ hlfir::Entity hlfir::genVariableBox(mlir::Location loc,
hlfir::Entity var) {
assert(var.isVariable() && "must be a variable");
var = hlfir::derefPointersAndAllocatables(loc, builder, var);
if (mlir::isa<fir::BaseBoxType>(var.getType()))
if (var.getType().isa<fir::BaseBoxType>())
return var;
// Note: if the var is not a fir.box/fir.class at that point, it has default
// lower bounds and is not polymorphic.
mlir::Value shape =
var.isArray() ? hlfir::genShape(loc, builder, var) : mlir::Value{};
llvm::SmallVector<mlir::Value> typeParams;
auto maybeCharType =
mlir::dyn_cast<fir::CharacterType>(var.getFortranElementType());
var.getFortranElementType().dyn_cast<fir::CharacterType>();
if (!maybeCharType || maybeCharType.hasDynamicLen())
hlfir::genLengthParameters(loc, builder, var, typeParams);
mlir::Value addr = var.getBase();
if (mlir::isa<fir::BoxCharType>(var.getType()))
if (var.getType().isa<fir::BoxCharType>())
addr = genVariableRawAddress(loc, builder, var);
mlir::Type boxType = fir::BoxType::get(var.getElementOrSequenceType());
auto embox =
Expand All @@ -348,7 +348,7 @@ hlfir::Entity hlfir::getElementAt(mlir::Location loc,
return entity;
llvm::SmallVector<mlir::Value> lenParams;
genLengthParameters(loc, builder, entity, lenParams);
if (mlir::isa<hlfir::ExprType>(entity.getType()))
if (entity.getType().isa<hlfir::ExprType>())
return hlfir::Entity{builder.create<hlfir::ApplyOp>(
loc, entity, oneBasedIndices, lenParams)};
// Build hlfir.designate. The lower bounds may need to be added to
Expand Down Expand Up @@ -394,7 +394,7 @@ static mlir::Value genUBound(mlir::Location loc, fir::FirOpBuilder &builder,
llvm::SmallVector<std::pair<mlir::Value, mlir::Value>>
hlfir::genBounds(mlir::Location loc, fir::FirOpBuilder &builder,
Entity entity) {
if (mlir::isa<hlfir::ExprType>(entity.getType()))
if (entity.getType().isa<hlfir::ExprType>())
TODO(loc, "bounds of expressions in hlfir");
auto [exv, cleanup] = translateToExtendedValue(loc, builder, entity);
assert(!cleanup && "translation of entity should not yield cleanup");
Expand All @@ -415,8 +415,8 @@ hlfir::genBounds(mlir::Location loc, fir::FirOpBuilder &builder,
llvm::SmallVector<std::pair<mlir::Value, mlir::Value>>
hlfir::genBounds(mlir::Location loc, fir::FirOpBuilder &builder,
mlir::Value shape) {
assert((mlir::isa<fir::ShapeShiftType>(shape.getType()) ||
mlir::isa<fir::ShapeType>(shape.getType())) &&
assert((shape.getType().isa<fir::ShapeShiftType>() ||
shape.getType().isa<fir::ShapeType>()) &&
"shape must contain extents");
auto extents = hlfir::getExplicitExtentsFromShape(shape, builder);
auto lowers = getExplicitLboundsFromShape(shape);
Expand Down Expand Up @@ -474,7 +474,7 @@ static mlir::Value computeVariableExtent(mlir::Location loc,
if (typeExtent != fir::SequenceType::getUnknownExtent())
return builder.createIntegerConstant(loc, idxTy, typeExtent);
}
assert(mlir::isa<fir::BaseBoxType>(variable.getType()) &&
assert(variable.getType().isa<fir::BaseBoxType>() &&
"array variable with dynamic extent must be boxed");
mlir::Value dimVal = builder.createIntegerConstant(loc, idxTy, dim);
auto dimInfo = builder.create<fir::BoxDimsOp>(loc, idxTy, idxTy, idxTy,
Expand All @@ -496,8 +496,9 @@ llvm::SmallVector<mlir::Value> getVariableExtents(mlir::Location loc,
variable = hlfir::derefPointersAndAllocatables(loc, builder, variable);
// Use the type shape information, and/or the fir.box/fir.class shape
// information if any extents are not static.
fir::SequenceType seqTy = mlir::cast<fir::SequenceType>(
hlfir::getFortranElementOrSequenceType(variable.getType()));
fir::SequenceType seqTy =
hlfir::getFortranElementOrSequenceType(variable.getType())
.cast<fir::SequenceType>();
unsigned rank = seqTy.getShape().size();
for (unsigned dim = 0; dim < rank; ++dim)
extents.push_back(
Expand All @@ -506,7 +507,7 @@ llvm::SmallVector<mlir::Value> getVariableExtents(mlir::Location loc,
}

static mlir::Value tryRetrievingShapeOrShift(hlfir::Entity entity) {
if (mlir::isa<hlfir::ExprType>(entity.getType())) {
if (entity.getType().isa<hlfir::ExprType>()) {
if (auto elemental = entity.getDefiningOp<hlfir::ElementalOp>())
return elemental.getShape();
return mlir::Value{};
Expand All @@ -522,13 +523,13 @@ mlir::Value hlfir::genShape(mlir::Location loc, fir::FirOpBuilder &builder,
entity = followShapeInducingSource(entity);
assert(entity && "what?");
if (auto shape = tryRetrievingShapeOrShift(entity)) {
if (mlir::isa<fir::ShapeType>(shape.getType()))
if (shape.getType().isa<fir::ShapeType>())
return shape;
if (mlir::isa<fir::ShapeShiftType>(shape.getType()))
if (shape.getType().isa<fir::ShapeShiftType>())
if (auto s = shape.getDefiningOp<fir::ShapeShiftOp>())
return builder.create<fir::ShapeOp>(loc, s.getExtents());
}
if (mlir::isa<hlfir::ExprType>(entity.getType()))
if (entity.getType().isa<hlfir::ExprType>())
return builder.create<hlfir::ShapeOfOp>(loc, entity.getBase());
// There is no shape lying around for this entity. Retrieve the extents and
// build a new fir.shape.
Expand Down Expand Up @@ -562,8 +563,9 @@ mlir::Value hlfir::genExtent(mlir::Location loc, fir::FirOpBuilder &builder,
entity = hlfir::derefPointersAndAllocatables(loc, builder, entity);
// Use the type shape information, and/or the fir.box/fir.class shape
// information if any extents are not static.
fir::SequenceType seqTy = mlir::cast<fir::SequenceType>(
hlfir::getFortranElementOrSequenceType(entity.getType()));
fir::SequenceType seqTy =
hlfir::getFortranElementOrSequenceType(entity.getType())
.cast<fir::SequenceType>();
return computeVariableExtent(loc, builder, entity, seqTy, dim);
}
TODO(loc, "get extent from HLFIR expr without producer holding the shape");
Expand All @@ -582,7 +584,7 @@ mlir::Value hlfir::genLBound(mlir::Location loc, fir::FirOpBuilder &builder,
}
if (entity.isMutableBox())
entity = hlfir::derefPointersAndAllocatables(loc, builder, entity);
assert(mlir::isa<fir::BaseBoxType>(entity.getType()) && "must be a box");
assert(entity.getType().isa<fir::BaseBoxType>() && "must be a box");
mlir::Type idxTy = builder.getIndexType();
mlir::Value dimVal = builder.createIntegerConstant(loc, idxTy, dim);
auto dimInfo =
Expand All @@ -595,7 +597,7 @@ void hlfir::genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
llvm::SmallVectorImpl<mlir::Value> &result) {
if (!entity.hasLengthParameters())
return;
if (mlir::isa<hlfir::ExprType>(entity.getType())) {
if (entity.getType().isa<hlfir::ExprType>()) {
mlir::Value expr = entity;
if (auto reassoc = expr.getDefiningOp<hlfir::NoReassocOp>())
expr = reassoc.getVal();
Expand Down Expand Up @@ -652,8 +654,8 @@ static mlir::Value asEmboxShape(mlir::Location loc, fir::FirOpBuilder &builder,
// fir.shape_shift) since this information is already in the input fir.box,
// it only accepts fir.shift because local lower bounds may not be reflected
// in the fir.box.
if (mlir::isa<fir::BaseBoxType>(fir::getBase(exv).getType()) &&
!mlir::isa<fir::ShiftType>(shape.getType()))
if (fir::getBase(exv).getType().isa<fir::BaseBoxType>() &&
!shape.getType().isa<fir::ShiftType>())
return builder.createShape(loc, exv);
return shape;
}
Expand Down Expand Up @@ -684,7 +686,7 @@ hlfir::Entity hlfir::derefPointersAndAllocatables(mlir::Location loc,
if (!entity.isPolymorphic() && !entity.hasLengthParameters())
return hlfir::Entity{builder.create<fir::BoxAddrOp>(loc, boxLoad)};
mlir::Type elementType = boxLoad.getFortranElementType();
if (auto charType = mlir::dyn_cast<fir::CharacterType>(elementType)) {
if (auto charType = elementType.dyn_cast<fir::CharacterType>()) {
mlir::Value base = builder.create<fir::BoxAddrOp>(loc, boxLoad);
if (charType.hasConstantLen())
return hlfir::Entity{base};
Expand Down Expand Up @@ -714,7 +716,7 @@ mlir::Type hlfir::getVariableElementType(hlfir::Entity variable) {
mlir::Type eleTy = variable.getFortranElementType();
if (variable.isPolymorphic())
return fir::ClassType::get(eleTy);
if (auto charType = mlir::dyn_cast<fir::CharacterType>(eleTy)) {
if (auto charType = eleTy.dyn_cast<fir::CharacterType>()) {
if (charType.hasDynamicLen())
return fir::BoxCharType::get(charType.getContext(), charType.getFKind());
} else if (fir::isRecordWithTypeParameters(eleTy)) {
Expand All @@ -735,7 +737,7 @@ mlir::Type hlfir::getEntityElementType(hlfir::Entity entity) {

static hlfir::ExprType getArrayExprType(mlir::Type elementType,
mlir::Value shape, bool isPolymorphic) {
unsigned rank = mlir::cast<fir::ShapeType>(shape.getType()).getRank();
unsigned rank = shape.getType().cast<fir::ShapeType>().getRank();
hlfir::ExprType::Shape typeShape(rank, hlfir::ExprType::getUnknownExtent());
if (auto shapeOp = shape.getDefiningOp<fir::ShapeOp>())
for (auto extent : llvm::enumerate(shapeOp.getExtents()))
Expand Down Expand Up @@ -857,7 +859,7 @@ translateVariableToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder,
return fir::MutableBoxValue(base, getExplicitTypeParams(variable),
fir::MutableProperties{});

if (mlir::isa<fir::BaseBoxType>(base.getType())) {
if (base.getType().isa<fir::BaseBoxType>()) {
if (!variable.isSimplyContiguous() || variable.isPolymorphic() ||
variable.isDerivedWithLengthParameters() || variable.isOptional()) {
llvm::SmallVector<mlir::Value> nonDefaultLbounds =
Expand All @@ -872,7 +874,7 @@ translateVariableToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder,

if (variable.isScalar()) {
if (variable.isCharacter()) {
if (mlir::isa<fir::BoxCharType>(base.getType()))
if (base.getType().isa<fir::BoxCharType>())
return genUnboxChar(loc, builder, base);
mlir::Value len = genCharacterVariableLength(loc, builder, variable);
return fir::CharBoxValue{base, len};
Expand All @@ -881,7 +883,7 @@ translateVariableToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder,
}
llvm::SmallVector<mlir::Value> extents;
llvm::SmallVector<mlir::Value> nonDefaultLbounds;
if (mlir::isa<fir::BaseBoxType>(variable.getType()) &&
if (variable.getType().isa<fir::BaseBoxType>() &&
!variable.getIfVariableInterface()) {
// This special case avoids generating two sets of identical
// fir.box_dim to get both the lower bounds and extents.
Expand Down Expand Up @@ -921,7 +923,7 @@ hlfir::translateToExtendedValue(mlir::Location loc, fir::FirOpBuilder &builder,
return {static_cast<mlir::Value>(entity), std::nullopt};
}

if (mlir::isa<hlfir::ExprType>(entity.getType())) {
if (entity.getType().isa<hlfir::ExprType>()) {
mlir::NamedAttribute byRefAttr = fir::getAdaptToByRefAttr(builder);
hlfir::AssociateOp associate = hlfir::genAssociateExpr(
loc, builder, entity, entity.getType(), "", byRefAttr);
Expand Down
107 changes: 52 additions & 55 deletions flang/lib/Optimizer/Builder/IntrinsicCall.cpp

Large diffs are not rendered by default.

35 changes: 17 additions & 18 deletions flang/lib/Optimizer/Builder/MutableBox.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ createNewFirBox(fir::FirOpBuilder &builder, mlir::Location loc,
const fir::MutableBoxValue &box, mlir::Value addr,
mlir::ValueRange lbounds, mlir::ValueRange extents,
mlir::ValueRange lengths, mlir::Value tdesc = {}) {
if (mlir::isa<fir::BaseBoxType>(addr.getType()))
if (addr.getType().isa<fir::BaseBoxType>())
// The entity is already boxed.
return builder.createConvert(loc, box.getBoxTy(), addr);

Expand All @@ -53,21 +53,20 @@ createNewFirBox(fir::FirOpBuilder &builder, mlir::Location loc,
// error in the embox).
llvm::SmallVector<mlir::Value> cleanedLengths;
auto cleanedAddr = addr;
if (auto charTy = mlir::dyn_cast<fir::CharacterType>(box.getEleTy())) {
if (auto charTy = box.getEleTy().dyn_cast<fir::CharacterType>()) {
// Cast address to box type so that both input and output type have
// unknown or constant lengths.
auto bt = box.getBaseTy();
auto addrTy = addr.getType();
auto type = mlir::isa<fir::HeapType>(addrTy) ? fir::HeapType::get(bt)
: mlir::isa<fir::PointerType>(addrTy)
? fir::PointerType::get(bt)
: builder.getRefType(bt);
auto type = addrTy.isa<fir::HeapType>() ? fir::HeapType::get(bt)
: addrTy.isa<fir::PointerType>() ? fir::PointerType::get(bt)
: builder.getRefType(bt);
cleanedAddr = builder.createConvert(loc, type, addr);
if (charTy.getLen() == fir::CharacterType::unknownLen())
cleanedLengths.append(lengths.begin(), lengths.end());
} else if (fir::isUnlimitedPolymorphicType(box.getBoxTy())) {
if (auto charTy = mlir::dyn_cast<fir::CharacterType>(
fir::dyn_cast_ptrEleTy(addr.getType()))) {
if (auto charTy = fir::dyn_cast_ptrEleTy(addr.getType())
.dyn_cast<fir::CharacterType>()) {
if (charTy.getLen() == fir::CharacterType::unknownLen())
cleanedLengths.append(lengths.begin(), lengths.end());
}
Expand Down Expand Up @@ -329,18 +328,18 @@ class MutablePropertyWriter {
mlir::Value fir::factory::createUnallocatedBox(
fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type boxType,
mlir::ValueRange nonDeferredParams, mlir::Value typeSourceBox) {
auto baseAddrType = mlir::dyn_cast<fir::BaseBoxType>(boxType).getEleTy();
auto baseAddrType = boxType.dyn_cast<fir::BaseBoxType>().getEleTy();
if (!fir::isa_ref_type(baseAddrType))
baseAddrType = builder.getRefType(baseAddrType);
auto type = fir::unwrapRefType(baseAddrType);
auto eleTy = fir::unwrapSequenceType(type);
if (auto recTy = mlir::dyn_cast<fir::RecordType>(eleTy))
if (auto recTy = eleTy.dyn_cast<fir::RecordType>())
if (recTy.getNumLenParams() > 0)
TODO(loc, "creating unallocated fir.box of derived type with length "
"parameters");
auto nullAddr = builder.createNullConstant(loc, baseAddrType);
mlir::Value shape;
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(type)) {
if (auto seqTy = type.dyn_cast<fir::SequenceType>()) {
auto zero = builder.createIntegerConstant(loc, builder.getIndexType(), 0);
llvm::SmallVector<mlir::Value> extents(seqTy.getDimension(), zero);
shape = builder.createShape(
Expand All @@ -349,7 +348,7 @@ mlir::Value fir::factory::createUnallocatedBox(
// Provide dummy length parameters if they are dynamic. If a length parameter
// is deferred. It is set to zero here and will be set on allocation.
llvm::SmallVector<mlir::Value> lenParams;
if (auto charTy = mlir::dyn_cast<fir::CharacterType>(eleTy)) {
if (auto charTy = eleTy.dyn_cast<fir::CharacterType>()) {
if (charTy.getLen() == fir::CharacterType::unknownLen()) {
if (!nonDeferredParams.empty()) {
lenParams.push_back(nonDeferredParams[0]);
Expand Down Expand Up @@ -593,7 +592,7 @@ void fir::factory::associateMutableBoxWithRemap(
auto cast = [&](mlir::Value addr) -> mlir::Value {
// Cast base addr to new sequence type.
auto ty = fir::dyn_cast_ptrEleTy(addr.getType());
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(ty)) {
if (auto seqTy = ty.dyn_cast<fir::SequenceType>()) {
fir::SequenceType::Shape shape(newRank,
fir::SequenceType::getUnknownExtent());
ty = fir::SequenceType::get(shape, seqTy.getEleTy());
Expand Down Expand Up @@ -674,10 +673,10 @@ void fir::factory::disassociateMutableBox(fir::FirOpBuilder &builder,
if (box.isPolymorphic() && polymorphicSetType) {
// 7.3.2.3 point 7. The dynamic type of a disassociated pointer is the
// same as its declared type.
auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(box.getBoxTy());
auto boxTy = box.getBoxTy().dyn_cast<fir::BaseBoxType>();
auto eleTy = fir::unwrapPassByRefType(boxTy.getEleTy());
mlir::Type derivedType = fir::getDerivedType(eleTy);
if (auto recTy = mlir::dyn_cast<fir::RecordType>(derivedType)) {
if (auto recTy = derivedType.dyn_cast<fir::RecordType>()) {
fir::runtime::genNullifyDerivedType(builder, loc, box.getAddr(), recTy,
box.rank());
return;
Expand All @@ -691,7 +690,7 @@ getNewLengths(fir::FirOpBuilder &builder, mlir::Location loc,
const fir::MutableBoxValue &box, mlir::ValueRange lenParams) {
llvm::SmallVector<mlir::Value> lengths;
auto idxTy = builder.getIndexType();
if (auto charTy = mlir::dyn_cast<fir::CharacterType>(box.getEleTy())) {
if (auto charTy = box.getEleTy().dyn_cast<fir::CharacterType>()) {
if (charTy.getLen() == fir::CharacterType::unknownLen()) {
if (box.hasNonDeferredLenParams()) {
lengths.emplace_back(
Expand All @@ -718,7 +717,7 @@ static mlir::Value allocateAndInitNewStorage(fir::FirOpBuilder &builder,
auto lengths = getNewLengths(builder, loc, box, lenParams);
auto newStorage = builder.create<fir::AllocMemOp>(
loc, box.getBaseTy(), allocName, lengths, extents);
if (mlir::isa<fir::RecordType>(box.getEleTy())) {
if (box.getEleTy().isa<fir::RecordType>()) {
// TODO: skip runtime initialization if this is not required. Currently,
// there is no way to know here if a derived type needs it or not. But the
// information is available at compile time and could be reflected here
Expand All @@ -743,7 +742,7 @@ void fir::factory::genInlinedAllocation(
lengths, safeExtents);
MutablePropertyWriter{builder, loc, box}.updateMutableBox(
heap, lbounds, safeExtents, lengths);
if (mlir::isa<fir::RecordType>(box.getEleTy())) {
if (box.getEleTy().isa<fir::RecordType>()) {
// TODO: skip runtime initialization if this is not required. Currently,
// there is no way to know here if a derived type needs it or not. But the
// information is available at compile time and could be reflected here
Expand Down
44 changes: 23 additions & 21 deletions flang/lib/Optimizer/Builder/PPCIntrinsicCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1119,7 +1119,7 @@ PPCIntrinsicLibrary::genVecAbs(mlir::Type resultType,
funcOp = builder.createFunction(loc, fname, ftype);
auto callOp{builder.create<fir::CallOp>(loc, funcOp, argBases[0])};
return callOp.getResult(0);
} else if (auto eleTy = mlir::dyn_cast<mlir::IntegerType>(vTypeInfo.eleTy)) {
} else if (auto eleTy = vTypeInfo.eleTy.dyn_cast<mlir::IntegerType>()) {
// vec_abs(arg1) = max(0 - arg1, arg1)

auto newVecTy{mlir::VectorType::get(vTypeInfo.len, eleTy)};
Expand Down Expand Up @@ -1173,13 +1173,12 @@ fir::ExtendedValue PPCIntrinsicLibrary::genVecAddAndMulSubXor(
assert(args.size() == 2);
auto argBases{getBasesForArgs(args)};
auto argsTy{getTypesForArgs(argBases)};
assert(mlir::isa<fir::VectorType>(argsTy[0]) &&
mlir::isa<fir::VectorType>(argsTy[1]));
assert(argsTy[0].isa<fir::VectorType>() && argsTy[1].isa<fir::VectorType>());

auto vecTyInfo{getVecTypeFromFir(argBases[0])};

const auto isInteger{mlir::isa<mlir::IntegerType>(vecTyInfo.eleTy)};
const auto isFloat{mlir::isa<mlir::FloatType>(vecTyInfo.eleTy)};
const auto isInteger{vecTyInfo.eleTy.isa<mlir::IntegerType>()};
const auto isFloat{vecTyInfo.eleTy.isa<mlir::FloatType>()};
assert((isInteger || isFloat) && "unknown vector type");

auto vargs{convertVecArgs(builder, loc, vecTyInfo, argBases)};
Expand Down Expand Up @@ -1213,7 +1212,7 @@ fir::ExtendedValue PPCIntrinsicLibrary::genVecAddAndMulSubXor(
arg2 = vargs[1];
} else if (isFloat) {
// bitcast the arguments to integer
auto wd{mlir::dyn_cast<mlir::FloatType>(vecTyInfo.eleTy).getWidth()};
auto wd{vecTyInfo.eleTy.dyn_cast<mlir::FloatType>().getWidth()};
auto ftype{builder.getIntegerType(wd)};
auto bcVecTy{mlir::VectorType::get(vecTyInfo.len, ftype)};
arg1 = builder.create<mlir::vector::BitCastOp>(loc, bcVecTy, vargs[0]);
Expand Down Expand Up @@ -1451,7 +1450,7 @@ PPCIntrinsicLibrary::genVecCmp(mlir::Type resultType,

mlir::Value res{nullptr};

if (auto eTy = mlir::dyn_cast<mlir::IntegerType>(vecTyInfo.eleTy)) {
if (auto eTy = vecTyInfo.eleTy.dyn_cast<mlir::IntegerType>()) {
constexpr int firstArg{0};
constexpr int secondArg{1};
std::map<VecOp, std::array<int, 2>> argOrder{
Expand Down Expand Up @@ -1560,7 +1559,7 @@ PPCIntrinsicLibrary::genVecConvert(mlir::Type resultType,
case VecOp::Ctf: {
assert(args.size() == 2);
auto convArg{builder.createConvert(loc, i32Ty, argBases[1])};
auto eTy{mlir::dyn_cast<mlir::IntegerType>(vecTyInfo.eleTy)};
auto eTy{vecTyInfo.eleTy.dyn_cast<mlir::IntegerType>()};
assert(eTy && "Unsupported vector type");
const auto isUnsigned{eTy.isUnsignedInteger()};
const auto width{eTy.getWidth()};
Expand Down Expand Up @@ -1588,9 +1587,10 @@ PPCIntrinsicLibrary::genVecConvert(mlir::Type resultType,
: builder.create<mlir::LLVM::SIToFPOp>(loc, ty, vArg1)};

// construct vector<1./(1<<arg1), 1.0/(1<<arg1)>
auto constInt{mlir::dyn_cast_or_null<mlir::IntegerAttr>(
auto constInt{
mlir::dyn_cast<mlir::arith::ConstantOp>(argBases[1].getDefiningOp())
.getValue())};
.getValue()
.dyn_cast_or_null<mlir::IntegerAttr>()};
assert(constInt && "expected integer constant argument");
double f{1.0 / (1 << constInt.getInt())};
llvm::SmallVector<double> vals{f, f};
Expand Down Expand Up @@ -1815,7 +1815,7 @@ static mlir::Value addOffsetToAddress(fir::FirOpBuilder &builder,
static mlir::Value reverseVectorElements(fir::FirOpBuilder &builder,
mlir::Location loc, mlir::Value v,
int64_t len) {
assert(mlir::isa<mlir::VectorType>(v.getType()));
assert(v.getType().isa<mlir::VectorType>());
assert(len > 0);
llvm::SmallVector<int64_t, 16> mask;
for (int64_t i = 0; i < len; ++i) {
Expand Down Expand Up @@ -2144,9 +2144,10 @@ PPCIntrinsicLibrary::genVecPerm(mlir::Type resultType,
}
case VecOp::Permi: {
// arg3 is a constant
auto constIntOp{mlir::dyn_cast_or_null<mlir::IntegerAttr>(
auto constIntOp{
mlir::dyn_cast<mlir::arith::ConstantOp>(argBases[2].getDefiningOp())
.getValue())};
.getValue()
.dyn_cast_or_null<mlir::IntegerAttr>()};
assert(constIntOp && "expected integer constant argument");
auto constInt{constIntOp.getInt()};
// arg1, arg2, and result type share same VecTypeInfo
Expand Down Expand Up @@ -2320,9 +2321,10 @@ PPCIntrinsicLibrary::genVecShift(mlir::Type resultType,
}
} else if (vop == VecOp::Sld || vop == VecOp::Sldw) {
assert(args.size() == 3);
auto constIntOp = mlir::dyn_cast_or_null<mlir::IntegerAttr>(
auto constIntOp =
mlir::dyn_cast<mlir::arith::ConstantOp>(argBases[2].getDefiningOp())
.getValue());
.getValue()
.dyn_cast_or_null<mlir::IntegerAttr>();
assert(constIntOp && "expected integer constant argument");

// Bitcast to vector<16xi8>
Expand Down Expand Up @@ -2795,16 +2797,16 @@ void PPCIntrinsicLibrary::genMmaIntr(llvm::ArrayRef<fir::ExtendedValue> args) {
auto vType{v.getType()};
mlir::Type targetType{intrFuncType.getInput(j)};
if (vType != targetType) {
if (mlir::isa<mlir::VectorType>(targetType)) {
if (targetType.isa<mlir::VectorType>()) {
// Perform vector type conversion for arguments passed by value.
auto eleTy{mlir::dyn_cast<fir::VectorType>(vType).getEleTy()};
auto len{mlir::dyn_cast<fir::VectorType>(vType).getLen()};
auto eleTy{vType.dyn_cast<fir::VectorType>().getEleTy()};
auto len{vType.dyn_cast<fir::VectorType>().getLen()};
mlir::VectorType mlirType = mlir::VectorType::get(len, eleTy);
auto v0{builder.createConvert(loc, mlirType, v)};
auto v1{builder.create<mlir::vector::BitCastOp>(loc, targetType, v0)};
intrArgs.push_back(v1);
} else if (mlir::isa<mlir::IntegerType>(targetType) &&
mlir::isa<mlir::IntegerType>(vType)) {
} else if (targetType.isa<mlir::IntegerType>() &&
vType.isa<mlir::IntegerType>()) {
auto v0{builder.createConvert(loc, targetType, v)};
intrArgs.push_back(v0);
} else {
Expand Down Expand Up @@ -2859,7 +2861,7 @@ void PPCIntrinsicLibrary::genVecStore(llvm::ArrayRef<fir::ExtendedValue> args) {
if (arg1TyInfo.isFloat32()) {
stTy = mlir::VectorType::get(len, i32ty);
fname = "llvm.ppc.altivec.stvewx";
} else if (mlir::isa<mlir::IntegerType>(arg1TyInfo.eleTy)) {
} else if (arg1TyInfo.eleTy.isa<mlir::IntegerType>()) {
stTy = mlir::VectorType::get(len, mlir::IntegerType::get(context, width));

switch (width) {
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/Builder/Runtime/Allocatable.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ mlir::Value fir::runtime::genMoveAlloc(fir::FirOpBuilder &builder,
if (fir::isPolymorphicType(from.getType()) &&
!fir::isUnlimitedPolymorphicType(from.getType())) {
fir::ClassType clTy =
mlir::dyn_cast<fir::ClassType>(fir::dyn_cast_ptrEleTy(from.getType()));
fir::dyn_cast_ptrEleTy(from.getType()).dyn_cast<fir::ClassType>();
mlir::Type derivedType = fir::unwrapInnerType(clTy.getEleTy());
declaredTypeDesc =
builder.create<fir::TypeDescOp>(loc, mlir::TypeAttr::get(derivedType));
Expand Down
8 changes: 4 additions & 4 deletions flang/lib/Optimizer/Builder/Runtime/Character.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,15 +39,15 @@ static void genCharacterSearch(FN func, fir::FirOpBuilder &builder,

/// Helper function to recover the KIND from the FIR type.
static int discoverKind(mlir::Type ty) {
if (auto charTy = mlir::dyn_cast<fir::CharacterType>(ty))
if (auto charTy = ty.dyn_cast<fir::CharacterType>())
return charTy.getFKind();
if (auto eleTy = fir::dyn_cast_ptrEleTy(ty))
return discoverKind(eleTy);
if (auto arrTy = mlir::dyn_cast<fir::SequenceType>(ty))
if (auto arrTy = ty.dyn_cast<fir::SequenceType>())
return discoverKind(arrTy.getEleTy());
if (auto boxTy = mlir::dyn_cast<fir::BoxCharType>(ty))
if (auto boxTy = ty.dyn_cast<fir::BoxCharType>())
return discoverKind(boxTy.getEleTy());
if (auto boxTy = mlir::dyn_cast<fir::BoxType>(ty))
if (auto boxTy = ty.dyn_cast<fir::BoxType>())
return discoverKind(boxTy.getEleTy());
llvm_unreachable("unexpected character type");
}
Expand Down
6 changes: 2 additions & 4 deletions flang/lib/Optimizer/Builder/Runtime/Intrinsics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -228,8 +228,7 @@ void fir::runtime::genSystemClock(fir::FirOpBuilder &builder,
fir::IfOp ifOp{};
const bool isOptionalArg =
fir::valueHasFirAttribute(arg, fir::getOptionalAttrName());
if (mlir::dyn_cast<fir::PointerType>(type) ||
mlir::dyn_cast<fir::HeapType>(type)) {
if (type.dyn_cast<fir::PointerType>() || type.dyn_cast<fir::HeapType>()) {
// Check for a disassociated pointer or an unallocated allocatable.
assert(!isOptionalArg && "invalid optional argument");
ifOp = builder.create<fir::IfOp>(loc, builder.genIsNotNullAddr(loc, arg),
Expand All @@ -243,8 +242,7 @@ void fir::runtime::genSystemClock(fir::FirOpBuilder &builder,
builder.setInsertionPointToStart(&ifOp.getThenRegion().front());
mlir::Type kindTy = func.getFunctionType().getInput(0);
int integerKind = 8;
if (auto intType =
mlir::dyn_cast<mlir::IntegerType>(fir::unwrapRefType(type)))
if (auto intType = fir::unwrapRefType(type).dyn_cast<mlir::IntegerType>())
integerKind = intType.getWidth() / 8;
mlir::Value kind = builder.createIntegerConstant(loc, kindTy, integerKind);
mlir::Value res =
Expand Down
3 changes: 1 addition & 2 deletions flang/lib/Optimizer/Builder/Runtime/Ragged.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,7 @@ void fir::runtime::genRaggedArrayAllocate(mlir::Location loc,
// Position of the bufferPointer in the header struct.
auto one = builder.createIntegerConstant(loc, i32Ty, 1);
auto eleTy = fir::unwrapSequenceType(fir::unwrapRefType(header.getType()));
auto ptrTy =
builder.getRefType(mlir::cast<mlir::TupleType>(eleTy).getType(1));
auto ptrTy = builder.getRefType(eleTy.cast<mlir::TupleType>().getType(1));
auto ptr = builder.create<fir::CoordinateOp>(loc, ptrTy, header, one);
auto heap = builder.create<fir::LoadOp>(loc, ptr);
auto cmp = builder.genIsNullAddr(loc, heap);
Expand Down
20 changes: 10 additions & 10 deletions flang/lib/Optimizer/Builder/Runtime/Reduction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -666,7 +666,7 @@ void fir::runtime::genMaxloc(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::func::FuncOp func;
auto ty = arrayBox.getType();
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
fir::factory::CharacterExprHelper charHelper{builder, loc};
if (eleTy.isF32())
func = fir::runtime::getRuntimeFunc<mkRTKey(MaxlocReal4)>(loc, builder);
Expand Down Expand Up @@ -713,7 +713,7 @@ mlir::Value fir::runtime::genMaxval(fir::FirOpBuilder &builder,
mlir::func::FuncOp func;
auto ty = arrayBox.getType();
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);

if (eleTy.isF32())
Expand Down Expand Up @@ -781,7 +781,7 @@ void fir::runtime::genMinloc(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::func::FuncOp func;
auto ty = arrayBox.getType();
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
fir::factory::CharacterExprHelper charHelper{builder, loc};
if (eleTy.isF32())
func = fir::runtime::getRuntimeFunc<mkRTKey(MinlocReal4)>(loc, builder);
Expand Down Expand Up @@ -853,7 +853,7 @@ mlir::Value fir::runtime::genMinval(fir::FirOpBuilder &builder,
mlir::func::FuncOp func;
auto ty = arrayBox.getType();
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);

if (eleTy.isF32())
Expand Down Expand Up @@ -895,7 +895,7 @@ void fir::runtime::genNorm2Dim(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::func::FuncOp func;
auto ty = arrayBox.getType();
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
if (eleTy.isF128())
func = fir::runtime::getRuntimeFunc<ForcedNorm2DimReal16>(loc, builder);
else
Expand All @@ -917,7 +917,7 @@ mlir::Value fir::runtime::genNorm2(fir::FirOpBuilder &builder,
mlir::func::FuncOp func;
auto ty = arrayBox.getType();
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);

if (eleTy.isF32())
Expand Down Expand Up @@ -968,7 +968,7 @@ mlir::Value fir::runtime::genProduct(fir::FirOpBuilder &builder,
mlir::func::FuncOp func;
auto ty = arrayBox.getType();
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);

if (eleTy.isF32())
Expand Down Expand Up @@ -1069,7 +1069,7 @@ mlir::Value fir::runtime::genDotProduct(fir::FirOpBuilder &builder,
else if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(16)))
func =
fir::runtime::getRuntimeFunc<ForcedDotProductInteger16>(loc, builder);
else if (mlir::isa<fir::LogicalType>(eleTy))
else if (eleTy.isa<fir::LogicalType>())
func =
fir::runtime::getRuntimeFunc<mkRTKey(DotProductLogical)>(loc, builder);
else
Expand Down Expand Up @@ -1111,7 +1111,7 @@ mlir::Value fir::runtime::genSum(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::func::FuncOp func;
auto ty = arrayBox.getType();
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty);
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy();
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy();
auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0);

if (eleTy.isF32())
Expand Down Expand Up @@ -1173,7 +1173,7 @@ mlir::Value fir::runtime::genSum(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::func::FuncOp func; \
auto ty = arrayBox.getType(); \
auto arrTy = fir::dyn_cast_ptrOrBoxEleTy(ty); \
auto eleTy = mlir::cast<fir::SequenceType>(arrTy).getEleTy(); \
auto eleTy = arrTy.cast<fir::SequenceType>().getEleTy(); \
auto dim = builder.createIntegerConstant(loc, builder.getIndexType(), 0); \
\
if (eleTy.isInteger(builder.getKindMap().getIntegerBitsize(1))) \
Expand Down
16 changes: 8 additions & 8 deletions flang/lib/Optimizer/CodeGen/BoxedProcedure.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ class BoxprocTypeRewriter : public mlir::TypeConverter {
/// not at all depending on the implementation target's characteristics and
/// preference.
bool needsConversion(mlir::Type ty) {
if (mlir::isa<BoxProcType>(ty))
if (ty.isa<BoxProcType>())
return true;
if (auto funcTy = mlir::dyn_cast<mlir::FunctionType>(ty)) {
if (auto funcTy = ty.dyn_cast<mlir::FunctionType>()) {
for (auto t : funcTy.getInputs())
if (needsConversion(t))
return true;
Expand All @@ -62,13 +62,13 @@ class BoxprocTypeRewriter : public mlir::TypeConverter {
return true;
return false;
}
if (auto tupleTy = mlir::dyn_cast<mlir::TupleType>(ty)) {
if (auto tupleTy = ty.dyn_cast<mlir::TupleType>()) {
for (auto t : tupleTy.getTypes())
if (needsConversion(t))
return true;
return false;
}
if (auto recTy = mlir::dyn_cast<RecordType>(ty)) {
if (auto recTy = ty.dyn_cast<RecordType>()) {
auto visited = visitedTypes.find(ty);
if (visited != visitedTypes.end())
return visited->second;
Expand Down Expand Up @@ -97,11 +97,11 @@ class BoxprocTypeRewriter : public mlir::TypeConverter {
visitedTypes.find(ty)->second = result;
return result;
}
if (auto boxTy = mlir::dyn_cast<BaseBoxType>(ty))
if (auto boxTy = ty.dyn_cast<BaseBoxType>())
return needsConversion(boxTy.getEleTy());
if (isa_ref_type(ty))
return needsConversion(unwrapRefType(ty));
if (auto t = mlir::dyn_cast<SequenceType>(ty))
if (auto t = ty.dyn_cast<SequenceType>())
return needsConversion(unwrapSequenceType(ty));
return false;
}
Expand Down Expand Up @@ -246,7 +246,7 @@ class BoxedProcedurePass
if (typeConverter.needsConversion(ty)) {
rewriter.startOpModification(func);
auto toTy =
mlir::cast<mlir::FunctionType>(typeConverter.convertType(ty));
typeConverter.convertType(ty).cast<mlir::FunctionType>();
if (!func.empty())
for (auto e : llvm::enumerate(toTy.getInputs())) {
unsigned i = e.index();
Expand All @@ -263,7 +263,7 @@ class BoxedProcedurePass
// Rewrite all `fir.emboxproc` ops to either `fir.convert` or a thunk
// as required.
mlir::Type toTy = typeConverter.convertType(
mlir::cast<BoxProcType>(embox.getType()).getEleTy());
embox.getType().cast<BoxProcType>().getEleTy());
rewriter.setInsertionPoint(embox);
if (embox.getHost()) {
// Create the thunk.
Expand Down
14 changes: 7 additions & 7 deletions flang/lib/Optimizer/CodeGen/CGOps.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,24 +41,24 @@ unsigned fir::cg::XEmboxOp::getOutRank() {
}

unsigned fir::cg::XReboxOp::getOutRank() {
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(
fir::dyn_cast_ptrOrBoxEleTy(getType())))
if (auto seqTy =
fir::dyn_cast_ptrOrBoxEleTy(getType()).dyn_cast<fir::SequenceType>())
return seqTy.getDimension();
return 0;
}

unsigned fir::cg::XReboxOp::getRank() {
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(
fir::dyn_cast_ptrOrBoxEleTy(getBox().getType())))
if (auto seqTy = fir::dyn_cast_ptrOrBoxEleTy(getBox().getType())
.dyn_cast<fir::SequenceType>())
return seqTy.getDimension();
return 0;
}

unsigned fir::cg::XArrayCoorOp::getRank() {
auto memrefTy = getMemref().getType();
if (mlir::isa<fir::BaseBoxType>(memrefTy))
if (auto seqty = mlir::dyn_cast<fir::SequenceType>(
fir::dyn_cast_ptrOrBoxEleTy(memrefTy)))
if (memrefTy.isa<fir::BaseBoxType>())
if (auto seqty =
fir::dyn_cast_ptrOrBoxEleTy(memrefTy).dyn_cast<fir::SequenceType>())
return seqty.getDimension();
return getShape().size();
}
203 changes: 96 additions & 107 deletions flang/lib/Optimizer/CodeGen/CodeGen.cpp

Large diffs are not rendered by default.

18 changes: 9 additions & 9 deletions flang/lib/Optimizer/CodeGen/FIROpPatterns.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ static inline mlir::Type getLlvmPtrType(mlir::MLIRContext *context,
}

static unsigned getTypeDescFieldId(mlir::Type ty) {
auto isArray = mlir::isa<fir::SequenceType>(fir::dyn_cast_ptrOrBoxEleTy(ty));
auto isArray = fir::dyn_cast_ptrOrBoxEleTy(ty).isa<fir::SequenceType>();
return isArray ? kOptTypePtrPosInBox : kDimsPosInBox;
}

Expand All @@ -37,7 +37,7 @@ ConvertFIRToLLVMPattern::ConvertFIRToLLVMPattern(
// reference.
mlir::Type
ConvertFIRToLLVMPattern::convertObjectType(mlir::Type firType) const {
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(firType))
if (auto boxTy = firType.dyn_cast<fir::BaseBoxType>())
return lowerTy().convertBoxTypeAsStruct(boxTy);
return lowerTy().convertType(firType);
}
Expand Down Expand Up @@ -69,7 +69,7 @@ ConvertFIRToLLVMPattern::integerCast(mlir::Location loc,
auto valTy = val.getType();
// If the value was not yet lowered, lower its type so that it can
// be used in getPrimitiveTypeSizeInBits.
if (!mlir::isa<mlir::IntegerType>(valTy))
if (!valTy.isa<mlir::IntegerType>())
valTy = convertType(valTy);
auto toSize = mlir::LLVM::getPrimitiveTypeSizeInBits(ty);
auto fromSize = mlir::LLVM::getPrimitiveTypeSizeInBits(valTy);
Expand All @@ -91,7 +91,7 @@ ConvertFIRToLLVMPattern::getBoxTypePair(mlir::Type firBoxTy) const {
mlir::Value ConvertFIRToLLVMPattern::getValueFromBox(
mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::Type resultTy,
mlir::ConversionPatternRewriter &rewriter, int boxValue) const {
if (mlir::isa<mlir::LLVM::LLVMPointerType>(box.getType())) {
if (box.getType().isa<mlir::LLVM::LLVMPointerType>()) {
auto pty = getLlvmPtrType(resultTy.getContext());
auto p = rewriter.create<mlir::LLVM::GEPOp>(
loc, pty, boxTy.llvm, box,
Expand Down Expand Up @@ -133,7 +133,7 @@ llvm::SmallVector<mlir::Value, 3> ConvertFIRToLLVMPattern::getDimsFromBox(
mlir::Value ConvertFIRToLLVMPattern::loadDimFieldFromBox(
mlir::Location loc, TypePair boxTy, mlir::Value box, mlir::Value dim,
int off, mlir::Type ty, mlir::ConversionPatternRewriter &rewriter) const {
assert(mlir::isa<mlir::LLVM::LLVMPointerType>(box.getType()) &&
assert(box.getType().isa<mlir::LLVM::LLVMPointerType>() &&
"descriptor inquiry with runtime dim can only be done on descriptor "
"in memory");
mlir::LLVM::GEPOp p = genGEP(loc, boxTy.llvm, rewriter, box, 0,
Expand All @@ -146,7 +146,7 @@ mlir::Value ConvertFIRToLLVMPattern::loadDimFieldFromBox(
mlir::Value ConvertFIRToLLVMPattern::getDimFieldFromBox(
mlir::Location loc, TypePair boxTy, mlir::Value box, int dim, int off,
mlir::Type ty, mlir::ConversionPatternRewriter &rewriter) const {
if (mlir::isa<mlir::LLVM::LLVMPointerType>(box.getType())) {
if (box.getType().isa<mlir::LLVM::LLVMPointerType>()) {
mlir::LLVM::GEPOp p = genGEP(loc, boxTy.llvm, rewriter, box, 0,
static_cast<int>(kDimsPosInBox), dim, off);
auto loadOp = rewriter.create<mlir::LLVM::LoadOp>(loc, ty, p);
Expand Down Expand Up @@ -184,12 +184,12 @@ mlir::Value ConvertFIRToLLVMPattern::getElementSizeFromBox(
mlir::Type ConvertFIRToLLVMPattern::getBoxEleTy(
mlir::Type type, llvm::ArrayRef<std::int64_t> indexes) const {
for (unsigned i : indexes) {
if (auto t = mlir::dyn_cast<mlir::LLVM::LLVMStructType>(type)) {
if (auto t = type.dyn_cast<mlir::LLVM::LLVMStructType>()) {
assert(!t.isOpaque() && i < t.getBody().size());
type = t.getBody()[i];
} else if (auto t = mlir::dyn_cast<mlir::LLVM::LLVMArrayType>(type)) {
} else if (auto t = type.dyn_cast<mlir::LLVM::LLVMArrayType>()) {
type = t.getElementType();
} else if (auto t = mlir::dyn_cast<mlir::VectorType>(type)) {
} else if (auto t = type.dyn_cast<mlir::VectorType>()) {
type = t.getElementType();
} else {
fir::emitFatalError(mlir::UnknownLoc::get(type.getContext()),
Expand Down
13 changes: 7 additions & 6 deletions flang/lib/Optimizer/CodeGen/PreCGRewrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ class EmboxConversion : public mlir::OpRewritePattern<fir::EmboxOp> {
// If the embox does not include a shape, then do not convert it
if (auto shapeVal = embox.getShape())
return rewriteDynamicShape(embox, rewriter, shapeVal);
if (mlir::isa<fir::ClassType>(embox.getType()))
if (embox.getType().isa<fir::ClassType>())
TODO(embox.getLoc(), "embox conversion for fir.class type");
if (auto boxTy = mlir::dyn_cast<fir::BoxType>(embox.getType()))
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(boxTy.getEleTy()))
if (auto boxTy = embox.getType().dyn_cast<fir::BoxType>())
if (auto seqTy = boxTy.getEleTy().dyn_cast<fir::SequenceType>())
if (!seqTy.hasDynamicExtents())
return rewriteStaticShape(embox, rewriter, seqTy);
return mlir::failure();
Expand Down Expand Up @@ -294,9 +294,10 @@ class CodeGenRewrite : public fir::impl::CodeGenRewriteBase<CodeGenRewrite> {
target.addIllegalOp<fir::ReboxOp>();
target.addIllegalOp<fir::DeclareOp>();
target.addDynamicallyLegalOp<fir::EmboxOp>([](fir::EmboxOp embox) {
return !(embox.getShape() ||
mlir::isa<fir::SequenceType>(
mlir::cast<fir::BaseBoxType>(embox.getType()).getEleTy()));
return !(embox.getShape() || embox.getType()
.cast<fir::BaseBoxType>()
.getEleTy()
.isa<fir::SequenceType>());
});
mlir::RewritePatternSet patterns(&context);
fir::populatePreCGRewritePatterns(patterns);
Expand Down
2 changes: 1 addition & 1 deletion flang/lib/Optimizer/CodeGen/TBAABuilder.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ void TBAABuilder::attachTBAATag(AliasAnalysisOpInterface op, Type baseFIRType,
// with both data and descriptor accesses.
// Conservatively set any-access tag if there is any descriptor member.
tbaaTagSym = getAnyAccessTag(func);
} else if (mlir::isa<fir::BaseBoxType>(baseFIRType)) {
} else if (baseFIRType.isa<fir::BaseBoxType>()) {
tbaaTagSym = getBoxAccessTag(baseFIRType, accessFIRType, gep, func);
} else {
tbaaTagSym = getDataAccessTag(baseFIRType, accessFIRType, gep, func);
Expand Down
4 changes: 2 additions & 2 deletions flang/lib/Optimizer/CodeGen/Target.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,9 +41,9 @@ llvm::StringRef Attributes::getIntExtensionAttrName() const {
static const llvm::fltSemantics &floatToSemantics(const KindMapping &kindMap,
mlir::Type type) {
assert(isa_real(type));
if (auto ty = mlir::dyn_cast<fir::RealType>(type))
if (auto ty = type.dyn_cast<fir::RealType>())
return kindMap.getFloatSemantics(ty.getFKind());
return mlir::cast<mlir::FloatType>(type).getFloatSemantics();
return type.cast<mlir::FloatType>().getFloatSemantics();
}

static void typeTodo(const llvm::fltSemantics *sem, mlir::Location loc,
Expand Down
21 changes: 10 additions & 11 deletions flang/lib/Optimizer/CodeGen/TargetRewrite.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -137,7 +137,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
if (!hasPortableSignature(dispatch.getFunctionType(), op))
convertCallOp(dispatch);
} else if (auto addr = mlir::dyn_cast<fir::AddrOfOp>(op)) {
if (mlir::isa<mlir::FunctionType>(addr.getType()) &&
if (addr.getType().isa<mlir::FunctionType>() &&
!hasPortableSignature(addr.getType(), op))
convertAddrOp(addr);
}
Expand Down Expand Up @@ -601,7 +601,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
/// Taking the address of a function. Modify the signature as needed.
void convertAddrOp(fir::AddrOfOp addrOp) {
rewriter->setInsertionPoint(addrOp);
auto addrTy = mlir::cast<mlir::FunctionType>(addrOp.getType());
auto addrTy = addrOp.getType().cast<mlir::FunctionType>();
fir::CodeGenSpecifics::Marshalling newInTyAndAttrs;
llvm::SmallVector<mlir::Type> newResTys;
auto loc = addrOp.getLoc();
Expand Down Expand Up @@ -705,23 +705,22 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
/// return `true`. Otherwise, the signature is not portable and `false` is
/// returned.
bool hasPortableSignature(mlir::Type signature, mlir::Operation *op) {
assert(mlir::isa<mlir::FunctionType>(signature));
auto func = mlir::dyn_cast<mlir::FunctionType>(signature);
assert(signature.isa<mlir::FunctionType>());
auto func = signature.dyn_cast<mlir::FunctionType>();
bool hasCCallingConv = isFuncWithCCallingConvention(op);
for (auto ty : func.getResults())
if ((mlir::isa<fir::BoxCharType>(ty) && !noCharacterConversion) ||
if ((ty.isa<fir::BoxCharType>() && !noCharacterConversion) ||
(fir::isa_complex(ty) && !noComplexConversion) ||
(mlir::isa<mlir::IntegerType>(ty) && hasCCallingConv)) {
(ty.isa<mlir::IntegerType>() && hasCCallingConv)) {
LLVM_DEBUG(llvm::dbgs() << "rewrite " << signature << " for target\n");
return false;
}
for (auto ty : func.getInputs())
if (((mlir::isa<fir::BoxCharType>(ty) ||
fir::isCharacterProcedureTuple(ty)) &&
if (((ty.isa<fir::BoxCharType>() || fir::isCharacterProcedureTuple(ty)) &&
!noCharacterConversion) ||
(fir::isa_complex(ty) && !noComplexConversion) ||
(mlir::isa<mlir::IntegerType>(ty) && hasCCallingConv) ||
(mlir::isa<fir::RecordType>(ty) && !noStructConversion)) {
(ty.isa<mlir::IntegerType>() && hasCCallingConv) ||
(ty.isa<fir::RecordType>() && !noStructConversion)) {
LLVM_DEBUG(llvm::dbgs() << "rewrite " << signature << " for target\n");
return false;
}
Expand All @@ -741,7 +740,7 @@ class TargetRewrite : public fir::impl::TargetRewritePassBase<TargetRewrite> {
/// Rewrite the signatures and body of the `FuncOp`s in the module for
/// the immediately subsequent target code gen.
void convertSignature(mlir::func::FuncOp func) {
auto funcTy = mlir::cast<mlir::FunctionType>(func.getFunctionType());
auto funcTy = func.getFunctionType().cast<mlir::FunctionType>();
if (hasPortableSignature(funcTy, func) && !hasHostAssociations(func))
return;
llvm::SmallVector<mlir::Type> newResTys;
Expand Down
18 changes: 8 additions & 10 deletions flang/lib/Optimizer/CodeGen/TypeConverter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,10 +103,10 @@ LLVMTypeConverter::LLVMTypeConverter(mlir::ModuleOp module, bool applyTBAA,
for (auto mem : tuple.getTypes()) {
// Prevent fir.box from degenerating to a pointer to a descriptor in the
// context of a tuple type.
if (auto box = mlir::dyn_cast<fir::BaseBoxType>(mem))
if (auto box = mem.dyn_cast<fir::BaseBoxType>())
members.push_back(convertBoxTypeAsStruct(box));
else
members.push_back(mlir::cast<mlir::Type>(convertType(mem)));
members.push_back(convertType(mem).cast<mlir::Type>());
}
return mlir::LLVM::LLVMStructType::getLiteral(&getContext(), members,
/*isPacked=*/false);
Expand Down Expand Up @@ -181,10 +181,10 @@ std::optional<mlir::LogicalResult> LLVMTypeConverter::convertRecordType(
for (auto mem : derived.getTypeList()) {
// Prevent fir.box from degenerating to a pointer to a descriptor in the
// context of a record type.
if (auto box = mlir::dyn_cast<fir::BaseBoxType>(mem.second))
if (auto box = mem.second.dyn_cast<fir::BaseBoxType>())
members.push_back(convertBoxTypeAsStruct(box));
else
members.push_back(mlir::cast<mlir::Type>(convertType(mem.second)));
members.push_back(convertType(mem.second).cast<mlir::Type>());
}
if (mlir::failed(st.setBody(members, /*isPacked=*/false)))
return mlir::failure();
Expand All @@ -196,7 +196,7 @@ std::optional<mlir::LogicalResult> LLVMTypeConverter::convertRecordType(
// Extended descriptors are required for derived types.
bool LLVMTypeConverter::requiresExtendedDesc(mlir::Type boxElementType) const {
auto eleTy = fir::unwrapSequenceType(boxElementType);
return mlir::isa<fir::RecordType>(eleTy);
return eleTy.isa<fir::RecordType>();
}

// This corresponds to the descriptor as defined in ISO_Fortran_binding.h and
Expand All @@ -211,8 +211,7 @@ mlir::Type LLVMTypeConverter::convertBoxTypeAsStruct(BaseBoxType box,
ele = removeIndirection;
auto eleTy = convertType(ele);
// base_addr*
if (mlir::isa<SequenceType>(ele) &&
mlir::isa<mlir::LLVM::LLVMPointerType>(eleTy))
if (ele.isa<SequenceType>() && eleTy.isa<mlir::LLVM::LLVMPointerType>())
dataDescFields.push_back(eleTy);
else
dataDescFields.push_back(
Expand All @@ -237,7 +236,7 @@ mlir::Type LLVMTypeConverter::convertBoxTypeAsStruct(BaseBoxType box,
getDescFieldTypeModel<kF18AddendumPosInBox>()(&getContext()));
// [dims]
if (rank == unknownRank()) {
if (auto seqTy = mlir::dyn_cast<SequenceType>(ele))
if (auto seqTy = ele.dyn_cast<SequenceType>())
rank = seqTy.getDimension();
else
rank = 0;
Expand All @@ -253,8 +252,7 @@ mlir::Type LLVMTypeConverter::convertBoxTypeAsStruct(BaseBoxType box,
auto rowTy =
getExtendedDescFieldTypeModel<kOptRowTypePosInBox>()(&getContext());
dataDescFields.push_back(mlir::LLVM::LLVMArrayType::get(rowTy, 1));
if (auto recTy =
mlir::dyn_cast<fir::RecordType>(fir::unwrapSequenceType(ele)))
if (auto recTy = fir::unwrapSequenceType(ele).dyn_cast<fir::RecordType>())
if (recTy.getNumLenParams() > 0) {
// The descriptor design needs to be clarified regarding the number of
// length parameters in the addendum. Since it can change for
Expand Down
14 changes: 7 additions & 7 deletions flang/lib/Optimizer/Dialect/FIRAttr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -264,23 +264,23 @@ void fir::FortranVariableFlagsAttr::print(mlir::AsmPrinter &printer) const {
void fir::printFirAttribute(FIROpsDialect *dialect, mlir::Attribute attr,
mlir::DialectAsmPrinter &p) {
auto &os = p.getStream();
if (auto exact = mlir::dyn_cast<fir::ExactTypeAttr>(attr)) {
if (auto exact = attr.dyn_cast<fir::ExactTypeAttr>()) {
os << fir::ExactTypeAttr::getAttrName() << '<';
p.printType(exact.getType());
os << '>';
} else if (auto sub = mlir::dyn_cast<fir::SubclassAttr>(attr)) {
} else if (auto sub = attr.dyn_cast<fir::SubclassAttr>()) {
os << fir::SubclassAttr::getAttrName() << '<';
p.printType(sub.getType());
os << '>';
} else if (mlir::dyn_cast_or_null<fir::PointIntervalAttr>(attr)) {
} else if (attr.dyn_cast_or_null<fir::PointIntervalAttr>()) {
os << fir::PointIntervalAttr::getAttrName();
} else if (mlir::dyn_cast_or_null<fir::ClosedIntervalAttr>(attr)) {
} else if (attr.dyn_cast_or_null<fir::ClosedIntervalAttr>()) {
os << fir::ClosedIntervalAttr::getAttrName();
} else if (mlir::dyn_cast_or_null<fir::LowerBoundAttr>(attr)) {
} else if (attr.dyn_cast_or_null<fir::LowerBoundAttr>()) {
os << fir::LowerBoundAttr::getAttrName();
} else if (mlir::dyn_cast_or_null<fir::UpperBoundAttr>(attr)) {
} else if (attr.dyn_cast_or_null<fir::UpperBoundAttr>()) {
os << fir::UpperBoundAttr::getAttrName();
} else if (auto a = mlir::dyn_cast_or_null<fir::RealAttr>(attr)) {
} else if (auto a = attr.dyn_cast_or_null<fir::RealAttr>()) {
os << fir::RealAttr::getAttrName() << '<' << a.getFKind() << ", i x";
llvm::SmallString<40> ss;
a.getValue().bitcastToAPInt().toStringUnsigned(ss, 16);
Expand Down
302 changes: 149 additions & 153 deletions flang/lib/Optimizer/Dialect/FIROps.cpp

Large diffs are not rendered by default.

121 changes: 59 additions & 62 deletions flang/lib/Optimizer/Dialect/FIRType.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,14 @@ TYPE parseTypeSingleton(mlir::AsmParser &parser) {
/// Is `ty` a standard or FIR integer type?
static bool isaIntegerType(mlir::Type ty) {
// TODO: why aren't we using isa_integer? investigatation required.
return mlir::isa<mlir::IntegerType, fir::IntegerType>(ty);
return ty.isa<mlir::IntegerType>() || ty.isa<fir::IntegerType>();
}

bool verifyRecordMemberType(mlir::Type ty) {
return !mlir::isa<BoxCharType, ShapeType, ShapeShiftType, ShiftType,
SliceType, FieldType, LenType, ReferenceType, TypeDescType>(
ty);
return !(ty.isa<BoxCharType>() || ty.isa<ShapeType>() ||
ty.isa<ShapeShiftType>() || ty.isa<ShiftType>() ||
ty.isa<SliceType>() || ty.isa<FieldType>() || ty.isa<LenType>() ||
ty.isa<ReferenceType>() || ty.isa<TypeDescType>());
}

bool verifySameLists(llvm::ArrayRef<RecordType::TypePair> a1,
Expand Down Expand Up @@ -193,7 +194,7 @@ bool isa_std_type(mlir::Type t) {
}

bool isa_fir_or_std_type(mlir::Type t) {
if (auto funcType = mlir::dyn_cast<mlir::FunctionType>(t))
if (auto funcType = t.dyn_cast<mlir::FunctionType>())
return llvm::all_of(funcType.getInputs(), isa_fir_or_std_type) &&
llvm::all_of(funcType.getResults(), isa_fir_or_std_type);
return isa_fir_type(t) || isa_std_type(t);
Expand All @@ -202,7 +203,7 @@ bool isa_fir_or_std_type(mlir::Type t) {
mlir::Type getDerivedType(mlir::Type ty) {
return llvm::TypeSwitch<mlir::Type, mlir::Type>(ty)
.Case<fir::PointerType, fir::HeapType, fir::SequenceType>([](auto p) {
if (auto seq = mlir::dyn_cast<fir::SequenceType>(p.getEleTy()))
if (auto seq = p.getEleTy().template dyn_cast<fir::SequenceType>())
return seq.getEleTy();
return p.getEleTy();
})
Expand All @@ -227,12 +228,12 @@ mlir::Type dyn_cast_ptrOrBoxEleTy(mlir::Type t) {

static bool hasDynamicSize(fir::RecordType recTy) {
for (auto field : recTy.getTypeList()) {
if (auto arr = mlir::dyn_cast<fir::SequenceType>(field.second)) {
if (auto arr = field.second.dyn_cast<fir::SequenceType>()) {
if (sequenceWithNonConstantShape(arr))
return true;
} else if (characterWithDynamicLen(field.second)) {
return true;
} else if (auto rec = mlir::dyn_cast<fir::RecordType>(field.second)) {
} else if (auto rec = field.second.dyn_cast<fir::RecordType>()) {
if (hasDynamicSize(rec))
return true;
}
Expand All @@ -241,14 +242,14 @@ static bool hasDynamicSize(fir::RecordType recTy) {
}

bool hasDynamicSize(mlir::Type t) {
if (auto arr = mlir::dyn_cast<fir::SequenceType>(t)) {
if (auto arr = t.dyn_cast<fir::SequenceType>()) {
if (sequenceWithNonConstantShape(arr))
return true;
t = arr.getEleTy();
}
if (characterWithDynamicLen(t))
return true;
if (auto rec = mlir::dyn_cast<fir::RecordType>(t))
if (auto rec = t.dyn_cast<fir::RecordType>())
return hasDynamicSize(rec);
return false;
}
Expand All @@ -268,47 +269,47 @@ mlir::Type extractSequenceType(mlir::Type ty) {
bool isPointerType(mlir::Type ty) {
if (auto refTy = fir::dyn_cast_ptrEleTy(ty))
ty = refTy;
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(ty))
return mlir::isa<fir::PointerType>(boxTy.getEleTy());
if (auto boxTy = ty.dyn_cast<fir::BaseBoxType>())
return boxTy.getEleTy().isa<fir::PointerType>();
return false;
}

bool isAllocatableType(mlir::Type ty) {
if (auto refTy = fir::dyn_cast_ptrEleTy(ty))
ty = refTy;
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(ty))
return mlir::isa<fir::HeapType>(boxTy.getEleTy());
if (auto boxTy = ty.dyn_cast<fir::BaseBoxType>())
return boxTy.getEleTy().isa<fir::HeapType>();
return false;
}

bool isBoxNone(mlir::Type ty) {
if (auto box = mlir::dyn_cast<fir::BoxType>(ty))
return mlir::isa<mlir::NoneType>(box.getEleTy());
if (auto box = ty.dyn_cast<fir::BoxType>())
return box.getEleTy().isa<mlir::NoneType>();
return false;
}

bool isBoxedRecordType(mlir::Type ty) {
if (auto refTy = fir::dyn_cast_ptrEleTy(ty))
ty = refTy;
if (auto boxTy = mlir::dyn_cast<fir::BoxType>(ty)) {
if (mlir::isa<fir::RecordType>(boxTy.getEleTy()))
if (auto boxTy = ty.dyn_cast<fir::BoxType>()) {
if (boxTy.getEleTy().isa<fir::RecordType>())
return true;
mlir::Type innerType = boxTy.unwrapInnerType();
return innerType && mlir::isa<fir::RecordType>(innerType);
return innerType && innerType.isa<fir::RecordType>();
}
return false;
}

bool isScalarBoxedRecordType(mlir::Type ty) {
if (auto refTy = fir::dyn_cast_ptrEleTy(ty))
ty = refTy;
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(ty)) {
if (mlir::isa<fir::RecordType>(boxTy.getEleTy()))
if (auto boxTy = ty.dyn_cast<fir::BaseBoxType>()) {
if (boxTy.getEleTy().isa<fir::RecordType>())
return true;
if (auto heapTy = mlir::dyn_cast<fir::HeapType>(boxTy.getEleTy()))
return mlir::isa<fir::RecordType>(heapTy.getEleTy());
if (auto ptrTy = mlir::dyn_cast<fir::PointerType>(boxTy.getEleTy()))
return mlir::isa<fir::RecordType>(ptrTy.getEleTy());
if (auto heapTy = boxTy.getEleTy().dyn_cast<fir::HeapType>())
return heapTy.getEleTy().isa<fir::RecordType>();
if (auto ptrTy = boxTy.getEleTy().dyn_cast<fir::PointerType>())
return ptrTy.getEleTy().isa<fir::RecordType>();
}
return false;
}
Expand Down Expand Up @@ -362,10 +363,10 @@ bool isPolymorphicType(mlir::Type ty) {
bool isUnlimitedPolymorphicType(mlir::Type ty) {
// CLASS(*)
if (auto clTy = mlir::dyn_cast<fir::ClassType>(fir::unwrapRefType(ty))) {
if (mlir::isa<mlir::NoneType>(clTy.getEleTy()))
if (clTy.getEleTy().isa<mlir::NoneType>())
return true;
mlir::Type innerType = clTy.unwrapInnerType();
return innerType && mlir::isa<mlir::NoneType>(innerType);
return innerType && innerType.isa<mlir::NoneType>();
}
// TYPE(*)
return isAssumedType(ty);
Expand All @@ -375,7 +376,7 @@ mlir::Type unwrapInnerType(mlir::Type ty) {
return llvm::TypeSwitch<mlir::Type, mlir::Type>(ty)
.Case<fir::PointerType, fir::HeapType, fir::SequenceType>([](auto t) {
mlir::Type eleTy = t.getEleTy();
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(eleTy))
if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>())
return seqTy.getEleTy();
return eleTy;
})
Expand All @@ -384,27 +385,25 @@ mlir::Type unwrapInnerType(mlir::Type ty) {
}

bool isRecordWithAllocatableMember(mlir::Type ty) {
if (auto recTy = mlir::dyn_cast<fir::RecordType>(ty))
if (auto recTy = ty.dyn_cast<fir::RecordType>())
for (auto [field, memTy] : recTy.getTypeList()) {
if (fir::isAllocatableType(memTy))
return true;
// A record type cannot recursively include itself as a direct member.
// There must be an intervening `ptr` type, so recursion is safe here.
if (mlir::isa<fir::RecordType>(memTy) &&
isRecordWithAllocatableMember(memTy))
if (memTy.isa<fir::RecordType>() && isRecordWithAllocatableMember(memTy))
return true;
}
return false;
}

bool isRecordWithDescriptorMember(mlir::Type ty) {
ty = unwrapSequenceType(ty);
if (auto recTy = mlir::dyn_cast<fir::RecordType>(ty))
if (auto recTy = ty.dyn_cast<fir::RecordType>())
for (auto [field, memTy] : recTy.getTypeList()) {
if (mlir::isa<fir::BaseBoxType>(memTy))
return true;
if (mlir::isa<fir::RecordType>(memTy) &&
isRecordWithDescriptorMember(memTy))
if (memTy.isa<fir::RecordType>() && isRecordWithDescriptorMember(memTy))
return true;
}
return false;
Expand All @@ -413,7 +412,7 @@ bool isRecordWithDescriptorMember(mlir::Type ty) {
mlir::Type unwrapAllRefAndSeqType(mlir::Type ty) {
while (true) {
mlir::Type nt = unwrapSequenceType(unwrapRefType(ty));
if (auto vecTy = mlir::dyn_cast<fir::VectorType>(nt))
if (auto vecTy = nt.dyn_cast<fir::VectorType>())
nt = vecTy.getEleTy();
if (nt == ty)
return ty;
Expand All @@ -422,27 +421,27 @@ mlir::Type unwrapAllRefAndSeqType(mlir::Type ty) {
}

mlir::Type unwrapSeqOrBoxedSeqType(mlir::Type ty) {
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(ty))
if (auto seqTy = ty.dyn_cast<fir::SequenceType>())
return seqTy.getEleTy();
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(ty)) {
if (auto boxTy = ty.dyn_cast<fir::BaseBoxType>()) {
auto eleTy = unwrapRefType(boxTy.getEleTy());
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(eleTy))
if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>())
return seqTy.getEleTy();
}
return ty;
}

unsigned getBoxRank(mlir::Type boxTy) {
auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(boxTy);
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(eleTy))
if (auto seqTy = eleTy.dyn_cast<fir::SequenceType>())
return seqTy.getDimension();
return 0;
}

/// Return the ISO_C_BINDING intrinsic module value of type \p ty.
int getTypeCode(mlir::Type ty, const fir::KindMapping &kindMap) {
unsigned width = 0;
if (mlir::IntegerType intTy = mlir::dyn_cast<mlir::IntegerType>(ty)) {
if (mlir::IntegerType intTy = ty.dyn_cast<mlir::IntegerType>()) {
switch (intTy.getWidth()) {
case 8:
return CFI_type_int8_t;
Expand All @@ -457,7 +456,7 @@ int getTypeCode(mlir::Type ty, const fir::KindMapping &kindMap) {
}
llvm_unreachable("unsupported integer type");
}
if (fir::LogicalType logicalTy = mlir::dyn_cast<fir::LogicalType>(ty)) {
if (fir::LogicalType logicalTy = ty.dyn_cast<fir::LogicalType>()) {
switch (kindMap.getLogicalBitsize(logicalTy.getFKind())) {
case 8:
return CFI_type_Bool;
Expand All @@ -470,7 +469,7 @@ int getTypeCode(mlir::Type ty, const fir::KindMapping &kindMap) {
}
llvm_unreachable("unsupported logical type");
}
if (mlir::FloatType floatTy = mlir::dyn_cast<mlir::FloatType>(ty)) {
if (mlir::FloatType floatTy = ty.dyn_cast<mlir::FloatType>()) {
switch (floatTy.getWidth()) {
case 16:
return floatTy.isBF16() ? CFI_type_bfloat : CFI_type_half_float;
Expand All @@ -486,14 +485,13 @@ int getTypeCode(mlir::Type ty, const fir::KindMapping &kindMap) {
llvm_unreachable("unsupported real type");
}
if (fir::isa_complex(ty)) {
if (mlir::ComplexType complexTy = mlir::dyn_cast<mlir::ComplexType>(ty)) {
if (mlir::ComplexType complexTy = ty.dyn_cast<mlir::ComplexType>()) {
mlir::FloatType floatTy =
mlir::cast<mlir::FloatType>(complexTy.getElementType());
complexTy.getElementType().cast<mlir::FloatType>();
if (floatTy.isBF16())
return CFI_type_bfloat_Complex;
width = floatTy.getWidth();
} else if (fir::ComplexType complexTy =
mlir::dyn_cast<fir::ComplexType>(ty)) {
} else if (fir::ComplexType complexTy = ty.dyn_cast<fir::ComplexType>()) {
auto FKind = complexTy.getFKind();
if (FKind == 3)
return CFI_type_bfloat_Complex;
Expand All @@ -513,7 +511,7 @@ int getTypeCode(mlir::Type ty, const fir::KindMapping &kindMap) {
}
llvm_unreachable("unsupported complex size");
}
if (fir::CharacterType charTy = mlir::dyn_cast<fir::CharacterType>(ty)) {
if (fir::CharacterType charTy = ty.dyn_cast<fir::CharacterType>()) {
switch (kindMap.getCharacterBitsize(charTy.getFKind())) {
case 8:
return CFI_type_char;
Expand All @@ -526,7 +524,7 @@ int getTypeCode(mlir::Type ty, const fir::KindMapping &kindMap) {
}
if (fir::isa_ref_type(ty))
return CFI_type_cptr;
if (mlir::isa<fir::RecordType>(ty))
if (ty.isa<fir::RecordType>())
return CFI_type_struct;
llvm_unreachable("unsupported type");
}
Expand All @@ -544,12 +542,12 @@ std::string getTypeAsString(mlir::Type ty, const fir::KindMapping &kindMap,
name << "idx";
} else if (ty.isIntOrIndex()) {
name << 'i' << ty.getIntOrFloatBitWidth();
} else if (mlir::isa<mlir::FloatType>(ty)) {
} else if (ty.isa<mlir::FloatType>()) {
name << 'f' << ty.getIntOrFloatBitWidth();
} else if (fir::isa_complex(ty)) {
name << 'z';
if (auto cplxTy = mlir::dyn_cast_or_null<mlir::ComplexType>(ty)) {
auto floatTy = mlir::cast<mlir::FloatType>(cplxTy.getElementType());
auto floatTy = cplxTy.getElementType().cast<mlir::FloatType>();
name << floatTy.getWidth();
} else if (auto cplxTy = mlir::dyn_cast_or_null<fir::ComplexType>(ty)) {
name << kindMap.getRealBitsize(cplxTy.getFKind());
Expand Down Expand Up @@ -646,7 +644,7 @@ static llvm::SmallPtrSet<detail::RecordTypeStorage const *, 4>
} // namespace

void fir::verifyIntegralType(mlir::Type type) {
if (isaIntegerType(type) || mlir::isa<mlir::IndexType>(type))
if (isaIntegerType(type) || type.isa<mlir::IndexType>())
return;
llvm::report_fatal_error("expected integral type");
}
Expand All @@ -658,9 +656,9 @@ void fir::printFirType(FIROpsDialect *, mlir::Type ty,
}

bool fir::isa_unknown_size_box(mlir::Type t) {
if (auto boxTy = mlir::dyn_cast<fir::BaseBoxType>(t)) {
if (auto boxTy = t.dyn_cast<fir::BaseBoxType>()) {
auto valueType = fir::unwrapPassByRefType(boxTy);
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(valueType))
if (auto seqTy = valueType.dyn_cast<fir::SequenceType>())
if (seqTy.hasUnknownShape())
return true;
}
Expand All @@ -686,10 +684,10 @@ void fir::BoxProcType::print(mlir::AsmPrinter &printer) const {
mlir::LogicalResult
BoxProcType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
mlir::Type eleTy) {
if (mlir::isa<mlir::FunctionType>(eleTy))
if (eleTy.isa<mlir::FunctionType>())
return mlir::success();
if (auto refTy = mlir::dyn_cast<ReferenceType>(eleTy))
if (mlir::isa<mlir::FunctionType>(refTy))
if (auto refTy = eleTy.dyn_cast<ReferenceType>())
if (refTy.isa<mlir::FunctionType>())
return mlir::success();
return emitError() << "invalid type for boxproc" << eleTy << '\n';
}
Expand All @@ -707,7 +705,7 @@ static bool cannotBePointerOrHeapElementType(mlir::Type eleTy) {
mlir::LogicalResult
fir::BoxType::verify(llvm::function_ref<mlir::InFlightDiagnostic()> emitError,
mlir::Type eleTy) {
if (mlir::isa<fir::BaseBoxType>(eleTy))
if (eleTy.isa<fir::BaseBoxType>())
return emitError() << "invalid element type\n";
// TODO
return mlir::success();
Expand Down Expand Up @@ -1238,19 +1236,18 @@ bool fir::VectorType::isValidElementType(mlir::Type t) {
}

bool fir::isCharacterProcedureTuple(mlir::Type ty, bool acceptRawFunc) {
mlir::TupleType tuple = mlir::dyn_cast<mlir::TupleType>(ty);
mlir::TupleType tuple = ty.dyn_cast<mlir::TupleType>();
return tuple && tuple.size() == 2 &&
(mlir::isa<fir::BoxProcType>(tuple.getType(0)) ||
(acceptRawFunc && mlir::isa<mlir::FunctionType>(tuple.getType(0)))) &&
(tuple.getType(0).isa<fir::BoxProcType>() ||
(acceptRawFunc && tuple.getType(0).isa<mlir::FunctionType>())) &&
fir::isa_integer(tuple.getType(1));
}

bool fir::hasAbstractResult(mlir::FunctionType ty) {
if (ty.getNumResults() == 0)
return false;
auto resultType = ty.getResult(0);
return mlir::isa<fir::SequenceType, fir::BaseBoxType, fir::RecordType>(
resultType);
return resultType.isa<fir::SequenceType, fir::BaseBoxType, fir::RecordType>();
}

/// Convert llvm::Type::TypeID to mlir::Type. \p kind is provided for error
Expand Down
11 changes: 5 additions & 6 deletions flang/lib/Optimizer/Dialect/FortranVariableInterface.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ mlir::LogicalResult
fir::FortranVariableOpInterface::verifyDeclareLikeOpImpl(mlir::Value memref) {
const unsigned numExplicitTypeParams = getExplicitTypeParams().size();
mlir::Type memType = memref.getType();
const bool sourceIsBoxValue = mlir::isa<fir::BaseBoxType>(memType);
const bool sourceIsBoxValue = memType.isa<fir::BaseBoxType>();
const bool sourceIsBoxAddress = fir::isBoxAddress(memType);
const bool sourceIsBox = sourceIsBoxValue || sourceIsBoxAddress;
if (isCharacter()) {
Expand All @@ -29,8 +29,7 @@ fir::FortranVariableOpInterface::verifyDeclareLikeOpImpl(mlir::Value memref) {
return emitOpError("must be provided exactly one type parameter when its "
"base is a character that is not a box");

} else if (auto recordType =
mlir::dyn_cast<fir::RecordType>(getElementType())) {
} else if (auto recordType = getElementType().dyn_cast<fir::RecordType>()) {
if (numExplicitTypeParams < recordType.getNumLenParams() && !sourceIsBox)
return emitOpError("must be provided all the derived type length "
"parameters when the base is not a box");
Expand All @@ -46,16 +45,16 @@ fir::FortranVariableOpInterface::verifyDeclareLikeOpImpl(mlir::Value memref) {
if (sourceIsBoxAddress)
return emitOpError("for box address must not have a shape operand");
unsigned shapeRank = 0;
if (auto shapeType = mlir::dyn_cast<fir::ShapeType>(shape.getType())) {
if (auto shapeType = shape.getType().dyn_cast<fir::ShapeType>()) {
shapeRank = shapeType.getRank();
} else if (auto shapeShiftType =
mlir::dyn_cast<fir::ShapeShiftType>(shape.getType())) {
shape.getType().dyn_cast<fir::ShapeShiftType>()) {
shapeRank = shapeShiftType.getRank();
} else {
if (!sourceIsBoxValue)
emitOpError("of array entity with a raw address base must have a "
"shape operand that is a shape or shapeshift");
shapeRank = mlir::cast<fir::ShiftType>(shape.getType()).getRank();
shapeRank = shape.getType().cast<fir::ShiftType>().getRank();
}

std::optional<unsigned> rank = getRank();
Expand Down
30 changes: 14 additions & 16 deletions flang/lib/Optimizer/HLFIR/IR/HLFIRDialect.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,8 +84,7 @@ bool hlfir::isFortranVariableType(mlir::Type type) {
return llvm::TypeSwitch<mlir::Type, bool>(type)
.Case<fir::ReferenceType, fir::PointerType, fir::HeapType>([](auto p) {
mlir::Type eleType = p.getEleTy();
return mlir::isa<fir::BaseBoxType>(eleType) ||
!fir::hasDynamicSize(eleType);
return eleType.isa<fir::BaseBoxType>() || !fir::hasDynamicSize(eleType);
})
.Case<fir::BaseBoxType, fir::BoxCharType>([](auto) { return true; })
.Case<fir::VectorType>([](auto) { return true; })
Expand All @@ -94,15 +93,15 @@ bool hlfir::isFortranVariableType(mlir::Type type) {

bool hlfir::isFortranScalarCharacterType(mlir::Type type) {
return isFortranScalarCharacterExprType(type) ||
mlir::isa<fir::BoxCharType>(type) ||
mlir::isa<fir::CharacterType>(
fir::unwrapPassByRefType(fir::unwrapRefType(type)));
type.isa<fir::BoxCharType>() ||
fir::unwrapPassByRefType(fir::unwrapRefType(type))
.isa<fir::CharacterType>();
}

bool hlfir::isFortranScalarCharacterExprType(mlir::Type type) {
if (auto exprType = mlir::dyn_cast<hlfir::ExprType>(type))
if (auto exprType = type.dyn_cast<hlfir::ExprType>())
return exprType.isScalar() &&
mlir::isa<fir::CharacterType>(exprType.getElementType());
exprType.getElementType().isa<fir::CharacterType>();
return false;
}

Expand All @@ -122,17 +121,17 @@ bool hlfir::isFortranScalarNumericalType(mlir::Type type) {
bool hlfir::isFortranNumericalArrayObject(mlir::Type type) {
if (isBoxAddressType(type))
return false;
if (auto arrayTy = mlir::dyn_cast<fir::SequenceType>(
getFortranElementOrSequenceType(type)))
if (auto arrayTy =
getFortranElementOrSequenceType(type).dyn_cast<fir::SequenceType>())
return isFortranScalarNumericalType(arrayTy.getEleTy());
return false;
}

bool hlfir::isFortranNumericalOrLogicalArrayObject(mlir::Type type) {
if (isBoxAddressType(type))
return false;
if (auto arrayTy = mlir::dyn_cast<fir::SequenceType>(
getFortranElementOrSequenceType(type))) {
if (auto arrayTy =
getFortranElementOrSequenceType(type).dyn_cast<fir::SequenceType>()) {
mlir::Type eleTy = arrayTy.getEleTy();
return isFortranScalarNumericalType(eleTy) ||
mlir::isa<fir::LogicalType>(eleTy);
Expand All @@ -143,8 +142,7 @@ bool hlfir::isFortranNumericalOrLogicalArrayObject(mlir::Type type) {
bool hlfir::isFortranArrayObject(mlir::Type type) {
if (isBoxAddressType(type))
return false;
return !!mlir::dyn_cast<fir::SequenceType>(
getFortranElementOrSequenceType(type));
return !!getFortranElementOrSequenceType(type).dyn_cast<fir::SequenceType>();
}

bool hlfir::isPassByRefOrIntegerType(mlir::Type type) {
Expand All @@ -153,7 +151,7 @@ bool hlfir::isPassByRefOrIntegerType(mlir::Type type) {
}

bool hlfir::isI1Type(mlir::Type type) {
if (mlir::IntegerType integer = mlir::dyn_cast<mlir::IntegerType>(type))
if (mlir::IntegerType integer = type.dyn_cast<mlir::IntegerType>())
if (integer.getWidth() == 1)
return true;
return false;
Expand All @@ -162,8 +160,8 @@ bool hlfir::isI1Type(mlir::Type type) {
bool hlfir::isFortranLogicalArrayObject(mlir::Type type) {
if (isBoxAddressType(type))
return false;
if (auto arrayTy = mlir::dyn_cast<fir::SequenceType>(
getFortranElementOrSequenceType(type))) {
if (auto arrayTy =
getFortranElementOrSequenceType(type).dyn_cast<fir::SequenceType>()) {
mlir::Type eleTy = arrayTy.getEleTy();
return mlir::isa<fir::LogicalType>(eleTy);
}
Expand Down
192 changes: 101 additions & 91 deletions flang/lib/Optimizer/HLFIR/IR/HLFIROps.cpp

Large diffs are not rendered by default.

29 changes: 14 additions & 15 deletions flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ static mlir::Value packageBufferizedExpr(mlir::Location loc,
/// currently enforced by the verifiers that only accept HLFIR value or
/// variable types which do not include tuples.
static hlfir::Entity getBufferizedExprStorage(mlir::Value bufferizedExpr) {
auto tupleType = mlir::dyn_cast<mlir::TupleType>(bufferizedExpr.getType());
auto tupleType = bufferizedExpr.getType().dyn_cast<mlir::TupleType>();
if (!tupleType)
return hlfir::Entity{bufferizedExpr};
assert(tupleType.size() == 2 && "unexpected tuple type");
Expand All @@ -90,7 +90,7 @@ static hlfir::Entity getBufferizedExprStorage(mlir::Value bufferizedExpr) {
/// Helper to extract the clean-up flag from a tuple created by
/// packageBufferizedExpr.
static mlir::Value getBufferizedExprMustFreeFlag(mlir::Value bufferizedExpr) {
auto tupleType = mlir::dyn_cast<mlir::TupleType>(bufferizedExpr.getType());
auto tupleType = bufferizedExpr.getType().dyn_cast<mlir::TupleType>();
if (!tupleType)
return bufferizedExpr;
assert(tupleType.size() == 2 && "unexpected tuple type");
Expand Down Expand Up @@ -218,7 +218,7 @@ struct ShapeOfOpConversion
} else {
// everything else failed so try to create a shape from static type info
hlfir::ExprType exprTy =
mlir::dyn_cast_or_null<hlfir::ExprType>(adaptor.getExpr().getType());
adaptor.getExpr().getType().dyn_cast_or_null<hlfir::ExprType>();
if (exprTy)
shape = hlfir::genExprShape(builder, loc, exprTy);
}
Expand Down Expand Up @@ -480,10 +480,10 @@ struct AssociateOpConversion
assert(mlir::isa<fir::ClassType>(sourceVar.getType()) &&
fir::isAllocatableType(sourceVar.getType()));
assert(sourceVar.getType() == assocType);
} else if ((mlir::isa<fir::BaseBoxType>(sourceVar.getType()) &&
!mlir::isa<fir::BaseBoxType>(assocType)) ||
((mlir::isa<fir::BoxCharType>(sourceVar.getType()) &&
!mlir::isa<fir::BoxCharType>(assocType)))) {
} else if ((sourceVar.getType().isa<fir::BaseBoxType>() &&
!assocType.isa<fir::BaseBoxType>()) ||
((sourceVar.getType().isa<fir::BoxCharType>() &&
!assocType.isa<fir::BoxCharType>()))) {
sourceVar = builder.create<fir::BoxAddrOp>(loc, assocType, sourceVar);
} else {
sourceVar = builder.createConvert(loc, assocType, sourceVar);
Expand Down Expand Up @@ -590,13 +590,13 @@ static void genBufferDestruction(mlir::Location loc, fir::FirOpBuilder &builder,
// for MERGE with polymorphic results.
if (mustFinalize)
TODO(loc, "finalizing polymorphic temporary in HLFIR");
} else if (mlir::isa<fir::BaseBoxType, fir::BoxCharType>(var.getType())) {
} else if (var.getType().isa<fir::BaseBoxType, fir::BoxCharType>()) {
if (mustFinalize && !mlir::isa<fir::BaseBoxType>(var.getType()))
fir::emitFatalError(loc, "non-finalizable variable");

addr = builder.create<fir::BoxAddrOp>(loc, heapType, var);
} else {
if (!mlir::isa<fir::HeapType>(var.getType()))
if (!var.getType().isa<fir::HeapType>())
addr = builder.create<fir::ConvertOp>(loc, heapType, var);

if (mustFinalize || deallocComponents) {
Expand Down Expand Up @@ -831,7 +831,7 @@ struct ElementalOpConversion
// the assign, insert an hlfir.destroy to mark the expression end-of-life.
// If the expression creation allocated a buffer on the heap inside the
// loop, this will ensure the buffer properly deallocated.
if (mlir::isa<hlfir::ExprType>(elementValue.getType()) &&
if (elementValue.getType().isa<hlfir::ExprType>() &&
wasCreatedInCurrentBlock(elementValue, builder))
builder.create<hlfir::DestroyOp>(loc, elementValue);
}
Expand Down Expand Up @@ -926,12 +926,11 @@ class BufferizeHLFIR : public hlfir::impl::BufferizeHLFIRBase<BufferizeHLFIR> {
hlfir::EndAssociateOp, hlfir::SetLengthOp>();

target.markUnknownOpDynamicallyLegal([](mlir::Operation *op) {
return llvm::all_of(op->getResultTypes(),
[](mlir::Type ty) {
return !mlir::isa<hlfir::ExprType>(ty);
}) &&
return llvm::all_of(
op->getResultTypes(),
[](mlir::Type ty) { return !ty.isa<hlfir::ExprType>(); }) &&
llvm::all_of(op->getOperandTypes(), [](mlir::Type ty) {
return !mlir::isa<hlfir::ExprType>(ty);
return !ty.isa<hlfir::ExprType>();
});
});
if (mlir::failed(
Expand Down
47 changes: 23 additions & 24 deletions flang/lib/Optimizer/HLFIR/Transforms/ConvertToFIR.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ using namespace mlir;
static mlir::Value genAllocatableTempFromSourceBox(mlir::Location loc,
fir::FirOpBuilder &builder,
mlir::Value sourceBox) {
assert(mlir::isa<fir::BaseBoxType>(sourceBox.getType()) &&
assert(sourceBox.getType().isa<fir::BaseBoxType>() &&
"must be a base box type");
// Use the runtime to make a quick and dirty temp with the rhs value.
// Overkill for scalar rhs that could be done in much more clever ways.
Expand All @@ -44,7 +44,7 @@ static mlir::Value genAllocatableTempFromSourceBox(mlir::Location loc,
// This has the huge benefit of dealing with all cases, including
// polymorphic entities.
mlir::Type fromHeapType = fir::HeapType::get(fir::unwrapRefType(
mlir::cast<fir::BaseBoxType>(sourceBox.getType()).getEleTy()));
sourceBox.getType().cast<fir::BaseBoxType>().getEleTy()));
mlir::Type fromBoxHeapType = fir::BoxType::get(fromHeapType);
mlir::Value fromMutableBox =
fir::factory::genNullBoxStorage(builder, loc, fromBoxHeapType);
Expand All @@ -69,7 +69,7 @@ class AssignOpConversion : public mlir::OpRewritePattern<hlfir::AssignOp> {
auto module = assignOp->getParentOfType<mlir::ModuleOp>();
fir::FirOpBuilder builder(rewriter, module);

if (mlir::isa<hlfir::ExprType>(rhs.getType())) {
if (rhs.getType().isa<hlfir::ExprType>()) {
mlir::emitError(loc, "hlfir must be bufferized with --bufferize-hlfir "
"pass before being converted to FIR");
return mlir::failure();
Expand Down Expand Up @@ -343,15 +343,16 @@ class DeclareOpConversion : public mlir::OpRewritePattern<hlfir::DeclareOp> {
auto firBase = firDeclareOp.getResult();
mlir::Value hlfirBase;
mlir::Type hlfirBaseType = declareOp.getBase().getType();
if (mlir::isa<fir::BaseBoxType>(hlfirBaseType)) {
if (hlfirBaseType.isa<fir::BaseBoxType>()) {
fir::FirOpBuilder builder(rewriter, declareOp.getOperation());
// Helper to generate the hlfir fir.box with the local lower bounds and
// type parameters.
auto genHlfirBox = [&]() -> mlir::Value {
if (!mlir::isa<fir::BaseBoxType>(firBase.getType())) {
if (!firBase.getType().isa<fir::BaseBoxType>()) {
llvm::SmallVector<mlir::Value> typeParams;
auto maybeCharType = mlir::dyn_cast<fir::CharacterType>(
fir::unwrapSequenceType(fir::unwrapPassByRefType(hlfirBaseType)));
auto maybeCharType =
fir::unwrapSequenceType(fir::unwrapPassByRefType(hlfirBaseType))
.dyn_cast<fir::CharacterType>();
if (!maybeCharType || maybeCharType.hasDynamicLen())
typeParams.append(declareOp.getTypeparams().begin(),
declareOp.getTypeparams().end());
Expand Down Expand Up @@ -398,7 +399,7 @@ class DeclareOpConversion : public mlir::OpRewritePattern<hlfir::DeclareOp> {
})
.getResults()[0];
}
} else if (mlir::isa<fir::BoxCharType>(hlfirBaseType)) {
} else if (hlfirBaseType.isa<fir::BoxCharType>()) {
assert(declareOp.getTypeparams().size() == 1 &&
"must contain character length");
hlfirBase = rewriter.create<fir::EmboxCharOp>(
Expand Down Expand Up @@ -479,12 +480,11 @@ class DesignateOpConversion
// - scalar%scalar_component [substring|complex_part] or
// - scalar%static_size_array_comp
// - scalar%array(indices) [substring| complex part]
mlir::Type componentType =
mlir::cast<fir::RecordType>(baseEleTy).getType(
designate.getComponent().value());
mlir::Type componentType = baseEleTy.cast<fir::RecordType>().getType(
designate.getComponent().value());
mlir::Type coorTy = fir::ReferenceType::get(componentType);
base = builder.create<fir::CoordinateOp>(loc, coorTy, base, fieldIndex);
if (mlir::isa<fir::BaseBoxType>(componentType)) {
if (componentType.isa<fir::BaseBoxType>()) {
auto variableInterface = mlir::cast<fir::FortranVariableOpInterface>(
designate.getOperation());
if (variableInterface.isAllocatable() ||
Expand All @@ -500,14 +500,14 @@ class DesignateOpConversion
} else {
// array%component[(indices) substring|complex part] cases.
// Component ref of array bases are dealt with below in embox/rebox.
assert(mlir::isa<fir::BaseBoxType>(designateResultType));
assert(designateResultType.isa<fir::BaseBoxType>());
}
}

if (mlir::isa<fir::BaseBoxType>(designateResultType)) {
if (designateResultType.isa<fir::BaseBoxType>()) {
// Generate embox or rebox.
mlir::Type eleTy = fir::unwrapPassByRefType(designateResultType);
bool isScalarDesignator = !mlir::isa<fir::SequenceType>(eleTy);
bool isScalarDesignator = !eleTy.isa<fir::SequenceType>();
mlir::Value sourceBox;
if (isScalarDesignator) {
// The base box will be used for emboxing the scalar element.
Expand Down Expand Up @@ -583,7 +583,7 @@ class DesignateOpConversion
assert(sliceFields.empty() && substring.empty());
llvm::SmallVector<mlir::Type> resultType{designateResultType};
mlir::Value resultBox;
if (mlir::isa<fir::BaseBoxType>(base.getType()))
if (base.getType().isa<fir::BaseBoxType>())
resultBox =
builder.create<fir::ReboxOp>(loc, resultType, base, shape, slice);
else
Expand All @@ -598,8 +598,7 @@ class DesignateOpConversion
// first element of a contiguous array section with compile time constant
// shape. The base may be an array, or a scalar.
mlir::Type resultAddressType = designateResultType;
if (auto boxCharType =
mlir::dyn_cast<fir::BoxCharType>(designateResultType))
if (auto boxCharType = designateResultType.dyn_cast<fir::BoxCharType>())
resultAddressType = fir::ReferenceType::get(boxCharType.getEleTy());

// Array element indexing.
Expand All @@ -621,15 +620,15 @@ class DesignateOpConversion
// Scalar complex part ref
if (designate.getComplexPart()) {
// Sequence types should have already been handled by this point
assert(!mlir::isa<fir::SequenceType>(designateResultType));
assert(!designateResultType.isa<fir::SequenceType>());
auto index = builder.createIntegerConstant(loc, builder.getIndexType(),
*designate.getComplexPart());
auto coorTy = fir::ReferenceType::get(resultEleTy);
base = builder.create<fir::CoordinateOp>(loc, coorTy, base, index);
}

// Cast/embox the computed scalar address if needed.
if (mlir::isa<fir::BoxCharType>(designateResultType)) {
if (designateResultType.isa<fir::BoxCharType>()) {
assert(designate.getTypeparams().size() == 1 &&
"must have character length");
auto emboxChar = builder.create<fir::EmboxCharOp>(
Expand Down Expand Up @@ -672,13 +671,13 @@ class ParentComponentOpConversion
mlir::PatternRewriter &rewriter) const override {
mlir::Location loc = parentComponent.getLoc();
mlir::Type resultType = parentComponent.getType();
if (!mlir::isa<fir::BoxType>(parentComponent.getType())) {
if (!parentComponent.getType().isa<fir::BoxType>()) {
mlir::Value baseAddr = parentComponent.getMemref();
// Scalar parent component ref without any length type parameters. The
// input may be a fir.class if it is polymorphic, since this is a scalar
// and the output will be monomorphic, the base address can be extracted
// from the fir.class.
if (mlir::isa<fir::BaseBoxType>(baseAddr.getType()))
if (baseAddr.getType().isa<fir::BaseBoxType>())
baseAddr = rewriter.create<fir::BoxAddrOp>(loc, baseAddr);
rewriter.replaceOpWithNewOp<fir::ConvertOp>(parentComponent, resultType,
baseAddr);
Expand All @@ -687,7 +686,7 @@ class ParentComponentOpConversion
// Array parent component ref or PDTs.
hlfir::Entity base{parentComponent.getMemref()};
mlir::Value baseAddr = base.getBase();
if (!mlir::isa<fir::BaseBoxType>(baseAddr.getType())) {
if (!baseAddr.getType().isa<fir::BaseBoxType>()) {
// Embox cannot directly be used to address parent components: it expects
// the output type to match the input type when there are no slices. When
// the types have at least one component, a slice to the first element can
Expand Down Expand Up @@ -749,7 +748,7 @@ class GetExtentOpConversion
// the hlfir.shape_of operation which led to the creation of this get_extent
// operation should now have been lowered to a fir.shape operation
if (auto s = mlir::dyn_cast_or_null<fir::ShapeOp>(shapeOp)) {
fir::ShapeType shapeTy = mlir::cast<fir::ShapeType>(shape.getType());
fir::ShapeType shapeTy = shape.getType().cast<fir::ShapeType>();
llvm::APInt dim = getExtentOp.getDim();
uint64_t dimVal = dim.getLimitedValue(shapeTy.getRank());
mlir::Value extent = s.getExtents()[dimVal];
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -185,7 +185,7 @@ class HlfirIntrinsicConversion : public mlir::OpRewritePattern<OP> {
// the width for use in runtime intrinsic calls.
static unsigned getKindForType(mlir::Type ty) {
mlir::Type eltty = hlfir::getFortranElementType(ty);
unsigned width = mlir::cast<mlir::IntegerType>(eltty).getWidth();
unsigned width = eltty.cast<mlir::IntegerType>().getWidth();
return width / 8;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1090,7 +1090,7 @@ void OrderedAssignmentRewriter::generateSaveEntity(
mlir::Value loopExtent =
computeLoopNestIterationNumber(loc, builder, loopNest);
auto sequenceType =
mlir::cast<fir::SequenceType>(builder.getVarLenSeqTy(entityType));
builder.getVarLenSeqTy(entityType).cast<fir::SequenceType>();
temp = insertSavedEntity(region,
fir::factory::HomogeneousScalarStack{
loc, builder, sequenceType, loopExtent,
Expand Down
12 changes: 6 additions & 6 deletions flang/lib/Optimizer/HLFIR/Transforms/OptimizedBufferization.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -249,7 +249,7 @@ static bool areIdenticalOrDisjointSlices(mlir::Value ref1, mlir::Value ref2) {
auto isPositiveConstant = [](mlir::Value v) -> bool {
if (auto conOp =
mlir::dyn_cast<mlir::arith::ConstantOp>(v.getDefiningOp()))
if (auto iattr = mlir::dyn_cast<mlir::IntegerAttr>(conOp.getValue()))
if (auto iattr = conOp.getValue().dyn_cast<mlir::IntegerAttr>())
return iattr.getInt() > 0;
return false;
};
Expand Down Expand Up @@ -601,7 +601,7 @@ mlir::LogicalResult VariableAssignBufferization::matchAndRewrite(
// TODO: ExprType check is here to avoid conflicts with
// ElementalAssignBufferization pattern. We need to combine
// these matchers into a single one that applies to AssignOp.
if (mlir::isa<hlfir::ExprType>(rhs.getType()))
if (rhs.getType().isa<hlfir::ExprType>())
return rewriter.notifyMatchFailure(assign, "RHS is not in memory");

if (!rhs.isArray())
Expand Down Expand Up @@ -834,7 +834,7 @@ class MinMaxlocElementalConversion : public mlir::OpRewritePattern<Op> {

unsigned rank = mlir::cast<hlfir::ExprType>(mloc.getType()).getShape()[0];
mlir::Type arrayType = array.getType();
if (!mlir::isa<fir::BoxType>(arrayType))
if (!arrayType.isa<fir::BoxType>())
return rewriter.notifyMatchFailure(
mloc, "Currently requires a boxed type input");
mlir::Type elementType = hlfir::getFortranElementType(arrayType);
Expand All @@ -850,7 +850,7 @@ class MinMaxlocElementalConversion : public mlir::OpRewritePattern<Op> {

auto init = [isMax](fir::FirOpBuilder builder, mlir::Location loc,
mlir::Type elementType) {
if (auto ty = mlir::dyn_cast<mlir::FloatType>(elementType)) {
if (auto ty = elementType.dyn_cast<mlir::FloatType>()) {
const llvm::fltSemantics &sem = ty.getFloatSemantics();
llvm::APFloat limit = llvm::APFloat::getInf(sem, /*Negative=*/isMax);
return builder.createRealConstant(loc, elementType, limit);
Expand Down Expand Up @@ -901,7 +901,7 @@ class MinMaxlocElementalConversion : public mlir::OpRewritePattern<Op> {

// Compare with the max reduction value
mlir::Value cmp;
if (mlir::isa<mlir::FloatType>(elementType)) {
if (elementType.isa<mlir::FloatType>()) {
// For FP reductions we want the first smallest value to be used, that
// is not NaN. A OGL/OLT condition will usually work for this unless all
// the values are Nan or Inf. This follows the same logic as
Expand All @@ -918,7 +918,7 @@ class MinMaxlocElementalConversion : public mlir::OpRewritePattern<Op> {
loc, mlir::arith::CmpFPredicate::OEQ, elem, elem);
cmpNan = builder.create<mlir::arith::AndIOp>(loc, cmpNan, cmpNan2);
cmp = builder.create<mlir::arith::OrIOp>(loc, cmp, cmpNan);
} else if (mlir::isa<mlir::IntegerType>(elementType)) {
} else if (elementType.isa<mlir::IntegerType>()) {
cmp = builder.create<mlir::arith::CmpIOp>(
loc,
isMax ? mlir::arith::CmpIPredicate::sgt
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -103,8 +103,7 @@ class SimplifyHLFIRIntrinsics
// by hlfir.elemental)
target.addDynamicallyLegalOp<hlfir::TransposeOp>(
[](hlfir::TransposeOp transpose) {
return mlir::cast<hlfir::ExprType>(transpose.getType())
.isPolymorphic();
return transpose.getType().cast<hlfir::ExprType>().isPolymorphic();
});
target.markUnknownOpDynamicallyLegal(
[](mlir::Operation *) { return true; });
Expand Down
16 changes: 8 additions & 8 deletions flang/lib/Optimizer/Transforms/AbstractResult.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ static mlir::FunctionType getCPtrFunctionType(mlir::FunctionType funcTy) {
auto resultType = funcTy.getResult(0);
assert(fir::isa_builtin_cptr_type(resultType));
llvm::SmallVector<mlir::Type> outputTypes;
auto recTy = mlir::dyn_cast<fir::RecordType>(resultType);
auto recTy = resultType.dyn_cast<fir::RecordType>();
outputTypes.emplace_back(recTy.getTypeList()[0].second);
return mlir::FunctionType::get(funcTy.getContext(), funcTy.getInputs(),
outputTypes);
}

static bool mustEmboxResult(mlir::Type resultType, bool shouldBoxResult) {
return mlir::isa<fir::SequenceType, fir::RecordType>(resultType) &&
return resultType.isa<fir::SequenceType, fir::RecordType>() &&
shouldBoxResult;
}

Expand Down Expand Up @@ -114,7 +114,7 @@ class CallConversion : public mlir::OpRewritePattern<Op> {
bool isResultBuiltinCPtr = fir::isa_builtin_cptr_type(result.getType());
Op newOp;
if (isResultBuiltinCPtr) {
auto recTy = mlir::dyn_cast<fir::RecordType>(result.getType());
auto recTy = result.getType().template dyn_cast<fir::RecordType>();
newResultTypes.emplace_back(recTy.getTypeList()[0].second);
}

Expand Down Expand Up @@ -261,7 +261,7 @@ class AddrOfOpConversion : public mlir::OpRewritePattern<fir::AddrOfOp> {
mlir::LogicalResult
matchAndRewrite(fir::AddrOfOp addrOf,
mlir::PatternRewriter &rewriter) const override {
auto oldFuncTy = mlir::cast<mlir::FunctionType>(addrOf.getType());
auto oldFuncTy = addrOf.getType().cast<mlir::FunctionType>();
mlir::FunctionType newFuncTy;
// TODO: This should be generalized for derived types, and it is
// architecture and OS dependent.
Expand Down Expand Up @@ -296,7 +296,7 @@ class AbstractResultOpt
auto loc = func.getLoc();
auto *context = &getContext();
// Convert function type itself if it has an abstract result.
auto funcTy = mlir::cast<mlir::FunctionType>(func.getFunctionType());
auto funcTy = func.getFunctionType().cast<mlir::FunctionType>();
if (hasAbstractResult(funcTy)) {
// TODO: This should be generalized for derived types, and it is
// architecture and OS dependent.
Expand Down Expand Up @@ -343,11 +343,11 @@ class AbstractResultOpt
return mlir::TypeSwitch<mlir::Type, bool>(type)
.Case([](fir::BoxProcType boxProc) {
return fir::hasAbstractResult(
mlir::cast<mlir::FunctionType>(boxProc.getEleTy()));
boxProc.getEleTy().cast<mlir::FunctionType>());
})
.Case([](fir::PointerType pointer) {
return fir::hasAbstractResult(
mlir::cast<mlir::FunctionType>(pointer.getEleTy()));
pointer.getEleTy().cast<mlir::FunctionType>());
})
.Default([](auto &&) { return false; });
}
Expand Down Expand Up @@ -411,7 +411,7 @@ class AbstractResultOpt
return !hasAbstractResult(call.getFunctionType());
});
target.addDynamicallyLegalOp<fir::AddrOfOp>([](fir::AddrOfOp addrOf) {
if (auto funTy = mlir::dyn_cast<mlir::FunctionType>(addrOf.getType()))
if (auto funTy = addrOf.getType().dyn_cast<mlir::FunctionType>())
return !hasAbstractResult(funTy);
return true;
});
Expand Down
6 changes: 3 additions & 3 deletions flang/lib/Optimizer/Transforms/AddDebugInfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ void AddDebugInfoPass::runOnOperation() {
// In that case, 'inputFilename' may be empty. Location embedded in the
// module will be used to get file name and its directory.
if (inputFilename.empty()) {
if (auto fileLoc = mlir::dyn_cast<mlir::FileLineColLoc>(module.getLoc())) {
if (auto fileLoc = module.getLoc().dyn_cast<mlir::FileLineColLoc>()) {
fileName = llvm::sys::path::filename(fileLoc.getFilename().getValue());
filePath = llvm::sys::path::parent_path(fileLoc.getFilename().getValue());
} else
Expand All @@ -94,14 +94,14 @@ void AddDebugInfoPass::runOnOperation() {
mlir::Location l = funcOp->getLoc();
// If fused location has already been created then nothing to do
// Otherwise, create a fused location.
if (mlir::dyn_cast<mlir::FusedLoc>(l))
if (l.dyn_cast<mlir::FusedLoc>())
return;

unsigned int CC = (funcOp.getName() == fir::NameUniquer::doProgramEntry())
? llvm::dwarf::getCallingConvention("DW_CC_program")
: llvm::dwarf::getCallingConvention("DW_CC_normal");

if (auto funcLoc = mlir::dyn_cast<mlir::FileLineColLoc>(l)) {
if (auto funcLoc = l.dyn_cast<mlir::FileLineColLoc>()) {
fileName = llvm::sys::path::filename(funcLoc.getFilename().getValue());
filePath = llvm::sys::path::parent_path(funcLoc.getFilename().getValue());
}
Expand Down
9 changes: 4 additions & 5 deletions flang/lib/Optimizer/Transforms/AffineDemotion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -98,15 +98,14 @@ class ConvertConversion : public mlir::OpRewritePattern<fir::ConvertOp> {
mlir::LogicalResult
matchAndRewrite(fir::ConvertOp op,
mlir::PatternRewriter &rewriter) const override {
if (mlir::isa<mlir::MemRefType>(op.getRes().getType())) {
if (op.getRes().getType().isa<mlir::MemRefType>()) {
// due to index calculation moving to affine maps we still need to
// add converts for sequence types this has a side effect of losing
// some information about arrays with known dimensions by creating:
// fir.convert %arg0 : (!fir.ref<!fir.array<5xi32>>) ->
// !fir.ref<!fir.array<?xi32>>
if (auto refTy =
mlir::dyn_cast<fir::ReferenceType>(op.getValue().getType()))
if (auto arrTy = mlir::dyn_cast<fir::SequenceType>(refTy.getEleTy())) {
if (auto refTy = op.getValue().getType().dyn_cast<fir::ReferenceType>())
if (auto arrTy = refTy.getEleTy().dyn_cast<fir::SequenceType>()) {
fir::SequenceType::Shape flatShape = {
fir::SequenceType::getUnknownExtent()};
auto flatArrTy = fir::SequenceType::get(flatShape, arrTy.getEleTy());
Expand Down Expand Up @@ -159,7 +158,7 @@ class AffineDialectDemotion
mlir::ConversionTarget target(*context);
target.addIllegalOp<memref::AllocOp>();
target.addDynamicallyLegalOp<fir::ConvertOp>([](fir::ConvertOp op) {
if (mlir::isa<mlir::MemRefType>(op.getRes().getType()))
if (op.getRes().getType().isa<mlir::MemRefType>())
return false;
return true;
});
Expand Down
Loading