]> git.lizzy.rs Git - rust.git/blob - src/librustc_trans/back/lto.rs
Refactor symbol export list generation.
[rust.git] / src / librustc_trans / back / lto.rs
1 // Copyright 2013 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use back::link;
12 use back::write;
13 use back::symbol_export::{self, ExportedSymbols};
14 use rustc::session::{self, config};
15 use llvm;
16 use llvm::archive_ro::ArchiveRO;
17 use llvm::{ModuleRef, TargetMachineRef, True, False};
18 use rustc::util::common::time;
19 use rustc::util::common::path2cstr;
20 use rustc::hir::def_id::LOCAL_CRATE;
21 use back::write::{ModuleConfig, with_llvm_pmb};
22
23 use libc;
24 use flate;
25
26 use std::ffi::CString;
27 use std::path::Path;
28
29 pub fn crate_type_allows_lto(crate_type: config::CrateType) -> bool {
30     match crate_type {
31         config::CrateTypeExecutable |
32         config::CrateTypeStaticlib  |
33         config::CrateTypeCdylib     => true,
34
35         config::CrateTypeDylib     |
36         config::CrateTypeRlib      |
37         config::CrateTypeMetadata  |
38         config::CrateTypeProcMacro => false,
39     }
40 }
41
42 pub fn run(sess: &session::Session,
43            llmod: ModuleRef,
44            tm: TargetMachineRef,
45            exported_symbols: &ExportedSymbols,
46            config: &ModuleConfig,
47            temp_no_opt_bc_filename: &Path) {
48     if sess.opts.cg.prefer_dynamic {
49         sess.struct_err("cannot prefer dynamic linking when performing LTO")
50             .note("only 'staticlib', 'bin', and 'cdylib' outputs are \
51                    supported with LTO")
52             .emit();
53         sess.abort_if_errors();
54     }
55
56     // Make sure we actually can run LTO
57     for crate_type in sess.crate_types.borrow().iter() {
58         if !crate_type_allows_lto(*crate_type) {
59             sess.fatal("lto can only be run for executables and \
60                             static library outputs");
61         }
62     }
63
64     let export_threshold =
65         symbol_export::crates_export_threshold(&sess.crate_types.borrow()[..]);
66
67     let symbol_filter = &|&(ref name, level): &(String, _)| {
68         if symbol_export::is_below_threshold(level, export_threshold) {
69             let mut bytes = Vec::with_capacity(name.len() + 1);
70             bytes.extend(name.bytes());
71             Some(CString::new(bytes).unwrap())
72         } else {
73             None
74         }
75     };
76
77     let mut symbol_white_list: Vec<CString> = exported_symbols
78         .exported_symbols(LOCAL_CRATE)
79         .iter()
80         .filter_map(symbol_filter)
81         .collect();
82
83     // For each of our upstream dependencies, find the corresponding rlib and
84     // load the bitcode from the archive. Then merge it into the current LLVM
85     // module that we've got.
86     link::each_linked_rlib(sess, &mut |cnum, path| {
87         // `#![no_builtins]` crates don't participate in LTO.
88         if sess.cstore.is_no_builtins(cnum) {
89             return;
90         }
91
92         symbol_white_list.extend(
93             exported_symbols.exported_symbols(cnum)
94                             .iter()
95                             .filter_map(symbol_filter));
96
97         let archive = ArchiveRO::open(&path).expect("wanted an rlib");
98         let bytecodes = archive.iter().filter_map(|child| {
99             child.ok().and_then(|c| c.name().map(|name| (name, c)))
100         }).filter(|&(name, _)| name.ends_with("bytecode.deflate"));
101         for (name, data) in bytecodes {
102             let bc_encoded = data.data();
103
104             let bc_decoded = if is_versioned_bytecode_format(bc_encoded) {
105                 time(sess.time_passes(), &format!("decode {}", name), || {
106                     // Read the version
107                     let version = extract_bytecode_format_version(bc_encoded);
108
109                     if version == 1 {
110                         // The only version existing so far
111                         let data_size = extract_compressed_bytecode_size_v1(bc_encoded);
112                         let compressed_data = &bc_encoded[
113                             link::RLIB_BYTECODE_OBJECT_V1_DATA_OFFSET..
114                             (link::RLIB_BYTECODE_OBJECT_V1_DATA_OFFSET + data_size as usize)];
115
116                         match flate::inflate_bytes(compressed_data) {
117                             Ok(inflated) => inflated,
118                             Err(_) => {
119                                 sess.fatal(&format!("failed to decompress bc of `{}`",
120                                                    name))
121                             }
122                         }
123                     } else {
124                         sess.fatal(&format!("Unsupported bytecode format version {}",
125                                            version))
126                     }
127                 })
128             } else {
129                 time(sess.time_passes(), &format!("decode {}", name), || {
130                     // the object must be in the old, pre-versioning format, so
131                     // simply inflate everything and let LLVM decide if it can
132                     // make sense of it
133                     match flate::inflate_bytes(bc_encoded) {
134                         Ok(bc) => bc,
135                         Err(_) => {
136                             sess.fatal(&format!("failed to decompress bc of `{}`",
137                                                name))
138                         }
139                     }
140                 })
141             };
142
143             let ptr = bc_decoded.as_ptr();
144             debug!("linking {}", name);
145             time(sess.time_passes(), &format!("ll link {}", name), || unsafe {
146                 if !llvm::LLVMRustLinkInExternalBitcode(llmod,
147                                                         ptr as *const libc::c_char,
148                                                         bc_decoded.len() as libc::size_t) {
149                     write::llvm_err(sess.diagnostic(),
150                                     format!("failed to load bc of `{}`",
151                                             &name[..]));
152                 }
153             });
154         }
155     });
156
157     // Internalize everything but the exported symbols of the current module
158     let arr: Vec<*const libc::c_char> = symbol_white_list.iter()
159                                                          .map(|c| c.as_ptr())
160                                                          .collect();
161     let ptr = arr.as_ptr();
162     unsafe {
163         llvm::LLVMRustRunRestrictionPass(llmod,
164                                          ptr as *const *const libc::c_char,
165                                          arr.len() as libc::size_t);
166     }
167
168     if sess.no_landing_pads() {
169         unsafe {
170             llvm::LLVMRustMarkAllFunctionsNounwind(llmod);
171         }
172     }
173
174     if sess.opts.cg.save_temps {
175         let cstr = path2cstr(temp_no_opt_bc_filename);
176         unsafe {
177             llvm::LLVMWriteBitcodeToFile(llmod, cstr.as_ptr());
178         }
179     }
180
181     // Now we have one massive module inside of llmod. Time to run the
182     // LTO-specific optimization passes that LLVM provides.
183     //
184     // This code is based off the code found in llvm's LTO code generator:
185     //      tools/lto/LTOCodeGenerator.cpp
186     debug!("running the pass manager");
187     unsafe {
188         let pm = llvm::LLVMCreatePassManager();
189         llvm::LLVMRustAddAnalysisPasses(tm, pm, llmod);
190         let pass = llvm::LLVMRustFindAndCreatePass("verify\0".as_ptr() as *const _);
191         assert!(!pass.is_null());
192         llvm::LLVMRustAddPass(pm, pass);
193
194         with_llvm_pmb(llmod, config, &mut |b| {
195             llvm::LLVMPassManagerBuilderPopulateLTOPassManager(b, pm,
196                 /* Internalize = */ False,
197                 /* RunInliner = */ True);
198         });
199
200         let pass = llvm::LLVMRustFindAndCreatePass("verify\0".as_ptr() as *const _);
201         assert!(!pass.is_null());
202         llvm::LLVMRustAddPass(pm, pass);
203
204         time(sess.time_passes(), "LTO passes", ||
205              llvm::LLVMRunPassManager(pm, llmod));
206
207         llvm::LLVMDisposePassManager(pm);
208     }
209     debug!("lto done");
210 }
211
212 fn is_versioned_bytecode_format(bc: &[u8]) -> bool {
213     let magic_id_byte_count = link::RLIB_BYTECODE_OBJECT_MAGIC.len();
214     return bc.len() > magic_id_byte_count &&
215            &bc[..magic_id_byte_count] == link::RLIB_BYTECODE_OBJECT_MAGIC;
216 }
217
218 fn extract_bytecode_format_version(bc: &[u8]) -> u32 {
219     let pos = link::RLIB_BYTECODE_OBJECT_VERSION_OFFSET;
220     let byte_data = &bc[pos..pos + 4];
221     let data = unsafe { *(byte_data.as_ptr() as *const u32) };
222     u32::from_le(data)
223 }
224
225 fn extract_compressed_bytecode_size_v1(bc: &[u8]) -> u64 {
226     let pos = link::RLIB_BYTECODE_OBJECT_V1_DATASIZE_OFFSET;
227     let byte_data = &bc[pos..pos + 8];
228     let data = unsafe { *(byte_data.as_ptr() as *const u64) };
229     u64::from_le(data)
230 }