2015-02-28 23:53:12 +02:00
|
|
|
|
// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
|
2012-12-03 16:48:01 -08:00
|
|
|
|
// 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.
|
2016-02-15 15:41:16 -05:00
|
|
|
|
|
2015-02-28 23:53:12 +02:00
|
|
|
|
//! Translate the completed AST to the LLVM IR.
|
|
|
|
|
//!
|
|
|
|
|
//! Some functions here, such as trans_block and trans_expr, return a value --
|
2016-02-23 21:39:35 +02:00
|
|
|
|
//! the result of the translation to LLVM -- while others, such as trans_fn
|
|
|
|
|
//! and trans_item, are called only for the side effect of adding a
|
2015-02-28 23:53:12 +02:00
|
|
|
|
//! particular definition to the LLVM IR output we're producing.
|
|
|
|
|
//!
|
|
|
|
|
//! Hopefully useful general knowledge about trans:
|
|
|
|
|
//!
|
|
|
|
|
//! * There's no way to find out the Ty type of a ValueRef. Doing so
|
|
|
|
|
//! would be "trying to get the eggs out of an omelette" (credit:
|
|
|
|
|
//! pcwalton). You can, instead, find out its TypeRef by calling val_ty,
|
|
|
|
|
//! but one TypeRef corresponds to many `Ty`s; for instance, tup(int, int,
|
|
|
|
|
//! int) and rec(x=int, y=int, z=int) will have the same TypeRef.
|
2011-12-13 16:25:51 -08:00
|
|
|
|
|
2014-11-27 07:21:26 -05:00
|
|
|
|
use super::CrateTranslation;
|
2016-07-21 12:49:59 -04:00
|
|
|
|
use super::ModuleLlvm;
|
|
|
|
|
use super::ModuleSource;
|
2014-11-27 07:21:26 -05:00
|
|
|
|
use super::ModuleTranslation;
|
|
|
|
|
|
2016-07-21 12:50:15 -04:00
|
|
|
|
use assert_module_sources;
|
2016-05-12 19:52:38 +03:00
|
|
|
|
use back::link;
|
2016-05-25 01:45:25 +03:00
|
|
|
|
use back::linker::LinkerInfo;
|
2016-11-30 10:03:42 -05:00
|
|
|
|
use back::symbol_export::{self, ExportedSymbols};
|
2016-08-16 17:41:38 +03:00
|
|
|
|
use llvm::{Linkage, ValueRef, Vector, get_param};
|
2014-07-07 17:58:01 -07:00
|
|
|
|
use llvm;
|
2016-11-30 10:03:42 -05:00
|
|
|
|
use rustc::hir::def_id::{DefId, LOCAL_CRATE};
|
2016-12-15 18:00:19 -07:00
|
|
|
|
use middle::lang_items::StartFnLangItem;
|
2016-06-15 01:40:09 +03:00
|
|
|
|
use rustc::ty::subst::Substs;
|
2016-03-22 17:30:57 +02:00
|
|
|
|
use rustc::traits;
|
|
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
|
|
|
|
use rustc::ty::adjustment::CustomCoerceUnsized;
|
2016-07-25 10:51:14 -04:00
|
|
|
|
use rustc::dep_graph::{DepNode, WorkProduct};
|
2016-03-29 08:50:44 +03:00
|
|
|
|
use rustc::hir::map as hir_map;
|
2015-12-25 13:59:02 -05:00
|
|
|
|
use rustc::util::common::time;
|
2016-08-16 17:41:38 +03:00
|
|
|
|
use session::config::{self, NoDebugInfo};
|
2016-08-23 07:47:14 -04:00
|
|
|
|
use rustc_incremental::IncrementalHashesMap;
|
2016-11-15 17:48:07 +01:00
|
|
|
|
use session::{self, DataTypeKind, Session};
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use abi::{self, Abi, FnType};
|
|
|
|
|
use adt;
|
|
|
|
|
use attributes;
|
2016-12-16 18:48:25 -07:00
|
|
|
|
use builder::Builder;
|
2016-08-16 17:41:38 +03:00
|
|
|
|
use callee::{Callee};
|
2016-12-10 20:32:44 -07:00
|
|
|
|
use common::{BlockAndBuilder, C_bool, C_bytes_in_context, C_i32, C_uint};
|
2016-06-16 18:56:14 -04:00
|
|
|
|
use collector::{self, TransItemCollectionMode};
|
2016-12-17 15:52:29 -07:00
|
|
|
|
use common::{C_struct_in_context, C_u64, C_undef};
|
2016-08-23 10:39:30 +03:00
|
|
|
|
use common::{CrateContext, FunctionContext};
|
2016-08-16 17:41:38 +03:00
|
|
|
|
use common::{fulfill_obligation};
|
|
|
|
|
use common::{type_is_zero_size, val_ty};
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use common;
|
|
|
|
|
use consts;
|
2016-05-05 14:14:41 -04:00
|
|
|
|
use context::{SharedCrateContext, CrateContextList};
|
2016-12-16 13:25:18 -07:00
|
|
|
|
use debuginfo;
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use declare;
|
|
|
|
|
use machine;
|
2016-06-02 23:43:16 +02:00
|
|
|
|
use machine::{llalign_of_min, llsize_of};
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use meth;
|
|
|
|
|
use mir;
|
|
|
|
|
use monomorphize::{self, Instance};
|
2016-05-09 14:26:15 -04:00
|
|
|
|
use partitioning::{self, PartitioningStrategy, CodegenUnit};
|
2016-05-26 08:59:58 -04:00
|
|
|
|
use symbol_map::SymbolMap;
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use symbol_names_test;
|
2016-11-04 17:37:42 -04:00
|
|
|
|
use trans_item::{TransItem, DefPathBasedNames};
|
2016-03-22 19:23:36 +02:00
|
|
|
|
use type_::Type;
|
|
|
|
|
use type_of;
|
|
|
|
|
use value::Value;
|
|
|
|
|
use Disr;
|
2016-11-08 14:02:55 +11:00
|
|
|
|
use util::nodemap::{NodeSet, FxHashMap, FxHashSet};
|
2013-06-16 22:52:44 +12:00
|
|
|
|
|
2015-02-28 23:55:50 +02:00
|
|
|
|
use libc::c_uint;
|
2015-02-17 22:47:40 -08:00
|
|
|
|
use std::ffi::{CStr, CString};
|
2016-05-26 08:59:58 -04:00
|
|
|
|
use std::rc::Rc;
|
2014-11-25 13:28:35 -08:00
|
|
|
|
use std::str;
|
2016-08-16 17:41:38 +03:00
|
|
|
|
use std::i32;
|
2016-06-21 18:08:13 -04:00
|
|
|
|
use syntax_pos::{Span, DUMMY_SP};
|
2015-09-14 21:58:20 +12:00
|
|
|
|
use syntax::attr;
|
2016-03-29 08:50:44 +03:00
|
|
|
|
use rustc::hir;
|
2016-11-15 17:48:07 +01:00
|
|
|
|
use rustc::ty::layout::{self, Layout};
|
2015-07-31 00:04:06 -07:00
|
|
|
|
use syntax::ast;
|
2012-03-03 17:49:23 -08:00
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
|
pub struct StatRecorder<'a, 'tcx: 'a> {
|
|
|
|
|
ccx: &'a CrateContext<'a, 'tcx>,
|
2014-05-22 16:57:53 -07:00
|
|
|
|
name: Option<String>,
|
2015-03-25 17:06:52 -07:00
|
|
|
|
istart: usize,
|
2013-06-28 11:15:34 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
|
impl<'a, 'tcx> StatRecorder<'a, 'tcx> {
|
2015-11-19 12:36:31 +01:00
|
|
|
|
pub fn new(ccx: &'a CrateContext<'a, 'tcx>, name: String) -> StatRecorder<'a, 'tcx> {
|
2014-09-05 09:18:53 -07:00
|
|
|
|
let istart = ccx.stats().n_llvm_insns.get();
|
2013-06-28 11:15:34 -07:00
|
|
|
|
StatRecorder {
|
|
|
|
|
ccx: ccx,
|
2014-02-14 07:07:09 +02:00
|
|
|
|
name: Some(name),
|
2013-06-28 11:15:34 -07:00
|
|
|
|
istart: istart,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2014-04-22 15:56:37 +03:00
|
|
|
|
impl<'a, 'tcx> Drop for StatRecorder<'a, 'tcx> {
|
2013-09-16 21:18:07 -04:00
|
|
|
|
fn drop(&mut self) {
|
2014-03-05 16:36:01 +02:00
|
|
|
|
if self.ccx.sess().trans_stats() {
|
2014-09-05 09:18:53 -07:00
|
|
|
|
let iend = self.ccx.stats().n_llvm_insns.get();
|
2016-12-17 10:21:15 -07:00
|
|
|
|
self.ccx.stats().fn_stats.borrow_mut()
|
2015-11-19 12:36:31 +01:00
|
|
|
|
.push((self.name.take().unwrap(), iend - self.istart));
|
2014-09-05 09:18:53 -07:00
|
|
|
|
self.ccx.stats().n_fns.set(self.ccx.stats().n_fns.get() + 1);
|
2013-06-28 11:15:34 -07:00
|
|
|
|
// Reset LLVM insn count to avoid compound costs.
|
2014-09-05 09:18:53 -07:00
|
|
|
|
self.ccx.stats().n_llvm_insns.set(self.istart);
|
2013-06-28 11:15:34 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 15:03:52 -07:00
|
|
|
|
pub fn get_meta(bcx: &Builder, fat_ptr: ValueRef) -> ValueRef {
|
2016-12-11 08:59:20 -07:00
|
|
|
|
bcx.struct_gep(fat_ptr, abi::FAT_PTR_EXTRA)
|
2016-08-16 17:41:38 +03:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 15:03:52 -07:00
|
|
|
|
pub fn get_dataptr(bcx: &Builder, fat_ptr: ValueRef) -> ValueRef {
|
2016-12-11 08:59:20 -07:00
|
|
|
|
bcx.struct_gep(fat_ptr, abi::FAT_PTR_ADDR)
|
2014-07-29 22:08:39 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-03-29 01:46:02 +02:00
|
|
|
|
pub fn bin_op_to_icmp_predicate(op: hir::BinOp_,
|
2015-11-19 12:36:31 +01:00
|
|
|
|
signed: bool)
|
2015-01-29 14:03:34 +02:00
|
|
|
|
-> llvm::IntPredicate {
|
|
|
|
|
match op {
|
2015-07-31 00:04:06 -07:00
|
|
|
|
hir::BiEq => llvm::IntEQ,
|
|
|
|
|
hir::BiNe => llvm::IntNE,
|
|
|
|
|
hir::BiLt => if signed { llvm::IntSLT } else { llvm::IntULT },
|
|
|
|
|
hir::BiLe => if signed { llvm::IntSLE } else { llvm::IntULE },
|
|
|
|
|
hir::BiGt => if signed { llvm::IntSGT } else { llvm::IntUGT },
|
|
|
|
|
hir::BiGe => if signed { llvm::IntSGE } else { llvm::IntUGE },
|
2015-01-29 14:03:34 +02:00
|
|
|
|
op => {
|
2016-03-29 01:46:02 +02:00
|
|
|
|
bug!("comparison_op_to_icmp_predicate: expected comparison operator, \
|
|
|
|
|
found {:?}",
|
|
|
|
|
op)
|
2015-01-29 14:03:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2011-06-15 11:19:50 -07:00
|
|
|
|
|
2016-03-29 01:46:02 +02:00
|
|
|
|
pub fn bin_op_to_fcmp_predicate(op: hir::BinOp_) -> llvm::RealPredicate {
|
2015-01-29 14:03:34 +02:00
|
|
|
|
match op {
|
2015-07-31 00:04:06 -07:00
|
|
|
|
hir::BiEq => llvm::RealOEQ,
|
|
|
|
|
hir::BiNe => llvm::RealUNE,
|
|
|
|
|
hir::BiLt => llvm::RealOLT,
|
|
|
|
|
hir::BiLe => llvm::RealOLE,
|
|
|
|
|
hir::BiGt => llvm::RealOGT,
|
|
|
|
|
hir::BiGe => llvm::RealOGE,
|
2015-01-29 14:03:34 +02:00
|
|
|
|
op => {
|
2016-03-29 01:46:02 +02:00
|
|
|
|
bug!("comparison_op_to_fcmp_predicate: expected comparison operator, \
|
|
|
|
|
found {:?}",
|
|
|
|
|
op);
|
2015-01-29 14:03:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 19:54:32 -07:00
|
|
|
|
pub fn compare_simd_types<'a, 'tcx>(
|
|
|
|
|
bcx: &BlockAndBuilder<'a, 'tcx>,
|
|
|
|
|
lhs: ValueRef,
|
|
|
|
|
rhs: ValueRef,
|
|
|
|
|
t: Ty<'tcx>,
|
|
|
|
|
ret_ty: Type,
|
|
|
|
|
op: hir::BinOp_
|
|
|
|
|
) -> ValueRef {
|
2015-01-29 14:03:34 +02:00
|
|
|
|
let signed = match t.sty {
|
2015-06-11 16:21:46 -07:00
|
|
|
|
ty::TyFloat(_) => {
|
2016-03-29 01:46:02 +02:00
|
|
|
|
let cmp = bin_op_to_fcmp_predicate(op);
|
2016-12-11 08:59:20 -07:00
|
|
|
|
return bcx.sext(bcx.fcmp(cmp, lhs, rhs), ret_ty);
|
2015-01-28 20:20:55 +11:00
|
|
|
|
},
|
2015-06-11 16:21:46 -07:00
|
|
|
|
ty::TyUint(_) => false,
|
|
|
|
|
ty::TyInt(_) => true,
|
2016-03-29 01:46:02 +02:00
|
|
|
|
_ => bug!("compare_simd_types: invalid SIMD type"),
|
2015-01-28 20:20:55 +11:00
|
|
|
|
};
|
2015-01-29 14:03:34 +02:00
|
|
|
|
|
2016-03-29 01:46:02 +02:00
|
|
|
|
let cmp = bin_op_to_icmp_predicate(op, signed);
|
2015-01-28 20:20:55 +11:00
|
|
|
|
// LLVM outputs an `< size x i1 >`, so we need to perform a sign extension
|
|
|
|
|
// to get the correctly sized type. This will compile to a single instruction
|
|
|
|
|
// once the IR is converted to assembly if the SIMD instruction is supported
|
|
|
|
|
// by the target architecture.
|
2016-12-11 08:59:20 -07:00
|
|
|
|
bcx.sext(bcx.icmp(cmp, lhs, rhs), ret_ty)
|
2014-05-02 11:04:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-11 22:02:51 +02:00
|
|
|
|
/// Retrieve the information we are losing (making dynamic) in an unsizing
|
|
|
|
|
/// adjustment.
|
|
|
|
|
///
|
|
|
|
|
/// The `old_info` argument is a bit funny. It is intended for use
|
|
|
|
|
/// in an upcast, where the new vtable for an object will be drived
|
|
|
|
|
/// from the old one.
|
|
|
|
|
pub fn unsized_info<'ccx, 'tcx>(ccx: &CrateContext<'ccx, 'tcx>,
|
|
|
|
|
source: Ty<'tcx>,
|
|
|
|
|
target: Ty<'tcx>,
|
2016-03-06 17:32:47 +02:00
|
|
|
|
old_info: Option<ValueRef>)
|
2015-11-11 22:02:51 +02:00
|
|
|
|
-> ValueRef {
|
|
|
|
|
let (source, target) = ccx.tcx().struct_lockstep_tails(source, target);
|
|
|
|
|
match (&source.sty, &target.sty) {
|
|
|
|
|
(&ty::TyArray(_, len), &ty::TySlice(_)) => C_uint(ccx, len),
|
2016-11-16 09:21:49 -07:00
|
|
|
|
(&ty::TyDynamic(..), &ty::TyDynamic(..)) => {
|
2015-11-11 22:02:51 +02:00
|
|
|
|
// For now, upcasts are limited to changes in marker
|
|
|
|
|
// traits, and hence never actually require an actual
|
|
|
|
|
// change to the vtable.
|
|
|
|
|
old_info.expect("unsized_info: missing old info for trait upcast")
|
|
|
|
|
}
|
2016-11-16 09:21:49 -07:00
|
|
|
|
(_, &ty::TyDynamic(ref data, ..)) => {
|
|
|
|
|
consts::ptrcast(meth::get_vtable(ccx, source, data.principal()),
|
2015-11-11 22:02:51 +02:00
|
|
|
|
Type::vtable_ptr(ccx))
|
|
|
|
|
}
|
2016-03-29 01:46:02 +02:00
|
|
|
|
_ => bug!("unsized_info: invalid unsizing {:?} -> {:?}",
|
2015-11-11 22:02:51 +02:00
|
|
|
|
source,
|
2016-03-29 01:46:02 +02:00
|
|
|
|
target),
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Coerce `src` to `dst_ty`. `src_ty` must be a thin pointer.
|
2016-12-17 19:54:32 -07:00
|
|
|
|
pub fn unsize_thin_ptr<'a, 'tcx>(
|
|
|
|
|
bcx: &BlockAndBuilder<'a, 'tcx>,
|
|
|
|
|
src: ValueRef,
|
|
|
|
|
src_ty: Ty<'tcx>,
|
|
|
|
|
dst_ty: Ty<'tcx>
|
|
|
|
|
) -> (ValueRef, ValueRef) {
|
2015-11-11 22:02:51 +02:00
|
|
|
|
debug!("unsize_thin_ptr: {:?} => {:?}", src_ty, dst_ty);
|
|
|
|
|
match (&src_ty.sty, &dst_ty.sty) {
|
|
|
|
|
(&ty::TyBox(a), &ty::TyBox(b)) |
|
|
|
|
|
(&ty::TyRef(_, ty::TypeAndMut { ty: a, .. }),
|
|
|
|
|
&ty::TyRef(_, ty::TypeAndMut { ty: b, .. })) |
|
|
|
|
|
(&ty::TyRef(_, ty::TypeAndMut { ty: a, .. }),
|
|
|
|
|
&ty::TyRawPtr(ty::TypeAndMut { ty: b, .. })) |
|
|
|
|
|
(&ty::TyRawPtr(ty::TypeAndMut { ty: a, .. }),
|
|
|
|
|
&ty::TyRawPtr(ty::TypeAndMut { ty: b, .. })) => {
|
2016-12-19 16:25:00 -07:00
|
|
|
|
assert!(bcx.ccx.shared().type_is_sized(a));
|
|
|
|
|
let ptr_ty = type_of::in_memory_type_of(bcx.ccx, b).ptr_to();
|
|
|
|
|
(bcx.pointercast(src, ptr_ty), unsized_info(bcx.ccx, a, b, None))
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
2016-03-29 01:46:02 +02:00
|
|
|
|
_ => bug!("unsize_thin_ptr: called on bad types"),
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Coerce `src`, which is a reference to a value of type `src_ty`,
|
|
|
|
|
/// to a value of type `dst_ty` and store the result in `dst`
|
2016-12-17 19:54:32 -07:00
|
|
|
|
pub fn coerce_unsized_into<'a, 'tcx>(bcx: &BlockAndBuilder<'a, 'tcx>,
|
|
|
|
|
src: ValueRef,
|
|
|
|
|
src_ty: Ty<'tcx>,
|
|
|
|
|
dst: ValueRef,
|
|
|
|
|
dst_ty: Ty<'tcx>) {
|
2015-11-11 22:02:51 +02:00
|
|
|
|
match (&src_ty.sty, &dst_ty.sty) {
|
|
|
|
|
(&ty::TyBox(..), &ty::TyBox(..)) |
|
|
|
|
|
(&ty::TyRef(..), &ty::TyRef(..)) |
|
|
|
|
|
(&ty::TyRef(..), &ty::TyRawPtr(..)) |
|
|
|
|
|
(&ty::TyRawPtr(..), &ty::TyRawPtr(..)) => {
|
2016-12-19 16:25:00 -07:00
|
|
|
|
let (base, info) = if common::type_is_fat_ptr(bcx.ccx, src_ty) {
|
2015-11-11 22:02:51 +02:00
|
|
|
|
// fat-ptr to fat-ptr unsize preserves the vtable
|
2016-05-18 22:25:03 -04:00
|
|
|
|
// i.e. &'a fmt::Debug+Send => &'a fmt::Debug
|
|
|
|
|
// So we need to pointercast the base to ensure
|
|
|
|
|
// the types match up.
|
|
|
|
|
let (base, info) = load_fat_ptr(bcx, src, src_ty);
|
2016-12-19 16:25:00 -07:00
|
|
|
|
let llcast_ty = type_of::fat_ptr_base_ty(bcx.ccx, dst_ty);
|
2016-12-11 08:59:20 -07:00
|
|
|
|
let base = bcx.pointercast(base, llcast_ty);
|
2016-05-18 22:25:03 -04:00
|
|
|
|
(base, info)
|
2015-11-11 22:02:51 +02:00
|
|
|
|
} else {
|
|
|
|
|
let base = load_ty(bcx, src, src_ty);
|
|
|
|
|
unsize_thin_ptr(bcx, base, src_ty, dst_ty)
|
|
|
|
|
};
|
|
|
|
|
store_fat_ptr(bcx, base, info, dst, dst_ty);
|
|
|
|
|
}
|
|
|
|
|
|
2016-08-28 20:44:19 -04:00
|
|
|
|
(&ty::TyAdt(def_a, substs_a), &ty::TyAdt(def_b, substs_b)) => {
|
2015-11-11 22:02:51 +02:00
|
|
|
|
assert_eq!(def_a, def_b);
|
|
|
|
|
|
2016-08-28 20:44:19 -04:00
|
|
|
|
let src_fields = def_a.variants[0].fields.iter().map(|f| {
|
|
|
|
|
monomorphize::field_ty(bcx.tcx(), substs_a, f)
|
|
|
|
|
});
|
|
|
|
|
let dst_fields = def_b.variants[0].fields.iter().map(|f| {
|
|
|
|
|
monomorphize::field_ty(bcx.tcx(), substs_b, f)
|
|
|
|
|
});
|
2015-11-11 22:02:51 +02:00
|
|
|
|
|
2015-12-07 02:38:29 +13:00
|
|
|
|
let src = adt::MaybeSizedValue::sized(src);
|
|
|
|
|
let dst = adt::MaybeSizedValue::sized(dst);
|
|
|
|
|
|
2016-08-28 20:44:19 -04:00
|
|
|
|
let iter = src_fields.zip(dst_fields).enumerate();
|
2015-11-11 22:02:51 +02:00
|
|
|
|
for (i, (src_fty, dst_fty)) in iter {
|
2016-12-19 16:25:00 -07:00
|
|
|
|
if type_is_zero_size(bcx.ccx, dst_fty) {
|
2015-11-19 12:36:31 +01:00
|
|
|
|
continue;
|
|
|
|
|
}
|
2015-11-11 22:02:51 +02:00
|
|
|
|
|
2016-08-28 20:44:19 -04:00
|
|
|
|
let src_f = adt::trans_field_ptr(bcx, src_ty, src, Disr(0), i);
|
|
|
|
|
let dst_f = adt::trans_field_ptr(bcx, dst_ty, dst, Disr(0), i);
|
2015-11-11 22:02:51 +02:00
|
|
|
|
if src_fty == dst_fty {
|
2016-12-29 02:20:26 +01:00
|
|
|
|
memcpy_ty(bcx, dst_f, src_f, src_fty, None);
|
2015-11-11 22:02:51 +02:00
|
|
|
|
} else {
|
2015-11-19 12:36:31 +01:00
|
|
|
|
coerce_unsized_into(bcx, src_f, src_fty, dst_f, dst_fty);
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-03-29 01:46:02 +02:00
|
|
|
|
_ => bug!("coerce_unsized_into: invalid coercion {:?} -> {:?}",
|
|
|
|
|
src_ty,
|
|
|
|
|
dst_ty),
|
2015-11-11 22:02:51 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 17:07:36 -04:00
|
|
|
|
pub fn custom_coerce_unsize_info<'scx, 'tcx>(scx: &SharedCrateContext<'scx, 'tcx>,
|
2015-11-02 14:46:39 +01:00
|
|
|
|
source_ty: Ty<'tcx>,
|
|
|
|
|
target_ty: Ty<'tcx>)
|
|
|
|
|
-> CustomCoerceUnsized {
|
|
|
|
|
let trait_ref = ty::Binder(ty::TraitRef {
|
2016-05-06 17:07:36 -04:00
|
|
|
|
def_id: scx.tcx().lang_items.coerce_unsized_trait().unwrap(),
|
2016-10-24 18:23:29 -06:00
|
|
|
|
substs: scx.tcx().mk_substs_trait(source_ty, &[target_ty])
|
2015-11-02 14:46:39 +01:00
|
|
|
|
});
|
|
|
|
|
|
2016-05-06 17:07:36 -04:00
|
|
|
|
match fulfill_obligation(scx, DUMMY_SP, trait_ref) {
|
2015-11-02 14:46:39 +01:00
|
|
|
|
traits::VtableImpl(traits::VtableImplData { impl_def_id, .. }) => {
|
2016-05-06 17:07:36 -04:00
|
|
|
|
scx.tcx().custom_coerce_unsized_kind(impl_def_id)
|
2015-11-02 14:46:39 +01:00
|
|
|
|
}
|
|
|
|
|
vtable => {
|
2016-03-29 01:46:02 +02:00
|
|
|
|
bug!("invalid CoerceUnsized vtable: {:?}", vtable);
|
2015-11-02 14:46:39 +01:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 08:59:20 -07:00
|
|
|
|
pub fn cast_shift_expr_rhs(
|
|
|
|
|
cx: &BlockAndBuilder, op: hir::BinOp_, lhs: ValueRef, rhs: ValueRef
|
|
|
|
|
) -> ValueRef {
|
|
|
|
|
cast_shift_rhs(op, lhs, rhs, |a, b| cx.trunc(a, b), |a, b| cx.zext(a, b))
|
2012-02-21 21:01:33 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-11-19 12:36:31 +01:00
|
|
|
|
pub fn cast_shift_const_rhs(op: hir::BinOp_, lhs: ValueRef, rhs: ValueRef) -> ValueRef {
|
|
|
|
|
cast_shift_rhs(op,
|
|
|
|
|
lhs,
|
|
|
|
|
rhs,
|
2013-06-16 22:52:44 +12:00
|
|
|
|
|a, b| unsafe { llvm::LLVMConstTrunc(a, b.to_ref()) },
|
|
|
|
|
|a, b| unsafe { llvm::LLVMConstZExt(a, b.to_ref()) })
|
2012-02-21 21:01:33 -08:00
|
|
|
|
}
|
|
|
|
|
|
2015-07-31 00:04:06 -07:00
|
|
|
|
fn cast_shift_rhs<F, G>(op: hir::BinOp_,
|
2015-03-19 19:52:08 +01:00
|
|
|
|
lhs: ValueRef,
|
|
|
|
|
rhs: ValueRef,
|
|
|
|
|
trunc: F,
|
|
|
|
|
zext: G)
|
2015-11-19 12:36:31 +01:00
|
|
|
|
-> ValueRef
|
|
|
|
|
where F: FnOnce(ValueRef, Type) -> ValueRef,
|
|
|
|
|
G: FnOnce(ValueRef, Type) -> ValueRef
|
2014-12-09 13:44:51 -05:00
|
|
|
|
{
|
2012-02-21 21:01:33 -08:00
|
|
|
|
// Shifts may have any size int on the rhs
|
2016-03-29 09:32:58 +03:00
|
|
|
|
if op.is_shift() {
|
2015-01-15 01:08:22 +11:00
|
|
|
|
let mut rhs_llty = val_ty(rhs);
|
|
|
|
|
let mut lhs_llty = val_ty(lhs);
|
2015-11-19 12:36:31 +01:00
|
|
|
|
if rhs_llty.kind() == Vector {
|
|
|
|
|
rhs_llty = rhs_llty.element_type()
|
|
|
|
|
}
|
|
|
|
|
if lhs_llty.kind() == Vector {
|
|
|
|
|
lhs_llty = lhs_llty.element_type()
|
|
|
|
|
}
|
2015-01-15 01:08:22 +11:00
|
|
|
|
let rhs_sz = rhs_llty.int_width();
|
|
|
|
|
let lhs_sz = lhs_llty.int_width();
|
|
|
|
|
if lhs_sz < rhs_sz {
|
|
|
|
|
trunc(rhs, lhs_llty)
|
|
|
|
|
} else if lhs_sz > rhs_sz {
|
|
|
|
|
// FIXME (#1877: If shifting by negative
|
|
|
|
|
// values becomes not undefined then this is wrong.
|
|
|
|
|
zext(rhs, lhs_llty)
|
2012-02-21 21:01:33 -08:00
|
|
|
|
} else {
|
|
|
|
|
rhs
|
|
|
|
|
}
|
2015-01-15 01:08:22 +11:00
|
|
|
|
} else {
|
|
|
|
|
rhs
|
2012-02-21 21:01:33 -08:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-11 11:48:43 -07:00
|
|
|
|
/// Returns whether this session's target will use SEH-based unwinding.
|
|
|
|
|
///
|
|
|
|
|
/// This is only true for MSVC targets, and even then the 64-bit MSVC target
|
|
|
|
|
/// currently uses SEH-ish unwinding with DWARF info tables to the side (same as
|
|
|
|
|
/// 64-bit MinGW) instead of "full SEH".
|
|
|
|
|
pub fn wants_msvc_seh(sess: &Session) -> bool {
|
2015-10-23 18:18:44 -07:00
|
|
|
|
sess.target.target.options.is_like_msvc
|
2015-08-11 11:48:43 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-10-04 19:24:49 +03:00
|
|
|
|
pub fn call_assume<'a, 'tcx>(b: &Builder<'a, 'tcx>, val: ValueRef) {
|
|
|
|
|
let assume_intrinsic = b.ccx.get_intrinsic("llvm.assume");
|
|
|
|
|
b.call(assume_intrinsic, &[val], None);
|
|
|
|
|
}
|
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
|
/// Helper for loading values from memory. Does the necessary conversion if the in-memory type
|
|
|
|
|
/// differs from the type used for SSA values. Also handles various special cases where the type
|
|
|
|
|
/// gives us better information about what we are loading.
|
2016-12-11 15:03:52 -07:00
|
|
|
|
pub fn load_ty<'a, 'tcx>(b: &Builder<'a, 'tcx>, ptr: ValueRef, t: Ty<'tcx>) -> ValueRef {
|
2016-03-09 14:20:22 +02:00
|
|
|
|
let ccx = b.ccx;
|
|
|
|
|
if type_is_zero_size(ccx, t) {
|
|
|
|
|
return C_undef(type_of::type_of(ccx, t));
|
|
|
|
|
}
|
2015-03-21 00:21:38 +01:00
|
|
|
|
|
|
|
|
|
unsafe {
|
|
|
|
|
let global = llvm::LLVMIsAGlobalVariable(ptr);
|
|
|
|
|
if !global.is_null() && llvm::LLVMIsGlobalConstant(global) == llvm::True {
|
|
|
|
|
let val = llvm::LLVMGetInitializer(global);
|
|
|
|
|
if !val.is_null() {
|
2016-03-09 14:20:22 +02:00
|
|
|
|
if t.is_bool() {
|
|
|
|
|
return llvm::LLVMConstTrunc(val, Type::i1(ccx).to_ref());
|
|
|
|
|
}
|
|
|
|
|
return val;
|
2015-01-29 14:03:34 +02:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-07-05 21:47:14 +02:00
|
|
|
|
}
|
2015-03-21 00:21:38 +01:00
|
|
|
|
|
2016-03-09 14:20:22 +02:00
|
|
|
|
if t.is_bool() {
|
|
|
|
|
b.trunc(b.load_range_assert(ptr, 0, 2, llvm::False), Type::i1(ccx))
|
2015-06-24 08:24:13 +03:00
|
|
|
|
} else if t.is_char() {
|
2015-03-21 00:21:38 +01:00
|
|
|
|
// a char is a Unicode codepoint, and so takes values from 0
|
|
|
|
|
// to 0x10FFFF inclusive only.
|
2016-03-09 14:20:22 +02:00
|
|
|
|
b.load_range_assert(ptr, 0, 0x10FFFF + 1, llvm::False)
|
2016-12-18 11:50:07 -07:00
|
|
|
|
} else if (t.is_region_ptr() || t.is_unique()) && !common::type_is_fat_ptr(ccx, t) {
|
2016-03-09 14:20:22 +02:00
|
|
|
|
b.load_nonnull(ptr)
|
2015-03-21 00:21:38 +01:00
|
|
|
|
} else {
|
2016-03-09 14:20:22 +02:00
|
|
|
|
b.load(ptr)
|
|
|
|
|
}
|
2014-07-05 21:47:14 +02:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-25 21:17:11 -05:00
|
|
|
|
/// Helper for storing values in memory. Does the necessary conversion if the in-memory type
|
|
|
|
|
/// differs from the type used for SSA values.
|
2016-12-17 19:54:32 -07:00
|
|
|
|
pub fn store_ty<'a, 'tcx>(cx: &BlockAndBuilder<'a, 'tcx>, v: ValueRef, dst: ValueRef, t: Ty<'tcx>) {
|
2016-02-18 19:49:45 +02:00
|
|
|
|
debug!("store_ty: {:?} : {:?} <- {:?}", Value(dst), t, Value(v));
|
2015-11-09 02:16:19 +02:00
|
|
|
|
|
2016-12-19 17:48:41 -07:00
|
|
|
|
if common::type_is_fat_ptr(cx.ccx, t) {
|
2016-12-11 08:59:20 -07:00
|
|
|
|
let lladdr = cx.extract_value(v, abi::FAT_PTR_ADDR);
|
|
|
|
|
let llextra = cx.extract_value(v, abi::FAT_PTR_EXTRA);
|
2016-10-04 18:34:03 +03:00
|
|
|
|
store_fat_ptr(cx, lladdr, llextra, dst, t);
|
2015-06-26 16:40:51 +02:00
|
|
|
|
} else {
|
2016-12-29 02:20:26 +01:00
|
|
|
|
cx.store(from_immediate(cx, v), dst, None);
|
2015-04-15 20:14:54 +02:00
|
|
|
|
}
|
2015-03-21 00:21:38 +01:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 19:54:32 -07:00
|
|
|
|
pub fn store_fat_ptr<'a, 'tcx>(cx: &BlockAndBuilder<'a, 'tcx>,
|
|
|
|
|
data: ValueRef,
|
|
|
|
|
extra: ValueRef,
|
|
|
|
|
dst: ValueRef,
|
|
|
|
|
_ty: Ty<'tcx>) {
|
2015-11-10 22:05:11 +02:00
|
|
|
|
// FIXME: emit metadata
|
2016-12-29 02:20:26 +01:00
|
|
|
|
cx.store(data, get_dataptr(cx, dst), None);
|
|
|
|
|
cx.store(extra, get_meta(cx, dst), None);
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-11 15:03:52 -07:00
|
|
|
|
pub fn load_fat_ptr<'a, 'tcx>(
|
2016-12-17 19:54:32 -07:00
|
|
|
|
b: &Builder<'a, 'tcx>, src: ValueRef, t: Ty<'tcx>
|
|
|
|
|
) -> (ValueRef, ValueRef) {
|
2016-12-11 15:03:52 -07:00
|
|
|
|
let ptr = get_dataptr(b, src);
|
2016-10-04 17:44:31 +03:00
|
|
|
|
let ptr = if t.is_region_ptr() || t.is_unique() {
|
|
|
|
|
b.load_nonnull(ptr)
|
|
|
|
|
} else {
|
|
|
|
|
b.load(ptr)
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// FIXME: emit metadata on `meta`.
|
2016-12-11 15:03:52 -07:00
|
|
|
|
let meta = b.load(get_meta(b, src));
|
2016-10-04 17:44:31 +03:00
|
|
|
|
|
|
|
|
|
(ptr, meta)
|
2015-11-10 22:05:11 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 20:32:44 -07:00
|
|
|
|
pub fn from_immediate(bcx: &BlockAndBuilder, val: ValueRef) -> ValueRef {
|
2016-12-19 16:25:00 -07:00
|
|
|
|
if val_ty(val) == Type::i1(bcx.ccx) {
|
|
|
|
|
bcx.zext(val, Type::i8(bcx.ccx))
|
2015-03-21 00:21:38 +01:00
|
|
|
|
} else {
|
|
|
|
|
val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-10 20:32:44 -07:00
|
|
|
|
pub fn to_immediate(bcx: &BlockAndBuilder, val: ValueRef, ty: Ty) -> ValueRef {
|
2015-06-24 08:24:13 +03:00
|
|
|
|
if ty.is_bool() {
|
2016-12-19 16:25:00 -07:00
|
|
|
|
bcx.trunc(val, Type::i1(bcx.ccx))
|
2015-03-21 00:21:38 +01:00
|
|
|
|
} else {
|
|
|
|
|
val
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-06-08 00:35:01 +03:00
|
|
|
|
pub enum Lifetime { Start, End }
|
2016-01-12 17:17:50 +01:00
|
|
|
|
|
2016-06-08 00:35:01 +03:00
|
|
|
|
impl Lifetime {
|
2016-12-11 15:03:52 -07:00
|
|
|
|
// If LLVM lifetime intrinsic support is enabled (i.e. optimizations
|
|
|
|
|
// on), and `ptr` is nonzero-sized, then extracts the size of `ptr`
|
|
|
|
|
// and the intrinsic for `lt` and passes them to `emit`, which is in
|
|
|
|
|
// charge of generating code to call the passed intrinsic on whatever
|
|
|
|
|
// block of generated code is targetted for the intrinsic.
|
|
|
|
|
//
|
|
|
|
|
// If LLVM lifetime intrinsic support is disabled (i.e. optimizations
|
|
|
|
|
// off) or `ptr` is zero-sized, then no-op (does not call `emit`).
|
2016-06-08 00:35:01 +03:00
|
|
|
|
pub fn call(self, b: &Builder, ptr: ValueRef) {
|
2016-12-11 15:03:52 -07:00
|
|
|
|
if b.ccx.sess().opts.optimize == config::OptLevel::No {
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let size = machine::llsize_of_alloc(b.ccx, val_ty(ptr).element_type());
|
|
|
|
|
if size == 0 {
|
|
|
|
|
return;
|
|
|
|
|
}
|
2016-06-08 00:35:01 +03:00
|
|
|
|
|
2016-12-11 15:03:52 -07:00
|
|
|
|
let lifetime_intrinsic = b.ccx.get_intrinsic(match self {
|
|
|
|
|
Lifetime::Start => "llvm.lifetime.start",
|
|
|
|
|
Lifetime::End => "llvm.lifetime.end"
|
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let ptr = b.pointercast(ptr, Type::i8p(b.ccx));
|
|
|
|
|
b.call(lifetime_intrinsic, &[C_u64(b.ccx, size), ptr], None);
|
|
|
|
|
}
|
Emit LLVM lifetime intrinsics to improve stack usage and codegen in general
Lifetime intrinsics help to reduce stack usage, because LLVM can apply
stack coloring to reuse the stack slots of dead allocas for new ones.
For example these functions now both use the same amount of stack, while
previous `bar()` used five times as much as `foo()`:
````rust
fn foo() {
println("{}", 5);
}
fn bar() {
println("{}", 5);
println("{}", 5);
println("{}", 5);
println("{}", 5);
println("{}", 5);
}
````
On top of that, LLVM can also optimize out certain operations when it
knows that memory is dead after a certain point. For example, it can
sometimes remove the zeroing used to cancel the drop glue. This is
possible when the glue drop itself was already removed because the
zeroing dominated the drop glue call. For example in:
````rust
pub fn bar(x: (Box<int>, int)) -> (Box<int>, int) {
x
}
````
With optimizations, this currently results in:
````llvm
define void @_ZN3bar20h330fa42547df8179niaE({ i64*, i64 }* noalias nocapture nonnull sret, { i64*, i64 }* noalias nocapture nonnull) unnamed_addr #0 {
"_ZN29_$LP$Box$LT$int$GT$$C$int$RP$39glue_drop.$x22glue_drop$x22$LP$1347$RP$17h88cf42702e5a322aE.exit":
%2 = bitcast { i64*, i64 }* %1 to i8*
%3 = bitcast { i64*, i64 }* %0 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 16, i32 8, i1 false)
tail call void @llvm.memset.p0i8.i64(i8* %2, i8 0, i64 16, i32 8, i1 false)
ret void
}
````
But with lifetime intrinsics we get:
````llvm
define void @_ZN3bar20h330fa42547df8179niaE({ i64*, i64 }* noalias nocapture nonnull sret, { i64*, i64 }* noalias nocapture nonnull) unnamed_addr #0 {
"_ZN29_$LP$Box$LT$int$GT$$C$int$RP$39glue_drop.$x22glue_drop$x22$LP$1347$RP$17h88cf42702e5a322aE.exit":
%2 = bitcast { i64*, i64 }* %1 to i8*
%3 = bitcast { i64*, i64 }* %0 to i8*
tail call void @llvm.memcpy.p0i8.p0i8.i64(i8* %3, i8* %2, i64 16, i32 8, i1 false)
tail call void @llvm.lifetime.end(i64 16, i8* %2)
ret void
}
````
Fixes #15665
2014-05-01 19:32:07 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 19:54:32 -07:00
|
|
|
|
pub fn call_memcpy<'a, 'tcx>(b: &Builder<'a, 'tcx>,
|
2016-03-08 14:29:46 +02:00
|
|
|
|
dst: ValueRef,
|
|
|
|
|
src: ValueRef,
|
|
|
|
|
n_bytes: ValueRef,
|
|
|
|
|
align: u32) {
|
|
|
|
|
let ccx = b.ccx;
|
2015-08-15 18:43:39 +12:00
|
|
|
|
let ptr_width = &ccx.sess().target.target.target_pointer_width[..];
|
|
|
|
|
let key = format!("llvm.memcpy.p0i8.p0i8.i{}", ptr_width);
|
2014-04-09 19:56:31 -04:00
|
|
|
|
let memcpy = ccx.get_intrinsic(&key);
|
2016-03-08 14:29:46 +02:00
|
|
|
|
let src_ptr = b.pointercast(src, Type::i8p(ccx));
|
|
|
|
|
let dst_ptr = b.pointercast(dst, Type::i8p(ccx));
|
|
|
|
|
let size = b.intcast(n_bytes, ccx.int_type());
|
2014-03-15 22:29:34 +02:00
|
|
|
|
let align = C_i32(ccx, align as i32);
|
2014-07-05 21:43:47 +02:00
|
|
|
|
let volatile = C_bool(ccx, false);
|
2016-03-08 14:29:46 +02:00
|
|
|
|
b.call(memcpy, &[dst_ptr, src_ptr, size, align, volatile], None);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 02:20:26 +01:00
|
|
|
|
pub fn memcpy_ty<'a, 'tcx>(bcx: &BlockAndBuilder<'a, 'tcx>,
|
|
|
|
|
dst: ValueRef,
|
|
|
|
|
src: ValueRef,
|
|
|
|
|
t: Ty<'tcx>,
|
|
|
|
|
align: Option<u32>) {
|
2016-12-19 16:25:00 -07:00
|
|
|
|
let ccx = bcx.ccx;
|
2015-08-22 17:07:37 +02:00
|
|
|
|
|
2016-12-10 20:35:02 -07:00
|
|
|
|
if type_is_zero_size(ccx, t) {
|
2015-08-22 17:07:37 +02:00
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-29 02:20:26 +01:00
|
|
|
|
let llty = type_of::type_of(ccx, t);
|
|
|
|
|
let llsz = llsize_of(ccx, llty);
|
|
|
|
|
let llalign = align.unwrap_or_else(|| type_of::align_of(ccx, t));
|
|
|
|
|
call_memcpy(bcx, dst, src, llsz, llalign as u32);
|
2012-08-28 15:54:45 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 19:54:32 -07:00
|
|
|
|
pub fn call_memset<'a, 'tcx>(b: &Builder<'a, 'tcx>,
|
2016-02-04 19:40:28 +02:00
|
|
|
|
ptr: ValueRef,
|
|
|
|
|
fill_byte: ValueRef,
|
|
|
|
|
size: ValueRef,
|
|
|
|
|
align: ValueRef,
|
2016-12-17 15:52:29 -07:00
|
|
|
|
volatile: bool) -> ValueRef {
|
|
|
|
|
let ptr_width = &b.ccx.sess().target.target.target_pointer_width[..];
|
2016-02-04 19:40:28 +02:00
|
|
|
|
let intrinsic_key = format!("llvm.memset.p0i8.i{}", ptr_width);
|
2016-12-17 15:52:29 -07:00
|
|
|
|
let llintrinsicfn = b.ccx.get_intrinsic(&intrinsic_key);
|
|
|
|
|
let volatile = C_bool(b.ccx, volatile);
|
|
|
|
|
b.call(llintrinsicfn, &[ptr, fill_byte, size, align, volatile], None)
|
2016-02-04 19:40:28 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-12-17 19:54:32 -07:00
|
|
|
|
pub fn alloc_ty<'a, 'tcx>(bcx: &BlockAndBuilder<'a, 'tcx>, ty: Ty<'tcx>, name: &str) -> ValueRef {
|
2016-08-16 17:41:38 +03:00
|
|
|
|
assert!(!ty.has_param_types());
|
2016-12-19 16:25:00 -07:00
|
|
|
|
bcx.fcx().alloca(type_of::type_of(bcx.ccx, ty), name)
|
Improve usage of lifetime intrinsics in match expressions
The allocas used in match expression currently don't get good lifetime
markers, in fact they only get lifetime start markers, because their
lifetimes don't match to cleanup scopes.
While the bindings themselves are bog standard and just need a matching
pair of start and end markers, they might need them twice, once for a
guard clause and once for the match body.
The __llmatch alloca OTOH needs a single lifetime start marker, but
when there's a guard clause, it needs two end markers, because its
lifetime ends either when the guard doesn't match or after the match
body.
With these intrinsics in place, LLVM can now, for example, optimize
code like this:
````rust
enum E {
A1(int),
A2(int),
A3(int),
A4(int),
}
pub fn variants(x: E) {
match x {
A1(m) => bar(&m),
A2(m) => bar(&m),
A3(m) => bar(&m),
A4(m) => bar(&m),
}
}
````
To a single call to bar, using only a single stack slot. It still fails
to eliminate some of checks.
````gas
.Ltmp5:
.cfi_def_cfa_offset 16
movb (%rdi), %al
testb %al, %al
je .LBB3_5
movzbl %al, %eax
cmpl $1, %eax
je .LBB3_5
cmpl $2, %eax
.LBB3_5:
movq 8(%rdi), %rax
movq %rax, (%rsp)
leaq (%rsp), %rdi
callq _ZN3bar20hcb7a0d8be8e17e37daaE@PLT
popq %rax
retq
````
2014-07-23 17:39:13 +02:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-09 23:09:28 +02:00
|
|
|
|
pub fn trans_instance<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>, instance: Instance<'tcx>) {
|
2016-11-04 17:37:42 -04:00
|
|
|
|
let _s = if ccx.sess().trans_stats() {
|
|
|
|
|
let mut instance_name = String::new();
|
|
|
|
|
DefPathBasedNames::new(ccx.tcx(), true, true)
|
|
|
|
|
.push_def_path(instance.def, &mut instance_name);
|
|
|
|
|
Some(StatRecorder::new(ccx, instance_name))
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
};
|
|
|
|
|
|
2016-11-09 23:09:28 +02:00
|
|
|
|
// this is an info! to allow collecting monomorphization statistics
|
|
|
|
|
// and to allow finding the last function before LLVM aborts from
|
|
|
|
|
// release builds.
|
|
|
|
|
info!("trans_instance({})", instance);
|
|
|
|
|
|
|
|
|
|
let fn_ty = ccx.tcx().item_type(instance.def);
|
|
|
|
|
let fn_ty = ccx.tcx().erase_regions(&fn_ty);
|
|
|
|
|
let fn_ty = monomorphize::apply_param_substs(ccx.shared(), instance.substs, &fn_ty);
|
|
|
|
|
|
|
|
|
|
let ty::BareFnTy { abi, ref sig, .. } = *common::ty_fn_ty(ccx, fn_ty);
|
|
|
|
|
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(sig);
|
|
|
|
|
|
|
|
|
|
let lldecl = match ccx.instances().borrow().get(&instance) {
|
|
|
|
|
Some(&val) => val,
|
|
|
|
|
None => bug!("Instance `{:?}` not already declared", instance)
|
|
|
|
|
};
|
|
|
|
|
|
2014-09-05 09:18:53 -07:00
|
|
|
|
ccx.stats().n_closures.set(ccx.stats().n_closures.get() + 1);
|
2013-12-22 13:50:04 -08:00
|
|
|
|
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
|
if !ccx.sess().no_landing_pads() {
|
2016-11-09 23:09:28 +02:00
|
|
|
|
attributes::emit_uwtable(lldecl, true);
|
rustc: Implement custom panic runtimes
This commit is an implementation of [RFC 1513] which allows applications to
alter the behavior of panics at compile time. A new compiler flag, `-C panic`,
is added and accepts the values `unwind` or `panic`, with the default being
`unwind`. This model affects how code is generated for the local crate, skipping
generation of landing pads with `-C panic=abort`.
[RFC 1513]: https://github.com/rust-lang/rfcs/blob/master/text/1513-less-unwinding.md
Panic implementations are then provided by crates tagged with
`#![panic_runtime]` and lazily required by crates with
`#![needs_panic_runtime]`. The panic strategy (`-C panic` value) of the panic
runtime must match the final product, and if the panic strategy is not `abort`
then the entire DAG must have the same panic strategy.
With the `-C panic=abort` strategy, users can expect a stable method to disable
generation of landing pads, improving optimization in niche scenarios,
decreasing compile time, and decreasing output binary size. With the `-C
panic=unwind` strategy users can expect the existing ability to isolate failure
in Rust code from the outside world.
Organizationally, this commit dismantles the `sys_common::unwind` module in
favor of some bits moving part of it to `libpanic_unwind` and the rest into the
`panicking` module in libstd. The custom panic runtime support is pretty similar
to the custom allocator support with the only major difference being how the
panic runtime is injected (takes the `-C panic` flag into account).
2016-04-08 16:18:40 -07:00
|
|
|
|
}
|
2010-11-26 17:47:27 -08:00
|
|
|
|
|
2016-11-09 23:09:28 +02:00
|
|
|
|
let fn_ty = FnType::new(ccx, abi, &sig, &[]);
|
2016-04-06 10:49:50 +03:00
|
|
|
|
|
2016-12-19 19:16:36 -07:00
|
|
|
|
let fcx = FunctionContext::new(ccx, lldecl);
|
2016-12-17 12:56:33 -07:00
|
|
|
|
let mir = ccx.tcx().item_mir(instance.def);
|
2016-12-19 19:16:36 -07:00
|
|
|
|
mir::trans_mir(&fcx, fn_ty, &mir, instance, &sig, abi);
|
2011-06-29 19:50:50 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-02-23 22:04:51 +02:00
|
|
|
|
pub fn trans_ctor_shim<'a, 'tcx>(ccx: &CrateContext<'a, 'tcx>,
|
2016-08-17 22:50:55 +03:00
|
|
|
|
def_id: DefId,
|
|
|
|
|
substs: &'tcx Substs<'tcx>,
|
2016-02-23 22:04:51 +02:00
|
|
|
|
disr: Disr,
|
|
|
|
|
llfndecl: ValueRef) {
|
2016-08-17 22:50:55 +03:00
|
|
|
|
attributes::inline(llfndecl, attributes::InlineAttr::Hint);
|
|
|
|
|
attributes::set_frame_pointer_elimination(ccx, llfndecl);
|
|
|
|
|
|
2016-11-10 16:49:53 +02:00
|
|
|
|
let ctor_ty = ccx.tcx().item_type(def_id);
|
2016-08-08 20:50:19 -04:00
|
|
|
|
let ctor_ty = monomorphize::apply_param_substs(ccx.shared(), substs, &ctor_ty);
|
2013-06-20 15:23:52 -04:00
|
|
|
|
|
2016-10-13 00:08:11 +03:00
|
|
|
|
let sig = ccx.tcx().erase_late_bound_regions_and_normalize(&ctor_ty.fn_sig());
|
2016-03-06 16:30:21 +02:00
|
|
|
|
let fn_ty = FnType::new(ccx, Abi::Rust, &sig, &[]);
|
2013-04-18 15:53:29 -07:00
|
|
|
|
|
2016-12-19 19:16:36 -07:00
|
|
|
|
let fcx = FunctionContext::new(ccx, llfndecl);
|
2016-12-16 16:45:52 -07:00
|
|
|
|
let bcx = fcx.get_entry_block();
|
2016-12-19 19:16:36 -07:00
|
|
|
|
if !fn_ty.ret.is_ignore() {
|
2016-12-18 09:07:35 -07:00
|
|
|
|
// But if there are no nested returns, we skip the indirection
|
|
|
|
|
// and have a single retslot
|
2016-12-19 19:16:36 -07:00
|
|
|
|
let dest = if fn_ty.ret.is_indirect() {
|
2016-12-18 09:07:35 -07:00
|
|
|
|
get_param(fcx.llfn, 0)
|
|
|
|
|
} else {
|
|
|
|
|
// We create an alloca to hold a pointer of type `ret.original_ty`
|
|
|
|
|
// which will hold the pointer to the right alloca which has the
|
|
|
|
|
// final ret value
|
2016-12-19 19:16:36 -07:00
|
|
|
|
fcx.alloca(fn_ty.ret.memory_ty(ccx), "sret_slot")
|
2016-12-18 09:07:35 -07:00
|
|
|
|
};
|
2015-12-07 02:38:29 +13:00
|
|
|
|
let dest_val = adt::MaybeSizedValue::sized(dest); // Can return unsized value
|
2016-12-19 19:16:36 -07:00
|
|
|
|
let mut llarg_idx = fn_ty.ret.is_indirect() as usize;
|
2016-03-06 16:30:21 +02:00
|
|
|
|
let mut arg_idx = 0;
|
2016-11-28 20:25:33 -07:00
|
|
|
|
for (i, arg_ty) in sig.inputs().iter().enumerate() {
|
2016-12-10 20:32:44 -07:00
|
|
|
|
let lldestptr = adt::trans_field_ptr(&bcx, sig.output(), dest_val, Disr::from(disr), i);
|
2016-12-19 19:16:36 -07:00
|
|
|
|
let arg = &fn_ty.args[arg_idx];
|
2016-03-06 16:30:21 +02:00
|
|
|
|
arg_idx += 1;
|
2016-12-19 16:25:00 -07:00
|
|
|
|
if common::type_is_fat_ptr(bcx.ccx, arg_ty) {
|
2016-12-19 19:16:36 -07:00
|
|
|
|
let meta = &fn_ty.args[arg_idx];
|
2016-03-06 16:30:21 +02:00
|
|
|
|
arg_idx += 1;
|
2016-12-10 20:32:44 -07:00
|
|
|
|
arg.store_fn_arg(&bcx, &mut llarg_idx, get_dataptr(&bcx, lldestptr));
|
|
|
|
|
meta.store_fn_arg(&bcx, &mut llarg_idx, get_meta(&bcx, lldestptr));
|
2015-07-02 18:18:22 +02:00
|
|
|
|
} else {
|
2016-12-10 20:32:44 -07:00
|
|
|
|
arg.store_fn_arg(&bcx, &mut llarg_idx, lldestptr);
|
2015-07-02 18:18:22 +02:00
|
|
|
|
}
|
2014-01-16 15:11:22 -05:00
|
|
|
|
}
|
2016-12-10 20:32:44 -07:00
|
|
|
|
adt::trans_set_discr(&bcx, sig.output(), dest, disr);
|
2014-01-15 14:39:08 -05:00
|
|
|
|
|
2016-12-19 19:16:36 -07:00
|
|
|
|
if fn_ty.ret.is_indirect() {
|
2016-12-18 09:07:35 -07:00
|
|
|
|
bcx.ret_void();
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2016-12-19 19:16:36 -07:00
|
|
|
|
if let Some(cast_ty) = fn_ty.ret.cast {
|
2016-12-18 09:07:35 -07:00
|
|
|
|
let load = bcx.load(bcx.pointercast(dest, cast_ty.ptr_to()));
|
2016-12-19 19:16:36 -07:00
|
|
|
|
let llalign = llalign_of_min(ccx, fn_ty.ret.ty);
|
2016-12-18 09:07:35 -07:00
|
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMSetAlignment(load, llalign);
|
|
|
|
|
}
|
|
|
|
|
bcx.ret(load)
|
|
|
|
|
} else {
|
2016-12-19 16:16:22 -07:00
|
|
|
|
bcx.ret(bcx.load(dest))
|
2016-12-18 09:07:35 -07:00
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
bcx.ret_void();
|
|
|
|
|
}
|
2012-10-24 14:36:00 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-11-11 20:22:41 -05:00
|
|
|
|
pub fn llvm_linkage_by_name(name: &str) -> Option<Linkage> {
|
|
|
|
|
// Use the names from src/llvm/docs/LangRef.rst here. Most types are only
|
|
|
|
|
// applicable to variable declarations and may not really make sense for
|
|
|
|
|
// Rust code in the first place but whitelist them anyway and trust that
|
|
|
|
|
// the user knows what s/he's doing. Who knows, unanticipated use cases
|
|
|
|
|
// may pop up in the future.
|
|
|
|
|
//
|
|
|
|
|
// ghost, dllimport, dllexport and linkonce_odr_autohide are not supported
|
|
|
|
|
// and don't have to be, LLVM treats them as no-ops.
|
|
|
|
|
match name {
|
2016-09-01 13:52:33 -05:00
|
|
|
|
"appending" => Some(llvm::Linkage::AppendingLinkage),
|
|
|
|
|
"available_externally" => Some(llvm::Linkage::AvailableExternallyLinkage),
|
|
|
|
|
"common" => Some(llvm::Linkage::CommonLinkage),
|
|
|
|
|
"extern_weak" => Some(llvm::Linkage::ExternalWeakLinkage),
|
|
|
|
|
"external" => Some(llvm::Linkage::ExternalLinkage),
|
|
|
|
|
"internal" => Some(llvm::Linkage::InternalLinkage),
|
|
|
|
|
"linkonce" => Some(llvm::Linkage::LinkOnceAnyLinkage),
|
|
|
|
|
"linkonce_odr" => Some(llvm::Linkage::LinkOnceODRLinkage),
|
|
|
|
|
"private" => Some(llvm::Linkage::PrivateLinkage),
|
|
|
|
|
"weak" => Some(llvm::Linkage::WeakAnyLinkage),
|
|
|
|
|
"weak_odr" => Some(llvm::Linkage::WeakODRLinkage),
|
2014-11-11 20:22:41 -05:00
|
|
|
|
_ => None,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 20:02:09 -04:00
|
|
|
|
pub fn set_link_section(ccx: &CrateContext,
|
|
|
|
|
llval: ValueRef,
|
|
|
|
|
attrs: &[ast::Attribute]) {
|
|
|
|
|
if let Some(sect) = attr::first_attr_value_str_by_name(attrs, "link_section") {
|
2016-11-16 10:52:37 +00:00
|
|
|
|
if contains_null(§.as_str()) {
|
2016-06-13 22:43:30 -07:00
|
|
|
|
ccx.sess().fatal(&format!("Illegal null byte in link_section value: `{}`", §));
|
|
|
|
|
}
|
|
|
|
|
unsafe {
|
2016-11-16 10:52:37 +00:00
|
|
|
|
let buf = CString::new(sect.as_str().as_bytes()).unwrap();
|
2016-06-13 22:43:30 -07:00
|
|
|
|
llvm::LLVMSetSection(llval, buf.as_ptr());
|
|
|
|
|
}
|
2015-08-03 15:38:06 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-26 12:18:39 -04:00
|
|
|
|
/// Create the `main` function which will initialise the rust runtime and call
|
|
|
|
|
/// users’ main function.
|
|
|
|
|
pub fn maybe_create_entry_wrapper(ccx: &CrateContext) {
|
|
|
|
|
let (main_def_id, span) = match *ccx.sess().entry_fn.borrow() {
|
|
|
|
|
Some((id, span)) => {
|
|
|
|
|
(ccx.tcx().map.local_def_id(id), span)
|
|
|
|
|
}
|
|
|
|
|
None => return,
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// check for the #[rustc_error] annotation, which forces an
|
|
|
|
|
// error in trans. This is used to write compile-fail tests
|
|
|
|
|
// that actually test that compilation succeeds without
|
|
|
|
|
// reporting an error.
|
|
|
|
|
if ccx.tcx().has_attr(main_def_id, "rustc_error") {
|
|
|
|
|
ccx.tcx().sess.span_fatal(span, "compilation successful");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let instance = Instance::mono(ccx.shared(), main_def_id);
|
|
|
|
|
|
2016-07-21 12:49:59 -04:00
|
|
|
|
if !ccx.codegen_unit().contains_item(&TransItem::Fn(instance)) {
|
2016-05-26 12:18:39 -04:00
|
|
|
|
// We want to create the wrapper in the same codegen unit as Rust's main
|
|
|
|
|
// function.
|
|
|
|
|
return;
|
2013-01-11 18:08:01 +09:00
|
|
|
|
}
|
|
|
|
|
|
2016-08-16 17:41:38 +03:00
|
|
|
|
let main_llfn = Callee::def(ccx, main_def_id, instance.substs).reify(ccx);
|
2016-05-26 12:18:39 -04:00
|
|
|
|
|
2014-03-05 16:36:01 +02:00
|
|
|
|
let et = ccx.sess().entry_type.get().unwrap();
|
2013-08-03 19:59:46 -07:00
|
|
|
|
match et {
|
2016-12-16 18:48:25 -07:00
|
|
|
|
config::EntryMain => create_entry_fn(ccx, span, main_llfn, true),
|
2016-05-26 12:18:39 -04:00
|
|
|
|
config::EntryStart => create_entry_fn(ccx, span, main_llfn, false),
|
2014-05-06 23:38:01 +12:00
|
|
|
|
config::EntryNone => {} // Do nothing.
|
2013-04-09 20:16:06 +12:00
|
|
|
|
}
|
2011-08-12 18:43:44 -07:00
|
|
|
|
|
2014-03-06 18:47:24 +02:00
|
|
|
|
fn create_entry_fn(ccx: &CrateContext,
|
2015-03-04 11:46:55 +02:00
|
|
|
|
sp: Span,
|
2013-04-18 15:53:29 -07:00
|
|
|
|
rust_main: ValueRef,
|
|
|
|
|
use_start_lang_item: bool) {
|
2015-11-19 12:36:31 +01:00
|
|
|
|
let llfty = Type::func(&[ccx.int_type(), Type::i8p(ccx).ptr_to()], &ccx.int_type());
|
2012-11-30 09:21:49 +09:00
|
|
|
|
|
2016-02-23 21:46:08 +02:00
|
|
|
|
if declare::get_defined_value(ccx, "main").is_some() {
|
2015-03-04 01:08:06 +02:00
|
|
|
|
// FIXME: We should be smart and show a better diagnostic here.
|
2015-12-21 10:00:43 +13:00
|
|
|
|
ccx.sess().struct_span_err(sp, "entry symbol `main` defined multiple times")
|
|
|
|
|
.help("did you use #[no_mangle] on `fn main`? Use #[start] instead")
|
|
|
|
|
.emit();
|
2015-03-04 01:08:06 +02:00
|
|
|
|
ccx.sess().abort_if_errors();
|
2016-03-29 01:46:02 +02:00
|
|
|
|
bug!();
|
2016-02-23 21:46:08 +02:00
|
|
|
|
}
|
|
|
|
|
let llfn = declare::declare_cfn(ccx, "main", llfty);
|
2014-08-18 14:15:05 -04:00
|
|
|
|
|
2016-11-03 10:53:13 +01:00
|
|
|
|
// `main` should respect same config for frame pointer elimination as rest of code
|
|
|
|
|
attributes::set_frame_pointer_elimination(ccx, llfn);
|
|
|
|
|
|
2014-11-25 13:28:35 -08:00
|
|
|
|
let llbb = unsafe {
|
2016-12-16 18:48:25 -07:00
|
|
|
|
let name = CString::new("top").unwrap();
|
|
|
|
|
llvm::LLVMAppendBasicBlockInContext(ccx.llcx(), llfn, name.as_ptr())
|
2014-11-25 13:28:35 -08:00
|
|
|
|
};
|
2016-12-16 18:48:25 -07:00
|
|
|
|
let bld = Builder::with_ccx(ccx);
|
|
|
|
|
bld.position_at_end(llbb);
|
2014-12-03 14:48:18 -08:00
|
|
|
|
|
2016-12-16 18:48:25 -07:00
|
|
|
|
debuginfo::gdb::insert_reference_to_gdb_debug_scripts_section_global(ccx, &bld);
|
2013-04-18 15:53:29 -07:00
|
|
|
|
|
2016-12-16 18:48:25 -07:00
|
|
|
|
let (start_fn, args) = if use_start_lang_item {
|
|
|
|
|
let start_def_id = ccx.tcx().require_lang_item(StartFnLangItem);
|
|
|
|
|
let empty_substs = ccx.tcx().intern_substs(&[]);
|
|
|
|
|
let start_fn = Callee::def(ccx, start_def_id, empty_substs).reify(ccx);
|
|
|
|
|
(start_fn, vec![bld.pointercast(rust_main, Type::i8p(ccx).ptr_to()), get_param(llfn, 0),
|
|
|
|
|
get_param(llfn, 1)])
|
|
|
|
|
} else {
|
|
|
|
|
debug!("using user-defined start fn");
|
|
|
|
|
(rust_main, vec![get_param(llfn, 0 as c_uint), get_param(llfn, 1 as c_uint)])
|
|
|
|
|
};
|
2013-07-07 13:30:48 -07:00
|
|
|
|
|
2016-12-16 18:48:25 -07:00
|
|
|
|
let result = bld.call(start_fn, &args, None);
|
|
|
|
|
bld.ret(result);
|
2011-10-20 13:48:10 +02:00
|
|
|
|
}
|
2011-02-28 17:33:46 -05:00
|
|
|
|
}
|
|
|
|
|
|
2014-09-23 00:14:46 -07:00
|
|
|
|
fn contains_null(s: &str) -> bool {
|
2014-09-23 12:54:16 -07:00
|
|
|
|
s.bytes().any(|b| b == 0)
|
2014-09-23 00:14:46 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-05-25 08:46:36 +03:00
|
|
|
|
fn write_metadata(cx: &SharedCrateContext,
|
2016-11-28 18:05:53 -05:00
|
|
|
|
exported_symbols: &NodeSet) -> Vec<u8> {
|
2014-01-24 21:00:31 -08:00
|
|
|
|
use flate;
|
Store metadata separately in rlib files
Right now whenever an rlib file is linked against, all of the metadata from the
rlib is pulled in to the final staticlib or binary. The reason for this is that
the metadata is currently stored in a section of the object file. Note that this
is intentional for dynamic libraries in order to distribute metadata bundled
with static libraries.
This commit alters the situation for rlib libraries to instead store the
metadata in a separate file in the archive. In doing so, when the archive is
passed to the linker, none of the metadata will get pulled into the result
executable. Furthermore, the metadata file is skipped when assembling rlibs into
an archive.
The snag in this implementation comes with multiple output formats. When
generating a dylib, the metadata needs to be in the object file, but when
generating an rlib this needs to be separate. In order to accomplish this, the
metadata variable is inserted into an entirely separate LLVM Module which is
then codegen'd into a different location (foo.metadata.o). This is then linked
into dynamic libraries and silently ignored for rlib files.
While changing how metadata is inserted into archives, I have also stopped
compressing metadata when inserted into rlib files. We have wanted to stop
compressing metadata, but the sections it creates in object file sections are
apparently too large. Thankfully if it's just an arbitrary file it doesn't
matter how large it is.
I have seen massive reductions in executable sizes, as well as staticlib output
sizes (to confirm that this is all working).
2013-12-03 17:41:01 -08:00
|
|
|
|
|
2016-10-19 12:06:46 +11:00
|
|
|
|
#[derive(PartialEq, Eq, PartialOrd, Ord)]
|
|
|
|
|
enum MetadataKind {
|
|
|
|
|
None,
|
|
|
|
|
Uncompressed,
|
|
|
|
|
Compressed
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let kind = cx.sess().crate_types.borrow().iter().map(|ty| {
|
|
|
|
|
match *ty {
|
|
|
|
|
config::CrateTypeExecutable |
|
|
|
|
|
config::CrateTypeStaticlib |
|
|
|
|
|
config::CrateTypeCdylib => MetadataKind::None,
|
|
|
|
|
|
2016-12-23 19:39:20 +13:00
|
|
|
|
config::CrateTypeRlib => MetadataKind::Uncompressed,
|
2016-10-19 12:06:46 +11:00
|
|
|
|
|
|
|
|
|
config::CrateTypeDylib |
|
|
|
|
|
config::CrateTypeProcMacro => MetadataKind::Compressed,
|
|
|
|
|
}
|
|
|
|
|
}).max().unwrap();
|
|
|
|
|
|
|
|
|
|
if kind == MetadataKind::None {
|
2015-11-19 12:36:31 +01:00
|
|
|
|
return Vec::new();
|
2013-12-22 14:40:03 -08:00
|
|
|
|
}
|
2013-06-13 19:19:50 +12:00
|
|
|
|
|
2015-11-21 01:08:09 +02:00
|
|
|
|
let cstore = &cx.tcx().sess.cstore;
|
2015-12-08 15:53:19 -05:00
|
|
|
|
let metadata = cstore.encode_metadata(cx.tcx(),
|
|
|
|
|
cx.export_map(),
|
|
|
|
|
cx.link_meta(),
|
2016-11-28 18:05:53 -05:00
|
|
|
|
exported_symbols);
|
2016-10-19 12:06:46 +11:00
|
|
|
|
if kind == MetadataKind::Uncompressed {
|
|
|
|
|
return metadata;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
assert!(kind == MetadataKind::Compressed);
|
2015-11-21 01:08:09 +02:00
|
|
|
|
let mut compressed = cstore.metadata_encoding_version().to_vec();
|
2015-12-02 17:31:49 -08:00
|
|
|
|
compressed.extend_from_slice(&flate::deflate_bytes(&metadata));
|
2015-11-21 01:08:09 +02:00
|
|
|
|
|
2015-02-18 14:48:57 -05:00
|
|
|
|
let llmeta = C_bytes_in_context(cx.metadata_llcx(), &compressed[..]);
|
2014-11-17 21:39:01 +13:00
|
|
|
|
let llconst = C_struct_in_context(cx.metadata_llcx(), &[llmeta], false);
|
2016-05-16 20:05:43 +03:00
|
|
|
|
let name = cx.metadata_symbol_name();
|
2015-02-17 22:47:40 -08:00
|
|
|
|
let buf = CString::new(name).unwrap();
|
2014-11-25 13:28:35 -08:00
|
|
|
|
let llglobal = unsafe {
|
2015-11-19 12:36:31 +01:00
|
|
|
|
llvm::LLVMAddGlobal(cx.metadata_llmod(), val_ty(llconst).to_ref(), buf.as_ptr())
|
2014-11-25 13:28:35 -08:00
|
|
|
|
};
|
2013-01-10 21:23:07 -08:00
|
|
|
|
unsafe {
|
|
|
|
|
llvm::LLVMSetInitializer(llglobal, llconst);
|
2016-08-14 11:16:28 +03:00
|
|
|
|
let section_name =
|
2015-11-21 01:08:09 +02:00
|
|
|
|
cx.tcx().sess.cstore.metadata_section_name(&cx.sess().target.target);
|
2016-08-14 11:16:28 +03:00
|
|
|
|
let name = CString::new(section_name).unwrap();
|
|
|
|
|
llvm::LLVMSetSection(llglobal, name.as_ptr());
|
|
|
|
|
|
|
|
|
|
// Also generate a .section directive to force no
|
|
|
|
|
// flags, at least for ELF outputs, so that the
|
|
|
|
|
// metadata doesn't get loaded into memory.
|
|
|
|
|
let directive = format!(".section {}", section_name);
|
|
|
|
|
let directive = CString::new(directive).unwrap();
|
|
|
|
|
llvm::LLVMSetModuleInlineAsm(cx.metadata_llmod(), directive.as_ptr())
|
2013-01-10 21:23:07 -08:00
|
|
|
|
}
|
Store metadata separately in rlib files
Right now whenever an rlib file is linked against, all of the metadata from the
rlib is pulled in to the final staticlib or binary. The reason for this is that
the metadata is currently stored in a section of the object file. Note that this
is intentional for dynamic libraries in order to distribute metadata bundled
with static libraries.
This commit alters the situation for rlib libraries to instead store the
metadata in a separate file in the archive. In doing so, when the archive is
passed to the linker, none of the metadata will get pulled into the result
executable. Furthermore, the metadata file is skipped when assembling rlibs into
an archive.
The snag in this implementation comes with multiple output formats. When
generating a dylib, the metadata needs to be in the object file, but when
generating an rlib this needs to be separate. In order to accomplish this, the
metadata variable is inserted into an entirely separate LLVM Module which is
then codegen'd into a different location (foo.metadata.o). This is then linked
into dynamic libraries and silently ignored for rlib files.
While changing how metadata is inserted into archives, I have also stopped
compressing metadata when inserted into rlib files. We have wanted to stop
compressing metadata, but the sections it creates in object file sections are
apparently too large. Thankfully if it's just an arbitrary file it doesn't
matter how large it is.
I have seen massive reductions in executable sizes, as well as staticlib output
sizes (to confirm that this is all working).
2013-12-03 17:41:01 -08:00
|
|
|
|
return metadata;
|
2011-06-27 16:09:28 -07:00
|
|
|
|
}
|
|
|
|
|
|
2014-08-01 10:29:44 -07:00
|
|
|
|
/// Find any symbols that are defined in one compilation unit, but not declared
|
|
|
|
|
/// in any other compilation unit. Give these symbols internal linkage.
|
2016-07-22 10:39:30 -04:00
|
|
|
|
fn internalize_symbols<'a, 'tcx>(sess: &Session,
|
|
|
|
|
ccxs: &CrateContextList<'a, 'tcx>,
|
2016-07-20 07:55:45 -04:00
|
|
|
|
symbol_map: &SymbolMap<'tcx>,
|
2016-11-30 10:03:42 -05:00
|
|
|
|
exported_symbols: &ExportedSymbols) {
|
|
|
|
|
let export_threshold =
|
|
|
|
|
symbol_export::crates_export_threshold(&sess.crate_types.borrow()[..]);
|
|
|
|
|
|
|
|
|
|
let exported_symbols = exported_symbols
|
|
|
|
|
.exported_symbols(LOCAL_CRATE)
|
|
|
|
|
.iter()
|
|
|
|
|
.filter(|&&(_, export_level)| {
|
|
|
|
|
symbol_export::is_below_threshold(export_level, export_threshold)
|
|
|
|
|
})
|
|
|
|
|
.map(|&(ref name, _)| &name[..])
|
|
|
|
|
.collect::<FxHashSet<&str>>();
|
|
|
|
|
|
2016-07-20 07:55:45 -04:00
|
|
|
|
let scx = ccxs.shared();
|
|
|
|
|
let tcx = scx.tcx();
|
|
|
|
|
|
2016-11-30 10:03:42 -05:00
|
|
|
|
let incr_comp = sess.opts.debugging_opts.incremental.is_some();
|
2016-07-22 10:39:30 -04:00
|
|
|
|
|
2016-07-20 07:55:45 -04:00
|
|
|
|
// 'unsafe' because we are holding on to CStr's from the LLVM module within
|
|
|
|
|
// this block.
|
2014-08-01 10:29:44 -07:00
|
|
|
|
unsafe {
|
2016-11-08 14:02:55 +11:00
|
|
|
|
let mut referenced_somewhere = FxHashSet();
|
2014-08-01 10:29:44 -07:00
|
|
|
|
|
2016-07-20 07:55:45 -04:00
|
|
|
|
// Collect all symbols that need to stay externally visible because they
|
2016-11-30 10:03:42 -05:00
|
|
|
|
// are referenced via a declaration in some other codegen unit. In
|
|
|
|
|
// incremental compilation, we don't need to collect. See below for more
|
|
|
|
|
// information.
|
|
|
|
|
if !incr_comp {
|
|
|
|
|
for ccx in ccxs.iter_need_trans() {
|
|
|
|
|
for val in iter_globals(ccx.llmod()).chain(iter_functions(ccx.llmod())) {
|
|
|
|
|
let linkage = llvm::LLVMRustGetLinkage(val);
|
|
|
|
|
// We only care about external declarations (not definitions)
|
|
|
|
|
// and available_externally definitions.
|
|
|
|
|
let is_available_externally =
|
|
|
|
|
linkage == llvm::Linkage::AvailableExternallyLinkage;
|
|
|
|
|
let is_decl = llvm::LLVMIsDeclaration(val) == llvm::True;
|
|
|
|
|
|
|
|
|
|
if is_decl || is_available_externally {
|
|
|
|
|
let symbol_name = CStr::from_ptr(llvm::LLVMGetValueName(val));
|
|
|
|
|
referenced_somewhere.insert(symbol_name);
|
|
|
|
|
}
|
2014-08-01 10:29:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-20 07:55:45 -04:00
|
|
|
|
// Also collect all symbols for which we cannot adjust linkage, because
|
2016-11-30 10:03:42 -05:00
|
|
|
|
// it is fixed by some directive in the source code.
|
|
|
|
|
let (locally_defined_symbols, linkage_fixed_explicitly) = {
|
|
|
|
|
let mut locally_defined_symbols = FxHashSet();
|
|
|
|
|
let mut linkage_fixed_explicitly = FxHashSet();
|
|
|
|
|
|
|
|
|
|
for trans_item in scx.translation_items().borrow().iter() {
|
|
|
|
|
let symbol_name = symbol_map.get_or_compute(scx, *trans_item);
|
|
|
|
|
if trans_item.explicit_linkage(tcx).is_some() {
|
|
|
|
|
linkage_fixed_explicitly.insert(symbol_name.clone());
|
|
|
|
|
}
|
|
|
|
|
locally_defined_symbols.insert(symbol_name);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
(locally_defined_symbols, linkage_fixed_explicitly)
|
|
|
|
|
};
|
2016-07-20 07:55:45 -04:00
|
|
|
|
|
2014-08-01 10:29:44 -07:00
|
|
|
|
// Examine each external definition. If the definition is not used in
|
|
|
|
|
// any other compilation unit, and is not reachable from other crates,
|
|
|
|
|
// then give it internal linkage.
|
2016-07-21 12:49:59 -04:00
|
|
|
|
for ccx in ccxs.iter_need_trans() {
|
2014-08-01 10:29:44 -07:00
|
|
|
|
for val in iter_globals(ccx.llmod()).chain(iter_functions(ccx.llmod())) {
|
2016-09-01 13:52:33 -05:00
|
|
|
|
let linkage = llvm::LLVMRustGetLinkage(val);
|
2016-05-14 05:41:42 +12:00
|
|
|
|
|
2016-09-01 13:52:33 -05:00
|
|
|
|
let is_externally_visible = (linkage == llvm::Linkage::ExternalLinkage) ||
|
|
|
|
|
(linkage == llvm::Linkage::LinkOnceODRLinkage) ||
|
|
|
|
|
(linkage == llvm::Linkage::WeakODRLinkage);
|
2016-11-30 10:03:42 -05:00
|
|
|
|
|
|
|
|
|
if !is_externally_visible {
|
|
|
|
|
// This symbol is not visible outside of its codegen unit,
|
|
|
|
|
// so there is nothing to do for it.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let name_cstr = CStr::from_ptr(llvm::LLVMGetValueName(val));
|
|
|
|
|
let name_str = name_cstr.to_str().unwrap();
|
|
|
|
|
|
|
|
|
|
if exported_symbols.contains(&name_str) {
|
|
|
|
|
// This symbol is explicitly exported, so we can't
|
|
|
|
|
// mark it as internal or hidden.
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let is_declaration = llvm::LLVMIsDeclaration(val) == llvm::True;
|
|
|
|
|
|
|
|
|
|
if is_declaration {
|
|
|
|
|
if locally_defined_symbols.contains(name_str) {
|
|
|
|
|
// Only mark declarations from the current crate as hidden.
|
|
|
|
|
// Otherwise we would mark things as hidden that are
|
|
|
|
|
// imported from other crates or native libraries.
|
|
|
|
|
llvm::LLVMRustSetVisibility(val, llvm::Visibility::Hidden);
|
|
|
|
|
}
|
|
|
|
|
} else {
|
|
|
|
|
let has_fixed_linkage = linkage_fixed_explicitly.contains(name_str);
|
|
|
|
|
|
|
|
|
|
if !has_fixed_linkage {
|
|
|
|
|
// In incremental compilation mode, we can't be sure that
|
|
|
|
|
// we saw all references because we don't know what's in
|
|
|
|
|
// cached compilation units, so we always assume that the
|
|
|
|
|
// given item has been referenced.
|
|
|
|
|
if incr_comp || referenced_somewhere.contains(&name_cstr) {
|
|
|
|
|
llvm::LLVMRustSetVisibility(val, llvm::Visibility::Hidden);
|
|
|
|
|
} else {
|
|
|
|
|
llvm::LLVMRustSetLinkage(val, llvm::Linkage::InternalLinkage);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
llvm::LLVMSetDLLStorageClass(val, llvm::DLLStorageClass::Default);
|
2016-05-14 05:41:42 +12:00
|
|
|
|
llvm::UnsetComdat(val);
|
|
|
|
|
}
|
2014-08-01 10:29:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-08-21 00:41:07 -07:00
|
|
|
|
}
|
2014-08-01 10:29:44 -07:00
|
|
|
|
|
2015-08-21 00:41:07 -07:00
|
|
|
|
// Create a `__imp_<symbol> = &symbol` global for every public static `symbol`.
|
|
|
|
|
// This is required to satisfy `dllimport` references to static data in .rlibs
|
|
|
|
|
// when using MSVC linker. We do this only for data, as linker can fix up
|
|
|
|
|
// code references on its own.
|
|
|
|
|
// See #26591, #27438
|
2016-05-05 14:14:41 -04:00
|
|
|
|
fn create_imps(cx: &CrateContextList) {
|
2015-09-29 16:26:34 -07:00
|
|
|
|
// The x86 ABI seems to require that leading underscores are added to symbol
|
|
|
|
|
// names, so we need an extra underscore on 32-bit. There's also a leading
|
|
|
|
|
// '\x01' here which disables LLVM's symbol mangling (e.g. no extra
|
|
|
|
|
// underscores added in front).
|
2016-05-05 14:14:41 -04:00
|
|
|
|
let prefix = if cx.shared().sess().target.target.target_pointer_width == "32" {
|
2015-09-29 16:26:34 -07:00
|
|
|
|
"\x01__imp__"
|
|
|
|
|
} else {
|
|
|
|
|
"\x01__imp_"
|
|
|
|
|
};
|
2015-08-21 00:41:07 -07:00
|
|
|
|
unsafe {
|
2016-07-21 12:49:59 -04:00
|
|
|
|
for ccx in cx.iter_need_trans() {
|
2015-08-21 00:41:07 -07:00
|
|
|
|
let exported: Vec<_> = iter_globals(ccx.llmod())
|
2015-11-19 12:36:31 +01:00
|
|
|
|
.filter(|&val| {
|
2016-09-01 13:52:33 -05:00
|
|
|
|
llvm::LLVMRustGetLinkage(val) ==
|
|
|
|
|
llvm::Linkage::ExternalLinkage &&
|
2015-11-19 12:36:31 +01:00
|
|
|
|
llvm::LLVMIsDeclaration(val) == 0
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
2015-08-21 00:41:07 -07:00
|
|
|
|
|
|
|
|
|
let i8p_ty = Type::i8p(&ccx);
|
|
|
|
|
for val in exported {
|
|
|
|
|
let name = CStr::from_ptr(llvm::LLVMGetValueName(val));
|
2015-09-29 16:26:34 -07:00
|
|
|
|
let mut imp_name = prefix.as_bytes().to_vec();
|
|
|
|
|
imp_name.extend(name.to_bytes());
|
2015-08-21 00:41:07 -07:00
|
|
|
|
let imp_name = CString::new(imp_name).unwrap();
|
2015-11-19 12:36:31 +01:00
|
|
|
|
let imp = llvm::LLVMAddGlobal(ccx.llmod(),
|
|
|
|
|
i8p_ty.to_ref(),
|
2015-08-21 00:41:07 -07:00
|
|
|
|
imp_name.as_ptr() as *const _);
|
2015-09-29 16:26:34 -07:00
|
|
|
|
let init = llvm::LLVMConstBitCast(val, i8p_ty.to_ref());
|
|
|
|
|
llvm::LLVMSetInitializer(imp, init);
|
2016-09-01 13:52:33 -05:00
|
|
|
|
llvm::LLVMRustSetLinkage(imp, llvm::Linkage::ExternalLinkage);
|
2015-08-21 00:41:07 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
2014-08-01 10:29:44 -07:00
|
|
|
|
}
|
2015-08-21 00:41:07 -07:00
|
|
|
|
}
|
2014-08-01 10:29:44 -07:00
|
|
|
|
|
2015-08-21 00:41:07 -07:00
|
|
|
|
struct ValueIter {
|
|
|
|
|
cur: ValueRef,
|
|
|
|
|
step: unsafe extern "C" fn(ValueRef) -> ValueRef,
|
|
|
|
|
}
|
2014-08-01 10:29:44 -07:00
|
|
|
|
|
2015-08-21 00:41:07 -07:00
|
|
|
|
impl Iterator for ValueIter {
|
|
|
|
|
type Item = ValueRef;
|
2014-08-01 10:29:44 -07:00
|
|
|
|
|
2015-08-21 00:41:07 -07:00
|
|
|
|
fn next(&mut self) -> Option<ValueRef> {
|
|
|
|
|
let old = self.cur;
|
|
|
|
|
if !old.is_null() {
|
2015-10-17 20:15:26 -04:00
|
|
|
|
self.cur = unsafe { (self.step)(old) };
|
2015-08-21 00:41:07 -07:00
|
|
|
|
Some(old)
|
|
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
2014-08-01 10:29:44 -07:00
|
|
|
|
}
|
2015-08-21 00:41:07 -07:00
|
|
|
|
}
|
2014-08-01 10:29:44 -07:00
|
|
|
|
|
2015-08-21 00:41:07 -07:00
|
|
|
|
fn iter_globals(llmod: llvm::ModuleRef) -> ValueIter {
|
|
|
|
|
unsafe {
|
|
|
|
|
ValueIter {
|
|
|
|
|
cur: llvm::LLVMGetFirstGlobal(llmod),
|
|
|
|
|
step: llvm::LLVMGetNextGlobal,
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
2015-01-01 23:54:03 -05:00
|
|
|
|
|
2015-08-21 00:41:07 -07:00
|
|
|
|
fn iter_functions(llmod: llvm::ModuleRef) -> ValueIter {
|
|
|
|
|
unsafe {
|
|
|
|
|
ValueIter {
|
|
|
|
|
cur: llvm::LLVMGetFirstFunction(llmod),
|
|
|
|
|
step: llvm::LLVMGetNextFunction,
|
2014-08-01 10:29:44 -07:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 17:19:08 -07:00
|
|
|
|
/// The context provided lists a set of reachable ids as calculated by
|
|
|
|
|
/// middle::reachable, but this contains far more ids and symbols than we're
|
|
|
|
|
/// actually exposing from the object file. This function will filter the set in
|
|
|
|
|
/// the context to the set of ids which correspond to symbols that are exposed
|
|
|
|
|
/// from the object file being generated.
|
|
|
|
|
///
|
|
|
|
|
/// This list is later used by linkers to determine the set of symbols needed to
|
|
|
|
|
/// be exposed from a dynamic library and it's also encoded into the metadata.
|
2016-11-28 18:05:53 -05:00
|
|
|
|
pub fn find_exported_symbols(tcx: TyCtxt, reachable: NodeSet) -> NodeSet {
|
2016-05-19 12:35:36 -04:00
|
|
|
|
reachable.into_iter().filter(|&id| {
|
2015-07-28 17:19:08 -07:00
|
|
|
|
// Next, we want to ignore some FFI functions that are not exposed from
|
|
|
|
|
// this crate. Reachable FFI functions can be lumped into two
|
|
|
|
|
// categories:
|
|
|
|
|
//
|
|
|
|
|
// 1. Those that are included statically via a static library
|
|
|
|
|
// 2. Those included otherwise (e.g. dynamically or via a framework)
|
|
|
|
|
//
|
|
|
|
|
// Although our LLVM module is not literally emitting code for the
|
|
|
|
|
// statically included symbols, it's an export of our library which
|
|
|
|
|
// needs to be passed on to the linker and encoded in the metadata.
|
|
|
|
|
//
|
|
|
|
|
// As a result, if this id is an FFI item (foreign item) then we only
|
|
|
|
|
// let it through if it's included statically.
|
2016-05-19 12:35:36 -04:00
|
|
|
|
match tcx.map.get(id) {
|
2015-07-31 00:04:06 -07:00
|
|
|
|
hir_map::NodeForeignItem(..) => {
|
2016-11-23 16:09:51 -08:00
|
|
|
|
let def_id = tcx.map.local_def_id(id);
|
|
|
|
|
tcx.sess.cstore.is_statically_included_foreign_item(def_id)
|
2015-07-28 17:19:08 -07:00
|
|
|
|
}
|
2016-05-12 19:52:38 +03:00
|
|
|
|
|
|
|
|
|
// Only consider nodes that actually have exported symbols.
|
|
|
|
|
hir_map::NodeItem(&hir::Item {
|
|
|
|
|
node: hir::ItemStatic(..), .. }) |
|
|
|
|
|
hir_map::NodeItem(&hir::Item {
|
|
|
|
|
node: hir::ItemFn(..), .. }) |
|
|
|
|
|
hir_map::NodeImplItem(&hir::ImplItem {
|
2016-05-25 08:46:36 +03:00
|
|
|
|
node: hir::ImplItemKind::Method(..), .. }) => {
|
2016-05-19 12:35:36 -04:00
|
|
|
|
let def_id = tcx.map.local_def_id(id);
|
2016-11-10 16:49:53 +02:00
|
|
|
|
let generics = tcx.item_generics(def_id);
|
2016-09-15 11:40:16 -04:00
|
|
|
|
let attributes = tcx.get_attrs(def_id);
|
|
|
|
|
(generics.parent_types == 0 && generics.types.is_empty()) &&
|
|
|
|
|
// Functions marked with #[inline] are only ever translated
|
|
|
|
|
// with "internal" linkage and are never exported.
|
|
|
|
|
!attr::requests_inline(&attributes[..])
|
2016-05-25 08:46:36 +03:00
|
|
|
|
}
|
2016-05-12 19:52:38 +03:00
|
|
|
|
|
|
|
|
|
_ => false
|
2015-07-28 17:19:08 -07:00
|
|
|
|
}
|
|
|
|
|
}).collect()
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-03 05:23:22 +03:00
|
|
|
|
pub fn trans_crate<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>,
|
2016-08-19 07:23:36 -04:00
|
|
|
|
analysis: ty::CrateAnalysis,
|
2016-08-23 07:47:14 -04:00
|
|
|
|
incremental_hashes_map: &IncrementalHashesMap)
|
2016-05-03 04:56:42 +03:00
|
|
|
|
-> CrateTranslation {
|
2015-12-22 16:35:02 -05:00
|
|
|
|
let _task = tcx.dep_graph.in_task(DepNode::TransCrate);
|
|
|
|
|
|
|
|
|
|
// Be careful with this krate: obviously it gives access to the
|
|
|
|
|
// entire contents of the krate. So if you push any subtasks of
|
|
|
|
|
// `TransCrate`, you need to be careful to register "reads" of the
|
|
|
|
|
// particular items that will be processed.
|
2014-09-07 20:09:06 +03:00
|
|
|
|
let krate = tcx.map.krate();
|
2014-03-15 22:29:34 +02:00
|
|
|
|
|
2015-12-22 16:35:02 -05:00
|
|
|
|
let ty::CrateAnalysis { export_map, reachable, name, .. } = analysis;
|
2016-11-28 18:05:53 -05:00
|
|
|
|
let exported_symbols = find_exported_symbols(tcx, reachable);
|
2015-12-22 16:35:02 -05:00
|
|
|
|
|
2015-01-06 00:56:30 -05:00
|
|
|
|
let check_overflow = if let Some(v) = tcx.sess.opts.debugging_opts.force_overflow_checks {
|
|
|
|
|
v
|
|
|
|
|
} else {
|
2015-03-02 14:51:24 -08:00
|
|
|
|
tcx.sess.opts.debug_assertions
|
2015-01-06 00:56:30 -05:00
|
|
|
|
};
|
|
|
|
|
|
2016-10-29 13:19:59 +03:00
|
|
|
|
let link_meta = link::build_link_meta(incremental_hashes_map, &name);
|
2011-12-05 14:56:11 +08:00
|
|
|
|
|
2016-05-05 14:14:41 -04:00
|
|
|
|
let shared_ccx = SharedCrateContext::new(tcx,
|
2014-12-18 20:27:17 +02:00
|
|
|
|
export_map,
|
2014-07-16 11:27:57 -07:00
|
|
|
|
link_meta.clone(),
|
2016-11-28 18:05:53 -05:00
|
|
|
|
exported_symbols,
|
2016-08-16 17:41:38 +03:00
|
|
|
|
check_overflow);
|
2016-05-25 08:46:36 +03:00
|
|
|
|
// Translate the metadata.
|
|
|
|
|
let metadata = time(tcx.sess.time_passes(), "write metadata", || {
|
2016-11-28 18:05:53 -05:00
|
|
|
|
write_metadata(&shared_ccx, shared_ccx.exported_symbols())
|
2016-05-25 08:46:36 +03:00
|
|
|
|
});
|
|
|
|
|
|
|
|
|
|
let metadata_module = ModuleTranslation {
|
2016-05-13 20:48:32 -04:00
|
|
|
|
name: "metadata".to_string(),
|
2016-07-21 12:49:59 -04:00
|
|
|
|
symbol_name_hash: 0, // we always rebuild metadata, at least for now
|
|
|
|
|
source: ModuleSource::Translated(ModuleLlvm {
|
|
|
|
|
llcx: shared_ccx.metadata_llcx(),
|
|
|
|
|
llmod: shared_ccx.metadata_llmod(),
|
|
|
|
|
}),
|
2016-05-25 08:46:36 +03:00
|
|
|
|
};
|
|
|
|
|
let no_builtins = attr::contains_name(&krate.attrs, "no_builtins");
|
|
|
|
|
|
2016-05-26 12:18:39 -04:00
|
|
|
|
// Run the translation item collector and partition the collected items into
|
|
|
|
|
// codegen units.
|
|
|
|
|
let (codegen_units, symbol_map) = collect_and_partition_translation_items(&shared_ccx);
|
2016-05-06 20:02:09 -04:00
|
|
|
|
|
2016-05-26 08:59:58 -04:00
|
|
|
|
let symbol_map = Rc::new(symbol_map);
|
2016-05-05 14:14:41 -04:00
|
|
|
|
|
2016-12-09 17:13:13 -05:00
|
|
|
|
let previous_work_products = trans_reuse_previous_work_products(&shared_ccx,
|
2016-07-21 12:49:59 -04:00
|
|
|
|
&codegen_units,
|
|
|
|
|
&symbol_map);
|
|
|
|
|
|
2016-05-26 08:59:58 -04:00
|
|
|
|
let crate_context_list = CrateContextList::new(&shared_ccx,
|
|
|
|
|
codegen_units,
|
2016-07-21 12:49:59 -04:00
|
|
|
|
previous_work_products,
|
2016-05-26 08:59:58 -04:00
|
|
|
|
symbol_map.clone());
|
2016-07-21 12:49:59 -04:00
|
|
|
|
let modules: Vec<_> = crate_context_list.iter_all()
|
|
|
|
|
.map(|ccx| {
|
|
|
|
|
let source = match ccx.previous_work_product() {
|
|
|
|
|
Some(buf) => ModuleSource::Preexisting(buf.clone()),
|
|
|
|
|
None => ModuleSource::Translated(ModuleLlvm {
|
|
|
|
|
llcx: ccx.llcx(),
|
|
|
|
|
llmod: ccx.llmod(),
|
|
|
|
|
}),
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
ModuleTranslation {
|
|
|
|
|
name: String::from(ccx.codegen_unit().name()),
|
2016-12-09 17:13:13 -05:00
|
|
|
|
symbol_name_hash: ccx.codegen_unit()
|
|
|
|
|
.compute_symbol_name_hash(&shared_ccx,
|
|
|
|
|
&symbol_map),
|
2016-07-21 12:49:59 -04:00
|
|
|
|
source: source,
|
|
|
|
|
}
|
2016-05-13 20:48:32 -04:00
|
|
|
|
})
|
2016-05-25 08:46:36 +03:00
|
|
|
|
.collect();
|
|
|
|
|
|
2016-07-21 12:50:15 -04:00
|
|
|
|
assert_module_sources::assert_module_sources(tcx, &modules);
|
|
|
|
|
|
2016-05-25 08:46:36 +03:00
|
|
|
|
// Skip crate items and just output metadata in -Z no-trans mode.
|
2016-10-26 11:14:02 +13:00
|
|
|
|
if tcx.sess.opts.debugging_opts.no_trans ||
|
2016-12-23 19:39:20 +13:00
|
|
|
|
tcx.sess.opts.output_types.contains_key(&config::OutputType::Metadata) {
|
2016-11-30 10:03:42 -05:00
|
|
|
|
let linker_info = LinkerInfo::new(&shared_ccx, &ExportedSymbols::empty());
|
2016-05-25 08:46:36 +03:00
|
|
|
|
return CrateTranslation {
|
|
|
|
|
modules: modules,
|
|
|
|
|
metadata_module: metadata_module,
|
|
|
|
|
link: link_meta,
|
|
|
|
|
metadata: metadata,
|
2016-11-30 10:03:42 -05:00
|
|
|
|
exported_symbols: ExportedSymbols::empty(),
|
2016-05-25 08:46:36 +03:00
|
|
|
|
no_builtins: no_builtins,
|
2016-10-31 09:36:30 -07:00
|
|
|
|
linker_info: linker_info,
|
|
|
|
|
windows_subsystem: None,
|
2016-05-25 08:46:36 +03:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 20:02:09 -04:00
|
|
|
|
// Instantiate translation items without filling out definitions yet...
|
2016-07-21 12:49:59 -04:00
|
|
|
|
for ccx in crate_context_list.iter_need_trans() {
|
|
|
|
|
let cgu = ccx.codegen_unit();
|
|
|
|
|
let trans_items = cgu.items_in_deterministic_order(tcx, &symbol_map);
|
2016-05-26 11:43:53 -04:00
|
|
|
|
|
2016-07-21 12:49:59 -04:00
|
|
|
|
tcx.dep_graph.with_task(cgu.work_product_dep_node(), || {
|
|
|
|
|
for (trans_item, linkage) in trans_items {
|
|
|
|
|
trans_item.predefine(&ccx, linkage);
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-05-06 20:02:09 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// ... and now that we have everything pre-defined, fill out those definitions.
|
2016-07-21 12:49:59 -04:00
|
|
|
|
for ccx in crate_context_list.iter_need_trans() {
|
|
|
|
|
let cgu = ccx.codegen_unit();
|
|
|
|
|
let trans_items = cgu.items_in_deterministic_order(tcx, &symbol_map);
|
|
|
|
|
tcx.dep_graph.with_task(cgu.work_product_dep_node(), || {
|
|
|
|
|
for (trans_item, _) in trans_items {
|
|
|
|
|
trans_item.define(&ccx);
|
|
|
|
|
}
|
2015-11-02 14:46:39 +01:00
|
|
|
|
|
2016-07-21 12:49:59 -04:00
|
|
|
|
// If this codegen unit contains the main function, also create the
|
|
|
|
|
// wrapper here
|
|
|
|
|
maybe_create_entry_wrapper(&ccx);
|
2016-03-16 15:00:20 -04:00
|
|
|
|
|
2016-07-21 12:49:59 -04:00
|
|
|
|
// Run replace-all-uses-with for statics that need it
|
|
|
|
|
for &(old_g, new_g) in ccx.statics_to_rauw().borrow().iter() {
|
|
|
|
|
unsafe {
|
|
|
|
|
let bitcast = llvm::LLVMConstPointerCast(new_g, llvm::LLVMTypeOf(old_g));
|
|
|
|
|
llvm::LLVMReplaceAllUsesWith(old_g, bitcast);
|
|
|
|
|
llvm::LLVMDeleteGlobal(old_g);
|
|
|
|
|
}
|
2015-06-28 10:36:46 -07:00
|
|
|
|
}
|
2016-05-26 12:18:39 -04:00
|
|
|
|
|
2016-07-21 12:49:59 -04:00
|
|
|
|
// Finalize debuginfo
|
|
|
|
|
if ccx.sess().opts.debuginfo != NoDebugInfo {
|
|
|
|
|
debuginfo::finalize(&ccx);
|
|
|
|
|
}
|
|
|
|
|
});
|
2016-05-26 12:18:39 -04:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
symbol_names_test::report_symbol_names(&shared_ccx);
|
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
|
if shared_ccx.sess().trans_stats() {
|
|
|
|
|
let stats = shared_ccx.stats();
|
2014-01-09 21:06:55 +11:00
|
|
|
|
println!("--- trans stats ---");
|
2014-07-16 11:27:57 -07:00
|
|
|
|
println!("n_glues_created: {}", stats.n_glues_created.get());
|
|
|
|
|
println!("n_null_glues: {}", stats.n_null_glues.get());
|
|
|
|
|
println!("n_real_glues: {}", stats.n_real_glues.get());
|
|
|
|
|
|
|
|
|
|
println!("n_fns: {}", stats.n_fns.get());
|
|
|
|
|
println!("n_inlines: {}", stats.n_inlines.get());
|
|
|
|
|
println!("n_closures: {}", stats.n_closures.get());
|
2014-01-09 21:06:55 +11:00
|
|
|
|
println!("fn stats:");
|
2014-11-10 12:27:56 -08:00
|
|
|
|
stats.fn_stats.borrow_mut().sort_by(|&(_, insns_a), &(_, insns_b)| {
|
2014-03-15 22:29:34 +02:00
|
|
|
|
insns_b.cmp(&insns_a)
|
|
|
|
|
});
|
2015-06-11 13:56:07 +01:00
|
|
|
|
for tuple in stats.fn_stats.borrow().iter() {
|
2014-03-15 22:29:34 +02:00
|
|
|
|
match *tuple {
|
2014-11-10 12:27:56 -08:00
|
|
|
|
(ref name, insns) => {
|
|
|
|
|
println!("{} insns, {}", insns, *name);
|
2013-07-08 11:05:52 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
2013-06-28 11:15:34 -07:00
|
|
|
|
}
|
2013-06-13 14:49:01 +12:00
|
|
|
|
}
|
2016-05-26 12:18:39 -04:00
|
|
|
|
|
2014-07-16 11:27:57 -07:00
|
|
|
|
if shared_ccx.sess().count_llvm_insns() {
|
2015-06-11 13:56:07 +01:00
|
|
|
|
for (k, v) in shared_ccx.stats().llvm_insns.borrow().iter() {
|
2014-11-17 11:29:38 -08:00
|
|
|
|
println!("{:7} {}", *v, *k);
|
2013-06-17 16:23:24 +12:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2015-07-28 17:19:08 -07:00
|
|
|
|
let sess = shared_ccx.sess();
|
2016-05-16 20:05:43 +03:00
|
|
|
|
|
2016-11-30 10:03:42 -05:00
|
|
|
|
let exported_symbols = ExportedSymbols::compute_from(&shared_ccx,
|
|
|
|
|
&symbol_map);
|
2013-06-13 21:25:12 -07:00
|
|
|
|
|
2016-11-30 10:03:42 -05:00
|
|
|
|
// Now that we have all symbols that are exported from the CGUs of this
|
|
|
|
|
// crate, we can run the `internalize_symbols` pass.
|
2016-07-20 07:55:45 -04:00
|
|
|
|
time(shared_ccx.sess().time_passes(), "internalize symbols", || {
|
2016-07-22 10:39:30 -04:00
|
|
|
|
internalize_symbols(sess,
|
|
|
|
|
&crate_context_list,
|
2016-07-20 07:55:45 -04:00
|
|
|
|
&symbol_map,
|
2016-11-30 10:03:42 -05:00
|
|
|
|
&exported_symbols);
|
2016-07-20 07:55:45 -04:00
|
|
|
|
});
|
2014-08-01 10:29:44 -07:00
|
|
|
|
|
2016-11-15 17:48:07 +01:00
|
|
|
|
if tcx.sess.opts.debugging_opts.print_type_sizes {
|
|
|
|
|
gather_type_sizes(tcx);
|
|
|
|
|
}
|
|
|
|
|
|
2015-08-21 00:41:07 -07:00
|
|
|
|
if sess.target.target.options.is_like_msvc &&
|
2015-09-25 18:53:14 -07:00
|
|
|
|
sess.crate_types.borrow().iter().any(|ct| *ct == config::CrateTypeRlib) {
|
2016-05-05 14:14:41 -04:00
|
|
|
|
create_imps(&crate_context_list);
|
2015-08-21 00:41:07 -07:00
|
|
|
|
}
|
|
|
|
|
|
2016-11-28 18:05:53 -05:00
|
|
|
|
let linker_info = LinkerInfo::new(&shared_ccx, &exported_symbols);
|
2016-05-13 20:48:32 -04:00
|
|
|
|
|
2016-10-31 09:36:30 -07:00
|
|
|
|
let subsystem = attr::first_attr_value_str_by_name(&krate.attrs,
|
|
|
|
|
"windows_subsystem");
|
|
|
|
|
let windows_subsystem = subsystem.map(|subsystem| {
|
|
|
|
|
if subsystem != "windows" && subsystem != "console" {
|
|
|
|
|
tcx.sess.fatal(&format!("invalid windows subsystem `{}`, only \
|
|
|
|
|
`windows` and `console` are allowed",
|
|
|
|
|
subsystem));
|
|
|
|
|
}
|
|
|
|
|
subsystem.to_string()
|
|
|
|
|
});
|
|
|
|
|
|
2015-06-14 01:49:28 +03:00
|
|
|
|
CrateTranslation {
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 10:52:52 -07:00
|
|
|
|
modules: modules,
|
2014-03-15 22:29:34 +02:00
|
|
|
|
metadata_module: metadata_module,
|
run optimization and codegen on worker threads
Refactor the code in `llvm::back` that invokes LLVM optimization and codegen
passes so that it can be called from worker threads. (Previously, it used
`&Session` extensively, and `Session` is not `Share`.) The new code can handle
multiple compilation units, by compiling each unit to `crate.0.o`, `crate.1.o`,
etc., and linking together all the `crate.N.o` files into a single `crate.o`
using `ld -r`. The later linking steps can then be run unchanged.
The new code preserves the behavior of `--emit`/`-o` when building a single
compilation unit. With multiple compilation units, the `--emit=asm/ir/bc`
options produce multiple files, so combinations like `--emit=ir -o foo.ll` will
not actually produce `foo.ll` (they instead produce several `foo.N.ll` files).
The new code supports `-Z lto` only when using a single compilation unit.
Compiling with multiple compilation units and `-Z lto` will produce an error.
(I can't think of any good reason to do such a thing.) Linking with `-Z lto`
against a library that was built as multiple compilation units will also fail,
because the rlib does not contain a `crate.bytecode.deflate` file. This could
be supported in the future by linking together the `crate.N.bc` files produced
when compiling the library into a single `crate.bc`, or by making the LTO code
support multiple `crate.N.bytecode.deflate` files.
2014-07-17 10:52:52 -07:00
|
|
|
|
link: link_meta,
|
Store metadata separately in rlib files
Right now whenever an rlib file is linked against, all of the metadata from the
rlib is pulled in to the final staticlib or binary. The reason for this is that
the metadata is currently stored in a section of the object file. Note that this
is intentional for dynamic libraries in order to distribute metadata bundled
with static libraries.
This commit alters the situation for rlib libraries to instead store the
metadata in a separate file in the archive. In doing so, when the archive is
passed to the linker, none of the metadata will get pulled into the result
executable. Furthermore, the metadata file is skipped when assembling rlibs into
an archive.
The snag in this implementation comes with multiple output formats. When
generating a dylib, the metadata needs to be in the object file, but when
generating an rlib this needs to be separate. In order to accomplish this, the
metadata variable is inserted into an entirely separate LLVM Module which is
then codegen'd into a different location (foo.metadata.o). This is then linked
into dynamic libraries and silently ignored for rlib files.
While changing how metadata is inserted into archives, I have also stopped
compressing metadata when inserted into rlib files. We have wanted to stop
compressing metadata, but the sections it creates in object file sections are
apparently too large. Thankfully if it's just an arbitrary file it doesn't
matter how large it is.
I have seen massive reductions in executable sizes, as well as staticlib output
sizes (to confirm that this is all working).
2013-12-03 17:41:01 -08:00
|
|
|
|
metadata: metadata,
|
2016-11-28 18:05:53 -05:00
|
|
|
|
exported_symbols: exported_symbols,
|
2014-05-14 11:24:12 -07:00
|
|
|
|
no_builtins: no_builtins,
|
2016-10-31 09:36:30 -07:00
|
|
|
|
linker_info: linker_info,
|
|
|
|
|
windows_subsystem: windows_subsystem,
|
2015-06-14 01:49:28 +03:00
|
|
|
|
}
|
2013-06-13 21:25:12 -07:00
|
|
|
|
}
|
2015-11-18 05:38:50 -05:00
|
|
|
|
|
2016-11-15 17:48:07 +01:00
|
|
|
|
fn gather_type_sizes<'a, 'tcx>(tcx: TyCtxt<'a, 'tcx, 'tcx>) {
|
|
|
|
|
let layout_cache = tcx.layout_cache.borrow();
|
|
|
|
|
for (ty, layout) in layout_cache.iter() {
|
|
|
|
|
|
|
|
|
|
// (delay format until we actually need it)
|
|
|
|
|
let record = |kind, opt_discr_size, variants| {
|
|
|
|
|
let type_desc = format!("{:?}", ty);
|
|
|
|
|
let overall_size = layout.size(&tcx.data_layout);
|
|
|
|
|
let align = layout.align(&tcx.data_layout);
|
|
|
|
|
tcx.sess.code_stats.borrow_mut().record_type_size(kind,
|
|
|
|
|
type_desc,
|
|
|
|
|
align,
|
|
|
|
|
overall_size,
|
|
|
|
|
opt_discr_size,
|
|
|
|
|
variants);
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let (adt_def, substs) = match ty.sty {
|
|
|
|
|
ty::TyAdt(ref adt_def, substs) => {
|
|
|
|
|
debug!("print-type-size t: `{:?}` process adt", ty);
|
|
|
|
|
(adt_def, substs)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
ty::TyClosure(..) => {
|
|
|
|
|
debug!("print-type-size t: `{:?}` record closure", ty);
|
|
|
|
|
record(DataTypeKind::Closure, None, vec![]);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
_ => {
|
|
|
|
|
debug!("print-type-size t: `{:?}` skip non-nominal", ty);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let adt_kind = adt_def.adt_kind();
|
|
|
|
|
|
|
|
|
|
let build_field_info = |(field_name, field_ty): (ast::Name, Ty), offset: &layout::Size| {
|
|
|
|
|
match layout_cache.get(&field_ty) {
|
|
|
|
|
None => bug!("no layout found for field {} type: `{:?}`", field_name, field_ty),
|
|
|
|
|
Some(field_layout) => {
|
|
|
|
|
session::FieldInfo {
|
|
|
|
|
name: field_name.to_string(),
|
|
|
|
|
offset: offset.bytes(),
|
|
|
|
|
size: field_layout.size(&tcx.data_layout).bytes(),
|
|
|
|
|
align: field_layout.align(&tcx.data_layout).abi(),
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
let build_primitive_info = |name: ast::Name, value: &layout::Primitive| {
|
|
|
|
|
session::VariantInfo {
|
|
|
|
|
name: Some(name.to_string()),
|
|
|
|
|
kind: session::SizeKind::Exact,
|
|
|
|
|
align: value.align(&tcx.data_layout).abi(),
|
|
|
|
|
size: value.size(&tcx.data_layout).bytes(),
|
|
|
|
|
fields: vec![],
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
enum Fields<'a> {
|
|
|
|
|
WithDiscrim(&'a layout::Struct),
|
|
|
|
|
NoDiscrim(&'a layout::Struct),
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let build_variant_info = |n: Option<ast::Name>, flds: &[(ast::Name, Ty)], layout: Fields| {
|
|
|
|
|
let (s, field_offsets) = match layout {
|
|
|
|
|
Fields::WithDiscrim(s) => (s, &s.offsets[1..]),
|
|
|
|
|
Fields::NoDiscrim(s) => (s, &s.offsets[0..]),
|
|
|
|
|
};
|
|
|
|
|
let field_info: Vec<_> = flds.iter()
|
|
|
|
|
.zip(field_offsets.iter())
|
|
|
|
|
.map(|(&field_name_ty, offset)| build_field_info(field_name_ty, offset))
|
|
|
|
|
.collect();
|
|
|
|
|
|
|
|
|
|
session::VariantInfo {
|
|
|
|
|
name: n.map(|n|n.to_string()),
|
|
|
|
|
kind: if s.sized {
|
|
|
|
|
session::SizeKind::Exact
|
|
|
|
|
} else {
|
|
|
|
|
session::SizeKind::Min
|
|
|
|
|
},
|
|
|
|
|
align: s.align.abi(),
|
|
|
|
|
size: s.min_size.bytes(),
|
|
|
|
|
fields: field_info,
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
match **layout {
|
|
|
|
|
Layout::StructWrappedNullablePointer { nonnull: ref variant_layout,
|
|
|
|
|
nndiscr,
|
2016-12-02 19:24:24 -05:00
|
|
|
|
discrfield: _,
|
|
|
|
|
discrfield_source: _ } => {
|
2016-11-15 17:48:07 +01:00
|
|
|
|
debug!("print-type-size t: `{:?}` adt struct-wrapped nullable nndiscr {} is {:?}",
|
|
|
|
|
ty, nndiscr, variant_layout);
|
|
|
|
|
let variant_def = &adt_def.variants[nndiscr as usize];
|
|
|
|
|
let fields: Vec<_> = variant_def.fields.iter()
|
|
|
|
|
.map(|field_def| (field_def.name, field_def.ty(tcx, substs)))
|
|
|
|
|
.collect();
|
|
|
|
|
record(adt_kind.into(),
|
|
|
|
|
None,
|
|
|
|
|
vec![build_variant_info(Some(variant_def.name),
|
|
|
|
|
&fields,
|
|
|
|
|
Fields::NoDiscrim(variant_layout))]);
|
|
|
|
|
}
|
|
|
|
|
Layout::RawNullablePointer { nndiscr, value } => {
|
|
|
|
|
debug!("print-type-size t: `{:?}` adt raw nullable nndiscr {} is {:?}",
|
|
|
|
|
ty, nndiscr, value);
|
|
|
|
|
let variant_def = &adt_def.variants[nndiscr as usize];
|
|
|
|
|
record(adt_kind.into(), None,
|
|
|
|
|
vec![build_primitive_info(variant_def.name, &value)]);
|
|
|
|
|
}
|
|
|
|
|
Layout::Univariant { variant: ref variant_layout, non_zero: _ } => {
|
|
|
|
|
let variant_names = || {
|
|
|
|
|
adt_def.variants.iter().map(|v|format!("{}", v.name)).collect::<Vec<_>>()
|
|
|
|
|
};
|
|
|
|
|
debug!("print-type-size t: `{:?}` adt univariant {:?} variants: {:?}",
|
|
|
|
|
ty, variant_layout, variant_names());
|
|
|
|
|
assert!(adt_def.variants.len() <= 1,
|
|
|
|
|
"univariant with variants {:?}", variant_names());
|
|
|
|
|
if adt_def.variants.len() == 1 {
|
|
|
|
|
let variant_def = &adt_def.variants[0];
|
|
|
|
|
let fields: Vec<_> = variant_def.fields.iter()
|
|
|
|
|
.map(|field_def| (field_def.name, field_def.ty(tcx, substs)))
|
|
|
|
|
.collect();
|
|
|
|
|
record(adt_kind.into(),
|
|
|
|
|
None,
|
|
|
|
|
vec![build_variant_info(Some(variant_def.name),
|
|
|
|
|
&fields,
|
|
|
|
|
Fields::NoDiscrim(variant_layout))]);
|
|
|
|
|
} else {
|
|
|
|
|
// (This case arises for *empty* enums; so give it
|
|
|
|
|
// zero variants.)
|
|
|
|
|
record(adt_kind.into(), None, vec![]);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Layout::General { ref variants, discr, .. } => {
|
|
|
|
|
debug!("print-type-size t: `{:?}` adt general variants def {} layouts {} {:?}",
|
|
|
|
|
ty, adt_def.variants.len(), variants.len(), variants);
|
|
|
|
|
let variant_infos: Vec<_> = adt_def.variants.iter()
|
|
|
|
|
.zip(variants.iter())
|
|
|
|
|
.map(|(variant_def, variant_layout)| {
|
|
|
|
|
let fields: Vec<_> = variant_def.fields.iter()
|
|
|
|
|
.map(|field_def| (field_def.name, field_def.ty(tcx, substs)))
|
|
|
|
|
.collect();
|
|
|
|
|
build_variant_info(Some(variant_def.name),
|
|
|
|
|
&fields,
|
|
|
|
|
Fields::WithDiscrim(variant_layout))
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
record(adt_kind.into(), Some(discr.size()), variant_infos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Layout::UntaggedUnion { ref variants } => {
|
|
|
|
|
debug!("print-type-size t: `{:?}` adt union variants {:?}",
|
|
|
|
|
ty, variants);
|
|
|
|
|
// layout does not currently store info about each
|
|
|
|
|
// variant...
|
|
|
|
|
record(adt_kind.into(), None, Vec::new());
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
Layout::CEnum { discr, .. } => {
|
|
|
|
|
debug!("print-type-size t: `{:?}` adt c-like enum", ty);
|
|
|
|
|
let variant_infos: Vec<_> = adt_def.variants.iter()
|
|
|
|
|
.map(|variant_def| {
|
|
|
|
|
build_primitive_info(variant_def.name,
|
|
|
|
|
&layout::Primitive::Int(discr))
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
record(adt_kind.into(), Some(discr.size()), variant_infos);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// other cases provide little interesting (i.e. adjustable
|
|
|
|
|
// via representation tweaks) size info beyond total size.
|
|
|
|
|
Layout::Scalar { .. } |
|
|
|
|
|
Layout::Vector { .. } |
|
|
|
|
|
Layout::Array { .. } |
|
|
|
|
|
Layout::FatPointer { .. } => {
|
|
|
|
|
debug!("print-type-size t: `{:?}` adt other", ty);
|
|
|
|
|
record(adt_kind.into(), None, Vec::new())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-07-21 12:49:59 -04:00
|
|
|
|
/// For each CGU, identify if we can reuse an existing object file (or
|
|
|
|
|
/// maybe other context).
|
2016-12-09 17:13:13 -05:00
|
|
|
|
fn trans_reuse_previous_work_products(scx: &SharedCrateContext,
|
2016-07-21 12:49:59 -04:00
|
|
|
|
codegen_units: &[CodegenUnit],
|
|
|
|
|
symbol_map: &SymbolMap)
|
2016-07-25 10:51:14 -04:00
|
|
|
|
-> Vec<Option<WorkProduct>> {
|
2016-07-21 12:49:59 -04:00
|
|
|
|
debug!("trans_reuse_previous_work_products()");
|
|
|
|
|
codegen_units
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|cgu| {
|
|
|
|
|
let id = cgu.work_product_id();
|
|
|
|
|
|
2016-12-09 17:13:13 -05:00
|
|
|
|
let hash = cgu.compute_symbol_name_hash(scx, symbol_map);
|
2016-07-21 12:49:59 -04:00
|
|
|
|
|
|
|
|
|
debug!("trans_reuse_previous_work_products: id={:?} hash={}", id, hash);
|
|
|
|
|
|
2016-12-09 17:13:13 -05:00
|
|
|
|
if let Some(work_product) = scx.dep_graph().previous_work_product(&id) {
|
2016-07-21 12:49:59 -04:00
|
|
|
|
if work_product.input_hash == hash {
|
|
|
|
|
debug!("trans_reuse_previous_work_products: reusing {:?}", work_product);
|
2016-07-25 10:51:14 -04:00
|
|
|
|
return Some(work_product);
|
2016-07-21 12:49:59 -04:00
|
|
|
|
} else {
|
2016-12-09 17:13:13 -05:00
|
|
|
|
if scx.sess().opts.debugging_opts.incremental_info {
|
2016-11-30 17:33:52 -05:00
|
|
|
|
println!("incremental: CGU `{}` invalidated because of \
|
|
|
|
|
changed partitioning hash.",
|
|
|
|
|
cgu.name());
|
|
|
|
|
}
|
2016-07-21 12:49:59 -04:00
|
|
|
|
debug!("trans_reuse_previous_work_products: \
|
|
|
|
|
not reusing {:?} because hash changed to {:?}",
|
|
|
|
|
work_product, hash);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
None
|
|
|
|
|
})
|
|
|
|
|
.collect()
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 14:27:34 -04:00
|
|
|
|
fn collect_and_partition_translation_items<'a, 'tcx>(scx: &SharedCrateContext<'a, 'tcx>)
|
2016-05-26 08:59:58 -04:00
|
|
|
|
-> (Vec<CodegenUnit<'tcx>>, SymbolMap<'tcx>) {
|
2016-05-06 14:27:34 -04:00
|
|
|
|
let time_passes = scx.sess().time_passes();
|
2015-11-02 14:46:39 +01:00
|
|
|
|
|
2016-05-06 14:27:34 -04:00
|
|
|
|
let collection_mode = match scx.sess().opts.debugging_opts.print_trans_items {
|
2015-11-02 14:46:39 +01:00
|
|
|
|
Some(ref s) => {
|
|
|
|
|
let mode_string = s.to_lowercase();
|
|
|
|
|
let mode_string = mode_string.trim();
|
|
|
|
|
if mode_string == "eager" {
|
|
|
|
|
TransItemCollectionMode::Eager
|
|
|
|
|
} else {
|
|
|
|
|
if mode_string != "lazy" {
|
|
|
|
|
let message = format!("Unknown codegen-item collection mode '{}'. \
|
|
|
|
|
Falling back to 'lazy' mode.",
|
|
|
|
|
mode_string);
|
2016-05-06 14:27:34 -04:00
|
|
|
|
scx.sess().warn(&message);
|
2015-11-02 14:46:39 +01:00
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
TransItemCollectionMode::Lazy
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
None => TransItemCollectionMode::Lazy
|
|
|
|
|
};
|
|
|
|
|
|
2016-05-26 08:59:58 -04:00
|
|
|
|
let (items, inlining_map) =
|
|
|
|
|
time(time_passes, "translation item collection", || {
|
|
|
|
|
collector::collect_crate_translation_items(&scx, collection_mode)
|
2015-11-02 14:46:39 +01:00
|
|
|
|
});
|
|
|
|
|
|
2016-05-26 08:59:58 -04:00
|
|
|
|
let symbol_map = SymbolMap::build(scx, items.iter().cloned());
|
|
|
|
|
|
2016-05-06 14:27:34 -04:00
|
|
|
|
let strategy = if scx.sess().opts.debugging_opts.incremental.is_some() {
|
2016-04-21 16:45:33 -04:00
|
|
|
|
PartitioningStrategy::PerModule
|
|
|
|
|
} else {
|
2016-05-06 14:27:34 -04:00
|
|
|
|
PartitioningStrategy::FixedUnitCount(scx.sess().opts.cg.codegen_units)
|
2016-04-21 16:45:33 -04:00
|
|
|
|
};
|
|
|
|
|
|
2016-03-24 11:40:49 -04:00
|
|
|
|
let codegen_units = time(time_passes, "codegen unit partitioning", || {
|
2016-08-08 20:50:19 -04:00
|
|
|
|
partitioning::partition(scx,
|
2016-04-21 16:45:33 -04:00
|
|
|
|
items.iter().cloned(),
|
|
|
|
|
strategy,
|
2016-09-15 20:39:58 -04:00
|
|
|
|
&inlining_map)
|
2016-03-24 11:40:49 -04:00
|
|
|
|
});
|
|
|
|
|
|
2016-05-26 12:18:39 -04:00
|
|
|
|
assert!(scx.tcx().sess.opts.cg.codegen_units == codegen_units.len() ||
|
|
|
|
|
scx.tcx().sess.opts.debugging_opts.incremental.is_some());
|
|
|
|
|
|
2016-06-07 21:14:51 -04:00
|
|
|
|
{
|
|
|
|
|
let mut ccx_map = scx.translation_items().borrow_mut();
|
|
|
|
|
|
|
|
|
|
for trans_item in items.iter().cloned() {
|
2016-06-16 18:56:14 -04:00
|
|
|
|
ccx_map.insert(trans_item);
|
2016-06-07 21:14:51 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2016-05-06 14:27:34 -04:00
|
|
|
|
if scx.sess().opts.debugging_opts.print_trans_items.is_some() {
|
2016-11-08 14:02:55 +11:00
|
|
|
|
let mut item_to_cgus = FxHashMap();
|
2016-03-24 11:40:49 -04:00
|
|
|
|
|
2016-05-06 14:27:34 -04:00
|
|
|
|
for cgu in &codegen_units {
|
2016-07-21 12:49:59 -04:00
|
|
|
|
for (&trans_item, &linkage) in cgu.items() {
|
2016-03-24 11:40:49 -04:00
|
|
|
|
item_to_cgus.entry(trans_item)
|
|
|
|
|
.or_insert(Vec::new())
|
2016-07-21 12:49:59 -04:00
|
|
|
|
.push((cgu.name().clone(), linkage));
|
2016-03-24 11:40:49 -04:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
let mut item_keys: Vec<_> = items
|
|
|
|
|
.iter()
|
|
|
|
|
.map(|i| {
|
2016-05-09 23:56:49 -04:00
|
|
|
|
let mut output = i.to_string(scx.tcx());
|
2016-03-24 11:40:49 -04:00
|
|
|
|
output.push_str(" @@");
|
|
|
|
|
let mut empty = Vec::new();
|
|
|
|
|
let mut cgus = item_to_cgus.get_mut(i).unwrap_or(&mut empty);
|
|
|
|
|
cgus.as_mut_slice().sort_by_key(|&(ref name, _)| name.clone());
|
|
|
|
|
cgus.dedup();
|
|
|
|
|
for &(ref cgu_name, linkage) in cgus.iter() {
|
|
|
|
|
output.push_str(" ");
|
|
|
|
|
output.push_str(&cgu_name[..]);
|
|
|
|
|
|
|
|
|
|
let linkage_abbrev = match linkage {
|
2016-09-01 13:52:33 -05:00
|
|
|
|
llvm::Linkage::ExternalLinkage => "External",
|
|
|
|
|
llvm::Linkage::AvailableExternallyLinkage => "Available",
|
|
|
|
|
llvm::Linkage::LinkOnceAnyLinkage => "OnceAny",
|
|
|
|
|
llvm::Linkage::LinkOnceODRLinkage => "OnceODR",
|
|
|
|
|
llvm::Linkage::WeakAnyLinkage => "WeakAny",
|
|
|
|
|
llvm::Linkage::WeakODRLinkage => "WeakODR",
|
|
|
|
|
llvm::Linkage::AppendingLinkage => "Appending",
|
|
|
|
|
llvm::Linkage::InternalLinkage => "Internal",
|
|
|
|
|
llvm::Linkage::PrivateLinkage => "Private",
|
|
|
|
|
llvm::Linkage::ExternalWeakLinkage => "ExternalWeak",
|
|
|
|
|
llvm::Linkage::CommonLinkage => "Common",
|
2016-03-24 11:40:49 -04:00
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
output.push_str("[");
|
|
|
|
|
output.push_str(linkage_abbrev);
|
|
|
|
|
output.push_str("]");
|
|
|
|
|
}
|
|
|
|
|
output
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2015-11-02 14:46:39 +01:00
|
|
|
|
item_keys.sort();
|
|
|
|
|
|
|
|
|
|
for item in item_keys {
|
|
|
|
|
println!("TRANS_ITEM {}", item);
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-05-06 14:27:34 -04:00
|
|
|
|
|
2016-05-26 08:59:58 -04:00
|
|
|
|
(codegen_units, symbol_map)
|
|
|
|
|
}
|