Skip to content

Commit 78df3c9

Browse files
authored
Merge pull request #23663 from alexrp/emit-asm-only
2 parents 5ed8bd5 + 711e055 commit 78df3c9

File tree

5 files changed

+73
-29
lines changed

5 files changed

+73
-29
lines changed

src/link/Elf.zig

+12-1
Original file line numberDiff line numberDiff line change
@@ -1663,7 +1663,18 @@ fn linkWithLLD(self: *Elf, arena: Allocator, tid: Zcu.PerThread.Id, prog_node: s
16631663
// copy when generating relocatables. Normally, we would expect `lld -r` to work.
16641664
// However, because LLD wants to resolve BPF relocations which it shouldn't, it fails
16651665
// before even generating the relocatable.
1666-
if (output_mode == .Obj and (comp.config.lto != .none or target.cpu.arch.isBpf())) {
1666+
//
1667+
// For m68k, we go through this path because LLD doesn't support it yet, but LLVM can
1668+
// produce usable object files.
1669+
if (output_mode == .Obj and
1670+
(comp.config.lto != .none or
1671+
target.cpu.arch.isBpf() or
1672+
target.cpu.arch == .lanai or
1673+
target.cpu.arch == .m68k or
1674+
target.cpu.arch.isSPARC() or
1675+
target.cpu.arch == .ve or
1676+
target.cpu.arch == .xcore))
1677+
{
16671678
// In this case we must do a simple file copy
16681679
// here. TODO: think carefully about how we can avoid this redundant operation when doing
16691680
// build-obj. See also the corresponding TODO in linkAsArchive.

src/main.zig

+4-2
Original file line numberDiff line numberDiff line change
@@ -3169,9 +3169,11 @@ fn buildOutputType(
31693169

31703170
const target = main_mod.resolved_target.result;
31713171

3172-
if (target.cpu.arch.isNvptx()) {
3172+
if (target.cpu.arch == .arc or target.cpu.arch.isNvptx()) {
31733173
if (emit_bin != .no and create_module.resolved_options.use_llvm) {
3174-
fatal("cannot emit PTX binary with the LLVM backend; only '-femit-asm' is supported", .{});
3174+
fatal("cannot emit {s} binary with the LLVM backend; only '-femit-asm' is supported", .{
3175+
@tagName(target.cpu.arch),
3176+
});
31753177
}
31763178
}
31773179

src/target.zig

+5-2
Original file line numberDiff line numberDiff line change
@@ -153,7 +153,6 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool {
153153
.avr,
154154
.bpfel,
155155
.bpfeb,
156-
.csky,
157156
.hexagon,
158157
.loongarch32,
159158
.loongarch64,
@@ -181,7 +180,6 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool {
181180
.x86,
182181
.x86_64,
183182
.xcore,
184-
.xtensa,
185183
.nvptx,
186184
.nvptx64,
187185
.lanai,
@@ -190,6 +188,11 @@ pub fn hasLlvmSupport(target: std.Target, ofmt: std.Target.ObjectFormat) bool {
190188
.ve,
191189
=> true,
192190

191+
// LLVM backend exists but can produce neither assembly nor object files.
192+
.csky,
193+
.xtensa,
194+
=> false,
195+
193196
// No LLVM backend exists.
194197
.kalimba,
195198
.propeller,

test/llvm_targets.zig

+23-23
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,9 @@ const targets = [_]std.Target.Query{
4444
// .{ .cpu_arch = .amdgcn, .os_tag = .amdpal, .abi = .none },
4545
// .{ .cpu_arch = .amdgcn, .os_tag = .mesa3d, .abi = .none },
4646

47-
// .{ .cpu_arch = .arc, .os_tag = .freestanding, .abi = .none },
48-
// .{ .cpu_arch = .arc, .os_tag = .linux, .abi = .gnu },
49-
// .{ .cpu_arch = .arc, .os_tag = .linux, .abi = .none },
47+
.{ .cpu_arch = .arc, .os_tag = .freestanding, .abi = .none },
48+
.{ .cpu_arch = .arc, .os_tag = .linux, .abi = .gnu },
49+
.{ .cpu_arch = .arc, .os_tag = .linux, .abi = .none },
5050

5151
.{ .cpu_arch = .arm, .os_tag = .freebsd, .abi = .eabi },
5252
.{ .cpu_arch = .arm, .os_tag = .freebsd, .abi = .eabihf },
@@ -101,7 +101,7 @@ const targets = [_]std.Target.Query{
101101
.{ .cpu_arch = .hexagon, .os_tag = .freestanding, .abi = .none },
102102
.{ .cpu_arch = .hexagon, .os_tag = .linux, .abi = .none },
103103

104-
// .{ .cpu_arch = .lanai, .os_tag = .freestanding, .abi = .none },
104+
.{ .cpu_arch = .lanai, .os_tag = .freestanding, .abi = .none },
105105

106106
// .{ .cpu_arch = .loongarch32, .os_tag = .freestanding, .abi = .none },
107107
// .{ .cpu_arch = .loongarch32, .os_tag = .linux, .abi = .none },
@@ -117,13 +117,13 @@ const targets = [_]std.Target.Query{
117117
.{ .cpu_arch = .loongarch64, .os_tag = .linux, .abi = .none },
118118
// .{ .cpu_arch = .loongarch64, .os_tag = .uefi, .abi = .none },
119119

120-
// .{ .cpu_arch = .m68k, .os_tag = .freestanding, .abi = .none },
121-
// .{ .cpu_arch = .m68k, .os_tag = .haiku, .abi = .none },
122-
// .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .gnu },
123-
// .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .musl },
124-
// .{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .none },
125-
// .{ .cpu_arch = .m68k, .os_tag = .netbsd, .abi = .none },
126-
// .{ .cpu_arch = .m68k, .os_tag = .rtems, .abi = .none },
120+
.{ .cpu_arch = .m68k, .os_tag = .freestanding, .abi = .none },
121+
.{ .cpu_arch = .m68k, .os_tag = .haiku, .abi = .none },
122+
.{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .gnu },
123+
.{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .musl },
124+
.{ .cpu_arch = .m68k, .os_tag = .linux, .abi = .none },
125+
.{ .cpu_arch = .m68k, .os_tag = .netbsd, .abi = .none },
126+
.{ .cpu_arch = .m68k, .os_tag = .rtems, .abi = .none },
127127

128128
.{ .cpu_arch = .mips, .os_tag = .freestanding, .abi = .eabi },
129129
.{ .cpu_arch = .mips, .os_tag = .freestanding, .abi = .eabihf },
@@ -171,10 +171,10 @@ const targets = [_]std.Target.Query{
171171

172172
.{ .cpu_arch = .msp430, .os_tag = .freestanding, .abi = .none },
173173

174-
// .{ .cpu_arch = .nvptx, .os_tag = .cuda, .abi = .none },
175-
// .{ .cpu_arch = .nvptx, .os_tag = .nvcl, .abi = .none },
176-
// .{ .cpu_arch = .nvptx64, .os_tag = .cuda, .abi = .none },
177-
// .{ .cpu_arch = .nvptx64, .os_tag = .nvcl, .abi = .none },
174+
.{ .cpu_arch = .nvptx, .os_tag = .cuda, .abi = .none },
175+
.{ .cpu_arch = .nvptx, .os_tag = .nvcl, .abi = .none },
176+
.{ .cpu_arch = .nvptx64, .os_tag = .cuda, .abi = .none },
177+
.{ .cpu_arch = .nvptx64, .os_tag = .nvcl, .abi = .none },
178178

179179
// .{ .cpu_arch = .powerpc, .os_tag = .aix, .abi = .eabihf },
180180
.{ .cpu_arch = .powerpc, .os_tag = .freebsd, .abi = .eabi },
@@ -241,11 +241,11 @@ const targets = [_]std.Target.Query{
241241
.{ .cpu_arch = .s390x, .os_tag = .linux, .abi = .none },
242242
// .{ .cpu_arch = .s390x, .os_tag = .zos, .abi = .none },
243243

244-
// .{ .cpu_arch = .sparc, .os_tag = .freestanding, .abi = .none },
245-
// .{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .gnu },
246-
// .{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .none },
247-
// .{ .cpu_arch = .sparc, .os_tag = .netbsd, .abi = .none },
248-
// .{ .cpu_arch = .sparc, .os_tag = .rtems, .abi = .none },
244+
.{ .cpu_arch = .sparc, .os_tag = .freestanding, .abi = .none },
245+
.{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .gnu },
246+
.{ .cpu_arch = .sparc, .os_tag = .linux, .abi = .none },
247+
.{ .cpu_arch = .sparc, .os_tag = .netbsd, .abi = .none },
248+
.{ .cpu_arch = .sparc, .os_tag = .rtems, .abi = .none },
249249

250250
.{ .cpu_arch = .sparc64, .os_tag = .freestanding, .abi = .none },
251251
.{ .cpu_arch = .sparc64, .os_tag = .haiku, .abi = .none },
@@ -279,8 +279,8 @@ const targets = [_]std.Target.Query{
279279
.{ .cpu_arch = .thumbeb, .os_tag = .rtems, .abi = .eabi },
280280
.{ .cpu_arch = .thumbeb, .os_tag = .rtems, .abi = .eabihf },
281281

282-
// .{ .cpu_arch = .ve, .os_tag = .freestanding, .abi = .none },
283-
// .{ .cpu_arch = .ve, .os_tag = .linux, .abi = .none },
282+
.{ .cpu_arch = .ve, .os_tag = .freestanding, .abi = .none },
283+
.{ .cpu_arch = .ve, .os_tag = .linux, .abi = .none },
284284

285285
.{ .cpu_arch = .wasm32, .os_tag = .emscripten, .abi = .none },
286286
.{ .cpu_arch = .wasm32, .os_tag = .freestanding, .abi = .none },
@@ -350,7 +350,7 @@ const targets = [_]std.Target.Query{
350350
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .itanium },
351351
.{ .cpu_arch = .x86_64, .os_tag = .windows, .abi = .msvc },
352352

353-
// .{ .cpu_arch = .xcore, .os_tag = .freestanding, .abi = .none },
353+
.{ .cpu_arch = .xcore, .os_tag = .freestanding, .abi = .none },
354354

355355
// .{ .cpu_arch = .xtensa, .os_tag = .freestanding, .abi = .none },
356356
// .{ .cpu_arch = .xtensa, .os_tag = .linux, .abi = .none },

test/src/Cases.zig

+29-1
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ pub const Case = struct {
6262
Header: []const u8,
6363
},
6464

65+
emit_asm: bool = false,
6566
emit_bin: bool = true,
6667
emit_h: bool = false,
6768
is_test: bool = false,
@@ -173,6 +174,23 @@ pub fn exeFromCompiledC(ctx: *Cases, name: []const u8, target_query: std.Target.
173174
}
174175

175176
pub fn addObjLlvm(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarget) *Case {
177+
const can_emit_asm = switch (target.result.cpu.arch) {
178+
.csky,
179+
.xtensa,
180+
=> false,
181+
else => true,
182+
};
183+
const can_emit_bin = switch (target.result.cpu.arch) {
184+
.arc,
185+
.csky,
186+
.nvptx,
187+
.nvptx64,
188+
.xcore,
189+
.xtensa,
190+
=> false,
191+
else => true,
192+
};
193+
176194
ctx.cases.append(.{
177195
.name = name,
178196
.target = target,
@@ -181,6 +199,8 @@ pub fn addObjLlvm(ctx: *Cases, name: []const u8, target: std.Build.ResolvedTarge
181199
.output_mode = .Obj,
182200
.deps = std.ArrayList(DepModule).init(ctx.arena),
183201
.backend = .llvm,
202+
.emit_bin = can_emit_bin,
203+
.emit_asm = can_emit_asm,
184204
}) catch @panic("out of memory");
185205
return &ctx.cases.items[ctx.cases.items.len - 1];
186206
}
@@ -371,6 +391,7 @@ fn addFromDirInner(
371391
const output_mode = try manifest.getConfigForKeyAssertSingle("output_mode", std.builtin.OutputMode);
372392
const pic = try manifest.getConfigForKeyAssertSingle("pic", ?bool);
373393
const pie = try manifest.getConfigForKeyAssertSingle("pie", ?bool);
394+
const emit_asm = try manifest.getConfigForKeyAssertSingle("emit_asm", bool);
374395
const emit_bin = try manifest.getConfigForKeyAssertSingle("emit_bin", bool);
375396
const imports = try manifest.getConfigForKeyAlloc(ctx.arena, "imports", []const u8);
376397

@@ -438,6 +459,7 @@ fn addFromDirInner(
438459
.backend = backend,
439460
.files = .init(ctx.arena),
440461
.case = null,
462+
.emit_asm = emit_asm,
441463
.emit_bin = emit_bin,
442464
.is_test = is_test,
443465
.output_mode = output_mode,
@@ -663,7 +685,10 @@ pub fn lowerToBuildSteps(
663685

664686
switch (case.case.?) {
665687
.Compile => {
666-
// Force the binary to be emitted if requested.
688+
// Force the assembly/binary to be emitted if requested.
689+
if (case.emit_asm) {
690+
_ = artifact.getEmittedAsm();
691+
}
667692
if (case.emit_bin) {
668693
_ = artifact.getEmittedBin();
669694
}
@@ -761,6 +786,8 @@ const TestManifestConfigDefaults = struct {
761786
.run_translated_c => "Obj",
762787
.cli => @panic("TODO test harness for CLI tests"),
763788
};
789+
} else if (std.mem.eql(u8, key, "emit_asm")) {
790+
return "false";
764791
} else if (std.mem.eql(u8, key, "emit_bin")) {
765792
return "true";
766793
} else if (std.mem.eql(u8, key, "is_test")) {
@@ -802,6 +829,7 @@ const TestManifest = struct {
802829
trailing_bytes: []const u8 = "",
803830

804831
const valid_keys = std.StaticStringMap(void).initComptime(.{
832+
.{ "emit_asm", {} },
805833
.{ "emit_bin", {} },
806834
.{ "is_test", {} },
807835
.{ "output_mode", {} },

0 commit comments

Comments
 (0)