[flang-commits] [flang] [flang] Simplify hlfir.index in a few limited cases. (PR #161558)
via flang-commits
flang-commits at lists.llvm.org
Thu Oct 2 00:49:59 PDT 2025
================
@@ -2284,6 +2284,262 @@ class CmpCharOpConversion : public mlir::OpRewritePattern<hlfir::CmpCharOp> {
}
};
+static std::pair<mlir::Value, hlfir::AssociateOp>
+getVariable(fir::FirOpBuilder &builder, mlir::Location loc, mlir::Value val) {
+ // If it is an expression - create a variable from it, or forward
+ // the value otherwise.
+ hlfir::AssociateOp associate;
+ if (!mlir::isa<hlfir::ExprType>(val.getType()))
+ return {val, associate};
+ hlfir::Entity entity{val};
+ mlir::NamedAttribute byRefAttr = fir::getAdaptToByRefAttr(builder);
+ associate = hlfir::genAssociateExpr(loc, builder, entity, entity.getType(),
+ "", byRefAttr);
+ return {associate.getBase(), associate};
+}
+
+// Return character length if known at compile time. Unlike genCharLength
+// it does not create any new op as specifically is intended for analysis.
+// It is inspired by genLengthParameters that does the job for genCharLength.
+static std::optional<std::int64_t> getCharLengthIfConst(hlfir::Entity entity) {
+ if (!entity.isCharacter()) {
+ return std::nullopt;
+ }
+
+ if (mlir::isa<hlfir::ExprType>(entity.getType())) {
+ mlir::Value expr = entity;
+ if (auto reassoc = expr.getDefiningOp<hlfir::NoReassocOp>())
+ expr = reassoc.getVal();
+ // Going through fir::ExtendedValue would create a temp,
----------------
jeanPerier wrote:
Can you move the getCharLengthIfConst helper to HLFIRTools so that you can share the piece of code about hlfir::Expr with the similar one in `hlfir::genLengthParameters` for easier maintenance.
It is also a rather useful tool to have for other passes/analysis dealing with characters.
https://github.com/llvm/llvm-project/pull/161558
More information about the flang-commits
mailing list