|
| 1 | +use rustc::middle::codegen_fn_attrs::CodegenFnAttrFlags; |
| 2 | +use rustc::mir::{Body, TerminatorKind}; |
| 3 | +use rustc::ty::query::Providers; |
| 4 | +use rustc::ty::TyCtxt; |
| 5 | +use rustc_attr::InlineAttr; |
| 6 | +use rustc_hir::def_id::DefId; |
| 7 | +//use rustc_hir::ItemKind; |
| 8 | +//use rustc_session::config::OptLevel; |
| 9 | +use rustc_session::config::Sanitizer; |
| 10 | + |
| 11 | +pub fn provide(providers: &mut Providers<'_>) { |
| 12 | + providers.cross_crate_inlinable = cross_crate_inlinable; |
| 13 | +} |
| 14 | + |
| 15 | +fn cross_crate_inlinable(tcx: TyCtxt<'_>, def_id: DefId) -> bool { |
| 16 | + /*use rustc_hir::Node; |
| 17 | + tcx.hir().as_local_hir_id(def_id).map(|id| match tcx.hir().get(id) { |
| 18 | + Node::Item(item) => match item.kind { |
| 19 | + ItemKind::Static(..) | ItemKind::Const(..) => { |
| 20 | + panic!("STATIC!! {:?}", def_id); |
| 21 | + } |
| 22 | + _ => (), |
| 23 | + }, |
| 24 | + _ => (), |
| 25 | + });*/ |
| 26 | + |
| 27 | + let attrs = tcx.codegen_fn_attrs(def_id); |
| 28 | + |
| 29 | + match attrs.inline { |
| 30 | + InlineAttr::Hint | InlineAttr::Always => return true, |
| 31 | + InlineAttr::Never => return false, |
| 32 | + InlineAttr::None => (), |
| 33 | + } |
| 34 | + |
| 35 | + if attrs.contains_extern_indicator() { |
| 36 | + return false; |
| 37 | + } |
| 38 | + |
| 39 | + if tcx.lang_items().start_fn() == Some(def_id) { |
| 40 | + return false; |
| 41 | + } |
| 42 | + |
| 43 | + // FIXME: Share some of this logic with the MIR inlining pass |
| 44 | + |
| 45 | + // FIXME: Is this needed? |
| 46 | + if attrs.flags.contains(CodegenFnAttrFlags::TRACK_CALLER) { |
| 47 | + return false; |
| 48 | + } |
| 49 | + |
| 50 | + // Avoid inlining functions marked as no_sanitize if sanitizer is enabled, |
| 51 | + // since instrumentation might be enabled and performed on the caller. |
| 52 | + match tcx.sess.opts.debugging_opts.sanitizer { |
| 53 | + Some(Sanitizer::Address) => { |
| 54 | + if attrs.flags.contains(CodegenFnAttrFlags::NO_SANITIZE_ADDRESS) { |
| 55 | + return false; |
| 56 | + } |
| 57 | + } |
| 58 | + Some(Sanitizer::Memory) => { |
| 59 | + if attrs.flags.contains(CodegenFnAttrFlags::NO_SANITIZE_MEMORY) { |
| 60 | + return false; |
| 61 | + } |
| 62 | + } |
| 63 | + Some(Sanitizer::Thread) => { |
| 64 | + if attrs.flags.contains(CodegenFnAttrFlags::NO_SANITIZE_THREAD) { |
| 65 | + return false; |
| 66 | + } |
| 67 | + } |
| 68 | + Some(Sanitizer::Leak) => {} |
| 69 | + None => {} |
| 70 | + } |
| 71 | + |
| 72 | + if tcx.sess.opts.debugging_opts.cross_crate_inline_threshold.is_none() { |
| 73 | + return false; |
| 74 | + } |
| 75 | + /* |
| 76 | + if tcx.sess.opts.optimize != OptLevel::Aggressive { |
| 77 | + return false; |
| 78 | + } |
| 79 | + */ |
| 80 | + let r = allow_cross_inline(tcx, def_id, &tcx.optimized_mir(def_id).unwrap_read_only()); |
| 81 | + /*if r { |
| 82 | + eprintln!("cross_crate_inlinable({:?})", def_id); |
| 83 | + }*/ |
| 84 | + r |
| 85 | +} |
| 86 | + |
| 87 | +const THRESHOLD: usize = 30; |
| 88 | + |
| 89 | +const INSTR_COST: usize = 2; |
| 90 | +const CALL_PENALTY: usize = 10; |
| 91 | +const LANDINGPAD_PENALTY: usize = 5; |
| 92 | +const RESUME_PENALTY: usize = 5; |
| 93 | + |
| 94 | +const UNKNOWN_SIZE_COST: usize = 2; |
| 95 | + |
| 96 | +fn allow_cross_inline<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, body: &Body<'tcx>) -> bool { |
| 97 | + // Generators should have been transformed here. |
| 98 | + debug_assert!(body.yield_ty.is_none()); |
| 99 | + |
| 100 | + let mut threshold = |
| 101 | + tcx.sess.opts.debugging_opts.cross_crate_inline_threshold.unwrap_or(THRESHOLD); |
| 102 | + |
| 103 | + // Give a bonus functions with a small number of blocks, |
| 104 | + // We normally have two or three blocks for even |
| 105 | + // very small functions. |
| 106 | + if body.basic_blocks().len() <= 3 { |
| 107 | + threshold += threshold / 4; |
| 108 | + } |
| 109 | + |
| 110 | + let mut cost = 0; |
| 111 | + |
| 112 | + for block in body.basic_blocks() { |
| 113 | + cost += block.statements.len(); |
| 114 | + |
| 115 | + match block.terminator().kind { |
| 116 | + TerminatorKind::Drop { unwind, .. } | TerminatorKind::DropAndReplace { unwind, .. } => { |
| 117 | + cost += CALL_PENALTY; |
| 118 | + if unwind.is_some() { |
| 119 | + cost += LANDINGPAD_PENALTY; |
| 120 | + } |
| 121 | + } |
| 122 | + TerminatorKind::Call { cleanup, .. } => { |
| 123 | + cost += CALL_PENALTY; |
| 124 | + if cleanup.is_some() { |
| 125 | + cost += LANDINGPAD_PENALTY; |
| 126 | + } |
| 127 | + } |
| 128 | + TerminatorKind::Assert { cleanup, .. } => { |
| 129 | + cost += CALL_PENALTY; |
| 130 | + |
| 131 | + if cleanup.is_some() { |
| 132 | + cost += LANDINGPAD_PENALTY; |
| 133 | + } |
| 134 | + } |
| 135 | + TerminatorKind::Resume => cost += RESUME_PENALTY, |
| 136 | + _ => cost += INSTR_COST, |
| 137 | + } |
| 138 | + } |
| 139 | + |
| 140 | + // Count up the cost of local variables and temps, if we know the size |
| 141 | + // use that, otherwise we use a moderately-large dummy cost. |
| 142 | + |
| 143 | + let ptr_size = tcx.data_layout.pointer_size.bytes(); |
| 144 | + |
| 145 | + let param_env = tcx.param_env(def_id); |
| 146 | + |
| 147 | + for v in body.vars_and_temps_iter() { |
| 148 | + let v = &body.local_decls[v]; |
| 149 | + // Cost of the var is the size in machine-words, if we know |
| 150 | + // it. |
| 151 | + if let Some(size) = |
| 152 | + tcx.layout_of(param_env.and(v.ty)).ok().map(|layout| layout.size.bytes()) |
| 153 | + { |
| 154 | + cost += (size / ptr_size) as usize; |
| 155 | + } else { |
| 156 | + cost += UNKNOWN_SIZE_COST; |
| 157 | + } |
| 158 | + } |
| 159 | + |
| 160 | + cost <= threshold |
| 161 | +} |
0 commit comments