Skip to content

Commit d3741a6

Browse files
MitchTurnerxgreenx
andauthored
"memory" opcode sanity checks (#1444)
#1386 ``` block target estimation/memory/aloc opcode time: [1.7153 ms 1.7225 ms 1.7304 ms] block target estimation/memory/cfe opcode time: [1.6035 ms 1.6133 ms 1.6242 ms] block target estimation/memory/cfei opcode time: [1.6067 ms 1.6161 ms 1.6261 ms] block target estimation/memory/cfs opcode time: [1.5815 ms 1.5926 ms 1.6051 ms] block target estimation/memory/cfsi opcode time: [1.6355 ms 1.6428 ms 1.6513 ms] block target estimation/memory/lb opcode time: [1.7495 ms 1.7592 ms 1.7703 ms] block target estimation/memory/lw opcode time: [1.7579 ms 1.7699 ms 1.7828 ms] block target estimation/memory/mcl opcode 1 time: [2.1388 ms 2.1553 ms 2.1799 ms] block target estimation/memory/mcl opcode 10 time: [2.1554 ms 2.1661 ms 2.1769 ms] block target estimation/memory/mcl opcode 100 time: [2.1288 ms 2.1415 ms 2.1547 ms] block target estimation/memory/mcl opcode 1000 time: [2.7993 ms 2.8172 ms 2.8354 ms] block target estimation/memory/mcl opcode 10000 time: [4.0290 ms 4.0541 ms 4.0791 ms] block target estimation/memory/mcl opcode 19753 time: [4.9775 ms 5.0025 ms 5.0281 ms] block target estimation/memory/mcl opcode 29629 time: [5.0122 ms 5.0411 ms 5.0712 ms] block target estimation/memory/mcl opcode 44444 time: [4.9450 ms 4.9724 ms 5.0005 ms] block target estimation/memory/mcl opcode 66666 time: [4.9388 ms 4.9640 ms 4.9899 ms] block target estimation/memory/mcl opcode 100000 time: [4.9764 ms 5.0030 ms 5.0310 ms] block target estimation/memory/mcli opcode 1 time: [2.0453 ms 2.0548 ms 2.0648 ms] block target estimation/memory/mcli opcode 10 time: [2.0375 ms 2.0480 ms 2.0585 ms] block target estimation/memory/mcli opcode 100 time: [2.0586 ms 2.0684 ms 2.0784 ms] block target estimation/memory/mcli opcode 1000 time: [2.7113 ms 2.7287 ms 2.7477 ms] block target estimation/memory/mcli opcode 10000 time: [3.9312 ms 3.9568 ms 3.9852 ms] block target estimation/memory/mcli opcode 19753 time: [4.9118 ms 4.9353 ms 4.9589 ms] block target estimation/memory/mcli opcode 29629 time: [4.9363 ms 4.9627 ms 4.9899 ms] block target estimation/memory/mcli opcode 44444 time: [4.8455 ms 4.8674 ms 4.8902 ms] block target estimation/memory/mcli opcode 66666 time: [4.8760 ms 4.9036 ms 4.9337 ms] block target estimation/memory/mcli opcode 100000 time: [4.9923 ms 5.0176 ms 5.0431 ms] block target estimation/memory/mcp opcode 1 time: [2.2783 ms 2.2892 ms 2.3000 ms] block target estimation/memory/mcp opcode 10 time: [2.2653 ms 2.2751 ms 2.2855 ms] block target estimation/memory/mcp opcode 100 time: [2.3478 ms 2.3608 ms 2.3740 ms] block target estimation/memory/mcp opcode 1000 time: [2.6947 ms 2.7140 ms 2.7390 ms] block target estimation/memory/mcp opcode 10000 time: [2.9727 ms 2.9851 ms 2.9976 ms] block target estimation/memory/mcp opcode 19753 time: [4.0645 ms 4.0992 ms 4.1429 ms] block target estimation/memory/mcp opcode 29629 time: [4.5143 ms 4.5370 ms 4.5602 ms] block target estimation/memory/mcp opcode 44444 time: [4.6528 ms 4.6742 ms 4.6967 ms] block target estimation/memory/mcp opcode 66666 time: [4.6505 ms 4.6744 ms 4.7010 ms] block target estimation/memory/mcp opcode 100000 time: [3.5548 ms 3.5713 ms 3.5883 ms] block target estimation/memory/mcpi opcode 1 time: [1.2296 ms 1.2352 ms 1.2405 ms] block target estimation/memory/mcpi opcode 10 time: [1.2135 ms 1.2188 ms 1.2240 ms] block target estimation/memory/mcpi opcode 100 time: [1.2760 ms 1.2816 ms 1.2873 ms] block target estimation/memory/mcpi opcode 1000 time: [1.4219 ms 1.4307 ms 1.4398 ms] block target estimation/memory/meq opcode 1 time: [1.9330 ms 1.9430 ms 1.9534 ms] block target estimation/memory/meq opcode 10 time: [1.9648 ms 1.9738 ms 1.9830 ms] block target estimation/memory/meq opcode 100 time: [1.9911 ms 2.0000 ms 2.0092 ms] block target estimation/memory/meq opcode 1000 time: [2.4182 ms 2.4293 ms 2.4405 ms] block target estimation/memory/meq opcode 10000 time: [3.3582 ms 3.3748 ms 3.3913 ms] block target estimation/memory/meq opcode 19753 time: [4.0710 ms 4.0870 ms 4.1029 ms] block target estimation/memory/meq opcode 29629 time: [4.0880 ms 4.1121 ms 4.1450 ms] block target estimation/memory/meq opcode 44444 time: [4.0922 ms 4.1076 ms 4.1230 ms] block target estimation/memory/meq opcode 66666 time: [4.1181 ms 4.1365 ms 4.1550 ms] block target estimation/memory/meq opcode 100000 time: [4.0834 ms 4.1013 ms 4.1201 ms] block target estimation/memory/poph opcode time: [1.5574 ms 1.5661 ms 1.5745 ms] block target estimation/memory/popl opcode time: [1.4805 ms 1.4882 ms 1.4964 ms] block target estimation/memory/pshh opcode time: [3.6456 ms 3.6765 ms 3.7107 ms] block target estimation/memory/pshl opcode time: [3.5495 ms 3.5675 ms 3.5862 ms] block target estimation/memory/sb opcode time: [1.8727 ms 1.8832 ms 1.8946 ms] block target estimation/memory/sw opcode time: [1.8648 ms 1.8746 ms 1.8846 ms] ``` --------- Co-authored-by: xgreenx <[email protected]>
1 parent ba21e24 commit d3741a6

File tree

7 files changed

+328
-27
lines changed

7 files changed

+328
-27
lines changed

CHANGELOG.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,11 @@ Description of the upcoming release here.
1010

1111
### Added
1212

13-
- [#1433](https://github.com/FuelLabs/fuel-core/pull/1433): Add "sanity" benchmarks for flow opcodes
14-
- [#1430](https://github.com/FuelLabs/fuel-core/pull/1430): Add "sanity" benchmarks for crypto opcodes
13+
- [#1444](https://github.com/FuelLabs/fuel-core/pull/1444): Add "sanity" benchmarks for memory opcodes.
14+
- [#1437](https://github.com/FuelLabs/fuel-core/pull/1437): Add some transaction throughput tests for basic transfers.
1515
- [#1436](https://github.com/FuelLabs/fuel-core/pull/1436): Add a github action to continuously test beta-4.
16+
- [#1433](https://github.com/FuelLabs/fuel-core/pull/1433): Add "sanity" benchmarks for flow opcodes.
1617
- [#1430](https://github.com/FuelLabs/fuel-core/pull/1430): Add "sanity" benchmarks for crypto opcodes.
17-
- [#1437](https://github.com/FuelLabs/fuel-core/pull/1437): Add some transaction throughput tests for basic transfers.
1818
- [#1432](https://github.com/FuelLabs/fuel-core/pull/1432): Add a new `--api-request-timeout` argument to control TTL for GraphQL requests.
1919
- [#1426](https://github.com/FuelLabs/fuel-core/pull/1426) Split keygen into a create and a binary
2020
- [#1419](https://github.com/FuelLabs/fuel-core/pull/1419): Add additional "sanity" benchmarks for arithmetic op code instructions.

benches/benches/block_target_gas.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ use block_target_gas_set::{
33
contract::run_contract,
44
crypto::run_crypto,
55
flow::run_flow,
6+
memory::run_memory,
67
};
78
use criterion::{
89
criterion_group,
@@ -183,6 +184,8 @@ fn block_target_gas(c: &mut Criterion) {
183184

184185
run_flow(&mut group);
185186

187+
run_memory(&mut group);
188+
186189
group.finish();
187190
}
188191

benches/benches/block_target_gas_set/alu.rs

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -26,13 +26,6 @@ pub fn run_alu(group: &mut BenchmarkGroup<WallTime>) {
2626
vec![],
2727
);
2828

29-
run(
30-
"alu/aloc opcode",
31-
group,
32-
[op::aloc(0x10), op::jmpb(RegId::ZERO, 0)].to_vec(),
33-
vec![],
34-
);
35-
3629
run(
3730
"alu/and opcode",
3831
group,
Lines changed: 293 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,293 @@
1+
use crate::{
2+
utils::{
3+
arb_dependent_cost_values,
4+
set_full_word,
5+
},
6+
*,
7+
};
8+
9+
// ALOC: Allocate memory
10+
// CFE: Extend call frame
11+
// CFEI: Extend call frame immediate
12+
// CFS: Shrink call frame
13+
// CFSI: Shrink call frame immediate
14+
// LB: Load byte
15+
// LW: Load word
16+
// MCL: Memory clear
17+
// MCLI: Memory clear immediate
18+
// MCP: Memory copy
19+
// MCPI: Memory copy immediate
20+
// MEQ: Memory equality
21+
// POPH: Pop a set of high registers from stack
22+
// POPL: Pop a set of low registers from stack
23+
// PSHH: Push a set of high registers to stack
24+
// PSHL: Push a set of low registers to stack
25+
// SB: Store byte
26+
// SW: Store word
27+
pub fn run_memory(group: &mut BenchmarkGroup<WallTime>) {
28+
run(
29+
"memory/aloc opcode",
30+
group,
31+
[
32+
op::movi(0x10, 1000),
33+
op::aloc(0x10),
34+
op::jmpb(RegId::ZERO, 0),
35+
]
36+
.to_vec(),
37+
vec![],
38+
);
39+
40+
// Extend by 10 10 times and then shrink by 100 once. This is to hopefully allow the extend to be
41+
// the dominant opcode
42+
run(
43+
"memory/cfe opcode",
44+
group,
45+
vec![
46+
op::movi(0x10, 10),
47+
op::movi(0x11, 100),
48+
op::cfe(0x10),
49+
op::cfe(0x10),
50+
op::cfe(0x10),
51+
op::cfe(0x10),
52+
op::cfe(0x10),
53+
op::cfe(0x10),
54+
op::cfe(0x10),
55+
op::cfe(0x10),
56+
op::cfe(0x10),
57+
op::cfe(0x10),
58+
op::cfs(0x11),
59+
op::jmpb(RegId::ZERO, 10),
60+
],
61+
vec![],
62+
);
63+
64+
// Extend by 10 10 times and then shrink by 100 once. This is to hopefully allow the extend to be
65+
// the dominant opcode
66+
run(
67+
"memory/cfei opcode",
68+
group,
69+
vec![
70+
op::cfei(10),
71+
op::cfei(10),
72+
op::cfei(10),
73+
op::cfei(10),
74+
op::cfei(10),
75+
op::cfei(10),
76+
op::cfei(10),
77+
op::cfei(10),
78+
op::cfei(10),
79+
op::cfei(10),
80+
op::cfei(10),
81+
op::cfsi(100),
82+
op::jmpb(RegId::ZERO, 10),
83+
],
84+
vec![],
85+
);
86+
87+
// Extend by 100 once and then shrink by 10 10 times. This is to hopefully allow the shrink to
88+
// be the dominant opcode
89+
run(
90+
"memory/cfs opcode",
91+
group,
92+
vec![
93+
op::movi(0x10, 100),
94+
op::movi(0x11, 10),
95+
op::cfe(0x10),
96+
op::cfs(0x11),
97+
op::cfs(0x11),
98+
op::cfs(0x11),
99+
op::cfs(0x11),
100+
op::cfs(0x11),
101+
op::cfs(0x11),
102+
op::cfs(0x11),
103+
op::cfs(0x11),
104+
op::cfs(0x11),
105+
op::cfs(0x11),
106+
op::jmpb(RegId::ZERO, 10),
107+
],
108+
vec![],
109+
);
110+
111+
// Extend by 100 once and then shrink by 10 10 times. This is to hopefully allow the shrink to
112+
// be the dominant opcode
113+
run(
114+
"memory/cfsi opcode",
115+
group,
116+
vec![
117+
op::cfei(100),
118+
op::cfsi(10),
119+
op::cfsi(10),
120+
op::cfsi(10),
121+
op::cfsi(10),
122+
op::cfsi(10),
123+
op::cfsi(10),
124+
op::cfsi(10),
125+
op::cfsi(10),
126+
op::cfsi(10),
127+
op::cfsi(10),
128+
op::jmpb(RegId::ZERO, 10),
129+
],
130+
vec![],
131+
);
132+
133+
run(
134+
"memory/lb opcode",
135+
group,
136+
[op::lb(0x10, RegId::ONE, 10), op::jmpb(RegId::ZERO, 0)].to_vec(),
137+
vec![],
138+
);
139+
140+
run(
141+
"memory/lw opcode",
142+
group,
143+
[op::lw(0x10, RegId::ONE, 10), op::jmpb(RegId::ZERO, 0)].to_vec(),
144+
vec![],
145+
);
146+
147+
for i in arb_dependent_cost_values() {
148+
let id = format!("memory/mcl opcode {:?}", i);
149+
run(
150+
&id,
151+
group,
152+
vec![
153+
op::movi(0x11, i),
154+
op::aloc(0x11),
155+
op::move_(0x10, RegId::HP),
156+
op::mcl(0x10, 0x11),
157+
op::jmpb(RegId::ZERO, 0),
158+
],
159+
vec![],
160+
);
161+
}
162+
163+
for i in arb_dependent_cost_values() {
164+
let id = format!("memory/mcli opcode {:?}", i);
165+
run(
166+
&id,
167+
group,
168+
vec![
169+
op::movi(0x11, i),
170+
op::aloc(0x11),
171+
op::move_(0x10, RegId::HP),
172+
op::mcli(0x10, i),
173+
op::jmpb(RegId::ZERO, 0),
174+
],
175+
vec![],
176+
);
177+
}
178+
179+
for i in arb_dependent_cost_values() {
180+
let id = format!("memory/mcp opcode {:?}", i);
181+
run(
182+
&id,
183+
group,
184+
vec![
185+
op::movi(0x11, i),
186+
op::aloc(0x11),
187+
op::move_(0x10, RegId::HP),
188+
op::mcp(0x10, RegId::ZERO, 0x11),
189+
op::jmpb(RegId::ZERO, 0),
190+
],
191+
vec![],
192+
);
193+
}
194+
195+
let valid_values: Vec<_> = arb_dependent_cost_values()
196+
.iter()
197+
.copied()
198+
.take_while(|p| *p < (1 << 12)) // 12 bits
199+
.collect();
200+
for val in valid_values {
201+
let id = format!("memory/mcpi opcode {:?}", val);
202+
let val_as_u16 = (val).try_into().unwrap();
203+
run(
204+
&id,
205+
group,
206+
vec![
207+
op::movi(0x11, val),
208+
op::aloc(0x11),
209+
op::move_(0x10, RegId::HP),
210+
op::mcpi(0x10, RegId::ZERO, val_as_u16),
211+
op::jmpb(RegId::ZERO, 0),
212+
],
213+
vec![],
214+
);
215+
}
216+
217+
for i in arb_dependent_cost_values() {
218+
let id = format!("memory/meq opcode {:?}", i);
219+
let mut script = set_full_word(0x13, i as u64);
220+
script.extend(vec![
221+
op::meq(0x10, RegId::ZERO, RegId::ZERO, 0x13),
222+
op::jmpb(RegId::ZERO, 0),
223+
]);
224+
run(&id, group, script, vec![]);
225+
}
226+
227+
let full_mask = (1 << 24) - 1;
228+
229+
// Assumes that `pshh` has a correct cost
230+
run(
231+
"memory/poph opcode",
232+
group,
233+
vec![
234+
op::pshh(full_mask),
235+
op::poph(full_mask),
236+
op::jmpb(RegId::ZERO, 1),
237+
],
238+
vec![],
239+
);
240+
241+
// Assumes that `pshl` has a correct cost
242+
run(
243+
"memory/popl opcode",
244+
group,
245+
vec![
246+
op::pshl(full_mask),
247+
op::popl(full_mask),
248+
op::jmpb(RegId::ZERO, 1),
249+
],
250+
vec![],
251+
);
252+
253+
run(
254+
"memory/pshh opcode",
255+
group,
256+
vec![op::pshh(full_mask), op::jmpb(RegId::ZERO, 0)],
257+
vec![],
258+
);
259+
260+
run(
261+
"memory/pshl opcode",
262+
group,
263+
vec![op::pshl(full_mask), op::jmpb(RegId::ZERO, 0)],
264+
vec![],
265+
);
266+
267+
run(
268+
"memory/sb opcode",
269+
group,
270+
vec![
271+
op::aloc(RegId::ONE),
272+
op::move_(0x10, RegId::HP),
273+
op::movi(0x11, 50),
274+
op::sb(0x10, 0x11, 0),
275+
op::jmpb(RegId::ZERO, 0),
276+
],
277+
vec![],
278+
);
279+
280+
run(
281+
"memory/sw opcode",
282+
group,
283+
vec![
284+
op::movi(0x10, 8),
285+
op::aloc(0x10),
286+
op::move_(0x10, RegId::HP),
287+
op::movi(0x11, 50),
288+
op::sw(0x10, 0x11, 0),
289+
op::jmpb(RegId::ZERO, 0),
290+
],
291+
vec![],
292+
);
293+
}

benches/benches/block_target_gas_set/mod.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,3 +5,5 @@ pub mod crypto;
55
pub mod flow;
66

77
pub mod contract;
8+
9+
pub mod memory;

benches/benches/utils.rs

Lines changed: 22 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
use core::iter::successors;
22
use ethnum::U256;
3-
use fuel_core_types::fuel_asm::{
4-
op,
5-
Instruction,
6-
RegId,
3+
use fuel_core_types::{
4+
fuel_asm::{
5+
op,
6+
Instruction,
7+
RegId,
8+
},
9+
fuel_types::{
10+
RegisterId,
11+
Word,
12+
},
713
};
814

915
/// Allocates a byte array from heap and initializes it. Then points `reg` to it.
@@ -37,3 +43,15 @@ pub fn arb_dependent_cost_values() -> Vec<u32> {
3743
linear.extend(l);
3844
linear
3945
}
46+
47+
/// Set a register `r` to a Word-sized number value using left-shifts
48+
pub fn set_full_word(r: RegisterId, v: Word) -> Vec<Instruction> {
49+
let r = u8::try_from(r).unwrap();
50+
let mut ops = vec![op::movi(r, 0)];
51+
for byte in v.to_be_bytes() {
52+
ops.push(op::ori(r, r, byte as u16));
53+
ops.push(op::slli(r, r, 8));
54+
}
55+
ops.pop().unwrap(); // Remove last shift
56+
ops
57+
}

0 commit comments

Comments
 (0)