Win64 AArch64 optimizer bug
Summary
When compiling a program for Win64 AArch64 certain code leads to crash when optimization is on.
May be @FPK2 can have a look at this?
System Information
- Operating system: Windows 11 ARM64
- Processor architecture: AARCH64
- Compiler version: trunk 3.3.1-12557-g2b0317f4
- Device: Computer
Steps to reproduce
The following program will crash when any optimization is applied (O1, O2 or O3).
program project2;
uses
Classes, SysUtils;
var
i: integer;
begin
i := 4;
case i of
1: WriteLn('1');
2: WriteLn('2');
3: WriteLn('3');
4: WriteLn('4');
5: WriteLn('5');
6: WriteLn('6');
7: WriteLn('7');
8: WriteLn('8');
9: WriteLn('9');
10: WriteLn('10');
11: WriteLn('11');
end;
end.
Compile without optimization: fpc -O- -Paarch64 project2.lpr
This will produce a valid EXE file that upon execution will print 4.
Now recompile with optimization /O1 or /O2: fpc -O1 -Paarch64 project2.lpr
This will produce an invalid EXE that will crash:
An unhandled exception occurred at $00007FF6C5311D98:
EAccessViolation: Access violation
$00007FF6C5311D98 main, line 12 of project2.lpr
$00007FF6C5312138
$00007FF6C5327844
$00007FF6C5311D60
$00007FFD70EF2400
$00007FFD72022DEC
What is the current bug behavior?
The program crashes upon execution when this code is compiled with /O1, O2 or O3. Only compiling with -O- produces a working EXE file.
What is the expected (correct) behavior?
The crash must not occur. Most likely incorrect machine code is generated.
The program will also run correctly if you reduce the number of case labels to 10 or fewer. So it appears that the optimizer incorrectly handles case statements with more than 10 labels.