| ... | ... | @@ -26,6 +26,8 @@ const Type = @import("../Type.zig"); |
| 26 | 26 | const Value = @import("../Value.zig"); |
| 27 | 27 | const Zcu = @import("../Zcu.zig"); |
| 28 | 28 | const Zir = std.zig.Zir; |
| 29 | const Zoir = std.zig.Zoir; |
| 30 | const ZonGen = std.zig.ZonGen; |
| 29 | 31 | |
| 30 | 32 | zcu: *Zcu, |
| 31 | 33 | |
| ... | ... | @@ -73,6 +75,8 @@ pub fn destroyFile(pt: Zcu.PerThread, file_index: Zcu.File.Index) void { |
| 73 | 75 | if (!is_builtin) gpa.destroy(file); |
| 74 | 76 | } |
| 75 | 77 | |
| 78 | /// Ensures that `file` has up-to-date ZIR. If not, loads the ZIR cache or runs |
| 79 | /// AstGen as needed. Also updates `file.status`. |
| 76 | 80 | pub fn updateFile( |
| 77 | 81 | pt: Zcu.PerThread, |
| 78 | 82 | file: *Zcu.File, |
| ... | ... | @@ -126,6 +130,24 @@ pub fn updateFile( |
| 126 | 130 | }, |
| 127 | 131 | }; |
| 128 | 132 | |
| 133 | // The old compile error, if any, is no longer relevant. |
| 134 | pt.lockAndClearFileCompileError(file); |
| 135 | |
| 136 | // If `zir` is not null, and `prev_zir` is null, then `TrackedInst`s are associated with `zir`. |
| 137 | // We need to keep it around! |
| 138 | // As an optimization, also check `loweringFailed`; if true, but `prev_zir == null`, then this |
| 139 | // file has never passed AstGen, so we actually need not cache the old ZIR. |
| 140 | if (file.zir != null and file.prev_zir == null and !file.zir.?.loweringFailed()) { |
| 141 | assert(file.prev_zir == null); |
| 142 | const prev_zir_ptr = try gpa.create(Zir); |
| 143 | file.prev_zir = prev_zir_ptr; |
| 144 | prev_zir_ptr.* = file.zir.?; |
| 145 | file.zir = null; |
| 146 | } |
| 147 | |
| 148 | // We're going to re-load everything, so unload source, AST, ZIR, ZOIR. |
| 149 | file.unload(gpa); |
| 150 | |
| 129 | 151 | // We ask for a lock in order to coordinate with other zig processes. |
| 130 | 152 | // If another process is already working on this file, we will get the cached |
| 131 | 153 | // version. Likewise if we're working on AstGen and another process asks for |
| ... | ... | @@ -180,175 +202,164 @@ pub fn updateFile( |
| 180 | 202 | }; |
| 181 | 203 | defer cache_file.close(); |
| 182 | 204 | |
| 183 | | while (true) { |
| 184 | | update: { |
| 185 | | // First we read the header to determine the lengths of arrays. |
| 186 | | const header = cache_file.reader().readStruct(Zir.Header) catch |err| switch (err) { |
| 187 | | // This can happen if Zig bails out of this function between creating |
| 188 | | // the cached file and writing it. |
| 189 | | error.EndOfStream => break :update, |
| 190 | | else => |e| return e, |
| 191 | | }; |
| 192 | | const unchanged_metadata = |
| 193 | | stat.size == header.stat_size and |
| 194 | | stat.mtime == header.stat_mtime and |
| 195 | | stat.inode == header.stat_inode; |
| 196 | | |
| 197 | | if (!unchanged_metadata) { |
| 198 | | log.debug("AstGen cache stale: {s}", .{file.sub_file_path}); |
| 199 | | break :update; |
| 200 | | } |
| 201 | | log.debug("AstGen cache hit: {s} instructions_len={d}", .{ |
| 202 | | file.sub_file_path, header.instructions_len, |
| 203 | | }); |
| 204 | | |
| 205 | | file.zir = Zcu.loadZirCacheBody(gpa, header, cache_file) catch |err| switch (err) { |
| 206 | | error.UnexpectedFileSize => { |
| 207 | | log.warn("unexpected EOF reading cached ZIR for {s}", .{file.sub_file_path}); |
| 208 | | break :update; |
| 209 | | }, |
| 210 | | else => |e| return e, |
| 211 | | }; |
| 212 | | file.stat = .{ |
| 213 | | .size = header.stat_size, |
| 214 | | .inode = header.stat_inode, |
| 215 | | .mtime = header.stat_mtime, |
| 216 | | }; |
| 217 | | file.status = .success; |
| 218 | | log.debug("AstGen cached success: {s}", .{file.sub_file_path}); |
| 219 | | |
| 220 | | if (file.zir.?.hasCompileErrors()) { |
| 221 | | comp.mutex.lock(); |
| 222 | | defer comp.mutex.unlock(); |
| 223 | | try zcu.failed_files.putNoClobber(gpa, file, null); |
| 224 | | } |
| 225 | | if (file.zir.?.loweringFailed()) { |
| 226 | | file.status = .astgen_failure; |
| 227 | | return error.AnalysisFail; |
| 228 | | } |
| 229 | | return; |
| 205 | const need_update = while (true) { |
| 206 | const result = switch (file.getMode()) { |
| 207 | inline else => |mode| try loadZirZoirCache(zcu, cache_file, stat, file, mode), |
| 208 | }; |
| 209 | switch (result) { |
| 210 | .success => { |
| 211 | log.debug("AstGen cached success: {s}", .{file.sub_file_path}); |
| 212 | break false; |
| 213 | }, |
| 214 | .invalid => {}, |
| 215 | .truncated => log.warn("unexpected EOF reading cached ZIR for {s}", .{file.sub_file_path}), |
| 216 | .stale => log.debug("AstGen cache stale: {s}", .{file.sub_file_path}), |
| 230 | 217 | } |
| 231 | 218 | |
| 232 | 219 | // If we already have the exclusive lock then it is our job to update. |
| 233 | | if (builtin.os.tag == .wasi or lock == .exclusive) break; |
| 220 | if (builtin.os.tag == .wasi or lock == .exclusive) break true; |
| 234 | 221 | // Otherwise, unlock to give someone a chance to get the exclusive lock |
| 235 | 222 | // and then upgrade to an exclusive lock. |
| 236 | 223 | cache_file.unlock(); |
| 237 | 224 | lock = .exclusive; |
| 238 | 225 | try cache_file.lock(lock); |
| 239 | | } |
| 226 | }; |
| 240 | 227 | |
| 241 | | // The cache is definitely stale so delete the contents to avoid an underwrite later. |
| 242 | | cache_file.setEndPos(0) catch |err| switch (err) { |
| 243 | | error.FileTooBig => unreachable, // 0 is not too big |
| 228 | if (need_update) { |
| 229 | // The cache is definitely stale so delete the contents to avoid an underwrite later. |
| 230 | cache_file.setEndPos(0) catch |err| switch (err) { |
| 231 | error.FileTooBig => unreachable, // 0 is not too big |
| 232 | else => |e| return e, |
| 233 | }; |
| 244 | 234 | |
| 245 | | else => |e| return e, |
| 246 | | }; |
| 235 | if (stat.size > std.math.maxInt(u32)) |
| 236 | return error.FileTooBig; |
| 247 | 237 | |
| 248 | | pt.lockAndClearFileCompileError(file); |
| 238 | const source = try gpa.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0); |
| 239 | defer if (file.source == null) gpa.free(source); |
| 240 | const amt = try source_file.readAll(source); |
| 241 | if (amt != stat.size) |
| 242 | return error.UnexpectedEndOfFile; |
| 249 | 243 | |
| 250 | | // If `zir` is not null, and `prev_zir` is null, then `TrackedInst`s are associated with `zir`. |
| 251 | | // We need to keep it around! |
| 252 | | // As an optimization, also check `loweringFailed`; if true, but `prev_zir == null`, then this |
| 253 | | // file has never passed AstGen, so we actually need not cache the old ZIR. |
| 254 | | if (file.zir != null and file.prev_zir == null and !file.zir.?.loweringFailed()) { |
| 255 | | assert(file.prev_zir == null); |
| 256 | | const prev_zir_ptr = try gpa.create(Zir); |
| 257 | | file.prev_zir = prev_zir_ptr; |
| 258 | | prev_zir_ptr.* = file.zir.?; |
| 259 | | file.zir = null; |
| 260 | | } |
| 261 | | file.unload(gpa); |
| 244 | file.source = source; |
| 245 | |
| 246 | // Any potential AST errors are converted to ZIR errors when we run AstGen/ZonGen. |
| 247 | file.tree = try Ast.parse(gpa, source, file.getMode()); |
| 262 | 248 | |
| 263 | | if (stat.size > std.math.maxInt(u32)) |
| 264 | | return error.FileTooBig; |
| 249 | switch (file.getMode()) { |
| 250 | .zig => { |
| 251 | file.zir = try AstGen.generate(gpa, file.tree.?); |
| 252 | Zcu.saveZirCache(gpa, cache_file, stat, file.zir.?) catch |err| switch (err) { |
| 253 | error.OutOfMemory => |e| return e, |
| 254 | else => log.warn("unable to write cached ZIR code for {}{s} to {}{s}: {s}", .{ |
| 255 | file.mod.root, file.sub_file_path, cache_directory, &hex_digest, @errorName(err), |
| 256 | }), |
| 257 | }; |
| 258 | }, |
| 259 | .zon => { |
| 260 | file.zoir = try ZonGen.generate(gpa, file.tree.?, .{}); |
| 261 | Zcu.saveZoirCache(cache_file, stat, file.zoir.?) catch |err| { |
| 262 | log.warn("unable to write cached ZOIR code for {}{s} to {}{s}: {s}", .{ |
| 263 | file.mod.root, file.sub_file_path, cache_directory, &hex_digest, @errorName(err), |
| 264 | }); |
| 265 | }; |
| 266 | }, |
| 267 | } |
| 265 | 268 | |
| 266 | | const source = try gpa.allocSentinel(u8, @as(usize, @intCast(stat.size)), 0); |
| 267 | | defer if (file.source == null) gpa.free(source); |
| 268 | | const amt = try source_file.readAll(source); |
| 269 | | if (amt != stat.size) |
| 270 | | return error.UnexpectedEndOfFile; |
| 269 | log.debug("AstGen fresh success: {s}", .{file.sub_file_path}); |
| 270 | } |
| 271 | 271 | |
| 272 | 272 | file.stat = .{ |
| 273 | 273 | .size = stat.size, |
| 274 | 274 | .inode = stat.inode, |
| 275 | 275 | .mtime = stat.mtime, |
| 276 | 276 | }; |
| 277 | | file.source = source; |
| 278 | 277 | |
| 279 | | file.tree = try Ast.parse(gpa, source, .zig); |
| 278 | // Now, `zir` or `zoir` is definitely populated and up-to-date. |
| 279 | // Mark file successes/failures as needed. |
| 280 | 280 | |
| 281 | | // Any potential AST errors are converted to ZIR errors here. |
| 282 | | file.zir = try AstGen.generate(gpa, file.tree.?); |
| 283 | | file.status = .success; |
| 284 | | log.debug("AstGen fresh success: {s}", .{file.sub_file_path}); |
| 281 | switch (file.getMode()) { |
| 282 | .zig => { |
| 283 | if (file.zir.?.hasCompileErrors()) { |
| 284 | comp.mutex.lock(); |
| 285 | defer comp.mutex.unlock(); |
| 286 | try zcu.failed_files.putNoClobber(gpa, file, null); |
| 287 | } |
| 288 | if (file.zir.?.loweringFailed()) { |
| 289 | file.status = .astgen_failure; |
| 290 | } else { |
| 291 | file.status = .success; |
| 292 | } |
| 293 | }, |
| 294 | .zon => { |
| 295 | if (file.zoir.?.hasCompileErrors()) { |
| 296 | file.status = .astgen_failure; |
| 297 | comp.mutex.lock(); |
| 298 | defer comp.mutex.unlock(); |
| 299 | try zcu.failed_files.putNoClobber(gpa, file, null); |
| 300 | } else { |
| 301 | file.status = .success; |
| 302 | } |
| 303 | }, |
| 304 | } |
| 285 | 305 | |
| 286 | | const safety_buffer = if (Zcu.data_has_safety_tag) |
| 287 | | try gpa.alloc([8]u8, file.zir.?.instructions.len) |
| 288 | | else |
| 289 | | undefined; |
| 290 | | defer if (Zcu.data_has_safety_tag) gpa.free(safety_buffer); |
| 291 | | const data_ptr = if (Zcu.data_has_safety_tag) |
| 292 | | if (file.zir.?.instructions.len == 0) |
| 293 | | @as([*]const u8, undefined) |
| 294 | | else |
| 295 | | @as([*]const u8, @ptrCast(safety_buffer.ptr)) |
| 296 | | else |
| 297 | | @as([*]const u8, @ptrCast(file.zir.?.instructions.items(.data).ptr)); |
| 298 | | if (Zcu.data_has_safety_tag) { |
| 299 | | // The `Data` union has a safety tag but in the file format we store it without. |
| 300 | | for (file.zir.?.instructions.items(.data), 0..) |*data, i| { |
| 301 | | const as_struct: *const Zcu.HackDataLayout = @ptrCast(data); |
| 302 | | safety_buffer[i] = as_struct.data; |
| 303 | | } |
| 306 | switch (file.status) { |
| 307 | .never_loaded => unreachable, |
| 308 | .retryable_failure => unreachable, |
| 309 | .astgen_failure => return error.AnalysisFail, |
| 310 | .success => return, |
| 304 | 311 | } |
| 312 | } |
| 305 | 313 | |
| 306 | | const header: Zir.Header = .{ |
| 307 | | .instructions_len = @as(u32, @intCast(file.zir.?.instructions.len)), |
| 308 | | .string_bytes_len = @as(u32, @intCast(file.zir.?.string_bytes.len)), |
| 309 | | .extra_len = @as(u32, @intCast(file.zir.?.extra.len)), |
| 314 | fn loadZirZoirCache( |
| 315 | zcu: *Zcu, |
| 316 | cache_file: std.fs.File, |
| 317 | stat: std.fs.File.Stat, |
| 318 | file: *Zcu.File, |
| 319 | comptime mode: Ast.Mode, |
| 320 | ) !enum { success, invalid, truncated, stale } { |
| 321 | assert(file.getMode() == mode); |
| 310 | 322 | |
| 311 | | .stat_size = stat.size, |
| 312 | | .stat_inode = stat.inode, |
| 313 | | .stat_mtime = stat.mtime, |
| 314 | | }; |
| 315 | | var iovecs = [_]std.posix.iovec_const{ |
| 316 | | .{ |
| 317 | | .base = @as([*]const u8, @ptrCast(&header)), |
| 318 | | .len = @sizeOf(Zir.Header), |
| 319 | | }, |
| 320 | | .{ |
| 321 | | .base = @as([*]const u8, @ptrCast(file.zir.?.instructions.items(.tag).ptr)), |
| 322 | | .len = file.zir.?.instructions.len, |
| 323 | | }, |
| 324 | | .{ |
| 325 | | .base = data_ptr, |
| 326 | | .len = file.zir.?.instructions.len * 8, |
| 327 | | }, |
| 328 | | .{ |
| 329 | | .base = file.zir.?.string_bytes.ptr, |
| 330 | | .len = file.zir.?.string_bytes.len, |
| 331 | | }, |
| 332 | | .{ |
| 333 | | .base = @as([*]const u8, @ptrCast(file.zir.?.extra.ptr)), |
| 334 | | .len = file.zir.?.extra.len * 4, |
| 335 | | }, |
| 323 | const gpa = zcu.gpa; |
| 324 | |
| 325 | const Header = switch (mode) { |
| 326 | .zig => Zir.Header, |
| 327 | .zon => Zoir.Header, |
| 336 | 328 | }; |
| 337 | | cache_file.writevAll(&iovecs) catch |err| { |
| 338 | | log.warn("unable to write cached ZIR code for {}{s} to {}{s}: {s}", .{ |
| 339 | | file.mod.root, file.sub_file_path, cache_directory, &hex_digest, @errorName(err), |
| 340 | | }); |
| 329 | |
| 330 | // First we read the header to determine the lengths of arrays. |
| 331 | const header = cache_file.reader().readStruct(Header) catch |err| switch (err) { |
| 332 | // This can happen if Zig bails out of this function between creating |
| 333 | // the cached file and writing it. |
| 334 | error.EndOfStream => return .invalid, |
| 335 | else => |e| return e, |
| 341 | 336 | }; |
| 342 | 337 | |
| 343 | | if (file.zir.?.hasCompileErrors()) { |
| 344 | | comp.mutex.lock(); |
| 345 | | defer comp.mutex.unlock(); |
| 346 | | try zcu.failed_files.putNoClobber(gpa, file, null); |
| 338 | const unchanged_metadata = |
| 339 | stat.size == header.stat_size and |
| 340 | stat.mtime == header.stat_mtime and |
| 341 | stat.inode == header.stat_inode; |
| 342 | |
| 343 | if (!unchanged_metadata) { |
| 344 | return .stale; |
| 347 | 345 | } |
| 348 | | if (file.zir.?.loweringFailed()) { |
| 349 | | file.status = .astgen_failure; |
| 350 | | return error.AnalysisFail; |
| 346 | |
| 347 | switch (mode) { |
| 348 | .zig => { |
| 349 | file.zir = Zcu.loadZirCacheBody(gpa, header, cache_file) catch |err| switch (err) { |
| 350 | error.UnexpectedFileSize => return .truncated, |
| 351 | else => |e| return e, |
| 352 | }; |
| 353 | }, |
| 354 | .zon => { |
| 355 | file.zoir = Zcu.loadZoirCacheBody(gpa, header, cache_file) catch |err| switch (err) { |
| 356 | error.UnexpectedFileSize => return .truncated, |
| 357 | else => |e| return e, |
| 358 | }; |
| 359 | }, |
| 351 | 360 | } |
| 361 | |
| 362 | return .success; |
| 352 | 363 | } |
| 353 | 364 | |
| 354 | 365 | const UpdatedFile = struct { |
| ... | ... | @@ -1819,7 +1830,6 @@ fn semaFile(pt: Zcu.PerThread, file_index: Zcu.File.Index) Zcu.SemaError!void { |
| 1819 | 1830 | defer tracy.end(); |
| 1820 | 1831 | |
| 1821 | 1832 | const zcu = pt.zcu; |
| 1822 | | const gpa = zcu.gpa; |
| 1823 | 1833 | const file = zcu.fileByIndex(file_index); |
| 1824 | 1834 | assert(file.getMode() == .zig); |
| 1825 | 1835 | assert(zcu.fileRootType(file_index) == .none); |
| ... | ... | @@ -1834,36 +1844,6 @@ fn semaFile(pt: Zcu.PerThread, file_index: Zcu.File.Index) Zcu.SemaError!void { |
| 1834 | 1844 | }); |
| 1835 | 1845 | const struct_ty = try pt.createFileRootStruct(file_index, new_namespace_index, false); |
| 1836 | 1846 | errdefer zcu.intern_pool.remove(pt.tid, struct_ty); |
| 1837 | | |
| 1838 | | switch (zcu.comp.cache_use) { |
| 1839 | | .whole => |whole| if (whole.cache_manifest) |man| { |
| 1840 | | const source = file.getSource(gpa) catch |err| { |
| 1841 | | try pt.reportRetryableFileError(file_index, "unable to load source: {s}", .{@errorName(err)}); |
| 1842 | | return error.AnalysisFail; |
| 1843 | | }; |
| 1844 | | |
| 1845 | | const resolved_path = std.fs.path.resolve(gpa, &.{ |
| 1846 | | file.mod.root.root_dir.path orelse ".", |
| 1847 | | file.mod.root.sub_path, |
| 1848 | | file.sub_file_path, |
| 1849 | | }) catch |err| { |
| 1850 | | try pt.reportRetryableFileError(file_index, "unable to resolve path: {s}", .{@errorName(err)}); |
| 1851 | | return error.AnalysisFail; |
| 1852 | | }; |
| 1853 | | errdefer gpa.free(resolved_path); |
| 1854 | | |
| 1855 | | whole.cache_manifest_mutex.lock(); |
| 1856 | | defer whole.cache_manifest_mutex.unlock(); |
| 1857 | | man.addFilePostContents(resolved_path, source.bytes, source.stat) catch |err| switch (err) { |
| 1858 | | error.OutOfMemory => |e| return e, |
| 1859 | | else => { |
| 1860 | | try pt.reportRetryableFileError(file_index, "unable to update cache: {s}", .{@errorName(err)}); |
| 1861 | | return error.AnalysisFail; |
| 1862 | | }, |
| 1863 | | }; |
| 1864 | | }, |
| 1865 | | .incremental => {}, |
| 1866 | | } |
| 1867 | 1847 | } |
| 1868 | 1848 | |
| 1869 | 1849 | pub fn importPkg(pt: Zcu.PerThread, mod: *Module) Allocator.Error!Zcu.ImportFileResult { |
| ... | ... | @@ -2800,8 +2780,16 @@ pub fn getErrorValueFromSlice(pt: Zcu.PerThread, name: []const u8) Allocator.Err |
| 2800 | 2780 | /// Removes any entry from `Zcu.failed_files` associated with `file`. Acquires `Compilation.mutex` as needed. |
| 2801 | 2781 | /// `file.zir` must be unchanged from the last update, as it is used to determine if there is such an entry. |
| 2802 | 2782 | fn lockAndClearFileCompileError(pt: Zcu.PerThread, file: *Zcu.File) void { |
| 2803 | | const zir = file.zir orelse return; |
| 2804 | | if (!zir.hasCompileErrors()) return; |
| 2783 | switch (file.getMode()) { |
| 2784 | .zig => { |
| 2785 | const zir = file.zir orelse return; |
| 2786 | if (!zir.hasCompileErrors()) return; |
| 2787 | }, |
| 2788 | .zon => { |
| 2789 | const zoir = file.zoir orelse return; |
| 2790 | if (!zoir.hasCompileErrors()) return; |
| 2791 | }, |
| 2792 | } |
| 2805 | 2793 | |
| 2806 | 2794 | pt.zcu.comp.mutex.lock(); |
| 2807 | 2795 | defer pt.zcu.comp.mutex.unlock(); |