11use std:: iter;
22
3- use itertools:: Itertools as _;
43use rustc_abi:: Align ;
54use rustc_codegen_ssa:: traits:: {
65 BaseTypeCodegenMethods , ConstCodegenMethods , StaticCodegenMethods ,
76} ;
87use rustc_data_structures:: fx:: { FxHashSet , FxIndexMap , FxIndexSet } ;
98use rustc_hir:: def_id:: { DefId , LocalDefId } ;
109use rustc_index:: IndexVec ;
10+ use rustc_middle:: mir;
1111use rustc_middle:: ty:: { self , TyCtxt } ;
12- use rustc_middle:: { bug, mir} ;
1312use rustc_session:: RemapFileNameExt ;
1413use rustc_session:: config:: RemapPathScopeComponents ;
1514use rustc_span:: def_id:: DefIdSet ;
@@ -67,25 +66,16 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
6766 return ;
6867 }
6968
70- let all_file_names = function_coverage_map
71- . iter ( )
72- . map ( |( _, fn_cov) | fn_cov. function_coverage_info . body_span )
73- . map ( |span| span_file_name ( tcx, span) ) ;
74- let global_file_table = GlobalFileTable :: new ( all_file_names) ;
75-
76- // Encode all filenames referenced by coverage mappings in this CGU.
77- let filenames_buffer = global_file_table. make_filenames_buffer ( tcx) ;
78- // The `llvm-cov` tool uses this hash to associate each covfun record with
79- // its corresponding filenames table, since the final binary will typically
80- // contain multiple covmap records from different compilation units.
81- let filenames_hash = llvm_cov:: hash_bytes ( & filenames_buffer) ;
82-
83- let mut unused_function_names = Vec :: new ( ) ;
69+ // The order of entries in this global file table is arbitrary, and depends
70+ // on the order in which this CGU visited functions and statements during
71+ // codegen. But that order should be deterministic, so the table order is
72+ // deterministic too.
73+ let mut global_file_table = GlobalFileTable :: new ( ) ;
8474
8575 let covfun_records = function_coverage_map
8676 . into_iter ( )
8777 . filter_map ( |( instance, function_coverage) | {
88- prepare_covfun_record ( tcx, & global_file_table, instance, & function_coverage)
78+ prepare_covfun_record ( tcx, & mut global_file_table, instance, & function_coverage)
8979 } )
9080 . collect :: < Vec < _ > > ( ) ;
9181
@@ -98,6 +88,15 @@ pub(crate) fn finalize(cx: &CodegenCx<'_, '_>) {
9888 return ;
9989 }
10090
91+ // Encode all filenames referenced by coverage mappings in this CGU.
92+ let filenames_buffer = global_file_table. make_filenames_buffer ( tcx) ;
93+ // The `llvm-cov` tool uses this hash to associate each covfun record with
94+ // its corresponding filenames table, since the final binary will typically
95+ // contain multiple covmap records from different compilation units.
96+ let filenames_hash = llvm_cov:: hash_bytes ( & filenames_buffer) ;
97+
98+ let mut unused_function_names = vec ! [ ] ;
99+
101100 for covfun in & covfun_records {
102101 unused_function_names. extend ( covfun. mangled_function_name_if_unused ( ) ) ;
103102
@@ -137,22 +136,13 @@ struct GlobalFileTable {
137136}
138137
139138impl GlobalFileTable {
140- fn new ( all_file_names : impl IntoIterator < Item = Symbol > ) -> Self {
141- // Collect all of the filenames into a set. Filenames usually come in
142- // contiguous runs, so we can dedup adjacent ones to save work.
143- let mut raw_file_table = all_file_names. into_iter ( ) . dedup ( ) . collect :: < FxIndexSet < Symbol > > ( ) ;
144-
145- // Sort the file table by its actual string values, not the arbitrary
146- // ordering of its symbols.
147- raw_file_table. sort_unstable_by ( |a, b| a. as_str ( ) . cmp ( b. as_str ( ) ) ) ;
148-
149- Self { raw_file_table }
139+ fn new ( ) -> Self {
140+ Self { raw_file_table : FxIndexSet :: default ( ) }
150141 }
151142
152- fn global_file_id_for_file_name ( & self , file_name : Symbol ) -> GlobalFileId {
153- let raw_id = self . raw_file_table . get_index_of ( & file_name) . unwrap_or_else ( || {
154- bug ! ( "file name not found in prepared global file table: {file_name}" ) ;
155- } ) ;
143+ fn global_file_id_for_file_name ( & mut self , file_name : Symbol ) -> GlobalFileId {
144+ // Ensure the given file has a table entry, and get its index.
145+ let ( raw_id, _) = self . raw_file_table . insert_full ( file_name) ;
156146 // The raw file table doesn't include an entry for the working dir
157147 // (which has ID 0), so add 1 to get the correct ID.
158148 GlobalFileId :: from_usize ( raw_id + 1 )
0 commit comments