authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2023-12-29 16:31:36-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2024-01-01 19:49:08-07:00
loga89d6878d2780f966b7327f8d366e6ac5f5cd8fc
tree547470c99ded448aabc88fa2ef6b968ebddf92cb
parente22102dfc6356a16e83341d0cd0526762c696ad6

Compilation: fix cache hash of incremental builds

Without this commit, unrelated test builds using incremental cache mode (self-hosted, no lld) would end up using the same cache namespace, which is undesireable since concurrent builds will clobber each other's work. This happened because of passing the root module to addModuleToCacheHash. In the case of a test build, the root module actually does not connect to the rest of the import table. Instead, the main module needs to be passed, which has "root" in its import table. The other call to addModuleTableToCacheHash which is in addNonIncrementalStuffToCacheManifest already correctly passes the main module. In the future, I think this problem can be fully addressed by obtaining an advisory lock on the output binary file. However, even in that case, it is still valuable to make different compilations use different cache namespaces lest unrelated compilations suffer from pointless thrashing rather than being independently edited.

1 files changed, 3 insertions(+), 2 deletions(-)

src/Compilation.zig+3-2
......@@ -1374,6 +1374,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
13741374 cache.hash.add(options.config.wasi_exec_model);
13751375 // TODO audit this and make sure everything is in it
13761376
1377 const main_mod = options.main_mod orelse options.root_mod;
13771378 const comp = try arena.create(Compilation);
13781379 const opt_zcu: ?*Module = if (have_zcu) blk: {
13791380 // Pre-open the directory handles for cached ZIR code so that it does not need
......@@ -1420,7 +1421,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
14201421 zcu.* = .{
14211422 .gpa = gpa,
14221423 .comp = comp,
1423 .main_mod = options.main_mod orelse options.root_mod,
1424 .main_mod = main_mod,
14241425 .root_mod = options.root_mod,
14251426 .std_mod = std_mod,
14261427 .global_zir_cache = global_zir_cache,
......@@ -1608,7 +1609,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil
16081609 // do want to namespace different source file names because they are
16091610 // likely different compilations and therefore this would be likely to
16101611 // cause cache hits.
1611 try addModuleTableToCacheHash(gpa, arena, &hash, options.root_mod, .path_bytes);
1612 try addModuleTableToCacheHash(gpa, arena, &hash, main_mod, .path_bytes);
16121613
16131614 // In the case of incremental cache mode, this `artifact_directory`
16141615 // is computed based on a hash of non-linker inputs, and it is where all