Recent

Author Topic: [CLOSED] FOR loop unrolling  (Read 7911 times)

julkas

  • Guest
[CLOSED] FOR loop unrolling
« on: December 20, 2019, 05:01:03 pm »
When compiler unroll loops?
Is it general behavior or ... ?
« Last Edit: December 22, 2019, 11:49:09 am by julkas »

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: FOR loop unrolling
« Reply #1 on: December 20, 2019, 05:04:55 pm »
 As with any compiler that supports it: what is the complexity of the loop?
A loop of 5-6 iterations may be unrolled but 10000 iterations is silly.
On small loops - with a determinate finish - FPC does an excellent job.
« Last Edit: December 20, 2019, 05:11:24 pm by Thaddy »
But I am sure they don't want the Trumps back...

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 12110
  • FPC developer.
Re: FOR loop unrolling
« Reply #2 on: December 20, 2019, 05:19:55 pm »
Unroll till 1500 muops ?

julkas

  • Guest
Re: FOR loop unrolling
« Reply #3 on: December 20, 2019, 05:22:22 pm »
And how disable / enable loop unrolling in code?
I compile with -O3 option.
Can be unrolled while and repeat loops or only for?
« Last Edit: December 20, 2019, 05:34:51 pm by julkas »

Thaddy

  • Hero Member
  • *****
  • Posts: 16653
  • Kallstadt seems a good place to evict Trump to.
Re: FOR loop unrolling
« Reply #4 on: December 20, 2019, 06:08:29 pm »
And how disable / enable loop unrolling in code?
I compile with -O3 option.
Can be unrolled while and repeat loops or only for?
-OoLOOPUNROLL-
But I am sure they don't want the Trumps back...

Handoko

  • Hero Member
  • *****
  • Posts: 5396
  • My goal: build my own game engine using Lazarus
Re: FOR loop unrolling
« Reply #5 on: December 20, 2019, 06:10:24 pm »
According to the documentations:
https://wiki.freepascal.org/Optimization
https://www.freepascal.org/docs-html/current/prog/progsu58.html

if (-O3 or -O4) and (FPC version >= 3.04) then loop unrolling on

or you can manually turn it on by

{$OPTIMIZATION LOOPUNROLL}
« Last Edit: December 20, 2019, 06:11:58 pm by Handoko »

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: FOR loop unrolling
« Reply #6 on: December 20, 2019, 07:05:43 pm »
Can be unrolled while and repeat loops or only for?

AFAIK only FOR loops are candidates to be unrolled.
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

julkas

  • Guest
Re: FOR loop unrolling
« Reply #7 on: December 20, 2019, 07:24:35 pm »
Can be unrolled while and repeat loops or only for?

AFAIK only FOR loops are candidates to be unrolled.
Yes . AVR compiler unrolls simple for loops (number iterations - 30) but not while loops.

julkas

  • Guest
Re: FOR loop unrolling
« Reply #8 on: December 20, 2019, 07:33:13 pm »
According to the documentations:
https://wiki.freepascal.org/Optimization
https://www.freepascal.org/docs-html/current/prog/progsu58.html

if (-O3 or -O4) and (FPC version >= 3.04) then loop unrolling on

or you can manually turn it on by

{$OPTIMIZATION LOOPUNROLL}
Ok. Assume I have LOOPUNROLL enabled, how disable unroll.
I want manually control UNROLL in my code.

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: FOR loop unrolling
« Reply #9 on: December 20, 2019, 07:53:50 pm »
Code: Pascal  [Select][+][-]
  1. {$optimization noloopunroll}

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: FOR loop unrolling
« Reply #10 on: December 20, 2019, 08:07:11 pm »
Code: Pascal  [Select][+][-]
  1. {$optimization noloopunroll}

Does that even exists? AFAICT the only recognized $optimization arguments (beyond ON/OFF) are these, from compiler/globtypes.pas:

Code: Pascal  [Select][+][-]
  1. const
  2.        OptimizerSwitchStr : array[toptimizerswitch] of string[17] = ('',
  3.          'LEVEL1','LEVEL2','LEVEL3',
  4.          'REGVAR','UNCERTAIN','SIZE','STACKFRAME',
  5.          'PEEPHOLE','ASMCSE','LOOPUNROLL','TAILREC','CSE',
  6.          'DFA','STRENGTH','SCHEDULE','AUTOINLINE','USEEBP','USERBP',
  7.          'ORDERFIELDS','FASTMATH','DEADVALUES','REMOVEEMPTYPROCS',
  8.          'CONSTPROP',
  9.          'DEADSTORE','FORCENOSTACKFRAME'
  10.        );
« Last Edit: December 20, 2019, 08:09:08 pm by lucamar »
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

Laksen

  • Hero Member
  • *****
  • Posts: 794
    • J-Software
Re: FOR loop unrolling
« Reply #11 on: December 20, 2019, 08:12:53 pm »
Looks like that isn't documented, but adding 'NO' in front of an optimization will turn it off. The same way as it's done on the commandline(this is documented)

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: FOR loop unrolling
« Reply #12 on: December 20, 2019, 08:33:32 pm »
Looks like that isn't documented, but adding 'NO' in front of an optimization will turn it off. The same way as it's done on the commandline(this is documented)

Thanks. Found it (at last, and only for the command tail) in Appendix A of the User Guide:

Quote
-O<x>  Optimizations:
      [...]
      -Oo[NO]<x> Enable or disable optimizations; see fpc -i or fpc -io for possible values

So yes, adding NO in front of a $optimization argument should work.

Thanks again for the tip, Laksen :)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

valdir.marcos

  • Hero Member
  • *****
  • Posts: 1106
Re: FOR loop unrolling
« Reply #13 on: December 21, 2019, 08:02:04 am »
Unroll till 1500 muops?
What muops stands for?

Hartmut

  • Hero Member
  • *****
  • Posts: 893
Re: FOR loop unrolling
« Reply #14 on: December 21, 2019, 10:27:10 am »
I found this topic but had no idea, what "Loop unrolling" could be. The provided links of the FPC documentation
Quote
https://wiki.freepascal.org/Optimization
https://www.freepascal.org/docs-html/current/prog/progsu58.html
only tell the syntax, how to enable/disable this function, but not what it means.

After some research I found https://en.wikipedia.org/wiki/Loop_unrolling which explains this method in detail. Maybe this helps other readers.

 

TinyPortal © 2005-2018