Skip to content

Commit

Permalink
Set up infrastructure for MidenVM compilation mode (FuelLabs#4173)
Browse files Browse the repository at this point in the history
## Description
Hello! I am beginning work on the MidenVM backend.

This PR closes: FuelLabs#3697 FuelLabs#3696 FuelLabs#3695 and FuelLabs#3694.

I have created a new trivial test under `should_pass/midenvm`. Ideally,
eventually we will be able to run the relevant section of the main test
suite via `supported_targets`. I will seek to do that after more
compilation is built out. For now, this is a POC test that directly
addresses FuelLabs#3696.

Note that the Miden VM ASM builder will be modified heavily going
forward, and is not in a final state. I am submitting this in the spirit
of early feedback, and look forward to the reviews, but note that this
code path should not be considered "production ready" (note the `todo`
panics).

## Checklist

- [x] I have linked to any relevant issues.
- [ ] I have commented my code, particularly in hard-to-understand
areas.
- [ ] I have updated the documentation where relevant (API docs, the
reference, and the Sway book).
- [x] I have added tests that prove my fix is effective or that my
feature works.
- [ ] I have added (or requested a maintainer to add) the necessary
`Breaking*` or `New Feature` labels where relevant.
- [x] I have done my best to ensure that my PR adheres to [the Fuel Labs
Code Review
Standards](https://github.com/FuelLabs/rfcs/blob/master/text/code-standards/external-contributors.md).
- [ ] I have requested a review from the relevant team or maintainers.

---------

Co-authored-by: Alex Hansen <[email protected]>
Co-authored-by: João Matos <[email protected]>
Co-authored-by: Joshua Batty <[email protected]>
  • Loading branch information
4 people authored Feb 27, 2023
1 parent b3af557 commit e8dee04
Show file tree
Hide file tree
Showing 25 changed files with 1,362 additions and 8 deletions.
12 changes: 12 additions & 0 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -253,6 +253,18 @@ jobs:
- name: Cargo Run E2E Tests (EVM)
run: cargo run --locked --release --bin test -- --target evm --locked

cargo-run-e2e-test-midenvm:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Install toolchain
uses: dtolnay/rust-toolchain@master
with:
toolchain: ${{ env.RUST_VERSION }}
- uses: Swatinem/rust-cache@v2
- name: Cargo Run E2E Tests (EVM)
run: cargo run --locked --release --bin test -- --target midenvm --locked

# TODO: Remove this upon merging std tests with the rest of the E2E tests.
cargo-test-lib-std:
runs-on: ubuntu-latest
Expand Down
197 changes: 195 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions forc-pkg/src/pkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,8 @@ impl BuiltPackage {
res?
}
}
// TODO?
ProgramABI::MidenVM(_) => (),
}

info!(" Bytecode size: {} bytes", self.bytecode.len());
Expand Down Expand Up @@ -1727,6 +1729,8 @@ pub fn compile(

ProgramABI::Evm(ops)
}

BuildTarget::MidenVM => ProgramABI::MidenVM(()),
};

let entries = asm_res
Expand Down
1 change: 1 addition & 0 deletions sway-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ hex = { version = "0.4", optional = true }
im = "15.0"
itertools = "0.10"
lazy_static = "1.4"
miden-core = "0.3.0"
pest = "2.1.3"
pest_derive = "2.1"
petgraph = "0.6"
Expand Down
1 change: 1 addition & 0 deletions sway-core/src/asm_generation/abi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ use super::EvmAbiResult;
pub enum ProgramABI {
Fuel(fuel_abi_types::program_abi::ProgramABI),
Evm(EvmAbiResult),
MidenVM(()),
}
6 changes: 5 additions & 1 deletion sway-core/src/asm_generation/asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@ use sway_ir::Function;

use crate::{asm_lang::Label, CompileResult};

use super::{evm::EvmAsmBuilderResult, fuel::fuel_asm_builder::FuelAsmBuilderResult};
use super::{
evm::EvmAsmBuilderResult, fuel::fuel_asm_builder::FuelAsmBuilderResult,
miden_vm::MidenVMAsmBuilderResult,
};

pub enum AsmBuilderResult {
Fuel(FuelAsmBuilderResult),
Evm(EvmAsmBuilderResult),
MidenVM(MidenVMAsmBuilderResult),
}

pub trait AsmBuilder {
Expand Down
10 changes: 10 additions & 0 deletions sway-core/src/asm_generation/finalized_asm.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use super::instruction_set::InstructionSet;
use super::ToMidenBytecode;
use super::{
fuel::{checks, data_section::DataSection},
ProgramABI, ProgramKind,
Expand Down Expand Up @@ -73,6 +74,14 @@ impl FinalizedAsm {
)
}
}
InstructionSet::MidenVM { ops } => ok(
CompiledBytecode {
bytecode: ops.to_bytecode().into(),
config_const_offsets: Default::default(),
},
vec![],
vec![],
),
}
}
}
Expand Down Expand Up @@ -192,5 +201,6 @@ pub fn check_invalid_opcodes(asm: &FinalizedAsm) -> CompileResult<()> {
ProgramKind::Predicate => checks::check_predicate_opcodes(&ops[..]),
},
InstructionSet::Evm { ops: _ } => ok((), vec![], vec![]),
InstructionSet::MidenVM { ops: _ } => ok((), vec![], vec![]),
}
}
3 changes: 3 additions & 0 deletions sway-core/src/asm_generation/from_ir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ use super::{
register_sequencer::RegisterSequencer,
},
programs::{AbstractEntry, AbstractProgram, FinalProgram, ProgramKind},
MidenVMAsmBuilder,
};

use crate::{err, ok, BuildConfig, BuildTarget, CompileResult, CompileWarning};
Expand Down Expand Up @@ -82,6 +83,7 @@ fn compile_module_to_asm(
context,
)),
BuildTarget::EVM => Box::new(EvmAsmBuilder::new(kind, context)),
BuildTarget::MidenVM => Box::new(MidenVMAsmBuilder::new(kind, context)),
};

// Pre-create labels for all functions before we generate other code, so we can call them
Expand Down Expand Up @@ -159,6 +161,7 @@ fn compile_module_to_asm(
ops: result.ops,
abi: result.abi,
},
AsmBuilderResult::MidenVM(result) => FinalProgram::MidenVM { ops: result.ops },
};

ok(final_program, warnings, errors)
Expand Down
4 changes: 2 additions & 2 deletions sway-core/src/asm_generation/fuel/fuel_asm_builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2072,8 +2072,8 @@ impl<'ir> FuelAsmBuilder<'ir> {
})
}

// Same as `opt_value_to_register` but returns a new register if no register is found or if
// `value` is not a constant.
/// Same as [`opt_value_to_register`] but returns a new register if no register is found or if
/// `value` is not a constant.
pub(super) fn value_to_register(&mut self, value: &Value) -> VirtualRegister {
match self.opt_value_to_register(value) {
Some(reg) => reg,
Expand Down
Loading

0 comments on commit e8dee04

Please sign in to comment.