diff options
author | Jean Perier <jperier@nvidia.com> | 2022-12-19 09:49:06 +0100 |
---|---|---|
committer | Jean Perier <jperier@nvidia.com> | 2022-12-19 09:49:23 +0100 |
commit | a1fae71f85994858e402a1fc0ed4d68c46b0a57c (patch) | |
tree | 609c7c29f60dc48f89207b674b5c3a39bb362ca2 | |
parent | 9d3f9adf04821f80b0017afff06edc73f9b86a5c (diff) | |
download | llvm-a1fae71f85994858e402a1fc0ed4d68c46b0a57c.zip llvm-a1fae71f85994858e402a1fc0ed4d68c46b0a57c.tar.gz llvm-a1fae71f85994858e402a1fc0ed4d68c46b0a57c.tar.bz2 |
[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
-rw-r--r-- | flang/include/flang/Optimizer/Builder/FIRBuilder.h | 4 | ||||
-rw-r--r-- | flang/include/flang/Optimizer/Dialect/FIROpsSupport.h | 9 | ||||
-rw-r--r-- | flang/lib/Lower/ConvertExprToHLFIR.cpp | 2 | ||||
-rw-r--r-- | flang/lib/Lower/IntrinsicCall.cpp | 3 | ||||
-rw-r--r-- | flang/lib/Optimizer/Builder/FIRBuilder.cpp | 12 | ||||
-rw-r--r-- | flang/lib/Optimizer/Builder/HLFIRTools.cpp | 4 | ||||
-rw-r--r-- | flang/lib/Optimizer/HLFIR/Transforms/BufferizeHLFIR.cpp | 2 |
7 files changed, 17 insertions, 19 deletions
diff --git a/flang/include/flang/Optimizer/Builder/FIRBuilder.h b/flang/include/flang/Optimizer/Builder/FIRBuilder.h index 81784f9..4d89e5b 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 366cc78..19cfdbb 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 06bee48..589e7d0 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 883d129..65bb310 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 946f93b..50e28e9 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 d096ca9..6903140 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 a6b4492..491d75f 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. |