[flang-commits] [flang] [Flang] Compute elemental character MIN/MAX result length in HLFIR (PR #189464)
via flang-commits
flang-commits at lists.llvm.org
Tue Mar 31 01:38:00 PDT 2026
================
@@ -2758,10 +2758,37 @@ class ElementalIntrinsicCallBuilder
intrinsic->name == "merge")
return loweredActuals[0].value().genCharLength(
callContext.loc, callContext.getBuilder());
- // Character MIN/MAX is the min/max of the arguments length that are
- // present.
- TODO(callContext.loc,
- "compute elemental character min/max function result length in HLFIR");
+ // Character MIN/MAX result length is the length of the longest
+ // argument that is present.
+ assert(intrinsic &&
+ (intrinsic->name == "min" || intrinsic->name == "max") &&
+ "unexpected elemental intrinsic with character result");
+ fir::FirOpBuilder &builder = callContext.getBuilder();
+ mlir::Location loc = callContext.loc;
+ mlir::Value resultLength;
+ for (auto &preparedActual : loweredActuals) {
+ if (!preparedActual)
+ continue;
+ mlir::Value argLen = preparedActual->genCharLength(loc, builder);
----------------
jeanPerier wrote:
`genCharLength` cannot be safely called on argument that may be absent (the descriptor may not be readable if it needs to be read for assumed length). You will need to generate fir.if with `builder.genIfOp(...` and do the `genCharLength` inside the then branch instead of using a select when `preparedActual->handleDynamicOptional()` is true.
This is likely segfaulting with your patch:
```
module m
contains
subroutine foo(res, x1, x2, x3)
character(*), dimension(:) :: res, x1, x2, x3
optional :: x3
res = max(x1, x2, x3)
end subroutine
end module
use m, only : foo
character(4), dimension(2) :: res, x1, x2
x1 = ["abcd", "efgh"]
x2 = ["ijkl", "mnop"]
call foo(res, x1, x2)
print *, res
end
```
https://github.com/llvm/llvm-project/pull/189464
More information about the flang-commits
mailing list