Skip to content

Commit a5d2aaa

Browse files
authored
Merge pull request #22526 from alexrp/cpu-feature-hacks
`std.zig.system`: Move CPU feature hacks after ABI detection.
2 parents 1286580 + 515c970 commit a5d2aaa

File tree

2 files changed

+20
-40
lines changed

2 files changed

+20
-40
lines changed

lib/compiler_rt/memcpy.zig

Lines changed: 2 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -9,36 +9,12 @@ comptime {
99
}
1010
}
1111

12-
const llvm_cannot_lower = switch (builtin.cpu.arch) {
13-
.arm, .armeb, .thumb, .thumbeb => builtin.zig_backend == .stage2_llvm,
14-
else => false,
15-
};
16-
1712
fn 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
2716
fn 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

Lines changed: 18 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -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

0 commit comments

Comments
 (0)