Skip to content

Commit

Permalink
rustc: Enable LTO and multiple codegen units
Browse files Browse the repository at this point in the history
This commit is a refactoring of the LTO backend in Rust to support compilations
with multiple codegen units. The immediate result of this PR is to remove the
artificial error emitted by rustc about `-C lto -C codegen-units-8`, but longer
term this is intended to lay the groundwork for LTO with incremental compilation
and ultimately be the underpinning of ThinLTO support.

The problem here that needed solving is that when rustc is producing multiple
codegen units in one compilation LTO needs to merge them all together.
Previously only upstream dependencies were merged and it was inherently relied
on that there was only one local codegen unit. Supporting this involved
refactoring the optimization backend architecture for rustc, namely splitting
the `optimize_and_codegen` function into `optimize` and `codegen`. After an LLVM
module has been optimized it may be blocked and queued up for LTO, and only
after LTO are modules code generated.

Non-LTO compilations should look the same as they do today backend-wise, we'll
spin up a thread for each codegen unit and optimize/codegen in that thread. LTO
compilations will, however, send the LLVM module back to the coordinator thread
once optimizations have finished. When all LLVM modules have finished optimizing
the coordinator will invoke the LTO backend, producing a further list of LLVM
modules. Currently this is always a list of one LLVM module. The coordinator
then spawns further work to run LTO and code generation passes over each module.

In the course of this refactoring a number of other pieces were refactored:

* Management of the bytecode encoding in rlibs was centralized into one module
  instead of being scattered across LTO and linking.
* Some internal refactorings on the link stage of the compiler was done to work
  directly from `CompiledModule` structures instead of lists of paths.
* The trans time-graph output was tweaked a little to include a name on each
  bar and inflate the size of the bars a little
  • Loading branch information
alexcrichton committed Sep 30, 2017
1 parent d514263 commit ded38db
Show file tree
Hide file tree
Showing 13 changed files with 803 additions and 430 deletions.
18 changes: 0 additions & 18 deletions src/librustc/session/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1552,24 +1552,6 @@ pub fn build_session_options_and_crate_config(matches: &getopts::Matches)
early_error(error_format, "Value for codegen units must be a positive nonzero integer");
}

// It's possible that we have `codegen_units > 1` but only one item in
// `trans.modules`. We could theoretically proceed and do LTO in that
// case, but it would be confusing to have the validity of
// `-Z lto -C codegen-units=2` depend on details of the crate being
// compiled, so we complain regardless.
if cg.lto {
if let Some(n) = codegen_units {
if n > 1 {
// This case is impossible to handle because LTO expects to be able
// to combine the entire crate and all its dependencies into a
// single compilation unit, but each codegen unit is in a separate
// LLVM context, so they can't easily be combined.
early_error(error_format, "can't perform LTO when using multiple codegen units");
}
}
codegen_units = Some(1);
}

if cg.lto && debugging_opts.incremental.is_some() {
early_error(error_format, "can't perform LTO when compiling incrementally");
}
Expand Down
7 changes: 7 additions & 0 deletions src/librustc_llvm/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -478,6 +478,7 @@ pub mod debuginfo {
}
}

pub enum ModuleBuffer {}

// Link to our native llvm bindings (things that we need to use the C++ api
// for) and because llvm is written in C++ we need to link against libstdc++
Expand Down Expand Up @@ -1609,6 +1610,7 @@ extern "C" {
pub fn LLVMRustSetNormalizedTarget(M: ModuleRef, triple: *const c_char);
pub fn LLVMRustAddAlwaysInlinePass(P: PassManagerBuilderRef, AddLifetimes: bool);
pub fn LLVMRustLinkInExternalBitcode(M: ModuleRef, bc: *const c_char, len: size_t) -> bool;
pub fn LLVMRustLinkInParsedExternalBitcode(M: ModuleRef, M: ModuleRef) -> bool;
pub fn LLVMRustRunRestrictionPass(M: ModuleRef, syms: *const *const c_char, len: size_t);
pub fn LLVMRustMarkAllFunctionsNounwind(M: ModuleRef);

Expand Down Expand Up @@ -1678,4 +1680,9 @@ extern "C" {
pub fn LLVMRustSetComdat(M: ModuleRef, V: ValueRef, Name: *const c_char);
pub fn LLVMRustUnsetComdat(V: ValueRef);
pub fn LLVMRustSetModulePIELevel(M: ModuleRef);
pub fn LLVMRustModuleBufferCreate(M: ModuleRef) -> *mut ModuleBuffer;
pub fn LLVMRustModuleBufferPtr(p: *const ModuleBuffer) -> *const u8;
pub fn LLVMRustModuleBufferLen(p: *const ModuleBuffer) -> usize;
pub fn LLVMRustModuleBufferFree(p: *mut ModuleBuffer);
pub fn LLVMRustModuleCost(M: ModuleRef) -> u64;
}
8 changes: 3 additions & 5 deletions src/librustc_trans/back/archive.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ use std::path::{Path, PathBuf};
use std::ptr;
use std::str;

use back::bytecode::RLIB_BYTECODE_EXTENSION;
use libc;
use llvm::archive_ro::{ArchiveRO, Child};
use llvm::{self, ArchiveKind};
Expand Down Expand Up @@ -154,12 +155,9 @@ impl<'a> ArchiveBuilder<'a> {
// might be also an extra name suffix
let obj_start = format!("{}", name);

// Ignoring all bytecode files, no matter of
// name
let bc_ext = ".bytecode.deflate";

self.add_archive(rlib, move |fname: &str| {
if fname.ends_with(bc_ext) || fname == METADATA_FILENAME {
// Ignore bytecode/metadata files, no matter the name.
if fname.ends_with(RLIB_BYTECODE_EXTENSION) || fname == METADATA_FILENAME {
return true
}

Expand Down
160 changes: 160 additions & 0 deletions src/librustc_trans/back/bytecode.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// http://rust-lang.org/COPYRIGHT.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Management of the encoding of LLVM bytecode into rlibs
//!
//! This module contains the management of encoding LLVM bytecode into rlibs,
//! primarily for the usage in LTO situations. Currently the compiler will
//! unconditionally encode LLVM-IR into rlibs regardless of what's happening
//! elsewhere, so we currently compress the bytecode via deflate to avoid taking
//! up too much space on disk.
//!
//! After compressing the bytecode we then have the rest of the format to
//! basically deal with various bugs in various archive implementations. The
//! format currently is:
//!
//! RLIB LLVM-BYTECODE OBJECT LAYOUT
//! Version 2
//! Bytes Data
//! 0..10 "RUST_OBJECT" encoded in ASCII
//! 11..14 format version as little-endian u32
//! 15..19 the length of the module identifier string
//! 20..n the module identifier string
//! n..n+8 size in bytes of deflate compressed LLVM bitcode as
//! little-endian u64
//! n+9.. compressed LLVM bitcode
//! ? maybe a byte to make this whole thing even length

use std::io::{Read, Write};
use std::ptr;
use std::str;

use flate2::Compression;
use flate2::read::DeflateDecoder;
use flate2::write::DeflateEncoder;

// This is the "magic number" expected at the beginning of a LLVM bytecode
// object in an rlib.
pub const RLIB_BYTECODE_OBJECT_MAGIC: &'static [u8] = b"RUST_OBJECT";

// The version number this compiler will write to bytecode objects in rlibs
pub const RLIB_BYTECODE_OBJECT_VERSION: u8 = 2;

pub const RLIB_BYTECODE_EXTENSION: &str = "bytecode.encoded";

pub fn encode(identifier: &str, bytecode: &[u8]) -> Vec<u8> {
let mut encoded = Vec::new();

// Start off with the magic string
encoded.extend_from_slice(RLIB_BYTECODE_OBJECT_MAGIC);

// Next up is the version
encoded.extend_from_slice(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]);

// Next is the LLVM module identifier length + contents
let identifier_len = identifier.len();
encoded.extend_from_slice(&[
(identifier_len >> 0) as u8,
(identifier_len >> 8) as u8,
(identifier_len >> 16) as u8,
(identifier_len >> 24) as u8,
]);
encoded.extend_from_slice(identifier.as_bytes());

// Next is the LLVM module deflate compressed, prefixed with its length. We
// don't know its length yet, so fill in 0s
let deflated_size_pos = encoded.len();
encoded.extend_from_slice(&[0, 0, 0, 0, 0, 0, 0, 0]);

let before = encoded.len();
DeflateEncoder::new(&mut encoded, Compression::Fast)
.write_all(bytecode)
.unwrap();
let after = encoded.len();

// Fill in the length we reserved space for before
let bytecode_len = (after - before) as u64;
encoded[deflated_size_pos + 0] = (bytecode_len >> 0) as u8;
encoded[deflated_size_pos + 1] = (bytecode_len >> 8) as u8;
encoded[deflated_size_pos + 2] = (bytecode_len >> 16) as u8;
encoded[deflated_size_pos + 3] = (bytecode_len >> 24) as u8;
encoded[deflated_size_pos + 4] = (bytecode_len >> 32) as u8;
encoded[deflated_size_pos + 5] = (bytecode_len >> 40) as u8;
encoded[deflated_size_pos + 6] = (bytecode_len >> 48) as u8;
encoded[deflated_size_pos + 7] = (bytecode_len >> 56) as u8;

// If the number of bytes written to the object so far is odd, add a
// padding byte to make it even. This works around a crash bug in LLDB
// (see issue #15950)
if encoded.len() % 2 == 1 {
encoded.push(0);
}

return encoded
}

pub struct DecodedBytecode<'a> {
identifier: &'a str,
encoded_bytecode: &'a [u8],
}

impl<'a> DecodedBytecode<'a> {
pub fn new(data: &'a [u8]) -> Result<DecodedBytecode<'a>, String> {
if !data.starts_with(RLIB_BYTECODE_OBJECT_MAGIC) {
return Err(format!("magic bytecode prefix not found"))
}
let data = &data[RLIB_BYTECODE_OBJECT_MAGIC.len()..];
if !data.starts_with(&[RLIB_BYTECODE_OBJECT_VERSION, 0, 0, 0]) {
return Err(format!("wrong version prefix found in bytecode"))
}
let data = &data[4..];
if data.len() < 4 {
return Err(format!("bytecode corrupted"))
}
let identifier_len = unsafe {
u32::from_le(ptr::read_unaligned(data.as_ptr() as *const u32)) as usize
};
let data = &data[4..];
if data.len() < identifier_len {
return Err(format!("bytecode corrupted"))
}
let identifier = match str::from_utf8(&data[..identifier_len]) {
Ok(s) => s,
Err(_) => return Err(format!("bytecode corrupted"))
};
let data = &data[identifier_len..];
if data.len() < 8 {
return Err(format!("bytecode corrupted"))
}
let bytecode_len = unsafe {
u64::from_le(ptr::read_unaligned(data.as_ptr() as *const u64)) as usize
};
let data = &data[8..];
if data.len() < bytecode_len {
return Err(format!("bytecode corrupted"))
}
let encoded_bytecode = &data[..bytecode_len];

Ok(DecodedBytecode {
identifier,
encoded_bytecode,
})
}

pub fn bytecode(&self) -> Vec<u8> {
let mut data = Vec::new();
DeflateDecoder::new(self.encoded_bytecode).read_to_end(&mut data).unwrap();
return data
}

pub fn identifier(&self) -> &'a str {
self.identifier
}
}
Loading

0 comments on commit ded38db

Please sign in to comment.