diff --git a/flang/include/flang/Optimizer/Builder/FIRBuilder.h b/flang/include/flang/Optimizer/Builder/FIRBuilder.h --- a/flang/include/flang/Optimizer/Builder/FIRBuilder.h +++ b/flang/include/flang/Optimizer/Builder/FIRBuilder.h @@ -18,6 +18,7 @@ #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" @@ -593,9 +594,6 @@ mlir::Value createZeroValue(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Type type); -/// Unwrap integer constant from an mlir::Value. -llvm::Optional getIntIfConstant(mlir::Value value); - /// Get the integer constants of triplet and compute the extent. llvm::Optional getExtentFromTriplet(mlir::Value lb, mlir::Value ub, mlir::Value stride); diff --git a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h --- a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h +++ b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h @@ -107,6 +107,15 @@ /// 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 getIntIfConstant(mlir::Value value) { + if (auto *definingOp = value.getDefiningOp()) + if (auto cst = mlir::dyn_cast(definingOp)) + if (auto intAttr = cst.getValue().dyn_cast()) + return intAttr.getInt(); + return {}; +} + } // namespace fir #endif // FORTRAN_OPTIMIZER_DIALECT_FIROPSSUPPORT_H diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp --- a/flang/lib/Lower/ConvertExprToHLFIR.cpp +++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp @@ -883,7 +883,7 @@ // 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)}; } diff --git a/flang/lib/Lower/IntrinsicCall.cpp b/flang/lib/Lower/IntrinsicCall.cpp --- a/flang/lib/Lower/IntrinsicCall.cpp +++ b/flang/lib/Lower/IntrinsicCall.cpp @@ -4676,8 +4676,7 @@ mlir::Value dim = fir::getBase(args[1]); // If it is a compile time constant, skip the runtime call. - if (llvm::Optional cstDim = - fir::factory::getIntIfConstant(dim)) { + if (std::optional 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); diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp --- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp +++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp @@ -1330,21 +1330,13 @@ "numeric or logical type"); } -llvm::Optional fir::factory::getIntIfConstant(mlir::Value value) { - if (auto *definingOp = value.getDefiningOp()) - if (auto cst = mlir::dyn_cast(definingOp)) - if (auto intAttr = cst.getValue().dyn_cast()) - return intAttr.getInt(); - return {}; -} - llvm::Optional fir::factory::getExtentFromTriplet(mlir::Value lb, mlir::Value ub, mlir::Value stride) { std::function(mlir::Value)> getConstantValue = [&](mlir::Value value) -> llvm::Optional { - 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(definingOp)) { auto valOp = mlir::dyn_cast(definingOp); diff --git a/flang/lib/Optimizer/Builder/HLFIRTools.cpp b/flang/lib/Optimizer/Builder/HLFIRTools.cpp --- a/flang/lib/Optimizer/Builder/HLFIRTools.cpp +++ b/flang/lib/Optimizer/Builder/HLFIRTools.cpp @@ -305,7 +305,7 @@ 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); @@ -500,7 +500,7 @@ hlfir::ExprType::Shape typeShape(rank, hlfir::ExprType::getUnknownExtent()); if (auto shapeOp = shape.getDefiningOp()) 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); diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp --- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp +++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp @@ -288,7 +288,7 @@ TODO(loc, "unbox"); rewriter.create(loc, var); }; - if (auto cstMustFree = fir::factory::getIntIfConstant(mustFree)) { + if (auto cstMustFree = fir::getIntIfConstant(mustFree)) { if (*cstMustFree != 0) genFree(); // else, nothing to do.