From 2d4574aafbf95f357ad6306b98d9c8ebebbafe2e Mon Sep 17 00:00:00 2001 From: David Rubin Date: Thu, 26 Dec 2024 05:30:56 -0800 Subject: [PATCH] Compilation: always import ubsan if a ZCU exists Unlike `compiler-rt`, `ubsan` uses the standard library quite a lot. Using a similar approach to how `compiler-rt` is handled today, where it's compiled into its own object and then linked would be sub-optimal as we'd be introducing a lot of code bloat. This approach always "imports" `ubsan` if the ZCU, if it exists. If it doesn't such as the case where we're compiling only C code, then we have no choice other than to compile it down to an object and link. There's still a tiny optimization we can do in that case, which is when compiling to a static library, there's no need to construct an archive with a single object. We'd only go back and parse out ubsan from the archive later in the pipeline. So we compile it to an object instead and link that to the static library. TLDR; - `zig build-exe foo.c` -> build `libubsan.a` and links - `zig build-obj foo.c` -> doesn't build anything, just emits references to ubsan runtime - `zig build-lib foo.c -static` -> build `ubsan.o` and link it - `zig build-exe foo.zig bar.c` -> import `ubsan-rt` into the ZCU - `zig build-obj foo.zig bar.c` -> import `ubsan-rt` into the ZCU - `zig build-lib foo.zig bar.c` -> import `ubsan-rt` into the ZCU --- src/Compilation.zig | 7 +++++-- src/Zcu.zig | 2 +- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/src/Compilation.zig b/src/Compilation.zig index e4bccdc2d8..1fc43b85a9 100644 --- a/src/Compilation.zig +++ b/src/Compilation.zig @@ -1309,7 +1309,7 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil (!options.skip_linker_dependencies and is_exe_or_dyn_lib); const include_ubsan_rt = options.want_ubsan_rt orelse - (!options.skip_linker_dependencies and is_exe_or_dyn_lib); + (!options.skip_linker_dependencies and is_exe_or_dyn_lib and !have_zcu); if (include_compiler_rt and output_mode == .Obj) { // For objects, this mechanism relies on essentially `_ = @import("compiler-rt");` @@ -1337,7 +1337,10 @@ pub fn create(gpa: Allocator, arena: Allocator, options: CreateOptions) !*Compil try options.root_mod.deps.putNoClobber(arena, "compiler_rt", compiler_rt_mod); } - if (include_ubsan_rt and output_mode == .Obj) { + // unlike compiler_rt, we always want to go through the `_ = @import("ubsan-rt")` + // approach, since the ubsan runtime uses quite a lot of the standard library + // and this reduces unnecessary bloat. + if (!options.skip_linker_dependencies and have_zcu) { const ubsan_rt_mod = try Package.Module.create(arena, .{ .global_cache_directory = options.global_cache_directory, .paths = .{ diff --git a/src/Zcu.zig b/src/Zcu.zig index fd0b0dba64..f262177da1 100644 --- a/src/Zcu.zig +++ b/src/Zcu.zig @@ -175,7 +175,7 @@ nav_val_analysis_queued: std.AutoArrayHashMapUnmanaged(InternPool.Nav.Index, voi /// These are the modules which we initially queue for analysis in `Compilation.update`. /// `resolveReferences` will use these as the root of its reachability traversal. -analysis_roots: std.BoundedArray(*Package.Module, 3) = .{}, +analysis_roots: std.BoundedArray(*Package.Module, 4) = .{}, /// This is the cached result of `Zcu.resolveReferences`. It is computed on-demand, and /// reset to `null` when any semantic analysis occurs (since this invalidates the data). /// Allocated into `gpa`.