Recent

Author Topic: Variable and Field Layout  (Read 2099 times)

PascalDragon

  • Hero Member
  • *****
  • Posts: 5446
  • Compiler Developer
Re: Variable and Field Layout
« Reply #15 on: December 19, 2023, 09:58:42 pm »
Your data being in the order you declare it isn't documented because it's very obvious and expected. This is also true of C++ and I'm willing to bet any other low level language. If it were to suddenly change there would be programmers rioting in the streets.

It is not documented as part of the languages and compilers will use this to optimize the stack layout if they see a benefit of it.

Why exactly do you think that data alignment is a well documented thing that programmers are aware of? Because data layout is an absolutely fundamental and critical aspect of code.

Data alignment is documented, because there are instructions that expect certain alignment. However the placement inside the stackframe or any padding between variables on the stack is not documented.

Same is also true for classes: the order of the fields and the padding between the fields is not documented. And the field reordering optimization that is enabled by default in -O4 uses this to - as the name suggests - reorder the fields. MSVC does something similiar. For record types however the order and padding is documented, so there such an optimization won't be used.

Which is why Thaddy suggests you to use a record. That is the only documented way to achieve a given order and padding. Anything else is an implementation detail and will change between platforms or even ABIs.

TL;DR: there will be no change for this, because everything here behaves as expected and designed.

Madoc

  • New Member
  • *
  • Posts: 48
Re: Variable and Field Layout
« Reply #16 on: December 20, 2023, 11:17:45 am »
It is not documented as part of the languages and compilers will use this to optimize the stack layout if they see a benefit of it.

Sure, stack space is precious, aligning and packing data is beneficial and these optimisations are to be expected, I have no issue with this. The problem is my data is already aligned and optimised for cache access, but the compiler is changing my declared order for no reason, while also undoing my optimisations.

Data alignment is documented, because there are instructions that expect certain alignment. However the placement inside the stackframe or any padding between variables on the stack is not documented.

Well, on Intel at least, only some SIMD vector loads require alignment and I'm pretty sure these aren't used by FPC. They're hardly used by anyone these days as they have very little benefit on anything but old CPUs and might raise an exception. Aligning data is still very benerficial for performance regardless of instructions that require it of course, but it's not the only concern and the compiler doesn't know what actual memory access patterns of larger data might be.

I'm glad at least class field re-ordering is optional as this would break so much of my code, but I can't say I'm happy the compiler is making me jump through hoops with ugly code and abitrarily undoing my optimised data layouts.

Delphi / Pascal's lack of C style strict aliasing is definitely one of its biggest stengths and reasons I love the language. For someone like me who does important low level optimisations around this rather than caring about some minor ineffectual compiler optimisation it's huge. Obviously tight control over data layouts is critical for this sort of thing to work.


I realise my usage of the language is somewhat atypical and low level optimisation is something programmers concern themselves with less and less. I'm in the process of learning to use FPC so I ask annoying questions and bring up what I consider to be issues that maybe are not a concern to others, but I am loving FPC so far and really appreciate many of its features. I first took interest in it several years ago, but it lacked features I rely on. I am impressed with the progress that has been done since.

Thaddy

  • Hero Member
  • *****
  • Posts: 14201
  • Probably until I exterminate Putin.
Re: Variable and Field Layout
« Reply #17 on: December 20, 2023, 02:54:59 pm »
An intermediate report.

When declaring records there is now an align post-fix that guarantees an aligned start of a record too. This also works on local variables, i.e. stack declared if possible.

Due to the holidays I am still not able to write a convincing example for you because I am still on Tizen but when I come back home for Christmas I will add it here.
Edit:
Code: Pascal  [Select][+][-]
  1. type
  2.    TSomeRecord = packed record
  3.         member1, member2, member3:single;
  4.     end align 16;
or something close to that. Not able to test as I wrote. There is a test in the test suite for FPC (the TWXXX ones)
Note that the outer array should be perfectly fine.

One of my friends helped the Tizen issue partially.
« Last Edit: December 20, 2023, 04:09:17 pm by Thaddy »
Specialize a type, not a var.

 

TinyPortal © 2005-2018