Recent

Author Topic: Problem with optimization -O2  (Read 455 times)

devport

  • New Member
  • *
  • Posts: 26
Problem with optimization -O2
« on: January 09, 2020, 08:21:01 pm »
Hello,
I have a problem with the compiler.
I write quite extensive code and I encountered a problem with "optimization". If I use -O2 optimization, the problem occurs, if I don't use optimization, the problem is gone.

Maybe you will advise how to find out where the problem is?

A few tips I have noticed:
- There is a problem with the procedural procedure
- if I don't use the last third variable, it works fine
- if I use this variable, the data changed in this procedure do not affect the outside.

Now some code:
1. In file project_eth.lpr in the loop calling SocketProcess(data_buf) from ethernet.pas.
2. In SocketProcess calling ETH_Process from 117 line
3. ETH_Process calling ETH_Process_Frame from 99 line
4. ETH_Process_Frame calling ETH_Protocol_IP from ETH_Process_Frame (55 line)
5. ETH_Protocol_IP_TCP calling socket^.recv_func from line 341

socket^.recv_func it contains the address  server_recv80 procedure
the procedure is performed but has no effect on external variables.

if use variable "rcv_size" in server_recv80 procedure, that server_recv80 not change data with the help of SocketSendData(tcb_id, @snd_buff[0], size);  <<< ---- write the addres snd_buff and write size to TCB
  SocketClose(tcb_id);   <<< ---- Set flag = FLAG_FIN in TCB

I test this with code after calling this procedure in ethernet_ip_tcp.pas line 342

I no use optimization in FPC, all calling OK and code from 342 line is true.

my question is what makes the same code after -O2 optimization not work.
Or why if I use the -O2 optimization and comment the code from line 84-95, it also works correctly.

how to find it?

In lib directory asm in non -O2 version, and -O2 version

Link to sources : https://github.com/devport/LPC1114

Sorry for the english, written with the help of google translator
« Last Edit: January 09, 2020, 08:33:33 pm by devport »

Laksen

  • Hero Member
  • *****
  • Posts: 637
    • J-Software
Re: Problem with optimization -O2
« Reply #1 on: January 09, 2020, 08:47:48 pm »
Which compiler version are you using?

devport

  • New Member
  • *
  • Posts: 26
Re: Problem with optimization -O2
« Reply #2 on: January 09, 2020, 09:00:38 pm »
Free Pascal Compiler version 3.3.1

Compiler date      : 2019/12/22
Compiler CPU target: arm

No tested last sources..

julkas

  • Hero Member
  • *****
  • Posts: 537
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;

devport

  • New Member
  • *
  • Posts: 26
Re: Problem with optimization -O2
« Reply #4 on: January 09, 2020, 09:16:42 pm »
the difference is that I won't get any warning message
the code compiles correctly

I tested last trunk and the error still occurs.

For Target ARM source from
https://bugs.freepascal.org/view.php?id=36242
compiled no bugs

I tried to reproduce my problem on simple code, but everything works.
« Last Edit: January 09, 2020, 09:34:03 pm by devport »

julkas

  • Hero Member
  • *****
  • Posts: 537
  • KISS principle / Lazarus 2.0.6 / FPC 3.0.4
Re: Problem with optimization -O2
« Reply #5 on: January 10, 2020, 10:15:26 am »
Try following
1. Reduce number of local variables in each function and procedure.
2. Don't use Pascal strings - replace them with array of byte/char and PChar
3. Minimize System calls FillByte, Move, ...

Regards.
procedure mulu64(a, b: QWORD; out clo, chi: QWORD); assembler;
asm
  mov rax, a
  mov rdx, b
  mul rdx
  mov [clo], rax
  mov [chi], rdx
end;