Skip to content

Commit

Permalink
Rollup merge of #110315 - oli-obk:mor_smir, r=WaffleLapkin
Browse files Browse the repository at this point in the history
Add a stable MIR way to get the main function

This is useful for analysis tools that only analyze the code paths that a specific program actually goes through. Or for code generators built on top of stable MIR.
  • Loading branch information
JohnTitor authored Apr 14, 2023
2 parents 35a3493 + e404e77 commit 4259133
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
4 changes: 4 additions & 0 deletions compiler/rustc_smir/src/rustc_smir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ pub fn all_local_items() -> stable_mir::CrateItems {
with(|tcx| tcx.mir_keys(()).iter().map(|item| crate_item(item.to_def_id())).collect())
}

pub fn entry_fn() -> Option<stable_mir::CrateItem> {
with(|tcx| Some(crate_item(tcx.entry_fn(())?.0)))
}

/// Build a stable mir crate from a given crate number.
fn smir_crate(tcx: TyCtxt<'_>, crate_num: CrateNum) -> stable_mir::Crate {
let crate_name = tcx.crate_name(crate_num).to_string();
Expand Down
7 changes: 7 additions & 0 deletions compiler/rustc_smir/src/stable_mir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,13 @@ impl CrateItem {
}
}

/// Return the function where execution starts if the current
/// crate defines that. This is usually `main`, but could be
/// `start` if the crate is a no-std crate.
pub fn entry_fn() -> Option<CrateItem> {
crate::rustc_smir::entry_fn()
}

/// Access to the local crate.
pub fn local_crate() -> Crate {
crate::rustc_smir::local_crate()
Expand Down
2 changes: 2 additions & 0 deletions tests/ui-fulldeps/stable-mir/crate-info.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ fn test_stable_mir(tcx: TyCtxt<'_>) {
let local = stable_mir::local_crate();
assert_eq!(&local.name, CRATE_NAME);

assert_eq!(stable_mir::entry_fn(), None);

// Find items in the local crate.
let items = stable_mir::all_local_items();
assert!(get_item(tcx, &items, (DefKind::Fn, "foo_bar")).is_some());
Expand Down

0 comments on commit 4259133

Please sign in to comment.