Recent

Author Topic: Two compiler issues detected - advise needed  (Read 2038 times)

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Two compiler issues detected - advise needed
« on: September 06, 2023, 09:01:05 am »
Hello all,

First things first

- machine: Intel Core i5 M430 (Nehalem architecture) @ 2.266 GHz, 4 GByte RAM
- system: Windows 7 Home Edition, Service Pack 1, 64 bit
- compiler: FPC 3.2.2 x86_64 in 64 bit mode

I have implemented some larger algebra stuff and discovered two compiler issues (I think)

* an assert in the code, when compiled with {$assertions on}, leads to a call of a sub-function two source lines later with an incorrect value handed over (and a subsequent SIGSEGV) - only happens with -O2 or higher optimisation settings
** I inspected the generated asm and it looks like the compiler is loosing track of register allocation
** my current work-around is to comment the assert out and everything works ok under all optimisation levels then

* a function declaration as inline leads to incorrect calculations - happens on all optimisation level settings
** I have not drilled down this one further, as the inlined function has ~50 LOC and following that in asm is a bit tedious
** current work-around - remove inline decoration and all computations work correct

Bringing the two above to minimum compilable demos will take me a couple of days (if feasible at all), and the result will still have a couple of 1.000 LOC in both cases. I'm willing to spent the effort if this helps. However I have two questions on this

* are the compiler devs aware of such/similar issue
** maybe working on it already, or
** in desperate hunt for a reproducible demo, to inspect this

* can somebody help me generate a ticket, once I have created a minimal compilable demo?

Cheers,
MathMan

AlexTP

  • Hero Member
  • *****
  • Posts: 2418
    • UVviewsoft
Re: Two compiler issues detected - advise needed
« Reply #1 on: September 06, 2023, 09:55:46 am »
Please provide full compilable examples (small) to reproduce.
Please then try the same on FPC 'latest' Git.
« Last Edit: September 06, 2023, 09:57:57 am by AlexTP »

Fibonacci

  • Sr. Member
  • ****
  • Posts: 438
Re: Two compiler issues detected - advise needed
« Reply #2 on: September 06, 2023, 10:06:00 am »
* a function declaration as inline leads to incorrect calculations - happens on all optimisation level settings
** I have not drilled down this one further, as the inlined function has ~50 LOC and following that in asm is a bit tedious
** current work-around - remove inline decoration and all computations work correct

As for inline, here is a litte demo

Code: Pascal  [Select][+][-]
  1. function is_int_even(i: integer): boolean; inline;
  2. begin
  3.   i := i mod 2; //here internal copy of "i" is actualy used byref instead of byval
  4.   result := i=0;
  5. end;
  6.  
  7. procedure intshow; inline;
  8. var
  9.   i: integer;
  10. begin
  11.   i := 10;
  12.  
  13.   if is_int_even(i) then begin
  14.     i := i*10;
  15.  
  16.     writeln('i = ', i); //prints 0, expected 10*10=100
  17.   end;
  18.  
  19.   readln;
  20. end;
  21.  
  22. begin
  23.   intshow;
  24. end.
  25.  

The bug is reported but nothing is done about it. Actually the patch was submitted, but not merged.
« Last Edit: September 06, 2023, 10:12:38 am by Fibonacci »

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 9991
  • Debugger - SynEdit - and more
    • wiki
Re: Two compiler issues detected - advise needed
« Reply #3 on: September 06, 2023, 10:16:35 am »
* an assert in the code, when compiled with {$assertions on}, leads to a call of a sub-function two source lines later with an incorrect value handed over (and a subsequent SIGSEGV) - only happens with -O2 or higher optimisation settings
** I inspected the generated asm and it looks like the compiler is loosing track of register allocation
** my current work-around is to comment the assert out and everything works ok under all optimisation levels then

There has been an issue that matches this description, it is fixed in 3.2.3.
It is not related to the assert. I guess the assert just happens to generate the asm for which the issue occurs.


Afaik one or two other bugs in the code generator were fixed. So I would advice to test with 3.2.3.




MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Two compiler issues detected - advise needed
« Reply #4 on: September 06, 2023, 10:35:11 am »
* an assert in the code, when compiled with {$assertions on}, leads to a call of a sub-function two source lines later with an incorrect value handed over (and a subsequent SIGSEGV) - only happens with -O2 or higher optimisation settings
** I inspected the generated asm and it looks like the compiler is loosing track of register allocation
** my current work-around is to comment the assert out and everything works ok under all optimisation levels then

There has been an issue that matches this description, it is fixed in 3.2.3.
It is not related to the assert. I guess the assert just happens to generate the asm for which the issue occurs.


Afaik one or two other bugs in the code generator were fixed. So I would advice to test with 3.2.3.

Thanks Martin - sounds promising. Can I get a compiled / installable 3.2.3 from somewhere? I'm a "conservative" user of releases who never tinkered with Git or self compiling FPC or Lazarus - hell, I don't even have Internet access at home  :-X

AlexTP

  • Hero Member
  • *****
  • Posts: 2418
    • UVviewsoft
Re: Two compiler issues detected - advise needed
« Reply #5 on: September 06, 2023, 10:37:09 am »
For such conservative users, fpcUpDeluxe program exists, it allows to install FPC 'latest' with 3 clicks. But with internet access.

Zvoni

  • Hero Member
  • *****
  • Posts: 2364
Re: Two compiler issues detected - advise needed
« Reply #6 on: September 06, 2023, 10:44:57 am »
As for inline, here is a litte demo

Code: Pascal  [Select][+][-]
  1. function is_int_even(i: integer): boolean; inline;
  2. begin
  3.   i := i mod 2; //here internal copy of "i" is actualy used byref instead of byval
  4.   result := i=0;
  5. end;
  6.  
  7. procedure intshow; inline;
  8. var
  9.   i: integer;
  10. begin
  11.   i := 10;
  12.  
  13.   if is_int_even(i) then begin
  14.     i := i*10;
  15.  
  16.     writeln('i = ', i); //prints 0, expected 10*10=100
  17.   end;
  18.  
  19.   readln;
  20. end;
  21.  
  22. begin
  23.   intshow;
  24. end.
  25.  

The bug is reported but nothing is done about it. Actually the patch was submitted, but not merged.
I'm not even sure this is a bug. At least not how i understand inline to work:
The way i understand how inline works would result in (Aircode!!):
Code: Pascal  [Select][+][-]
  1. procedure intshow; inline;
  2. var
  3.   i: integer;
  4. begin
  5.   i := 10;
  6.  
  7.   //if is_int_even(i) then begin
  8.   i:=i mod 2
  9.   If i=0 Then Begin
  10.     i := i*10;  // 0 times Something is still 0
  11.  
  12.     writeln('i = ', i); //prints 0, expected 10*10=100
  13.   end;
  14.  
  15.   readln;
  16. end;

EDIT: I finally got your point.
Yes, looks like a bug (handled as "var/out"-Parameter)

Do you get the same "result" if you change as follows:
Code: Pascal  [Select][+][-]
  1. function is_int_even(i: integer): boolean; inline;
  2. begin
  3.   result :=  (i mod 2=0);
  4. end;
« Last Edit: September 06, 2023, 10:52:46 am by Zvoni »
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Two compiler issues detected - advise needed
« Reply #7 on: September 06, 2023, 10:48:36 am »
* a function declaration as inline leads to incorrect calculations - happens on all optimisation level settings
** I have not drilled down this one further, as the inlined function has ~50 LOC and following that in asm is a bit tedious
** current work-around - remove inline decoration and all computations work correct

As for inline, here is a litte demo

<snip>

Thanks Fibonacci - yes, such a byref vs byval issue could definitely happen in my case too (top-of-the-head statement - I don't have the sources with me, but if memory serves ...)

Fibonacci

  • Sr. Member
  • ****
  • Posts: 438
Re: Two compiler issues detected - advise needed
« Reply #8 on: September 06, 2023, 10:54:06 am »
I'm not even sure this is a bug. At least not how i understand inline to work:
The way i understand how inline works would result in (Aircode!!):
Code: Pascal  [Select][+][-]
  1. procedure intshow; inline;
  2. var
  3.   i: integer;
  4. begin
  5.   i := 10;
  6.  
  7.   //if is_int_even(i) then begin
  8.   i:=i mod 2
  9.   If i=0 Then Begin
  10.     i := i*10;  // 0 times Something is still 0
  11.  
  12.     writeln('i = ', i); //prints 0, expected 10*10=100
  13.   end;
  14.  
  15.   readln;
  16. end;

So you understand it wrong. Its not a "macro" that just "paste" the code where it was called.

I have my FPC patched, just 1 line added that fixes this particular issue.

@MathMan: I give 90% chances this is it. If you compile FPC with fpcupdeluxe, goto Setup+ and add FPC patch, it's in the attachment. Build 3.3.1 version.

EDIT: I finally got your point.
Yes, looks like a bug (handled as "var/out"-Parameter)

Do you get the same "result" if you change as follows:
Code: Pascal  [Select][+][-]
  1. function is_int_even(i: integer): boolean; inline;
  2. begin
  3.   result :=  (i mod 2=0);
  4. end;

No, the bug doesnt exists in this case. The variable must be modified.
« Last Edit: September 06, 2023, 10:56:18 am by Fibonacci »

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Two compiler issues detected - advise needed
« Reply #9 on: September 06, 2023, 11:19:24 am »
For such conservative users, fpcUpDeluxe program exists, it allows to install FPC 'latest' with 3 clicks. But with internet access.

AlexTP - I'm fully aware of FpcUpDeluxe. But as said I do not have Internet access @ home, and I'm not allowed to bring my private PCs to the office. So unfortunately no option for me.

I'm just a hobby Pascal programmer - I don't do this for a living. Maybe this explains why I asked initially if this is already known - I'm willing to help, but there are limits to what I can do.

Zvoni

  • Hero Member
  • *****
  • Posts: 2364
Re: Two compiler issues detected - advise needed
« Reply #10 on: September 06, 2023, 11:24:17 am »
AlexTP - I'm fully aware of FpcUpDeluxe. But as said I do not have Internet access @ home, and I'm not allowed to bring my private PCs to the office. So unfortunately no option for me.
Err..... buy a WiFi-USB-Dongle, plug it into your Desktop-PC, open a Hotspot on your Smartphone, connect your PC via WiFi-Dongle to your Smartphone......
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Two compiler issues detected - advise needed
« Reply #11 on: September 06, 2023, 12:07:13 pm »
AlexTP - I'm fully aware of FpcUpDeluxe. But as said I do not have Internet access @ home, and I'm not allowed to bring my private PCs to the office. So unfortunately no option for me.
Err..... buy a WiFi-USB-Dongle, plug it into your Desktop-PC, open a Hotspot on your Smartphone, connect your PC via WiFi-Dongle to your Smartphone......

Hm - somehow I managed to get myself in a defensive position again, not knowing how. Zvoni - you may believe it or not, but beside buying the WiFi USB dongle none of the following is feasible for me, sorry.

Zvoni

  • Hero Member
  • *****
  • Posts: 2364
Re: Two compiler issues detected - advise needed
« Reply #12 on: September 06, 2023, 01:08:28 pm »
Hm - somehow I managed to get myself in a defensive position again, not knowing how. Zvoni - you may believe it or not, but beside buying the WiFi USB dongle none of the following is feasible for me, sorry.
No Smartphone (Internet-Capable) at all?
No Smartphone with HotSpot-Option?

Huh? I'm doing it all the time, when i travel around, and need urgent Internet-Access (i use my iPad for HotSpot)
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

MathMan

  • Sr. Member
  • ****
  • Posts: 325
Re: Two compiler issues detected - advise needed
« Reply #13 on: September 06, 2023, 01:51:20 pm »
Hm - somehow I managed to get myself in a defensive position again, not knowing how. Zvoni - you may believe it or not, but beside buying the WiFi USB dongle none of the following is feasible for me, sorry.
No Smartphone (Internet-Capable) at all?
No Smartphone with HotSpot-Option?

Huh? I'm doing it all the time, when i travel around, and need urgent Internet-Access (i use my iPad for HotSpot)

I know, hard to believe, but in fact - no smartphone at all. Even if, my flat is in a low enery building with high shielding, so I can not get Mobile access when I'm in. I don't buy online, i don't do online banking, etc.

But we are digressing heavily and should stop this discussion here, I think.

Zvoni

  • Hero Member
  • *****
  • Posts: 2364
Re: Two compiler issues detected - advise needed
« Reply #14 on: September 06, 2023, 01:55:34 pm »
But we are digressing heavily and should stop this discussion here, I think.
Agreed
One System to rule them all, One Code to find them,
One IDE to bring them all, and to the Framework bind them,
in the Land of Redmond, where the Windows lie
---------------------------------------------------------------------
Code is like a joke: If you have to explain it, it's bad

 

TinyPortal © 2005-2018