Skip to content

Commit a5ae54a

Browse files
authored
[flang][NFC] Unify getIfConstantIntValue helpers (#87633)
There were different helpers for attempting to fetch compile time constants from MLIR: one in fir::getIntIfConstant and one in CodeGen. Unify the two.
1 parent c0211ff commit a5ae54a

File tree

3 files changed

+15
-26
lines changed

3 files changed

+15
-26
lines changed

flang/include/flang/Optimizer/Dialect/FIROpsSupport.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,7 @@ bool valueMayHaveFirAttributes(mlir::Value value,
155155
bool anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr);
156156

157157
/// Unwrap integer constant from an mlir::Value.
158-
inline std::optional<std::int64_t> getIntIfConstant(mlir::Value value) {
159-
if (auto *definingOp = value.getDefiningOp())
160-
if (auto cst = mlir::dyn_cast<mlir::arith::ConstantOp>(definingOp))
161-
if (auto intAttr = cst.getValue().dyn_cast<mlir::IntegerAttr>())
162-
return intAttr.getInt();
163-
return {};
164-
}
158+
std::optional<std::int64_t> getIntIfConstant(mlir::Value value);
165159

166160
static constexpr llvm::StringRef getAdaptToByRefAttrName() {
167161
return "adapt.valuebyref";

flang/lib/Optimizer/CodeGen/CodeGen.cpp

Lines changed: 2 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -92,27 +92,10 @@ static mlir::Block *createBlock(mlir::ConversionPatternRewriter &rewriter,
9292
mlir::Region::iterator(insertBefore));
9393
}
9494

95-
/// Extract constant from a value if it is a result of one of the
96-
/// ConstantOp operations, otherwise, return std::nullopt.
97-
static std::optional<int64_t> getIfConstantIntValue(mlir::Value val) {
98-
if (!val || !val.dyn_cast<mlir::OpResult>())
99-
return {};
100-
101-
mlir::Operation *defop = val.getDefiningOp();
102-
103-
if (auto constOp = mlir::dyn_cast<mlir::arith::ConstantIntOp>(defop))
104-
return constOp.value();
105-
if (auto llConstOp = mlir::dyn_cast<mlir::LLVM::ConstantOp>(defop))
106-
if (auto attr = llConstOp.getValue().dyn_cast<mlir::IntegerAttr>())
107-
return attr.getValue().getSExtValue();
108-
109-
return {};
110-
}
111-
11295
/// Extract constant from a value that must be the result of one of the
11396
/// ConstantOp operations.
11497
static int64_t getConstantIntValue(mlir::Value val) {
115-
if (auto constVal = getIfConstantIntValue(val))
98+
if (auto constVal = fir::getIntIfConstant(val))
11699
return *constVal;
117100
fir::emitFatalError(val.getLoc(), "must be a constant");
118101
}
@@ -664,7 +647,7 @@ struct ConvertOpConversion : public fir::FIROpConversion<fir::ConvertOp> {
664647
<< " -> " << toTy;
665648

666649
// Do folding for constant inputs.
667-
if (auto constVal = getIfConstantIntValue(op0)) {
650+
if (auto constVal = fir::getIntIfConstant(op0)) {
668651
mlir::Value normVal =
669652
genConstantIndex(loc, toTy, rewriter, *constVal ? 1 : 0);
670653
rewriter.replaceOp(convert, normVal);

flang/lib/Optimizer/Dialect/FIROps.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3834,6 +3834,18 @@ bool fir::anyFuncArgsHaveAttr(mlir::func::FuncOp func, llvm::StringRef attr) {
38343834
return false;
38353835
}
38363836

3837+
std::optional<std::int64_t> fir::getIntIfConstant(mlir::Value value) {
3838+
if (auto *definingOp = value.getDefiningOp()) {
3839+
if (auto cst = mlir::dyn_cast<mlir::arith::ConstantOp>(definingOp))
3840+
if (auto intAttr = cst.getValue().dyn_cast<mlir::IntegerAttr>())
3841+
return intAttr.getInt();
3842+
if (auto llConstOp = mlir::dyn_cast<mlir::LLVM::ConstantOp>(definingOp))
3843+
if (auto attr = llConstOp.getValue().dyn_cast<mlir::IntegerAttr>())
3844+
return attr.getValue().getSExtValue();
3845+
}
3846+
return {};
3847+
}
3848+
38373849
mlir::Type fir::applyPathToType(mlir::Type eleTy, mlir::ValueRange path) {
38383850
for (auto i = path.begin(), end = path.end(); eleTy && i < end;) {
38393851
eleTy = llvm::TypeSwitch<mlir::Type, mlir::Type>(eleTy)

0 commit comments

Comments
 (0)