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

builtin cost rework with fixes and entry point info #875

Merged
merged 39 commits into from
Nov 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
39 commits
Select commit Hold shift + click to select a range
f641827
Revert "Revert builtin cost rework (#874)"
edg-l Oct 22, 2024
319038f
x
edg-l Oct 22, 2024
8e48a50
fix gas
edg-l Oct 22, 2024
74981dc
Merge branch 'main' into revert_revert
edg-l Oct 22, 2024
11aa07f
add paralel test
edg-l Oct 23, 2024
b566791
Merge branch 'main' into revert_revert
pefontana Oct 24, 2024
f2b7d32
Add entry point info to AotContractExecutor (#889)
edg-l Oct 28, 2024
f1356ab
Merge branch 'main' into revert_revert
edg-l Oct 28, 2024
ad818ad
Merge branch 'main' into revert_revert
edg-l Oct 28, 2024
8bb356c
upd replay
edg-l Oct 29, 2024
899a6b0
try with thread local
edg-l Oct 29, 2024
a72a146
try to fix with thread local in runtime2
edg-l Oct 30, 2024
ab48666
Merge remote-tracking branch 'origin/main' into revert_revert
edg-l Nov 4, 2024
2bb59c9
fix
edg-l Nov 4, 2024
4a07cf4
upd ref ci
edg-l Nov 4, 2024
675a285
upd replay
edg-l Nov 4, 2024
fe4d4b8
fix macos
edg-l Nov 4, 2024
1024bc6
try to fix macos
edg-l Nov 4, 2024
6d1603c
try to fix bench
edg-l Nov 4, 2024
07ac00c
fix
edg-l Nov 4, 2024
d2d8ef0
try to fix bench ci
edg-l Nov 4, 2024
939bb87
save space
edg-l Nov 4, 2024
73fa0ba
Merge branch 'main' into revert_revert
edg-l Nov 4, 2024
6b0ead1
upd rev
edg-l Nov 5, 2024
0845cb0
try to fix broken pipe
edg-l Nov 5, 2024
2a6606f
try to fix bench on ci
edg-l Nov 5, 2024
18fa33a
remove all features
edg-l Nov 5, 2024
adcec65
add cache key
edg-l Nov 5, 2024
4ed44e7
Merge branch 'main' into revert_revert
FrancoGiachetta Nov 5, 2024
40f1acc
update replay rev
FrancoGiachetta Nov 5, 2024
fce7142
fix cargo.lock
FrancoGiachetta Nov 5, 2024
9442bbf
Merge branch 'revert_revert' of github.com:lambdaclass/cairo_native i…
FrancoGiachetta Nov 5, 2024
ad6c2ce
Revert "update replay rev"
FrancoGiachetta Nov 5, 2024
e188415
fix cargo.lock
FrancoGiachetta Nov 5, 2024
44f85af
works with leaks
edg-l Nov 6, 2024
acc3a3b
fix
edg-l Nov 6, 2024
01994ed
upd replay
edg-l Nov 6, 2024
cdb84b2
fix bench
edg-l Nov 7, 2024
5a5d1b3
Reorder deps
JulianGCalderon Nov 8, 2024
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
8 changes: 2 additions & 6 deletions .github/workflows/bench-hyperfine.yml
Original file line number Diff line number Diff line change
Expand Up @@ -58,15 +58,11 @@ jobs:
- name: Install hyperfine
uses: taiki-e/install-action@v2
with:
tool: hyperfine@1.16
tool: hyperfine@1.18
- name: Install deps
run: make deps
- name: Build project
run: make build
- name: Build runtime subproject
run: make runtime-ci
- name: Run benchmarks
run: ./scripts/bench-hyperfine.sh programs/benches/*.cairo
run: make bench
- name: Create markdown file
run: bash .github/scripts/merge-benches.sh

Expand Down
22 changes: 20 additions & 2 deletions .github/workflows/starknet-blocks.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ jobs:
with:
components: clippy
- uses: Swatinem/rust-cache@v2
with:
key: "ref-dc35685315f4df544d5d1cf006d3a2a25d8c2c9a"

- name: Check and free hdd space left
if: ${{ matrix.runner == 'native' }}
Expand Down Expand Up @@ -62,7 +64,7 @@ jobs:
run: make deps
- name: Build Cairo Native project
if: ${{ matrix.runner == 'native' }}
run: cargo b --release --all-features
run: cargo b --release
- name: Build runtime
if: ${{ matrix.runner == 'native' }}
run: |
Expand All @@ -74,7 +76,7 @@ jobs:
uses: actions/checkout@v4
with:
repository: lambdaclass/starknet-replay
ref: 08aa133b11e1e319036354c9acd8270483c844b5
ref: dc35685315f4df544d5d1cf006d3a2a25d8c2c9a
path: replay

- name: Install Starknet Replay deps
Expand Down Expand Up @@ -116,6 +118,22 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Check and free hdd space left
if: ${{ matrix.runner == 'native' }}
run: |
sudo apt-get update
sudo apt-get remove -y '^llvm-.*'
sudo apt-get remove -y 'php.*'
sudo apt-get remove -y '^dotnet-.*'
sudo apt-get remove -y '^temurin-.*'
sudo apt-get remove -y azure-cli microsoft-edge-stable google-chrome-stable firefox mono-devel
sudo apt-get autoremove -y
sudo apt-get clean
echo "Removing large directories"
# deleting 15GB
sudo rm -rf /usr/share/dotnet/
sudo rm -rf /usr/local/lib/android

- name: Fetch Native dumps
uses: actions/download-artifact@v4
with:
Expand Down
1 change: 1 addition & 0 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ rstest = "0.23.0"
test-case = "3.3"
walkdir = "2.5.0"
serde_json = { version = "1.0.128" }
rayon = "1.10.0"

[build-dependencies]
cc = "1.1.28"
Expand Down
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,7 @@ doc-open: check-llvm
.PHONY: bench
bench: needs-cairo2 runtime
cargo b --release --bin cairo-native-run
cargo b --release --bin cairo-native-compile
./scripts/bench-hyperfine.sh

.PHONY: bench-ci
Expand Down
11 changes: 8 additions & 3 deletions programs/benches/factorial_2M.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ typedef struct factorial_return_values
uint8_t discriminant;
struct {
void* ptr;
uint32_t len;
uint32_t start;
uint32_t end;
uint32_t cap;
} err;
} result;
} factorial_return_values_t;


static void run_bench(factorial_return_values_t*, uint64_t)
__attribute__((weakref("_mlir_ciface_factorial_2M::factorial_2M::main(f1)")));

extern uint64_t* cairo_native__set_costs_builtin(uint64_t*);

int main()
{
factorial_return_values_t return_values;

uint64_t BuiltinCosts[7] = {1, 4050, 583, 4085, 491, 230, 604};

cairo_native__set_costs_builtin(&BuiltinCosts[0]);

run_bench(&return_values, 0);
assert(return_values.result.discriminant == 0);
assert((return_values.result.discriminant & 0x1) == 0);

return 0;
}
11 changes: 8 additions & 3 deletions programs/benches/fib_2M.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ typedef struct fib_return_values
uint8_t discriminant;
struct {
void *ptr;
uint32_t len;
uint32_t start;
uint32_t end;
uint32_t cap;
} err;
} result;
} fib_return_values_t;


static void run_bench(fib_return_values_t *, uint64_t)
__attribute__((weakref("_mlir_ciface_fib_2M::fib_2M::main(f1)")));

extern uint64_t* cairo_native__set_costs_builtin(uint64_t*);

int main()
{
uint64_t BuiltinCosts[7] = {1, 4050, 583, 4085, 491, 230, 604};

cairo_native__set_costs_builtin(&BuiltinCosts[0]);

fib_return_values_t return_values;

run_bench(&return_values, 0);
assert(return_values.result.discriminant == 0);
assert((return_values.result.discriminant & 0x1) == 0);

return 0;
}
11 changes: 8 additions & 3 deletions programs/benches/logistic_map.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,23 +10,28 @@ typedef struct map_return_values
uint8_t discriminant;
struct {
void *ptr;
uint32_t len;
uint32_t start;
uint32_t end;
uint32_t cap;
} err;
} result;
} map_return_values_t;


static void run_bench(map_return_values_t *, uint64_t)
__attribute__((weakref("_mlir_ciface_logistic_map::logistic_map::main(f2)")));

extern uint64_t* cairo_native__set_costs_builtin(uint64_t*);

int main()
{
uint64_t BuiltinCosts[7] = {1, 4050, 583, 4085, 491, 230, 604};

cairo_native__set_costs_builtin(&BuiltinCosts[0]);

map_return_values_t return_values;

run_bench(&return_values, 0);
assert(return_values.result.discriminant == 0);
assert((return_values.result.discriminant & 0x1) == 0);

return 0;
}
29 changes: 28 additions & 1 deletion runtime/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ use starknet_types_core::{
hash::StarkHash,
};
use std::{
collections::HashMap, ffi::c_void, fs::File, io::Write, mem::ManuallyDrop, os::fd::FromRawFd,
cell::Cell, collections::HashMap, ffi::c_void, fs::File, io::Write, mem::ManuallyDrop,
os::fd::FromRawFd, ptr::null,
};
use std::{ops::Mul, vec::IntoIter};

Expand Down Expand Up @@ -475,6 +476,32 @@ pub unsafe extern "C" fn cairo_native__libfunc__ec__ec_state_try_finalize_nz(
}
}

thread_local! {
// We can use cell because a ptr is copy.
static BUILTIN_COSTS: Cell<*const u64> = const {
Cell::new(null())
};
}

/// Store the gas builtin in the internal thread local. Returns the old pointer, to restore it after execution.
/// Not a runtime metadata method, it should be called before the program is executed.
#[no_mangle]
pub extern "C" fn cairo_native__set_costs_builtin(ptr: *const u64) -> *const u64 {
let old = BUILTIN_COSTS.get();
BUILTIN_COSTS.set(ptr);
old
}

/// Get the gas builtin from the internal thread local.
#[no_mangle]
pub extern "C" fn cairo_native__get_costs_builtin() -> *const u64 {
if BUILTIN_COSTS.get().is_null() {
// We shouldn't panic here, but we can print a big message.
eprintln!("BUILTIN_COSTS POINTER IS NULL!");
}
BUILTIN_COSTS.get()
}

/// Utility methods for the print runtime function

/// Formats the given felts as a debug string.
Expand Down
48 changes: 10 additions & 38 deletions scripts/bench-hyperfine.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

# Configuration.
ROOT_DIR="$(dirname "$(dirname "${0%/*}")")"
ROOT_DIR="$(dirname "$(readlink -f "${0%/*}")")"
MLIR_DIR="$MLIR_SYS_190_PREFIX"

CAIRO_SRCS=$(find \
Expand All @@ -10,7 +10,7 @@ CAIRO_SRCS=$(find \
IFS=$'\n' read -rd '' -a CAIRO_SRCS <<<"$CAIRO_SRCS"

CAIRO_RUN="$ROOT_DIR/cairo2/bin/cairo-run"
COMPILER_CLI="$ROOT_DIR/target/release/cairo-native-dump"
COMPILER_CLI="$ROOT_DIR/target/release/cairo-native-compile"
JIT_CLI="$ROOT_DIR/target/release/cairo-native-run"
OUTPUT_DIR="$ROOT_DIR/target/bench-outputs"

Expand Down Expand Up @@ -42,51 +42,24 @@ run_bench() {
base_name=$(basename $base_path)

"$COMPILER_CLI" \
"$base_path.cairo" \
--output "$OUTPUT_DIR/$base_name.mlir" \
>> /dev/stderr

"$MLIR_DIR/bin/mlir-opt" \
--canonicalize \
--convert-scf-to-cf \
--canonicalize \
--cse \
--expand-strided-metadata \
--finalize-memref-to-llvm \
--convert-func-to-llvm \
--convert-index-to-llvm \
--reconcile-unrealized-casts \
-s "$base_path.cairo" \
"$OUTPUT_DIR/$base_name.mlir" \
-o "$OUTPUT_DIR/$base_name.opt.mlir" \
>> /dev/stderr

"$MLIR_DIR/bin/mlir-translate" \
--mlir-to-llvmir \
"$OUTPUT_DIR/$base_name.opt.mlir" \
-o "$OUTPUT_DIR/$base_name.ll" \
>> /dev/stderr

"$MLIR_DIR/bin/clang" \
-O3 \
-Wno-override-module \
"$base_path.c" \
"$OUTPUT_DIR/$base_name.ll" \
-L "target/release" \
-Wl,-rpath "$MLIR_DIR/lib" \
-Wl,-rpath "target/release" \
-o "$OUTPUT_DIR/$base_name" \
"$OUTPUT_DIR/lib$base_name.so" \
>> /dev/stderr

"$MLIR_DIR/bin/clang" \
-O3 \
-march=native \
-mtune=native \
-fPIC \
-Wno-override-module \
"$base_path.c" \
"$OUTPUT_DIR/$base_name.ll" \
-L "target/release" \
-L"$OUTPUT_DIR/" \
-Wl,-rpath "$MLIR_DIR/lib" \
-Wl,-rpath "target/release" \
-Wl,-rpath "$OUTPUT_DIR" \
-Wl,--rpath-link "$OUTPUT_DIR" \
-l"$base_name" \
-lm \
-o "$OUTPUT_DIR/$base_name-march-native" \
>> /dev/stderr

Expand All @@ -97,7 +70,6 @@ run_bench() {
-n "Cairo-vm (Rust, Cairo 1)" "$CAIRO_RUN --available-gas 18446744073709551615 -s $base_path.cairo" \
-n "cairo-native (embedded AOT)" "$JIT_CLI --run-mode=aot -s $base_path.cairo --opt-level 3 --available-gas 18446744073709551615 " \
-n "cairo-native (embedded JIT using LLVM's ORC Engine)" "$JIT_CLI --run-mode=jit -s $base_path.cairo --opt-level 3 --available-gas 18446744073709551615 " \
-n "cairo-native (standalone AOT)" "$OUTPUT_DIR/$base_name" \
-n "cairo-native (standalone AOT with -march=native)" "$OUTPUT_DIR/$base_name-march-native" \
>> /dev/stderr
}
Expand Down
6 changes: 2 additions & 4 deletions scripts/diff-check.sh
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,9 @@ for vm_dump in state_dumps/vm/*/*.json; do
continue
fi

base=$(basename "$vm_dump")

if ! cmp -s \
<(sed '/"reverted": /d' "$native_dump") \
<(sed '/"reverted": /d' "$vm_dump")
<(sed '/"reverted": /d' "$native_dump" 2>/dev/null) \
<(sed '/"reverted": /d' "$vm_dump" 2>/dev/null)
then
echo "NATIVE DIFFING IN TX: $native_dump"
diffing=1
Expand Down
2 changes: 1 addition & 1 deletion src/compiler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -446,7 +446,7 @@ fn compile_func(
initial_state,
|statement_idx, mut state| {
if let Some(gas_metadata) = metadata.get::<GasMetadata>() {
let gas_cost = gas_metadata.get_gas_cost_for_statement(statement_idx);
let gas_cost = gas_metadata.get_gas_costs_for_statement(statement_idx);
metadata.remove::<GasCost>();
metadata.insert(GasCost(gas_cost));
}
Expand Down
6 changes: 6 additions & 0 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,12 @@ pub enum Error {
#[error("integer conversion failed")]
IntegerConversion,

#[error("missing BuiltinCosts global symbol, should never happen, this is a bug")]
MissingBuiltinCostsSymbol,

#[error("selector not found in the AotContractExecutor mappings")]
SelectorNotFound,

#[error(transparent)]
IoError(#[from] std::io::Error),

Expand Down
Loading
Loading