Skip to content

Commit

Permalink
[flang][NFC] Move genMaxWithZero into fir:::factory
Browse files Browse the repository at this point in the history
Move tthe function to allow its usage in the Optimizer/Builder functions.

This patch is part of the upstreaming effort from fir-dev branch.

Reviewed By: jeanPerier

Differential Revision: https://reviews.llvm.org/D127295
  • Loading branch information
jeanPerier authored and clementval committed Jun 8, 2022
1 parent 37028d3 commit d91735b
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 23 deletions.
4 changes: 0 additions & 4 deletions flang/include/flang/Lower/ConvertExpr.h
Expand Up @@ -232,10 +232,6 @@ inline mlir::NamedAttribute getAdaptToByRefAttr(fir::FirOpBuilder &builder) {
builder.getUnitAttr()};
}

/// Generate max(\p value, 0) where \p value is a scalar integer.
mlir::Value genMaxWithZero(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value value);

} // namespace Fortran::lower

#endif // FORTRAN_LOWER_CONVERTEXPR_H
4 changes: 4 additions & 0 deletions flang/include/flang/Optimizer/Builder/FIRBuilder.h
Expand Up @@ -542,6 +542,10 @@ mlir::Value createZeroValue(fir::FirOpBuilder &builder, mlir::Location loc,
/// Unwrap integer constant from an mlir::Value.
llvm::Optional<std::int64_t> getIntIfConstant(mlir::Value value);

/// Generate max(\p value, 0) where \p value is a scalar integer.
mlir::Value genMaxWithZero(fir::FirOpBuilder &builder, mlir::Location loc,
mlir::Value value);

} // namespace fir::factory

#endif // FORTRAN_OPTIMIZER_BUILDER_FIRBUILDER_H
16 changes: 2 additions & 14 deletions flang/lib/Lower/ConvertExpr.cpp
Expand Up @@ -839,7 +839,7 @@ class ScalarExprLowering {
mlir::Value rawLen = fir::getBase(genval(*lengthExpr));
// F2018 7.4.4.2 point 5.
funcPtrResultLength =
Fortran::lower::genMaxWithZero(builder, getLoc(), rawLen);
fir::factory::genMaxWithZero(builder, getLoc(), rawLen);
}
}
if (!funcPtrResultLength)
Expand Down Expand Up @@ -2223,7 +2223,7 @@ class ScalarExprLowering {
type->characterTypeSpec().length().GetExplicit()) {
mlir::Value len = fir::getBase(genval(*lenExpr));
// F2018 7.4.4.2 point 5.
len = Fortran::lower::genMaxWithZero(builder, getLoc(), len);
len = fir::factory::genMaxWithZero(builder, getLoc(), len);
symMap.addSymbol(*arg,
replaceScalarCharacterLength(gen(*expr), len));
continue;
Expand Down Expand Up @@ -7588,15 +7588,3 @@ void Fortran::lower::createArrayMergeStores(
esp.resetBindings();
esp.incrementCounter();
}

mlir::Value Fortran::lower::genMaxWithZero(fir::FirOpBuilder &builder,
mlir::Location loc,
mlir::Value value) {
mlir::Value zero = builder.createIntegerConstant(loc, value.getType(), 0);
if (mlir::Operation *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() < 0 ? zero : value;
return Fortran::lower::genMax(builder, loc,
llvm::SmallVector<mlir::Value>{value, zero});
}
10 changes: 5 additions & 5 deletions flang/lib/Lower/ConvertVariable.cpp
Expand Up @@ -1090,7 +1090,7 @@ static mlir::Value computeExtent(fir::FirOpBuilder &builder, mlir::Location loc,
auto diff = builder.create<mlir::arith::SubIOp>(loc, idxTy, ub, lb);
mlir::Value one = builder.createIntegerConstant(loc, idxTy, 1);
auto rawExtent = builder.create<mlir::arith::AddIOp>(loc, idxTy, diff, one);
return Fortran::lower::genMaxWithZero(builder, loc, rawExtent);
return fir::factory::genMaxWithZero(builder, loc, rawExtent);
}

/// Lower explicit lower bounds into \p result. Does nothing if this is not an
Expand Down Expand Up @@ -1145,7 +1145,7 @@ lowerExplicitExtents(Fortran::lower::AbstractConverter &converter,
mlir::Value ub = builder.createConvert(
loc, idxTy, genScalarValue(converter, loc, expr, symMap, stmtCtx));
if (lowerBounds.empty())
result.emplace_back(Fortran::lower::genMaxWithZero(builder, loc, ub));
result.emplace_back(fir::factory::genMaxWithZero(builder, loc, ub));
else
result.emplace_back(
computeExtent(builder, loc, lowerBounds[spec.index()], ub));
Expand Down Expand Up @@ -1173,7 +1173,7 @@ lowerExplicitCharLen(Fortran::lower::AbstractConverter &converter,
if (llvm::Optional<Fortran::lower::SomeExpr> lenExpr = box.getCharLenExpr())
// If the length expression is negative, the length is zero. See F2018
// 7.4.4.2 point 5.
return Fortran::lower::genMaxWithZero(
return fir::factory::genMaxWithZero(
builder, loc,
genScalarValue(converter, loc, *lenExpr, symMap, stmtCtx));
return mlir::Value{};
Expand Down Expand Up @@ -1338,7 +1338,7 @@ void Fortran::lower::mapSymbolAttributes(
Fortran::lower::SomeExpr highEx{*high};
mlir::Value ub = genValue(highEx);
ub = builder.createConvert(loc, idxTy, ub);
shapes.emplace_back(genMaxWithZero(builder, loc, ub));
shapes.emplace_back(fir::factory::genMaxWithZero(builder, loc, ub));
} else if (spec->ubound().isColon()) {
assert(box && "assumed bounds require a descriptor");
mlir::Value dim =
Expand Down Expand Up @@ -1409,7 +1409,7 @@ void Fortran::lower::mapSymbolAttributes(
mlir::Value rawLen = genValue(*charLen);
// If the length expression is negative, the length is zero. See
// F2018 7.4.4.2 point 5.
return genMaxWithZero(builder, loc, rawLen);
return fir::factory::genMaxWithZero(builder, loc, rawLen);
};

ba.match(
Expand Down
14 changes: 14 additions & 0 deletions flang/lib/Optimizer/Builder/FIRBuilder.cpp
Expand Up @@ -1216,3 +1216,17 @@ llvm::Optional<std::int64_t> fir::factory::getIntIfConstant(mlir::Value value) {
return intAttr.getInt();
return {};
}

mlir::Value fir::factory::genMaxWithZero(fir::FirOpBuilder &builder,
mlir::Location loc,
mlir::Value value) {
mlir::Value zero = builder.createIntegerConstant(loc, value.getType(), 0);
if (mlir::Operation *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() > 0 ? value : zero;
mlir::Value valueIsGreater = builder.create<mlir::arith::CmpIOp>(
loc, mlir::arith::CmpIPredicate::sgt, value, zero);
return builder.create<mlir::arith::SelectOp>(loc, valueIsGreater, value,
zero);
}

0 comments on commit d91735b

Please sign in to comment.