View Issue Details

IDProjectCategoryView StatusLast Update
0037527FPCCompilerpublic2021-01-31 05:59
Reporteravk Assigned ToFlorian  
Status closedResolutionunable to reproduce 
Product Version3.3.1 
Fixed in Version3.3.1 
Summary0037527: X86_64 code optimization
DescriptionIn some cases, when using optimization level 2 and above, FPC generates a completely inoperable code (seemingly starting with revision 45801). Unfortunately, I have no other example besides the one attached.
TagsNo tags attached.
Fixed in Revision
Attached Files


related to 0038129 resolvedFlorian Wrong code generated on x86-64 



2020-08-09 13:53

reporter (4,761 bytes)

J. Gareth Moreton

2020-08-09 14:01

developer   ~0124692

What do you mean by 'inoperable' exactly? Does it not run at all or is it dead code etc?


2020-08-09 14:32

reporter   ~0124693

It crashes with AV.

J. Gareth Moreton

2020-08-09 19:35

developer   ~0124704

I'd take a look myself, but my Intel laptop is out of commission until further notice. However, if you can help me a bit, can you add the custom options "-a" and "-al" and supply the generated .s file (assembly dump), and mark where it crashes in your code (Just a line number in the Pascal source will do), I might be able to spot something and help someone else to make a fix... if they haven't found the problem already!


2020-08-10 09:13

reporter   ~0124711

Yes, sure.
According to the debugger, the AV occurs on line 361 of the timsort unit. (15,715 bytes)


2020-09-10 12:25

reporter   ~0125459

Any news?

J. Gareth Moreton

2020-09-13 22:07

developer   ~0125531

My apologies for the delay - my time has been recently focused on a programming contract and also on some events outside of software development. I'll start looking at it soon.


2020-12-01 16:34

reporter   ~0127300

BTW, compiling example with -O3 -OoNOREGVAR produces a workable code.

J. Gareth Moreton

2020-12-01 21:20

developer   ~0127308

How does the patch over at 0038129 affect it?

Do-wan Kim

2020-12-02 03:26

reporter   ~0127313

It still crash with AV but works ok with '-OoNOREGVAR' switch.

J. Gareth Moreton

2020-12-02 04:25

developer   ~0127314

Aah, drat! Okay, back to work.

J. Gareth Moreton

2020-12-02 06:39

developer   ~0127315

Last edited: 2020-12-02 06:40

View 2 revisions

Okay, analysing the disassembly and the virtual registers (before they are allocated by the compiler), at a certain point, the compiler seems to get confused and forget which real register has been assigned to a virtual register.

In the attached file, on line 3553, is "addsd 16(%ireg29q,%ireg63q),%mreg33md". Up until this point, when fully compiled, %rdx is assigned to %ireg29q, but at this instruction (and the next addsd instruction, although execution doesn't get that far), %ireg29q is suddenly replaced with %rax instead, which is erroneous because %rax is also assigned to %ireg63q, hence the command becomes "addsd 16(%rax,%rax,1),%xmm0, which is very much incorrect.

On another note (also occurs on -O3), Common Subexpression Elimination (CSE) misses the fact that "mov %r12,%rax; shl $5,%rax" appears multiple times.

(Removing "inline" from TVec4.Less erases the error, but this is very much not ideal)
timsort.s (275,185 bytes)


2021-01-30 14:07

reporter   ~0128673

Just tested my example against the current trunk and fixes and it seems everything to be okay, thanks.


2021-01-30 19:01

administrator   ~0128681

As it is apparently fixed, I set it to unable to reproduce

Issue History

Date Modified Username Field Change
2020-08-09 13:53 avk New Issue
2020-08-09 13:53 avk File Added:
2020-08-09 14:01 J. Gareth Moreton Note Added: 0124692
2020-08-09 14:32 avk Note Added: 0124693
2020-08-09 19:31 J. Gareth Moreton Priority normal => high
2020-08-09 19:31 J. Gareth Moreton Severity minor => crash
2020-08-09 19:31 J. Gareth Moreton Platform x84_64 => x86_64
2020-08-09 19:31 J. Gareth Moreton FPCTarget => -
2020-08-09 19:35 J. Gareth Moreton Note Added: 0124704
2020-08-10 09:13 avk Note Added: 0124711
2020-08-10 09:13 avk File Added:
2020-09-10 12:25 avk Note Added: 0125459
2020-09-13 22:07 J. Gareth Moreton Note Added: 0125531
2020-11-26 16:15 J. Gareth Moreton Relationship added related to 0038129
2020-12-01 16:34 avk Note Added: 0127300
2020-12-01 21:20 J. Gareth Moreton Note Added: 0127308
2020-12-02 03:26 Do-wan Kim Note Added: 0127313
2020-12-02 04:25 J. Gareth Moreton Note Added: 0127314
2020-12-02 06:39 J. Gareth Moreton Note Added: 0127315
2020-12-02 06:39 J. Gareth Moreton File Added: timsort.s
2020-12-02 06:40 J. Gareth Moreton Note Edited: 0127315 View Revisions
2021-01-30 14:07 avk Note Added: 0128673
2021-01-30 19:01 Florian Assigned To => Florian
2021-01-30 19:01 Florian Status new => resolved
2021-01-30 19:01 Florian Resolution open => unable to reproduce
2021-01-30 19:01 Florian Fixed in Version => 3.3.1
2021-01-30 19:01 Florian Note Added: 0128681
2021-01-31 05:59 avk Status resolved => closed