authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-14 21:18:09-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-07-14 21:18:09-07:00
log716b128a24ffc44a8694f3d61e3d74b5297fd564
treea3d37e3df26af301e478162229ebe2ea481cf2ee
parentabf895595189eb45df8c97f4029c58976815b450

frontend: add -fincremental, -fno-incremental flag

Remove --debug-incremental This flag is also added to the build system. Importantly, this tells Compile step whether or not to keep the compiler running between rebuilds. It defaults off because it is currently crashing zirUpdateRefs.

8 files changed, 36 insertions(+), 21 deletions(-)

lib/compiler/build_runner.zig+7
......@@ -72,6 +72,7 @@ pub fn main() !void {
7272 .query = .{},
7373 .result = try std.zig.system.resolveTargetQuery(.{}),
7474 },
75 .incremental = null,
7576 };
7677
7778 graph.cache.addPrefix(.{ .path = null, .handle = std.fs.cwd() });
......@@ -235,6 +236,10 @@ pub fn main() !void {
235236 prominent_compile_errors = true;
236237 } else if (mem.eql(u8, arg, "--watch")) {
237238 watch = true;
239 } else if (mem.eql(u8, arg, "-fincremental")) {
240 graph.incremental = true;
241 } else if (mem.eql(u8, arg, "-fno-incremental")) {
242 graph.incremental = false;
238243 } else if (mem.eql(u8, arg, "-fwine")) {
239244 builder.enable_wine = true;
240245 } else if (mem.eql(u8, arg, "-fno-wine")) {
......@@ -1216,6 +1221,8 @@ fn usage(b: *std.Build, out_stream: anytype) !void {
12161221 \\ --fetch Exit after fetching dependency tree
12171222 \\ --watch Continuously rebuild when source files are modified
12181223 \\ --debounce <ms> Delay before rebuilding after changed file detected
1224 \\ -fincremental Enable incremental compilation
1225 \\ -fno-incremental Disable incremental compilation
12191226 \\
12201227 \\Project-Specific Options:
12211228 \\
lib/std/Build.zig+1
......@@ -120,6 +120,7 @@ pub const Graph = struct {
120120 needed_lazy_dependencies: std.StringArrayHashMapUnmanaged(void) = .{},
121121 /// Information about the native target. Computed before build() is invoked.
122122 host: ResolvedTarget,
123 incremental: ?bool,
123124};
124125
125126const AvailableDeps = []const struct { []const u8, []const u8 };
lib/std/Build/Step/Compile.zig+3-1
......@@ -1679,6 +1679,8 @@ fn getZigArgs(compile: *Compile) ![][]const u8 {
16791679 b.fmt("{}", .{err_limit}),
16801680 });
16811681
1682 try addFlag(&zig_args, "incremental", b.graph.incremental);
1683
16821684 try zig_args.append("--listen=-");
16831685
16841686 // Windows has an argument length limit of 32,766 characters, macOS 262,144 and Linux
......@@ -1750,7 +1752,7 @@ fn make(step: *Step, options: Step.MakeOptions) !void {
17501752 const maybe_output_bin_path = step.evalZigProcess(
17511753 zig_args,
17521754 options.progress_node,
1753 options.watch,
1755 (b.graph.incremental == true) and options.watch,
17541756 ) catch |err| switch (err) {
17551757 error.NeedCompileErrorCheck => {
17561758 assert(compile.expect_errors != null);
src/Compilation.zig+3-3
......@@ -169,7 +169,7 @@ time_report: bool,
169169stack_report: bool,
170170debug_compiler_runtime_libs: bool,
171171debug_compile_errors: bool,
172debug_incremental: bool,
172incremental: bool,
173173job_queued_compiler_rt_lib: bool = false,
174174job_queued_compiler_rt_obj: bool = false,
175175job_queued_update_builtin_zig: bool,
......@@ -1134,7 +1134,7 @@ pub const CreateOptions = struct {
11341134 verbose_llvm_cpu_features: bool = false,
11351135 debug_compiler_runtime_libs: bool = false,
11361136 debug_compile_errors: bool = false,
1137 debug_incremental: bool = false,
1137 incremental: bool = false,
11381138 /// Normally when you create a `Compilation`, Zig will automatically build
11391139 /// and link in required dependencies, such as compiler-rt and libc. When
11401140 /// building such dependencies themselves, this flag must be set to avoid
......@@ -1516,7 +1516,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
15161516 .test_name_prefix = options.test_name_prefix,
15171517 .debug_compiler_runtime_libs = options.debug_compiler_runtime_libs,
15181518 .debug_compile_errors = options.debug_compile_errors,
1519 .debug_incremental = options.debug_incremental,
1519 .incremental = options.incremental,
15201520 .libcxx_abi_version = options.libcxx_abi_version,
15211521 .root_name = root_name,
15221522 .sysroot = sysroot,
src/Sema.zig+6-6
......@@ -2726,7 +2726,7 @@ fn maybeRemoveOutdatedType(sema: *Sema, ty: InternPool.Index) !bool {
27262726 const pt = sema.pt;
27272727 const zcu = pt.zcu;
27282728
2729 if (!zcu.comp.debug_incremental) return false;
2729 if (!zcu.comp.incremental) return false;
27302730
27312731 const decl_index = Type.fromInterned(ty).getOwnerDecl(zcu);
27322732 const decl_as_depender = AnalUnit.wrap(.{ .decl = decl_index });
......@@ -2826,7 +2826,7 @@ fn zirStructDecl(
28262826 mod.declPtr(new_decl_index).owns_tv = true;
28272827 errdefer pt.abortAnonDecl(new_decl_index);
28282828
2829 if (pt.zcu.comp.debug_incremental) {
2829 if (pt.zcu.comp.incremental) {
28302830 try ip.addDependency(
28312831 sema.gpa,
28322832 AnalUnit.wrap(.{ .decl = new_decl_index }),
......@@ -3064,7 +3064,7 @@ fn zirEnumDecl(
30643064 new_decl.owns_tv = true;
30653065 errdefer if (!done) pt.abortAnonDecl(new_decl_index);
30663066
3067 if (pt.zcu.comp.debug_incremental) {
3067 if (pt.zcu.comp.incremental) {
30683068 try mod.intern_pool.addDependency(
30693069 gpa,
30703070 AnalUnit.wrap(.{ .decl = new_decl_index }),
......@@ -3331,7 +3331,7 @@ fn zirUnionDecl(
33313331 mod.declPtr(new_decl_index).owns_tv = true;
33323332 errdefer pt.abortAnonDecl(new_decl_index);
33333333
3334 if (pt.zcu.comp.debug_incremental) {
3334 if (pt.zcu.comp.incremental) {
33353335 try mod.intern_pool.addDependency(
33363336 gpa,
33373337 AnalUnit.wrap(.{ .decl = new_decl_index }),
......@@ -3421,7 +3421,7 @@ fn zirOpaqueDecl(
34213421 mod.declPtr(new_decl_index).owns_tv = true;
34223422 errdefer pt.abortAnonDecl(new_decl_index);
34233423
3424 if (pt.zcu.comp.debug_incremental) {
3424 if (pt.zcu.comp.incremental) {
34253425 try ip.addDependency(
34263426 gpa,
34273427 AnalUnit.wrap(.{ .decl = new_decl_index }),
......@@ -38098,7 +38098,7 @@ fn isKnownZigType(sema: *Sema, ref: Air.Inst.Ref, tag: std.builtin.TypeId) bool
3809838098
3809938099pub fn declareDependency(sema: *Sema, dependee: InternPool.Dependee) !void {
3810038100 const zcu = sema.pt.zcu;
38101 if (!zcu.comp.debug_incremental) return;
38101 if (!zcu.comp.incremental) return;
3810238102
3810338103 // Avoid creating dependencies on ourselves. This situation can arise when we analyze the fields
3810438104 // of a type and they use `@This()`. This dependency would be unnecessary, and in fact would
src/Zcu.zig+1-1
......@@ -2679,7 +2679,7 @@ fn markTransitiveDependersPotentiallyOutdated(zcu: *Zcu, maybe_outdated: AnalUni
26792679}
26802680
26812681pub fn findOutdatedToAnalyze(zcu: *Zcu) Allocator.Error!?AnalUnit {
2682 if (!zcu.comp.debug_incremental) return null;
2682 if (!zcu.comp.incremental) return null;
26832683
26842684 if (zcu.outdated.count() == 0 and zcu.potentially_outdated.count() == 0) {
26852685 log.debug("findOutdatedToAnalyze: no outdated depender", .{});
src/Zcu/PerThread.zig+1-1
......@@ -888,7 +888,7 @@ fn getFileRootStruct(
888888 };
889889 errdefer wip_ty.cancel(ip, pt.tid);
890890
891 if (zcu.comp.debug_incremental) {
891 if (zcu.comp.incremental) {
892892 try ip.addDependency(
893893 gpa,
894894 InternPool.AnalUnit.wrap(.{ .decl = decl_index }),
src/main.zig+14-9
......@@ -404,6 +404,8 @@ const usage_build_generic =
404404 \\ -h, --help Print this help and exit
405405 \\ --color [auto|off|on] Enable or disable colored error messages
406406 \\ -j<N> Limit concurrent jobs (default is to use all CPU cores)
407 \\ -fincremental Enable incremental compilation
408 \\ -fno-incremental Disable incremental compilation
407409 \\ -femit-bin[=path] (default) Output machine code
408410 \\ -fno-emit-bin Do not output machine code
409411 \\ -femit-asm[=path] Output .s (assembly code)
......@@ -642,7 +644,6 @@ const usage_build_generic =
642644 \\ --debug-log [scope] Enable printing debug/info log messages for scope
643645 \\ --debug-compile-errors Crash with helpful diagnostics at the first compile error
644646 \\ --debug-link-snapshot Enable dumping of the linker's state in JSON format
645 \\ --debug-incremental Enable experimental feature: incremental compilation
646647 \\
647648;
648649
......@@ -904,7 +905,7 @@ fn buildOutputType(
904905 var minor_subsystem_version: ?u16 = null;
905906 var mingw_unicode_entry_point: bool = false;
906907 var enable_link_snapshots: bool = false;
907 var debug_incremental: bool = false;
908 var opt_incremental: ?bool = null;
908909 var install_name: ?[]const u8 = null;
909910 var hash_style: link.File.Elf.HashStyle = .both;
910911 var entitlements: ?[]const u8 = null;
......@@ -1357,8 +1358,10 @@ fn buildOutputType(
13571358 } else {
13581359 enable_link_snapshots = true;
13591360 }
1360 } else if (mem.eql(u8, arg, "--debug-incremental")) {
1361 debug_incremental = true;
1361 } else if (mem.eql(u8, arg, "-fincremental")) {
1362 opt_incremental = true;
1363 } else if (mem.eql(u8, arg, "-fno-incremental")) {
1364 opt_incremental = false;
13621365 } else if (mem.eql(u8, arg, "--entitlements")) {
13631366 entitlements = args_iter.nextOrFatal();
13641367 } else if (mem.eql(u8, arg, "-fcompiler-rt")) {
......@@ -3225,6 +3228,8 @@ fn buildOutputType(
32253228 break :b .incremental;
32263229 };
32273230
3231 const incremental = opt_incremental orelse false;
3232
32283233 process.raiseFileDescriptorLimit();
32293234
32303235 var file_system_inputs: std.ArrayListUnmanaged(u8) = .{};
......@@ -3336,7 +3341,7 @@ fn buildOutputType(
33363341 .cache_mode = cache_mode,
33373342 .subsystem = subsystem,
33383343 .debug_compile_errors = debug_compile_errors,
3339 .debug_incremental = debug_incremental,
3344 .incremental = incremental,
33403345 .enable_link_snapshots = enable_link_snapshots,
33413346 .install_name = install_name,
33423347 .entitlements = entitlements,
......@@ -3443,7 +3448,7 @@ fn buildOutputType(
34433448 updateModule(comp, color, root_prog_node) catch |err| switch (err) {
34443449 error.SemanticAnalyzeFail => {
34453450 assert(listen == .none);
3446 saveState(comp, debug_incremental);
3451 saveState(comp, incremental);
34473452 process.exit(1);
34483453 },
34493454 else => |e| return e,
......@@ -3451,7 +3456,7 @@ fn buildOutputType(
34513456 }
34523457 if (build_options.only_c) return cleanExit();
34533458 try comp.makeBinFileExecutable();
3454 saveState(comp, debug_incremental);
3459 saveState(comp, incremental);
34553460
34563461 if (test_exec_args.items.len == 0 and target.ofmt == .c) default_exec_args: {
34573462 // Default to using `zig run` to execute the produced .c code from `zig test`.
......@@ -4032,8 +4037,8 @@ fn createModule(
40324037 return mod;
40334038}
40344039
4035fn saveState(comp: *Compilation, debug_incremental: bool) void {
4036 if (debug_incremental) {
4040fn saveState(comp: *Compilation, incremental: bool) void {
4041 if (incremental) {
40374042 comp.saveState() catch |err| {
40384043 warn("unable to save incremental compilation state: {s}", .{@errorName(err)});
40394044 };