Skip to content

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

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
sidkshatriya opened this issue Oct 11, 2024 · 6 comments
Open
Labels
bug Observed behavior contradicts documented or intended behavior

Comments

@sidkshatriya
Copy link

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 sidkshatriya added the bug Observed behavior contradicts documented or intended behavior label Oct 11, 2024
@sidkshatriya
Copy link
Author

cc: @devins2518 #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 ?

@Rexicon226
Copy link
Contributor

Rexicon226 commented Oct 11, 2024

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

@sidkshatriya
Copy link
Author

sidkshatriya commented Oct 11, 2024

@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

@Rexicon226
Copy link
Contributor

Rexicon226 commented Oct 11, 2024

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.

@sidkshatriya
Copy link
Author

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
Copy link
Author

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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Observed behavior contradicts documented or intended behavior
Projects
None yet
Development

No branches or pull requests

2 participants