@@ -16,7 +16,7 @@ use crate::kani_middle::{check_reachable_items, dump_mir_items};
1616use crate :: kani_queries:: QueryDb ;
1717use cbmc:: goto_program:: Location ;
1818use cbmc:: irep:: goto_binary_serde:: write_goto_binary_file;
19- use cbmc:: RoundingMode ;
19+ use cbmc:: { InternString , RoundingMode } ;
2020use cbmc:: { InternedString , MachineModel } ;
2121use kani_metadata:: artifact:: convert_type;
2222use kani_metadata:: CompilerArtifactStub ;
@@ -32,12 +32,10 @@ use rustc_data_structures::fx::{FxHashMap, FxIndexMap};
3232use rustc_data_structures:: temp_dir:: MaybeTempDir ;
3333use rustc_errors:: { ErrorGuaranteed , DEFAULT_LOCALE_RESOURCE } ;
3434use rustc_hir:: def_id:: LOCAL_CRATE ;
35- use rustc_hir:: definitions:: DefPathHash ;
3635use rustc_metadata:: creader:: MetadataLoaderDyn ;
3736use rustc_metadata:: fs:: { emit_wrapper_file, METADATA_FILENAME } ;
3837use rustc_metadata:: EncodedMetadata ;
3938use rustc_middle:: dep_graph:: { WorkProduct , WorkProductId } ;
40- use rustc_middle:: mir:: mono:: MonoItem ;
4139use rustc_middle:: ty:: TyCtxt ;
4240use rustc_middle:: util:: Providers ;
4341use rustc_session:: config:: { CrateType , OutputFilenames , OutputType } ;
@@ -46,7 +44,7 @@ use rustc_session::Session;
4644use rustc_smir:: rustc_internal;
4745use rustc_target:: abi:: Endian ;
4846use rustc_target:: spec:: PanicStrategy ;
49- use stable_mir:: mir:: mono:: MonoItem as MonoItemStable ;
47+ use stable_mir:: mir:: mono:: { Instance , MonoItem } ;
5048use stable_mir:: CrateDef ;
5149use std:: any:: Any ;
5250use std:: collections:: BTreeMap ;
@@ -82,10 +80,10 @@ impl GotocCodegenBackend {
8280 fn codegen_items < ' tcx > (
8381 & self ,
8482 tcx : TyCtxt < ' tcx > ,
85- starting_items : & [ MonoItem < ' tcx > ] ,
83+ starting_items : & [ MonoItem ] ,
8684 symtab_goto : & Path ,
8785 machine_model : & MachineModel ,
88- ) -> ( GotocCtx < ' tcx > , Vec < MonoItem < ' tcx > > ) {
86+ ) -> ( GotocCtx < ' tcx > , Vec < MonoItem > ) {
8987 let items = with_timer (
9088 || collect_reachable_items ( tcx, starting_items) ,
9189 "codegen reachability analysis" ,
@@ -103,17 +101,13 @@ impl GotocCodegenBackend {
103101 for item in & items {
104102 match * item {
105103 MonoItem :: Fn ( instance) => {
106- let instance = rustc_internal:: stable ( instance) ;
107104 gcx. call_with_panic_debug_info (
108105 |ctx| ctx. declare_function ( instance) ,
109106 format ! ( "declare_function: {}" , instance. name( ) ) ,
110107 instance. def ,
111108 ) ;
112109 }
113- MonoItem :: Static ( _) => {
114- let MonoItemStable :: Static ( def) = rustc_internal:: stable ( item) else {
115- unreachable ! ( )
116- } ;
110+ MonoItem :: Static ( def) => {
117111 gcx. call_with_panic_debug_info (
118112 |ctx| ctx. declare_static ( def) ,
119113 format ! ( "declare_static: {}" , def. name( ) ) ,
@@ -128,7 +122,6 @@ impl GotocCodegenBackend {
128122 for item in & items {
129123 match * item {
130124 MonoItem :: Fn ( instance) => {
131- let instance = rustc_internal:: stable ( instance) ;
132125 gcx. call_with_panic_debug_info (
133126 |ctx| ctx. codegen_function ( instance) ,
134127 format ! (
@@ -139,10 +132,7 @@ impl GotocCodegenBackend {
139132 instance. def ,
140133 ) ;
141134 }
142- MonoItem :: Static ( _) => {
143- let MonoItemStable :: Static ( def) = rustc_internal:: stable ( item) else {
144- unreachable ! ( )
145- } ;
135+ MonoItem :: Static ( def) => {
146136 gcx. call_with_panic_debug_info (
147137 |ctx| ctx. codegen_static ( def) ,
148138 format ! ( "codegen_static: {}" , def. name( ) ) ,
@@ -237,28 +227,31 @@ impl CodegenBackend for GotocCodegenBackend {
237227 ReachabilityType :: Harnesses => {
238228 // Cross-crate collecting of all items that are reachable from the crate harnesses.
239229 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 ( ) ) ;
241231 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 ( ) )
244234 } ) ;
245235 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 ) ;
252245 }
253246 }
254247 ReachabilityType :: Tests => {
255248 // We're iterating over crate items here, so what we have to codegen is the "test description" containing the
256249 // test closure that we want to execute
257250 // TODO: Refactor this code so we can guarantee that the pair (test_fn, test_desc) actually match.
258251 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 ) ;
262255 true
263256 } else {
264257 false
@@ -289,11 +282,15 @@ impl CodegenBackend for GotocCodegenBackend {
289282 }
290283 ReachabilityType :: None => { }
291284 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 < _ > > ( ) ;
297294 let model_path = base_filename. with_extension ( ArtifactType :: SymTabGoto ) ;
298295 let ( gcx, items) = self . codegen_items (
299296 tcx,
@@ -527,17 +524,17 @@ where
527524 }
528525}
529526
530- struct GotoCodegenResults < ' tcx > {
527+ struct GotoCodegenResults {
531528 reachability : ReachabilityType ,
532529 harnesses : Vec < HarnessMetadata > ,
533530 unsupported_constructs : UnsupportedConstructs ,
534531 concurrent_constructs : UnsupportedConstructs ,
535- items : Vec < MonoItem < ' tcx > > ,
532+ items : Vec < MonoItem > ,
536533 crate_name : InternedString ,
537534 machine_model : MachineModel ,
538535}
539536
540- impl < ' tcx > GotoCodegenResults < ' tcx > {
537+ impl GotoCodegenResults {
541538 pub fn new ( tcx : TyCtxt , reachability : ReachabilityType ) -> Self {
542539 GotoCodegenResults {
543540 reachability,
@@ -585,12 +582,7 @@ impl<'tcx> GotoCodegenResults<'tcx> {
585582 }
586583 }
587584
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 > ) {
594586 let mut items = items;
595587 self . harnesses . extend ( metadata) ;
596588 self . concurrent_constructs . extend ( gcx. concurrent_constructs ) ;
@@ -599,7 +591,7 @@ impl<'tcx> GotoCodegenResults<'tcx> {
599591 }
600592
601593 /// Prints a report at the end of the compilation.
602- fn print_report ( & self , tcx : TyCtxt < ' tcx > ) {
594+ fn print_report ( & self , tcx : TyCtxt ) {
603595 // Print all unsupported constructs.
604596 if !self . unsupported_constructs . is_empty ( ) {
605597 // Sort alphabetically.
@@ -631,7 +623,7 @@ impl<'tcx> GotoCodegenResults<'tcx> {
631623
632624 // Print some compilation stats.
633625 if tracing:: enabled!( tracing:: Level :: INFO ) {
634- analysis:: print_stats ( tcx , & self . items ) ;
626+ analysis:: print_stats ( & self . items ) ;
635627 }
636628 }
637629}
0 commit comments