llvm / llvm/llvm-project

[Flang] Strength-reduce pow(base, IV) to multiplicative recurrence in loops

Open
#198,774 4 comments 0 reactions 0 assignees View on GitHub
flang
Dominant language
LLVM
Stars
40.5k
Forks
18.7k
PR merge metrics
PR metrics pending

Description

Flang does not optimize s**i — where s is loop-invariant and i is an integer induction variable — into a multiplicative recurrence. Each iteration emits a runtime call to `__powisf2@PLT`, which can be replaced with a single fmul per iteration.

Example
```
do i = 1, n
a(i) = s**i
end do
```

Current Behavior
Flang lowers s**i via fir::genPow() → runtime call on every iteration. For n = 1000, that's 1000 calls to FPowI, each performing O(log i) multiplications internally.

Expected Behavior
Under appropriate fast-math flags, the loop should become equivalent to:

```
temp = s
do i = 1, n
a(i) = temp
temp = temp * s
end do
```

This replaces ~50-200 cycle runtime calls with ~3-5 cycle fmul instructions.

Contributor guide

Open the contributing guide

Research direction

Start at fir::genPow() and trace how the Fortran loop in the example is lowered to the FPowI runtime call. Inspect the generated loop under appropriate fast-math flags, then verify that the repeated calls are replaced by a multiplicative recurrence with one fmul per iteration.

Written by the indexing model from the issue text.

Assessment

Tech stack
fortran
Domain
compilers, performance
Issue type
Bug
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
42/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.