zig icon indicating copy to clipboard operation
zig copied to clipboard

On aarch64 zig compiler codebase outline atomics do not seem to be used

Open sidkshatriya opened this issue 1 year ago • 6 comments

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 ?

sidkshatriya avatar Oct 11 '24 16:10 sidkshatriya

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 ?

sidkshatriya avatar Oct 11 '24 16:10 sidkshatriya

You need to compile Zig with the lse extension enabled. https://zig.godbolt.org/z/KGcrdKds6

Rexicon226 avatar Oct 11 '24 16:10 Rexicon226

@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

sidkshatriya avatar Oct 11 '24 17:10 sidkshatriya

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:

  1. 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 -OReleaseFast and without specifying the target. Check if it generates LSE instructions.
  2. Does your CPU support LSE?
  3. If 1) is No and 2) is Yes, there may be an issue with our feature detection then.

Rexicon226 avatar Oct 11 '24 17:10 Rexicon226

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.

sidkshatriya avatar Oct 11 '24 17:10 sidkshatriya

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)

sidkshatriya avatar Oct 11 '24 18:10 sidkshatriya