authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-21 18:59:51-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-06-23 10:44:46-07:00
logcf65ab86011c3098bff131d6b98e13a494fc4814
treeeb3f84060d0461356f14a88912054512898993c0
parentfc185a6f71b9bd611a6d808082999a9da0f107e8

run AstGen even when using the stage1 backend

This change reduces the amount of divergence in the compiler's main pipeline logic enough to run AstGen for all files in the compilation, regardless of whether the stage1 or stage2 backend is being used. Practically, this means that all Zig code is subject to new compile errors, such as unused local variables. Additionally: * remove leftover unsound asserts from recent hash map changes * fix sub-Compilation errors not indenting correctly

1 files changed, 56 insertions(+), 51 deletions(-)

src/Compilation.zig+56-51
...@@ -342,6 +342,7 @@ pub const AllErrors = struct {...@@ -342,6 +342,7 @@ pub const AllErrors = struct {
342 const stderr = stderr_file.writer();342 const stderr = stderr_file.writer();
343 switch (msg) {343 switch (msg) {
344 .src => |src| {344 .src => |src| {
345 try stderr.writeByteNTimes(' ', indent);
345 ttyconf.setColor(stderr, .Bold);346 ttyconf.setColor(stderr, .Bold);
346 try stderr.print("{s}:{d}:{d}: ", .{347 try stderr.print("{s}:{d}:{d}: ", .{
347 src.src_path,348 src.src_path,
...@@ -349,7 +350,6 @@ pub const AllErrors = struct {...@@ -349,7 +350,6 @@ pub const AllErrors = struct {
349 src.column + 1,350 src.column + 1,
350 });351 });
351 ttyconf.setColor(stderr, color);352 ttyconf.setColor(stderr, color);
352 try stderr.writeByteNTimes(' ', indent);
353 try stderr.writeAll(kind);353 try stderr.writeAll(kind);
354 ttyconf.setColor(stderr, .Reset);354 ttyconf.setColor(stderr, .Reset);
355 ttyconf.setColor(stderr, .Bold);355 ttyconf.setColor(stderr, .Bold);
...@@ -731,6 +731,7 @@ fn addPackageTableToCacheHash(...@@ -731,6 +731,7 @@ fn addPackageTableToCacheHash(
731 hash: *Cache.HashHelper,731 hash: *Cache.HashHelper,
732 arena: *std.heap.ArenaAllocator,732 arena: *std.heap.ArenaAllocator,
733 pkg_table: Package.Table,733 pkg_table: Package.Table,
734 seen_table: *std.AutoHashMap(*Package, void),
734 hash_type: union(enum) { path_bytes, files: *Cache.Manifest },735 hash_type: union(enum) { path_bytes, files: *Cache.Manifest },
735) (error{OutOfMemory} || std.os.GetCwdError)!void {736) (error{OutOfMemory} || std.os.GetCwdError)!void {
736 const allocator = &arena.allocator;737 const allocator = &arena.allocator;
...@@ -755,6 +756,8 @@ fn addPackageTableToCacheHash(...@@ -755,6 +756,8 @@ fn addPackageTableToCacheHash(
755 }.lessThan);756 }.lessThan);
756757
757 for (packages) |pkg| {758 for (packages) |pkg| {
759 if ((try seen_table.getOrPut(pkg.value)).found_existing) continue;
760
758 // Finally insert the package name and path to the cache hash.761 // Finally insert the package name and path to the cache hash.
759 hash.addBytes(pkg.key);762 hash.addBytes(pkg.key);
760 switch (hash_type) {763 switch (hash_type) {
...@@ -770,7 +773,7 @@ fn addPackageTableToCacheHash(...@@ -770,7 +773,7 @@ fn addPackageTableToCacheHash(
770 },773 },
771 }774 }
772 // Recurse to handle the package's dependencies775 // Recurse to handle the package's dependencies
773 try addPackageTableToCacheHash(hash, arena, pkg.value.table, hash_type);776 try addPackageTableToCacheHash(hash, arena, pkg.value.table, seen_table, hash_type);
774 }777 }
775}778}
776779
...@@ -1116,7 +1119,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1116,7 +1119,8 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1116 {1119 {
1117 var local_arena = std.heap.ArenaAllocator.init(gpa);1120 var local_arena = std.heap.ArenaAllocator.init(gpa);
1118 defer local_arena.deinit();1121 defer local_arena.deinit();
1119 try addPackageTableToCacheHash(&hash, &local_arena, root_pkg.table, .path_bytes);1122 var seen_table = std.AutoHashMap(*Package, void).init(&local_arena.allocator);
1123 try addPackageTableToCacheHash(&hash, &local_arena, root_pkg.table, &seen_table, .path_bytes);
1120 }1124 }
1121 hash.add(valgrind);1125 hash.add(valgrind);
1122 hash.add(single_threaded);1126 hash.add(single_threaded);
...@@ -1137,36 +1141,32 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {...@@ -1137,36 +1141,32 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
1137 artifact_sub_dir,1141 artifact_sub_dir,
1138 };1142 };
11391143
1140 // If we rely on stage1, we must not redundantly add these packages.1144 const builtin_pkg = try Package.createWithDir(
1141 const use_stage1 = build_options.is_stage1 and use_llvm;1145 gpa,
1142 if (!use_stage1) {1146 zig_cache_artifact_directory,
1143 const builtin_pkg = try Package.createWithDir(1147 null,
1144 gpa,1148 "builtin.zig",
1145 zig_cache_artifact_directory,1149 );
1146 null,1150 errdefer builtin_pkg.destroy(gpa);
1147 "builtin.zig",
1148 );
1149 errdefer builtin_pkg.destroy(gpa);
11501151
1151 const std_pkg = try Package.createWithDir(1152 const std_pkg = try Package.createWithDir(
1152 gpa,1153 gpa,
1153 options.zig_lib_directory,1154 options.zig_lib_directory,
1154 "std",1155 "std",
1155 "std.zig",1156 "std.zig",
1156 );1157 );
1157 errdefer std_pkg.destroy(gpa);1158 errdefer std_pkg.destroy(gpa);
11581159
1159 try root_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);1160 try root_pkg.addAndAdopt(gpa, "builtin", builtin_pkg);
1160 try root_pkg.add(gpa, "root", root_pkg);1161 try root_pkg.add(gpa, "root", root_pkg);
1161 try root_pkg.addAndAdopt(gpa, "std", std_pkg);1162 try root_pkg.addAndAdopt(gpa, "std", std_pkg);
11621163
1163 try std_pkg.add(gpa, "builtin", builtin_pkg);1164 try std_pkg.add(gpa, "builtin", builtin_pkg);
1164 try std_pkg.add(gpa, "root", root_pkg);1165 try std_pkg.add(gpa, "root", root_pkg);
1165 try std_pkg.add(gpa, "std", std_pkg);1166 try std_pkg.add(gpa, "std", std_pkg);
11661167
1167 try builtin_pkg.add(gpa, "std", std_pkg);1168 try builtin_pkg.add(gpa, "std", std_pkg);
1168 try builtin_pkg.add(gpa, "builtin", builtin_pkg);1169 try builtin_pkg.add(gpa, "builtin", builtin_pkg);
1169 }
11701170
1171 // Pre-open the directory handles for cached ZIR code so that it does not need1171 // Pre-open the directory handles for cached ZIR code so that it does not need
1172 // to redundantly happen for each AstGen operation.1172 // to redundantly happen for each AstGen operation.
...@@ -1625,30 +1625,31 @@ pub fn update(self: *Compilation) !void {...@@ -1625,30 +1625,31 @@ pub fn update(self: *Compilation) !void {
1625 // Add a Job for each C object.1625 // Add a Job for each C object.
1626 try self.c_object_work_queue.ensureUnusedCapacity(self.c_object_table.count());1626 try self.c_object_work_queue.ensureUnusedCapacity(self.c_object_table.count());
1627 for (self.c_object_table.keys()) |key| {1627 for (self.c_object_table.keys()) |key| {
1628 assert(@ptrToInt(key) != 0xaaaa_aaaa_aaaa_aaaa);
1629 self.c_object_work_queue.writeItemAssumeCapacity(key);1628 self.c_object_work_queue.writeItemAssumeCapacity(key);
1630 }1629 }
16311630
1632 const use_stage1 = build_options.omit_stage2 or1631 const use_stage1 = build_options.omit_stage2 or
1633 (build_options.is_stage1 and self.bin_file.options.use_llvm);1632 (build_options.is_stage1 and self.bin_file.options.use_llvm);
1634 if (!use_stage1) {1633 if (self.bin_file.options.module) |module| {
1635 if (self.bin_file.options.module) |module| {1634 module.compile_log_text.shrinkAndFree(module.gpa, 0);
1636 module.compile_log_text.shrinkAndFree(module.gpa, 0);1635 module.generation += 1;
1637 module.generation += 1;1636
16381637 // Make sure std.zig is inside the import_table. We unconditionally need
1639 // Make sure std.zig is inside the import_table. We unconditionally need1638 // it for start.zig.
1640 // it for start.zig.1639 const std_pkg = module.root_pkg.table.get("std").?;
1641 const std_pkg = module.root_pkg.table.get("std").?;1640 _ = try module.importPkg(std_pkg);
1642 _ = try module.importPkg(std_pkg);1641
16431642 // Put a work item in for every known source file to detect if
1644 // Put a work item in for every known source file to detect if1643 // it changed, and, if so, re-compute ZIR and then queue the job
1645 // it changed, and, if so, re-compute ZIR and then queue the job1644 // to update it.
1646 // to update it.1645 // We still want AstGen work items for stage1 so that we expose compile errors
1647 try self.astgen_work_queue.ensureUnusedCapacity(module.import_table.count());1646 // that are implemented in stage2 but not stage1.
1648 for (module.import_table.values()) |value| {1647 try self.astgen_work_queue.ensureUnusedCapacity(module.import_table.count());
1649 self.astgen_work_queue.writeItemAssumeCapacity(value);1648 for (module.import_table.values()) |value| {
1650 }1649 self.astgen_work_queue.writeItemAssumeCapacity(value);
1650 }
16511651
1652 if (!use_stage1) {
1652 try self.work_queue.writeItem(.{ .analyze_pkg = std_pkg });1653 try self.work_queue.writeItem(.{ .analyze_pkg = std_pkg });
1653 }1654 }
1654 }1655 }
...@@ -1936,7 +1937,6 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor...@@ -1936,7 +1937,6 @@ pub fn performAllTheWork(self: *Compilation) error{ TimerUnsupported, OutOfMemor
1936 }1937 }
19371938
1938 while (self.c_object_work_queue.readItem()) |c_object| {1939 while (self.c_object_work_queue.readItem()) |c_object| {
1939 assert(@ptrToInt(c_object) != 0xaaaa_aaaa_aaaa_aaaa);
1940 self.work_queue_wait_group.start();1940 self.work_queue_wait_group.start();
1941 try self.thread_pool.spawn(workerUpdateCObject, .{1941 try self.thread_pool.spawn(workerUpdateCObject, .{
1942 self, c_object, &c_obj_prog_node, &self.work_queue_wait_group,1942 self, c_object, &c_obj_prog_node, &self.work_queue_wait_group,
...@@ -3830,9 +3830,8 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node...@@ -3830,9 +3830,8 @@ fn updateStage1Module(comp: *Compilation, main_progress_node: *std.Progress.Node
38303830
3831 _ = try man.addFile(main_zig_file, null);3831 _ = try man.addFile(main_zig_file, null);
3832 {3832 {
3833 var local_arena = std.heap.ArenaAllocator.init(comp.gpa);3833 var seen_table = std.AutoHashMap(*Package, void).init(&arena_allocator.allocator);
3834 defer local_arena.deinit();3834 try addPackageTableToCacheHash(&man.hash, &arena_allocator, mod.root_pkg.table, &seen_table, .{ .files = &man });
3835 try addPackageTableToCacheHash(&man.hash, &local_arena, mod.root_pkg.table, .{ .files = &man });
3836 }3835 }
3837 man.hash.add(comp.bin_file.options.valgrind);3836 man.hash.add(comp.bin_file.options.valgrind);
3838 man.hash.add(comp.bin_file.options.single_threaded);3837 man.hash.add(comp.bin_file.options.single_threaded);
...@@ -4103,6 +4102,12 @@ fn createStage1Pkg(...@@ -4103,6 +4102,12 @@ fn createStage1Pkg(
4103 var children = std.ArrayList(*stage1.Pkg).init(arena);4102 var children = std.ArrayList(*stage1.Pkg).init(arena);
4104 var it = pkg.table.iterator();4103 var it = pkg.table.iterator();
4105 while (it.next()) |entry| {4104 while (it.next()) |entry| {
4105 if (mem.eql(u8, entry.key_ptr.*, "std") or
4106 mem.eql(u8, entry.key_ptr.*, "builtin") or
4107 mem.eql(u8, entry.key_ptr.*, "root"))
4108 {
4109 continue;
4110 }
4106 try children.append(try createStage1Pkg(arena, entry.key_ptr.*, entry.value_ptr.*, child_pkg));4111 try children.append(try createStage1Pkg(arena, entry.key_ptr.*, entry.value_ptr.*, child_pkg));
4107 }4112 }
4108 break :blk children.items;4113 break :blk children.items;