Skip to content
Merged
3 changes: 3 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
- [907](https://github.com/FuelLabs/fuel-vm/pull/907): `StorageRead` and `StorageWrite` traits no longer return the number of bytes written. They already required that the whole buffer is used, but now this is reflected in signature and documentation as well.
- [914](https://github.com/FuelLabs/fuel-vm/pull/914): The built-in profiler is removed. Use the debugger with single-stepping instead.

### Changed
- [904](https://github.com/FuelLabs/fuel-vm/pull/904): Moved the logic of each opcode into its own function. It helps so reduce the size of the `instruction_inner` function, allowing compiler to do better optimizations. The Mira swaps receive performance improvement in 16.5%.

### Fixed
- [895](https://github.com/FuelLabs/fuel-vm/pull/895): Fix elided lifetimes compilation warnings that became errors after the release of rust 1.83.0.
- [895](https://github.com/FuelLabs/fuel-vm/pull/895): Bump proptest-derive to version `0.5.1` to fix non-local impl errors on the derivation of `proptest_derive::Arbitrary` introduced by rust 1.83.0.
Expand Down
14 changes: 9 additions & 5 deletions fuel-asm/src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1233,17 +1233,21 @@ macro_rules! impl_instructions {
impl core::convert::TryFrom<[u8; 4]> for Instruction {
type Error = InvalidOpcode;
fn try_from([op, a, b, c]: [u8; 4]) -> Result<Self, Self::Error> {
match Opcode::try_from(op)? {
let op = match op {
$(
Opcode::$Op => Ok(Self::$Op({
$ix => {
let op = op::$Op([a, b, c]);
if !op.reserved_part_is_zero() {
return Err(InvalidOpcode);
}
op
})),

Self::$Op(op)
},
)*
}
_ => return Err(InvalidOpcode),
};

Ok(op)
}
}

Expand Down
31 changes: 11 additions & 20 deletions fuel-vm/src/constraints/reg_key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use core::ops::{
use fuel_asm::{
PanicReason,
RegId,
RegisterId,
Word,
};

Expand All @@ -34,15 +33,19 @@ pub struct Reg<'r, const INDEX: u8>(&'r Word);
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
/// A key to a writable register that is within
/// the bounds of the writable registers.
pub struct WriteRegKey(usize);
pub struct WriteRegKey(RegId);

impl WriteRegKey {
/// Create a new writable register key if the index is within the bounds
/// of the writable registers.
pub fn new(k: impl Into<usize>) -> Result<Self, PanicReason> {
pub fn new(k: impl Into<RegId>) -> Result<Self, PanicReason> {
let k = k.into();
is_register_writable(&k)?;
Ok(Self(k))

if k >= RegId::WRITABLE {
Ok(Self(k))
} else {
Err(PanicReason::ReservedRegisterNotWritable)
}
}

/// Translate this key from an absolute register index
Expand All @@ -51,19 +54,7 @@ impl WriteRegKey {
/// This subtracts the number of system registers from the key.
#[allow(clippy::arithmetic_side_effects)] // Safety: checked in constructor
fn translate(self) -> usize {
self.0 - VM_REGISTER_SYSTEM_COUNT
}
}

/// Check that the register is above the system registers and below the total
/// number of registers.
pub(crate) fn is_register_writable(r: &RegisterId) -> Result<(), PanicReason> {
const W_USIZE: usize = RegId::WRITABLE.to_u8() as usize;
const RANGE: core::ops::Range<usize> = W_USIZE..(W_USIZE + VM_REGISTER_PROGRAM_COUNT);
if RANGE.contains(r) {
Ok(())
} else {
Err(PanicReason::ReservedRegisterNotWritable)
self.0.to_u8() as usize - VM_REGISTER_SYSTEM_COUNT
}
}

Expand Down Expand Up @@ -368,10 +359,10 @@ impl<'a> From<ProgramRegisters<'a>> for ProgramRegistersRef<'a> {
}
}

impl TryFrom<RegisterId> for WriteRegKey {
impl TryFrom<RegId> for WriteRegKey {
type Error = PanicReason;

fn try_from(r: RegisterId) -> Result<Self, Self::Error> {
fn try_from(r: RegId) -> Result<Self, Self::Error> {
Self::new(r)
}
}
Expand Down
7 changes: 5 additions & 2 deletions fuel-vm/src/constraints/reg_key/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ fn can_split_writes(a: usize, b: usize) -> Option<(Word, Word)> {
.unwrap();
let s = VM_REGISTER_SYSTEM_COUNT;
let mut reg = ProgramRegisters(&mut reg);
reg.get_mut_two(WriteRegKey(s + a), WriteRegKey(s + b))
.map(|(a, b)| (*a, *b))
reg.get_mut_two(
WriteRegKey(RegId::new((s + a) as u8)),
WriteRegKey(RegId::new((s + b) as u8)),
)
.map(|(a, b)| (*a, *b))
}
16 changes: 8 additions & 8 deletions fuel-vm/src/interpreter/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ use crate::{
error::SimpleResult,
};

use fuel_asm::PanicReason;
use fuel_types::{
RegisterId,
Word,
use fuel_asm::{
PanicReason,
RegId,
};
use fuel_types::Word;

mod muldiv;
mod wideint;
Expand All @@ -26,7 +26,7 @@ where
/// Stores the overflowed wrapped value into RegId::OF
pub(crate) fn alu_capture_overflow<F, B, C>(
&mut self,
ra: RegisterId,
ra: RegId,
f: F,
b: B,
c: C,
Expand All @@ -48,7 +48,7 @@ where
/// Set RegId::OF to true and zero the result register if overflow occurred.
pub(crate) fn alu_boolean_overflow<F, B, C>(
&mut self,
ra: RegisterId,
ra: RegId,
f: F,
b: B,
c: C,
Expand All @@ -69,7 +69,7 @@ where

pub(crate) fn alu_error<F, B, C>(
&mut self,
ra: RegisterId,
ra: RegId,
f: F,
b: B,
c: C,
Expand All @@ -89,7 +89,7 @@ where
alu_error(dest, flag.as_ref(), common, f, b, c, err_bool)
}

pub(crate) fn alu_set(&mut self, ra: RegisterId, b: Word) -> SimpleResult<()> {
pub(crate) fn alu_set(&mut self, ra: RegId, b: Word) -> SimpleResult<()> {
let (SystemRegisters { of, err, pc, .. }, mut w) =
split_registers(&mut self.registers);
let dest = &mut w[ra.try_into()?];
Expand Down
10 changes: 5 additions & 5 deletions fuel-vm/src/interpreter/alu/muldiv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ use crate::{
error::SimpleResult,
};

use fuel_asm::PanicReason;
use fuel_types::{
RegisterId,
Word,
use fuel_asm::{
PanicReason,
RegId,
};
use fuel_types::Word;

impl<M, S, Tx, Ecal> Interpreter<M, S, Tx, Ecal>
where
Expand All @@ -22,7 +22,7 @@ where
/// Stores the overflowed wrapped value into RegId::OF
pub(crate) fn alu_muldiv(
&mut self,
ra: RegisterId,
ra: RegId,
lhs: Word,
rhs: Word,
divider: Word,
Expand Down
8 changes: 3 additions & 5 deletions fuel-vm/src/interpreter/alu/wideint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,9 @@ use ethnum::U256;
use fuel_asm::{
wideint::*,
PanicReason,
RegId,
};
use fuel_types::{
RegisterId,
Word,
};
use fuel_types::Word;

use super::super::{
internal::inc_pc,
Expand Down Expand Up @@ -70,7 +68,7 @@ macro_rules! wideint_ops {
{
pub(crate) fn [<alu_wideint_cmp_ $t:lower>](
&mut self,
ra: RegisterId,
ra: RegId,
b: Word,
c: Word,
args: CompareArgs,
Expand Down
4 changes: 2 additions & 2 deletions fuel-vm/src/interpreter/blob.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use fuel_asm::{
RegisterId,
RegId,
Word,
};
use fuel_tx::PanicReason;
Expand Down Expand Up @@ -35,7 +35,7 @@ where
{
pub(crate) fn blob_size(
&mut self,
dst: RegisterId,
dst: RegId,
blob_id_ptr: Word,
) -> IoResult<(), S::DataError> {
let gas_cost = self
Expand Down
28 changes: 10 additions & 18 deletions fuel-vm/src/interpreter/blockchain.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ use alloc::vec::Vec;
use fuel_asm::{
Imm06,
PanicReason,
RegId,
};
use fuel_storage::StorageSize;
use fuel_tx::{
Expand All @@ -69,7 +70,6 @@ use fuel_types::{
BlockHeight,
Bytes32,
ContractId,
RegisterId,
Word,
};

Expand Down Expand Up @@ -237,7 +237,7 @@ where
)
}

pub(crate) fn block_height(&mut self, ra: RegisterId) -> IoResult<(), S::DataError> {
pub(crate) fn block_height(&mut self, ra: RegId) -> IoResult<(), S::DataError> {
let (SystemRegisters { pc, .. }, mut w) = split_registers(&mut self.registers);
let result = &mut w[WriteRegKey::try_from(ra)?];
Ok(block_height(&self.context, pc, result)?)
Expand Down Expand Up @@ -277,11 +277,7 @@ where
.code_root(a, b)
}

pub(crate) fn code_size(
&mut self,
ra: RegisterId,
b: Word,
) -> IoResult<(), S::DataError> {
pub(crate) fn code_size(&mut self, ra: RegId, b: Word) -> IoResult<(), S::DataError> {
let gas_cost = self.gas_costs().csiz();
// Charge only for the `base` execution.
// We will charge for the contracts size in the `code_size`.
Expand All @@ -308,7 +304,7 @@ where
pub(crate) fn state_clear_qword(
&mut self,
a: Word,
rb: RegisterId,
rb: RegId,
c: Word,
) -> IoResult<(), S::DataError> {
let contract_id = self.internal_contract();
Expand All @@ -327,8 +323,8 @@ where

pub(crate) fn state_read_word(
&mut self,
ra: RegisterId,
rb: RegisterId,
ra: RegId,
rb: RegId,
c: Word,
) -> IoResult<(), S::DataError> {
let (SystemRegisters { fp, pc, .. }, mut w) =
Expand Down Expand Up @@ -361,7 +357,7 @@ where
pub(crate) fn state_read_qword(
&mut self,
a: Word,
rb: RegisterId,
rb: RegId,
c: Word,
d: Word,
) -> IoResult<(), S::DataError> {
Expand Down Expand Up @@ -396,7 +392,7 @@ where
pub(crate) fn state_write_word(
&mut self,
a: Word,
rb: RegisterId,
rb: RegId,
c: Word,
) -> IoResult<(), S::DataError> {
let new_storage_gas_per_byte = self.gas_costs().new_storage_per_byte();
Expand Down Expand Up @@ -433,7 +429,7 @@ where
pub(crate) fn state_write_qword(
&mut self,
a: Word,
rb: RegisterId,
rb: RegId,
c: Word,
d: Word,
) -> IoResult<(), S::DataError> {
Expand Down Expand Up @@ -468,11 +464,7 @@ where
)
}

pub(crate) fn timestamp(
&mut self,
ra: RegisterId,
b: Word,
) -> IoResult<(), S::DataError> {
pub(crate) fn timestamp(&mut self, ra: RegId, b: Word) -> IoResult<(), S::DataError> {
let block_height = self.get_block_height()?;
let (SystemRegisters { pc, .. }, mut w) = split_registers(&mut self.registers);
let result = &mut w[WriteRegKey::try_from(ra)?];
Expand Down
4 changes: 2 additions & 2 deletions fuel-vm/src/interpreter/contract.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ use crate::{
};
use fuel_asm::{
PanicReason,
RegisterId,
RegId,
Word,
};
use fuel_storage::StorageSize;
Expand All @@ -60,7 +60,7 @@ where
{
pub(crate) fn contract_balance(
&mut self,
ra: RegisterId,
ra: RegId,
b: Word,
c: Word,
) -> Result<(), RuntimeError<S::DataError>> {
Expand Down
4 changes: 2 additions & 2 deletions fuel-vm/src/interpreter/crypto.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ use bn::{
G1,
G2,
};
use fuel_asm::RegId;
use fuel_crypto::{
Hasher,
Message,
Expand All @@ -36,7 +37,6 @@ use fuel_crypto::{
use fuel_types::{
Bytes32,
Bytes64,
RegisterId,
Word,
};

Expand Down Expand Up @@ -126,7 +126,7 @@ where

pub(crate) fn ec_pairing(
&mut self,
ra: RegisterId,
ra: RegId,
b: Word,
c: Word,
d: Word,
Expand Down
1 change: 1 addition & 0 deletions fuel-vm/src/interpreter/executors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ mod main;
mod predicate;

mod debug;
mod opcodes_impl;

pub use main::predicates;
Loading
Loading