Skip to content

Refactor CTFE and const-prop machines to share common code #71129

Closed
@vakaras

Description

@vakaras

Miri, CTFE, and const-prop are implemented by using a MIR interpreter that can be customized by implementing the Machine trait. Currently, the CTFE and const-prop machines have quite a bit of common code that is duplicated. It would be nice to reduce the code duplication.

One idea that could be used to reduce the code duplication would be to add a new trait CommonMachine and put all the common implementation into it:

pub trait CommonMachine<'mir, 'tcx>: Sized {

    /// Borrow the current thread's stack.
    fn stack(
        ecx: &'a InterpCx<'mir, 'tcx, Self>,
    ) -> &'a [Frame<'mir, 'tcx, (), ()>];

    /// Mutably borrow the current thread's stack.
    fn stack_mut(
        ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
    ) -> &'a mut Vec<Frame<'mir, 'tcx, (), ()>>;

    // Other methods that require custom implementations for the CTFE and const-prop machines.
}

impl<'mir, 'tcx, T: CommonMachine<'mir, 'tcx>> super::machine::Machine<'mir, 'tcx> for T {

    type MemoryKind = !;
    type PointerTag = ();
    type ExtraFnVal = !;

    type FrameExtra = ();
    type MemoryExtra = ();
    type AllocExtra = ();

    #[inline(always)]
    fn enforce_alignment(_memory_extra: &Self::MemoryExtra) -> bool {
        false
    }

    /// Borrow the current thread's stack.
    fn stack(
        ecx: &'a InterpCx<'mir, 'tcx, Self>,
    ) -> &'a [Frame<'mir, 'tcx, (), ()>] {
        CommonMachine::stack(ecx)
    }

    /// Mutably borrow the current thread's stack.
    fn stack_mut(
        ecx: &'a mut InterpCx<'mir, 'tcx, Self>,
    ) -> &'a mut Vec<Frame<'mir, 'tcx, (), ()>> {
        CommonMachine::stack_mut(ecx)
    }

    // TODO: other methods
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-miriArea: The miri toolC-cleanupCategory: PRs that clean code up or issues documenting cleanup.E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions