[fpc-devel] Debugging Loop Unroll Optimization

Florian Klämpfl florian at freepascal.org
Thu May 17 18:34:04 CEST 2018

Am 17.05.2018 um 18:01 schrieb Martok:
> I think I found something, see below. Answering the question first ;-)
> Am 16.05.2018 um 19:20 schrieb Florian Klämpfl:
>> How big is the project? Normally, the number of unrolled loops is reasonable small so comparing the
>> generated assembler with and without unrolling should be feasible.According to cloc, 40kLOC, 10kLOC of which are DSP code, not counting packages
> that are not in the source repo. With the very spurious crashes, not the best
> test case...
> I did, however, do the insane thing and diff Lazarus (or rather, IDAs output).

Thanks for the analysis!

> There are a lot of unrolled loops in the LCL alone, but the code reordering for
> the first 15% or so that I checked looks fine. {Off-Topic: could parts of that
> variable rewriting be used for more aggressive inlining?}

No. Inlining does variable rewriting. If something is not inlined, there are other reasons.

> Some more testing with Lazarus and -gt has shown that at some variables are
> uninitialized with -Ooloopunroll. There is a fairly common case where removing
> the variable is not good:
> -------------
> for Result:= alow to ahigh do
>   if Something(Result) then exit;
> -------------
> If that gets unrolled, Result is undefined, and whatever it gets assigned to
> receives the random content of r/eax.

ISO7185: "After a for-statement is executed, other than being left by a goto-statement, the
control-variable shall be undefined"

"After the for statement terminates (provided this was not forced by a Break or an Exit procedure),
the value of counter is undefined."

So this shall not be unrolled, but is still error prone, if Result is not set after the regular end
of the for loop.

> function tfun: integer;
> begin
>   for Result:=1 to 10 do
>     s:=s+1;
> end;

This might get unrolled, Result as result is undefined after the loop.

More information about the fpc-devel mailing list