blob: fa83aa380e489c3b9393688b312cd55d172eaac1 [file] [log] [blame]
//===-- FIROps.cpp --------------------------------------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//
//
// Coding style: https://mlir.llvm.org/getting_started/DeveloperGuide/
//
//===----------------------------------------------------------------------===//
#include "flang/Optimizer/Dialect/FIROps.h"
#include "flang/Optimizer/Dialect/FIRAttr.h"
#include "flang/Optimizer/Dialect/FIRDialect.h"
#include "flang/Optimizer/Dialect/FIROpsSupport.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/Dialect/Support/FIRContext.h"
#include "flang/Optimizer/Dialect/Support/KindMapping.h"
#include "flang/Optimizer/Support/Utils.h"
#include "mlir/Dialect/CommonFolders.h"
#include "mlir/Dialect/Func/IR/FuncOps.h"
#include "mlir/Dialect/OpenACC/OpenACC.h"
#include "mlir/Dialect/OpenMP/OpenMPDialect.h"
#include "mlir/IR/Attributes.h"
#include "mlir/IR/BuiltinAttributes.h"
#include "mlir/IR/BuiltinOps.h"
#include "mlir/IR/Diagnostics.h"
#include "mlir/IR/Matchers.h"
#include "mlir/IR/OpDefinition.h"
#include "mlir/IR/PatternMatch.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/ADT/SmallVector.h"
#include "llvm/ADT/TypeSwitch.h"
namespace {
#include "flang/Optimizer/Dialect/CanonicalizationPatterns.inc"
} // namespace
static void propagateAttributes(mlir::Operation *fromOp,
mlir::Operation *toOp) {
if (!fromOp || !toOp)
return;
for (mlir::NamedAttribute attr : fromOp->getAttrs()) {
if (attr.getName().getValue().starts_with(
mlir::acc::OpenACCDialect::getDialectNamespace()))
toOp->setAttr(attr.getName(), attr.getValue());
}
}
/// Return true if a sequence type is of some incomplete size or a record type
/// is malformed or contains an incomplete sequence type. An incomplete sequence
/// type is one with more unknown extents in the type than have been provided
/// via `dynamicExtents`. Sequence types with an unknown rank are incomplete by
/// definition.
static bool verifyInType(mlir::Type inType,
llvm::SmallVectorImpl<llvm::StringRef> &visited,
unsigned dynamicExtents = 0) {
if (auto st = mlir::dyn_cast<fir::SequenceType>(inType)) {
auto shape = st.getShape();
if (shape.size() == 0)
return true;
for (std::size_t i = 0, end = shape.size(); i < end; ++i) {
if (shape[i] != fir::SequenceType::getUnknownExtent())
continue;
if (dynamicExtents-- == 0)
return true;
}
} else if (auto rt = mlir::dyn_cast<fir::RecordType>(inType)) {
// don't recurse if we're already visiting this one
if (llvm::is_contained(visited, rt.getName()))
return false;
// keep track of record types currently being visited
visited.push_back(rt.getName());
for (auto &field : rt.getTypeList())
if (verifyInType(field.second, visited))
return true;
visited.pop_back();
}
return false;
}
static bool verifyTypeParamCount(mlir::Type inType, unsigned numParams) {
auto ty = fir::unwrapSequenceType(inType);
if (numParams > 0) {
if (auto recTy = mlir::dyn_cast<fir::RecordType>(ty))
return numParams != recTy.getNumLenParams();
if (auto chrTy = mlir::dyn_cast<fir::CharacterType>(ty))
return !(numParams == 1 && chrTy.hasDynamicLen());
return true;
}
if (auto chrTy = mlir::dyn_cast<fir::CharacterType>(ty))
return !chrTy.hasConstantLen();
return false;
}
/// Parser shared by Alloca and Allocmem
///
/// operation ::= %res = (`fir.alloca` | `fir.allocmem`) $in_type
/// ( `(` $typeparams `)` )? ( `,` $shape )?
/// attr-dict-without-keyword
template <typename FN>
static mlir::ParseResult parseAllocatableOp(FN wrapResultType,
mlir::OpAsmParser &parser,
mlir::OperationState &result) {
mlir::Type intype;
if (parser.parseType(intype))
return mlir::failure();
auto &builder = parser.getBuilder();
result.addAttribute("in_type", mlir::TypeAttr::get(intype));
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands;
llvm::SmallVector<mlir::Type> typeVec;
bool hasOperands = false;
std::int32_t typeparamsSize = 0;
if (!parser.parseOptionalLParen()) {
// parse the LEN params of the derived type. (<params> : <types>)
if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) ||
parser.parseColonTypeList(typeVec) || parser.parseRParen())
return mlir::failure();
typeparamsSize = operands.size();
hasOperands = true;
}
std::int32_t shapeSize = 0;
if (!parser.parseOptionalComma()) {
// parse size to scale by, vector of n dimensions of type index
if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None))
return mlir::failure();
shapeSize = operands.size() - typeparamsSize;
auto idxTy = builder.getIndexType();
for (std::int32_t i = typeparamsSize, end = operands.size(); i != end; ++i)
typeVec.push_back(idxTy);
hasOperands = true;
}
if (hasOperands &&
parser.resolveOperands(operands, typeVec, parser.getNameLoc(),
result.operands))
return mlir::failure();
mlir::Type restype = wrapResultType(intype);
if (!restype) {
parser.emitError(parser.getNameLoc(), "invalid allocate type: ") << intype;
return mlir::failure();
}
result.addAttribute("operandSegmentSizes", builder.getDenseI32ArrayAttr(
{typeparamsSize, shapeSize}));
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.addTypeToList(restype, result.types))
return mlir::failure();
return mlir::success();
}
template <typename OP>
static void printAllocatableOp(mlir::OpAsmPrinter &p, OP &op) {
p << ' ' << op.getInType();
if (!op.getTypeparams().empty()) {
p << '(' << op.getTypeparams() << " : " << op.getTypeparams().getTypes()
<< ')';
}
// print the shape of the allocation (if any); all must be index type
for (auto sh : op.getShape()) {
p << ", ";
p.printOperand(sh);
}
p.printOptionalAttrDict(op->getAttrs(), {"in_type", "operandSegmentSizes"});
}
//===----------------------------------------------------------------------===//
// AllocaOp
//===----------------------------------------------------------------------===//
/// Create a legal memory reference as return type
static mlir::Type wrapAllocaResultType(mlir::Type intype) {
// FIR semantics: memory references to memory references are disallowed
if (mlir::isa<fir::ReferenceType>(intype))
return {};
return fir::ReferenceType::get(intype);
}
mlir::Type fir::AllocaOp::getAllocatedType() {
return mlir::cast<fir::ReferenceType>(getType()).getEleTy();
}
mlir::Type fir::AllocaOp::getRefTy(mlir::Type ty) {
return fir::ReferenceType::get(ty);
}
void fir::AllocaOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
llvm::StringRef uniqName, mlir::ValueRange typeparams,
mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
auto nameAttr = builder.getStringAttr(uniqName);
build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, {},
/*pinned=*/false, typeparams, shape);
result.addAttributes(attributes);
}
void fir::AllocaOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
llvm::StringRef uniqName, bool pinned,
mlir::ValueRange typeparams, mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
auto nameAttr = builder.getStringAttr(uniqName);
build(builder, result, wrapAllocaResultType(inType), inType, nameAttr, {},
pinned, typeparams, shape);
result.addAttributes(attributes);
}
void fir::AllocaOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
llvm::StringRef uniqName, llvm::StringRef bindcName,
mlir::ValueRange typeparams, mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
auto nameAttr =
uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName);
auto bindcAttr =
bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName);
build(builder, result, wrapAllocaResultType(inType), inType, nameAttr,
bindcAttr, /*pinned=*/false, typeparams, shape);
result.addAttributes(attributes);
}
void fir::AllocaOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
llvm::StringRef uniqName, llvm::StringRef bindcName,
bool pinned, mlir::ValueRange typeparams,
mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
auto nameAttr =
uniqName.empty() ? mlir::StringAttr{} : builder.getStringAttr(uniqName);
auto bindcAttr =
bindcName.empty() ? mlir::StringAttr{} : builder.getStringAttr(bindcName);
build(builder, result, wrapAllocaResultType(inType), inType, nameAttr,
bindcAttr, pinned, typeparams, shape);
result.addAttributes(attributes);
}
void fir::AllocaOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
mlir::ValueRange typeparams, mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
build(builder, result, wrapAllocaResultType(inType), inType, {}, {},
/*pinned=*/false, typeparams, shape);
result.addAttributes(attributes);
}
void fir::AllocaOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
bool pinned, mlir::ValueRange typeparams,
mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
build(builder, result, wrapAllocaResultType(inType), inType, {}, {}, pinned,
typeparams, shape);
result.addAttributes(attributes);
}
mlir::ParseResult fir::AllocaOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
return parseAllocatableOp(wrapAllocaResultType, parser, result);
}
void fir::AllocaOp::print(mlir::OpAsmPrinter &p) {
printAllocatableOp(p, *this);
}
llvm::LogicalResult fir::AllocaOp::verify() {
llvm::SmallVector<llvm::StringRef> visited;
if (verifyInType(getInType(), visited, numShapeOperands()))
return emitOpError("invalid type for allocation");
if (verifyTypeParamCount(getInType(), numLenParams()))
return emitOpError("LEN params do not correspond to type");
mlir::Type outType = getType();
if (!mlir::isa<fir::ReferenceType>(outType))
return emitOpError("must be a !fir.ref type");
return mlir::success();
}
bool fir::AllocaOp::ownsNestedAlloca(mlir::Operation *op) {
return op->hasTrait<mlir::OpTrait::IsIsolatedFromAbove>() ||
op->hasTrait<mlir::OpTrait::AutomaticAllocationScope>() ||
mlir::isa<mlir::LoopLikeOpInterface>(*op);
}
mlir::Region *fir::AllocaOp::getOwnerRegion() {
mlir::Operation *currentOp = getOperation();
while (mlir::Operation *parentOp = currentOp->getParentOp()) {
// If the operation was not registered, inquiries about its traits will be
// incorrect and it is not possible to reason about the operation. This
// should not happen in a normal Fortran compilation flow, but be foolproof.
if (!parentOp->isRegistered())
return nullptr;
if (fir::AllocaOp::ownsNestedAlloca(parentOp))
return currentOp->getParentRegion();
currentOp = parentOp;
}
return nullptr;
}
//===----------------------------------------------------------------------===//
// AllocMemOp
//===----------------------------------------------------------------------===//
/// Create a legal heap reference as return type
static mlir::Type wrapAllocMemResultType(mlir::Type intype) {
// Fortran semantics: C852 an entity cannot be both ALLOCATABLE and POINTER
// 8.5.3 note 1 prohibits ALLOCATABLE procedures as well
// FIR semantics: one may not allocate a memory reference value
if (mlir::isa<fir::ReferenceType, fir::HeapType, fir::PointerType,
mlir::FunctionType>(intype))
return {};
return fir::HeapType::get(intype);
}
mlir::Type fir::AllocMemOp::getAllocatedType() {
return mlir::cast<fir::HeapType>(getType()).getEleTy();
}
mlir::Type fir::AllocMemOp::getRefTy(mlir::Type ty) {
return fir::HeapType::get(ty);
}
void fir::AllocMemOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
llvm::StringRef uniqName,
mlir::ValueRange typeparams, mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
auto nameAttr = builder.getStringAttr(uniqName);
build(builder, result, wrapAllocMemResultType(inType), inType, nameAttr, {},
typeparams, shape);
result.addAttributes(attributes);
}
void fir::AllocMemOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
llvm::StringRef uniqName, llvm::StringRef bindcName,
mlir::ValueRange typeparams, mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
auto nameAttr = builder.getStringAttr(uniqName);
auto bindcAttr = builder.getStringAttr(bindcName);
build(builder, result, wrapAllocMemResultType(inType), inType, nameAttr,
bindcAttr, typeparams, shape);
result.addAttributes(attributes);
}
void fir::AllocMemOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Type inType,
mlir::ValueRange typeparams, mlir::ValueRange shape,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
build(builder, result, wrapAllocMemResultType(inType), inType, {}, {},
typeparams, shape);
result.addAttributes(attributes);
}
mlir::ParseResult fir::AllocMemOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
return parseAllocatableOp(wrapAllocMemResultType, parser, result);
}
void fir::AllocMemOp::print(mlir::OpAsmPrinter &p) {
printAllocatableOp(p, *this);
}
llvm::LogicalResult fir::AllocMemOp::verify() {
llvm::SmallVector<llvm::StringRef> visited;
if (verifyInType(getInType(), visited, numShapeOperands()))
return emitOpError("invalid type for allocation");
if (verifyTypeParamCount(getInType(), numLenParams()))
return emitOpError("LEN params do not correspond to type");
mlir::Type outType = getType();
if (!mlir::dyn_cast<fir::HeapType>(outType))
return emitOpError("must be a !fir.heap type");
if (fir::isa_unknown_size_box(fir::dyn_cast_ptrEleTy(outType)))
return emitOpError("cannot allocate !fir.box of unknown rank or type");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// ArrayCoorOp
//===----------------------------------------------------------------------===//
// CHARACTERs and derived types with LEN PARAMETERs are dependent types that
// require runtime values to fully define the type of an object.
static bool validTypeParams(mlir::Type dynTy, mlir::ValueRange typeParams) {
dynTy = fir::unwrapAllRefAndSeqType(dynTy);
// A box value will contain type parameter values itself.
if (mlir::isa<fir::BoxType>(dynTy))
return typeParams.size() == 0;
// Derived type must have all type parameters satisfied.
if (auto recTy = mlir::dyn_cast<fir::RecordType>(dynTy))
return typeParams.size() == recTy.getNumLenParams();
// Characters with non-constant LEN must have a type parameter value.
if (auto charTy = mlir::dyn_cast<fir::CharacterType>(dynTy))
if (charTy.hasDynamicLen())
return typeParams.size() == 1;
// Otherwise, any type parameters are invalid.
return typeParams.size() == 0;
}
llvm::LogicalResult fir::ArrayCoorOp::verify() {
auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(getMemref().getType());
auto arrTy = mlir::dyn_cast<fir::SequenceType>(eleTy);
if (!arrTy)
return emitOpError("must be a reference to an array");
auto arrDim = arrTy.getDimension();
if (auto shapeOp = getShape()) {
auto shapeTy = shapeOp.getType();
unsigned shapeTyRank = 0;
if (auto s = mlir::dyn_cast<fir::ShapeType>(shapeTy)) {
shapeTyRank = s.getRank();
} else if (auto ss = mlir::dyn_cast<fir::ShapeShiftType>(shapeTy)) {
shapeTyRank = ss.getRank();
} else {
auto s = mlir::cast<fir::ShiftType>(shapeTy);
shapeTyRank = s.getRank();
// TODO: it looks like PreCGRewrite and CodeGen can support
// fir.shift with plain array reference, so we may consider
// removing this check.
if (!mlir::isa<fir::BaseBoxType>(getMemref().getType()))
return emitOpError("shift can only be provided with fir.box memref");
}
if (arrDim && arrDim != shapeTyRank)
return emitOpError("rank of dimension mismatched");
// TODO: support slicing with changing the number of dimensions,
// e.g. when array_coor represents an element access to array(:,1,:)
// slice: the shape is 3D and the number of indices is 2 in this case.
if (shapeTyRank != getIndices().size())
return emitOpError("number of indices do not match dim rank");
}
if (auto sliceOp = getSlice()) {
if (auto sl = mlir::dyn_cast_or_null<fir::SliceOp>(sliceOp.getDefiningOp()))
if (!sl.getSubstr().empty())
return emitOpError("array_coor cannot take a slice with substring");
if (auto sliceTy = mlir::dyn_cast<fir::SliceType>(sliceOp.getType()))
if (sliceTy.getRank() != arrDim)
return emitOpError("rank of dimension in slice mismatched");
}
if (!validTypeParams(getMemref().getType(), getTypeparams()))
return emitOpError("invalid type parameters");
return mlir::success();
}
// Pull in fir.embox and fir.rebox into fir.array_coor when possible.
struct SimplifyArrayCoorOp : public mlir::OpRewritePattern<fir::ArrayCoorOp> {
using mlir::OpRewritePattern<fir::ArrayCoorOp>::OpRewritePattern;
llvm::LogicalResult
matchAndRewrite(fir::ArrayCoorOp op,
mlir::PatternRewriter &rewriter) const override {
mlir::Value memref = op.getMemref();
if (!mlir::isa<fir::BaseBoxType>(memref.getType()))
return mlir::failure();
mlir::Value boxedMemref, boxedShape, boxedSlice;
if (auto emboxOp =
mlir::dyn_cast_or_null<fir::EmboxOp>(memref.getDefiningOp())) {
boxedMemref = emboxOp.getMemref();
boxedShape = emboxOp.getShape();
boxedSlice = emboxOp.getSlice();
// If any of operands, that are not currently supported for migration
// to ArrayCoorOp, is present, don't rewrite.
if (!emboxOp.getTypeparams().empty() || emboxOp.getSourceBox() ||
emboxOp.getAccessMap())
return mlir::failure();
} else if (auto reboxOp = mlir::dyn_cast_or_null<fir::ReboxOp>(
memref.getDefiningOp())) {
boxedMemref = reboxOp.getBox();
boxedShape = reboxOp.getShape();
// Avoid pulling in rebox that performs reshaping.
// There is no way to represent box reshaping with array_coor.
if (boxedShape && !mlir::isa<fir::ShiftType>(boxedShape.getType()))
return mlir::failure();
boxedSlice = reboxOp.getSlice();
} else {
return mlir::failure();
}
bool boxedShapeIsShift =
boxedShape && mlir::isa<fir::ShiftType>(boxedShape.getType());
bool boxedShapeIsShape =
boxedShape && mlir::isa<fir::ShapeType>(boxedShape.getType());
bool boxedShapeIsShapeShift =
boxedShape && mlir::isa<fir::ShapeShiftType>(boxedShape.getType());
// Slices changing the number of dimensions are not supported
// for array_coor yet.
unsigned origBoxRank;
if (mlir::isa<fir::BaseBoxType>(boxedMemref.getType()))
origBoxRank = fir::getBoxRank(boxedMemref.getType());
else if (auto arrTy = mlir::dyn_cast<fir::SequenceType>(
fir::unwrapRefType(boxedMemref.getType())))
origBoxRank = arrTy.getDimension();
else
return mlir::failure();
if (fir::getBoxRank(memref.getType()) != origBoxRank)
return mlir::failure();
// Slices with substring are not supported by array_coor.
if (boxedSlice)
if (auto sliceOp =
mlir::dyn_cast_or_null<fir::SliceOp>(boxedSlice.getDefiningOp()))
if (!sliceOp.getSubstr().empty())
return mlir::failure();
// If embox/rebox and array_coor have conflicting shapes or slices,
// do nothing.
if (op.getShape() && boxedShape && boxedShape != op.getShape())
return mlir::failure();
if (op.getSlice() && boxedSlice && boxedSlice != op.getSlice())
return mlir::failure();
std::optional<IndicesVectorTy> shiftedIndices;
// The embox/rebox and array_coor either have compatible
// shape/slice at this point or shape/slice is null
// in one of them but not in the other.
// The compatibility means they are equal or both null.
if (!op.getShape()) {
if (boxedShape) {
if (op.getSlice()) {
if (!boxedSlice) {
if (boxedShapeIsShift) {
// %0 = fir.rebox %arg(%shift)
// %1 = fir.array_coor %0 [%slice] %idx
// Both the slice indices and %idx are 1-based, so the rebox
// may be pulled in as:
// %1 = fir.array_coor %arg [%slice] %idx
boxedShape = nullptr;
} else if (boxedShapeIsShape) {
// %0 = fir.embox %arg(%shape)
// %1 = fir.array_coor %0 [%slice] %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) [%slice] %idx
} else if (boxedShapeIsShapeShift) {
// %0 = fir.embox %arg(%shapeshift)
// %1 = fir.array_coor %0 [%slice] %idx
// Pull in as:
// %shape = fir.shape <extents from the %shapeshift>
// %1 = fir.array_coor %arg(%shape) [%slice] %idx
boxedShape = getShapeFromShapeShift(boxedShape, rewriter);
if (!boxedShape)
return mlir::failure();
} else {
return mlir::failure();
}
} else {
if (boxedShapeIsShift) {
// %0 = fir.rebox %arg(%shift) [%slice]
// %1 = fir.array_coor %0 [%slice] %idx
// This FIR may only be valid if the shape specifies
// that all lower bounds are 1s and the slice's start indices
// and strides are all 1s.
// We could pull in the rebox as:
// %1 = fir.array_coor %arg [%slice] %idx
// Do not do anything for the time being.
return mlir::failure();
} else if (boxedShapeIsShape) {
// %0 = fir.embox %arg(%shape) [%slice]
// %1 = fir.array_coor %0 [%slice] %idx
// This FIR may only be valid if the slice's start indices
// and strides are all 1s.
// We could pull in the embox as:
// %1 = fir.array_coor %arg(%shape) [%slice] %idx
return mlir::failure();
} else if (boxedShapeIsShapeShift) {
// %0 = fir.embox %arg(%shapeshift) [%slice]
// %1 = fir.array_coor %0 [%slice] %idx
// This FIR may only be valid if the shape specifies
// that all lower bounds are 1s and the slice's start indices
// and strides are all 1s.
// We could pull in the embox as:
// %shape = fir.shape <extents from the %shapeshift>
// %1 = fir.array_coor %arg(%shape) [%slice] %idx
return mlir::failure();
} else {
return mlir::failure();
}
}
} else { // !op.getSlice()
if (!boxedSlice) {
if (boxedShapeIsShift) {
// %0 = fir.rebox %arg(%shift)
// %1 = fir.array_coor %0 %idx
// Pull in as:
// %1 = fir.array_coor %arg %idx
boxedShape = nullptr;
} else if (boxedShapeIsShape) {
// %0 = fir.embox %arg(%shape)
// %1 = fir.array_coor %0 %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) %idx
} else if (boxedShapeIsShapeShift) {
// %0 = fir.embox %arg(%shapeshift)
// %1 = fir.array_coor %0 %idx
// Pull in as:
// %shape = fir.shape <extents from the %shapeshift>
// %1 = fir.array_coor %arg(%shape) %idx
boxedShape = getShapeFromShapeShift(boxedShape, rewriter);
if (!boxedShape)
return mlir::failure();
} else {
return mlir::failure();
}
} else {
if (boxedShapeIsShift) {
// %0 = fir.embox %arg(%shift) [%slice]
// %1 = fir.array_coor %0 %idx
// Pull in as:
// %tmp = arith.addi %idx, %shift.origin
// %idx_shifted = arith.subi %tmp, 1
// %1 = fir.array_coor %arg(%shift) %[slice] %idx_shifted
shiftedIndices =
getShiftedIndices(boxedShape, op.getIndices(), rewriter);
if (!shiftedIndices)
return mlir::failure();
} else if (boxedShapeIsShape) {
// %0 = fir.embox %arg(%shape) [%slice]
// %1 = fir.array_coor %0 %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) %[slice] %idx
} else if (boxedShapeIsShapeShift) {
// %0 = fir.embox %arg(%shapeshift) [%slice]
// %1 = fir.array_coor %0 %idx
// Pull in as:
// %tmp = arith.addi %idx, %shapeshift.lb
// %idx_shifted = arith.subi %tmp, 1
// %1 = fir.array_coor %arg(%shapeshift) %[slice] %idx_shifted
shiftedIndices =
getShiftedIndices(boxedShape, op.getIndices(), rewriter);
if (!shiftedIndices)
return mlir::failure();
} else {
return mlir::failure();
}
}
}
} else { // !boxedShape
if (op.getSlice()) {
if (!boxedSlice) {
// %0 = fir.rebox %arg
// %1 = fir.array_coor %0 [%slice] %idx
// Pull in as:
// %1 = fir.array_coor %arg [%slice] %idx
} else {
// %0 = fir.rebox %arg [%slice]
// %1 = fir.array_coor %0 [%slice] %idx
// This is a valid FIR iff the slice's lower bounds
// and strides are all 1s.
// Pull in as:
// %1 = fir.array_coor %arg [%slice] %idx
}
} else { // !op.getSlice()
if (!boxedSlice) {
// %0 = fir.rebox %arg
// %1 = fir.array_coor %0 %idx
// Pull in as:
// %1 = fir.array_coor %arg %idx
} else {
// %0 = fir.rebox %arg [%slice]
// %1 = fir.array_coor %0 %idx
// Pull in as:
// %1 = fir.array_coor %arg [%slice] %idx
}
}
}
} else { // op.getShape()
if (boxedShape) {
// Check if pulling in non-default shape is correct.
if (op.getSlice()) {
if (!boxedSlice) {
// %0 = fir.embox %arg(%shape)
// %1 = fir.array_coor %0(%shape) [%slice] %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) [%slice] %idx
} else {
// %0 = fir.embox %arg(%shape) [%slice]
// %1 = fir.array_coor %0(%shape) [%slice] %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) [%slice] %idx
}
} else { // !op.getSlice()
if (!boxedSlice) {
// %0 = fir.embox %arg(%shape)
// %1 = fir.array_coor %0(%shape) %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) %idx
} else {
// %0 = fir.embox %arg(%shape) [%slice]
// %1 = fir.array_coor %0(%shape) %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) [%slice] %idx
}
}
} else { // !boxedShape
if (op.getSlice()) {
if (!boxedSlice) {
// %0 = fir.rebox %arg
// %1 = fir.array_coor %0(%shape) [%slice] %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) [%slice] %idx
} else {
// %0 = fir.rebox %arg [%slice]
// %1 = fir.array_coor %0(%shape) [%slice] %idx
return mlir::failure();
}
} else { // !op.getSlice()
if (!boxedSlice) {
// %0 = fir.rebox %arg
// %1 = fir.array_coor %0(%shape) %idx
// Pull in as:
// %1 = fir.array_coor %arg(%shape) %idx
} else {
// %0 = fir.rebox %arg [%slice]
// %1 = fir.array_coor %0(%shape) %idx
// Cannot pull in without adjusting the slice indices.
return mlir::failure();
}
}
}
}
// TODO: temporarily avoid producing array_coor with the shape shift
// and plain array reference (it seems to be a limitation of
// ArrayCoorOp verifier).
if (!mlir::isa<fir::BaseBoxType>(boxedMemref.getType())) {
if (boxedShape) {
if (mlir::isa<fir::ShiftType>(boxedShape.getType()))
return mlir::failure();
} else if (op.getShape() &&
mlir::isa<fir::ShiftType>(op.getShape().getType())) {
return mlir::failure();
}
}
rewriter.modifyOpInPlace(op, [&]() {
op.getMemrefMutable().assign(boxedMemref);
if (boxedShape)
op.getShapeMutable().assign(boxedShape);
if (boxedSlice)
op.getSliceMutable().assign(boxedSlice);
if (shiftedIndices)
op.getIndicesMutable().assign(*shiftedIndices);
});
return mlir::success();
}
private:
using IndicesVectorTy = std::vector<mlir::Value>;
// If v is a shape_shift operation:
// fir.shape_shift %l1, %e1, %l2, %e2, ...
// create:
// fir.shape %e1, %e2, ...
static mlir::Value getShapeFromShapeShift(mlir::Value v,
mlir::PatternRewriter &rewriter) {
auto shapeShiftOp =
mlir::dyn_cast_or_null<fir::ShapeShiftOp>(v.getDefiningOp());
if (!shapeShiftOp)
return nullptr;
mlir::OpBuilder::InsertionGuard guard(rewriter);
rewriter.setInsertionPoint(shapeShiftOp);
return rewriter.create<fir::ShapeOp>(shapeShiftOp.getLoc(),
shapeShiftOp.getExtents());
}
static std::optional<IndicesVectorTy>
getShiftedIndices(mlir::Value v, mlir::ValueRange indices,
mlir::PatternRewriter &rewriter) {
auto insertAdjustments = [&](mlir::Operation *op, mlir::ValueRange lbs) {
// Compute the shifted indices using the extended type.
// Note that this can probably result in less efficient
// MLIR and further LLVM IR due to the extra conversions.
mlir::OpBuilder::InsertPoint savedIP = rewriter.saveInsertionPoint();
rewriter.setInsertionPoint(op);
mlir::Location loc = op->getLoc();
mlir::Type idxTy = rewriter.getIndexType();
mlir::Value one = rewriter.create<mlir::arith::ConstantOp>(
loc, idxTy, rewriter.getIndexAttr(1));
rewriter.restoreInsertionPoint(savedIP);
auto nsw = mlir::arith::IntegerOverflowFlags::nsw;
IndicesVectorTy shiftedIndices;
for (auto [lb, idx] : llvm::zip(lbs, indices)) {
mlir::Value extLb = rewriter.create<fir::ConvertOp>(loc, idxTy, lb);
mlir::Value extIdx = rewriter.create<fir::ConvertOp>(loc, idxTy, idx);
mlir::Value add =
rewriter.create<mlir::arith::AddIOp>(loc, extIdx, extLb, nsw);
mlir::Value sub =
rewriter.create<mlir::arith::SubIOp>(loc, add, one, nsw);
shiftedIndices.push_back(sub);
}
return shiftedIndices;
};
if (auto shiftOp =
mlir::dyn_cast_or_null<fir::ShiftOp>(v.getDefiningOp())) {
return insertAdjustments(shiftOp.getOperation(), shiftOp.getOrigins());
} else if (auto shapeShiftOp = mlir::dyn_cast_or_null<fir::ShapeShiftOp>(
v.getDefiningOp())) {
return insertAdjustments(shapeShiftOp.getOperation(),
shapeShiftOp.getOrigins());
}
return std::nullopt;
}
};
void fir::ArrayCoorOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &patterns, mlir::MLIRContext *context) {
// TODO: !fir.shape<1> operand may be removed from array_coor always.
patterns.add<SimplifyArrayCoorOp>(context);
}
//===----------------------------------------------------------------------===//
// ArrayLoadOp
//===----------------------------------------------------------------------===//
static mlir::Type adjustedElementType(mlir::Type t) {
if (auto ty = mlir::dyn_cast<fir::ReferenceType>(t)) {
auto eleTy = ty.getEleTy();
if (fir::isa_char(eleTy))
return eleTy;
if (fir::isa_derived(eleTy))
return eleTy;
if (mlir::isa<fir::SequenceType>(eleTy))
return eleTy;
}
return t;
}
std::vector<mlir::Value> fir::ArrayLoadOp::getExtents() {
if (auto sh = getShape())
if (auto *op = sh.getDefiningOp()) {
if (auto shOp = mlir::dyn_cast<fir::ShapeOp>(op)) {
auto extents = shOp.getExtents();
return {extents.begin(), extents.end()};
}
return mlir::cast<fir::ShapeShiftOp>(op).getExtents();
}
return {};
}
llvm::LogicalResult fir::ArrayLoadOp::verify() {
auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(getMemref().getType());
auto arrTy = mlir::dyn_cast<fir::SequenceType>(eleTy);
if (!arrTy)
return emitOpError("must be a reference to an array");
auto arrDim = arrTy.getDimension();
if (auto shapeOp = getShape()) {
auto shapeTy = shapeOp.getType();
unsigned shapeTyRank = 0u;
if (auto s = mlir::dyn_cast<fir::ShapeType>(shapeTy)) {
shapeTyRank = s.getRank();
} else if (auto ss = mlir::dyn_cast<fir::ShapeShiftType>(shapeTy)) {
shapeTyRank = ss.getRank();
} else {
auto s = mlir::cast<fir::ShiftType>(shapeTy);
shapeTyRank = s.getRank();
if (!mlir::isa<fir::BaseBoxType>(getMemref().getType()))
return emitOpError("shift can only be provided with fir.box memref");
}
if (arrDim && arrDim != shapeTyRank)
return emitOpError("rank of dimension mismatched");
}
if (auto sliceOp = getSlice()) {
if (auto sl = mlir::dyn_cast_or_null<fir::SliceOp>(sliceOp.getDefiningOp()))
if (!sl.getSubstr().empty())
return emitOpError("array_load cannot take a slice with substring");
if (auto sliceTy = mlir::dyn_cast<fir::SliceType>(sliceOp.getType()))
if (sliceTy.getRank() != arrDim)
return emitOpError("rank of dimension in slice mismatched");
}
if (!validTypeParams(getMemref().getType(), getTypeparams()))
return emitOpError("invalid type parameters");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// ArrayMergeStoreOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::ArrayMergeStoreOp::verify() {
if (!mlir::isa<fir::ArrayLoadOp>(getOriginal().getDefiningOp()))
return emitOpError("operand #0 must be result of a fir.array_load op");
if (auto sl = getSlice()) {
if (auto sliceOp =
mlir::dyn_cast_or_null<fir::SliceOp>(sl.getDefiningOp())) {
if (!sliceOp.getSubstr().empty())
return emitOpError(
"array_merge_store cannot take a slice with substring");
if (!sliceOp.getFields().empty()) {
// This is an intra-object merge, where the slice is projecting the
// subfields that are to be overwritten by the merge operation.
auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(getMemref().getType());
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(eleTy)) {
auto projTy =
fir::applyPathToType(seqTy.getEleTy(), sliceOp.getFields());
if (fir::unwrapSequenceType(getOriginal().getType()) != projTy)
return emitOpError(
"type of origin does not match sliced memref type");
if (fir::unwrapSequenceType(getSequence().getType()) != projTy)
return emitOpError(
"type of sequence does not match sliced memref type");
return mlir::success();
}
return emitOpError("referenced type is not an array");
}
}
return mlir::success();
}
auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(getMemref().getType());
if (getOriginal().getType() != eleTy)
return emitOpError("type of origin does not match memref element type");
if (getSequence().getType() != eleTy)
return emitOpError("type of sequence does not match memref element type");
if (!validTypeParams(getMemref().getType(), getTypeparams()))
return emitOpError("invalid type parameters");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// ArrayFetchOp
//===----------------------------------------------------------------------===//
// Template function used for both array_fetch and array_update verification.
template <typename A>
mlir::Type validArraySubobject(A op) {
auto ty = op.getSequence().getType();
return fir::applyPathToType(ty, op.getIndices());
}
llvm::LogicalResult fir::ArrayFetchOp::verify() {
auto arrTy = mlir::cast<fir::SequenceType>(getSequence().getType());
auto indSize = getIndices().size();
if (indSize < arrTy.getDimension())
return emitOpError("number of indices != dimension of array");
if (indSize == arrTy.getDimension() &&
::adjustedElementType(getElement().getType()) != arrTy.getEleTy())
return emitOpError("return type does not match array");
auto ty = validArraySubobject(*this);
if (!ty || ty != ::adjustedElementType(getType()))
return emitOpError("return type and/or indices do not type check");
if (!mlir::isa<fir::ArrayLoadOp>(getSequence().getDefiningOp()))
return emitOpError("argument #0 must be result of fir.array_load");
if (!validTypeParams(arrTy, getTypeparams()))
return emitOpError("invalid type parameters");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// ArrayAccessOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::ArrayAccessOp::verify() {
auto arrTy = mlir::cast<fir::SequenceType>(getSequence().getType());
std::size_t indSize = getIndices().size();
if (indSize < arrTy.getDimension())
return emitOpError("number of indices != dimension of array");
if (indSize == arrTy.getDimension() &&
getElement().getType() != fir::ReferenceType::get(arrTy.getEleTy()))
return emitOpError("return type does not match array");
mlir::Type ty = validArraySubobject(*this);
if (!ty || fir::ReferenceType::get(ty) != getType())
return emitOpError("return type and/or indices do not type check");
if (!validTypeParams(arrTy, getTypeparams()))
return emitOpError("invalid type parameters");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// ArrayUpdateOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::ArrayUpdateOp::verify() {
if (fir::isa_ref_type(getMerge().getType()))
return emitOpError("does not support reference type for merge");
auto arrTy = mlir::cast<fir::SequenceType>(getSequence().getType());
auto indSize = getIndices().size();
if (indSize < arrTy.getDimension())
return emitOpError("number of indices != dimension of array");
if (indSize == arrTy.getDimension() &&
::adjustedElementType(getMerge().getType()) != arrTy.getEleTy())
return emitOpError("merged value does not have element type");
auto ty = validArraySubobject(*this);
if (!ty || ty != ::adjustedElementType(getMerge().getType()))
return emitOpError("merged value and/or indices do not type check");
if (!validTypeParams(arrTy, getTypeparams()))
return emitOpError("invalid type parameters");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// ArrayModifyOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::ArrayModifyOp::verify() {
auto arrTy = mlir::cast<fir::SequenceType>(getSequence().getType());
auto indSize = getIndices().size();
if (indSize < arrTy.getDimension())
return emitOpError("number of indices must match array dimension");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// BoxAddrOp
//===----------------------------------------------------------------------===//
void fir::BoxAddrOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Value val) {
mlir::Type type =
llvm::TypeSwitch<mlir::Type, mlir::Type>(val.getType())
.Case<fir::BaseBoxType>([&](fir::BaseBoxType ty) -> mlir::Type {
mlir::Type eleTy = ty.getEleTy();
if (fir::isa_ref_type(eleTy))
return eleTy;
return fir::ReferenceType::get(eleTy);
})
.Case<fir::BoxCharType>([&](fir::BoxCharType ty) -> mlir::Type {
return fir::ReferenceType::get(ty.getEleTy());
})
.Case<fir::BoxProcType>(
[&](fir::BoxProcType ty) { return ty.getEleTy(); })
.Default([&](const auto &) { return mlir::Type{}; });
assert(type && "bad val type");
build(builder, result, type, val);
}
mlir::OpFoldResult fir::BoxAddrOp::fold(FoldAdaptor adaptor) {
if (auto *v = getVal().getDefiningOp()) {
if (auto box = mlir::dyn_cast<fir::EmboxOp>(v)) {
// Fold only if not sliced
if (!box.getSlice() && box.getMemref().getType() == getType()) {
propagateAttributes(getOperation(), box.getMemref().getDefiningOp());
return box.getMemref();
}
}
if (auto box = mlir::dyn_cast<fir::EmboxCharOp>(v))
if (box.getMemref().getType() == getType())
return box.getMemref();
}
return {};
}
//===----------------------------------------------------------------------===//
// BoxCharLenOp
//===----------------------------------------------------------------------===//
mlir::OpFoldResult fir::BoxCharLenOp::fold(FoldAdaptor adaptor) {
if (auto v = getVal().getDefiningOp()) {
if (auto box = mlir::dyn_cast<fir::EmboxCharOp>(v))
return box.getLen();
}
return {};
}
//===----------------------------------------------------------------------===//
// BoxDimsOp
//===----------------------------------------------------------------------===//
/// Get the result types packed in a tuple tuple
mlir::Type fir::BoxDimsOp::getTupleType() {
// note: triple, but 4 is nearest power of 2
llvm::SmallVector<mlir::Type> triple{
getResult(0).getType(), getResult(1).getType(), getResult(2).getType()};
return mlir::TupleType::get(getContext(), triple);
}
//===----------------------------------------------------------------------===//
// BoxRankOp
//===----------------------------------------------------------------------===//
void fir::BoxRankOp::getEffects(
llvm::SmallVectorImpl<
mlir::SideEffects::EffectInstance<mlir::MemoryEffects::Effect>>
&effects) {
mlir::OpOperand &inputBox = getBoxMutable();
if (fir::isBoxAddress(inputBox.get().getType()))
effects.emplace_back(mlir::MemoryEffects::Read::get(), &inputBox,
mlir::SideEffects::DefaultResource::get());
}
//===----------------------------------------------------------------------===//
// CallOp
//===----------------------------------------------------------------------===//
mlir::FunctionType fir::CallOp::getFunctionType() {
return mlir::FunctionType::get(getContext(), getOperandTypes(),
getResultTypes());
}
void fir::CallOp::print(mlir::OpAsmPrinter &p) {
bool isDirect = getCallee().has_value();
p << ' ';
if (isDirect)
p << *getCallee();
else
p << getOperand(0);
p << '(' << (*this)->getOperands().drop_front(isDirect ? 0 : 1) << ')';
// Print `proc_attrs<...>`, if present.
fir::FortranProcedureFlagsEnumAttr procAttrs = getProcedureAttrsAttr();
if (procAttrs &&
procAttrs.getValue() != fir::FortranProcedureFlagsEnum::none) {
p << ' ' << fir::FortranProcedureFlagsEnumAttr::getMnemonic();
p.printStrippedAttrOrType(procAttrs);
}
// Print 'fastmath<...>' (if it has non-default value) before
// any other attributes.
mlir::arith::FastMathFlagsAttr fmfAttr = getFastmathAttr();
if (fmfAttr.getValue() != mlir::arith::FastMathFlags::none) {
p << ' ' << mlir::arith::FastMathFlagsAttr::getMnemonic();
p.printStrippedAttrOrType(fmfAttr);
}
p.printOptionalAttrDict((*this)->getAttrs(),
{fir::CallOp::getCalleeAttrNameStr(),
getFastmathAttrName(), getProcedureAttrsAttrName()});
auto resultTypes{getResultTypes()};
llvm::SmallVector<mlir::Type> argTypes(
llvm::drop_begin(getOperandTypes(), isDirect ? 0 : 1));
p << " : " << mlir::FunctionType::get(getContext(), argTypes, resultTypes);
}
mlir::ParseResult fir::CallOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands;
if (parser.parseOperandList(operands))
return mlir::failure();
mlir::NamedAttrList attrs;
mlir::SymbolRefAttr funcAttr;
bool isDirect = operands.empty();
if (isDirect)
if (parser.parseAttribute(funcAttr, fir::CallOp::getCalleeAttrNameStr(),
attrs))
return mlir::failure();
mlir::Type type;
if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::Paren))
return mlir::failure();
// Parse `proc_attrs<...>`, if present.
fir::FortranProcedureFlagsEnumAttr procAttr;
if (mlir::succeeded(parser.parseOptionalKeyword(
fir::FortranProcedureFlagsEnumAttr::getMnemonic())))
if (parser.parseCustomAttributeWithFallback(
procAttr, mlir::Type{}, getProcedureAttrsAttrName(result.name),
attrs))
return mlir::failure();
// Parse 'fastmath<...>', if present.
mlir::arith::FastMathFlagsAttr fmfAttr;
llvm::StringRef fmfAttrName = getFastmathAttrName(result.name);
if (mlir::succeeded(parser.parseOptionalKeyword(fmfAttrName)))
if (parser.parseCustomAttributeWithFallback(fmfAttr, mlir::Type{},
fmfAttrName, attrs))
return mlir::failure();
if (parser.parseOptionalAttrDict(attrs) || parser.parseColon() ||
parser.parseType(type))
return mlir::failure();
auto funcType = mlir::dyn_cast<mlir::FunctionType>(type);
if (!funcType)
return parser.emitError(parser.getNameLoc(), "expected function type");
if (isDirect) {
if (parser.resolveOperands(operands, funcType.getInputs(),
parser.getNameLoc(), result.operands))
return mlir::failure();
} else {
auto funcArgs =
llvm::ArrayRef<mlir::OpAsmParser::UnresolvedOperand>(operands)
.drop_front();
if (parser.resolveOperand(operands[0], funcType, result.operands) ||
parser.resolveOperands(funcArgs, funcType.getInputs(),
parser.getNameLoc(), result.operands))
return mlir::failure();
}
result.addTypes(funcType.getResults());
result.attributes = attrs;
return mlir::success();
}
void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::func::FuncOp callee, mlir::ValueRange operands) {
result.addOperands(operands);
result.addAttribute(getCalleeAttrNameStr(), mlir::SymbolRefAttr::get(callee));
result.addTypes(callee.getFunctionType().getResults());
}
void fir::CallOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::SymbolRefAttr callee,
llvm::ArrayRef<mlir::Type> results,
mlir::ValueRange operands) {
result.addOperands(operands);
if (callee)
result.addAttribute(getCalleeAttrNameStr(), callee);
result.addTypes(results);
}
//===----------------------------------------------------------------------===//
// CharConvertOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::CharConvertOp::verify() {
auto unwrap = [&](mlir::Type t) {
t = fir::unwrapSequenceType(fir::dyn_cast_ptrEleTy(t));
return mlir::dyn_cast<fir::CharacterType>(t);
};
auto inTy = unwrap(getFrom().getType());
auto outTy = unwrap(getTo().getType());
if (!(inTy && outTy))
return emitOpError("not a reference to a character");
if (inTy.getFKind() == outTy.getFKind())
return emitOpError("buffers must have different KIND values");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// CmpOp
//===----------------------------------------------------------------------===//
template <typename OPTY>
static void printCmpOp(mlir::OpAsmPrinter &p, OPTY op) {
p << ' ';
auto predSym = mlir::arith::symbolizeCmpFPredicate(
op->template getAttrOfType<mlir::IntegerAttr>(
OPTY::getPredicateAttrName())
.getInt());
assert(predSym.has_value() && "invalid symbol value for predicate");
p << '"' << mlir::arith::stringifyCmpFPredicate(predSym.value()) << '"'
<< ", ";
p.printOperand(op.getLhs());
p << ", ";
p.printOperand(op.getRhs());
p.printOptionalAttrDict(op->getAttrs(),
/*elidedAttrs=*/{OPTY::getPredicateAttrName()});
p << " : " << op.getLhs().getType();
}
template <typename OPTY>
static mlir::ParseResult parseCmpOp(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> ops;
mlir::NamedAttrList attrs;
mlir::Attribute predicateNameAttr;
mlir::Type type;
if (parser.parseAttribute(predicateNameAttr, OPTY::getPredicateAttrName(),
attrs) ||
parser.parseComma() || parser.parseOperandList(ops, 2) ||
parser.parseOptionalAttrDict(attrs) || parser.parseColonType(type) ||
parser.resolveOperands(ops, type, result.operands))
return mlir::failure();
if (!mlir::isa<mlir::StringAttr>(predicateNameAttr))
return parser.emitError(parser.getNameLoc(),
"expected string comparison predicate attribute");
// Rewrite string attribute to an enum value.
llvm::StringRef predicateName =
mlir::cast<mlir::StringAttr>(predicateNameAttr).getValue();
auto predicate = fir::CmpcOp::getPredicateByName(predicateName);
auto builder = parser.getBuilder();
mlir::Type i1Type = builder.getI1Type();
attrs.set(OPTY::getPredicateAttrName(),
builder.getI64IntegerAttr(static_cast<std::int64_t>(predicate)));
result.attributes = attrs;
result.addTypes({i1Type});
return mlir::success();
}
//===----------------------------------------------------------------------===//
// CmpcOp
//===----------------------------------------------------------------------===//
void fir::buildCmpCOp(mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::arith::CmpFPredicate predicate, mlir::Value lhs,
mlir::Value rhs) {
result.addOperands({lhs, rhs});
result.types.push_back(builder.getI1Type());
result.addAttribute(
fir::CmpcOp::getPredicateAttrName(),
builder.getI64IntegerAttr(static_cast<std::int64_t>(predicate)));
}
mlir::arith::CmpFPredicate
fir::CmpcOp::getPredicateByName(llvm::StringRef name) {
auto pred = mlir::arith::symbolizeCmpFPredicate(name);
assert(pred.has_value() && "invalid predicate name");
return pred.value();
}
void fir::CmpcOp::print(mlir::OpAsmPrinter &p) { printCmpOp(p, *this); }
mlir::ParseResult fir::CmpcOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
return parseCmpOp<fir::CmpcOp>(parser, result);
}
//===----------------------------------------------------------------------===//
// ConvertOp
//===----------------------------------------------------------------------===//
void fir::ConvertOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
results.insert<ConvertConvertOptPattern, ConvertAscendingIndexOptPattern,
ConvertDescendingIndexOptPattern, RedundantConvertOptPattern,
CombineConvertOptPattern, CombineConvertTruncOptPattern,
ForwardConstantConvertPattern, ChainedPointerConvertsPattern>(
context);
}
mlir::OpFoldResult fir::ConvertOp::fold(FoldAdaptor adaptor) {
if (getValue().getType() == getType())
return getValue();
if (matchPattern(getValue(), mlir::m_Op<fir::ConvertOp>())) {
auto inner = mlir::cast<fir::ConvertOp>(getValue().getDefiningOp());
// (convert (convert 'a : logical -> i1) : i1 -> logical) ==> forward 'a
if (auto toTy = mlir::dyn_cast<fir::LogicalType>(getType()))
if (auto fromTy =
mlir::dyn_cast<fir::LogicalType>(inner.getValue().getType()))
if (mlir::isa<mlir::IntegerType>(inner.getType()) && (toTy == fromTy))
return inner.getValue();
// (convert (convert 'a : i1 -> logical) : logical -> i1) ==> forward 'a
if (auto toTy = mlir::dyn_cast<mlir::IntegerType>(getType()))
if (auto fromTy =
mlir::dyn_cast<mlir::IntegerType>(inner.getValue().getType()))
if (mlir::isa<fir::LogicalType>(inner.getType()) && (toTy == fromTy) &&
(fromTy.getWidth() == 1))
return inner.getValue();
}
return {};
}
bool fir::ConvertOp::isInteger(mlir::Type ty) {
return mlir::isa<mlir::IntegerType, mlir::IndexType, fir::IntegerType>(ty);
}
bool fir::ConvertOp::isIntegerCompatible(mlir::Type ty) {
return isInteger(ty) || mlir::isa<fir::LogicalType>(ty);
}
bool fir::ConvertOp::isFloatCompatible(mlir::Type ty) {
return mlir::isa<mlir::FloatType>(ty);
}
bool fir::ConvertOp::isPointerCompatible(mlir::Type ty) {
return mlir::isa<fir::ReferenceType, fir::PointerType, fir::HeapType,
fir::LLVMPointerType, mlir::MemRefType, mlir::FunctionType,
fir::TypeDescType, mlir::LLVM::LLVMPointerType>(ty);
}
static std::optional<mlir::Type> getVectorElementType(mlir::Type ty) {
mlir::Type elemTy;
if (mlir::isa<fir::VectorType>(ty))
elemTy = mlir::dyn_cast<fir::VectorType>(ty).getElementType();
else if (mlir::isa<mlir::VectorType>(ty))
elemTy = mlir::dyn_cast<mlir::VectorType>(ty).getElementType();
else
return std::nullopt;
// e.g. fir.vector<4:ui32> => mlir.vector<4xi32>
// e.g. mlir.vector<4xui32> => mlir.vector<4xi32>
if (elemTy.isUnsignedInteger()) {
elemTy = mlir::IntegerType::get(
ty.getContext(), mlir::dyn_cast<mlir::IntegerType>(elemTy).getWidth());
}
return elemTy;
}
static std::optional<uint64_t> getVectorLen(mlir::Type ty) {
if (mlir::isa<fir::VectorType>(ty))
return mlir::dyn_cast<fir::VectorType>(ty).getLen();
else if (mlir::isa<mlir::VectorType>(ty)) {
// fir.vector only supports 1-D vector
if (!(mlir::dyn_cast<mlir::VectorType>(ty).isScalable()))
return mlir::dyn_cast<mlir::VectorType>(ty).getShape()[0];
}
return std::nullopt;
}
bool fir::ConvertOp::areVectorsCompatible(mlir::Type inTy, mlir::Type outTy) {
if (!(mlir::isa<fir::VectorType>(inTy) &&
mlir::isa<mlir::VectorType>(outTy)) &&
!(mlir::isa<mlir::VectorType>(inTy) && mlir::isa<fir::VectorType>(outTy)))
return false;
// Only support integer, unsigned and real vector
// Both vectors must have the same element type
std::optional<mlir::Type> inElemTy = getVectorElementType(inTy);
std::optional<mlir::Type> outElemTy = getVectorElementType(outTy);
if (!inElemTy.has_value() || !outElemTy.has_value() ||
inElemTy.value() != outElemTy.value())
return false;
// Both vectors must have the same number of elements
std::optional<uint64_t> inLen = getVectorLen(inTy);
std::optional<uint64_t> outLen = getVectorLen(outTy);
if (!inLen.has_value() || !outLen.has_value() ||
inLen.value() != outLen.value())
return false;
return true;
}
static bool areRecordsCompatible(mlir::Type inTy, mlir::Type outTy) {
// Both records must have the same field types.
// Trust frontend semantics for in-depth checks, such as if both records
// have the BIND(C) attribute.
auto inRecTy = mlir::dyn_cast<fir::RecordType>(inTy);
auto outRecTy = mlir::dyn_cast<fir::RecordType>(outTy);
return inRecTy && outRecTy && inRecTy.getTypeList() == outRecTy.getTypeList();
}
bool fir::ConvertOp::canBeConverted(mlir::Type inType, mlir::Type outType) {
if (inType == outType)
return true;
return (isPointerCompatible(inType) && isPointerCompatible(outType)) ||
(isIntegerCompatible(inType) && isIntegerCompatible(outType)) ||
(isInteger(inType) && isFloatCompatible(outType)) ||
(isFloatCompatible(inType) && isInteger(outType)) ||
(isFloatCompatible(inType) && isFloatCompatible(outType)) ||
(isIntegerCompatible(inType) && isPointerCompatible(outType)) ||
(isPointerCompatible(inType) && isIntegerCompatible(outType)) ||
(mlir::isa<fir::BoxType>(inType) &&
mlir::isa<fir::BoxType>(outType)) ||
(mlir::isa<fir::BoxProcType>(inType) &&
mlir::isa<fir::BoxProcType>(outType)) ||
(fir::isa_complex(inType) && fir::isa_complex(outType)) ||
(fir::isBoxedRecordType(inType) && fir::isPolymorphicType(outType)) ||
(fir::isPolymorphicType(inType) && fir::isPolymorphicType(outType)) ||
(fir::isPolymorphicType(inType) && mlir::isa<BoxType>(outType)) ||
areVectorsCompatible(inType, outType) ||
areRecordsCompatible(inType, outType);
}
llvm::LogicalResult fir::ConvertOp::verify() {
if (canBeConverted(getValue().getType(), getType()))
return mlir::success();
return emitOpError("invalid type conversion")
<< getValue().getType() << " / " << getType();
}
//===----------------------------------------------------------------------===//
// CoordinateOp
//===----------------------------------------------------------------------===//
void fir::CoordinateOp::print(mlir::OpAsmPrinter &p) {
p << ' ' << getRef() << ", " << getCoor();
p.printOptionalAttrDict((*this)->getAttrs(), /*elideAttrs=*/{"baseType"});
p << " : ";
p.printFunctionalType(getOperandTypes(), (*this)->getResultTypes());
}
mlir::ParseResult fir::CoordinateOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
mlir::OpAsmParser::UnresolvedOperand memref;
if (parser.parseOperand(memref) || parser.parseComma())
return mlir::failure();
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> coorOperands;
if (parser.parseOperandList(coorOperands))
return mlir::failure();
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> allOperands;
allOperands.push_back(memref);
allOperands.append(coorOperands.begin(), coorOperands.end());
mlir::FunctionType funcTy;
auto loc = parser.getCurrentLocation();
if (parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType(funcTy) ||
parser.resolveOperands(allOperands, funcTy.getInputs(), loc,
result.operands) ||
parser.addTypesToList(funcTy.getResults(), result.types))
return mlir::failure();
result.addAttribute("baseType", mlir::TypeAttr::get(funcTy.getInput(0)));
return mlir::success();
}
llvm::LogicalResult fir::CoordinateOp::verify() {
const mlir::Type refTy = getRef().getType();
if (fir::isa_ref_type(refTy)) {
auto eleTy = fir::dyn_cast_ptrEleTy(refTy);
if (auto arrTy = mlir::dyn_cast<fir::SequenceType>(eleTy)) {
if (arrTy.hasUnknownShape())
return emitOpError("cannot find coordinate in unknown shape");
if (arrTy.getConstantRows() < arrTy.getDimension() - 1)
return emitOpError("cannot find coordinate with unknown extents");
}
if (!(fir::isa_aggregate(eleTy) || fir::isa_complex(eleTy) ||
fir::isa_char_string(eleTy)))
return emitOpError("cannot apply to this element type");
}
auto eleTy = fir::dyn_cast_ptrOrBoxEleTy(refTy);
unsigned dimension = 0;
const unsigned numCoors = getCoor().size();
for (auto coorOperand : llvm::enumerate(getCoor())) {
auto co = coorOperand.value();
if (dimension == 0 && mlir::isa<fir::SequenceType>(eleTy)) {
dimension = mlir::cast<fir::SequenceType>(eleTy).getDimension();
if (dimension == 0)
return emitOpError("cannot apply to array of unknown rank");
}
if (auto *defOp = co.getDefiningOp()) {
if (auto index = mlir::dyn_cast<fir::LenParamIndexOp>(defOp)) {
// Recovering a LEN type parameter only makes sense from a boxed
// value. For a bare reference, the LEN type parameters must be
// passed as additional arguments to `index`.
if (mlir::isa<fir::BoxType>(refTy)) {
if (coorOperand.index() != numCoors - 1)
return emitOpError("len_param_index must be last argument");
if (getNumOperands() != 2)
return emitOpError("too many operands for len_param_index case");
}
if (eleTy != index.getOnType())
emitOpError(
"len_param_index type not compatible with reference type");
return mlir::success();
} else if (auto index = mlir::dyn_cast<fir::FieldIndexOp>(defOp)) {
if (eleTy != index.getOnType())
emitOpError("field_index type not compatible with reference type");
if (auto recTy = mlir::dyn_cast<fir::RecordType>(eleTy)) {
eleTy = recTy.getType(index.getFieldName());
continue;
}
return emitOpError("field_index not applied to !fir.type");
}
}
if (dimension) {
if (--dimension == 0)
eleTy = mlir::cast<fir::SequenceType>(eleTy).getElementType();
} else {
if (auto t = mlir::dyn_cast<mlir::TupleType>(eleTy)) {
// FIXME: Generally, we don't know which field of the tuple is being
// referred to unless the operand is a constant. Just assume everything
// is good in the tuple case for now.
return mlir::success();
} else if (auto t = mlir::dyn_cast<fir::RecordType>(eleTy)) {
// FIXME: This is the same as the tuple case.
return mlir::success();
} else if (auto t = mlir::dyn_cast<mlir::ComplexType>(eleTy)) {
eleTy = t.getElementType();
} else if (auto t = mlir::dyn_cast<fir::CharacterType>(eleTy)) {
if (t.getLen() == fir::CharacterType::singleton())
return emitOpError("cannot apply to character singleton");
eleTy = fir::CharacterType::getSingleton(t.getContext(), t.getFKind());
if (fir::unwrapRefType(getType()) != eleTy)
return emitOpError("character type mismatch");
} else {
return emitOpError("invalid parameters (too many)");
}
}
}
return mlir::success();
}
//===----------------------------------------------------------------------===//
// DispatchOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::DispatchOp::verify() {
// Check that pass_arg_pos is in range of actual operands. pass_arg_pos is
// unsigned so check for less than zero is not needed.
if (getPassArgPos() && *getPassArgPos() > (getArgOperands().size() - 1))
return emitOpError(
"pass_arg_pos must be smaller than the number of operands");
// Operand pointed by pass_arg_pos must have polymorphic type.
if (getPassArgPos() &&
!fir::isPolymorphicType(getArgOperands()[*getPassArgPos()].getType()))
return emitOpError("pass_arg_pos must be a polymorphic operand");
return mlir::success();
}
mlir::FunctionType fir::DispatchOp::getFunctionType() {
return mlir::FunctionType::get(getContext(), getOperandTypes(),
getResultTypes());
}
//===----------------------------------------------------------------------===//
// TypeInfoOp
//===----------------------------------------------------------------------===//
void fir::TypeInfoOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, fir::RecordType type,
fir::RecordType parentType,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
result.addRegion();
result.addRegion();
result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
builder.getStringAttr(type.getName()));
result.addAttribute(getTypeAttrName(result.name), mlir::TypeAttr::get(type));
if (parentType)
result.addAttribute(getParentTypeAttrName(result.name),
mlir::TypeAttr::get(parentType));
result.addAttributes(attrs);
}
llvm::LogicalResult fir::TypeInfoOp::verify() {
if (!getDispatchTable().empty())
for (auto &op : getDispatchTable().front().without_terminator())
if (!mlir::isa<fir::DTEntryOp>(op))
return op.emitOpError("dispatch table must contain dt_entry");
if (!mlir::isa<fir::RecordType>(getType()))
return emitOpError("type must be a fir.type");
if (getParentType() && !mlir::isa<fir::RecordType>(*getParentType()))
return emitOpError("parent_type must be a fir.type");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// EmboxOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::EmboxOp::verify() {
auto eleTy = fir::dyn_cast_ptrEleTy(getMemref().getType());
bool isArray = false;
if (auto seqTy = mlir::dyn_cast<fir::SequenceType>(eleTy)) {
eleTy = seqTy.getEleTy();
isArray = true;
}
if (hasLenParams()) {
auto lenPs = numLenParams();
if (auto rt = mlir::dyn_cast<fir::RecordType>(eleTy)) {
if (lenPs != rt.getNumLenParams())
return emitOpError("number of LEN params does not correspond"
" to the !fir.type type");
} else if (auto strTy = mlir::dyn_cast<fir::CharacterType>(eleTy)) {
if (strTy.getLen() != fir::CharacterType::unknownLen())
return emitOpError("CHARACTER already has static LEN");
} else {
return emitOpError("LEN parameters require CHARACTER or derived type");
}
for (auto lp : getTypeparams())
if (!fir::isa_integer(lp.getType()))
return emitOpError("LEN parameters must be integral type");
}
if (getShape() && !isArray)
return emitOpError("shape must not be provided for a scalar");
if (getSlice() && !isArray)
return emitOpError("slice must not be provided for a scalar");
if (getSourceBox() && !mlir::isa<fir::ClassType>(getResult().getType()))
return emitOpError("source_box must be used with fir.class result type");
return mlir::success();
}
//===----------------------------------------------------------------------===//
// EmboxCharOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::EmboxCharOp::verify() {
auto eleTy = fir::dyn_cast_ptrEleTy(getMemref().getType());
if (!mlir::dyn_cast_or_null<fir::CharacterType>(eleTy))
return mlir::failure();
return mlir::success();
}
//===----------------------------------------------------------------------===//
// EmboxProcOp
//===----------------------------------------------------------------------===//
llvm::LogicalResult fir::EmboxProcOp::verify() {
// host bindings (optional) must be a reference to a tuple
if (auto h = getHost()) {
if (auto r = mlir::dyn_cast<fir::ReferenceType>(h.getType()))
if (mlir::isa<mlir::TupleType>(r.getEleTy()))
return mlir::success();
return mlir::failure();
}
return mlir::success();
}
//===----------------------------------------------------------------------===//
// TypeDescOp
//===----------------------------------------------------------------------===//
void fir::TypeDescOp::build(mlir::OpBuilder &, mlir::OperationState &result,
mlir::TypeAttr inty) {
result.addAttribute("in_type", inty);
result.addTypes(TypeDescType::get(inty.getValue()));
}
mlir::ParseResult fir::TypeDescOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
mlir::Type intype;
if (parser.parseType(intype))
return mlir::failure();
result.addAttribute("in_type", mlir::TypeAttr::get(intype));
mlir::Type restype = fir::TypeDescType::get(intype);
if (parser.addTypeToList(restype, result.types))
return mlir::failure();
return mlir::success();
}
void fir::TypeDescOp::print(mlir::OpAsmPrinter &p) {
p << ' ' << getOperation()->getAttr("in_type");
p.printOptionalAttrDict(getOperation()->getAttrs(), {"in_type"});
}
llvm::LogicalResult fir::TypeDescOp::verify() {
mlir::Type resultTy = getType();
if (auto tdesc = mlir::dyn_cast<fir::TypeDescType>(resultTy)) {
if (tdesc.getOfTy() != getInType())
return emitOpError("wrapped type mismatched");
return mlir::success();
}
return emitOpError("must be !fir.tdesc type");
}
//===----------------------------------------------------------------------===//
// GlobalOp
//===----------------------------------------------------------------------===//
mlir::Type fir::GlobalOp::resultType() {
return wrapAllocaResultType(getType());
}
mlir::ParseResult fir::GlobalOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
// Parse the optional linkage
llvm::StringRef linkage;
auto &builder = parser.getBuilder();
if (mlir::succeeded(parser.parseOptionalKeyword(&linkage))) {
if (fir::GlobalOp::verifyValidLinkage(linkage))
return mlir::failure();
mlir::StringAttr linkAttr = builder.getStringAttr(linkage);
result.addAttribute(fir::GlobalOp::getLinkNameAttrName(result.name),
linkAttr);
}
// Parse the name as a symbol reference attribute.
mlir::SymbolRefAttr nameAttr;
if (parser.parseAttribute(nameAttr,
fir::GlobalOp::getSymrefAttrName(result.name),
result.attributes))
return mlir::failure();
result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
nameAttr.getRootReference());
bool simpleInitializer = false;
if (mlir::succeeded(parser.parseOptionalLParen())) {
mlir::Attribute attr;
if (parser.parseAttribute(attr, getInitValAttrName(result.name),
result.attributes) ||
parser.parseRParen())
return mlir::failure();
simpleInitializer = true;
}
if (parser.parseOptionalAttrDict(result.attributes))
return mlir::failure();
if (succeeded(
parser.parseOptionalKeyword(getConstantAttrName(result.name)))) {
// if "constant" keyword then mark this as a constant, not a variable
result.addAttribute(getConstantAttrName(result.name),
builder.getUnitAttr());
}
if (succeeded(parser.parseOptionalKeyword(getTargetAttrName(result.name))))
result.addAttribute(getTargetAttrName(result.name), builder.getUnitAttr());
mlir::Type globalType;
if (parser.parseColonType(globalType))
return mlir::failure();
result.addAttribute(fir::GlobalOp::getTypeAttrName(result.name),
mlir::TypeAttr::get(globalType));
if (simpleInitializer) {
result.addRegion();
} else {
// Parse the optional initializer body.
auto parseResult =
parser.parseOptionalRegion(*result.addRegion(), /*arguments=*/{});
if (parseResult.has_value() && mlir::failed(*parseResult))
return mlir::failure();
}
return mlir::success();
}
void fir::GlobalOp::print(mlir::OpAsmPrinter &p) {
if (getLinkName())
p << ' ' << *getLinkName();
p << ' ';
p.printAttributeWithoutType(getSymrefAttr());
if (auto val = getValueOrNull())
p << '(' << val << ')';
// Print all other attributes that are not pretty printed here.
p.printOptionalAttrDict((*this)->getAttrs(), /*elideAttrs=*/{
getSymNameAttrName(), getSymrefAttrName(),
getTypeAttrName(), getConstantAttrName(),
getTargetAttrName(), getLinkNameAttrName(),
getInitValAttrName()});
if (getOperation()->getAttr(getConstantAttrName()))
p << " " << getConstantAttrName().strref();
if (getOperation()->getAttr(getTargetAttrName()))
p << " " << getTargetAttrName().strref();
p << " : ";
p.printType(getType());
if (hasInitializationBody()) {
p << ' ';
p.printRegion(getOperation()->getRegion(0),
/*printEntryBlockArgs=*/false,
/*printBlockTerminators=*/true);
}
}
void fir::GlobalOp::appendInitialValue(mlir::Operation *op) {
getBlock().getOperations().push_back(op);
}
void fir::GlobalOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, llvm::StringRef name,
bool isConstant, bool isTarget, mlir::Type type,
mlir::Attribute initialVal, mlir::StringAttr linkage,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
result.addRegion();
result.addAttribute(getTypeAttrName(result.name), mlir::TypeAttr::get(type));
result.addAttribute(mlir::SymbolTable::getSymbolAttrName(),
builder.getStringAttr(name));
result.addAttribute(getSymrefAttrName(result.name),
mlir::SymbolRefAttr::get(builder.getContext(), name));
if (isConstant)
result.addAttribute(getConstantAttrName(result.name),
builder.getUnitAttr());
if (isTarget)
result.addAttribute(getTargetAttrName(result.name), builder.getUnitAttr());
if (initialVal)
result.addAttribute(getInitValAttrName(result.name), initialVal);
if (linkage)
result.addAttribute(getLinkNameAttrName(result.name), linkage);
result.attributes.append(attrs.begin(), attrs.end());
}
void fir::GlobalOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, llvm::StringRef name,
mlir::Type type, mlir::Attribute initialVal,
mlir::StringAttr linkage,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
build(builder, result, name, /*isConstant=*/false, /*isTarget=*/false, type,
{}, linkage, attrs);
}
void fir::GlobalOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, llvm::StringRef name,
bool isConstant, bool isTarget, mlir::Type type,
mlir::StringAttr linkage,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
build(builder, result, name, isConstant, isTarget, type, {}, linkage, attrs);
}
void fir::GlobalOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, llvm::StringRef name,
mlir::Type type, mlir::StringAttr linkage,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
build(builder, result, name, /*isConstant=*/false, /*isTarget=*/false, type,
{}, linkage, attrs);
}
void fir::GlobalOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, llvm::StringRef name,
bool isConstant, bool isTarget, mlir::Type type,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
build(builder, result, name, isConstant, isTarget, type, mlir::StringAttr{},
attrs);
}
void fir::GlobalOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, llvm::StringRef name,
mlir::Type type,
llvm::ArrayRef<mlir::NamedAttribute> attrs) {
build(builder, result, name, /*isConstant=*/false, /*isTarget=*/false, type,
attrs);
}
mlir::ParseResult fir::GlobalOp::verifyValidLinkage(llvm::StringRef linkage) {
// Supporting only a subset of the LLVM linkage types for now
static const char *validNames[] = {"common", "internal", "linkonce",
"linkonce_odr", "weak"};
return mlir::success(llvm::is_contained(validNames, linkage));
}
//===----------------------------------------------------------------------===//
// GlobalLenOp
//===----------------------------------------------------------------------===//
mlir::ParseResult fir::GlobalLenOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::StringRef fieldName;
if (failed(parser.parseOptionalKeyword(&fieldName))) {
mlir::StringAttr fieldAttr;
if (parser.parseAttribute(fieldAttr,
fir::GlobalLenOp::getLenParamAttrName(),
result.attributes))
return mlir::failure();
} else {
result.addAttribute(fir::GlobalLenOp::getLenParamAttrName(),
parser.getBuilder().getStringAttr(fieldName));
}
mlir::IntegerAttr constant;
if (parser.parseComma() ||
parser.parseAttribute(constant, fir::GlobalLenOp::getIntAttrName(),
result.attributes))
return mlir::failure();
return mlir::success();
}
void fir::GlobalLenOp::print(mlir::OpAsmPrinter &p) {
p << ' ' << getOperation()->getAttr(fir::GlobalLenOp::getLenParamAttrName())
<< ", " << getOperation()->getAttr(fir::GlobalLenOp::getIntAttrName());
}
//===----------------------------------------------------------------------===//
// FieldIndexOp
//===----------------------------------------------------------------------===//
template <typename TY>
mlir::ParseResult parseFieldLikeOp(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
llvm::StringRef fieldName;
auto &builder = parser.getBuilder();
mlir::Type recty;
if (parser.parseOptionalKeyword(&fieldName) || parser.parseComma() ||
parser.parseType(recty))
return mlir::failure();
result.addAttribute(fir::FieldIndexOp::getFieldAttrName(),
builder.getStringAttr(fieldName));
if (!mlir::dyn_cast<fir::RecordType>(recty))
return mlir::failure();
result.addAttribute(fir::FieldIndexOp::getTypeAttrName(),
mlir::TypeAttr::get(recty));
if (!parser.parseOptionalLParen()) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands;
llvm::SmallVector<mlir::Type> types;
auto loc = parser.getNameLoc();
if (parser.parseOperandList(operands, mlir::OpAsmParser::Delimiter::None) ||
parser.parseColonTypeList(types) || parser.parseRParen() ||
parser.resolveOperands(operands, types, loc, result.operands))
return mlir::failure();
}
mlir::Type fieldType = TY::get(builder.getContext());
if (parser.addTypeToList(fieldType, result.types))
return mlir::failure();
return mlir::success();
}
mlir::ParseResult fir::FieldIndexOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
return parseFieldLikeOp<fir::FieldType>(parser, result);
}
template <typename OP>
void printFieldLikeOp(mlir::OpAsmPrinter &p, OP &op) {
p << ' '
<< op.getOperation()
->template getAttrOfType<mlir::StringAttr>(
fir::FieldIndexOp::getFieldAttrName())
.getValue()
<< ", " << op.getOperation()->getAttr(fir::FieldIndexOp::getTypeAttrName());
if (op.getNumOperands()) {
p << '(';
p.printOperands(op.getTypeparams());
auto sep = ") : ";
for (auto op : op.getTypeparams()) {
p << sep;
if (op)
p.printType(op.getType());
else
p << "()";
sep = ", ";
}
}
}
void fir::FieldIndexOp::print(mlir::OpAsmPrinter &p) {
printFieldLikeOp(p, *this);
}
void fir::FieldIndexOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result,
llvm::StringRef fieldName, mlir::Type recTy,
mlir::ValueRange operands) {
result.addAttribute(getFieldAttrName(), builder.getStringAttr(fieldName));
result.addAttribute(getTypeAttrName(), mlir::TypeAttr::get(recTy));
result.addOperands(operands);
}
llvm::SmallVector<mlir::Attribute> fir::FieldIndexOp::getAttributes() {
llvm::SmallVector<mlir::Attribute> attrs;
attrs.push_back(getFieldIdAttr());
attrs.push_back(getOnTypeAttr());
return attrs;
}
//===----------------------------------------------------------------------===//
// InsertOnRangeOp
//===----------------------------------------------------------------------===//
static mlir::ParseResult
parseCustomRangeSubscript(mlir::OpAsmParser &parser,
mlir::DenseIntElementsAttr &coord) {
llvm::SmallVector<std::int64_t> lbounds;
llvm::SmallVector<std::int64_t> ubounds;
if (parser.parseKeyword("from") ||
parser.parseCommaSeparatedList(
mlir::AsmParser::Delimiter::Paren,
[&] { return parser.parseInteger(lbounds.emplace_back(0)); }) ||
parser.parseKeyword("to") ||
parser.parseCommaSeparatedList(mlir::AsmParser::Delimiter::Paren, [&] {
return parser.parseInteger(ubounds.emplace_back(0));
}))
return mlir::failure();
llvm::SmallVector<std::int64_t> zippedBounds;
for (auto zip : llvm::zip(lbounds, ubounds)) {
zippedBounds.push_back(std::get<0>(zip));
zippedBounds.push_back(std::get<1>(zip));
}
coord = mlir::Builder(parser.getContext()).getIndexTensorAttr(zippedBounds);
return mlir::success();
}
static void printCustomRangeSubscript(mlir::OpAsmPrinter &printer,
fir::InsertOnRangeOp op,
mlir::DenseIntElementsAttr coord) {
printer << "from (";
auto enumerate = llvm::enumerate(coord.getValues<std::int64_t>());
// Even entries are the lower bounds.
llvm::interleaveComma(
make_filter_range(
enumerate,
[](auto indexed_value) { return indexed_value.index() % 2 == 0; }),
printer, [&](auto indexed_value) { printer << indexed_value.value(); });
printer << ") to (";
// Odd entries are the upper bounds.
llvm::interleaveComma(
make_filter_range(
enumerate,
[](auto indexed_value) { return indexed_value.index() % 2 != 0; }),
printer, [&](auto indexed_value) { printer << indexed_value.value(); });
printer << ")";
}
/// Range bounds must be nonnegative, and the range must not be empty.
llvm::LogicalResult fir::InsertOnRangeOp::verify() {
if (fir::hasDynamicSize(getSeq().getType()))
return emitOpError("must have constant shape and size");
mlir::DenseIntElementsAttr coorAttr = getCoor();
if (coorAttr.size() < 2 || coorAttr.size() % 2 != 0)
return emitOpError("has uneven number of values in ranges");
bool rangeIsKnownToBeNonempty = false;
for (auto i = coorAttr.getValues<std::int64_t>().end(),
b = coorAttr.getValues<std::int64_t>().begin();
i != b;) {
int64_t ub = (*--i);
int64_t lb = (*--i);
if (lb < 0 || ub < 0)
return emitOpError("negative range bound");
if (rangeIsKnownToBeNonempty)
continue;
if (lb > ub)
return emitOpError("empty range");
rangeIsKnownToBeNonempty = lb < ub;
}
return mlir::success();
}
//===----------------------------------------------------------------------===//
// InsertValueOp
//===----------------------------------------------------------------------===//
static bool checkIsIntegerConstant(mlir::Attribute attr, std::int64_t conVal) {
if (auto iattr = mlir::dyn_cast<mlir::IntegerAttr>(attr))
return iattr.getInt() == conVal;
return false;
}
static bool isZero(mlir::Attribute a) { return checkIsIntegerConstant(a, 0); }
static bool isOne(mlir::Attribute a) { return checkIsIntegerConstant(a, 1); }
// Undo some complex patterns created in the front-end and turn them back into
// complex ops.
template <typename FltOp, typename CpxOp>
struct UndoComplexPattern : public mlir::RewritePattern {
UndoComplexPattern(mlir::MLIRContext *ctx)
: mlir::RewritePattern("fir.insert_value", 2, ctx) {}
llvm::LogicalResult
matchAndRewrite(mlir::Operation *op,
mlir::PatternRewriter &rewriter) const override {
auto insval = mlir::dyn_cast_or_null<fir::InsertValueOp>(op);
if (!insval || !mlir::isa<mlir::ComplexType>(insval.getType()))
return mlir::failure();
auto insval2 = mlir::dyn_cast_or_null<fir::InsertValueOp>(
insval.getAdt().getDefiningOp());
if (!insval2)
return mlir::failure();
auto binf = mlir::dyn_cast_or_null<FltOp>(insval.getVal().getDefiningOp());
auto binf2 =
mlir::dyn_cast_or_null<FltOp>(insval2.getVal().getDefiningOp());
if (!binf || !binf2 || insval.getCoor().size() != 1 ||
!isOne(insval.getCoor()[0]) || insval2.getCoor().size() != 1 ||
!isZero(insval2.getCoor()[0]))
return mlir::failure();
auto eai = mlir::dyn_cast_or_null<fir::ExtractValueOp>(
binf.getLhs().getDefiningOp());
auto ebi = mlir::dyn_cast_or_null<fir::ExtractValueOp>(
binf.getRhs().getDefiningOp());
auto ear = mlir::dyn_cast_or_null<fir::ExtractValueOp>(
binf2.getLhs().getDefiningOp());
auto ebr = mlir::dyn_cast_or_null<fir::ExtractValueOp>(
binf2.getRhs().getDefiningOp());
if (!eai || !ebi || !ear || !ebr || ear.getAdt() != eai.getAdt() ||
ebr.getAdt() != ebi.getAdt() || eai.getCoor().size() != 1 ||
!isOne(eai.getCoor()[0]) || ebi.getCoor().size() != 1 ||
!isOne(ebi.getCoor()[0]) || ear.getCoor().size() != 1 ||
!isZero(ear.getCoor()[0]) || ebr.getCoor().size() != 1 ||
!isZero(ebr.getCoor()[0]))
return mlir::failure();
rewriter.replaceOpWithNewOp<CpxOp>(op, ear.getAdt(), ebr.getAdt());
return mlir::success();
}
};
void fir::InsertValueOp::getCanonicalizationPatterns(
mlir::RewritePatternSet &results, mlir::MLIRContext *context) {
results.insert<UndoComplexPattern<mlir::arith::AddFOp, fir::AddcOp>,
UndoComplexPattern<mlir::arith::SubFOp, fir::SubcOp>>(context);
}
//===----------------------------------------------------------------------===//
// IterWhileOp
//===----------------------------------------------------------------------===//
void fir::IterWhileOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Value lb,
mlir::Value ub, mlir::Value step,
mlir::Value iterate, bool finalCountValue,
mlir::ValueRange iterArgs,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
result.addOperands({lb, ub, step, iterate});
if (finalCountValue) {
result.addTypes(builder.getIndexType());
result.addAttribute(getFinalValueAttrNameStr(), builder.getUnitAttr());
}
result.addTypes(iterate.getType());
result.addOperands(iterArgs);
for (auto v : iterArgs)
result.addTypes(v.getType());
mlir::Region *bodyRegion = result.addRegion();
bodyRegion->push_back(new mlir::Block{});
bodyRegion->front().addArgument(builder.getIndexType(), result.location);
bodyRegion->front().addArgument(iterate.getType(), result.location);
bodyRegion->front().addArguments(
iterArgs.getTypes(),
llvm::SmallVector<mlir::Location>(iterArgs.size(), result.location));
result.addAttributes(attributes);
}
mlir::ParseResult fir::IterWhileOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
auto &builder = parser.getBuilder();
mlir::OpAsmParser::Argument inductionVariable, iterateVar;
mlir::OpAsmParser::UnresolvedOperand lb, ub, step, iterateInput;
if (parser.parseLParen() || parser.parseArgument(inductionVariable) ||
parser.parseEqual())
return mlir::failure();
// Parse loop bounds.
auto indexType = builder.getIndexType();
auto i1Type = builder.getIntegerType(1);
if (parser.parseOperand(lb) ||
parser.resolveOperand(lb, indexType, result.operands) ||
parser.parseKeyword("to") || parser.parseOperand(ub) ||
parser.resolveOperand(ub, indexType, result.operands) ||
parser.parseKeyword("step") || parser.parseOperand(step) ||
parser.parseRParen() ||
parser.resolveOperand(step, indexType, result.operands) ||
parser.parseKeyword("and") || parser.parseLParen() ||
parser.parseArgument(iterateVar) || parser.parseEqual() ||
parser.parseOperand(iterateInput) || parser.parseRParen() ||
parser.resolveOperand(iterateInput, i1Type, result.operands))
return mlir::failure();
// Parse the initial iteration arguments.
auto prependCount = false;
// Induction variable.
llvm::SmallVector<mlir::OpAsmParser::Argument> regionArgs;
regionArgs.push_back(inductionVariable);
regionArgs.push_back(iterateVar);
if (succeeded(parser.parseOptionalKeyword("iter_args"))) {
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> operands;
llvm::SmallVector<mlir::Type> regionTypes;
// Parse assignment list and results type list.
if (parser.parseAssignmentList(regionArgs, operands) ||
parser.parseArrowTypeList(regionTypes))
return mlir::failure();
if (regionTypes.size() == operands.size() + 2)
prependCount = true;
llvm::ArrayRef<mlir::Type> resTypes = regionTypes;
resTypes = prependCount ? resTypes.drop_front(2) : resTypes;
// Resolve input operands.
for (auto operandType : llvm::zip(operands, resTypes))
if (parser.resolveOperand(std::get<0>(operandType),
std::get<1>(operandType), result.operands))
return mlir::failure();
if (prependCount) {
result.addTypes(regionTypes);
} else {
result.addTypes(i1Type);
result.addTypes(resTypes);
}
} else if (succeeded(parser.parseOptionalArrow())) {
llvm::SmallVector<mlir::Type> typeList;
if (parser.parseLParen() || parser.parseTypeList(typeList) ||
parser.parseRParen())
return mlir::failure();
// Type list must be "(index, i1)".
if (typeList.size() != 2 || !mlir::isa<mlir::IndexType>(typeList[0]) ||
!typeList[1].isSignlessInteger(1))
return mlir::failure();
result.addTypes(typeList);
prependCount = true;
} else {
result.addTypes(i1Type);
}
if (parser.parseOptionalAttrDictWithKeyword(result.attributes))
return mlir::failure();
llvm::SmallVector<mlir::Type> argTypes;
// Induction variable (hidden)
if (prependCount)
result.addAttribute(IterWhileOp::getFinalValueAttrNameStr(),
builder.getUnitAttr());
else
argTypes.push_back(indexType);
// Loop carried variables (including iterate)
argTypes.append(result.types.begin(), result.types.end());
// Parse the body region.
auto *body = result.addRegion();
if (regionArgs.size() != argTypes.size())
return parser.emitError(
parser.getNameLoc(),
"mismatch in number of loop-carried values and defined values");
for (size_t i = 0, e = regionArgs.size(); i != e; ++i)
regionArgs[i].type = argTypes[i];
if (parser.parseRegion(*body, regionArgs))
return mlir::failure();
fir::IterWhileOp::ensureTerminator(*body, builder, result.location);
return mlir::success();
}
llvm::LogicalResult fir::IterWhileOp::verify() {
// Check that the body defines as single block argument for the induction
// variable.
auto *body = getBody();
if (!body->getArgument(1).getType().isInteger(1))
return emitOpError(
"expected body second argument to be an index argument for "
"the induction variable");
if (!body->getArgument(0).getType().isIndex())
return emitOpError(
"expected body first argument to be an index argument for "
"the induction variable");
auto opNumResults = getNumResults();
if (getFinalValue()) {
// Result type must be "(index, i1, ...)".
if (!mlir::isa<mlir::IndexType>(getResult(0).getType()))
return emitOpError("result #0 expected to be index");
if (!getResult(1).getType().isSignlessInteger(1))
return emitOpError("result #1 expected to be i1");
opNumResults--;
} else {
// iterate_while always returns the early exit induction value.
// Result type must be "(i1, ...)"
if (!getResult(0).getType().isSignlessInteger(1))
return emitOpError("result #0 expected to be i1");
}
if (opNumResults == 0)
return mlir::failure();
if (getNumIterOperands() != opNumResults)
return emitOpError(
"mismatch in number of loop-carried values and defined values");
if (getNumRegionIterArgs() != opNumResults)
return emitOpError(
"mismatch in number of basic block args and defined values");
auto iterOperands = getIterOperands();
auto iterArgs = getRegionIterArgs();
auto opResults = getFinalValue() ? getResults().drop_front() : getResults();
unsigned i = 0u;
for (auto e : llvm::zip(iterOperands, iterArgs, opResults)) {
if (std::get<0>(e).getType() != std::get<2>(e).getType())
return emitOpError() << "types mismatch between " << i
<< "th iter operand and defined value";
if (std::get<1>(e).getType() != std::get<2>(e).getType())
return emitOpError() << "types mismatch between " << i
<< "th iter region arg and defined value";
i++;
}
return mlir::success();
}
void fir::IterWhileOp::print(mlir::OpAsmPrinter &p) {
p << " (" << getInductionVar() << " = " << getLowerBound() << " to "
<< getUpperBound() << " step " << getStep() << ") and (";
assert(hasIterOperands());
auto regionArgs = getRegionIterArgs();
auto operands = getIterOperands();
p << regionArgs.front() << " = " << *operands.begin() << ")";
if (regionArgs.size() > 1) {
p << " iter_args(";
llvm::interleaveComma(
llvm::zip(regionArgs.drop_front(), operands.drop_front()), p,
[&](auto it) { p << std::get<0>(it) << " = " << std::get<1>(it); });
p << ") -> (";
llvm::interleaveComma(
llvm::drop_begin(getResultTypes(), getFinalValue() ? 0 : 1), p);
p << ")";
} else if (getFinalValue()) {
p << " -> (" << getResultTypes() << ')';
}
p.printOptionalAttrDictWithKeyword((*this)->getAttrs(),
{getFinalValueAttrNameStr()});
p << ' ';
p.printRegion(getRegion(), /*printEntryBlockArgs=*/false,
/*printBlockTerminators=*/true);
}
llvm::SmallVector<mlir::Region *> fir::IterWhileOp::getLoopRegions() {
return {&getRegion()};
}
mlir::BlockArgument fir::IterWhileOp::iterArgToBlockArg(mlir::Value iterArg) {
for (auto i : llvm::enumerate(getInitArgs()))
if (iterArg == i.value())
return getRegion().front().getArgument(i.index() + 1);
return {};
}
void fir::IterWhileOp::resultToSourceOps(
llvm::SmallVectorImpl<mlir::Value> &results, unsigned resultNum) {
auto oper = getFinalValue() ? resultNum + 1 : resultNum;
auto *term = getRegion().front().getTerminator();
if (oper < term->getNumOperands())
results.push_back(term->getOperand(oper));
}
mlir::Value fir::IterWhileOp::blockArgToSourceOp(unsigned blockArgNum) {
if (blockArgNum > 0 && blockArgNum <= getInitArgs().size())
return getInitArgs()[blockArgNum - 1];
return {};
}
std::optional<llvm::MutableArrayRef<mlir::OpOperand>>
fir::IterWhileOp::getYieldedValuesMutable() {
auto *term = getRegion().front().getTerminator();
return getFinalValue() ? term->getOpOperands().drop_front()
: term->getOpOperands();
}
//===----------------------------------------------------------------------===//
// LenParamIndexOp
//===----------------------------------------------------------------------===//
mlir::ParseResult fir::LenParamIndexOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
return parseFieldLikeOp<fir::LenType>(parser, result);
}
void fir::LenParamIndexOp::print(mlir::OpAsmPrinter &p) {
printFieldLikeOp(p, *this);
}
void fir::LenParamIndexOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result,
llvm::StringRef fieldName, mlir::Type recTy,
mlir::ValueRange operands) {
result.addAttribute(getFieldAttrName(), builder.getStringAttr(fieldName));
result.addAttribute(getTypeAttrName(), mlir::TypeAttr::get(recTy));
result.addOperands(operands);
}
llvm::SmallVector<mlir::Attribute> fir::LenParamIndexOp::getAttributes() {
llvm::SmallVector<mlir::Attribute> attrs;
attrs.push_back(getFieldIdAttr());
attrs.push_back(getOnTypeAttr());
return attrs;
}
//===----------------------------------------------------------------------===//
// LoadOp
//===----------------------------------------------------------------------===//
void fir::LoadOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::Value refVal) {
if (!refVal) {
mlir::emitError(result.location, "LoadOp has null argument");
return;
}
auto eleTy = fir::dyn_cast_ptrEleTy(refVal.getType());
if (!eleTy) {
mlir::emitError(result.location, "not a memory reference type");
return;
}
build(builder, result, eleTy, refVal);
}
void fir::LoadOp::build(mlir::OpBuilder &builder, mlir::OperationState &result,
mlir::Type resTy, mlir::Value refVal) {
if (!refVal) {
mlir::emitError(result.location, "LoadOp has null argument");
return;
}
result.addOperands(refVal);
result.addTypes(resTy);
}
mlir::ParseResult fir::LoadOp::getElementOf(mlir::Type &ele, mlir::Type ref) {
if ((ele = fir::dyn_cast_ptrEleTy(ref)))
return mlir::success();
return mlir::failure();
}
mlir::ParseResult fir::LoadOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
mlir::Type type;
mlir::OpAsmParser::UnresolvedOperand oper;
if (parser.parseOperand(oper) ||
parser.parseOptionalAttrDict(result.attributes) ||
parser.parseColonType(type) ||
parser.resolveOperand(oper, type, result.operands))
return mlir::failure();
mlir::Type eleTy;
if (fir::LoadOp::getElementOf(eleTy, type) ||
parser.addTypeToList(eleTy, result.types))
return mlir::failure();
return mlir::success();
}
void fir::LoadOp::print(mlir::OpAsmPrinter &p) {
p << ' ';
p.printOperand(getMemref());
p.printOptionalAttrDict(getOperation()->getAttrs(), {});
p << " : " << getMemref().getType();
}
//===----------------------------------------------------------------------===//
// DoLoopOp
//===----------------------------------------------------------------------===//
void fir::DoLoopOp::build(mlir::OpBuilder &builder,
mlir::OperationState &result, mlir::Value lb,
mlir::Value ub, mlir::Value step, bool unordered,
bool finalCountValue, mlir::ValueRange iterArgs,
mlir::ValueRange reduceOperands,
llvm::ArrayRef<mlir::Attribute> reduceAttrs,
llvm::ArrayRef<mlir::NamedAttribute> attributes) {
result.addOperands({lb, ub, step});
result.addOperands(reduceOperands);
result.addOperands(iterArgs);
result.addAttribute(getOperandSegmentSizeAttr(),
builder.getDenseI32ArrayAttr(
{1, 1, 1, static_cast<int32_t>(reduceOperands.size()),
static_cast<int32_t>(iterArgs.size())}));
if (finalCountValue) {
result.addTypes(builder.getIndexType());
result.addAttribute(getFinalValueAttrName(result.name),
builder.getUnitAttr());
}
for (auto v : iterArgs)
result.addTypes(v.getType());
mlir::Region *bodyRegion = result.addRegion();
bodyRegion->push_back(new mlir::Block{});
if (iterArgs.empty() && !finalCountValue)
fir::DoLoopOp::ensureTerminator(*bodyRegion, builder, result.location);
bodyRegion->front().addArgument(builder.getIndexType(), result.location);
bodyRegion->front().addArguments(
iterArgs.getTypes(),
llvm::SmallVector<mlir::Location>(iterArgs.size(), result.location));
if (unordered)
result.addAttribute(getUnorderedAttrName(result.name),
builder.getUnitAttr());
if (!reduceAttrs.empty())
result.addAttribute(getReduceAttrsAttrName(result.name),
builder.getArrayAttr(reduceAttrs));
result.addAttributes(attributes);
}
mlir::ParseResult fir::DoLoopOp::parse(mlir::OpAsmParser &parser,
mlir::OperationState &result) {
auto &builder = parser.getBuilder();
mlir::OpAsmParser::Argument inductionVariable;
mlir::OpAsmParser::UnresolvedOperand lb, ub, step;
// Parse the induction variable followed by '='.
if (parser.parseArgument(inductionVariable) || parser.parseEqual())
return mlir::failure();
// Parse loop bounds.
auto indexType = builder.getIndexType();
if (parser.parseOperand(lb) ||
parser.resolveOperand(lb, indexType, result.operands) ||
parser.parseKeyword("to") || parser.parseOperand(ub) ||
parser.resolveOperand(ub, indexType, result.operands) ||
parser.parseKeyword("step") || parser.parseOperand(step) ||
parser.resolveOperand(step, indexType, result.operands))
return mlir::failure();
if (mlir::succeeded(parser.parseOptionalKeyword("unordered")))
result.addAttribute("unordered", builder.getUnitAttr());
// Parse the reduction arguments.
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> reduceOperands;
llvm::SmallVector<mlir::Type> reduceArgTypes;
if (succeeded(parser.parseOptionalKeyword("reduce"))) {
// Parse reduction attributes and variables.
llvm::SmallVector<ReduceAttr> attributes;
if (failed(parser.parseCommaSeparatedList(
mlir::AsmParser::Delimiter::Paren, [&]() {
if (parser.parseAttribute(attributes.emplace_back()) ||
parser.parseArrow() ||
parser.parseOperand(reduceOperands.emplace_back()) ||
parser.parseColonType(reduceArgTypes.emplace_back()))
return mlir::failure();
return mlir::success();
})))
return mlir::failure();
// Resolve input operands.
for (auto operand_type : llvm::zip(reduceOperands, reduceArgTypes))
if (parser.resolveOperand(std::get<0>(operand_type),
std::get<1>(operand_type), result.operands))
return mlir::failure();
llvm::SmallVector<mlir::Attribute> arrayAttr(attributes.begin(),
attributes.end());
result.addAttribute(getReduceAttrsAttrName(result.name),
builder.getArrayAttr(arrayAttr));
}
// Parse the optional initial iteration arguments.
llvm::SmallVector<mlir::OpAsmParser::Argument> regionArgs;
llvm::SmallVector<mlir::OpAsmParser::UnresolvedOperand> iterOperands;
llvm::SmallVector<mlir::Type> argTypes;
bool prependCount = false;
regionArgs.push_back(inductionVariable);
if (succeeded(parser.parseOptionalKeyword(