Debian Codegraph @

build/moz.configure/lto-pgo.configure

raw
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/.
6
7 # PGO
8 # ==============================================================
9
10
11 option(
12 "--enable-profile-generate",
13 env="MOZ_PROFILE_GENERATE",
14 nargs="?",
15 choices=("cross",),
16 help="Build a PGO instrumented binary",
17 )
18
19 enable_profile_generate = depends_if("--enable-profile-generate")(lambda _: True)
20
21
22 imply_option("MOZ_PGO", enable_profile_generate)
23
24 set_config("MOZ_PROFILE_GENERATE", enable_profile_generate)
25
26 set_define("MOZ_PROFILE_GENERATE", enable_profile_generate)
27
28 option(
29 "--enable-profile-use",
30 env="MOZ_PROFILE_USE",
31 nargs="?",
32 choices=("cross",),
33 help="Use a generated profile during the build",
34 )
35 enable_profile_use = depends_if("--enable-profile-use")(lambda _: True)
36
37 imply_option("MOZ_PGO", enable_profile_use)
38 set_config("MOZ_PROFILE_USE", enable_profile_use)
39
40 llvm_profdata = check_prog(
41 "LLVM_PROFDATA", ["llvm-profdata"], allow_missing=True, paths=clang_search_path
42 )
43
44
45 # --call-graph-profile-sort is the default behavior for lld, and it proves to be
46 # more efficient than pgo-based orderfile. Don't pass it explicitly because it's
47 # option support differ from one version to the other (w/ or W: argument and the
48 # argument value).
49 @depends(select_linker, target)
50 def pgo_cg_sort(linker, target):
51 return linker and linker.KIND == "lld" and target.os != "OSX"
52
53
54 @depends_if(llvm_profdata, when=enable_profile_use & ~pgo_cg_sort)
55 @checking("whether llvm-profdata supports 'order' subcommand")
56 def llvm_profdata_order(profdata):
57 retcode, _, _ = get_cmd_output(profdata, "order", "--help")
58 return retcode == 0
59
60
61 option(
62 "--with-pgo-profile-path",
63 help="Path to the directory with unmerged profile data to use during the build"
64 ", or to a merged profdata file",
65 nargs=1,
66 )
67
68
69 @depends(
70 "--with-pgo-profile-path",
71 "--enable-profile-use",
72 llvm_profdata,
73 build_environment,
74 )
75 @imports("os")
76 def pgo_profile_path(path, pgo_use, profdata, build_env):
77 topobjdir = build_env.topobjdir
78 if topobjdir.endswith("/js/src"):
79 topobjdir = topobjdir[:-7]
80
81 if not path:
82 return os.path.join(topobjdir, "instrumented", "merged.profdata")
83 if path and not pgo_use:
84 die("Pass --enable-profile-use to use --with-pgo-profile-path.")
85 if path and not profdata:
86 die("LLVM_PROFDATA must be set to process the pgo profile.")
87 if not os.path.isfile(path[0]):
88 die("Argument to --with-pgo-profile-path must be a file.")
89 if not os.path.isabs(path[0]):
90 die("Argument to --with-pgo-profile-path must be an absolute path.")
91 return path[0]
92
93
94 set_config("PGO_PROFILE_PATH", pgo_profile_path)
95
96
97 @depends(
98 "--enable-profile-use",
99 pgo_profile_path,
100 llvm_profdata,
101 llvm_profdata_order,
102 build_environment,
103 )
104 def orderfile_path(profile_use, path, profdata, profdata_order, build_env):
105 if not profile_use:
106 return None
107
108 if not profdata_order:
109 return None
110
111 topobjdir = build_env.topobjdir
112
113 orderfile = os.path.join(topobjdir, "orderfile.txt")
114 check_cmd_output(profdata, "order", path, "-o", orderfile)
115 return orderfile
116
117
118 pgo_temporal = c_compiler.try_compile(
119 flags=["-fprofile-generate", "-mllvm", "-pgo-temporal-instrumentation"],
120 check_msg="whether the C compiler supports temporal instrumentation",
121 when=enable_profile_generate & ~pgo_cg_sort,
122 )
123
124
125 @depends(
126 c_compiler,
127 select_linker,
128 target,
129 pgo_profile_path,
130 target_is_windows,
131 pgo_temporal,
132 orderfile_path,
133 )
134 @imports("multiprocessing")
135 def pgo_flags(
136 compiler,
137 linker,
138 target,
139 profdata,
140 target_is_windows,
141 pgo_temporal,
142 orderfile,
143 ):
144 if compiler.type == "gcc":
145 return namespace(
146 gen_cflags=["-fprofile-generate"],
147 gen_ldflags=["-fprofile-generate"],
148 use_cflags=["-fprofile-use", "-fprofile-correction", "-Wcoverage-mismatch"],
149 use_ldflags=["-fprofile-use"],
150 )
151
152 if compiler.type in ("clang-cl", "clang"):
153 prefix = ""
154 if compiler.type == "clang-cl":
155 prefix = "/clang:"
156 gen_ldflags = None
157 else:
158 gen_ldflags = ["-fprofile-generate"]
159
160 use_ldflags = []
161 if orderfile:
162 if compiler.type == "clang-cl":
163 use_ldflags += [
164 "-ORDER:@" + orderfile,
165 "/ignore:4037", # Disable warn missing order symbol
166 ]
167 elif linker.KIND == "ld64" or (linker.KIND == "lld" and target.os == "OSX"):
168 use_ldflags += ["-Wl,-order_file", orderfile]
169 elif linker.KIND == "lld":
170 use_ldflags += [
171 "-Wl,--symbol-ordering-file",
172 orderfile,
173 "-Wl,--no-warn-symbol-ordering",
174 ]
175
176 if use_ldflags:
177 log.info("Activating PGO-based orderfile")
178
179 gen_cflags = [prefix + "-fprofile-generate"]
180
181 if pgo_temporal:
182 gen_cflags += ["-mllvm", "-pgo-temporal-instrumentation"]
183
184 if target_is_windows:
185 # native llvm-profdata.exe on Windows can't read profile data
186 # if name compression is enabled (which cross-compiling enables
187 # by default)
188 gen_cflags += ["-mllvm", "-enable-name-compression=false"]
189
190 return namespace(
191 gen_cflags=gen_cflags,
192 gen_ldflags=gen_ldflags,
193 use_cflags=[
194 prefix + "-fprofile-use=%s" % profdata,
195 # Some error messages about mismatched profile data
196 # come in via -Wbackend-plugin, so disable those too.
197 "-Wno-error=backend-plugin",
198 ],
199 use_ldflags=use_ldflags,
200 )
201
202
203 set_config("PROFILE_GEN_CFLAGS", pgo_flags.gen_cflags)
204 set_config("PROFILE_GEN_LDFLAGS", pgo_flags.gen_ldflags)
205 set_config("PROFILE_USE_CFLAGS", pgo_flags.use_cflags)
206 set_config("PROFILE_USE_LDFLAGS", pgo_flags.use_ldflags)
207
208 option(
209 "--with-pgo-jarlog",
210 help="Use the provided jarlog file when packaging during a profile-use " "build",
211 nargs=1,
212 )
213
214 set_config("PGO_JARLOG_PATH", depends_if("--with-pgo-jarlog")(lambda p: p))
215
216
217 @depends("MOZ_PGO", "--enable-profile-use", "--enable-profile-generate", c_compiler)
218 def moz_pgo_rust(pgo, profile_use, profile_generate, c_compiler):
219 if not pgo:
220 return
221
222 # Enabling PGO through MOZ_PGO only and not --enable* flags.
223 if not profile_use and not profile_generate:
224 return
225
226 if profile_use and profile_generate:
227 die("Cannot build with --enable-profile-use and --enable-profile-generate.")
228
229 want_cross = (len(profile_use) and profile_use[0] == "cross") or (
230 len(profile_generate) and profile_generate[0] == "cross"
231 )
232
233 if not want_cross:
234 return
235
236 if c_compiler.type == "gcc":
237 die("Cannot use cross-language PGO with GCC.")
238
239 return True
240
241
242 set_config("MOZ_PGO_RUST", moz_pgo_rust)
243
244 # LTO
245 # ==============================================================
246
247 option(
248 "--enable-lto",
249 env="MOZ_LTO",
250 nargs="*",
251 choices=("full", "thin", "cross"),
252 help="Enable LTO",
253 )
254
255 option(
256 env="MOZ_LD64_KNOWN_GOOD",
257 nargs=1,
258 help="Indicate that ld64 is free of symbol aliasing bugs",
259 )
260
261 imply_option("MOZ_LD64_KNOWN_GOOD", moz_automation)
262
263
264 @depends(
265 "--enable-lto",
266 c_compiler,
267 select_linker,
268 "MOZ_LD64_KNOWN_GOOD",
269 target,
270 "--enable-profile-generate",
271 pass_manager.enabled,
272 "--enable-profile-use",
273 moz_automation,
274 )
275 @imports("multiprocessing")
276 def lto(
277 values,
278 c_compiler,
279 select_linker,
280 ld64_known_good,
281 target,
282 instrumented_build,
283 pass_manager,
284 pgo_build,
285 moz_automation,
286 ):
287 cflags = []
288 ldflags = []
289 enabled = None
290 rust_lto = False
291
292 if not values:
293 return
294
295 # Sanitize LTO modes.
296 if "full" in values and "thin" in values:
297 die("incompatible --enable-lto choices 'full' and 'thin'")
298
299 # If a value was given to --enable-lto, use that. Otherwise, make the lto
300 # mode explicit, using full with gcc, and full or thin with clang depending
301 # on the performance benefit.
302 # Defaulting to full LTO is costly in terms of compilation time, so we only
303 # default to it if MOZ_AUTOMATION and PGO are on, and for some platforms.
304 # Based on speedometer3 scores, full lto + pgo is beneficial for Linux and
305 # Windows for x86_64 targets.
306 if values == () or values == ("cross",):
307 if c_compiler.type == "gcc":
308 values += ("full",)
309 elif (
310 pgo_build
311 and moz_automation
312 and target.os in ("WINNT", "GNU")
313 and target.cpu == "x86_64"
314 ):
315 values += ("full",)
316 else:
317 values += ("thin",)
318
319 if instrumented_build:
320 log.warning("Disabling LTO because --enable-profile-generate is specified")
321 return
322
323 if c_compiler.type == "gcc":
324 if "cross" in values:
325 die("Cross-language LTO is not supported with GCC.")
326 if "thin" in values:
327 die(
328 "gcc does not support thin LTO. Use `--enable-lto` "
329 "to enable full LTO for gcc."
330 )
331
332 if (
333 target.kernel == "Darwin"
334 and "cross" in values
335 and select_linker.KIND == "ld64"
336 and not ld64_known_good
337 ):
338 die(
339 "The Mac linker is known to have a bug that affects cross-language "
340 "LTO. If you know that your linker is free from this bug, please "
341 "set the environment variable `MOZ_LD64_KNOWN_GOOD=1` and re-run "
342 "configure."
343 )
344
345 if c_compiler.type == "clang":
346 if "full" in values:
347 cflags.append("-flto")
348 ldflags.append("-flto")
349 else:
350 cflags.append("-flto=thin")
351 ldflags.append("-flto=thin")
352
353 if target.os == "Android" and "cross" in values:
354 # Work around https://github.com/rust-lang/rust/issues/90088
355 # by enabling the highest level of SSE the rust targets default
356 # to.
357 # https://github.com/rust-lang/rust/blob/bdfcb88e8b6203ccb46a2fb6649979b773efc8ac/compiler/rustc_target/src/spec/i686_linux_android.rs#L13
358 # https://github.com/rust-lang/rust/blob/8d1083e319841624f64400e1524805a40d725439/compiler/rustc_target/src/spec/x86_64_linux_android.rs#L7
359 if target.cpu == "x86":
360 ldflags.append("-Wl,-plugin-opt=-mattr=+ssse3")
361 elif target.cpu == "x86_64":
362 ldflags.append("-Wl,-plugin-opt=-mattr=+sse4.2")
363 elif c_compiler.type == "clang-cl":
364 if "full" in values:
365 cflags.append("-flto")
366 else:
367 cflags.append("-flto=thin")
368 # With clang-cl, -flto can only be used with -c or -fuse-ld=lld.
369 # AC_TRY_LINKs during configure don't have -c, so pass -fuse-ld=lld.
370 cflags.append("-fuse-ld=lld")
371
372 # Explicitly set the CPU to optimize for so the linker doesn't
373 # choose a poor default. Rust compilation by default uses the
374 # pentium4 CPU on x86:
375 #
376 # https://github.com/rust-lang/rust/blob/049a49b91151a88c95fa0d62a53fd0a0ac2c3af9/compiler/rustc_target/src/spec/i686_pc_windows_msvc.rs#L5
377 #
378 # which specifically supports "long" (multi-byte) nops. See
379 # https://bugzilla.mozilla.org/show_bug.cgi?id=1568450#c8 for details.
380 #
381 # The pentium4 seems like kind of a weird CPU to optimize for, but
382 # it seems to have worked out OK thus far. LLVM does not seem to
383 # specifically schedule code for the pentium4's deep pipeline, so
384 # that probably contributes to it being an OK default for our
385 # purposes.
386 if target.cpu == "x86":
387 ldflags.append("-mllvm:-mcpu=pentium4")
388 # This is also the CPU that Rust uses. The LLVM source code
389 # recommends this as the "generic 64-bit specific x86 processor model":
390 #
391 # https://github.com/llvm/llvm-project/blob/e7694f34ab6a12b8bb480cbfcb396d0a64fe965f/llvm/lib/Target/X86/X86.td#L1165-L1187
392 if target.cpu == "x86_64":
393 ldflags.append("-mllvm:-mcpu=x86-64")
394 # We do not need special flags for arm64. Hooray for fixed-length
395 # instruction sets.
396 else:
397 num_cores = multiprocessing.cpu_count()
398 cflags.append("-flto")
399 cflags.append("-flifetime-dse=1")
400
401 ldflags.append("-flto=%s" % num_cores)
402 ldflags.append("-flifetime-dse=1")
403
404 # Tell LTO not to inline functions above a certain size, to mitigate
405 # binary size growth while still getting good performance.
406 # (For hot functions, PGO will put a multiplier on this limit.)
407 if target.os == "WINNT":
408 ldflags.append("-mllvm:-import-instr-limit=10")
409 elif target.kernel == "Darwin":
410 ldflags.append("-Wl,-mllvm,-import-instr-limit=10")
411 elif c_compiler.type == "clang":
412 ldflags.append("-Wl,-plugin-opt=-import-instr-limit=10")
413
414 # If we're using the new pass manager, we can also enable the new PM
415 # during LTO. Further we can use the resulting size savings to increase
416 # the import limit in hot functions.
417 if pass_manager:
418 if target.os == "WINNT":
419 if c_compiler.version >= "12.0.0" and c_compiler.version < "13.0.0":
420 ldflags.append("-opt:ltonewpassmanager")
421 if c_compiler.version >= "12.0.0":
422 ldflags.append("-mllvm:-import-hot-multiplier=30")
423 elif target.kernel == "Darwin":
424 ldflags.append("-Wl,-mllvm,-import-hot-multiplier=30")
425 else:
426 if c_compiler.version < "13.0.0":
427 ldflags.append("-Wl,-plugin-opt=new-pass-manager")
428 ldflags.append("-Wl,-plugin-opt=-import-hot-multiplier=30")
429
430 # Pick Rust LTO mode in case of cross lTO. Thin is the default.
431 if "cross" in values:
432 rust_lto = "full" if "full" in values else "thin"
433 else:
434 rust_lto = ""
435
436 return namespace(
437 enabled=True,
438 cflags=cflags,
439 ldflags=ldflags,
440 rust_lto=rust_lto,
441 )
442
443
444 @depends(
445 dso_flags,
446 when=building_with_gnu_compatible_cc
447 & gcc_use_gnu_ld
448 & ~developer_options
449 & ~enable_profile_generate,
450 )
451 def remove_dead_symbols(dso_flags):
452 dso_flags.ldopts.append("-Wl,--gc-sections")
453
454
455 set_config("MOZ_LTO", lto.enabled)
456 set_define("MOZ_LTO", lto.enabled)
457 set_config("MOZ_LTO_CFLAGS", lto.cflags)
458 set_config("MOZ_LTO_LDFLAGS", lto.ldflags)
459 set_config("MOZ_LTO_RUST_CROSS", lto.rust_lto)