| Issue |
236858
|
| Summary |
[flang] Compile time doubles with each level of nested nonconstant intrinsic calls (`abs(abs(...))`, CHARACTER `max(max(...))`)
|
| Labels |
slow-compile,
flang:semantics
|
| Assignees |
|
| Reporter |
eugeneepshteyn
|
Folding a nest of nonconstant intrinsic function references takes time exponential in the nesting depth. Each extra level doubles the compile time, so 24 levels of `abs` take about 25 s in `-fsyntax-only`. gfortran 13.3 compiles the same file in 0.02 s.
Reproducer (24 levels of `abs`):
```fortran
subroutine s(a, r)
integer :: a, r
r = abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(abs(a))))))))))))))))))))))))
end subroutine
```
```console
$ time flang -fc1 -fsyntax-only abs24.f90
```
Other depths, and the CHARACTER `MAX` form, come from this generator (`abs` or `cmax`, then the depth):
```python
import sys
kind, n = sys.argv[1], int(sys.argv[2])
if kind == "abs":
decl, args, e, w = "integer :: a, r", "a, r", "a", "abs(%s)"
else:
decl, args, e, w = "character(3) :: c, d, r", "c, d, r", "c", "max(%s, d)"
for _ in range(n):
e = w % e
print("subroutine s(%s)\n %s\n r = %s\nend subroutine" % (args, decl, e))
```
User CPU time of `flang -fc1 -fsyntax-only`, x86_64 Linux, release build of `main` at 703bc0de1e00:
| depth | 12 | 16 | 20 | 22 | 24 |
|---|---|---|---|---|---|
| integer `abs` | 0.01 s | 0.10 s | 1.75 s | 5.76 s | 24.8 s |
| `character(3)` `max(..., d)` | 0.01 s | 0.08 s | 1.18 s | 3.44 s | 16.6 s |
The same growth shows up with `sqrt`, real `abs`, `mod` and `adjustl`. Two-argument INTEGER and REAL `MAX`/`MIN` are not affected, and neither are operators such as unary minus.
**Cause.** Each argument is folded twice at each level:
1. [`FoldOperation(FoldingContext &, FunctionRef<T> &&)`](https://github.com/llvm/llvm-project/blob/703bc0de1e006192b19d9c5a9697019a28eb10ed/flang/lib/Evaluate/fold-implementation.h#L1370-L1393) folds every actual argument (`*expr = Fold(context, std::move(*expr))`, line 1390).
2. It then calls the intrinsic's folder. For elemental intrinsics that is `FoldElementalIntrinsic` → `GetConstantArguments`; for MIN/MAX it is `FoldMINorMAX`. Both reach [`Folder<T>::Folding(std::optional<ActualArgument> &)`](https://github.com/llvm/llvm-project/blob/703bc0de1e006192b19d9c5a9697019a28eb10ed/flang/lib/Evaluate/fold-implementation.h#L414-L416), which calls `Fold` on the same, already folded argument again (line 416).
When the argument is itself a nonconstant intrinsic reference, it is still a `FunctionRef` after folding, so the second `Fold` repeats the whole subtree: T(n) = 2 T(n-1). The backtrace shows the nesting (outermost frames last):
```text
Folder<Type<Integer,4>>::Folding(std::optional<ActualArgument>&)
FoldElementalIntrinsicHelper<ScalarFunc, Type<Integer,4>, Type<Integer,4>, 0>
FoldElementalIntrinsic<Type<Integer,4>, Type<Integer,4>>
FoldIntrinsicFunction<4>
FoldOperation<Type<Integer,4>>(FoldingContext&, FunctionRef<Type<Integer,4>>&&)
ExpressionBase<Type<Integer,4>>::Rewrite
ExpressionBase<SomeKind<Integer>>::Rewrite
ExpressionBase<SomeType>::Rewrite
Folder<Type<Integer,4>>::Folding(std::optional<ActualArgument>&)
FoldElementalIntrinsicHelper<...>
...
```
Counting calls to `FoldOperation<Type<Integer,4>>(FoldingContext&, FunctionRef&&)` under gdb on the reproducer gives exactly 2^(N+1) - N - 2:
| depth N | 8 | 9 | 10 | 11 |
|---|---|---|---|---|
| calls | 502 | 1013 | 2036 | 4083 |
Two-argument INTEGER/REAL `MAX`/`MIN` escape this because `FoldMINorMAX` rewrites them to `Extremum<T>`, which the outer refold walks only once. CHARACTER `MAX`/`MIN` keep the `FunctionRef`: `extremumAnyway` is false for CHARACTER ([line 1142](https://github.com/llvm/llvm-project/blob/703bc0de1e006192b19d9c5a9697019a28eb10ed/flang/lib/Evaluate/fold-implementation.h#L1142)).
With scalar operands, the allocation count stays flat as the depth grows, so the cost is repeated traversal, not copying. With array-element operands (`c(1)`, `c(2)`, ...) every traversal also refolds the subscripts, and depth 24 of CHARACTER `max` makes about 101 M allocations.
A possible direction: `Folder<T>::Folding` could skip its own `Fold` when `FoldOperation` has already folded the arguments, and fold only after a type conversion. I have not tried this.
(This issue was filed with the help of AI)
_______________________________________________
llvm-bugs mailing list
[email protected]
https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-bugs