Hello guys

Hello guys,

Does the Delphi compilers implement/use "Loop Unrolling" optimization? Or is it no longer necessary these days?

Thanks a lot :D

Comments

  1. I don't believe that compiler implements said optimisation

    ReplyDelete
  2. This is somehow relevant:
    http://stackoverflow.com/questions/24196076/is-gcc-loop-unrolling-flag-really-effective

    TLDR: You may gain some, you may lose some with unrolling loops. It depends on your data and your loop.

    ReplyDelete
  3. I think the convenience of correct looping in terms of readability and maintainability far outstrips any manual unrolling. As per the automatic optimization, even if implemented, it seems to me that it can't be applied in 99% of cases.

    ReplyDelete
  4. No harm putting in a QP to implement it though. I am entirely in favour of more compiler optimisations.

    ReplyDelete
  5. The x86 Delphi compiler does not unroll loops, and I guess the LLVM backend (for mobile targets) is not configured for this either. Unrolling was a good idea at Pentium 4 time, but modern x86/x64 architecture does not benefit from it, but for very specific kind of code. On the contrary, when optimizing manual asm code, I saw that unrolling was slower than an optimized rolled version, e.g. for SynCrypto's SHA-256. https://github.com/synopse/mORMot/blob/master/SynCrypto.pas
    So I do not regret that the Delphi compiler does not feature loop unrolling. It is easy to force it on purpose (as I do in SynCommons.pas).

    ReplyDelete
  6. BUT I regret that it does not optimize better the register assignment at code compilation. Register assignment is made globally for a whole function, not following local scope. In order to have efficient code, we have to write a separate function/procedure (e.g. one per loop), which adds a call - not always a good idea. And when you use function inlining, you have to take care of the generated asm (using Alt-F2 in the IDE), which may be worse once inlined, due to bad register assignment.

    ReplyDelete
  7. A. Bouchez In my sonewhat dated experience, register assignment is only an issue if it causes register spills. Otherwise register renaming by the CPU seems to mitigate any poor assignments.

    ReplyDelete
  8. Asbjørn Heid The issue I'm talking about is that stack is used instead of registers to store variables (e.g. redundant pointers or loop counters), since registers are affected for the whole function.

    ReplyDelete
  9. A. Bouchez I added a QC about this, (many years ago), for marking variables "var i : Integer register;" hinting (or forcing) the compiler to always put "i" into a register

    ReplyDelete
  10. A. Bouchez Right, that's what I meant by register spills, though I guess it's not technically the right term as the Delphi compiler frequently has registers to use, it just doesn't.

    ReplyDelete
  11. David Berneda A little known "hint"... list the local variables you'd like to enregister first... up to about the first 3-4 4-byte sized items. This works for the x86 compiler... the other CPU targets function differently.

    ReplyDelete
  12. I don't think the 64 bit compiler has this optimization.

    ReplyDelete

Post a Comment