Skip to content

Commit 807778a

Browse files
authored
Rollup merge of #141554 - Noratrieb:document-codegen-opts-better, r=bjorn3
Improve documentation for codegen options This adds more information to many different codegen options. It should not add any new guarantees, just document existing behavior. r? bjorn3
2 parents b17dba4 + 198e89b commit 807778a

File tree

1 file changed

+49
-3
lines changed
  • src/doc/rustc/src/codegen-options

1 file changed

+49
-3
lines changed

src/doc/rustc/src/codegen-options/index.md

Lines changed: 49 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,8 @@ to save information after compiling a crate to be reused when recompiling the
192192
crate, improving re-compile times. This takes a path to a directory where
193193
incremental files will be stored.
194194

195+
Using incremental compilation inhibits certain optimizations (for example by increasing the amount of codegen units) and is therefore not recommend for release builds.
196+
195197
## inline-threshold
196198

197199
This option is deprecated and does nothing.
@@ -213,6 +215,8 @@ This flag lets you append a single extra argument to the linker invocation.
213215

214216
"Append" is significant; you can pass this flag multiple times to add multiple arguments.
215217

218+
On Unix-like targets that use `cc` as the linker driver, use `-Clink-arg=-Wl,$ARG` to pass an argument to the actual linker.
219+
216220
## link-args
217221

218222
This flag lets you append multiple extra arguments to the linker invocation. The
@@ -248,6 +252,10 @@ path to the linker executable. If this flag is not specified, the linker will
248252
be inferred based on the target. See also the [linker-flavor](#linker-flavor)
249253
flag for another way to specify the linker.
250254

255+
Note that on Unix-like targets (for example, `*-unknown-linux-gnu` or `*-unknown-freebsd`)
256+
the C compiler (for example `cc` or `clang`) is used as the "linker" here, serving as a linker driver.
257+
It will invoke the actual linker with all the necessary flags to be able to link against the system libraries like libc.
258+
251259
## linker-flavor
252260

253261
This flag controls the linker flavor used by `rustc`. If a linker is given with
@@ -301,6 +309,12 @@ The list must be separated by spaces.
301309

302310
Pass `--help` to see a list of options.
303311

312+
<div class="warning">
313+
314+
Because this flag directly talks to LLVM, it is not subject to the usual stability guarantees of rustc's CLI interface.
315+
316+
</div>
317+
304318
## lto
305319

306320
This flag controls whether LLVM uses [link time
@@ -315,6 +329,7 @@ linking time. It takes one of the following values:
315329
LTO](http://blog.llvm.org/2016/06/thinlto-scalable-and-incremental-lto.html).
316330
This is similar to "fat", but takes substantially less time to run while
317331
still achieving performance gains similar to "fat".
332+
For larger projects like the Rust compiler, ThinLTO can even result in better performance than fat LTO.
318333

319334
If `-C lto` is not specified, then the compiler will attempt to perform "thin
320335
local LTO" which performs "thin" LTO on the local crate only across its
@@ -343,6 +358,8 @@ between two different versions of the same crate being linked.
343358
This flag tells the pass manager to use an empty list of passes, instead of the
344359
usual pre-populated list of passes.
345360

361+
When combined with `-O --emit llvm-ir`, it can be used to see the optimized LLVM IR emitted by rustc before any optimizations are applied by LLVM.
362+
346363
## no-redzone
347364

348365
This flag allows you to disable [the
@@ -379,7 +396,7 @@ This flag controls the optimization level.
379396
* `2`: some optimizations.
380397
* `3`: all optimizations.
381398
* `s`: optimize for binary size.
382-
* `z`: optimize for binary size, but also turn off loop vectorization.
399+
* `z`: optimize for binary size, but more aggressively. Often results in larger binaries than `s`
383400

384401
Note: The [`-O` flag][option-o-optimize] is an alias for `-C opt-level=3`.
385402

@@ -407,6 +424,9 @@ This option lets you control what happens when the code panics.
407424

408425
If not specified, the default depends on the target.
409426

427+
If any crate in the crate graph uses `abort`, the final binary (`bin`, `dylib`, `cdylib`, `staticlib`) must also use `abort`.
428+
If `std` is used as a `dylib` with `unwind`, the final binary must also use `unwind`.
429+
410430
## passes
411431

412432
This flag can be used to add extra [LLVM
@@ -416,6 +436,12 @@ The list must be separated by spaces.
416436

417437
See also the [`no-prepopulate-passes`](#no-prepopulate-passes) flag.
418438

439+
<div class="warning">
440+
441+
Because this flag directly talks to LLVM, it not subject to the usual stability guarantees of rustc's CLI interface.
442+
443+
</div>
444+
419445
## prefer-dynamic
420446

421447
By default, `rustc` prefers to statically link dependencies. This option will
@@ -523,12 +549,30 @@ The list of passes should be separated by spaces.
523549

524550
## rpath
525551

526-
This flag controls whether [`rpath`](https://en.wikipedia.org/wiki/Rpath) is
527-
enabled. It takes one of the following values:
552+
This flag controls whether rustc sets an [`rpath`](https://en.wikipedia.org/wiki/Rpath) for the binary.
553+
It takes one of the following values:
528554

529555
* `y`, `yes`, `on`, `true` or no value: enable rpath.
530556
* `n`, `no`, `off` or `false`: disable rpath (the default).
531557

558+
This flag only does something on Unix-like platforms (Mach-O and ELF), it is ignored on other platforms.
559+
560+
If enabled, rustc will add output-relative (using `@load_path` on Mach-O and `$ORIGIN` on ELF respectively) rpaths to all `dylib` dependencies.
561+
562+
For example, for the following directory structure, with `libdep.so` being a `dylib` crate compiled with `-Cprefer-dynamic`:
563+
564+
```text
565+
dep
566+
|- libdep.so
567+
a.rs
568+
```
569+
570+
`rustc a.rs --extern dep=dep/libdep.so -Crpath` will, on x86-64 Linux, result in approximately the following `DT_RUNPATH`: `$ORIGIN/dep:$ORIGIN/$RELATIVE_PATH_TO_SYSROOT/lib/rustlib/x86_64-unknown-linux-gnu/lib` (where `RELATIVE_PATH_TO_SYSROOT` depends on the build directory location).
571+
572+
This is primarily useful for local development, to ensure that all the `dylib` dependencies can be found appropriately.
573+
574+
To set the rpath to a different value (which can be useful for distribution), `-Clink-arg` with a platform-specific linker argument can be used to set the rpath directly.
575+
532576
## save-temps
533577

534578
This flag controls whether temporary files generated during compilation are
@@ -545,6 +589,8 @@ point instructions in software. It takes one of the following values:
545589
* `y`, `yes`, `on`, `true` or no value: use soft floats.
546590
* `n`, `no`, `off` or `false`: use hardware floats (the default).
547591

592+
This flag only works on `*eabihf` targets and **is unsound and deprecated**.
593+
548594
## split-debuginfo
549595

550596
This option controls the emission of "split debuginfo" for debug information

0 commit comments

Comments
 (0)