[fpc-devel] LLVM backend Pascal bindings?

2021-11-13 Thread Ryan Joseph via fpc-devel
As a fun weekend project I wanted to follow along with the tutorial at 
https://llvm.org/docs/tutorial/index.html but I noticed the API in in C++ so it 
wouldn't be possible to do this in Pascal without at least some plain C API. 

How did Free Pascal/Jonas accomplish this in the LLVM backend? 

Regards,
Ryan Joseph

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Optimisation and thread safety

2021-11-13 Thread J. Gareth Moreton via fpc-devel
I figured that if you read from memory, and then write to memory, it's 
not safe to read from the same memory block again because the write 
address could be equal to (or overlap) the read memory and it's 
impossible to rule that out if different registers are used. It might be 
okay if the references use the same registers (and remain constant) and 
only the offsets differ (and, again, there's no overlap), but otherwise 
it would only be safe when using %rsp, since I'm pretty sure the 
compiler does not use that for anything other than the stack pointer.


If it's the programmer's job to handle thread synchronisation, that 
alleviates some pressure.


Thanks for the insight, Florian and Michael.

Gareth aka. Kit

On 13/11/2021 10:54, Florian Klämpfl via fpc-devel wrote:



Am 13.11.2021 um 00:55 schrieb J. Gareth Moreton via fpc-devel 
:

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.

This is not possible as rsp could be used as normal register.

If the reference is not marked as being read volatile, you can change the 
second mov into movl %edx,%ea.

For further optimizations you have to track tai_tempalloc which we don’t yet.


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?

I am aware of other examples that require caution.  For example:

 movslq-608(%rbp),%rdx
 subl%eax,-84(%rbp,%rdx,4)
 movslq-608(%rbp),%rdx

Here it might be tempting to remove the second "movslq" instruction, but the 
value of %rdx could happen to be equal to -131, which would allow the subl instruction to 
modify -608(%rbp), and in this situation, it's quite likely if a malicious input is given 
to the program to manipulate the value stored at -608(%rbp) and invoke a buffer overrun.

It could be also a pointer aliasing the memory location or an array, so this is 
not safe in any case. So something like a[a[1234]] which might be valid code 
imo.


So in conclusion, theoretically, where is it perfectly safe to assume the value 
in memory hasn't changed, where would it be permissible only under -O4 and 
where must it not be optimised at all?

Gareth aka. Kit

P.S. Assembly examples were taken from the System unit.


--
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Optimisation and thread safety

2021-11-13 Thread Florian Klämpfl via fpc-devel


> Am 13.11.2021 um 00:55 schrieb J. Gareth Moreton via fpc-devel 
> :
> 
> 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. 

This is not possible as rsp could be used as normal register.

If the reference is not marked as being read volatile, you can change the 
second mov into movl %edx,%ea.

For further optimizations you have to track tai_tempalloc which we don’t yet.

> 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?
> 
> I am aware of other examples that require caution.  For example:
> 
> movslq-608(%rbp),%rdx
> subl%eax,-84(%rbp,%rdx,4)
> movslq-608(%rbp),%rdx
> 
> Here it might be tempting to remove the second "movslq" instruction, but the 
> value of %rdx could happen to be equal to -131, which would allow the subl 
> instruction to modify -608(%rbp), and in this situation, it's quite likely if 
> a malicious input is given to the program to manipulate the value stored at 
> -608(%rbp) and invoke a buffer overrun.

It could be also a pointer aliasing the memory location or an array, so this is 
not safe in any case. So something like a[a[1234]] which might be valid code 
imo.

> 
> So in conclusion, theoretically, where is it perfectly safe to assume the 
> value in memory hasn't changed, where would it be permissible only under -O4 
> and where must it not be optimised at all?
> 
> Gareth aka. Kit
> 
> P.S. Assembly examples were taken from the System unit.
> 
> 
> -- 
> This email has been checked for viruses by Avast antivirus software.
> https://www.avast.com/antivirus
> 
> ___
> fpc-devel maillist  -  fpc-devel@lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Optimisation and thread safety

2021-11-13 Thread Michael Van Canneyt via fpc-devel



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.___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel


Re: [fpc-devel] Cross-compiling and warnings from linker

2021-11-13 Thread Sergey Organov via fpc-devel
Jonas Maebe via fpc-devel
 writes:

> On 2021-11-12 10:20, Pierre Muller via fpc-devel wrote:
>> Le 10/11/2021 à 21:19, Sergey Organov via fpc-devel a écrit :
>>> Hello,
>>> Using cross-compiler from x86-linux to arm-linux, I keep getting a lot
>>> of warnings at the linking stage of my programs, in the form:
>>> /opt/[...]/arm-linux-gnueabihf-ld: warning: library search path 
>>> "/usr/lib/eject/" is unsafe for cross-compilation
>>> /opt/[...]/arm-linux-gnueabihf-ld: warning: warning: library search
>>> path "/usr/lib/console-setup/" is unsafe for cross-compilation
>>> [...]
>>> for virtually every sub-directory in /usr/lib. FPC 3.2.0 and 3.2.2 
>>> both
>>> have this issue.
>>> This makes me suspect FPC cross produces instructions for the linker 
>>> to
>>> search for files in wrong directories, where host libraries reside, that
>>> could lead to unpredictable results, and then tons of the warnings are
>>> really annoying.
>>> What's the way to fix this?
>>   Try to use -Xd compiler option
>> ~/bin$ fpc -h | grep -- -Xd
>>   -XdDo not search default library path (sometimes
>> required for cross-compiling when not using -XR)
>
> Or better: use -XR/path/to/cross-root. -Xd is a hack that predates the
> introduction of -XR.

Thanks, I believe I already do use -XR, here is entire compilation line:

/opt/cross/br/arm/armv7a/6.2.0.v6/usr/lib/fpc/3.2.0/ppcrossarm -O3 -CX -XX -Xs 
-veibq -vw-n-h- -MDelphiUnicode -Sg -Sc  
-XS -Tlinux -dCPU_ARM -Parm -CaEABIHF 
-XR/opt/cross/br/arm/armv7a/6.2.0.v6/usr/arm-javad-linux-gnueabihf/sysroot  
  
-XP/opt/cross/br/arm/armv7a/6.2.0.v6/usr/bin/arm-javad-linux-gnueabihf- 

-k-T/opt/cross/br/arm/armv7a/6.2.0.v6/usr/lib/ldscripts/armelf_linux_eabi.x 

-Fu/opt/cross/br/arm/armv7a/6.2.0.v6/usr/lib/fpc/3.2.0/units/arm-linux/*

-Fl/opt/cross/br/arm/armv7a/6.2.0.v6/usr/arm-javad-linux-gnueabihf/sysroot/usr/lib/
 -Fu./src
-Fu./generics.collections-master/src -Fu./lazutils -dJXT_PRCNTG -dJXTDB 
-dJXT_LNX -dJXT_RTPK -dCONSOLE  
-dSQLITE_LOAD_DYNAMICALLY -dCGGTTS -FU./build/arm-linux/obj/mkCGGTTS 
-o./build/arm-linux/bin/mkCGGTTS   
./src/mkCGGTTS.pas

and still I get all those warnings. Something's wrong with my command-line 
options?

Thanks,
-- Sergey Organov
___
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel