From d20f54bb1a873a16681c687a58b7946dc6924913 Mon Sep 17 00:00:00 2001 From: bjorn3 Date: Mon, 5 Nov 2018 18:42:43 +0100 Subject: [PATCH] Add some benchmarks --- .travis.yml | 1 + build.sh | 24 +++++++++++++++++++---- example/alloc_example.rs | 41 ++++++++++++++++++++++++++++++++++++++++ example/mod_bench.rs | 37 ++++++++++++++++++++++++++++++++++++ 4 files changed, 99 insertions(+), 4 deletions(-) create mode 100644 example/alloc_example.rs create mode 100644 example/mod_bench.rs diff --git a/.travis.yml b/.travis.yml index db9dbdcc259..9ee74793c72 100644 --- a/.travis.yml +++ b/.travis.yml @@ -8,6 +8,7 @@ rust: script: - rustup component add rust-src - cargo install xargo || echo "Skipping xargo install" + - cargo install hyperfine || echo "Skipping hyperfine install" - ./prepare_libcore.sh - ./build.sh diff --git a/build.sh b/build.sh index 6bbf024044b..acb9bbc92d7 100755 --- a/build.sh +++ b/build.sh @@ -48,13 +48,29 @@ SHOULD_RUN=1 $RUSTC --crate-type bin example/mini_core_hello_world.rs --cfg jit echo "[AOT] mini_core_hello_world" build_example_bin mini_core_hello_world example/mini_core_hello_world.rs -echo "[BUILD] core" -time $RUSTC target/libcore/src/libcore/lib.rs --crate-type lib --crate-name core -Cincremental=target/incremental_core - pushd xargo rm -r ~/.xargo/HOST || true rm -r target || true -time xargo build --color always +time xargo build popd +$RUSTC --sysroot ~/.xargo/HOST example/alloc_example.rs --crate-type bin +# TODO linux linker doesn't accept duplicate definitions +#gcc -Wl,--gc-sections target/out/alloc_example ~/.xargo/HOST/lib/rustlib/*/lib/libcore-*.rlib ~/.xargo/HOST/lib/rustlib/*/lib/liballoc-*.rlib ~/.xargo/HOST/lib/rustlib/*/lib/liballoc_system-*.rlib -o target/out/alloc_example_exe +#hyperfine ./target/out/alloc_example_exe + +$RUSTC --sysroot ~/.xargo/HOST example/mod_bench.rs --crate-type bin +gcc -Wl,--gc-sections target/out/mod_bench -lc -o target/out/mod_bench_exe + +$RUSTC --sysroot ~/.xargo/HOST example/mod_bench.rs --crate-type bin -Zmir-opt-level=3 --crate-name mod_bench_inline +gcc -Wl,--gc-sections target/out/mod_bench_inline -lc -o target/out/mod_bench_inline_exe + +rustc example/mod_bench.rs --crate-type bin -Copt-level=0 -o target/out/mod_bench_llvm_0 -Cpanic=abort +rustc example/mod_bench.rs --crate-type bin -Copt-level=1 -o target/out/mod_bench_llvm_1 -Cpanic=abort +rustc example/mod_bench.rs --crate-type bin -Copt-level=2 -o target/out/mod_bench_llvm_2 -Cpanic=abort +rustc example/mod_bench.rs --crate-type bin -Copt-level=3 -o target/out/mod_bench_llvm_3 -Cpanic=abort +echo +echo "[Bench] mod_bench" +hyperfine ./target/out/mod_bench{,_inline}_exe ./target/out/mod_bench_llvm_* + cat target/out/log.txt | sort | uniq -c diff --git a/example/alloc_example.rs b/example/alloc_example.rs new file mode 100644 index 00000000000..06746ab1f76 --- /dev/null +++ b/example/alloc_example.rs @@ -0,0 +1,41 @@ +#![feature(start, box_syntax, alloc_system, core_intrinsics, alloc, alloc_error_handler)] +#![no_std] + +extern crate alloc; +extern crate alloc_system; + +use alloc::prelude::*; + +use alloc_system::System; + +#[global_allocator] +static ALLOC: System = System; + +#[link(name = "c")] +extern "C" { + fn puts(s: *const u8); +} + +#[panic_handler] +fn panic_handler(_: &core::panic::PanicInfo) -> ! { + unsafe { + core::intrinsics::abort(); + } +} + +#[alloc_error_handler] +fn alloc_error_handler(_: alloc::alloc::Layout) -> ! { + unsafe { + core::intrinsics::abort(); + } +} + +#[start] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + let world: Box<&str> = box "Hello World!\0"; + unsafe { + puts(*world as *const str as *const u8); + } + + 0 +} diff --git a/example/mod_bench.rs b/example/mod_bench.rs new file mode 100644 index 00000000000..2e2b0052dee --- /dev/null +++ b/example/mod_bench.rs @@ -0,0 +1,37 @@ +#![feature(start, box_syntax, core_intrinsics, lang_items)] +#![no_std] + +#[link(name = "c")] +extern {} + +#[panic_handler] +fn panic_handler(_: &core::panic::PanicInfo) -> ! { + unsafe { + core::intrinsics::abort(); + } +} + +#[lang="eh_personality"] +fn eh_personality(){} + +// Required for rustc_codegen_llvm +#[no_mangle] +unsafe extern "C" fn _Unwind_Resume() { + core::intrinsics::unreachable(); +} + +#[start] +fn main(_argc: isize, _argv: *const *const u8) -> isize { + for i in 2..100_000_000 { + black_box((i + 1) % i); + } + + 0 +} + +#[inline(never)] +fn black_box(i: u32) { + if i != 1 { + unsafe { core::intrinsics::abort(); } + } +}