1 # -*- Mode: python; indent-tabs-mode: nil; tab-width: 40 -*-
2 # vim: set filetype=python:
3 # This Source Code Form is subject to the terms of the Mozilla Public
4 # License, v. 2.0. If a copy of the MPL was not distributed with this
5 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 # ==============================================================
9 llvm_profdata = check_prog(
10 "LLVM_PROFDATA", ["llvm-profdata"], allow_missing=True, paths=clang_search_path
14 @depends_if(llvm_profdata)
15 @checking("whether llvm-profdata supports 'order' subcommand")
16 def llvm_profdata_order(profdata):
17 retcode, _, _ = get_cmd_output(profdata, "order", "--help")
22 "--enable-profile-generate",
23 env="MOZ_PROFILE_GENERATE",
26 help="Build a PGO instrumented binary",
29 enable_profile_generate = depends_if("--enable-profile-generate")(lambda _: True)
32 imply_option("MOZ_PGO", enable_profile_generate)
34 set_config("MOZ_PROFILE_GENERATE", enable_profile_generate)
36 set_define("MOZ_PROFILE_GENERATE", enable_profile_generate)
39 "--enable-profile-use",
40 env="MOZ_PROFILE_USE",
43 help="Use a generated profile during the build",
45 enable_profile_use = depends_if("--enable-profile-use")(lambda _: True)
47 imply_option("MOZ_PGO", enable_profile_use)
48 set_config("MOZ_PROFILE_USE", enable_profile_use)
52 "--with-pgo-profile-path",
53 help="Path to the directory with unmerged profile data to use during the build"
54 ", or to a merged profdata file",
60 "--with-pgo-profile-path",
61 "--enable-profile-use",
66 def pgo_profile_path(path, pgo_use, profdata, build_env):
67 topobjdir = build_env.topobjdir
68 if topobjdir.endswith("/js/src"):
69 topobjdir = topobjdir[:-7]
72 return os.path.join(topobjdir, "instrumented", "merged.profdata")
73 if path and not pgo_use:
74 die("Pass --enable-profile-use to use --with-pgo-profile-path.")
75 if path and not profdata:
76 die("LLVM_PROFDATA must be set to process the pgo profile.")
77 if not os.path.isfile(path[0]):
78 die("Argument to --with-pgo-profile-path must be a file.")
79 if not os.path.isabs(path[0]):
80 die("Argument to --with-pgo-profile-path must be an absolute path.")
84 set_config("PGO_PROFILE_PATH", pgo_profile_path)
88 "--enable-profile-use",
94 def orderfile_path(profile_use, path, profdata, profdata_order, build_env):
98 if not profdata_order:
101 topobjdir = build_env.topobjdir
103 orderfile = os.path.join(topobjdir, "orderfile.txt")
104 check_cmd_output(profdata, "order", path, "-o", orderfile)
108 pgo_temporal = c_compiler.try_compile(
109 flags=["-fprofile-generate", "-mllvm", "-pgo-temporal-instrumentation"],
110 check_msg="whether the C compiler supports temporal instrumentation",
111 when="--enable-profile-generate",
124 @imports("multiprocessing")
126 compiler, linker, target, profdata, orderfile, target_is_windows, pgo_temporal
128 if compiler.type == "gcc":
130 gen_cflags=["-fprofile-generate"],
131 gen_ldflags=["-fprofile-generate"],
132 use_cflags=["-fprofile-use", "-fprofile-correction", "-Wcoverage-mismatch"],
133 use_ldflags=["-fprofile-use"],
136 if compiler.type in ("clang-cl", "clang"):
138 if compiler.type == "clang-cl":
142 gen_ldflags = ["-fprofile-generate"]
146 if compiler.type == "clang-cl":
148 "-ORDER:@" + orderfile,
149 "/ignore:4037", # Disable warn missing order symbol
151 elif linker.KIND == "ld64" or (linker.KIND == "lld" and target.os == "OSX"):
152 use_ldflags += ["-Wl,-order_file", orderfile]
153 elif linker.KIND == "lld":
155 "-Wl,--symbol-ordering-file",
157 "-Wl,--no-warn-symbol-ordering",
161 log.info("Activating PGO-based orderfile")
163 gen_cflags = [prefix + "-fprofile-generate"]
166 gen_cflags += ["-mllvm", "-pgo-temporal-instrumentation"]
168 if target_is_windows:
169 # native llvm-profdata.exe on Windows can't read profile data
170 # if name compression is enabled (which cross-compiling enables
172 gen_cflags += ["-mllvm", "-enable-name-compression=false"]
175 gen_cflags=gen_cflags,
176 gen_ldflags=gen_ldflags,
178 prefix + "-fprofile-use=%s" % profdata,
179 # Some error messages about mismatched profile data
180 # come in via -Wbackend-plugin, so disable those too.
181 "-Wno-error=backend-plugin",
183 use_ldflags=use_ldflags,
187 set_config("PROFILE_GEN_CFLAGS", pgo_flags.gen_cflags)
188 set_config("PROFILE_GEN_LDFLAGS", pgo_flags.gen_ldflags)
189 set_config("PROFILE_USE_CFLAGS", pgo_flags.use_cflags)
190 set_config("PROFILE_USE_LDFLAGS", pgo_flags.use_ldflags)
194 help="Use the provided jarlog file when packaging during a profile-use " "build",
198 set_config("PGO_JARLOG_PATH", depends_if("--with-pgo-jarlog")(lambda p: p))
201 @depends("MOZ_PGO", "--enable-profile-use", "--enable-profile-generate", c_compiler)
202 def moz_pgo_rust(pgo, profile_use, profile_generate, c_compiler):
206 # Enabling PGO through MOZ_PGO only and not --enable* flags.
207 if not profile_use and not profile_generate:
210 if profile_use and profile_generate:
211 die("Cannot build with --enable-profile-use and --enable-profile-generate.")
213 want_cross = (len(profile_use) and profile_use[0] == "cross") or (
214 len(profile_generate) and profile_generate[0] == "cross"
220 if c_compiler.type == "gcc":
221 die("Cannot use cross-language PGO with GCC.")
226 set_config("MOZ_PGO_RUST", moz_pgo_rust)
229 # ==============================================================
235 choices=("full", "thin", "cross"),
240 env="MOZ_LD64_KNOWN_GOOD",
242 help="Indicate that ld64 is free of symbol aliasing bugs.",
245 imply_option("MOZ_LD64_KNOWN_GOOD", depends_if("MOZ_AUTOMATION")(lambda _: True))
252 "MOZ_LD64_KNOWN_GOOD",
254 "--enable-profile-generate",
255 pass_manager.enabled,
256 "--enable-profile-use",
259 @imports("multiprocessing")
279 # Sanitize LTO modes.
280 if "full" in values and "thin" in values:
281 die("incompatible --enable-lto choices 'full' and 'thin'")
283 # If a value was given to --enable-lto, use that. Otherwise, make the lto
284 # mode explicit, using full with gcc, and full or thin with clang depending
285 # on the performance benefit.
286 # Defaulting to full LTO is costly in terms of compilation time, so we only
287 # default to it if MOZ_AUTOMATION and PGO are on, and for some platforms.
288 # Based on speedometer3 scores, full lto + pgo is beneficial for Linux and
289 # Windows for x86_64 targets.
290 if values == () or values == ("cross",):
291 if c_compiler.type == "gcc":
296 and target.os in ("WINNT", "GNU")
297 and target.cpu == "x86_64"
303 if instrumented_build:
304 log.warning("Disabling LTO because --enable-profile-generate is specified")
307 if c_compiler.type == "gcc":
308 if "cross" in values:
309 die("Cross-language LTO is not supported with GCC.")
312 "gcc does not support thin LTO. Use `--enable-lto` "
313 "to enable full LTO for gcc."
317 target.kernel == "Darwin"
318 and "cross" in values
319 and select_linker.KIND == "ld64"
320 and not ld64_known_good
323 "The Mac linker is known to have a bug that affects cross-language "
324 "LTO. If you know that your linker is free from this bug, please "
325 "set the environment variable `MOZ_LD64_KNOWN_GOOD=1` and re-run "
329 if c_compiler.type == "clang":
331 cflags.append("-flto")
332 ldflags.append("-flto")
334 cflags.append("-flto=thin")
335 ldflags.append("-flto=thin")
337 if target.os == "Android" and "cross" in values:
338 # Work around https://github.com/rust-lang/rust/issues/90088
339 # by enabling the highest level of SSE the rust targets default
341 # https://github.com/rust-lang/rust/blob/bdfcb88e8b6203ccb46a2fb6649979b773efc8ac/compiler/rustc_target/src/spec/i686_linux_android.rs#L13
342 # https://github.com/rust-lang/rust/blob/8d1083e319841624f64400e1524805a40d725439/compiler/rustc_target/src/spec/x86_64_linux_android.rs#L7
343 if target.cpu == "x86":
344 ldflags.append("-Wl,-plugin-opt=-mattr=+ssse3")
345 elif target.cpu == "x86_64":
346 ldflags.append("-Wl,-plugin-opt=-mattr=+sse4.2")
347 elif c_compiler.type == "clang-cl":
349 cflags.append("-flto")
351 cflags.append("-flto=thin")
352 # With clang-cl, -flto can only be used with -c or -fuse-ld=lld.
353 # AC_TRY_LINKs during configure don't have -c, so pass -fuse-ld=lld.
354 cflags.append("-fuse-ld=lld")
356 # Explicitly set the CPU to optimize for so the linker doesn't
357 # choose a poor default. Rust compilation by default uses the
358 # pentium4 CPU on x86:
360 # https://github.com/rust-lang/rust/blob/049a49b91151a88c95fa0d62a53fd0a0ac2c3af9/compiler/rustc_target/src/spec/i686_pc_windows_msvc.rs#L5
362 # which specifically supports "long" (multi-byte) nops. See
363 # https://bugzilla.mozilla.org/show_bug.cgi?id=1568450#c8 for details.
365 # The pentium4 seems like kind of a weird CPU to optimize for, but
366 # it seems to have worked out OK thus far. LLVM does not seem to
367 # specifically schedule code for the pentium4's deep pipeline, so
368 # that probably contributes to it being an OK default for our
370 if target.cpu == "x86":
371 ldflags.append("-mllvm:-mcpu=pentium4")
372 # This is also the CPU that Rust uses. The LLVM source code
373 # recommends this as the "generic 64-bit specific x86 processor model":
375 # https://github.com/llvm/llvm-project/blob/e7694f34ab6a12b8bb480cbfcb396d0a64fe965f/llvm/lib/Target/X86/X86.td#L1165-L1187
376 if target.cpu == "x86_64":
377 ldflags.append("-mllvm:-mcpu=x86-64")
378 # We do not need special flags for arm64. Hooray for fixed-length
381 num_cores = multiprocessing.cpu_count()
382 cflags.append("-flto")
383 cflags.append("-flifetime-dse=1")
385 ldflags.append("-flto=%s" % num_cores)
386 ldflags.append("-flifetime-dse=1")
388 # Tell LTO not to inline functions above a certain size, to mitigate
389 # binary size growth while still getting good performance.
390 # (For hot functions, PGO will put a multiplier on this limit.)
391 if target.os == "WINNT":
392 ldflags.append("-mllvm:-import-instr-limit=10")
393 elif target.kernel == "Darwin":
394 ldflags.append("-Wl,-mllvm,-import-instr-limit=10")
395 elif c_compiler.type == "clang":
396 ldflags.append("-Wl,-plugin-opt=-import-instr-limit=10")
398 # If we're using the new pass manager, we can also enable the new PM
399 # during LTO. Further we can use the resulting size savings to increase
400 # the import limit in hot functions.
402 if target.os == "WINNT":
403 if c_compiler.version >= "12.0.0" and c_compiler.version < "13.0.0":
404 ldflags.append("-opt:ltonewpassmanager")
405 if c_compiler.version >= "12.0.0":
406 ldflags.append("-mllvm:-import-hot-multiplier=30")
407 elif target.kernel == "Darwin":
408 ldflags.append("-Wl,-mllvm,-import-hot-multiplier=30")
410 if c_compiler.version < "13.0.0":
411 ldflags.append("-Wl,-plugin-opt=new-pass-manager")
412 ldflags.append("-Wl,-plugin-opt=-import-hot-multiplier=30")
414 # Pick Rust LTO mode in case of cross lTO. Thin is the default.
415 if "cross" in values:
416 rust_lto = "full" if "full" in values else "thin"
430 when=building_with_gnu_compatible_cc
433 & ~enable_profile_generate,
435 def remove_dead_symbols(dso_flags):
436 dso_flags.ldopts.append("-Wl,--gc-sections")
439 add_old_configure_assignment("MOZ_LTO", lto.enabled)
440 set_config("MOZ_LTO", lto.enabled)
441 set_define("MOZ_LTO", lto.enabled)
442 set_config("MOZ_LTO_CFLAGS", lto.cflags)
443 set_config("MOZ_LTO_LDFLAGS", lto.ldflags)
444 set_config("MOZ_LTO_RUST_CROSS", lto.rust_lto)
445 add_old_configure_assignment("MOZ_LTO_CFLAGS", lto.cflags)
446 add_old_configure_assignment("MOZ_LTO_LDFLAGS", lto.ldflags)