[fpc-devel] Optimisation and thread safety

Michael Van Canneyt michael at freepascal.org
Sat Nov 13 10:57:18 CET 2021



On Fri, 12 Nov 2021, J. Gareth Moreton via fpc-devel wrote:

> Hi everyone,
>
> I have a question when it comes to optimising memory reads and writes.  
> What are the rules for FPC when it comes to writing to memory and then 
> reading from it later within a single subroutine? For example, say I had 
> this pair of commands:
>
>     movq    %rdx,-584(%rbp)
>     movl    -584(%rbp),%eax
>
> That could easily be converted to "movl %edx,%eax", especially as %rbp 
> is likely pointing to the top of the stack.  But if the reference uses 
> different registers, would it still be safe to make this optimisation 
> given that the scheduler could suspend the thread in between the two 
> instructions and then another thread writes to the same memory block 
> before control is returned?

The user is responsible for synchronizing between threads, so I don't think
the compiler needs to take this into account ?

Michael.


More information about the fpc-devel mailing list