[flang-commits] [flang] [flang][NFC] Unify getIfConstantIntValue helpers (PR #87633)
Tom Eccles via flang-commits
flang-commits at lists.llvm.org
Fri Apr 5 04:39:05 PDT 2024
https://github.com/tblah updated https://github.com/llvm/llvm-project/pull/87633
>From e4b4334e7878d2996dba4b39b8102e78a8a9d895 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Thu, 4 Apr 2024 12:34:42 +0000
Subject: [PATCH 1/3] [flang][NFC] Move getIfConstantIntValue to common header
I would like to re-use this function in a subsequent patch. I picked
fir::factory because that seemed to contain various free functions that
don't need to be in FirOpBuilder. This might be better as a static
method of FirOpBuilder - I'm unsure.
---
.../flang/Optimizer/Builder/FIRBuilder.h | 4 ++++
flang/lib/Optimizer/Builder/FIRBuilder.cpp | 15 +++++++++++++
flang/lib/Optimizer/CodeGen/CodeGen.cpp | 21 ++-----------------
3 files changed, 21 insertions(+), 19 deletions(-)
diff --git a/flang/include/flang/Optimizer/Builder/FIRBuilder.h b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
index 940866b25d2fe8..8993559eca2ef9 100644
--- a/flang/include/flang/Optimizer/Builder/FIRBuilder.h
+++ b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
@@ -695,6 +695,10 @@ mlir::Value createNullBoxProc(fir::FirOpBuilder &builder, mlir::Location loc,
/// Set internal linkage attribute on a function.
void setInternalLinkage(mlir::func::FuncOp);
+
+/// Extract constant from a value if it is a result of one of the ConstantOp
+/// operations, otherwise, return std::nullopt.
+std::optional<int64_t> getIfConstantIntValue(mlir::Value val);
} // namespace fir::factory
#endif // FORTRAN_OPTIMIZER_BUILDER_FIRBUILDER_H
diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index e4362b2f9e6945..4e4c79bc8c03aa 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -1584,3 +1584,18 @@ void fir::factory::setInternalLinkage(mlir::func::FuncOp func) {
mlir::LLVM::LinkageAttr::get(func->getContext(), internalLinkage);
func->setAttr("llvm.linkage", linkage);
}
+
+std::optional<int64_t> fir::factory::getIfConstantIntValue(mlir::Value val) {
+ if (!val || !val.dyn_cast<mlir::OpResult>())
+ return {};
+
+ mlir::Operation *defop = val.getDefiningOp();
+
+ if (auto constOp = mlir::dyn_cast<mlir::arith::ConstantIntOp>(defop))
+ return constOp.value();
+ if (auto llConstOp = mlir::dyn_cast<mlir::LLVM::ConstantOp>(defop))
+ if (auto attr = llConstOp.getValue().dyn_cast<mlir::IntegerAttr>())
+ return attr.getValue().getSExtValue();
+
+ return {};
+}
\ No newline at end of file
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 06ce84f1543a3f..526cad884df881 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -92,27 +92,10 @@ static mlir::Block *createBlock(mlir::ConversionPatternRewriter &rewriter,
mlir::Region::iterator(insertBefore));
}
-/// Extract constant from a value if it is a result of one of the
-/// ConstantOp operations, otherwise, return std::nullopt.
-static std::optional<int64_t> getIfConstantIntValue(mlir::Value val) {
- if (!val || !val.dyn_cast<mlir::OpResult>())
- return {};
-
- mlir::Operation *defop = val.getDefiningOp();
-
- if (auto constOp = mlir::dyn_cast<mlir::arith::ConstantIntOp>(defop))
- return constOp.value();
- if (auto llConstOp = mlir::dyn_cast<mlir::LLVM::ConstantOp>(defop))
- if (auto attr = llConstOp.getValue().dyn_cast<mlir::IntegerAttr>())
- return attr.getValue().getSExtValue();
-
- return {};
-}
-
/// Extract constant from a value that must be the result of one of the
/// ConstantOp operations.
static int64_t getConstantIntValue(mlir::Value val) {
- if (auto constVal = getIfConstantIntValue(val))
+ if (auto constVal = fir::factory::getIfConstantIntValue(val))
return *constVal;
fir::emitFatalError(val.getLoc(), "must be a constant");
}
@@ -664,7 +647,7 @@ struct ConvertOpConversion : public fir::FIROpConversion<fir::ConvertOp> {
<< " -> " << toTy;
// Do folding for constant inputs.
- if (auto constVal = getIfConstantIntValue(op0)) {
+ if (auto constVal = fir::factory::getIfConstantIntValue(op0)) {
mlir::Value normVal =
genConstantIndex(loc, toTy, rewriter, *constVal ? 1 : 0);
rewriter.replaceOp(convert, normVal);
>From dfcf6de088a0a961e23e2f7ededb83f7d7939d46 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Thu, 4 Apr 2024 14:23:53 +0000
Subject: [PATCH 2/3] Unify helpers
---
.../flang/Optimizer/Dialect/FIROpsSupport.h | 8 +-------
flang/lib/Optimizer/Builder/FIRBuilder.cpp | 15 ---------------
flang/lib/Optimizer/CodeGen/CodeGen.cpp | 4 ++--
flang/lib/Optimizer/Dialect/FIROps.cpp | 12 ++++++++++++
4 files changed, 15 insertions(+), 24 deletions(-)
diff --git a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
index f29e44504acb63..3266ea3aa7fdc6 100644
--- a/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
+++ b/flang/include/flang/Optimizer/Dialect/FIROpsSupport.h
@@ -155,13 +155,7 @@ bool valueMayHaveFirAttributes(mlir::Value value,
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 {};
-}
+std::optional<std::int64_t> getIntIfConstant(mlir::Value value);
static constexpr llvm::StringRef getAdaptToByRefAttrName() {
return "adapt.valuebyref";
diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index 4e4c79bc8c03aa..ec689c37c2330a 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -1583,19 +1583,4 @@ void fir::factory::setInternalLinkage(mlir::func::FuncOp func) {
auto linkage =
mlir::LLVM::LinkageAttr::get(func->getContext(), internalLinkage);
func->setAttr("llvm.linkage", linkage);
-}
-
-std::optional<int64_t> fir::factory::getIfConstantIntValue(mlir::Value val) {
- if (!val || !val.dyn_cast<mlir::OpResult>())
- return {};
-
- mlir::Operation *defop = val.getDefiningOp();
-
- if (auto constOp = mlir::dyn_cast<mlir::arith::ConstantIntOp>(defop))
- return constOp.value();
- if (auto llConstOp = mlir::dyn_cast<mlir::LLVM::ConstantOp>(defop))
- if (auto attr = llConstOp.getValue().dyn_cast<mlir::IntegerAttr>())
- return attr.getValue().getSExtValue();
-
- return {};
}
\ No newline at end of file
diff --git a/flang/lib/Optimizer/CodeGen/CodeGen.cpp b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
index 526cad884df881..d909bda89cdeb4 100644
--- a/flang/lib/Optimizer/CodeGen/CodeGen.cpp
+++ b/flang/lib/Optimizer/CodeGen/CodeGen.cpp
@@ -95,7 +95,7 @@ static mlir::Block *createBlock(mlir::ConversionPatternRewriter &rewriter,
/// Extract constant from a value that must be the result of one of the
/// ConstantOp operations.
static int64_t getConstantIntValue(mlir::Value val) {
- if (auto constVal = fir::factory::getIfConstantIntValue(val))
+ if (auto constVal = fir::getIntIfConstant(val))
return *constVal;
fir::emitFatalError(val.getLoc(), "must be a constant");
}
@@ -647,7 +647,7 @@ struct ConvertOpConversion : public fir::FIROpConversion<fir::ConvertOp> {
<< " -> " << toTy;
// Do folding for constant inputs.
- if (auto constVal = fir::factory::getIfConstantIntValue(op0)) {
+ if (auto constVal = fir::getIntIfConstant(op0)) {
mlir::Value normVal =
genConstantIndex(loc, toTy, rewriter, *constVal ? 1 : 0);
rewriter.replaceOp(convert, normVal);
diff --git a/flang/lib/Optimizer/Dialect/FIROps.cpp b/flang/lib/Optimizer/Dialect/FIROps.cpp
index dba2c30d1851bf..8ab74103cb6a80 100644
--- a/flang/lib/Optimizer/Dialect/FIROps.cpp
+++ b/flang/lib/Optimizer/Dialect/FIROps.cpp
@@ -3834,6 +3834,18 @@ bool fir::anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr) {
return false;
}
+std::optional<std::int64_t> fir::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();
+ if (auto llConstOp = mlir::dyn_cast<mlir::LLVM::ConstantOp>(definingOp))
+ if (auto attr = llConstOp.getValue().dyn_cast<mlir::IntegerAttr>())
+ return attr.getValue().getSExtValue();
+ }
+ return {};
+}
+
mlir::Type fir::applyPathToType(mlir::Type eleTy, mlir::ValueRange path) {
for (auto i = path.begin(), end = path.end(); eleTy && i < end;) {
eleTy = llvm::TypeSwitch<mlir::Type, mlir::Type>(eleTy)
>From 86467f382c17a54f8280b4854a12716d0d6859d4 Mon Sep 17 00:00:00 2001
From: Tom Eccles <tom.eccles at arm.com>
Date: Fri, 5 Apr 2024 11:37:21 +0000
Subject: [PATCH 3/3] Remove unrelated changes
---
flang/include/flang/Optimizer/Builder/FIRBuilder.h | 4 ----
flang/lib/Optimizer/Builder/FIRBuilder.cpp | 2 +-
2 files changed, 1 insertion(+), 5 deletions(-)
diff --git a/flang/include/flang/Optimizer/Builder/FIRBuilder.h b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
index 8993559eca2ef9..940866b25d2fe8 100644
--- a/flang/include/flang/Optimizer/Builder/FIRBuilder.h
+++ b/flang/include/flang/Optimizer/Builder/FIRBuilder.h
@@ -695,10 +695,6 @@ mlir::Value createNullBoxProc(fir::FirOpBuilder &builder, mlir::Location loc,
/// Set internal linkage attribute on a function.
void setInternalLinkage(mlir::func::FuncOp);
-
-/// Extract constant from a value if it is a result of one of the ConstantOp
-/// operations, otherwise, return std::nullopt.
-std::optional<int64_t> getIfConstantIntValue(mlir::Value val);
} // namespace fir::factory
#endif // FORTRAN_OPTIMIZER_BUILDER_FIRBUILDER_H
diff --git a/flang/lib/Optimizer/Builder/FIRBuilder.cpp b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
index ec689c37c2330a..e4362b2f9e6945 100644
--- a/flang/lib/Optimizer/Builder/FIRBuilder.cpp
+++ b/flang/lib/Optimizer/Builder/FIRBuilder.cpp
@@ -1583,4 +1583,4 @@ void fir::factory::setInternalLinkage(mlir::func::FuncOp func) {
auto linkage =
mlir::LLVM::LinkageAttr::get(func->getContext(), internalLinkage);
func->setAttr("llvm.linkage", linkage);
-}
\ No newline at end of file
+}
More information about the flang-commits
mailing list