Open Ivan-Velickovic opened 2 weeks ago
Not familiar with issues like this, but did you build Zig yourself, or download a master/nightly build?
(I assume building with a system-provided LLVM/Clang that was patched somehow could lead to issues like this?)
EDIT: I think the "native" section from the end of the output of zig targets
might also be of interest, but not sure.
@Ivan-Velickovic probably you use -cpu max
in qemu-system (I had same issue)? Different cpu should workaround your issue eg: -cpu EPYC-v4
(Note that even if there's a workaround, I think we'd still be interested in fixing this, so please keep the issue open. Zig should ideally work on all CPUs.)
athlon_xp appears to be a 32-bit CPU only. The error comes from clang because zig detects the CPU as athlon_xp and therefore sends athlon-xp
to clang, but clang rejects it because the target is x86_64.
Therefore, this is a bug in the CPU detection, it seems that the host CPU is not in fact athlon_xp.
perhaps the bug is here
@Ivan-Velickovic probably you use
-cpu max
in qemu-system (I had same issue)? Different cpu should workaround your issue eg:-cpu EPYC-v4
This is in a VPS that I rent so I unfortunately can't control how it's being virtualised.
Not familiar with issues like this, but did you build Zig yourself, or download a master/nightly build?
Downloaded from the Zig website.
@Ivan-Velickovic Could you please run the following code and let me know the result?
const std = @import("std");
const CpuidLeaf = struct {
eax: u32,
ebx: u32,
ecx: u32,
edx: u32,
};
fn cpuid(leaf_id: u32, subid: u32) CpuidLeaf {
var eax: u32 = undefined;
var ebx: u32 = undefined;
var ecx: u32 = undefined;
var edx: u32 = undefined;
asm volatile ("cpuid"
: [_eax] "={eax}" (eax),
[_ebx] "={ebx}" (ebx),
[_ecx] "={ecx}" (ecx),
[_edx] "={edx}" (edx),
: [_in_eax] "{eax}" (leaf_id),
[_in_ecx] "{ecx}" (subid),
);
return .{ .eax = eax, .ebx = ebx, .ecx = ecx, .edx = edx };
}
pub fn main() void {
const leaf = cpuid(0, 0);
std.debug.print("{s}{s}{s}\n", .{ std.mem.toBytes(leaf.ebx), std.mem.toBytes(leaf.edx), std.mem.toBytes(leaf.ecx) });
}
Because, target "athlon" is only in the AMD branch, but you have an Intel CPU. That's strange.
The result is AuthenticAMD
.
I suspected it, but I have no idea why your system shows "Intel" in /proc/cpuinfo, but outputs "AMD" via the register query. I suspect that this may have something to do with the virtual environment. Maybe you should take another look at the settings. What kind of CPU did you rent?
Now I've googled a bit and found out the following: Often the KVM settings are set in such a way that the CPU type is 'AMD', even though another hardware is providing the virtual environment. This then leads to the behavior you're observing. You can fix this by passing through the original CPU values (called CPU Passthrough). This can be done using a setting in the KVM XML setup: <cpu mode="host-passthrough"/>
I see.
What kind of CPU did you rent?
There wasn't any choice in the kind of CPU I can rent, it's just a vCPU hosted by https://www.binarylane.com.au/.
Often the KVM settings are set in such a way that the CPU type is 'AMD', even though another hardware is providing the virtual environment
I see. So I guess it's just an issue with the virtualisation of the CPU? Why does this only occur on Zig and not on Clang? Is it because Clang isn't trying to find the exact kind of x86 CPU by default?
I've been wondering that too, since /proc/cpuinfo
also shows Intel. But I don't have a really good idea yet.
I've now looked at how LLVM does this and it's a lot more than what happens in Zig. Of course you can replicate this and you probably should, but that requires a lot more effort and, above all, testing. So I don't think there is a quick solution. Unfortunately.
Link to LLVM: https://github.com/llvm/llvm-project/blob/99fd1c5536547ed4fc360b16e7fa2e06278707a8/llvm/lib/TargetParser/Host.cpp#L1342
Zig Version
0.14.0-dev.2178+bd7dda0c5
Steps to Reproduce and Observed Behavior
Run the following command:
zig cc anything.c
no need to create
anything.c
.Get the following output:
This occurs on Linux x86_64, my
uname -a
looks like this:Linux vista-genetic 5.15.0-91-generic #101-Ubuntu SMP Tue Nov 14 13:30:08 UTC 2023 x86_64 x86_64 x86_64 GNU/Linux
my
/proc/cpuinfo
looks like this:Let me know if I need to give any more information about the system. The only other perhaps notable context is that it is a virtual machine.
Expected Behavior
No error message.