From 3e7708b02b48595f004083b17a05363d9fb2fa57 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sat, 26 Apr 2025 16:51:16 +0200 Subject: [PATCH 1/6] link.Elf: Skip invoking LLD for `zig build-obj` for unsupported targets. LLD doesn't support these yet. Doing this hack will at least allow basic experimentation for these targets. --- src/link/Elf.zig | 13 ++++++++++++- 1 file changed, 12 insertions(+), 1 deletion(-) diff --git a/src/link/Elf.zig b/src/link/Elf.zig index a338ec722d..ea4037b33c 100644 --- a/src/link/Elf.zig +++ b/src/link/Elf.zig @@ -1663,7 +1663,18 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s // copy when generating relocatables. Normally, we would expect `lld -r` to work. // However, because LLD wants to resolve BPF relocations which it shouldn't, it fails // before even generating the relocatable. - if (output_mode == .Obj and (comp.config.lto != .none or target.cpu.arch.isBpf())) { + // + // For m68k, we go through this path because LLD doesn't support it yet, but LLVM can + // produce usable object files. + if (output_mode == .Obj and + (comp.config.lto != .none or + target.cpu.arch.isBpf() or + target.cpu.arch == .lanai or + target.cpu.arch == .m68k or + target.cpu.arch.isSPARC() or + target.cpu.arch == .ve or + target.cpu.arch == .xcore)) + { // In this case we must do a simple file copy // here. TODO: think carefully about how we can avoid this redundant operation when doing // build-obj. See also the corresponding TODO in linkAsArchive. From 386b869ec43dbc7f5f2da809ad43ee3056187107 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sat, 26 Apr 2025 16:55:50 +0200 Subject: [PATCH 2/6] compiler: Error if the user targets arc with -femit-bin. LLVM can only produce assembly files for this target currently. --- src/main.zig | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/main.zig b/src/main.zig index f05208324f..cac0c78163 100644 --- a/src/main.zig +++ b/src/main.zig @@ -3169,9 +3169,11 @@ fn buildOutputType( const target = main_mod.resolved_target.result; - if (target.cpu.arch.isNvptx()) { + if (target.cpu.arch == .arc or target.cpu.arch.isNvptx()) { if (emit_bin != .no and create_module.resolved_options.use_llvm) { - fatal("cannot emit PTX binary with the LLVM backend; only '-femit-asm' is supported", .{}); + fatal("cannot emit {s} binary with the LLVM backend; only '-femit-asm' is supported", .{ + @tagName(target.cpu.arch), + }); } } From 26bb429093141e70e136da1f7861e2d6473328df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sat, 26 Apr 2025 16:59:40 +0200 Subject: [PATCH 3/6] compiler: Reject using LLVM for csky and xtensa. These backends are completely unusable at the moment; they can produce neither assembly files nor object files. So give a nicer error when users try to use them. --- src/target.zig | 7 +++++-- 1 file changed, 5 insertions(+), 2 deletions(-) diff --git a/src/target.zig b/src/target.zig index 2a2bffc572..7179868f42 100644 --- a/src/target.zig +++ b/src/target.zig @@ -153,7 +153,6 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool { .avr, .bpfel, .bpfeb, - .csky, .hexagon, .loongarch32, .loongarch64, @@ -181,7 +180,6 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool { .x86, .x86_64, .xcore, - .xtensa, .nvptx, .nvptx64, .lanai, @@ -190,6 +188,11 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool { .ve, => true, + // LLVM backend exists but can produce neither assembly nor object files. + .csky, + .xtensa, + => false, + // No LLVM backend exists. .kalimba, .propeller, From 5411358956ef1c07e28ab477b10b66ade1000c58 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sun, 27 Apr 2025 00:05:20 +0200 Subject: [PATCH 4/6] test: Allow cases to set emit_asm (defaults to false). --- test/src/Cases.zig | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 5f0c12c2b2..367db9765d 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -62,6 +62,7 @@ pub const Case = struct { Header: []const u8, }, + emit_asm: bool = false, emit_bin: bool = true, emit_h: bool = false, is_test: bool = false, @@ -371,6 +372,7 @@ fn addFromDirInner( const output_mode = try manifest.getConfigForKeyAssertSingle("output_mode", std.builtin.OutputMode); const pic = try manifest.getConfigForKeyAssertSingle("pic", ?bool); const pie = try manifest.getConfigForKeyAssertSingle("pie", ?bool); + const emit_asm = try manifest.getConfigForKeyAssertSingle("emit_asm", bool); const emit_bin = try manifest.getConfigForKeyAssertSingle("emit_bin", bool); const imports = try manifest.getConfigForKeyAlloc(ctx.arena, "imports", []const u8); @@ -438,6 +440,7 @@ fn addFromDirInner( .backend = backend, .files = .init(ctx.arena), .case = null, + .emit_asm = emit_asm, .emit_bin = emit_bin, .is_test = is_test, .output_mode = output_mode, @@ -663,7 +666,10 @@ pub fn lowerToBuildSteps( switch (case.case.?) { .Compile => { - // Force the binary to be emitted if requested. + // Force the assembly/binary to be emitted if requested. + if (case.emit_asm) { + _ = artifact.getEmittedAsm(); + } if (case.emit_bin) { _ = artifact.getEmittedBin(); } @@ -761,6 +767,8 @@ const TestManifestConfigDefaults = struct { .run_translated_c => "Obj", .cli => @panic("TODO test harness for CLI tests"), }; + } else if (std.mem.eql(u8, key, "emit_asm")) { + return "false"; } else if (std.mem.eql(u8, key, "emit_bin")) { return "true"; } else if (std.mem.eql(u8, key, "is_test")) { @@ -802,6 +810,7 @@ const TestManifest = struct { trailing_bytes: []const u8 = "", const valid_keys = std.StaticStringMap(void).initComptime(.{ + .{ "emit_asm", {} }, .{ "emit_bin", {} }, .{ "is_test", {} }, .{ "output_mode", {} }, From b31b309b53ccafd8f1de01cfe1065c84d147a1c7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sun, 27 Apr 2025 00:05:37 +0200 Subject: [PATCH 5/6] test: Configure emit_asm/emit_bin correctly for some targets in llvm_targets. --- test/src/Cases.zig | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/test/src/Cases.zig b/test/src/Cases.zig index 367db9765d..34ca62a49b 100644 --- a/test/src/Cases.zig +++ b/test/src/Cases.zig @@ -174,6 +174,23 @@ pub fn exeFromCompiledC(ctx: *Cases, name: []const u8, target_query: std.Target. } pub fn addObjLlvm(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarget) *Case { + const can_emit_asm = switch (target.result.cpu.arch) { + .csky, + .xtensa, + => false, + else => true, + }; + const can_emit_bin = switch (target.result.cpu.arch) { + .arc, + .csky, + .nvptx, + .nvptx64, + .xcore, + .xtensa, + => false, + else => true, + }; + ctx.cases.append(.{ .name = name, .target = target, @@ -182,6 +199,8 @@ pub fn addObjLlvm(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarge .output_mode = .Obj, .deps = std.ArrayList(DepModule).init(ctx.arena), .backend = .llvm, + .emit_bin = can_emit_bin, + .emit_asm = can_emit_asm, }) catch @panic("out of memory"); return &ctx.cases.items[ctx.cases.items.len - 1]; } From 711e055f1852be93dd74dcbbb5777efc30f43506 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alex=20R=C3=B8nne=20Petersen?= Date: Sun, 27 Apr 2025 00:04:57 +0200 Subject: [PATCH 6/6] test: Uncomment a bunch of targets in llvm_targets. --- test/llvm_targets.zig | 46 +++++++++++++++++++++---------------------- 1 file changed, 23 insertions(+), 23 deletions(-) diff --git a/test/llvm_targets.zig b/test/llvm_targets.zig index 33d7be42a9..59767ae097 100644 --- a/test/llvm_targets.zig +++ b/test/llvm_targets.zig @@ -44,9 +44,9 @@ const targets = [_]std.Target.Query{ // .{ .cpu_arch = .amdgcn, .os_tag = .amdpal, .abi = .none }, // .{ .cpu_arch = .amdgcn, .os_tag = .mesa3d, .abi = .none }, - // .{ .cpu_arch = .arc, .os_tag = .freestanding, .abi = .none }, - // .{ .cpu_arch = .arc, .os_tag = .linux, .abi = .gnu }, - // .{ .cpu_arch = .arc, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .arc, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .arc, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .arc, .os_tag = .linux, .abi = .none }, .{ .cpu_arch = .arm, .os_tag = .freebsd, .abi = .eabi }, .{ .cpu_arch = .arm, .os_tag = .freebsd, .abi = .eabihf }, @@ -101,7 +101,7 @@ const targets = [_]std.Target.Query{ .{ .cpu_arch = .hexagon, .os_tag = .freestanding, .abi = .none }, .{ .cpu_arch = .hexagon, .os_tag = .linux, .abi = .none }, - // .{ .cpu_arch = .lanai, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .lanai, .os_tag = .freestanding, .abi = .none }, // .{ .cpu_arch = .loongarch32, .os_tag = .freestanding, .abi = .none }, // .{ .cpu_arch = .loongarch32, .os_tag = .linux, .abi = .none }, @@ -117,13 +117,13 @@ const targets = [_]std.Target.Query{ .{ .cpu_arch = .loongarch64, .os_tag = .linux, .abi = .none }, // .{ .cpu_arch = .loongarch64, .os_tag = .uefi, .abi = .none }, - // .{ .cpu_arch = .m68k, .os_tag = .freestanding, .abi = .none }, - // .{ .cpu_arch = .m68k, .os_tag = .haiku, .abi = .none }, - // .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .gnu }, - // .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .musl }, - // .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .none }, - // .{ .cpu_arch = .m68k, .os_tag = .netbsd, .abi = .none }, - // .{ .cpu_arch = .m68k, .os_tag = .rtems, .abi = .none }, + .{ .cpu_arch = .m68k, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .m68k, .os_tag = .haiku, .abi = .none }, + .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .musl }, + .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .m68k, .os_tag = .netbsd, .abi = .none }, + .{ .cpu_arch = .m68k, .os_tag = .rtems, .abi = .none }, .{ .cpu_arch = .mips, .os_tag = .freestanding, .abi = .eabi }, .{ .cpu_arch = .mips, .os_tag = .freestanding, .abi = .eabihf }, @@ -171,10 +171,10 @@ const targets = [_]std.Target.Query{ .{ .cpu_arch = .msp430, .os_tag = .freestanding, .abi = .none }, - // .{ .cpu_arch = .nvptx, .os_tag = .cuda, .abi = .none }, - // .{ .cpu_arch = .nvptx, .os_tag = .nvcl, .abi = .none }, - // .{ .cpu_arch = .nvptx64, .os_tag = .cuda, .abi = .none }, - // .{ .cpu_arch = .nvptx64, .os_tag = .nvcl, .abi = .none }, + .{ .cpu_arch = .nvptx, .os_tag = .cuda, .abi = .none }, + .{ .cpu_arch = .nvptx, .os_tag = .nvcl, .abi = .none }, + .{ .cpu_arch = .nvptx64, .os_tag = .cuda, .abi = .none }, + .{ .cpu_arch = .nvptx64, .os_tag = .nvcl, .abi = .none }, // .{ .cpu_arch = .powerpc, .os_tag = .aix, .abi = .eabihf }, .{ .cpu_arch = .powerpc, .os_tag = .freebsd, .abi = .eabi }, @@ -241,11 +241,11 @@ const targets = [_]std.Target.Query{ .{ .cpu_arch = .s390x, .os_tag = .linux, .abi = .none }, // .{ .cpu_arch = .s390x, .os_tag = .zos, .abi = .none }, - // .{ .cpu_arch = .sparc, .os_tag = .freestanding, .abi = .none }, - // .{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .gnu }, - // .{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .none }, - // .{ .cpu_arch = .sparc, .os_tag = .netbsd, .abi = .none }, - // .{ .cpu_arch = .sparc, .os_tag = .rtems, .abi = .none }, + .{ .cpu_arch = .sparc, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .gnu }, + .{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .sparc, .os_tag = .netbsd, .abi = .none }, + .{ .cpu_arch = .sparc, .os_tag = .rtems, .abi = .none }, .{ .cpu_arch = .sparc64, .os_tag = .freestanding, .abi = .none }, .{ .cpu_arch = .sparc64, .os_tag = .haiku, .abi = .none }, @@ -279,8 +279,8 @@ const targets = [_]std.Target.Query{ .{ .cpu_arch = .thumbeb, .os_tag = .rtems, .abi = .eabi }, .{ .cpu_arch = .thumbeb, .os_tag = .rtems, .abi = .eabihf }, - // .{ .cpu_arch = .ve, .os_tag = .freestanding, .abi = .none }, - // .{ .cpu_arch = .ve, .os_tag = .linux, .abi = .none }, + .{ .cpu_arch = .ve, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .ve, .os_tag = .linux, .abi = .none }, .{ .cpu_arch = .wasm32, .os_tag = .emscripten, .abi = .none }, .{ .cpu_arch = .wasm32, .os_tag = .freestanding, .abi = .none }, @@ -351,7 +351,7 @@ const targets = [_]std.Target.Query{ .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .itanium }, .{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .msvc }, - // .{ .cpu_arch = .xcore, .os_tag = .freestanding, .abi = .none }, + .{ .cpu_arch = .xcore, .os_tag = .freestanding, .abi = .none }, // .{ .cpu_arch = .xtensa, .os_tag = .freestanding, .abi = .none }, // .{ .cpu_arch = .xtensa, .os_tag = .linux, .abi = .none },