Merge pull request #8060 from DrDeano/feature/add-mcpu-to-target-options

Add CPU feature check to standardTargetOptions
This commit is contained in:
Andrew Kelley 2021-06-11 17:03:46 -04:00 committed by GitHub
commit f7361970b3
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 62 additions and 11 deletions

View File

@ -637,7 +637,7 @@ pub const Builder = struct {
"target",
"The CPU architecture, OS, and ABI to build for",
);
const mcpu = self.option([]const u8, "cpu", "Target CPU");
const mcpu = self.option([]const u8, "cpu", "Target CPU features to add or subtract");
const triple = maybe_triple orelse return args.default_target;
@ -699,20 +699,64 @@ pub const Builder = struct {
if (args.whitelist) |list| whitelist_check: {
// Make sure it's a match of one of the list.
var mismatch_triple = true;
var mismatch_cpu_features = true;
var whitelist_item = CrossTarget{};
for (list) |t| {
mismatch_cpu_features = true;
mismatch_triple = true;
const t_triple = t.zigTriple(self.allocator) catch unreachable;
if (mem.eql(u8, t_triple, selected_canonicalized_triple)) {
break :whitelist_check;
mismatch_triple = false;
whitelist_item = t;
if (t.getCpuFeatures().isSuperSetOf(selected_target.getCpuFeatures())) {
mismatch_cpu_features = false;
break :whitelist_check;
} else {
break;
}
}
}
warn("Chosen target '{s}' does not match one of the supported targets:\n", .{
selected_canonicalized_triple,
});
for (list) |t| {
const t_triple = t.zigTriple(self.allocator) catch unreachable;
warn(" {s}\n", .{t_triple});
if (mismatch_triple) {
warn("Chosen target '{s}' does not match one of the supported targets:\n", .{
selected_canonicalized_triple,
});
for (list) |t| {
const t_triple = t.zigTriple(self.allocator) catch unreachable;
warn(" {s}\n", .{t_triple});
}
warn("\n", .{});
} else {
assert(mismatch_cpu_features);
const whitelist_cpu = whitelist_item.getCpu();
const selected_cpu = selected_target.getCpu();
warn("Chosen CPU model '{s}' does not match one of the supported targets:\n", .{
selected_cpu.model.name,
});
warn(" Supported feature Set: ", .{});
const all_features = whitelist_cpu.arch.allFeaturesList();
var populated_cpu_features = whitelist_cpu.model.features;
populated_cpu_features.populateDependencies(all_features);
for (all_features) |feature, i_usize| {
const i = @intCast(std.Target.Cpu.Feature.Set.Index, i_usize);
const in_cpu_set = populated_cpu_features.isEnabled(i);
if (in_cpu_set) {
warn("{s} ", .{feature.name});
}
}
warn("\n", .{});
warn(" Remove: ", .{});
for (all_features) |feature, i_usize| {
const i = @intCast(std.Target.Cpu.Feature.Set.Index, i_usize);
const in_cpu_set = populated_cpu_features.isEnabled(i);
const in_actual_set = selected_cpu.features.isEnabled(i);
if (in_actual_set and !in_cpu_set) {
warn("{s} ", .{feature.name});
}
}
warn("\n", .{});
}
warn("\n", .{});
self.markInvalidUserInput();
return args.default_target;
}

View File

@ -664,8 +664,15 @@ pub const Target = struct {
return @ptrCast(*const [byte_count]u8, &set.ints);
}
pub fn eql(set: Set, other: Set) bool {
return mem.eql(usize, &set.ints, &other.ints);
pub fn eql(set: Set, other_set: Set) bool {
return mem.eql(usize, &set.ints, &other_set.ints);
}
pub fn isSuperSetOf(set: Set, other_set: Set) bool {
const V = std.meta.Vector(usize_count, usize);
const set_v: V = set.ints;
const other_v: V = other_set.ints;
return @reduce(.And, (set_v & other_v) == other_v);
}
};