authorgravatar for alex@alexrp.comAlex Rønne Petersen <alex@alexrp.com> 2024-10-16 04:59:02+02:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2024-10-16 04:59:02+02:00
logecd5878b74940852e8a63154aba165047bca0e0a
tree543bdd9ac090d7e4051892f00cf1cbb5b716a070
parent1efc9c149c8310802a1fb9bb15024079e033ef5e
parenta2c466220c13392e85f4910316883d5042ec248c
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #21714 from alexrp/target-cpu-baseline

`std.Target`: Make `Cpu.baseline()` take OS into consideration and pick a better CPU for Apple targets

6 files changed, 23 insertions(+), 16 deletions(-)

lib/compiler/aro/aro/target.zig+2-2
......@@ -709,8 +709,8 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
709709test "alignment functions - smoke test" {
710710 var target: std.Target = undefined;
711711 const x86 = std.Target.Cpu.Arch.x86_64;
712 target.cpu = std.Target.Cpu.baseline(x86);
713712 target.os = std.Target.Os.Tag.defaultVersionRange(.linux, x86);
713 target.cpu = std.Target.Cpu.baseline(x86, target.os);
714714 target.abi = std.Target.Abi.default(x86, target.os);
715715
716716 try std.testing.expect(isTlsSupported(target));
......@@ -722,8 +722,8 @@ test "alignment functions - smoke test" {
722722 try std.testing.expect(systemCompiler(target) == .gcc);
723723
724724 const arm = std.Target.Cpu.Arch.arm;
725 target.cpu = std.Target.Cpu.baseline(arm);
726725 target.os = std.Target.Os.Tag.defaultVersionRange(.ios, arm);
726 target.cpu = std.Target.Cpu.baseline(arm, target.os);
727727 target.abi = std.Target.Abi.default(arm, target.os);
728728
729729 try std.testing.expect(!isTlsSupported(target));
lib/std/Target.zig+10-3
......@@ -1669,9 +1669,16 @@ pub const Cpu = struct {
16691669 };
16701670 }
16711671
1672 pub fn baseline(arch: Arch) *const Model {
1672 pub fn baseline(arch: Arch, os: Os) *const Model {
16731673 return switch (arch) {
16741674 .arm, .armeb, .thumb, .thumbeb => &arm.cpu.baseline,
1675 .aarch64 => switch (os.tag) {
1676 .bridgeos, .driverkit, .macos => &aarch64.cpu.apple_m1,
1677 .ios, .tvos => &aarch64.cpu.apple_a7,
1678 .visionos => &aarch64.cpu.apple_m2,
1679 .watchos => &aarch64.cpu.apple_s4,
1680 else => generic(arch),
1681 },
16751682 .hexagon => &hexagon.cpu.hexagonv60, // gcc/clang do not have a generic hexagon model.
16761683 .riscv32 => &riscv.cpu.baseline_rv32,
16771684 .riscv64 => &riscv.cpu.baseline_rv64,
......@@ -1688,8 +1695,8 @@ pub const Cpu = struct {
16881695
16891696 /// The "default" set of CPU features for cross-compiling. A conservative set
16901697 /// of features that is expected to be supported on most available hardware.
1691 pub fn baseline(arch: Arch) Cpu {
1692 return Model.baseline(arch).toCpu(arch);
1698 pub fn baseline(arch: Arch, os: Os) Cpu {
1699 return Model.baseline(arch, os).toCpu(arch);
16931700 }
16941701};
16951702
lib/std/Target/Query.zig+5-5
......@@ -6,7 +6,7 @@
66/// `null` means native.
77cpu_arch: ?Target.Cpu.Arch = null,
88
9cpu_model: CpuModel = CpuModel.determined_by_cpu_arch,
9cpu_model: CpuModel = CpuModel.determined_by_arch_os,
1010
1111/// Sparse set of CPU features to add to the set from `cpu_model`.
1212cpu_features_add: Target.Cpu.Feature.Set = Target.Cpu.Feature.Set.empty,
......@@ -48,7 +48,7 @@ pub const CpuModel = union(enum) {
4848
4949 /// If CPU Architecture is native, then the CPU model will be native. Otherwise,
5050 /// it will be baseline.
51 determined_by_cpu_arch,
51 determined_by_arch_os,
5252
5353 explicit: *const Target.Cpu.Model,
5454
......@@ -58,7 +58,7 @@ pub const CpuModel = union(enum) {
5858 const b_tag: Tag = b;
5959 if (a_tag != b_tag) return false;
6060 return switch (a) {
61 .native, .baseline, .determined_by_cpu_arch => true,
61 .native, .baseline, .determined_by_arch_os => true,
6262 .explicit => |a_model| a_model == b.explicit,
6363 };
6464 }
......@@ -349,7 +349,7 @@ test parseVersion {
349349
350350pub fn isNativeCpu(self: Query) bool {
351351 return self.cpu_arch == null and
352 (self.cpu_model == .native or self.cpu_model == .determined_by_cpu_arch) and
352 (self.cpu_model == .native or self.cpu_model == .determined_by_arch_os) and
353353 self.cpu_features_sub.isEmpty() and self.cpu_features_add.isEmpty();
354354}
355355
......@@ -461,7 +461,7 @@ pub fn serializeCpu(q: Query, buffer: *std.ArrayList(u8)) Allocator.Error!void {
461461 .baseline => {
462462 buffer.appendSliceAssumeCapacity("baseline");
463463 },
464 .determined_by_cpu_arch => {
464 .determined_by_arch_os => {
465465 if (q.cpu_arch == null) {
466466 buffer.appendSliceAssumeCapacity("native");
467467 } else {
lib/std/simd.zig+1-1
......@@ -90,7 +90,7 @@ pub fn suggestVectorLength(comptime T: type) ?comptime_int {
9090}
9191
9292test "suggestVectorLengthForCpu works with signed and unsigned values" {
93 comptime var cpu = std.Target.Cpu.baseline(std.Target.Cpu.Arch.x86_64);
93 comptime var cpu = std.Target.Cpu.baseline(std.Target.Cpu.Arch.x86_64, builtin.os);
9494 comptime cpu.features.addFeature(@intFromEnum(std.Target.x86.Feature.avx512f));
9595 comptime cpu.features.populateDependencies(&std.Target.x86.all_features);
9696 const expected_len: usize = switch (builtin.zig_backend) {
lib/std/zig/system.zig+4-4
......@@ -337,14 +337,14 @@ pub fn resolveTargetQuery(query: Target.Query) DetectError!Target {
337337
338338 const cpu = switch (query.cpu_model) {
339339 .native => detectNativeCpuAndFeatures(cpu_arch, os, query),
340 .baseline => Target.Cpu.baseline(cpu_arch),
341 .determined_by_cpu_arch => if (query.cpu_arch == null)
340 .baseline => Target.Cpu.baseline(cpu_arch, os),
341 .determined_by_arch_os => if (query.cpu_arch == null)
342342 detectNativeCpuAndFeatures(cpu_arch, os, query)
343343 else
344 Target.Cpu.baseline(cpu_arch),
344 Target.Cpu.baseline(cpu_arch, os),
345345 .explicit => |model| model.toCpu(cpu_arch),
346346 } orelse backup_cpu_detection: {
347 break :backup_cpu_detection Target.Cpu.baseline(cpu_arch);
347 break :backup_cpu_detection Target.Cpu.baseline(cpu_arch, os);
348348 };
349349 var result = try detectAbiAndDynamicLinker(cpu, os, query);
350350 // For x86, we need to populate some CPU feature flags depending on architecture
src/main.zig+1-1
......@@ -6299,7 +6299,7 @@ fn detectNativeCpuWithLLVM(
62996299 llvm_cpu_name_z: ?[*:0]const u8,
63006300 llvm_cpu_features_opt: ?[*:0]const u8,
63016301) !std.Target.Cpu {
6302 var result = std.Target.Cpu.baseline(arch);
6302 var result = std.Target.Cpu.baseline(arch, builtin.os);
63036303
63046304 if (llvm_cpu_name_z) |cpu_name_z| {
63056305 const llvm_cpu_name = mem.span(cpu_name_z);