33

Executing rustc -C help shows (among other things):

-C opt-level=val       -- optimize with possible levels 0-3, s, or z

The levels 0 to 3 are fairly intuitive, I think: the higher the level, the more aggressive optimizations will be performed. However, I have no clue what the s and z options are doing and I couldn't find Rust-related information about them.

Lukas Kalbertodt
  • 79,749
  • 26
  • 255
  • 305
  • and see: https://clang.llvm.org/docs/CommandGuide/clang.html -Os Like -O2 with extra optimizations to reduce code size. -Oz Like -Os (and thus -O2), but reduces code size further. – wasmup Aug 10 '17 at 08:48

3 Answers3

21

It seems like you are not the only one confused, as described in a Rust issue. It seems to follow the same pattern as Clang:

  • Os For optimising the size when compiling.
  • Oz For even more size optimisation.
Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
Englund
  • 1,067
  • 7
  • 13
  • I'm guessing "z" means strive for getting as close to "zero" size as possible? – user541686 Aug 10 '17 at 11:36
  • 4
    @Mehrdad I suppose it's more liks `s` is for Size, while `z` is for siZe. – Ruslan Aug 10 '17 at 14:28
  • 1
    Could you describe in more detail how `s` and `z` work compared to 0--3? Are they completely different optimization profiles? Or are they "like 2, plus a few additional optimization passes" or something like that? – Lukas Kalbertodt Aug 10 '17 at 15:47
15

Looking at these and these lines in Rust's source code, I can say that s means optimize for size, and z means optimize for size some more.

All optimizations seem to be performed by the LLVM code-generation engine.

Shepmaster
  • 388,571
  • 95
  • 1,107
  • 1,366
red75prime
  • 3,733
  • 1
  • 16
  • 22
  • 2
    Could you describe in more detail how `s` and `z` work compared to 0--3? Are they completely different optimization profiles? Or are they "like 2, plus a few additional optimization passes" or something like that? – Lukas Kalbertodt Aug 11 '17 at 09:41
  • 1
    @LukasKalbertodt since those optimizations are performed by LLVM and are not rust-specific you should consult the LLVM documentation. – the8472 Dec 18 '17 at 09:43
3

These two sequences, Os and Oz, within LLVM, are pretty similar. Oz invokes 260 passes (I am using LLVM 12.0), whereas Os invokes 264. Oz' sequence of analyses and optimizations is almost a strict subsequence of Os', except for one pass (opt -loops), which appears in a different place within Os. This said, notice that the effects of the optimizations can still be different, because they use different cost models, e.g., constants that determine the behavior of optimizations. Thus, optimizations that have impact on size, like loop unrolling and inlining can behave differently in these two sequences.

  • Nice information, thanks! Could you still link to some document listing those passes? (Could be code or docs). – Lukas Kalbertodt Apr 06 '21 at 15:11
  • Hi @LukasKalbertodt, you can check the passes invoked by an optimization level, using something like: `llvm-as < /dev/null | opt -O3 -disable-output -debug-pass=Arguments` We have a paper where we analyze the effectiveness of the different size-reduction levels [here](https://homepages.dcc.ufmg.br/~fernando/publications/papers/FaustinoCC21.pdf). And there is a technical report that accompanies the paper, with a few new code-size reduction sequences [here](http://lac.dcc.ufmg.br/pubs/TechReports/LaC_TechReport022020.pdf). – Fernando Magno Quintao Pereira Apr 07 '21 at 17:06