Skip to content

Commit c9a63f4

Browse files
committed
move getCharLengthIfConst to HLFIRTools as requested. NFC
1 parent 4169187 commit c9a63f4

File tree

3 files changed

+69
-72
lines changed

3 files changed

+69
-72
lines changed

flang/include/flang/Optimizer/Builder/HLFIRTools.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,10 @@ void genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
324324
mlir::Value genCharLength(mlir::Location loc, fir::FirOpBuilder &builder,
325325
Entity entity);
326326

327+
/// Return character length if known at compile time. Unlike genCharLength
328+
/// it does not create any new op as specifically is intended for analysis.
329+
std::optional<std::int64_t> getCharLengthIfConst(Entity entity);
330+
327331
mlir::Value genRank(mlir::Location loc, fir::FirOpBuilder &builder,
328332
Entity entity, mlir::Type resultType);
329333

flang/lib/Optimizer/Builder/HLFIRTools.cpp

Lines changed: 62 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -676,6 +676,34 @@ mlir::Value hlfir::genLBound(mlir::Location loc, fir::FirOpBuilder &builder,
676676
return dimInfo.getLowerBound();
677677
}
678678

679+
static bool
680+
getExprLengthParameters(mlir::Value expr,
681+
llvm::SmallVectorImpl<mlir::Value> &result) {
682+
if (auto concat = expr.getDefiningOp<hlfir::ConcatOp>()) {
683+
result.push_back(concat.getLength());
684+
return true;
685+
}
686+
if (auto setLen = expr.getDefiningOp<hlfir::SetLengthOp>()) {
687+
result.push_back(setLen.getLength());
688+
return true;
689+
}
690+
if (auto elemental = expr.getDefiningOp<hlfir::ElementalOp>()) {
691+
result.append(elemental.getTypeparams().begin(),
692+
elemental.getTypeparams().end());
693+
return true;
694+
}
695+
if (auto evalInMem = expr.getDefiningOp<hlfir::EvaluateInMemoryOp>()) {
696+
result.append(evalInMem.getTypeparams().begin(),
697+
evalInMem.getTypeparams().end());
698+
return true;
699+
}
700+
if (auto apply = expr.getDefiningOp<hlfir::ApplyOp>()) {
701+
result.append(apply.getTypeparams().begin(), apply.getTypeparams().end());
702+
return true;
703+
}
704+
return false;
705+
}
706+
679707
void hlfir::genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
680708
Entity entity,
681709
llvm::SmallVectorImpl<mlir::Value> &result) {
@@ -688,29 +716,14 @@ void hlfir::genLengthParameters(mlir::Location loc, fir::FirOpBuilder &builder,
688716
// Going through fir::ExtendedValue would create a temp,
689717
// which is not desired for an inquiry.
690718
// TODO: make this an interface when adding further character producing ops.
691-
if (auto concat = expr.getDefiningOp<hlfir::ConcatOp>()) {
692-
result.push_back(concat.getLength());
693-
return;
694-
} else if (auto concat = expr.getDefiningOp<hlfir::SetLengthOp>()) {
695-
result.push_back(concat.getLength());
696-
return;
697-
} else if (auto asExpr = expr.getDefiningOp<hlfir::AsExprOp>()) {
719+
720+
if (auto asExpr = expr.getDefiningOp<hlfir::AsExprOp>()) {
698721
hlfir::genLengthParameters(loc, builder, hlfir::Entity{asExpr.getVar()},
699722
result);
700723
return;
701-
} else if (auto elemental = expr.getDefiningOp<hlfir::ElementalOp>()) {
702-
result.append(elemental.getTypeparams().begin(),
703-
elemental.getTypeparams().end());
704-
return;
705-
} else if (auto evalInMem =
706-
expr.getDefiningOp<hlfir::EvaluateInMemoryOp>()) {
707-
result.append(evalInMem.getTypeparams().begin(),
708-
evalInMem.getTypeparams().end());
709-
return;
710-
} else if (auto apply = expr.getDefiningOp<hlfir::ApplyOp>()) {
711-
result.append(apply.getTypeparams().begin(), apply.getTypeparams().end());
712-
return;
713724
}
725+
if (getExprLengthParameters(expr, result))
726+
return;
714727
if (entity.isCharacter()) {
715728
result.push_back(hlfir::GetLengthOp::create(builder, loc, expr));
716729
return;
@@ -733,6 +746,36 @@ mlir::Value hlfir::genCharLength(mlir::Location loc, fir::FirOpBuilder &builder,
733746
return lenParams[0];
734747
}
735748

749+
std::optional<std::int64_t> hlfir::getCharLengthIfConst(hlfir::Entity entity) {
750+
if (!entity.isCharacter()) {
751+
return std::nullopt;
752+
}
753+
if (mlir::isa<hlfir::ExprType>(entity.getType())) {
754+
mlir::Value expr = entity;
755+
if (auto reassoc = expr.getDefiningOp<hlfir::NoReassocOp>())
756+
expr = reassoc.getVal();
757+
758+
if (auto asExpr = expr.getDefiningOp<hlfir::AsExprOp>())
759+
return getCharLengthIfConst(hlfir::Entity{asExpr.getVar()});
760+
761+
llvm::SmallVector<mlir::Value> param;
762+
if (getExprLengthParameters(expr, param)) {
763+
assert(param.size() == 1 && "characters must have one length parameters");
764+
return fir::getIntIfConstant(param.pop_back_val());
765+
}
766+
return std::nullopt;
767+
}
768+
769+
// entity is a var
770+
if (mlir::Value len = tryGettingNonDeferredCharLen(entity))
771+
return fir::getIntIfConstant(len);
772+
auto charType =
773+
mlir::cast<fir::CharacterType>(entity.getFortranElementType());
774+
if (charType.hasConstantLen())
775+
return charType.getLen();
776+
return std::nullopt;
777+
}
778+
736779
mlir::Value hlfir::genRank(mlir::Location loc, fir::FirOpBuilder &builder,
737780
hlfir::Entity entity, mlir::Type resultType) {
738781
if (!entity.isAssumedRank())

flang/lib/Optimizer/HLFIR/Transforms/SimplifyHLFIRIntrinsics.cpp

Lines changed: 3 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -2298,57 +2298,6 @@ getVariable(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value val) {
22982298
return {associate.getBase(), associate};
22992299
}
23002300

2301-
// Return character length if known at compile time. Unlike genCharLength
2302-
// it does not create any new op as specifically is intended for analysis.
2303-
// It is inspired by genLengthParameters that does the job for genCharLength.
2304-
static std::optional<std::int64_t> getCharLengthIfConst(hlfir::Entity entity) {
2305-
if (!entity.isCharacter()) {
2306-
return std::nullopt;
2307-
}
2308-
2309-
if (mlir::isa<hlfir::ExprType>(entity.getType())) {
2310-
mlir::Value expr = entity;
2311-
if (auto reassoc = expr.getDefiningOp<hlfir::NoReassocOp>())
2312-
expr = reassoc.getVal();
2313-
// Going through fir::ExtendedValue would create a temp,
2314-
// which is not desired for an inquiry.
2315-
// TODO: make this an interface when adding further character producing ops.
2316-
if (auto concat = expr.getDefiningOp<hlfir::ConcatOp>()) {
2317-
return fir::getIntIfConstant(concat.getLength());
2318-
} else if (auto setLength = expr.getDefiningOp<hlfir::SetLengthOp>()) {
2319-
return fir::getIntIfConstant(setLength.getLength());
2320-
} else if (auto asExpr = expr.getDefiningOp<hlfir::AsExprOp>()) {
2321-
return getCharLengthIfConst(hlfir::Entity{asExpr.getVar()});
2322-
} else {
2323-
llvm::SmallVector<mlir::Value> param;
2324-
if (auto elemental = expr.getDefiningOp<hlfir::ElementalOp>()) {
2325-
param.append(elemental.getTypeparams().begin(),
2326-
elemental.getTypeparams().end());
2327-
} else if (auto evalInMem =
2328-
expr.getDefiningOp<hlfir::EvaluateInMemoryOp>()) {
2329-
param.append(evalInMem.getTypeparams().begin(),
2330-
evalInMem.getTypeparams().end());
2331-
} else if (auto apply = expr.getDefiningOp<hlfir::ApplyOp>()) {
2332-
param.append(apply.getTypeparams().begin(),
2333-
apply.getTypeparams().end());
2334-
} else {
2335-
return std::nullopt;
2336-
}
2337-
assert(param.size() == 1 && "characters must have one length parameters");
2338-
return fir::getIntIfConstant(param.pop_back_val());
2339-
}
2340-
}
2341-
2342-
if (auto varIface = entity.getMaybeDereferencedVariableInterface())
2343-
if (!varIface.getExplicitTypeParams().empty())
2344-
return fir::getIntIfConstant(varIface.getExplicitTypeParams()[0]);
2345-
auto charType =
2346-
mlir::cast<fir::CharacterType>(entity.getFortranElementType());
2347-
if (charType.hasConstantLen())
2348-
return charType.getLen();
2349-
return std::nullopt;
2350-
}
2351-
23522301
class IndexOpConversion : public mlir::OpRewritePattern<hlfir::IndexOp> {
23532302
public:
23542303
using mlir::OpRewritePattern<hlfir::IndexOp>::OpRewritePattern;
@@ -2372,7 +2321,8 @@ class IndexOpConversion : public mlir::OpRewritePattern<hlfir::IndexOp> {
23722321

23732322
auto resultTy = op.getType();
23742323
mlir::Value back = op.getBack();
2375-
auto substrLenCst = getCharLengthIfConst(hlfir::Entity{op.getSubstr()});
2324+
auto substrLenCst =
2325+
hlfir::getCharLengthIfConst(hlfir::Entity{op.getSubstr()});
23762326
if (!substrLenCst) {
23772327
return rewriter.notifyMatchFailure(
23782328
op, "substring length unknown at compile time");
@@ -2398,7 +2348,7 @@ class IndexOpConversion : public mlir::OpRewritePattern<hlfir::IndexOp> {
23982348
return mlir::success();
23992349
}
24002350

2401-
if (auto strLenCst = getCharLengthIfConst(strEntity)) {
2351+
if (auto strLenCst = hlfir::getCharLengthIfConst(strEntity)) {
24022352
if (*strLenCst < *substrLenCst) {
24032353
rewriter.replaceOp(op, builder.createIntegerConstant(loc, resultTy, 0));
24042354
return mlir::success();

0 commit comments

Comments
 (0)