#include "flang/Common/MathOptionsBase.h"
#include "flang/Optimizer/Dialect/FIROps.h"
+#include "flang/Optimizer/Dialect/FIROpsSupport.h"
#include "flang/Optimizer/Dialect/FIRType.h"
#include "flang/Optimizer/Support/KindMapping.h"
#include "mlir/IR/Builders.h"
mlir::Value createZeroValue(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Type type);
-/// Unwrap integer constant from an mlir::Value.
-llvm::Optional<std::int64_t> getIntIfConstant(mlir::Value value);
-
/// Get the integer constants of triplet and compute the extent.
llvm::Optional<std::int64_t>
getExtentFromTriplet(mlir::Value lb, mlir::Value ub, mlir::Value stride);
/// function has any host associations, for example.
bool anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr);
+/// Unwrap integer constant from an mlir::Value.
+inline std::optional<std::int64_t> getIntIfConstant(mlir::Value value) {
+ if (auto *definingOp = value.getDefiningOp())
+ if (auto cst = mlir::dyn_cast<mlir::arith::ConstantOp>(definingOp))
+ if (auto intAttr = cst.getValue().dyn_cast<mlir::IntegerAttr>())
+ return intAttr.getInt();
+ return {};
+}
+
} // namespace fir
#endif // FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H
// IR harder to read: directly use index constants for constant subscripts.
mlir::Type idxTy = builder.getIndexType();
if (loweredExpr.getType() != idxTy)
- if (auto cstIndex = fir::factory::getIntIfConstant(loweredExpr))
+ if (auto cstIndex = fir::getIntIfConstant(loweredExpr))
return hlfir::EntityWithAttributes{
builder.createIntegerConstant(getLoc(), idxTy, *cstIndex)};
}
mlir::Value dim = fir::getBase(args[1]);
// If it is a compile time constant, skip the runtime call.
- if (llvm::Optional<std::int64_t> cstDim =
- fir::factory::getIntIfConstant(dim)) {
+ if (std::optional<std::int64_t> cstDim = fir::getIntIfConstant(dim)) {
mlir::Value one = builder.createIntegerConstant(loc, resultType, 1);
mlir::Value zero = builder.createIntegerConstant(loc, indexType, 0);
mlir::Value lb = computeLBOUND(builder, loc, array, *cstDim - 1, zero, one);
"numeric or logical type");
}
-llvm::Optional<std::int64_t> fir::factory::getIntIfConstant(mlir::Value value) {
- if (auto *definingOp = value.getDefiningOp())
- if (auto cst = mlir::dyn_cast<mlir::arith::ConstantOp>(definingOp))
- if (auto intAttr = cst.getValue().dyn_cast<mlir::IntegerAttr>())
- return intAttr.getInt();
- return {};
-}
-
llvm::Optional<std::int64_t>
fir::factory::getExtentFromTriplet(mlir::Value lb, mlir::Value ub,
mlir::Value stride) {
std::function<llvm::Optional<std::int64_t>(mlir::Value)> getConstantValue =
[&](mlir::Value value) -> llvm::Optional<std::int64_t> {
- if (auto valInt = fir::factory::getIntIfConstant(value))
- return valInt;
+ if (auto valInt = fir::getIntIfConstant(value))
+ return *valInt;
auto *definingOp = value.getDefiningOp();
if (mlir::isa_and_nonnull<fir::ConvertOp>(definingOp)) {
auto valOp = mlir::dyn_cast<fir::ConvertOp>(definingOp);
static mlir::Value genUBound(mlir::Location loc, fir::FirOpBuilder &builder,
mlir::Value lb, mlir::Value extent,
mlir::Value one) {
- if (auto constantLb = fir::factory::getIntIfConstant(lb))
+ if (auto constantLb = fir::getIntIfConstant(lb))
if (*constantLb == 1)
return extent;
extent = builder.createConvert(loc, one.getType(), extent);
hlfir::ExprType::Shape typeShape(rank, hlfir::ExprType::getUnknownExtent());
if (auto shapeOp = shape.getDefiningOp<fir::ShapeOp>())
for (auto extent : llvm::enumerate(shapeOp.getExtents()))
- if (auto cstExtent = fir::factory::getIntIfConstant(extent.value()))
+ if (auto cstExtent = fir::getIntIfConstant(extent.value()))
typeShape[extent.index()] = *cstExtent;
return hlfir::ExprType::get(elementType.getContext(), typeShape, elementType,
isPolymorphic);
TODO(loc, "unbox");
rewriter.create<fir::FreeMemOp>(loc, var);
};
- if (auto cstMustFree = fir::factory::getIntIfConstant(mustFree)) {
+ if (auto cstMustFree = fir::getIntIfConstant(mustFree)) {
if (*cstMustFree != 0)
genFree();
// else, nothing to do.