LLVM bug specific to x86-64 builds
My large Lazarus projects generally work fine when compiled with LLVM. However, I have found a bizarre bug that is specific to compiling with LLVM for the x86-64 target. The program works correctly if compiled using FPC (for either AArch64 or x86-64). It also works correctly if I use round
despite the fact that the variables are integers.
In the example project, running the executable without any parameters will not use the round function (and crashes), while running with any argument will will round values and works correctly:
$ rm -rf ./lib
$ ~/src/lazarus/lazbuild -B --compiler=/Users/chrisrorden/src/fpcllvm86/lib/fpc/3.3.1/ppcx64 --ws=cocoa --cpu=x86_64 ./basicLegacy.lpi
....compiles program
$ ./basicLegacy.app/Contents/MacOS/basicLegacy withAnyParamter
...works fine
> ./basicLegacy.app/Contents/MacOS/basicLegacy
[FORMS.PP] ExceptionOccurred
Sender=EAccessViolation
Exception=Access violation
Stack trace:
$00007FFF6BFE492A
[FORMS.PP] ExceptionOccurred
WARNING: TOpenGLControl.Destroy with LCLRefCount>0. Hint: Maybe the component is processing an event?
The relevant code is:
type
TPoint3f = Packed Record
x,y,z: single;
end;
TRGBA = packed record //red,green,blue,alpha
R,G,B,A : byte;
end;
TVtxClr = Packed Record
vtx : TPoint3f; //vertex coordinates
clr : TRGBA;
end;
...
if gDoRound then
glColor4ub(round(g2Dvnc[i].clr.R), round(g2Dvnc[i].clr.G), round(g2Dvnc[i].clr.B), round(g2Dvnc[i].clr.A))
else
glColor4ub(g2Dvnc[i].clr.R, g2Dvnc[i].clr.G, g2Dvnc[i].clr.B, g2Dvnc[i].clr.A);
FPC version: Free Pascal Compiler version 3.3.1 [2021/08/09] for x86_64 LLVM version: Apple clang version 12.0.5 (clang-1205.0.22.11)
I realize this is a rare edge case, and it is easy enough for me to avoid. So I can understand if this is given a low priority. However, I wonder if this same bug might impact other projects. It is 100% reproducible and does not make any sense to me.
I apologize the example requires Lazarus with the OpenGL package installed. This is the most minimal project I could create that exhibits the bug. Everything else in my large projects seems to work as expected.