From c7f98332383d71a57699426027e82c435e91addc Mon Sep 17 00:00:00 2001 From: Jacob Young Date: Sat, 15 Oct 2022 02:34:34 -0400 Subject: [PATCH] Module: fix early exit conditions during compilation * `--verbose-llvm-ir` should trigger analysis and codegen * `-femit-asm` etc should trigger codegen even with `-fno-emit-bin` Closes #12588 --- src/Module.zig | 18 +++++++++++------- test/standalone.zig | 1 + test/standalone/issue_12588/build.zig | 18 ++++++++++++++++++ test/standalone/issue_12588/main.zig | 6 ++++++ 4 files changed, 36 insertions(+), 7 deletions(-) create mode 100644 test/standalone/issue_12588/build.zig create mode 100644 test/standalone/issue_12588/main.zig diff --git a/src/Module.zig b/src/Module.zig index fd5cf29516..4edba007e9 100644 --- a/src/Module.zig +++ b/src/Module.zig @@ -4319,10 +4319,9 @@ pub fn ensureFuncBodyAnalyzed(mod: *Module, func: *Fn) SemaError!void { comp.emit_llvm_bc == null); const dump_air = builtin.mode == .Debug and comp.verbose_air; + const dump_llvm_ir = builtin.mode == .Debug and comp.verbose_llvm_ir; - if (no_bin_file and !dump_air) { - return; - } + if (no_bin_file and !dump_air and !dump_llvm_ir) return; log.debug("analyze liveness of {s}", .{decl.name}); var liveness = try Liveness.analyze(gpa, air); @@ -4337,9 +4336,7 @@ pub fn ensureFuncBodyAnalyzed(mod: *Module, func: *Fn) SemaError!void { std.debug.print("# End Function AIR: {s}\n\n", .{fqn}); } - if (no_bin_file) { - return; - } + if (no_bin_file and !dump_llvm_ir) return; comp.bin_file.updateFunc(mod, func, air, liveness) catch |err| switch (err) { error.OutOfMemory => return error.OutOfMemory, @@ -6484,7 +6481,14 @@ pub fn populateTestFunctions(mod: *Module) !void { pub fn linkerUpdateDecl(mod: *Module, decl_index: Decl.Index) !void { const comp = mod.comp; - if (comp.bin_file.options.emit == null) return; + const no_bin_file = (comp.bin_file.options.emit == null and + comp.emit_asm == null and + comp.emit_llvm_ir == null and + comp.emit_llvm_bc == null); + + const dump_llvm_ir = builtin.mode == .Debug and comp.verbose_llvm_ir; + + if (no_bin_file and !dump_llvm_ir) return; const decl = mod.declPtr(decl_index); diff --git a/test/standalone.zig b/test/standalone.zig index 9c600dfee5..16cb61a85d 100644 --- a/test/standalone.zig +++ b/test/standalone.zig @@ -103,4 +103,5 @@ pub fn addCases(cases: *tests.StandaloneContext) void { cases.addBuildFile("test/standalone/issue_13030/build.zig", .{ .build_modes = true }); cases.addBuildFile("test/standalone/emit_asm_and_bin/build.zig", .{}); + cases.addBuildFile("test/standalone/issue_12588/build.zig", .{}); } diff --git a/test/standalone/issue_12588/build.zig b/test/standalone/issue_12588/build.zig new file mode 100644 index 0000000000..02fa5e1680 --- /dev/null +++ b/test/standalone/issue_12588/build.zig @@ -0,0 +1,18 @@ +const std = @import("std"); +const Builder = std.build.Builder; + +pub fn build(b: *Builder) void { + const mode = b.standardReleaseOptions(); + const target = b.standardTargetOptions(.{}); + + const obj = b.addObject("main", "main.zig"); + obj.setBuildMode(mode); + obj.setTarget(target); + obj.emit_llvm_ir = .{ .emit_to = b.pathFromRoot("main.ll") }; + obj.emit_llvm_bc = .{ .emit_to = b.pathFromRoot("main.bc") }; + obj.emit_bin = .no_emit; + b.default_step.dependOn(&obj.step); + + const test_step = b.step("test", "Test the program"); + test_step.dependOn(&obj.step); +} diff --git a/test/standalone/issue_12588/main.zig b/test/standalone/issue_12588/main.zig new file mode 100644 index 0000000000..47f67515c7 --- /dev/null +++ b/test/standalone/issue_12588/main.zig @@ -0,0 +1,6 @@ +const std = @import("std"); + +export fn strFromFloatHelp(float: f64) void { + var buf: [400]u8 = undefined; + _ = std.fmt.bufPrint(&buf, "{d}", .{float}) catch unreachable; +}