Skip to content

Commit

Permalink
work around LLVM 18 miscompilation
Browse files Browse the repository at this point in the history
LLVM 18 was incorrectly allocating the `rax` register for the `ebx` variable even though
the constraint list defined `ax` as a write-only register. Work around this by marking
the `q1` allocating as an earlyclobber. This is confirmed to be fixed in LLVM 19
and we can revert the change when upgrading to Zig 0.14
  • Loading branch information
Rexicon226 committed Dec 15, 2024
1 parent d879aa8 commit 3793e72
Showing 1 changed file with 8 additions and 2 deletions.
10 changes: 8 additions & 2 deletions port/port_posix.cc
Original file line number Diff line number Diff line change
Expand Up @@ -188,9 +188,15 @@ int PhysicalCoreID() {
// clang/gcc both provide cpuid.h, which defines __get_cpuid(), for x86_64 and
// i386.
unsigned eax, ebx = 0, ecx, edx;
if (!__get_cpuid(1, &eax, &ebx, &ecx, &edx)) {
return -1;
unsigned int __max_leaf = __get_cpuid_max(1 & 0x80000000, 0);

This comment has been minimized.

Copy link
@dnut

dnut Dec 16, 2024

1 & 0x80000000 == 0?

This comment has been minimized.

Copy link
@Rexicon226

Rexicon226 Dec 16, 2024

Author Collaborator

I am simply pulling out the relevant code and duplicating behavior. I don't plan on doing anything else.

if (__max_leaf == 0 || __max_leaf < 1) {

This comment has been minimized.

Copy link
@dnut

dnut Dec 16, 2024

Isn't this redundant?

This comment has been minimized.

Copy link
@Rexicon226

Rexicon226 Dec 16, 2024

Author Collaborator

No idea, this is what cpuid.h does.

return -1;
}
__asm(" xchgq %%rbx,%q1\n" \
" cpuid\n" \
" xchgq %%rbx,%q1" \
: "=a"(eax), "=&r" (ebx), "=c"(ecx), "=d"(edx) \

This comment has been minimized.

Copy link
@dnut

dnut Dec 16, 2024

why use =&r instead of =b?

This comment has been minimized.

Copy link
@Rexicon226

Rexicon226 Dec 16, 2024

Author Collaborator

that would be the opposite of the behavior we need. that would directly force the miscompilation to happen.

This comment has been minimized.

Copy link
@dnut

dnut Dec 16, 2024

ok i guess i'm just trying to make sense of why this fixes the issue. i assumed the xchgq was enough to deal with clobbering. i just don't fully grasp what's going on here since i haven't worked with assembly before.

: "0"(1) );
return ebx >> 24;
#else
// give up, the caller can generate a random number or something.
Expand Down

0 comments on commit 3793e72

Please sign in to comment.