11use crate :: llvm;
22
3- use crate :: abi:: Abi ;
43use crate :: builder:: Builder ;
54use crate :: common:: CodegenCx ;
65use crate :: coverageinfo:: ffi:: { CounterExpression , CounterMappingRegion } ;
@@ -12,26 +11,20 @@ use rustc_codegen_ssa::traits::{
1211 StaticMethods ,
1312} ;
1413use rustc_data_structures:: fx:: FxHashMap ;
15- use rustc_hir as hir;
1614use rustc_hir:: def_id:: DefId ;
1715use rustc_llvm:: RustString ;
1816use rustc_middle:: bug;
19- use rustc_middle:: mir:: coverage:: { CounterId , CoverageKind , FunctionCoverageInfo } ;
17+ use rustc_middle:: mir:: coverage:: { CoverageKind , FunctionCoverageInfo } ;
2018use rustc_middle:: mir:: Coverage ;
21- use rustc_middle:: ty;
22- use rustc_middle:: ty:: layout:: { FnAbiOf , HasTyCtxt } ;
23- use rustc_middle:: ty:: GenericArgs ;
24- use rustc_middle:: ty:: Instance ;
25- use rustc_middle:: ty:: Ty ;
19+ use rustc_middle:: ty:: layout:: HasTyCtxt ;
20+ use rustc_middle:: ty:: { self , GenericArgs , Instance , TyCtxt } ;
2621
2722use std:: cell:: RefCell ;
2823
2924pub ( crate ) mod ffi;
3025pub ( crate ) mod map_data;
3126pub mod mapgen;
3227
33- const UNUSED_FUNCTION_COUNTER_ID : CounterId = CounterId :: START ;
34-
3528const VAR_ALIGN_BYTES : usize = 8 ;
3629
3730/// A context object for maintaining all state needed by the coverageinfo module.
@@ -77,22 +70,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
7770 }
7871 }
7972
80- /// Functions with MIR-based coverage are normally codegenned _only_ if
81- /// called. LLVM coverage tools typically expect every function to be
82- /// defined (even if unused), with at least one call to LLVM intrinsic
83- /// `instrprof.increment`.
84- ///
85- /// Codegen a small function that will never be called, with one counter
86- /// that will never be incremented.
87- ///
88- /// For used/called functions, the coverageinfo was already added to the
89- /// `function_coverage_map` (keyed by function `Instance`) during codegen.
90- /// But in this case, since the unused function was _not_ previously
91- /// codegenned, collect the function coverage info from MIR and add an
92- /// "unused" entry to the function coverage map.
9373 fn define_unused_fn ( & self , def_id : DefId , function_coverage_info : & ' tcx FunctionCoverageInfo ) {
94- let instance = declare_unused_fn ( self , def_id) ;
95- codegen_unused_fn_and_counter ( self , instance) ;
74+ let instance = declare_unused_fn ( self . tcx , def_id) ;
9675 add_unused_function_coverage ( self , instance, function_coverage_info) ;
9776 }
9877}
@@ -159,10 +138,8 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
159138 }
160139}
161140
162- fn declare_unused_fn < ' tcx > ( cx : & CodegenCx < ' _ , ' tcx > , def_id : DefId ) -> Instance < ' tcx > {
163- let tcx = cx. tcx ;
164-
165- let instance = Instance :: new (
141+ fn declare_unused_fn < ' tcx > ( tcx : TyCtxt < ' tcx > , def_id : DefId ) -> Instance < ' tcx > {
142+ Instance :: new (
166143 def_id,
167144 GenericArgs :: for_item ( tcx, def_id, |param, _| {
168145 if let ty:: GenericParamDefKind :: Lifetime = param. kind {
@@ -171,46 +148,7 @@ fn declare_unused_fn<'tcx>(cx: &CodegenCx<'_, 'tcx>, def_id: DefId) -> Instance<
171148 tcx. mk_param_from_def ( param)
172149 }
173150 } ) ,
174- ) ;
175-
176- let llfn = cx. declare_fn (
177- tcx. symbol_name ( instance) . name ,
178- cx. fn_abi_of_fn_ptr (
179- ty:: Binder :: dummy ( tcx. mk_fn_sig (
180- [ Ty :: new_unit ( tcx) ] ,
181- Ty :: new_unit ( tcx) ,
182- false ,
183- hir:: Unsafety :: Unsafe ,
184- Abi :: Rust ,
185- ) ) ,
186- ty:: List :: empty ( ) ,
187- ) ,
188- None ,
189- ) ;
190-
191- llvm:: set_linkage ( llfn, llvm:: Linkage :: PrivateLinkage ) ;
192- llvm:: set_visibility ( llfn, llvm:: Visibility :: Default ) ;
193-
194- assert ! ( cx. instances. borrow_mut( ) . insert( instance, llfn) . is_none( ) ) ;
195-
196- instance
197- }
198-
199- fn codegen_unused_fn_and_counter < ' tcx > ( cx : & CodegenCx < ' _ , ' tcx > , instance : Instance < ' tcx > ) {
200- let llfn = cx. get_fn ( instance) ;
201- let llbb = Builder :: append_block ( cx, llfn, "unused_function" ) ;
202- let mut bx = Builder :: build ( cx, llbb) ;
203- let fn_name = bx. get_pgo_func_name_var ( instance) ;
204- let hash = bx. const_u64 ( 0 ) ;
205- let num_counters = bx. const_u32 ( 1 ) ;
206- let index = bx. const_u32 ( u32:: from ( UNUSED_FUNCTION_COUNTER_ID ) ) ;
207- debug ! (
208- "codegen intrinsic instrprof.increment(fn_name={:?}, hash={:?}, num_counters={:?},
209- index={:?}) for unused function: {:?}" ,
210- fn_name, hash, num_counters, index, instance
211- ) ;
212- bx. instrprof_increment ( fn_name, hash, num_counters, index) ;
213- bx. ret_void ( ) ;
151+ )
214152}
215153
216154fn add_unused_function_coverage < ' tcx > (
0 commit comments