Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for target details (CPUs and their supported features) #3927

Closed
wants to merge 26 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
4d275d1
Create initial target details infrastructure
layneson Dec 16, 2019
0fc0a44
Update term feature deps -> subfeatures
layneson Dec 16, 2019
481a6c6
Add parseArchTag and fix parseArchSub
layneson Dec 17, 2019
ef8a3aa
Fix CPU and feature defs
layneson Dec 17, 2019
9f6ef2f
Make targets cmd able to list CPUs and features
layneson Dec 17, 2019
942b8f1
Fix spacing in main.cpp
layneson Dec 17, 2019
1270097
Switch CPU/features to simple format
layneson Dec 21, 2019
c1f8817
Remove llvm_name from features
layneson Dec 21, 2019
0a973bb
Add cpu/feature specification to cmndline
layneson Dec 21, 2019
3880b7b
Add cpu/feature to cache hash
layneson Dec 22, 2019
0605c07
Add build.zig cpu and feature options
layneson Dec 22, 2019
2c39df7
Filter out non-features
layneson Jan 4, 2020
36812bd
Rename subfeatures -> dependencies
layneson Jan 7, 2020
da513be
Add llvm_name to feature defs
layneson Jan 9, 2020
e192337
Add TargetDetails abstraction
layneson Jan 9, 2020
051324f
Add builtin.zig support
layneson Jan 9, 2020
23d2874
Add defaut feature support
layneson Jan 9, 2020
d0836c5
Make sure llvm strings are null-terminated
layneson Jan 9, 2020
ba40d49
Only enable requested features
layneson Jan 15, 2020
a6e7784
No allocations for n.t. empty strings
layneson Jan 15, 2020
ad5d85d
Remove features/cpus not in LLVM v9
layneson Jan 16, 2020
a4fe853
Enable 64bit feature for riscv64
layneson Jan 16, 2020
78e55aa
Fix sentinel mismatch in llvm strings
layneson Jan 16, 2020
ee382f9
Pass target details to c compiler
layneson Jan 16, 2020
c31a7af
Allow target details with no LLVM support
layneson Jan 16, 2020
caa942b
Pass target_details to child CodeGens
layneson Jan 17, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 28 additions & 0 deletions lib/std/build.zig
Original file line number Diff line number Diff line change
Expand Up @@ -1199,6 +1199,8 @@ pub const LibExeObjStep = struct {

subsystem: ?builtin.SubSystem = null,

target_details: ?std.target.TargetDetails = null,

const LinkObject = union(enum) {
StaticPath: []const u8,
OtherStep: *LibExeObjStep,
Expand Down Expand Up @@ -1384,6 +1386,10 @@ pub const LibExeObjStep = struct {
self.computeOutFileNames();
}

pub fn setTargetDetails(self: *LibExeObjStep, target_details: std.target.TargetDetails) void {
self.target_details = target_details;
}

pub fn setTargetGLibC(self: *LibExeObjStep, major: u32, minor: u32, patch: u32) void {
self.target_glibc = Version{
.major = major,
Expand Down Expand Up @@ -1974,6 +1980,28 @@ pub const LibExeObjStep = struct {
},
}

if (self.target_details) |td| {
switch (td) {
.cpu => |cpu| {
try zig_args.append("--cpu");
try zig_args.append(cpu.name);
},
.features => |features| {
try zig_args.append("--features");

var feature_str_buffer = try std.Buffer.initSize(builder.allocator, 0);
defer feature_str_buffer.deinit();

for (features) |feature| {
try feature_str_buffer.append(feature.name);
try feature_str_buffer.append(",");
}

try zig_args.append(feature_str_buffer.toOwnedSlice());
},
}
}

if (self.target_glibc) |ver| {
try zig_args.append("-target-glibc");
try zig_args.append(builder.fmt("{}.{}.{}", .{ ver.major, ver.minor, ver.patch }));
Expand Down
1 change: 1 addition & 0 deletions lib/std/std.zig
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ pub const rand = @import("rand.zig");
pub const rb = @import("rb.zig");
pub const sort = @import("sort.zig");
pub const ascii = @import("ascii.zig");
pub const target = @import("target.zig");
pub const testing = @import("testing.zig");
pub const time = @import("time.zig");
pub const unicode = @import("unicode.zig");
Expand Down
107 changes: 106 additions & 1 deletion lib/std/target.zig
Original file line number Diff line number Diff line change
Expand Up @@ -458,7 +458,7 @@ pub const Target = union(enum) {
pub fn parseArchSub(text: []const u8) ParseArchSubError!Arch {
const info = @typeInfo(Arch);
inline for (info.Union.fields) |field| {
if (mem.eql(u8, text, field.name)) {
if (text.len >= field.name.len and mem.eql(u8, text[0..field.name.len], field.name)) {
if (field.field_type == void) {
return @as(Arch, @field(Arch, field.name));
} else {
Expand All @@ -476,6 +476,31 @@ pub const Target = union(enum) {
return error.UnknownArchitecture;
}

pub fn parseArchTag(text: []const u8) ParseArchSubError!@TagType(Arch) {
const info = @typeInfo(Arch);
inline for (info.Union.fields) |field| {
if (text.len >= field.name.len and mem.eql(u8, text[0..field.name.len], field.name)) {
if (text.len == field.name.len) return @as(@TagType(Arch), @field(Arch, field.name));

if (field.field_type == void) {
return error.UnknownArchitecture;
}

const sub_info = @typeInfo(field.field_type);
inline for (sub_info.Enum.fields) |sub_field| {
const combined = field.name ++ sub_field.name;
if (mem.eql(u8, text, combined)) {
return @as(@TagType(Arch), @field(Arch, field.name));
}
}

return error.UnknownSubArchitecture;
}
}

return error.UnknownArchitecture;
}

pub fn parseOs(text: []const u8) !Os {
const info = @typeInfo(Os);
inline for (info.Enum.fields) |field| {
Expand Down Expand Up @@ -778,3 +803,83 @@ pub const Target = union(enum) {
return .unavailable;
}
};

pub const aarch64 = @import("target/aarch64.zig");
pub const amdgpu = @import("target/amdgpu.zig");
pub const arm = @import("target/arm.zig");
pub const avr = @import("target/avr.zig");
pub const bpf = @import("target/bpf.zig");
pub const hexagon = @import("target/hexagon.zig");
pub const mips = @import("target/mips.zig");
pub const msp430 = @import("target/msp430.zig");
pub const nvptx = @import("target/nvptx.zig");
pub const powerpc = @import("target/powerpc.zig");
pub const riscv = @import("target/riscv.zig");
pub const sparc = @import("target/sparc.zig");
pub const systemz = @import("target/systemz.zig");
pub const wasm = @import("target/wasm.zig");
pub const x86 = @import("target/x86.zig");

pub const Feature = struct {
name: []const u8,
llvm_name: ?[]const u8,
description: []const u8,

dependencies: []*const Feature,
};

pub const Cpu = struct {
name: []const u8,
llvm_name: ?[]const u8,

dependencies: []*const Feature,
};

pub const TargetDetails = union(enum) {
cpu: *const Cpu,
features: []*const Feature,
};

pub fn getFeaturesForArch(arch: @TagType(Target.Arch)) []*const Feature {
return switch (arch) {
.arm, .armeb, .thumb, .thumbeb => arm.features,
.aarch64, .aarch64_be, .aarch64_32 => aarch64.features,
.avr => avr.features,
.bpfel, .bpfeb => bpf.features,
.hexagon => hexagon.features,
.mips, .mipsel, .mips64, .mips64el => mips.features,
.msp430 => msp430.features,
.powerpc, .powerpc64, .powerpc64le => powerpc.features,
.amdgcn => amdgpu.features,
.riscv32, .riscv64 => riscv.features,
.sparc, .sparcv9, .sparcel => sparc.features,
.s390x => systemz.features,
.i386, .x86_64 => x86.features,
.nvptx, .nvptx64 => nvptx.features,
.wasm32, .wasm64 => wasm.features,

else => &[_]*const Feature{},
};
}

pub fn getCpusForArch(arch: @TagType(Target.Arch)) []*const Cpu {
return switch (arch) {
.arm, .armeb, .thumb, .thumbeb => arm.cpus,
.aarch64, .aarch64_be, .aarch64_32 => aarch64.cpus,
.avr => avr.cpus,
.bpfel, .bpfeb => bpf.cpus,
.hexagon => hexagon.cpus,
.mips, .mipsel, .mips64, .mips64el => mips.cpus,
.msp430 => msp430.cpus,
.powerpc, .powerpc64, .powerpc64le => powerpc.cpus,
.amdgcn => amdgpu.cpus,
.riscv32, .riscv64 => riscv.cpus,
.sparc, .sparcv9, .sparcel => sparc.cpus,
.s390x => systemz.cpus,
.i386, .x86_64 => x86.cpus,
.nvptx, .nvptx64 => nvptx.cpus,
.wasm32, .wasm64 => wasm.cpus,

else => &[_]*const Cpu{},
};
}
Loading