authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 17:28:28-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2021-04-16 17:28:28-07:00
log5ff45b3f44e7f1272b655b366cee4089d0aa8509
tree7a2987020988e85f60eff90db71ca08a3bdf6864
parenta271f12a149239ab6e6361423589687e32f64cda

stage2: use import list from ZIR to queue up more AstGen tasks


4 files changed, 50 insertions(+), 7 deletions(-)

BRANCH_TODO-1
......@@ -1,6 +1,5 @@
11 * look for cached zir code
22 * save zir code to cache
3 * use list of imported strings to queue up more astgen tasks
43 * keep track of file dependencies/dependants
54 * unload files from memory when a dependency is dropped
65 * implement zir error notes
src/Compilation.zig+30-1
......@@ -30,6 +30,7 @@ const c_codegen = @import("codegen/c.zig");
3030const ThreadPool = @import("ThreadPool.zig");
3131const WaitGroup = @import("WaitGroup.zig");
3232const libtsan = @import("libtsan.zig");
33const Zir = @import("Zir.zig");
3334
3435/// General-purpose allocator. Used for both temporary and long-term storage.
3536gpa: *Allocator,
......@@ -431,7 +432,6 @@ pub const AllErrors = struct {
431432 ) !void {
432433 assert(file.zir_loaded);
433434 assert(file.tree_loaded);
434 const Zir = @import("Zir.zig");
435435 const payload_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.compile_errors)];
436436 assert(payload_index != 0);
437437
......@@ -2120,6 +2120,35 @@ fn workerAstGenFile(
21202120 };
21212121 },
21222122 };
2123
2124 // Pre-emptively look for `@import` paths and queue them up.
2125 // If we experience an error preemptively fetching the
2126 // file, just ignore it and let it happen again later during Sema.
2127 assert(file.zir_loaded);
2128 const imports_index = file.zir.extra[@enumToInt(Zir.ExtraIndex.imports)];
2129 if (imports_index != 0) {
2130 const imports_len = file.zir.extra[imports_index];
2131
2132 for (file.zir.extra[imports_index + 1 ..][0..imports_len]) |str_index| {
2133 const import_path = file.zir.nullTerminatedString(str_index);
2134
2135 const import_result = blk: {
2136 const lock = comp.mutex.acquire();
2137 defer lock.release();
2138
2139 break :blk mod.importFile(file.pkg, import_path) catch continue;
2140 };
2141 if (import_result.is_new) {
2142 wg.start();
2143 comp.thread_pool.spawn(workerAstGenFile, .{
2144 comp, import_result.file, prog_node, wg,
2145 }) catch {
2146 wg.finish();
2147 continue;
2148 };
2149 }
2150 }
2151 }
21232152}
21242153
21252154pub fn obtainCObjectCacheManifest(comp: *const Compilation) Cache.Manifest {
src/Module.zig+18-3
......@@ -2628,7 +2628,16 @@ pub fn declareDeclDependency(mod: *Module, depender: *Decl, dependee: *Decl) !vo
26282628 depender.dependencies.putAssumeCapacity(dependee, {});
26292629}
26302630
2631pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*Scope.File {
2631pub const ImportFileResult = struct {
2632 file: *Scope.File,
2633 is_new: bool,
2634};
2635
2636pub fn importFile(
2637 mod: *Module,
2638 cur_pkg: *Package,
2639 import_string: []const u8,
2640) !ImportFileResult {
26322641 const gpa = mod.gpa;
26332642
26342643 const cur_pkg_dir_path = cur_pkg.root_src_directory.path orelse ".";
......@@ -2642,7 +2651,10 @@ pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*
26422651 defer if (!keep_resolved_path) gpa.free(resolved_path);
26432652
26442653 const gop = try mod.import_table.getOrPut(gpa, resolved_path);
2645 if (gop.found_existing) return gop.entry.value;
2654 if (gop.found_existing) return ImportFileResult{
2655 .file = gop.entry.value,
2656 .is_new = false,
2657 };
26462658
26472659 if (found_pkg == null) {
26482660 const resolved_root_path = try std.fs.path.resolve(gpa, &[_][]const u8{cur_pkg_dir_path});
......@@ -2671,7 +2683,10 @@ pub fn importFile(mod: *Module, cur_pkg: *Package, import_string: []const u8) !*
26712683 .namespace = undefined,
26722684 };
26732685 keep_resolved_path = true;
2674 return new_file;
2686 return ImportFileResult{
2687 .file = new_file,
2688 .is_new = true,
2689 };
26752690}
26762691
26772692pub fn analyzeNamespace(
src/Sema.zig+2-2
......@@ -3904,7 +3904,7 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
39043904 const src = inst_data.src();
39053905 const operand = inst_data.get(sema.code);
39063906
3907 const file = mod.importFile(block.getFileScope().pkg, operand) catch |err| switch (err) {
3907 const result = mod.importFile(block.getFileScope().pkg, operand) catch |err| switch (err) {
39083908 error.ImportOutsidePkgPath => {
39093909 return mod.fail(&block.base, src, "import of file outside package path: '{s}'", .{operand});
39103910 },
......@@ -3914,7 +3914,7 @@ fn zirImport(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!
39143914 return mod.fail(&block.base, src, "unable to open '{s}': {s}", .{ operand, @errorName(err) });
39153915 },
39163916 };
3917 return mod.constType(sema.arena, src, file.namespace.ty);
3917 return mod.constType(sema.arena, src, result.file.namespace.ty);
39183918}
39193919
39203920fn zirShl(sema: *Sema, block: *Scope.Block, inst: Zir.Inst.Index) InnerError!*Inst {