|
| 1 | +use crate::core::compiler::{Context, Unit}; |
| 2 | +use crate::core::interning::InternedString; |
| 3 | +use crate::core::profiles; |
| 4 | +use crate::util::errors::CargoResult; |
| 5 | +use std::collections::hash_map::{Entry, HashMap}; |
| 6 | + |
| 7 | +/// Possible ways to run rustc and request various parts of LTO. |
| 8 | +#[derive(Copy, Clone, PartialEq, Eq, Hash)] |
| 9 | +pub enum Lto { |
| 10 | + /// LTO is run for this rustc, and it's `-Clto=foo` where `foo` is optional. |
| 11 | + Run(Option<InternedString>), |
| 12 | + |
| 13 | + /// This rustc invocation only needs to produce bitcode, there's no need to |
| 14 | + /// produce object files, so we can pass `-Clinker-plugin-lto` |
| 15 | + OnlyBitcode, |
| 16 | + |
| 17 | + /// This rustc invocation needs to embed bitcode in object files. This means |
| 18 | + /// that object files may be used for a normal link, and the crate may be |
| 19 | + /// loaded for LTO later, so both are required. |
| 20 | + EmbedBitcode, |
| 21 | + |
| 22 | + /// Nothing related to LTO is required of this compilation. |
| 23 | + None, |
| 24 | +} |
| 25 | + |
| 26 | +pub fn generate(cx: &mut Context<'_, '_>) -> CargoResult<()> { |
| 27 | + let mut map = HashMap::new(); |
| 28 | + for unit in cx.bcx.roots.iter() { |
| 29 | + calculate(cx, &mut map, unit, false)?; |
| 30 | + } |
| 31 | + cx.lto = map; |
| 32 | + Ok(()) |
| 33 | +} |
| 34 | + |
| 35 | +fn calculate( |
| 36 | + cx: &Context<'_, '_>, |
| 37 | + map: &mut HashMap<Unit, Lto>, |
| 38 | + unit: &Unit, |
| 39 | + require_bitcode: bool, |
| 40 | +) -> CargoResult<()> { |
| 41 | + let (lto, require_bitcode_for_deps) = if unit.target.for_host() { |
| 42 | + // Disable LTO for host builds since we only really want to perform LTO |
| 43 | + // for the final binary, and LTO on plugins/build scripts/proc macros is |
| 44 | + // largely not desired. |
| 45 | + (Lto::None, false) |
| 46 | + } else if unit.target.can_lto() { |
| 47 | + // Otherwise if this target can perform LTO then we're going to read the |
| 48 | + // LTO value out of the profile. |
| 49 | + assert!(!require_bitcode); // can't depend on binaries/staticlib/etc |
| 50 | + match unit.profile.lto { |
| 51 | + profiles::Lto::Named(s) => match s.as_str() { |
| 52 | + "n" | "no" | "off" => (Lto::Run(Some(s)), false), |
| 53 | + _ => (Lto::Run(Some(s)), true), |
| 54 | + }, |
| 55 | + profiles::Lto::Bool(true) => (Lto::Run(None), true), |
| 56 | + profiles::Lto::Bool(false) => (Lto::None, false), |
| 57 | + } |
| 58 | + } else if require_bitcode { |
| 59 | + // Otherwise we're a dependency of something, an rlib. This means that |
| 60 | + // if our parent required bitcode of some kind then we need to generate |
| 61 | + // bitcode. |
| 62 | + (Lto::OnlyBitcode, true) |
| 63 | + } else { |
| 64 | + (Lto::None, false) |
| 65 | + }; |
| 66 | + |
| 67 | + match map.entry(unit.clone()) { |
| 68 | + // If we haven't seen this unit before then insert our value and keep |
| 69 | + // going. |
| 70 | + Entry::Vacant(v) => { |
| 71 | + v.insert(lto); |
| 72 | + } |
| 73 | + |
| 74 | + Entry::Occupied(mut v) => { |
| 75 | + let result = match (lto, v.get()) { |
| 76 | + // Targets which execute LTO cannot be depended on, so these |
| 77 | + // units should only show up once in the dependency graph, so we |
| 78 | + // should never hit this case. |
| 79 | + (Lto::Run(_), _) | (_, Lto::Run(_)) => { |
| 80 | + unreachable!("lto-able targets shouldn't show up twice") |
| 81 | + } |
| 82 | + |
| 83 | + // If we calculated the same thing as before then we can bail |
| 84 | + // out quickly. |
| 85 | + (Lto::OnlyBitcode, Lto::OnlyBitcode) | (Lto::None, Lto::None) => return Ok(()), |
| 86 | + |
| 87 | + // This is where the trickiness happens. This unit needs |
| 88 | + // bitcode and the previously calculated value for this unit |
| 89 | + // says it didn't need bitcode (or vice versa). This means that |
| 90 | + // we're a shared dependency between some targets which require |
| 91 | + // LTO and some which don't. This means that instead of being |
| 92 | + // either only-objects or only-bitcode we have to embed both in |
| 93 | + // rlibs (used for different compilations), so we switch to |
| 94 | + // embedding bitcode. |
| 95 | + (Lto::OnlyBitcode, Lto::None) |
| 96 | + | (Lto::OnlyBitcode, Lto::EmbedBitcode) |
| 97 | + | (Lto::None, Lto::OnlyBitcode) |
| 98 | + | (Lto::None, Lto::EmbedBitcode) => Lto::EmbedBitcode, |
| 99 | + |
| 100 | + // Currently this variant is never calculated above, so no need |
| 101 | + // to handle this case. |
| 102 | + (Lto::EmbedBitcode, _) => unreachable!(), |
| 103 | + }; |
| 104 | + // No need to recurse if we calculated the same value as before. |
| 105 | + if result == *v.get() { |
| 106 | + return Ok(()); |
| 107 | + } |
| 108 | + v.insert(result); |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + for dep in cx.unit_deps(unit) { |
| 113 | + calculate(cx, map, &dep.unit, require_bitcode_for_deps)?; |
| 114 | + } |
| 115 | + Ok(()) |
| 116 | +} |
0 commit comments