-
Notifications
You must be signed in to change notification settings - Fork 10.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[flang][NFC] Unify getIfConstantIntValue helpers #87633
Conversation
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.
@llvm/pr-subscribers-flang-fir-hlfir @llvm/pr-subscribers-flang-codegen Author: Tom Eccles (tblah) ChangesI 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. Full diff: https://github.com/llvm/llvm-project/pull/87633.diff 3 Files Affected:
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);
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There is actually fir::getIntIfConstant that is doing a similar job except it does not handle the LLVM::ConstantOp (which needs to be handled on codegen).
Could you merge them so that there is a single helper?
|
||
/// 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); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't this declaration be removed?
There were different helpers for attempting to fetch compile time constants from MLIR: one in fir::getIntIfConstant and one in CodeGen. Unify the two.