[flang-commits] [flang] a1fae71 - [flang][NFC] move getIntIfConstant into FIROpsSupport.h
Jean Perier via flang-commits
flang-commits at lists.llvm.org
Mon Dec 19 00:49:49 PST 2022
Author: Jean Perier
Date: 2022-12-19T09:49:23+01:00
New Revision: a1fae71f85994858e402a1fc0ed4d68c46b0a57c
URL: https://github.com/llvm/llvm-project/commit/a1fae71f85994858e402a1fc0ed4d68c46b0a57c
DIFF: https://github.com/llvm/llvm-project/commit/a1fae71f85994858e402a1fc0ed4d68c46b0a57c.diff
LOG: [flang][NFC] move getIntIfConstant into FIROpsSupport.h
The motivation is to have it accessible in HLFIROps.cpp to
use it in hlfir.set_length builder to build the result length
type as best as possible.
Differential Revision: https://reviews.llvm.org/D140214
Added:
Modified:
flang/include/flang/Optimizer/Builder/FIRBuilder.h
flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
flang/lib/Lower/ConvertExprToHLFIR.cpp
flang/lib/Lower/IntrinsicCall.cpp
flang/lib/Optimizer/Builder/FIRBuilder.cpp
flang/lib/Optimizer/Builder/HLFIRTools.cpp
flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
Removed:
################################################################################
diff --git a/flang/include/flang/Optimizer/Builder/FIRBuilder.h b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
index 81784f95a072d..4d89e5b2931ef 100644
--- 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 genLenOfCharacter(fir::FirOpBuilder &builder, mlir::Location loc,
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);
diff --git a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
index 366cc78383121..19cfdbb0fbf74 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
+++ b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
@@ -107,6 +107,15 @@ bool valueHasFirAttribute(mlir::Value value, llvm::StringRef attributeName);
/// 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
diff --git a/flang/lib/Lower/ConvertExprToHLFIR.cpp b/flang/lib/Lower/ConvertExprToHLFIR.cpp
index 06bee481be2be..589e7d0579ad8 100644
--- a/flang/lib/Lower/ConvertExprToHLFIR.cpp
+++ b/flang/lib/Lower/ConvertExprToHLFIR.cpp
@@ -883,7 +883,7 @@ HlfirDesignatorBuilder::genSubscript(const Fortran::evaluate::Expr<T> &expr) {
// 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
index 883d129a35636..65bb310ba90c2 100644
--- a/flang/lib/Lower/IntrinsicCall.cpp
+++ b/flang/lib/Lower/IntrinsicCall.cpp
@@ -4676,8 +4676,7 @@ IntrinsicLibrary::genLbound(mlir::Type resultType,
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);
diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index 946f93b12ba0d..50e28e9966477 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -1330,21 +1330,13 @@ mlir::Value fir::factory::createZeroValue(fir::FirOpBuilder &builder,
"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);
diff --git a/flang/lib/Optimizer/Builder/HLFIRTools.cpp b/flang/lib/Optimizer/Builder/HLFIRTools.cpp
index d096ca9ff860e..6903140c3331a 100644
--- a/flang/lib/Optimizer/Builder/HLFIRTools.cpp
+++ b/flang/lib/Optimizer/Builder/HLFIRTools.cpp
@@ -305,7 +305,7 @@ hlfir::Entity hlfir::getElementAt(mlir::Location loc,
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 @@ static hlfir::ExprType getArrayExprType(mlir::Type elementType,
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);
diff --git a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
index a6b44921979b4..491d75fe4eb1e 100644
--- a/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
+++ b/flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp
@@ -288,7 +288,7 @@ struct EndAssociateOpConversion
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.
More information about the flang-commits
mailing list