authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2025-01-18 18:22:11-05:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2025-01-18 18:22:11-05:00
loga5d2aaa936dc74b85d7cf6695e6a7627d7a645d2
treeaff47a0d59ae8ed164cbdd794eac8ac3301b9f96
parent128658038d26a75dd1a52f82fc65fba3e91bba9e
parent515c97065a5352448747fbaf1f5ea43173a4457a
signaturebadge-check Signed by PGP key B5690EEEBB952194

Merge pull request #22526 from alexrp/cpu-feature-hacks

`std.zig.system`: Move CPU feature hacks after ABI detection.

2 files changed, 20 insertions(+), 40 deletions(-)

lib/compiler_rt/memcpy.zig+2-26
......@@ -9,36 +9,12 @@ comptime {
99 }
1010}
1111
12const llvm_cannot_lower = switch (builtin.cpu.arch) {
13 .arm, .armeb, .thumb, .thumbeb => builtin.zig_backend == .stage2_llvm,
14 else => false,
15};
16
1712fn memcpy(noalias opt_dest: ?[*]u8, noalias opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
18 if (llvm_cannot_lower) {
19 for (0..len) |i| opt_dest.?[i] = opt_src.?[i];
20 return opt_dest;
21 } else {
22 return memmove(opt_dest, opt_src, len);
23 }
13 return memmove(opt_dest, opt_src, len);
2414}
2515
26/// A port of https://github.com/facebook/folly/blob/1c8bc50e88804e2a7361a57cd9b551dd10f6c5fd/folly/memcpy.S
2716fn memmove(opt_dest: ?[*]u8, opt_src: ?[*]const u8, len: usize) callconv(.C) ?[*]u8 {
28 if (llvm_cannot_lower) {
29 if (@intFromPtr(opt_dest) < @intFromPtr(opt_src)) {
30 for (0..len) |i| opt_dest.?[i] = opt_src.?[i];
31 return opt_dest;
32 } else {
33 var index = len;
34 while (index != 0) {
35 index -= 1;
36 opt_dest.?[index] = opt_src.?[index];
37 }
38 return opt_dest;
39 }
40 }
41
17 // a port of https://github.com/facebook/folly/blob/1c8bc50e88804e2a7361a57cd9b551dd10f6c5fd/folly/memcpy.S
4218 if (len == 0) {
4319 @branchHint(.unlikely);
4420 return opt_dest;
lib/std/zig/system.zig+18-14
......@@ -399,22 +399,26 @@ pub fn resolveTargetQuery(query: Target.Query) DetectError!Target {
399399 query.cpu_features_sub,
400400 );
401401
402 if (cpu.arch == .hexagon) {
403 // Both LLVM and LLD have broken support for the small data area. Yet LLVM has the feature
404 // on by default for all Hexagon CPUs. Clang sort of solves this by defaulting the `-gpsize`
405 // command line parameter for the Hexagon backend to 0, so that no constants get placed in
406 // the SDA. (This of course breaks down if the user passes `-G <n>` to Clang...) We can't do
407 // the `-gpsize` hack because we can have multiple concurrent LLVM emit jobs, and command
408 // line options in LLVM are shared globally. So just force this feature off. Lovely stuff.
409 cpu.features.removeFeature(@intFromEnum(Target.hexagon.Feature.small_data));
410 }
402 var result = try detectAbiAndDynamicLinker(cpu, os, query);
411403
412 // https://github.com/llvm/llvm-project/issues/105978
413 if (cpu.arch.isArm() and query_abi.floatAbi() == .soft) {
414 cpu.features.removeFeature(@intFromEnum(Target.arm.Feature.vfp2));
415 }
404 // These CPU feature hacks have to come after ABI detection.
405 {
406 if (result.cpu.arch == .hexagon) {
407 // Both LLVM and LLD have broken support for the small data area. Yet LLVM has the
408 // feature on by default for all Hexagon CPUs. Clang sort of solves this by defaulting
409 // the `-gpsize` command line parameter for the Hexagon backend to 0, so that no
410 // constants get placed in the SDA. (This of course breaks down if the user passes
411 // `-G <n>` to Clang...) We can't do the `-gpsize` hack because we can have multiple
412 // concurrent LLVM emit jobs, and command line options in LLVM are shared globally. So
413 // just force this feature off. Lovely stuff.
414 result.cpu.features.removeFeature(@intFromEnum(Target.hexagon.Feature.small_data));
415 }
416416
417 var result = try detectAbiAndDynamicLinker(cpu, os, query);
417 // https://github.com/llvm/llvm-project/issues/105978
418 if (result.cpu.arch.isArm() and result.abi.floatAbi() == .soft) {
419 result.cpu.features.removeFeature(@intFromEnum(Target.arm.Feature.vfp2));
420 }
421 }
418422
419423 // It's possible that we detect the native ABI, but fail to detect the OS version or were told
420424 // to use the default OS version range. In that case, while we can't determine the exact native