@@ -16,7 +16,7 @@ use crate::kani_middle::{check_reachable_items, dump_mir_items};
16
16
use crate :: kani_queries:: QueryDb ;
17
17
use cbmc:: goto_program:: Location ;
18
18
use cbmc:: irep:: goto_binary_serde:: write_goto_binary_file;
19
- use cbmc:: RoundingMode ;
19
+ use cbmc:: { InternString , RoundingMode } ;
20
20
use cbmc:: { InternedString , MachineModel } ;
21
21
use kani_metadata:: artifact:: convert_type;
22
22
use kani_metadata:: CompilerArtifactStub ;
@@ -32,12 +32,10 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
32
32
use rustc_data_structures:: temp_dir:: MaybeTempDir ;
33
33
use rustc_errors:: { ErrorGuaranteed , DEFAULT_LOCALE_RESOURCE } ;
34
34
use rustc_hir:: def_id:: LOCAL_CRATE ;
35
- use rustc_hir:: definitions:: DefPathHash ;
36
35
use rustc_metadata:: creader:: MetadataLoaderDyn ;
37
36
use rustc_metadata:: fs:: { emit_wrapper_file, METADATA_FILENAME } ;
38
37
use rustc_metadata:: EncodedMetadata ;
39
38
use rustc_middle:: dep_graph:: { WorkProduct , WorkProductId } ;
40
- use rustc_middle:: mir:: mono:: MonoItem ;
41
39
use rustc_middle:: ty:: TyCtxt ;
42
40
use rustc_middle:: util:: Providers ;
43
41
use rustc_session:: config:: { CrateType , OutputFilenames , OutputType } ;
@@ -46,7 +44,7 @@ use rustc_session::Session;
46
44
use rustc_smir:: rustc_internal;
47
45
use rustc_target:: abi:: Endian ;
48
46
use rustc_target:: spec:: PanicStrategy ;
49
- use stable_mir:: mir:: mono:: MonoItem as MonoItemStable ;
47
+ use stable_mir:: mir:: mono:: { Instance , MonoItem } ;
50
48
use stable_mir:: CrateDef ;
51
49
use std:: any:: Any ;
52
50
use std:: collections:: BTreeMap ;
@@ -82,10 +80,10 @@ impl GotocCodegenBackend {
82
80
fn codegen_items < ' tcx > (
83
81
& self ,
84
82
tcx : TyCtxt < ' tcx > ,
85
- starting_items : & [ MonoItem < ' tcx > ] ,
83
+ starting_items : & [ MonoItem ] ,
86
84
symtab_goto : & Path ,
87
85
machine_model : & MachineModel ,
88
- ) -> ( GotocCtx < ' tcx > , Vec < MonoItem < ' tcx > > ) {
86
+ ) -> ( GotocCtx < ' tcx > , Vec < MonoItem > ) {
89
87
let items = with_timer (
90
88
|| collect_reachable_items ( tcx, starting_items) ,
91
89
"codegen reachability analysis" ,
@@ -103,17 +101,13 @@ impl GotocCodegenBackend {
103
101
for item in & items {
104
102
match * item {
105
103
MonoItem :: Fn ( instance) => {
106
- let instance = rustc_internal:: stable ( instance) ;
107
104
gcx. call_with_panic_debug_info (
108
105
|ctx| ctx. declare_function ( instance) ,
109
106
format ! ( "declare_function: {}" , instance. name( ) ) ,
110
107
instance. def ,
111
108
) ;
112
109
}
113
- MonoItem :: Static ( _) => {
114
- let MonoItemStable :: Static ( def) = rustc_internal:: stable ( item) else {
115
- unreachable ! ( )
116
- } ;
110
+ MonoItem :: Static ( def) => {
117
111
gcx. call_with_panic_debug_info (
118
112
|ctx| ctx. declare_static ( def) ,
119
113
format ! ( "declare_static: {}" , def. name( ) ) ,
@@ -128,7 +122,6 @@ impl GotocCodegenBackend {
128
122
for item in & items {
129
123
match * item {
130
124
MonoItem :: Fn ( instance) => {
131
- let instance = rustc_internal:: stable ( instance) ;
132
125
gcx. call_with_panic_debug_info (
133
126
|ctx| ctx. codegen_function ( instance) ,
134
127
format ! (
@@ -139,10 +132,7 @@ impl GotocCodegenBackend {
139
132
instance. def ,
140
133
) ;
141
134
}
142
- MonoItem :: Static ( _) => {
143
- let MonoItemStable :: Static ( def) = rustc_internal:: stable ( item) else {
144
- unreachable ! ( )
145
- } ;
135
+ MonoItem :: Static ( def) => {
146
136
gcx. call_with_panic_debug_info (
147
137
|ctx| ctx. codegen_static ( def) ,
148
138
format ! ( "codegen_static: {}" , def. name( ) ) ,
@@ -237,28 +227,31 @@ impl CodegenBackend for GotocCodegenBackend {
237
227
ReachabilityType :: Harnesses => {
238
228
// Cross-crate collecting of all items that are reachable from the crate harnesses.
239
229
let harnesses = queries. target_harnesses ( ) ;
240
- let mut items: HashSet < DefPathHash > = HashSet :: with_capacity ( harnesses. len ( ) ) ;
230
+ let mut items: HashSet < _ > = HashSet :: with_capacity ( harnesses. len ( ) ) ;
241
231
items. extend ( harnesses) ;
242
- let harnesses = filter_crate_items ( tcx, |_, def_id | {
243
- items. contains ( & tcx . def_path_hash ( def_id ) )
232
+ let harnesses = filter_crate_items ( tcx, |_, instance | {
233
+ items. contains ( & instance . mangled_name ( ) . intern ( ) )
244
234
} ) ;
245
235
for harness in harnesses {
246
- let model_path = queries
247
- . harness_model_path ( & tcx. def_path_hash ( harness. def_id ( ) ) )
248
- . unwrap ( ) ;
249
- let ( gcx, items) =
250
- self . codegen_items ( tcx, & [ harness] , model_path, & results. machine_model ) ;
251
- results. extend ( gcx, items, None ) ;
236
+ let model_path =
237
+ queries. harness_model_path ( & harness. mangled_name ( ) ) . unwrap ( ) ;
238
+ let ( gcx, mono_items) = self . codegen_items (
239
+ tcx,
240
+ & [ MonoItem :: Fn ( harness) ] ,
241
+ model_path,
242
+ & results. machine_model ,
243
+ ) ;
244
+ results. extend ( gcx, mono_items, None ) ;
252
245
}
253
246
}
254
247
ReachabilityType :: Tests => {
255
248
// We're iterating over crate items here, so what we have to codegen is the "test description" containing the
256
249
// test closure that we want to execute
257
250
// TODO: Refactor this code so we can guarantee that the pair (test_fn, test_desc) actually match.
258
251
let mut descriptions = vec ! [ ] ;
259
- let harnesses = filter_const_crate_items ( tcx, |_, def_id | {
260
- if is_test_harness_description ( tcx, def_id ) {
261
- descriptions. push ( def_id ) ;
252
+ let harnesses = filter_const_crate_items ( tcx, |_, item | {
253
+ if is_test_harness_description ( tcx, item . def ) {
254
+ descriptions. push ( item . def ) ;
262
255
true
263
256
} else {
264
257
false
@@ -289,11 +282,15 @@ impl CodegenBackend for GotocCodegenBackend {
289
282
}
290
283
ReachabilityType :: None => { }
291
284
ReachabilityType :: PubFns => {
292
- let entry_fn = tcx. entry_fn ( ( ) ) . map ( |( id, _) | id) ;
293
- let local_reachable = filter_crate_items ( tcx, |_, def_id| {
294
- ( tcx. is_reachable_non_generic ( def_id) && tcx. def_kind ( def_id) . is_fn_like ( ) )
295
- || entry_fn == Some ( def_id)
296
- } ) ;
285
+ let main_instance =
286
+ stable_mir:: entry_fn ( ) . map ( |main_fn| Instance :: try_from ( main_fn) . unwrap ( ) ) ;
287
+ let local_reachable = filter_crate_items ( tcx, |_, instance| {
288
+ let def_id = rustc_internal:: internal ( instance. def . def_id ( ) ) ;
289
+ Some ( instance) == main_instance || tcx. is_reachable_non_generic ( def_id)
290
+ } )
291
+ . into_iter ( )
292
+ . map ( MonoItem :: Fn )
293
+ . collect :: < Vec < _ > > ( ) ;
297
294
let model_path = base_filename. with_extension ( ArtifactType :: SymTabGoto ) ;
298
295
let ( gcx, items) = self . codegen_items (
299
296
tcx,
@@ -527,17 +524,17 @@ where
527
524
}
528
525
}
529
526
530
- struct GotoCodegenResults < ' tcx > {
527
+ struct GotoCodegenResults {
531
528
reachability : ReachabilityType ,
532
529
harnesses : Vec < HarnessMetadata > ,
533
530
unsupported_constructs : UnsupportedConstructs ,
534
531
concurrent_constructs : UnsupportedConstructs ,
535
- items : Vec < MonoItem < ' tcx > > ,
532
+ items : Vec < MonoItem > ,
536
533
crate_name : InternedString ,
537
534
machine_model : MachineModel ,
538
535
}
539
536
540
- impl < ' tcx > GotoCodegenResults < ' tcx > {
537
+ impl GotoCodegenResults {
541
538
pub fn new ( tcx : TyCtxt , reachability : ReachabilityType ) -> Self {
542
539
GotoCodegenResults {
543
540
reachability,
@@ -585,12 +582,7 @@ impl<'tcx> GotoCodegenResults<'tcx> {
585
582
}
586
583
}
587
584
588
- fn extend (
589
- & mut self ,
590
- gcx : GotocCtx ,
591
- items : Vec < MonoItem < ' tcx > > ,
592
- metadata : Option < HarnessMetadata > ,
593
- ) {
585
+ fn extend ( & mut self , gcx : GotocCtx , items : Vec < MonoItem > , metadata : Option < HarnessMetadata > ) {
594
586
let mut items = items;
595
587
self . harnesses . extend ( metadata) ;
596
588
self . concurrent_constructs . extend ( gcx. concurrent_constructs ) ;
@@ -599,7 +591,7 @@ impl<'tcx> GotoCodegenResults<'tcx> {
599
591
}
600
592
601
593
/// Prints a report at the end of the compilation.
602
- fn print_report ( & self , tcx : TyCtxt < ' tcx > ) {
594
+ fn print_report ( & self , tcx : TyCtxt ) {
603
595
// Print all unsupported constructs.
604
596
if !self . unsupported_constructs . is_empty ( ) {
605
597
// Sort alphabetically.
@@ -631,7 +623,7 @@ impl<'tcx> GotoCodegenResults<'tcx> {
631
623
632
624
// Print some compilation stats.
633
625
if tracing:: enabled!( tracing:: Level :: INFO ) {
634
- analysis:: print_stats ( tcx , & self . items ) ;
626
+ analysis:: print_stats ( & self . items ) ;
635
627
}
636
628
}
637
629
}
0 commit comments