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 {...@@ -709,8 +709,8 @@ pub fn toLLVMTriple(target: std.Target, buf: []u8) []const u8 {
709test "alignment functions - smoke test" {709test "alignment functions - smoke test" {
710 var target: std.Target = undefined;710 var target: std.Target = undefined;
711 const x86 = std.Target.Cpu.Arch.x86_64;711 const x86 = std.Target.Cpu.Arch.x86_64;
712 target.cpu = std.Target.Cpu.baseline(x86);
713 target.os = std.Target.Os.Tag.defaultVersionRange(.linux, x86);712 target.os = std.Target.Os.Tag.defaultVersionRange(.linux, x86);
713 target.cpu = std.Target.Cpu.baseline(x86, target.os);
714 target.abi = std.Target.Abi.default(x86, target.os);714 target.abi = std.Target.Abi.default(x86, target.os);
715715
716 try std.testing.expect(isTlsSupported(target));716 try std.testing.expect(isTlsSupported(target));
...@@ -722,8 +722,8 @@ test "alignment functions - smoke test" {...@@ -722,8 +722,8 @@ test "alignment functions - smoke test" {
722 try std.testing.expect(systemCompiler(target) == .gcc);722 try std.testing.expect(systemCompiler(target) == .gcc);
723723
724 const arm = std.Target.Cpu.Arch.arm;724 const arm = std.Target.Cpu.Arch.arm;
725 target.cpu = std.Target.Cpu.baseline(arm);
726 target.os = std.Target.Os.Tag.defaultVersionRange(.ios, arm);725 target.os = std.Target.Os.Tag.defaultVersionRange(.ios, arm);
726 target.cpu = std.Target.Cpu.baseline(arm, target.os);
727 target.abi = std.Target.Abi.default(arm, target.os);727 target.abi = std.Target.Abi.default(arm, target.os);
728728
729 try std.testing.expect(!isTlsSupported(target));729 try std.testing.expect(!isTlsSupported(target));
lib/std/Target.zig+10-3
...@@ -1669,9 +1669,16 @@ pub const Cpu = struct {...@@ -1669,9 +1669,16 @@ pub const Cpu = struct {
1669 };1669 };
1670 }1670 }
16711671
1672 pub fn baseline(arch: Arch) *const Model {1672 pub fn baseline(arch: Arch, os: Os) *const Model {
1673 return switch (arch) {1673 return switch (arch) {
1674 .arm, .armeb, .thumb, .thumbeb => &arm.cpu.baseline,1674 .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 },
1675 .hexagon => &hexagon.cpu.hexagonv60, // gcc/clang do not have a generic hexagon model.1682 .hexagon => &hexagon.cpu.hexagonv60, // gcc/clang do not have a generic hexagon model.
1676 .riscv32 => &riscv.cpu.baseline_rv32,1683 .riscv32 => &riscv.cpu.baseline_rv32,
1677 .riscv64 => &riscv.cpu.baseline_rv64,1684 .riscv64 => &riscv.cpu.baseline_rv64,
...@@ -1688,8 +1695,8 @@ pub const Cpu = struct {...@@ -1688,8 +1695,8 @@ pub const Cpu = struct {
16881695
1689 /// The "default" set of CPU features for cross-compiling. A conservative set1696 /// The "default" set of CPU features for cross-compiling. A conservative set
1690 /// of features that is expected to be supported on most available hardware.1697 /// of features that is expected to be supported on most available hardware.
1691 pub fn baseline(arch: Arch) Cpu {1698 pub fn baseline(arch: Arch, os: Os) Cpu {
1692 return Model.baseline(arch).toCpu(arch);1699 return Model.baseline(arch, os).toCpu(arch);
1693 }1700 }
1694};1701};
16951702
lib/std/Target/Query.zig+5-5
...@@ -6,7 +6,7 @@...@@ -6,7 +6,7 @@
6/// `null` means native.6/// `null` means native.
7cpu_arch: ?Target.Cpu.Arch = null,7cpu_arch: ?Target.Cpu.Arch = null,
88
9cpu_model: CpuModel = CpuModel.determined_by_cpu_arch,9cpu_model: CpuModel = CpuModel.determined_by_arch_os,
1010
11/// Sparse set of CPU features to add to the set from `cpu_model`.11/// Sparse set of CPU features to add to the set from `cpu_model`.
12cpu_features_add: Target.Cpu.Feature.Set = Target.Cpu.Feature.Set.empty,12cpu_features_add: Target.Cpu.Feature.Set = Target.Cpu.Feature.Set.empty,
...@@ -48,7 +48,7 @@ pub const CpuModel = union(enum) {...@@ -48,7 +48,7 @@ pub const CpuModel = union(enum) {
4848
49 /// If CPU Architecture is native, then the CPU model will be native. Otherwise,49 /// If CPU Architecture is native, then the CPU model will be native. Otherwise,
50 /// it will be baseline.50 /// it will be baseline.
51 determined_by_cpu_arch,51 determined_by_arch_os,
5252
53 explicit: *const Target.Cpu.Model,53 explicit: *const Target.Cpu.Model,
5454
...@@ -58,7 +58,7 @@ pub const CpuModel = union(enum) {...@@ -58,7 +58,7 @@ pub const CpuModel = union(enum) {
58 const b_tag: Tag = b;58 const b_tag: Tag = b;
59 if (a_tag != b_tag) return false;59 if (a_tag != b_tag) return false;
60 return switch (a) {60 return switch (a) {
61 .native, .baseline, .determined_by_cpu_arch => true,61 .native, .baseline, .determined_by_arch_os => true,
62 .explicit => |a_model| a_model == b.explicit,62 .explicit => |a_model| a_model == b.explicit,
63 };63 };
64 }64 }
...@@ -349,7 +349,7 @@ test parseVersion {...@@ -349,7 +349,7 @@ test parseVersion {
349349
350pub fn isNativeCpu(self: Query) bool {350pub fn isNativeCpu(self: Query) bool {
351 return self.cpu_arch == null and351 return self.cpu_arch == null and
352 (self.cpu_model == .native or self.cpu_model == .determined_by_cpu_arch) and352 (self.cpu_model == .native or self.cpu_model == .determined_by_arch_os) and
353 self.cpu_features_sub.isEmpty() and self.cpu_features_add.isEmpty();353 self.cpu_features_sub.isEmpty() and self.cpu_features_add.isEmpty();
354}354}
355355
...@@ -461,7 +461,7 @@ pub fn serializeCpu(q: Query, buffer: *std.ArrayList(u8)) Allocator.Error!void {...@@ -461,7 +461,7 @@ pub fn serializeCpu(q: Query, buffer: *std.ArrayList(u8)) Allocator.Error!void {
461 .baseline => {461 .baseline => {
462 buffer.appendSliceAssumeCapacity("baseline");462 buffer.appendSliceAssumeCapacity("baseline");
463 },463 },
464 .determined_by_cpu_arch => {464 .determined_by_arch_os => {
465 if (q.cpu_arch == null) {465 if (q.cpu_arch == null) {
466 buffer.appendSliceAssumeCapacity("native");466 buffer.appendSliceAssumeCapacity("native");
467 } else {467 } else {
lib/std/simd.zig+1-1
...@@ -90,7 +90,7 @@ pub fn suggestVectorLength(comptime T: type) ?comptime_int {...@@ -90,7 +90,7 @@ pub fn suggestVectorLength(comptime T: type) ?comptime_int {
90}90}
9191
92test "suggestVectorLengthForCpu works with signed and unsigned values" {92test "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);
94 comptime cpu.features.addFeature(@intFromEnum(std.Target.x86.Feature.avx512f));94 comptime cpu.features.addFeature(@intFromEnum(std.Target.x86.Feature.avx512f));
95 comptime cpu.features.populateDependencies(&std.Target.x86.all_features);95 comptime cpu.features.populateDependencies(&std.Target.x86.all_features);
96 const expected_len: usize = switch (builtin.zig_backend) {96 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 {...@@ -337,14 +337,14 @@ pub fn resolveTargetQuery(query: Target.Query) DetectError!Target {
337337
338 const cpu = switch (query.cpu_model) {338 const cpu = switch (query.cpu_model) {
339 .native => detectNativeCpuAndFeatures(cpu_arch, os, query),339 .native => detectNativeCpuAndFeatures(cpu_arch, os, query),
340 .baseline => Target.Cpu.baseline(cpu_arch),340 .baseline => Target.Cpu.baseline(cpu_arch, os),
341 .determined_by_cpu_arch => if (query.cpu_arch == null)341 .determined_by_arch_os => if (query.cpu_arch == null)
342 detectNativeCpuAndFeatures(cpu_arch, os, query)342 detectNativeCpuAndFeatures(cpu_arch, os, query)
343 else343 else
344 Target.Cpu.baseline(cpu_arch),344 Target.Cpu.baseline(cpu_arch, os),
345 .explicit => |model| model.toCpu(cpu_arch),345 .explicit => |model| model.toCpu(cpu_arch),
346 } orelse backup_cpu_detection: {346 } 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);
348 };348 };
349 var result = try detectAbiAndDynamicLinker(cpu, os, query);349 var result = try detectAbiAndDynamicLinker(cpu, os, query);
350 // For x86, we need to populate some CPU feature flags depending on architecture350 // For x86, we need to populate some CPU feature flags depending on architecture
src/main.zig+1-1
...@@ -6299,7 +6299,7 @@ fn detectNativeCpuWithLLVM(...@@ -6299,7 +6299,7 @@ fn detectNativeCpuWithLLVM(
6299 llvm_cpu_name_z: ?[*:0]const u8,6299 llvm_cpu_name_z: ?[*:0]const u8,
6300 llvm_cpu_features_opt: ?[*:0]const u8,6300 llvm_cpu_features_opt: ?[*:0]const u8,
6301) !std.Target.Cpu {6301) !std.Target.Cpu {
6302 var result = std.Target.Cpu.baseline(arch);6302 var result = std.Target.Cpu.baseline(arch, builtin.os);
63036303
6304 if (llvm_cpu_name_z) |cpu_name_z| {6304 if (llvm_cpu_name_z) |cpu_name_z| {
6305 const llvm_cpu_name = mem.span(cpu_name_z);6305 const llvm_cpu_name = mem.span(cpu_name_z);