authorgravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-24 14:11:58-07:00
committergravatar for andrew@ziglang.orgAndrew Kelley <andrew@ziglang.org> 2020-12-24 14:11:58-07:00
log6ab9268a9024fe8e9076f07cb207107d2cad0876
tree64269aabb1a50ba35b9ac0951d9c365d2916be2e
parent894434988d96f3246e0525000f281563c1febb73

stage2: re-use compiler runtime libs across opt modes and strip flag

Previously Zig would need to recompile runtime libs if you changed the values of --strip or -O. Now, unless the `debug_compiler_runtime_libs` flag is set (which is currently not exposed to the CLI), Zig will always choose ReleaseFast or ReleaseSmall for compiler runtime libraries. When the main application chooses ReleaseFast or ReleaseSmall, that value is propagated to compiler runtime libraries. Otherwise a decision is made based on the target, which is currently ReleaseSmall for freestanding WebAssembly and ReleaseFast for everything else. Ultimately the purpose of this commit is to have Debug and ReleaseSafe builds of applications still get optimized builds of, e.g. libcxx and libunwind, as well as to spend less time unnecessarily rebuilding compiler runtime libraries.

8 files changed, 50 insertions(+), 58 deletions(-)

src/Compilation.zig+30-12
......@@ -131,6 +131,7 @@ mutex: std.Mutex = .{},
131131test_filter: ?[]const u8,
132132test_name_prefix: ?[]const u8,
133133test_evented_io: bool,
134debug_compiler_runtime_libs: bool,
134135
135136emit_asm: ?EmitLoc,
136137emit_llvm_ir: ?EmitLoc,
......@@ -429,6 +430,7 @@ pub const InitOptions = struct {
429430 verbose_llvm_cpu_features: bool = false,
430431 is_test: bool = false,
431432 test_evented_io: bool = false,
433 debug_compiler_runtime_libs: bool = false,
432434 /// Normally when you create a `Compilation`, Zig will automatically build
433435 /// and link in required dependencies, such as compiler-rt and libc. When
434436 /// building such dependencies themselves, this flag must be set to avoid
......@@ -1025,6 +1027,7 @@ pub fn create(gpa: *Allocator, options: InitOptions) !*Compilation {
10251027 .test_filter = options.test_filter,
10261028 .test_name_prefix = options.test_name_prefix,
10271029 .test_evented_io = options.test_evented_io,
1030 .debug_compiler_runtime_libs = options.debug_compiler_runtime_libs,
10281031 .work_queue_wait_group = undefined,
10291032 };
10301033 break :comp comp;
......@@ -2891,14 +2894,6 @@ fn buildOutputFromZig(
28912894 .directory = null, // Put it in the cache directory.
28922895 .basename = bin_basename,
28932896 };
2894 const optimize_mode: std.builtin.Mode = blk: {
2895 if (comp.bin_file.options.is_test)
2896 break :blk comp.bin_file.options.optimize_mode;
2897 switch (comp.bin_file.options.optimize_mode) {
2898 .Debug, .ReleaseFast, .ReleaseSafe => break :blk .ReleaseFast,
2899 .ReleaseSmall => break :blk .ReleaseSmall,
2900 }
2901 };
29022897 const sub_compilation = try Compilation.create(comp.gpa, .{
29032898 .global_cache_directory = comp.global_cache_directory,
29042899 .local_cache_directory = comp.global_cache_directory,
......@@ -2910,7 +2905,7 @@ fn buildOutputFromZig(
29102905 .thread_pool = comp.thread_pool,
29112906 .libc_installation = comp.bin_file.options.libc_installation,
29122907 .emit_bin = emit_bin,
2913 .optimize_mode = optimize_mode,
2908 .optimize_mode = comp.compilerRtOptMode(),
29142909 .link_mode = .Static,
29152910 .function_sections = true,
29162911 .want_sanitize_c = false,
......@@ -2920,7 +2915,7 @@ fn buildOutputFromZig(
29202915 .want_pic = comp.bin_file.options.pic,
29212916 .want_pie = comp.bin_file.options.pie,
29222917 .emit_h = null,
2923 .strip = comp.bin_file.options.strip,
2918 .strip = comp.compilerRtStrip(),
29242919 .is_native_os = comp.bin_file.options.is_native_os,
29252920 .is_native_abi = comp.bin_file.options.is_native_abi,
29262921 .self_exe_path = comp.self_exe_path,
......@@ -3289,7 +3284,7 @@ pub fn build_crt_file(
32893284 .thread_pool = comp.thread_pool,
32903285 .libc_installation = comp.bin_file.options.libc_installation,
32913286 .emit_bin = emit_bin,
3292 .optimize_mode = comp.bin_file.options.optimize_mode,
3287 .optimize_mode = comp.compilerRtOptMode(),
32933288 .want_sanitize_c = false,
32943289 .want_stack_check = false,
32953290 .want_valgrind = false,
......@@ -3297,7 +3292,7 @@ pub fn build_crt_file(
32973292 .want_pic = comp.bin_file.options.pic,
32983293 .want_pie = comp.bin_file.options.pie,
32993294 .emit_h = null,
3300 .strip = comp.bin_file.options.strip,
3295 .strip = comp.compilerRtStrip(),
33013296 .is_native_os = comp.bin_file.options.is_native_os,
33023297 .is_native_abi = comp.bin_file.options.is_native_abi,
33033298 .self_exe_path = comp.self_exe_path,
......@@ -3344,3 +3339,26 @@ pub fn stage1AddLinkLib(comp: *Compilation, lib_name: []const u8) !void {
33443339 });
33453340 }
33463341}
3342
3343/// This decides the optimization mode for all zig-provided libraries, including
3344/// compiler-rt, libcxx, libc, libunwind, etc.
3345pub fn compilerRtOptMode(comp: Compilation) std.builtin.Mode {
3346 if (comp.debug_compiler_runtime_libs) {
3347 return comp.bin_file.options.optimize_mode;
3348 }
3349 switch (comp.bin_file.options.optimize_mode) {
3350 .Debug, .ReleaseSafe => return target_util.defaultCompilerRtOptimizeMode(comp.getTarget()),
3351 .ReleaseFast => return .ReleaseFast,
3352 .ReleaseSmall => return .ReleaseSmall,
3353 }
3354}
3355
3356/// This decides whether to strip debug info for all zig-provided libraries, including
3357/// compiler-rt, libcxx, libc, libunwind, etc.
3358pub fn compilerRtStrip(comp: Compilation) bool {
3359 if (comp.debug_compiler_runtime_libs) {
3360 return comp.bin_file.options.strip;
3361 } else {
3362 return true;
3363 }
3364}
src/glibc.zig+2-10
......@@ -271,7 +271,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
271271 try lib_path(comp, arena, lib_libc_glibc ++ "include" ++ path.sep_str ++ "libc-symbols.h"),
272272 "-DTOP_NAMESPACE=glibc",
273273 "-DASSEMBLER",
274 "-g",
275274 "-Wa,--noexecstack",
276275 });
277276 return comp.build_crt_file("crti", .Obj, &[1]Compilation.CSourceFile{
......@@ -291,7 +290,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
291290 try lib_path(comp, arena, lib_libc_glibc ++ "include" ++ path.sep_str ++ "libc-symbols.h"),
292291 "-DTOP_NAMESPACE=glibc",
293292 "-DASSEMBLER",
294 "-g",
295293 "-Wa,--noexecstack",
296294 });
297295 return comp.build_crt_file("crtn", .Obj, &[1]Compilation.CSourceFile{
......@@ -317,7 +315,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
317315 "-DSHARED",
318316 "-DTOP_NAMESPACE=glibc",
319317 "-DASSEMBLER",
320 "-g",
321318 "-Wa,--noexecstack",
322319 });
323320 break :blk .{
......@@ -337,7 +334,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
337334 "-DMODULE_NAME=libc",
338335 "-DTOP_NAMESPACE=glibc",
339336 "-DASSEMBLER",
340 "-g",
341337 "-Wa,--noexecstack",
342338 });
343339 break :blk .{
......@@ -372,8 +368,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
372368 try args.appendSlice(&[_][]const u8{
373369 "-std=gnu11",
374370 "-fgnu89-inline",
375 "-g",
376 "-O2",
377371 "-fmerge-all-constants",
378372 "-fno-stack-protector",
379373 "-fmath-errno",
......@@ -409,8 +403,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
409403 try args.appendSlice(&[_][]const u8{
410404 "-std=gnu11",
411405 "-fgnu89-inline",
412 "-g",
413 "-O2",
414406 "-fmerge-all-constants",
415407 "-fno-stack-protector",
416408 "-fmath-errno",
......@@ -939,13 +931,13 @@ fn buildSharedLib(
939931 .thread_pool = comp.thread_pool,
940932 .libc_installation = comp.bin_file.options.libc_installation,
941933 .emit_bin = emit_bin,
942 .optimize_mode = comp.bin_file.options.optimize_mode,
934 .optimize_mode = comp.compilerRtOptMode(),
943935 .want_sanitize_c = false,
944936 .want_stack_check = false,
945937 .want_valgrind = false,
946938 .want_tsan = false,
947939 .emit_h = null,
948 .strip = comp.bin_file.options.strip,
940 .strip = comp.compilerRtStrip(),
949941 .is_native_os = false,
950942 .is_native_abi = false,
951943 .self_exe_path = comp.self_exe_path,
src/libcxx.zig+4-9
......@@ -138,7 +138,6 @@ pub fn buildLibCXX(comp: *Compilation) !void {
138138 try cflags.append("-I");
139139 try cflags.append(cxxabi_include_path);
140140
141 try cflags.append("-O3");
142141 if (target_util.supports_fpic(target)) {
143142 try cflags.append("-fPIC");
144143 }
......@@ -164,7 +163,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {
164163 .thread_pool = comp.thread_pool,
165164 .libc_installation = comp.bin_file.options.libc_installation,
166165 .emit_bin = emit_bin,
167 .optimize_mode = comp.bin_file.options.optimize_mode,
166 .optimize_mode = comp.compilerRtOptMode(),
168167 .link_mode = link_mode,
169168 .want_sanitize_c = false,
170169 .want_stack_check = false,
......@@ -173,7 +172,7 @@ pub fn buildLibCXX(comp: *Compilation) !void {
173172 .want_pic = comp.bin_file.options.pic,
174173 .want_pie = comp.bin_file.options.pie,
175174 .emit_h = null,
176 .strip = comp.bin_file.options.strip,
175 .strip = comp.compilerRtStrip(),
177176 .is_native_os = comp.bin_file.options.is_native_os,
178177 .is_native_abi = comp.bin_file.options.is_native_abi,
179178 .self_exe_path = comp.self_exe_path,
......@@ -256,16 +255,12 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
256255 try cflags.append("-I");
257256 try cflags.append(cxx_include_path);
258257
259 try cflags.append("-O3");
260 try cflags.append("-DNDEBUG");
261258 if (target_util.supports_fpic(target)) {
262259 try cflags.append("-fPIC");
263260 }
264261 try cflags.append("-nostdinc++");
265262 try cflags.append("-fstrict-aliasing");
266263 try cflags.append("-funwind-tables");
267 try cflags.append("-D_DEBUG");
268 try cflags.append("-UNDEBUG");
269264 try cflags.append("-std=c++11");
270265
271266 c_source_files[i] = .{
......@@ -285,7 +280,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
285280 .thread_pool = comp.thread_pool,
286281 .libc_installation = comp.bin_file.options.libc_installation,
287282 .emit_bin = emit_bin,
288 .optimize_mode = comp.bin_file.options.optimize_mode,
283 .optimize_mode = comp.compilerRtOptMode(),
289284 .link_mode = link_mode,
290285 .want_sanitize_c = false,
291286 .want_stack_check = false,
......@@ -294,7 +289,7 @@ pub fn buildLibCXXABI(comp: *Compilation) !void {
294289 .want_pic = comp.bin_file.options.pic,
295290 .want_pie = comp.bin_file.options.pie,
296291 .emit_h = null,
297 .strip = comp.bin_file.options.strip,
292 .strip = comp.compilerRtStrip(),
298293 .is_native_os = comp.bin_file.options.is_native_os,
299294 .is_native_abi = comp.bin_file.options.is_native_abi,
300295 .self_exe_path = comp.self_exe_path,
src/libtsan.zig+2-14
......@@ -43,8 +43,6 @@ pub fn buildTsan(comp: *Compilation) !void {
4343 try cflags.append("-I");
4444 try cflags.append(tsan_include_path);
4545
46 try cflags.append("-O3");
47 try cflags.append("-DNDEBUG");
4846 try cflags.append("-nostdinc++");
4947 try cflags.append("-fvisibility-inlines-hidden");
5048 try cflags.append("-std=c++14");
......@@ -67,8 +65,6 @@ pub fn buildTsan(comp: *Compilation) !void {
6765 try cflags.append("-I");
6866 try cflags.append(tsan_include_path);
6967
70 try cflags.append("-O3");
71 try cflags.append("-DNDEBUG");
7268 try cflags.append("-nostdinc++");
7369 try cflags.append("-fvisibility-inlines-hidden");
7470 try cflags.append("-std=c++14");
......@@ -110,8 +106,6 @@ pub fn buildTsan(comp: *Compilation) !void {
110106 try cflags.append("-I");
111107 try cflags.append(sanitizer_common_include_path);
112108
113 try cflags.append("-O3");
114 try cflags.append("-DNDEBUG");
115109 try cflags.append("-nostdinc++");
116110 try cflags.append("-fvisibility-inlines-hidden");
117111 try cflags.append("-std=c++14");
......@@ -136,8 +130,6 @@ pub fn buildTsan(comp: *Compilation) !void {
136130 try cflags.append("-I");
137131 try cflags.append(sanitizer_common_include_path);
138132
139 try cflags.append("-O3");
140 try cflags.append("-DNDEBUG");
141133 try cflags.append("-nostdinc++");
142134 try cflags.append("-fvisibility-inlines-hidden");
143135 try cflags.append("-std=c++14");
......@@ -158,8 +150,6 @@ pub fn buildTsan(comp: *Compilation) !void {
158150 try cflags.append("-I");
159151 try cflags.append(tsan_include_path);
160152
161 try cflags.append("-O3");
162 try cflags.append("-DNDEBUG");
163153 try cflags.append("-nostdinc++");
164154 try cflags.append("-fvisibility-inlines-hidden");
165155 try cflags.append("-std=c++14");
......@@ -188,8 +178,6 @@ pub fn buildTsan(comp: *Compilation) !void {
188178 try cflags.append("-I");
189179 try cflags.append(tsan_include_path);
190180
191 try cflags.append("-O3");
192 try cflags.append("-DNDEBUG");
193181 try cflags.append("-nostdinc++");
194182 try cflags.append("-fvisibility-inlines-hidden");
195183 try cflags.append("-std=c++14");
......@@ -218,7 +206,7 @@ pub fn buildTsan(comp: *Compilation) !void {
218206 .thread_pool = comp.thread_pool,
219207 .libc_installation = comp.bin_file.options.libc_installation,
220208 .emit_bin = emit_bin,
221 .optimize_mode = comp.bin_file.options.optimize_mode,
209 .optimize_mode = comp.compilerRtOptMode(),
222210 .link_mode = link_mode,
223211 .want_sanitize_c = false,
224212 .want_stack_check = false,
......@@ -227,7 +215,7 @@ pub fn buildTsan(comp: *Compilation) !void {
227215 .want_pic = true,
228216 .want_pie = true,
229217 .emit_h = null,
230 .strip = comp.bin_file.options.strip,
218 .strip = comp.compilerRtStrip(),
231219 .is_native_os = comp.bin_file.options.is_native_os,
232220 .is_native_abi = comp.bin_file.options.is_native_abi,
233221 .self_exe_path = comp.self_exe_path,
src/libunwind.zig+2-2
......@@ -104,7 +104,7 @@ pub fn buildStaticLib(comp: *Compilation) !void {
104104 .thread_pool = comp.thread_pool,
105105 .libc_installation = comp.bin_file.options.libc_installation,
106106 .emit_bin = emit_bin,
107 .optimize_mode = comp.bin_file.options.optimize_mode,
107 .optimize_mode = comp.compilerRtOptMode(),
108108 .link_mode = link_mode,
109109 .want_sanitize_c = false,
110110 .want_stack_check = false,
......@@ -113,7 +113,7 @@ pub fn buildStaticLib(comp: *Compilation) !void {
113113 .want_pic = comp.bin_file.options.pic,
114114 .want_pie = comp.bin_file.options.pie,
115115 .emit_h = null,
116 .strip = comp.bin_file.options.strip,
116 .strip = comp.compilerRtStrip(),
117117 .is_native_os = comp.bin_file.options.is_native_os,
118118 .is_native_abi = comp.bin_file.options.is_native_abi,
119119 .self_exe_path = comp.self_exe_path,
src/mingw.zig-9
......@@ -91,8 +91,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
9191 "-D_CRTBLD",
9292 "-D_WIN32_WINNT=0x0f00",
9393 "-D__MSVCRT_VERSION__=0x700",
94 "-g",
95 "-O2",
9694 });
9795 c_source_files[i] = .{
9896 .src_path = try comp.zig_lib_directory.join(arena, &[_][]const u8{
......@@ -119,9 +117,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
119117
120118 "-isystem",
121119 try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "include", "any-windows-any" }),
122
123 "-g",
124 "-O2",
125120 });
126121 var c_source_files = std.ArrayList(Compilation.CSourceFile).init(arena);
127122
......@@ -167,8 +162,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
167162 "-D_CRTBLD",
168163 "-D_WIN32_WINNT=0x0f00",
169164 "-D__MSVCRT_VERSION__=0x700",
170 "-g",
171 "-O2",
172165
173166 "-isystem",
174167 try comp.zig_lib_directory.join(arena, &[_][]const u8{ "libc", "include", "any-windows-any" }),
......@@ -233,8 +226,6 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
233226 "-D_CRTBLD",
234227 "-D_WIN32_WINNT=0x0f00",
235228 "-D__MSVCRT_VERSION__=0x700",
236 "-g",
237 "-O2",
238229
239230 "-isystem",
240231 try comp.zig_lib_directory.join(arena, &[_][]const u8{
src/musl.zig+2-2
......@@ -203,13 +203,13 @@ pub fn buildCRTFile(comp: *Compilation, crt_file: CRTFile) !void {
203203 .thread_pool = comp.thread_pool,
204204 .libc_installation = comp.bin_file.options.libc_installation,
205205 .emit_bin = Compilation.EmitLoc{ .directory = null, .basename = "libc.so" },
206 .optimize_mode = comp.bin_file.options.optimize_mode,
206 .optimize_mode = comp.compilerRtOptMode(),
207207 .want_sanitize_c = false,
208208 .want_stack_check = false,
209209 .want_valgrind = false,
210210 .want_tsan = false,
211211 .emit_h = null,
212 .strip = comp.bin_file.options.strip,
212 .strip = comp.compilerRtStrip(),
213213 .is_native_os = false,
214214 .is_native_abi = false,
215215 .self_exe_path = comp.self_exe_path,
src/target.zig+8
......@@ -344,3 +344,11 @@ pub fn is_libcpp_lib_name(target: std.Target, name: []const u8) bool {
344344pub fn hasDebugInfo(target: std.Target) bool {
345345 return !target.cpu.arch.isWasm();
346346}
347
348pub fn defaultCompilerRtOptimizeMode(target: std.Target) std.builtin.Mode {
349 if (target.cpu.arch.isWasm() and target.os.tag == .freestanding) {
350 return .ReleaseSmall;
351 } else {
352 return .ReleaseFast;
353 }
354}