Merge pull request #23663 from alexrp/emit-asm-only

This commit is contained in:
Alex Rønne Petersen 2025-04-28 02:18:45 +02:00 committed by GitHub
commit 78df3c9301
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
5 changed files with 73 additions and 29 deletions

View file

@ -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.

View file

@ -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),
});
}
}

View file

@ -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,

View file

@ -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 },
@ -350,7 +350,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 },

View file

@ -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,
@ -173,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,
@ -181,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];
}
@ -371,6 +391,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 +459,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 +685,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 +786,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 +829,7 @@ const TestManifest = struct {
trailing_bytes: []const u8 = "",
const valid_keys = std.StaticStringMap(void).initComptime(.{
.{ "emit_asm", {} },
.{ "emit_bin", {} },
.{ "is_test", {} },
.{ "output_mode", {} },