Skip to content

StableMIR: Implement CompilerInterface #139852

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

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

makai410
Copy link
Contributor

@makai410 makai410 commented Apr 15, 2025

This PR implements part of the document.

With TablesWrapper wrapped by CompilerInterface, the stable-mir's TLV stores a pointer to CompilerInterface, while the rustc-specific TLV stores a pointer to tables.

@rustbot
Copy link
Collaborator

rustbot commented Apr 15, 2025

r? @scottmcm

rustbot has assigned @scottmcm.
They will have a look at your PR within the next two weeks and either review your PR or reassign to another reviewer.

Use r? to explicitly pick a reviewer

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Apr 15, 2025
@makai410
Copy link
Contributor Author

r? @celinval btw should we create a tracking issue for this?

@rustbot rustbot assigned celinval and unassigned scottmcm Apr 15, 2025
@@ -233,7 +234,12 @@ where
mir_consts: IndexMap::default(),
layouts: IndexMap::default(),
}));
stable_mir::compiler_interface::run(&tables, || init(&tables, f))

let interface = CompilerInterface { cx: tables };
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I put the interface here since I'd prefer not to refactor the launch process now. It would be more appropriate to do this at the end.

Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Awesome!

/// Stable-MIR’s interface to compiler internals.
/// All internal queries must go through [`Context`].
///
/// Do not use this directly.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you please document that this is currently used in the macro expansion?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm tbh it is not. We directly create a CompilerInterface in the rustc_internal::run() function for now, which is also called by pretty::write_smir_pretty(), not only the run_driver!.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

but yeah, maybe we can make it into a macro

Copy link
Contributor Author

@makai410 makai410 Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh my bad! I got it wrong...

// A thread local variable that stores a pointer to the tables mapping between TyCtxt
// datastructures and stable MIR datastructures
/// Stable-MIR’s interface to compiler internals.
/// All internal queries must go through [`Context`].
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove Context in this PR?

Also, I think it may be worth documenting this a bit better, so people know why this is here. Something on the lines that this structure will serve as a bridge between StableMIR and the rustc_smir which will provide similar APIs but based on internal rustc constructs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we remove Context in this PR?

Sorry that i didn't get it. You mean we directly write something like impl<'tcx> TablesWrapper<'tcx>, then move everything that used to be in Context to there?

Copy link
Contributor Author

@makai410 makai410 Apr 16, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh and then we create a brand-new Context in rustc_smir, and gradually move methods from TablesWrapper to it until TablesWrapper is completely hollowed out.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, you don't need the trait anymore. Next step I believe would be to move the stable / internal calls out of the TablesWrapper.

Btw, you could rename TablesWrapper to Context too. ☺️

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also, feel free to improve the name scheme, it's not my strongest skill. LoL

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmmm what if we change CompilerInterface to SmirInterface?🤔

scoped_tls::scoped_thread_local!(static TLV: Cell<*const ()>);

pub fn run<F, T>(context: &dyn Context, f: F) -> Result<T, Error>
pub fn run<'tcx, T, F>(interface: &CompilerInterface<'tcx>, f: F) -> Result<T, Error>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@oli-obk Any chance you can take a look at this TLV magic? I could use a second pair of eyes here. Thanks!

pub(crate) struct Context<'tcx>(pub RefCell<Tables<'tcx>>);

impl<'tcx> Context<'tcx> {
pub(crate) fn target_info(&self) -> MachineInfo {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You might as well make these public so we don't have to update the visibility when we pull the StableMir stuff back to its crate

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

 - With `Context` wrapped by `SmirInterface`, the stable-mir's TLV stores a pointer to `SmirInterface`, while the rustc-specific TLV stores a pointer to tables.
 - This PR make the `rustc_smir` mod public.
Copy link
Contributor

@celinval celinval left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good! Thanks.

/// similar APIs but based on internal rustc constructs.
///
/// Do not use this directly. This is currently used in the macro expansion.
pub struct SmirInterface<'tcx> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

On the other hand, this one should be pub(crate), since users of stable_mir should not interact with it.

use stable_mir::ty::IndexedVal;

use crate::rustc_smir::context::TablesWrapper;
use crate::rustc_smir::context::Context;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if we should call this SmirContext

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

SmirCtxt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants