Skip to content
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

"memory" opcode sanity checks #1444

Merged
merged 29 commits into from
Oct 24, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
d912246
WIP Broken
MitchTurner Oct 18, 2023
c7e318f
Add first test
MitchTurner Oct 19, 2023
1f47b1d
Add other tests, not sure if correct
MitchTurner Oct 19, 2023
de5f327
Add dependent k256 benches, move helper to utils
MitchTurner Oct 19, 2023
590bd77
Add s256 benchmark
MitchTurner Oct 19, 2023
c5cc553
Update changelog
MitchTurner Oct 19, 2023
1aa43d7
Remove comment
MitchTurner Oct 19, 2023
baf119c
Remove other commented code
MitchTurner Oct 19, 2023
10f4b29
Add invalid case, remove unused opcode
MitchTurner Oct 20, 2023
52607e2
Add first benchmark
MitchTurner Oct 19, 2023
3b66c7a
Update CHANGELOG
MitchTurner Oct 19, 2023
ed2cc12
WIP
MitchTurner Oct 20, 2023
10fc114
Kinda finish flow opcodes, organize others
MitchTurner Oct 20, 2023
45f895d
Remove unused import
MitchTurner Oct 20, 2023
f85e595
Appease Clippy-sama
MitchTurner Oct 20, 2023
278d056
Merge remote-tracking branch 'origin/master' into flow-op-code-sanity…
MitchTurner Oct 23, 2023
c92da04
Rename function
MitchTurner Oct 23, 2023
bebd99a
Fix bad import
MitchTurner Oct 23, 2023
e874e0b
Add relative jumps
MitchTurner Oct 23, 2023
8c2abd7
WIP
MitchTurner Oct 23, 2023
148aac1
Restructure, add 4 benches
MitchTurner Oct 23, 2023
38c5d83
Update CHANGELOG
MitchTurner Oct 23, 2023
5d37136
Add all remaining except pop/push ops
MitchTurner Oct 23, 2023
3f3e539
Fix import
MitchTurner Oct 23, 2023
282f8c7
Fix import
MitchTurner Oct 24, 2023
8474be7
Merge branch 'master' into mem-op-code-sanity-check
xgreenx Oct 24, 2023
33b2b1b
Aloc more, simplify meq, create worst-case pushes
MitchTurner Oct 24, 2023
61e5447
Get pop benchmarks passing
MitchTurner Oct 24, 2023
f0465b3
Merge branch 'master' into mem-op-code-sanity-check
MitchTurner Oct 24, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,11 @@ Description of the upcoming release here.

### Added

- [#1433](https://github.com/FuelLabs/fuel-core/pull/1433): Add "sanity" benchmarks for flow opcodes
- [#1430](https://github.com/FuelLabs/fuel-core/pull/1430): Add "sanity" benchmarks for crypto opcodes
- [#1444](https://github.com/FuelLabs/fuel-core/pull/1444): Add "sanity" benchmarks for memory opcodes.
- [#1437](https://github.com/FuelLabs/fuel-core/pull/1437): Add some transaction throughput tests for basic transfers.
- [#1436](https://github.com/FuelLabs/fuel-core/pull/1436): Add a github action to continuously test beta-4.
- [#1433](https://github.com/FuelLabs/fuel-core/pull/1433): Add "sanity" benchmarks for flow opcodes.
- [#1430](https://github.com/FuelLabs/fuel-core/pull/1430): Add "sanity" benchmarks for crypto opcodes.
- [#1437](https://github.com/FuelLabs/fuel-core/pull/1437): Add some transaction throughput tests for basic transfers.
- [#1432](https://github.com/FuelLabs/fuel-core/pull/1432): Add a new `--api-request-timeout` argument to control TTL for GraphQL requests.
- [#1426](https://github.com/FuelLabs/fuel-core/pull/1426) Split keygen into a create and a binary
- [#1419](https://github.com/FuelLabs/fuel-core/pull/1419): Add additional "sanity" benchmarks for arithmetic op code instructions.
Expand Down
3 changes: 3 additions & 0 deletions benches/benches/block_target_gas.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use block_target_gas_set::{
contract::run_contract,
crypto::run_crypto,
flow::run_flow,
memory::run_memory,
};
use criterion::{
criterion_group,
Expand Down Expand Up @@ -183,6 +184,8 @@ fn block_target_gas(c: &mut Criterion) {

run_flow(&mut group);

run_memory(&mut group);

group.finish();
}

Expand Down
7 changes: 0 additions & 7 deletions benches/benches/block_target_gas_set/alu.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,6 @@ pub fn run_alu(group: &mut BenchmarkGroup<WallTime>) {
vec![],
);

run(
"alu/aloc opcode",
group,
[op::aloc(0x10), op::jmpb(RegId::ZERO, 0)].to_vec(),
vec![],
);

run(
"alu/and opcode",
group,
Expand Down
293 changes: 293 additions & 0 deletions benches/benches/block_target_gas_set/memory.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,293 @@
use crate::{
utils::{
arb_dependent_cost_values,
set_full_word,
},
*,
};

// ALOC: Allocate memory
// CFE: Extend call frame
// CFEI: Extend call frame immediate
// CFS: Shrink call frame
// CFSI: Shrink call frame immediate
// LB: Load byte
// LW: Load word
// MCL: Memory clear
// MCLI: Memory clear immediate
// MCP: Memory copy
// MCPI: Memory copy immediate
// MEQ: Memory equality
// POPH: Pop a set of high registers from stack
// POPL: Pop a set of low registers from stack
// PSHH: Push a set of high registers to stack
// PSHL: Push a set of low registers to stack
// SB: Store byte
// SW: Store word
pub fn run_memory(group: &mut BenchmarkGroup<WallTime>) {
run(
"memory/aloc opcode",
group,
[
op::movi(0x10, 1000),
op::aloc(0x10),
op::jmpb(RegId::ZERO, 0),
]
.to_vec(),
vec![],
);

// Extend by 10 10 times and then shrink by 100 once. This is to hopefully allow the extend to be
// the dominant opcode
run(
"memory/cfe opcode",
group,
vec![
op::movi(0x10, 10),
op::movi(0x11, 100),
op::cfe(0x10),
op::cfe(0x10),
op::cfe(0x10),
op::cfe(0x10),
op::cfe(0x10),
op::cfe(0x10),
op::cfe(0x10),
op::cfe(0x10),
op::cfe(0x10),
op::cfe(0x10),
op::cfs(0x11),
op::jmpb(RegId::ZERO, 10),
],
vec![],
);

// Extend by 10 10 times and then shrink by 100 once. This is to hopefully allow the extend to be
// the dominant opcode
run(
"memory/cfei opcode",
group,
vec![
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfei(10),
op::cfsi(100),
op::jmpb(RegId::ZERO, 10),
],
vec![],
);

// Extend by 100 once and then shrink by 10 10 times. This is to hopefully allow the shrink to
// be the dominant opcode
run(
"memory/cfs opcode",
group,
vec![
op::movi(0x10, 100),
op::movi(0x11, 10),
op::cfe(0x10),
op::cfs(0x11),
op::cfs(0x11),
op::cfs(0x11),
op::cfs(0x11),
op::cfs(0x11),
op::cfs(0x11),
op::cfs(0x11),
op::cfs(0x11),
op::cfs(0x11),
op::cfs(0x11),
op::jmpb(RegId::ZERO, 10),
],
vec![],
);

// Extend by 100 once and then shrink by 10 10 times. This is to hopefully allow the shrink to
// be the dominant opcode
run(
"memory/cfsi opcode",
group,
vec![
op::cfei(100),
op::cfsi(10),
op::cfsi(10),
op::cfsi(10),
op::cfsi(10),
op::cfsi(10),
op::cfsi(10),
op::cfsi(10),
op::cfsi(10),
op::cfsi(10),
op::cfsi(10),
op::jmpb(RegId::ZERO, 10),
],
vec![],
);

run(
"memory/lb opcode",
group,
[op::lb(0x10, RegId::ONE, 10), op::jmpb(RegId::ZERO, 0)].to_vec(),
vec![],
);

run(
"memory/lw opcode",
group,
[op::lw(0x10, RegId::ONE, 10), op::jmpb(RegId::ZERO, 0)].to_vec(),
vec![],
);

for i in arb_dependent_cost_values() {
let id = format!("memory/mcl opcode {:?}", i);
run(
&id,
group,
vec![
op::movi(0x11, i),
op::aloc(0x11),
op::move_(0x10, RegId::HP),
op::mcl(0x10, 0x11),
op::jmpb(RegId::ZERO, 0),
],
vec![],
);
}

for i in arb_dependent_cost_values() {
let id = format!("memory/mcli opcode {:?}", i);
run(
&id,
group,
vec![
op::movi(0x11, i),
op::aloc(0x11),
op::move_(0x10, RegId::HP),
op::mcli(0x10, i),
op::jmpb(RegId::ZERO, 0),
],
vec![],
);
}

for i in arb_dependent_cost_values() {
let id = format!("memory/mcp opcode {:?}", i);
run(
&id,
group,
vec![
op::movi(0x11, i),
op::aloc(0x11),
op::move_(0x10, RegId::HP),
op::mcp(0x10, RegId::ZERO, 0x11),
op::jmpb(RegId::ZERO, 0),
],
vec![],
);
}

let valid_values: Vec<_> = arb_dependent_cost_values()
.iter()
.copied()
.take_while(|p| *p < (1 << 12)) // 12 bits
.collect();
for val in valid_values {
let id = format!("memory/mcpi opcode {:?}", val);
let val_as_u16 = (val).try_into().unwrap();
run(
&id,
group,
vec![
op::movi(0x11, val),
op::aloc(0x11),
op::move_(0x10, RegId::HP),
op::mcpi(0x10, RegId::ZERO, val_as_u16),
op::jmpb(RegId::ZERO, 0),
],
vec![],
);
}

for i in arb_dependent_cost_values() {
let id = format!("memory/meq opcode {:?}", i);
let mut script = set_full_word(0x13, i as u64);
script.extend(vec![
op::meq(0x10, RegId::ZERO, RegId::ZERO, 0x13),
op::jmpb(RegId::ZERO, 0),
]);
run(&id, group, script, vec![]);
}

let full_mask = (1 << 24) - 1;

// Assumes that `pshh` has a correct cost
run(
"memory/poph opcode",
group,
vec![
op::pshh(full_mask),
op::poph(full_mask),
op::jmpb(RegId::ZERO, 1),
],
vec![],
);

// Assumes that `pshl` has a correct cost
run(
"memory/popl opcode",
group,
vec![
op::pshl(full_mask),
op::popl(full_mask),
op::jmpb(RegId::ZERO, 1),
],
vec![],
);

run(
"memory/pshh opcode",
group,
vec![op::pshh(full_mask), op::jmpb(RegId::ZERO, 0)],
vec![],
);

run(
"memory/pshl opcode",
group,
vec![op::pshl(full_mask), op::jmpb(RegId::ZERO, 0)],
vec![],
);

run(
"memory/sb opcode",
group,
vec![
op::aloc(RegId::ONE),
op::move_(0x10, RegId::HP),
op::movi(0x11, 50),
op::sb(0x10, 0x11, 0),
op::jmpb(RegId::ZERO, 0),
],
vec![],
);

run(
"memory/sw opcode",
group,
vec![
op::movi(0x10, 8),
op::aloc(0x10),
op::move_(0x10, RegId::HP),
op::movi(0x11, 50),
op::sw(0x10, 0x11, 0),
op::jmpb(RegId::ZERO, 0),
],
vec![],
);
}
2 changes: 2 additions & 0 deletions benches/benches/block_target_gas_set/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,5 @@ pub mod crypto;
pub mod flow;

pub mod contract;

pub mod memory;
26 changes: 22 additions & 4 deletions benches/benches/utils.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,15 @@
use core::iter::successors;
use ethnum::U256;
use fuel_core_types::fuel_asm::{
op,
Instruction,
RegId,
use fuel_core_types::{
fuel_asm::{
op,
Instruction,
RegId,
},
fuel_types::{
RegisterId,
Word,
},
};

/// Allocates a byte array from heap and initializes it. Then points `reg` to it.
Expand Down Expand Up @@ -37,3 +43,15 @@ pub fn arb_dependent_cost_values() -> Vec<u32> {
linear.extend(l);
linear
}

/// Set a register `r` to a Word-sized number value using left-shifts
pub fn set_full_word(r: RegisterId, v: Word) -> Vec<Instruction> {
let r = u8::try_from(r).unwrap();
let mut ops = vec![op::movi(r, 0)];
for byte in v.to_be_bytes() {
ops.push(op::ori(r, r, byte as u16));
ops.push(op::slli(r, r, 8));
}
ops.pop().unwrap(); // Remove last shift
ops
}
Loading
Loading