authorgravatar for jacobly@ziglang.orgJacob Young <jacobly@ziglang.org> 2022-10-14 06:29:24-04:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2022-10-14 14:46:50-04:00
log9b45dc1608e44377bd1f972694809e0a4d34a8b8
tree90551f618f74adb112937364c963243a5a93c904
parentcb257d59f97ea5655bf453d8e7f07bbfb0a88e58

stage2: fix emitting asm and bin at the same time

This logic is copied from stage1. Fixes #12800

4 files changed, 41 insertions(+), 1 deletions(-)

src/codegen/llvm.zig+28-1
......@@ -780,7 +780,7 @@ pub const Object = struct {
780780 null;
781781
782782 const emit_asm_path = try locPath(arena, comp.emit_asm, cache_dir);
783 const emit_llvm_ir_path = try locPath(arena, comp.emit_llvm_ir, cache_dir);
783 var emit_llvm_ir_path = try locPath(arena, comp.emit_llvm_ir, cache_dir);
784784 const emit_llvm_bc_path = try locPath(arena, comp.emit_llvm_bc, cache_dir);
785785
786786 const emit_asm_msg = emit_asm_path orelse "(none)";
......@@ -791,7 +791,34 @@ pub const Object = struct {
791791 emit_asm_msg, emit_bin_msg, emit_llvm_ir_msg, emit_llvm_bc_msg,
792792 });
793793
794 // Unfortunately, LLVM shits the bed when we ask for both binary and assembly.
795 // So we call the entire pipeline multiple times if this is requested.
794796 var error_message: [*:0]const u8 = undefined;
797 if (emit_asm_path != null and emit_bin_path != null) {
798 if (self.target_machine.emitToFile(
799 self.llvm_module,
800 &error_message,
801 comp.bin_file.options.optimize_mode == .Debug,
802 comp.bin_file.options.optimize_mode == .ReleaseSmall,
803 comp.time_report,
804 comp.bin_file.options.tsan,
805 comp.bin_file.options.lto,
806 null,
807 emit_bin_path,
808 emit_llvm_ir_path,
809 null,
810 )) {
811 defer llvm.disposeMessage(error_message);
812
813 log.err("LLVM failed to emit bin={s} ir={s}: {s}", .{
814 emit_bin_msg, emit_llvm_ir_msg, error_message,
815 });
816 return error.FailedToEmit;
817 }
818 emit_bin_path = null;
819 emit_llvm_ir_path = null;
820 }
821
795822 if (self.target_machine.emitToFile(
796823 self.llvm_module,
797824 &error_message,
test/standalone.zig+1
......@@ -99,4 +99,5 @@ pub fn addCases(cases: *tests.StandaloneContext) void {
9999 //cases.add("tools/update_spirv_features.zig");
100100
101101 cases.addBuildFile("test/standalone/issue_13030/build.zig", .{ .build_modes = true });
102 cases.addBuildFile("test/standalone/emit_asm_and_bin/build.zig", .{});
102103}
test/standalone/emit_asm_and_bin/build.zig created+11
......@@ -0,0 +1,11 @@
1const Builder = @import("std").build.Builder;
2
3pub fn build(b: *Builder) void {
4 const main = b.addTest("main.zig");
5 main.setBuildMode(b.standardReleaseOptions());
6 main.emit_asm = .{ .emit_to = b.pathFromRoot("main.s") };
7 main.emit_bin = .{ .emit_to = b.pathFromRoot("main") };
8
9 const test_step = b.step("test", "Run test");
10 test_step.dependOn(&main.step);
11}
test/standalone/emit_asm_and_bin/main.zig created+1
......@@ -0,0 +1 @@
1pub fn main() void {}