-
Notifications
You must be signed in to change notification settings - Fork 12
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Smir demo #74
Merged
Merged
Smir demo #74
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
# Run a job to ensure formatting is OK | ||
name: Run demo | ||
on: | ||
pull_request: | ||
paths: | ||
- demo/** | ||
|
||
push: | ||
paths: | ||
- demo/** | ||
|
||
jobs: | ||
check_demo: | ||
runs-on: ubuntu-latest | ||
steps: | ||
- name: Checkout | ||
uses: actions/checkout@v3 | ||
|
||
- name: Run Demo | ||
run: ./demo/run_demo.sh |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -9,4 +9,5 @@ members = [ | |
exclude = [ | ||
"build", | ||
"target", | ||
"demo", | ||
] |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
[package] | ||
name = "smir-demo" | ||
description = "A little demo tool on how to use StableMIR to analyze crates" | ||
version = "0.0.0" | ||
edition = "2021" | ||
|
||
[dependencies] | ||
|
||
[package.metadata.rust-analyzer] | ||
# This crate uses #[feature(rustc_private)]. | ||
# See https://github.com/rust-analyzer/rust-analyzer/pull/7891 | ||
rustc_private = true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
use std::env; | ||
use std::path::PathBuf; | ||
|
||
pub fn main() { | ||
// Add rustup to the rpath in order to properly link with the correct rustc version. | ||
let rustup_home = env::var("RUSTUP_HOME").unwrap(); | ||
let toolchain = env::var("RUSTUP_TOOLCHAIN").unwrap(); | ||
let rustc_lib: PathBuf = [&rustup_home, "toolchains", &toolchain, "lib"] | ||
.iter() | ||
.collect(); | ||
println!( | ||
"cargo:rustc-link-arg-bin=smir-demo=-Wl,-rpath,{}", | ||
rustc_lib.display() | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#!/usr/bin/env bash | ||
# Builds and run the demo driver against itself. | ||
# I.e.: This bootstrap itself. | ||
|
||
REPO_DIR=$(git rev-parse --show-toplevel) | ||
DEMO_DIR="${REPO_DIR}/demo" | ||
|
||
cd "${DEMO_DIR}" | ||
cargo run -- src/main.rs --crate-name demo --edition 2021 -C panic=abort | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
[toolchain] | ||
channel = "nightly-2024-04-02" | ||
components = ["llvm-tools-preview", "rustc-dev", "rust-src", "rustfmt"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
//! Small utility that print some information about a crate. | ||
|
||
#![feature(rustc_private)] | ||
#![feature(assert_matches)] | ||
|
||
extern crate rustc_driver; | ||
extern crate rustc_interface; | ||
#[macro_use] | ||
extern crate rustc_smir; | ||
extern crate stable_mir; | ||
|
||
use std::collections::HashSet; | ||
use std::io::stdout; | ||
use rustc_smir::{run, rustc_internal}; | ||
use stable_mir::{CompilerError, CrateDef}; | ||
use std::ops::ControlFlow; | ||
use std::process::ExitCode; | ||
use stable_mir::mir::{LocalDecl, MirVisitor, Terminator, TerminatorKind}; | ||
use stable_mir::mir::mono::Instance; | ||
use stable_mir::mir::visit::Location; | ||
use stable_mir::ty::{RigidTy, Ty, TyKind}; | ||
|
||
|
||
/// This is a wrapper that can be used to replace rustc. | ||
fn main() -> ExitCode { | ||
let rustc_args = std::env::args().into_iter().collect(); | ||
let result = run!(rustc_args, start_demo); | ||
match result { | ||
Ok(_) | Err(CompilerError::Skipped | CompilerError::Interrupted(_)) => ExitCode::SUCCESS, | ||
_ => ExitCode::FAILURE, | ||
} | ||
} | ||
|
||
fn start_demo() -> ControlFlow<()> { | ||
let crate_name = stable_mir::local_crate().name; | ||
eprintln!("--- Analyzing crate: {crate_name}"); | ||
|
||
let crate_items = stable_mir::all_local_items(); | ||
for item in crate_items { | ||
eprintln!(" - {} @{:?}", item.name(), item.span()) | ||
} | ||
|
||
let entry_fn = stable_mir::entry_fn().unwrap(); | ||
let entry_instance = Instance::try_from(entry_fn).unwrap(); | ||
analyze_instance(entry_instance); | ||
ControlFlow::Break(()) | ||
} | ||
|
||
fn analyze_instance(instance: Instance) { | ||
eprintln!("--- Analyzing instance: {}", instance.name()); | ||
eprintln!(" - Mangled name: {}", instance.mangled_name()); | ||
eprintln!(" - FnABI: {:?}", instance.fn_abi().unwrap()); | ||
|
||
let body = instance.body().unwrap(); | ||
let mut visitor = Visitor { | ||
locals: body.locals(), | ||
tys: Default::default(), | ||
fn_calls: Default::default(), | ||
}; | ||
visitor.visit_body(&body); | ||
visitor.tys.iter().for_each(|ty| eprintln!(" - Visited: {ty}")); | ||
visitor.fn_calls.iter().for_each(|instance| eprintln!(" - Call: {}", instance.name())); | ||
|
||
body.dump(&mut stdout().lock(), &instance.name()).unwrap(); | ||
} | ||
|
||
struct Visitor<'a> { | ||
locals: &'a [LocalDecl], | ||
tys: HashSet<Ty>, | ||
fn_calls: HashSet<Instance>, | ||
} | ||
|
||
impl<'a> MirVisitor for Visitor<'a> { | ||
fn visit_terminator(&mut self, term: &Terminator, _location: Location) { | ||
match term.kind { | ||
TerminatorKind::Call { ref func, .. } => { | ||
let op_ty = func.ty(self.locals).unwrap(); | ||
let TyKind::RigidTy(RigidTy::FnDef(def, args)) = op_ty.kind() else { return; }; | ||
self.fn_calls.insert(Instance::resolve(def, &args).unwrap()); | ||
} | ||
_ => {} | ||
} | ||
} | ||
|
||
fn visit_ty(&mut self, ty: &Ty, _location: Location) { | ||
self.tys.insert(*ty); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this something all users need to do? Can we do anything on the rustc side to make this simpler?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think users have to do this if they want to be able to invoke their binaries directly. If you run the program under cargo or rustup run, it should configure the environment correctly for you.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I.e.: if you remove this build file, the following still works:
But if you run the binary directly: