@@ -5,7 +5,7 @@ use crate::coverageinfo::map_data::{FunctionCoverage, FunctionCoverageCollector}
55use crate :: llvm;
66
77use itertools:: Itertools as _;
8- use rustc_codegen_ssa:: traits:: { BaseTypeMethods , ConstMethods } ;
8+ use rustc_codegen_ssa:: traits:: { BaseTypeMethods , ConstMethods , StaticMethods } ;
99use rustc_data_structures:: fx:: { FxIndexMap , FxIndexSet } ;
1010use rustc_hir:: def:: DefKind ;
1111use rustc_hir:: def_id:: DefId ;
@@ -69,14 +69,9 @@ pub fn finalize(cx: &CodegenCx<'_, '_>) {
6969
7070 // Encode all filenames referenced by counters/expressions in this module
7171 let filenames_buffer = global_file_table. filenames_buffer ( ) ;
72- let filenames_size = filenames_buffer. len ( ) ;
73- let filenames_val = cx. const_bytes ( filenames_buffer) ;
72+ save_covmap_filenames_record ( cx, version, filenames_buffer) ;
7473 let filenames_ref = coverageinfo:: hash_bytes ( filenames_buffer) ;
7574
76- // Generate the LLVM IR representation of the coverage map and store it in a well-known global
77- let cov_data_val = generate_coverage_map ( cx, version, filenames_size, filenames_val) ;
78- coverageinfo:: save_cov_data_to_mod ( cx, cov_data_val) ;
79-
8075 let mut unused_function_names = Vec :: new ( ) ;
8176 let covfun_section_name = coverageinfo:: covfun_section_name ( cx) ;
8277
@@ -288,15 +283,8 @@ fn encode_mappings_for_function(
288283 } )
289284}
290285
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 ( ) ;
300288 debug ! ( "cov map: filenames_size = {}, 0-based version = {}" , filenames_size, version) ;
301289
302290 // Create the coverage data header (Note, fields 0 and 2 are now always zero,
@@ -311,7 +299,30 @@ fn generate_coverage_map<'ll>(
311299 ) ;
312300
313301 // 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) ;
315326}
316327
317328/// Construct a function record and combine it with the function's coverage mapping data.
0 commit comments