@@ -5,7 +5,7 @@ use crate::coverageinfo::map_data::{FunctionCoverage, FunctionCoverageCollector}
5
5
use crate :: llvm;
6
6
7
7
use itertools:: Itertools as _;
8
- use rustc_codegen_ssa:: traits:: { BaseTypeMethods , ConstMethods } ;
8
+ use rustc_codegen_ssa:: traits:: { BaseTypeMethods , ConstMethods , StaticMethods } ;
9
9
use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
10
10
use rustc_hir:: def:: DefKind ;
11
11
use rustc_hir:: def_id:: DefId ;
@@ -70,14 +70,9 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
70
70
71
71
// Encode all filenames referenced by counters/expressions in this module
72
72
let filenames_buffer = global_file_table. filenames_buffer ( ) ;
73
- let filenames_size = filenames_buffer. len ( ) ;
74
- let filenames_val = cx. const_bytes ( filenames_buffer) ;
73
+ save_covmap_filenames_record ( cx, version, filenames_buffer) ;
75
74
let filenames_ref = coverageinfo:: hash_bytes ( filenames_buffer) ;
76
75
77
- // Generate the LLVM IR representation of the coverage map and store it in a well-known global
78
- let cov_data_val = generate_coverage_map ( cx, version, filenames_size, filenames_val) ;
79
- coverageinfo:: save_cov_data_to_mod ( cx, cov_data_val) ;
80
-
81
76
let mut unused_function_names = Vec :: new ( ) ;
82
77
let covfun_section_name = coverageinfo:: covfun_section_name ( cx) ;
83
78
@@ -288,15 +283,8 @@ fn encode_mappings_for_function(
288
283
} )
289
284
}
290
285
291
- /// Construct coverage map header and the array of function records, and combine them into the
292
- /// coverage map. Save the coverage map data into the LLVM IR as a static global using a
293
- /// specific, well-known section and name.
294
- fn generate_coverage_map < ' ll > (
295
- cx : & CodegenCx < ' ll , ' _ > ,
296
- version : u32 ,
297
- filenames_size : usize ,
298
- filenames_val : & ' ll llvm:: Value ,
299
- ) -> & ' ll llvm:: Value {
286
+ fn save_covmap_filenames_record ( cx : & CodegenCx < ' _ , ' _ > , version : u32 , filenames_buffer : & [ u8 ] ) {
287
+ let filenames_size = filenames_buffer. len ( ) ;
300
288
debug ! ( "cov map: filenames_size = {}, 0-based version = {}" , filenames_size, version) ;
301
289
302
290
// Create the coverage data header (Note, fields 0 and 2 are now always zero,
@@ -311,7 +299,30 @@ fn generate_coverage_map<'ll>(
311
299
) ;
312
300
313
301
// Create the complete LLVM coverage data value to add to the LLVM IR
314
- cx. const_struct ( & [ cov_data_header_val, filenames_val] , /*packed=*/ false )
302
+ let covmap_val = cx. const_struct (
303
+ & [ cov_data_header_val, cx. const_bytes ( filenames_buffer) ] ,
304
+ /*packed=*/ false ,
305
+ ) ;
306
+
307
+ let covmap_var_name = llvm:: build_string ( |s| unsafe {
308
+ llvm:: LLVMRustCoverageWriteMappingVarNameToString ( s) ;
309
+ } )
310
+ . expect ( "Rust Coverage Mapping var name failed UTF-8 conversion" ) ;
311
+ debug ! ( "covmap var name: {:?}" , covmap_var_name) ;
312
+
313
+ let covmap_section_name = llvm:: build_string ( |s| unsafe {
314
+ llvm:: LLVMRustCoverageWriteMapSectionNameToString ( cx. llmod , s) ;
315
+ } )
316
+ . expect ( "Rust Coverage section name failed UTF-8 conversion" ) ;
317
+ debug ! ( "covmap section name: {:?}" , covmap_section_name) ;
318
+
319
+ let llglobal = llvm:: add_global ( cx. llmod , cx. val_ty ( covmap_val) , & covmap_var_name) ;
320
+ llvm:: set_initializer ( llglobal, covmap_val) ;
321
+ llvm:: set_global_constant ( llglobal, true ) ;
322
+ llvm:: set_linkage ( llglobal, llvm:: Linkage :: PrivateLinkage ) ;
323
+ llvm:: set_section ( llglobal, & covmap_section_name) ;
324
+ llvm:: set_alignment ( llglobal, coverageinfo:: VAR_ALIGN_BYTES ) ;
325
+ cx. add_used_global ( llglobal) ;
315
326
}
316
327
317
328
/// Construct a function record and combine it with the function's coverage mapping data.
0 commit comments