1
1
use crate :: llvm;
2
2
3
- use crate :: abi:: Abi ;
4
3
use crate :: builder:: Builder ;
5
4
use crate :: common:: CodegenCx ;
6
5
use crate :: coverageinfo:: ffi:: { CounterExpression , CounterMappingRegion } ;
@@ -12,26 +11,20 @@ use rustc_codegen_ssa::traits::{
12
11
StaticMethods ,
13
12
} ;
14
13
use rustc_data_structures:: fx:: FxHashMap ;
15
- use rustc_hir as hir;
16
14
use rustc_hir:: def_id:: DefId ;
17
15
use rustc_llvm:: RustString ;
18
16
use rustc_middle:: bug;
19
- use rustc_middle:: mir:: coverage:: { CounterId , CoverageKind , FunctionCoverageInfo } ;
17
+ use rustc_middle:: mir:: coverage:: { CoverageKind , FunctionCoverageInfo } ;
20
18
use 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 } ;
26
21
27
22
use std:: cell:: RefCell ;
28
23
29
24
pub ( crate ) mod ffi;
30
25
pub ( crate ) mod map_data;
31
26
pub mod mapgen;
32
27
33
- const UNUSED_FUNCTION_COUNTER_ID : CounterId = CounterId :: START ;
34
-
35
28
const VAR_ALIGN_BYTES : usize = 8 ;
36
29
37
30
/// A context object for maintaining all state needed by the coverageinfo module.
@@ -77,22 +70,8 @@ impl<'ll, 'tcx> CodegenCx<'ll, 'tcx> {
77
70
}
78
71
}
79
72
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.
93
73
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) ;
96
75
add_unused_function_coverage ( self , instance, function_coverage_info) ;
97
76
}
98
77
}
@@ -159,10 +138,8 @@ impl<'tcx> CoverageInfoBuilderMethods<'tcx> for Builder<'_, '_, 'tcx> {
159
138
}
160
139
}
161
140
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 (
166
143
def_id,
167
144
GenericArgs :: for_item ( tcx, def_id, |param, _| {
168
145
if let ty:: GenericParamDefKind :: Lifetime = param. kind {
@@ -171,46 +148,7 @@ fn declare_unused_fn<'tcx>(cx: &CodegenCx<'_, 'tcx>, def_id: DefId) -> Instance<
171
148
tcx. mk_param_from_def ( param)
172
149
}
173
150
} ) ,
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
+ )
214
152
}
215
153
216
154
fn add_unused_function_coverage < ' tcx > (
0 commit comments