authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-12-03 16:37:56-08:00
committergravatar for noreply@github.comGitHub <noreply@github.com> 2021-12-03 16:37:56-08:00
log3311ef3262c6d650d7ef23ad62ae0483835f79c4
tree922d865e78b10012f773c292795b9d7479894bdd
parent84704ef43e5414860aa9d3dc0f94f9737404f6e5
parent4c1a62326b5090fad67c004a6e58a6e4265a77ad
signaturebadge-question-mark Signed by PGP key 4AEE18F83AFDEB23

Merge pull request #10006 from akovaski/riscv-default-abi

Default target-abi based on RISC-V extensions and user selectable -mabi/target-abi

6 files changed, 69 insertions(+), 33 deletions(-)

src/Compilation.zig+28-16
......@@ -992,6 +992,12 @@ pub fn create(gpa: Allocator, options: InitOptions) !*Compilation {
992992 } else if (options.target.os.tag == .windows and link_libcpp) {
993993 // https://github.com/ziglang/zig/issues/8531
994994 break :blk false;
995 } else if (options.target.cpu.arch.isRISCV()) {
996 // Clang and LLVM currently don't support RISC-V target-abi for LTO.
997 // Compiling with LTO may fail or produce undesired results.
998 // See https://reviews.llvm.org/D71387
999 // See https://reviews.llvm.org/D102582
1000 break :blk false;
9951001 } else switch (options.output_mode) {
9961002 .Lib, .Obj => break :blk false,
9971003 .Exe => switch (options.optimize_mode) {
......@@ -1780,8 +1786,8 @@ pub fn getTarget(self: Compilation) Target {
17801786
17811787/// Detect changes to source files, perform semantic analysis, and update the output files.
17821788pub fn update(self: *Compilation) !void {
1783 const t = trace(@src());
1784 defer t.end();
1789 const tracy_trace = trace(@src());
1790 defer tracy_trace.end();
17851791
17861792 self.clearMiscFailures();
17871793
......@@ -2822,8 +2828,8 @@ pub fn cImport(comp: *Compilation, c_src: []const u8) !CImportResult {
28222828 if (!build_options.have_llvm)
28232829 return error.ZigCompilerNotBuiltWithLLVMExtensions;
28242830
2825 const t = trace(@src());
2826 defer t.end();
2831 const tracy_trace = trace(@src());
2832 defer tracy_trace.end();
28272833
28282834 const cimport_zig_basename = "cimport.zig";
28292835
......@@ -3077,8 +3083,8 @@ fn updateCObject(comp: *Compilation, c_object: *CObject, c_obj_prog_node: *std.P
30773083 const self_exe_path = comp.self_exe_path orelse
30783084 return comp.failCObj(c_object, "clang compilation disabled", .{});
30793085
3080 const t = trace(@src());
3081 defer t.end();
3086 const tracy_trace = trace(@src());
3087 defer tracy_trace.end();
30823088
30833089 log.debug("updating C object: {s}", .{c_object.src.src_path});
30843090
......@@ -3590,6 +3596,11 @@ pub fn addCCArgs(
35903596 }
35913597 },
35923598 }
3599
3600 if (target_util.llvmMachineAbi(target)) |mabi| {
3601 try argv.append(try std.fmt.allocPrint(arena, "-mabi={s}", .{mabi}));
3602 }
3603
35933604 if (out_dep_path) |p| {
35943605 try argv.appendSlice(&[_][]const u8{ "-MD", "-MV", "-MF", p });
35953606 }
......@@ -4036,8 +4047,8 @@ fn wantBuildLibUnwindFromSource(comp: *Compilation) bool {
40364047}
40374048
40384049fn updateBuiltinZigFile(comp: *Compilation, mod: *Module) Allocator.Error!void {
4039 const t = trace(@src());
4040 defer t.end();
4050 const tracy_trace = trace(@src());
4051 defer tracy_trace.end();
40414052
40424053 const source = try comp.generateBuiltinZigSource(comp.gpa);
40434054 defer comp.gpa.free(source);
......@@ -4074,8 +4085,8 @@ pub fn dump_argv(argv: []const []const u8) void {
40744085}
40754086
40764087pub fn generateBuiltinZigSource(comp: *Compilation, allocator: Allocator) Allocator.Error![]u8 {
4077 const t = trace(@src());
4078 defer t.end();
4088 const tracy_trace = trace(@src());
4089 defer tracy_trace.end();
40794090
40804091 var buffer = std.ArrayList(u8).init(allocator);
40814092 defer buffer.deinit();
......@@ -4320,8 +4331,8 @@ fn buildOutputFromZig(
43204331 out: *?CRTFile,
43214332 misc_task_tag: MiscTask,
43224333) !void {
4323 const t = trace(@src());
4324 defer t.end();
4334 const tracy_trace = trace(@src());
4335 defer tracy_trace.end();
43254336
43264337 std.debug.assert(output_mode != .Exe);
43274338 const special_sub = "std" ++ std.fs.path.sep_str ++ "special";
......@@ -4419,8 +4430,8 @@ fn buildOutputFromZig(
44194430}
44204431
44214432fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node) !void {
4422 const t = trace(@src());
4423 defer t.end();
4433 const tracy_trace = trace(@src());
4434 defer tracy_trace.end();
44244435
44254436 var arena_allocator = std.heap.ArenaAllocator.init(comp.gpa);
44264437 defer arena_allocator.deinit();
......@@ -4566,6 +4577,7 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
45664577 .is_native_cpu = false, // Only true when bootstrapping the compiler.
45674578 .llvm_cpu_name = if (target.cpu.model.llvm_name) |s| s.ptr else null,
45684579 .llvm_cpu_features = comp.bin_file.options.llvm_cpu_features.?,
4580 .llvm_target_abi = if (target_util.llvmMachineAbi(target)) |s| s.ptr else null,
45694581 };
45704582
45714583 comp.stage1_cache_manifest = &man;
......@@ -4773,8 +4785,8 @@ pub fn build_crt_file(
47734785 output_mode: std.builtin.OutputMode,
47744786 c_source_files: []const Compilation.CSourceFile,
47754787) !void {
4776 const t = trace(@src());
4777 defer t.end();
4788 const tracy_trace = trace(@src());
4789 defer tracy_trace.end();
47784790
47794791 const target = comp.getTarget();
47804792 const basename = try std.zig.binNameAlloc(comp.gpa, .{
src/codegen/llvm.zig+2-14
......@@ -15,6 +15,7 @@ const TypedValue = @import("../TypedValue.zig");
1515const Zir = @import("../Zir.zig");
1616const Air = @import("../Air.zig");
1717const Liveness = @import("../Liveness.zig");
18const target_util = @import("../target.zig");
1819
1920const Value = @import("../value.zig").Value;
2021const Type = @import("../type.zig").Type;
......@@ -244,19 +245,6 @@ pub const Object = struct {
244245 // TODO handle float ABI better- it should depend on the ABI portion of std.Target
245246 const float_abi: llvm.ABIType = .Default;
246247
247 // TODO a way to override this as part of std.Target ABI?
248 const abi_name: ?[*:0]const u8 = switch (options.target.cpu.arch) {
249 .riscv32 => switch (options.target.os.tag) {
250 .linux => "ilp32d",
251 else => "ilp32",
252 },
253 .riscv64 => switch (options.target.os.tag) {
254 .linux => "lp64d",
255 else => "lp64",
256 },
257 else => null,
258 };
259
260248 const target_machine = llvm.TargetMachine.create(
261249 target,
262250 llvm_target_triple.ptr,
......@@ -267,7 +255,7 @@ pub const Object = struct {
267255 code_model,
268256 options.function_sections,
269257 float_abi,
270 abi_name,
258 if (target_util.llvmMachineAbi(options.target)) |s| s.ptr else null,
271259 );
272260 errdefer target_machine.dispose();
273261
src/stage1.zig+1
......@@ -364,6 +364,7 @@ pub const Stage2Target = extern struct {
364364
365365 llvm_cpu_name: ?[*:0]const u8,
366366 llvm_cpu_features: ?[*:0]const u8,
367 llvm_target_abi: ?[*:0]const u8,
367368};
368369
369370// ABI warning
src/stage1/codegen.cpp+2-3
......@@ -9487,9 +9487,8 @@ static void init(CodeGen *g) {
94879487 // TODO handle float ABI better- it should depend on the ABI portion of std.Target
94889488 ZigLLVMABIType float_abi = ZigLLVMABITypeDefault;
94899489
9490 // TODO a way to override this as part of std.Target ABI?
9491 const char *abi_name = nullptr;
9492 if (target_is_riscv(g->zig_target)) {
9490 const char *abi_name = g->zig_target->llvm_target_abi;
9491 if (abi_name == nullptr && target_is_riscv(g->zig_target)) {
94939492 // RISC-V Linux defaults to ilp32d/lp64d
94949493 if (g->zig_target->os == OsLinux) {
94959494 abi_name = (g->zig_target->arch == ZigLLVM_riscv32) ? "ilp32d" : "lp64d";
src/stage1/stage1.h+1
......@@ -112,6 +112,7 @@ struct ZigTarget {
112112
113113 const char *llvm_cpu_name;
114114 const char *llvm_cpu_features;
115 const char *llvm_target_abi;
115116};
116117
117118// ABI warning
src/target.zig+35
......@@ -599,3 +599,38 @@ pub fn defaultAddressSpace(
599599 _ = context;
600600 return .generic;
601601}
602
603pub fn llvmMachineAbi(target: std.Target) ?[:0]const u8 {
604 const have_float = switch (target.abi) {
605 .gnuilp32 => return "ilp32",
606 .gnueabihf, .musleabihf, .eabihf => true,
607 else => false,
608 };
609
610 switch (target.cpu.arch) {
611 .riscv64 => {
612 const featureSetHas = std.Target.riscv.featureSetHas;
613 if (featureSetHas(target.cpu.features, .d)) {
614 return "lp64d";
615 } else if (have_float) {
616 return "lp64f";
617 } else {
618 return "lp64";
619 }
620 },
621 .riscv32 => {
622 const featureSetHas = std.Target.riscv.featureSetHas;
623 if (featureSetHas(target.cpu.features, .d)) {
624 return "ilp32d";
625 } else if (have_float) {
626 return "ilp32f";
627 } else if (featureSetHas(target.cpu.features, .e)) {
628 return "ilp32e";
629 } else {
630 return "ilp32";
631 }
632 },
633 //TODO add ARM, Mips, and PowerPC
634 else => return null,
635 }
636}