On aarch64 zig compiler codebase outline atomics do not seem to be used
Zig Version
0.13.0
Steps to Reproduce and Observed Behavior
When looking at the disassembly of e.g. workerAstGenFile in the zig compiler binary, I see plenty of "old style" atomic instructions.
Dump of assembler code for function Compilation.workerAstGenFile:
...
...
0x0000aaaad5686874 <+1568>: ldxr w8, [x0]
0x0000aaaad5686878 <+1572>: add w8, w8, #0x1
0x0000aaaad568687c <+1576>: stlxr w9, w8, [x0]
0x0000aaaad5686880 <+1580>: cbnz w9, 0xaaaad5686874
Expected Behavior
If outline atomics were being used, I'd see modern LSE version of the above instructions also... unfortunately they don't seem to be there.
How can I ensure that (a) The zig compiler codebase and (b) the executables the zig compiler compiles, always have either LSE atomics or outline atomics so that LSE atomics are always chosen ?
cc: @devins2518 https://github.com/ziglang/zig/pull/11828 might be a helpful PR but I'm too new to zig/zig compiler to understand who might know how to resolve this. Maybe the zig compiler needs to be compiled with a particular switch ?
You need to compile Zig with the lse extension enabled.
https://zig.godbolt.org/z/KGcrdKds6
@Rexicon226 Thank you ! That was helpful.
I compiled the the zig compiler itself by doing:
$ zig build -Dcpu=generic+v8_3a+lse -Doptimize=ReleaseSafe
Now I can see workerAstGenFile uses LSE atomics.
Now, it would be great if the compiler I just obtained above by default compiles zig programs with LSE and assuming arm v8.3a -- or do I need to specify that manually... investigating
Now, it would be great if the compiler I just obtained above by default compiles zig programs LSE -- or do I need to specify that manually... investigating
How the compiler was built will not affect the binaries it outputs.
Followup questions would be:
- Does Zig already output binaries with LSE enabled by default? If you downloaded the original zig binary from ziglang.org, it was not built with LSE enabled. That wouldn't affect Zig's ability to generate LSE instructions, so it's important to check.
For example, compile the godbolt link I sent with
zig build-obj -OReleaseFastand without specifying the target. Check if it generates LSE instructions. - Does your CPU support LSE?
- If 1) is No and 2) is Yes, there may be an issue with our feature detection then.
Yes, my CPU supports LSE. On my machine, I need to explictly say -mcpu=generic+lse when I want LSE instructions in the zig executable.
However, it would be better if I get outline atomics -- by looking at the auxv the zig executable should automatically choose between LSE and non-LSE modes.
Somewhat worryingly, when I try to build zig the compiler with a zig compiler that I built using -Dcpu=generic+lse the compilation crashes.
# here zig itself was compiled with -Dcpu=generic+lse -Doptimize=ReleaseSafe
$ zig-out/bin/zig build -Dcpu=generic+lse -Doptimize=ReleaseSafe
...
thread 589644 panic: reached unreachable code
fish: Job 1, 'zig-out/bin/zig build -Dcpu=gen…' terminated by signal SIGABRT (Abort)