|
| 1 | +# Enabling Link Time Optimization (LTO) with MMTk |
| 2 | + |
| 3 | +MMTk's API is designed with an assumption that LTO will be enabled for a performant build. |
| 4 | +It is essential to allow the Rust compiler to optimize across the crate boundary between the binding crate and mmtk-core. |
| 5 | +LTO allows inlining for both directions (from mmtk-core to the binding, and from the binding to mmtk-core), |
| 6 | +and allows further optimization such as specializing and constant folding for the `VMBinding` trait. |
| 7 | + |
| 8 | +We suggest enabling LTO for the release build in the binding's manifest (`Cargo.toml`) by adding a profile for the release build, |
| 9 | +so LTO is always enabled for a release build. |
| 10 | + |
| 11 | +```toml |
| 12 | +[profile.release] |
| 13 | +lto = true |
| 14 | +``` |
| 15 | + |
| 16 | +If your binding project is a Rust binary (e.g. the VM is written in Rust), this should be enough. However, if your binding project |
| 17 | +is a library, there are some limitations with cargo that you should be aware of. |
| 18 | + |
| 19 | + |
| 20 | +## Binding as a library |
| 21 | + |
| 22 | +Cargo only allows LTO for certain crate types. You will need to specify the crate type properly, otherwise cargo may skip LTO without |
| 23 | +any warning or error. |
| 24 | + |
| 25 | +```toml |
| 26 | +[lib] |
| 27 | +... |
| 28 | +# be careful - LTO is only allowed for certain crate types |
| 29 | +crate-type = ["cdylib"] |
| 30 | +``` |
| 31 | + |
| 32 | +At the time of writing, cargo has some limitations about LTO with different crate types: |
| 33 | +1. LTO is only allowed with `cdylib` and `staticlib` (other than `bin`). |
| 34 | +Check the code of [`can_lto`](https://github.com/rust-lang/cargo/blob/5f40a97e5c85affecfbc4fde67fc06bf188c07db/src/cargo/core/compiler/crate_type.rs#L33) |
| 35 | +for your Rust version to clarify. |
| 36 | +2. If the `crate-type` field includes any type that LTO is not allowed, LTO will be skipped for all the libraries generated (https://github.com/rust-lang/rust/issues/51009). |
| 37 | +For example, if you have `crate-type = ["cdylib", "rlib"]` and cargo cannot do LTO for `rlib`, LTO will be skipped for `cdylib` as well. |
| 38 | +So only keep the crate type that you actually need in the `crate-type` field. |
0 commit comments