Skip to content

Commit

Permalink
Use compile_error macro to replace build script (#71)
Browse files Browse the repository at this point in the history
* Use compile_error macro to replace build script

* Update rustc in bench runners

* Add CI to test cross compilation

* Add more targets

* Prepare for merge

* Also build from macos arm

* Use nightly rust for hybrid

* Fix more cross compilation cases

* Install toolchain if needed

* Add last target back, even if not working

* Check if changes fixes doctest

* Try use rustdocflags

* Reuse std::hash::RandomState to get rid of rand dependency

* Try set rustflags in x compile CI

* Try use all flags in x compile CI

* Fine tune cross compilation ci
  • Loading branch information
ogxd authored May 27, 2024
1 parent c9c9f61 commit b85cfb9
Show file tree
Hide file tree
Showing 10 changed files with 100 additions and 41 deletions.
3 changes: 2 additions & 1 deletion .cargo/config.toml
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
[build]
rustflags = ["-C", "target-cpu=native"]
rustflags = ["-C", "target-cpu=native"]
rustdocflags = ["-C", "target-cpu=native"]
8 changes: 7 additions & 1 deletion .github/workflows/bench.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Update rust
run: rustup update

- name: Benchmark
run: cargo bench --bench throughput --features bench-plot

Expand All @@ -33,7 +36,7 @@ jobs:
run: rustup default nightly

- name: Benchmark
run: cargo bench --bench throughput --features bench-plot
run: cargo bench --bench throughput --features bench-plot hybrid

- uses: actions/upload-artifact@v3
with:
Expand All @@ -47,6 +50,9 @@ jobs:
steps:
- uses: actions/checkout@v4

- name: Update rust
run: rustup update

- name: Benchmark
run: cargo bench --bench throughput --features bench-plot

Expand Down
8 changes: 4 additions & 4 deletions .github/workflows/build_test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ jobs:
run: cargo build --release

- name: Test
run: cargo test --release --lib
run: cargo test --release

build_test_x86_avx2:
name: Build & Test X86 AVX2
Expand All @@ -40,10 +40,10 @@ jobs:
run: cargo rustc -- --version

- name: Build
run: cargo build --release
run: cargo build --release --features hybrid

- name: Test
run: cargo test --release --lib
run: cargo test --release --features hybrid

build_test_arm:
name: Build & Test ARM
Expand All @@ -59,4 +59,4 @@ jobs:
run: cargo build --release

- name: Test
run: cargo test --release --lib
run: cargo test --release
66 changes: 66 additions & 0 deletions .github/workflows/cross_compile.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
name: Cross Compile

on:
# Trigger when merged on main
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
# Manual trigger
workflow_dispatch:

env:
CARGO_TERM_COLOR: always
RUSTFLAGS: "-C target-feature=+aes,+vaes,+avx2"

jobs:

build_x86:
name: Build

strategy:
fail-fast: false
matrix:
include:
- from: ubuntu-latest
target: "aarch64-apple-darwin"
allow_failure_hybrid: true
- from: ubuntu-latest
target: "aarch64-unknown-linux-gnu"
allow_failure_hybrid: true
- from: ubuntu-latest
target: "aarch64-unknown-linux-musl"
allow_failure_hybrid: true
- from: ubuntu-latest
target: "i686-unknown-linux-gnu"
allow_failure_hybrid: true
- from: ubuntu-latest
target: "x86_64-pc-windows-msvc"
allow_failure_hybrid: true # Supposed to work as hybrid but not tested yet
- from: ubuntu-latest
target: "x86_64-unknown-linux-gnu"
allow_failure_hybrid: false
- from: macos-latest
target: "aarch64-apple-darwin"
allow_failure_hybrid: true
- from: macos-latest
target: "x86_64-unknown-linux-gnu"
allow_failure_hybrid: true # Supposed to work as hybrid but not tested yet

runs-on: ${{ matrix.from }}

steps:
- uses: actions/checkout@v3

- name: Install target
run: rustup target add ${{ matrix.target }}

- name: Build
run: cargo build --release --target ${{ matrix.target }}

- name: Switch to nightly rust
run: rustup default nightly

- name: Build Hybrid
continue-on-error: ${{ matrix.allow_failure_hybrid }}
run: cargo build --release --features hybrid --target ${{ matrix.target }}
7 changes: 5 additions & 2 deletions .github/workflows/rust_version.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
name: Rust Version Compatibility

on:
# Trigger when merged on main
push:
branches: [ "main" ]
# Manual trigger
workflow_dispatch:

env:
Expand All @@ -24,5 +28,4 @@ jobs:
run: rustup toolchain install ${{ matrix.version }}

- name: Build
# RUSTFLAGS="-C target-cpu=native --cfg hybrid"
run: RUSTFLAGS="-C target-cpu=native" cargo +${{ matrix.version }} build --release
run: cargo +${{ matrix.version }} build --release
9 changes: 4 additions & 5 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
[package]
name = "gxhash"
authors = ["Olivier Giniaux"]
version = "3.2.0"
version = "3.3.0"
edition = "2021"
description = "GxHash non-cryptographic algorithm"
license = "MIT"
Expand All @@ -17,11 +17,13 @@ exclude = ["article/*"]
bench-csv = []
bench-md = []
bench-plot = []
hybrid = []

[dependencies]
rand = "0.8"
# No dependencies!

[dev-dependencies]
rand = "0.8"
lazy_static = { version = "1.4" }
itertools = "0.12.0"
# Benchmarks
Expand All @@ -35,9 +37,6 @@ seahash = "4.1.0"
metrohash = "1.0.6"
fnv = "1.0.3"

[build-dependencies]
rustc_version = "0.4.0"

[dev-dependencies.plotters]
version = "0.3.5"
default-features = false
Expand Down
16 changes: 0 additions & 16 deletions build.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,3 @@
extern crate rustc_version;
use rustc_version::{version_meta, Channel};

fn main() {
// When conditions permits, enable hybrid feature to leverage wider intrinsics for even more throughput
if version_meta().unwrap().channel == Channel::Nightly
&& cfg!(target_arch = "x86_64")
&& cfg!(target_feature = "avx2")
&& cfg!(target_feature = "vaes") {
println!("cargo:rustc-cfg=hybrid");
}

// If not cross compiling, make sure the aes feature is available
if std::env::var("HOST").unwrap_or_default() == std::env::var("TARGET").unwrap_or_default()
&& cfg!(not(target_feature = "aes")) {
panic!("| GxHash requires target-feature 'aes' to be enabled.\n\
| Build with RUSTFLAGS=\"-C target-cpu=native\" or RUSTFLAGS=\"-C target-feature=+aes\" to enable.");
}
}
4 changes: 2 additions & 2 deletions src/gxhash/platform/x86.rs
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ pub unsafe fn ld(array: *const u32) -> State {
_mm_loadu_si128(array as *const State)
}

#[cfg(not(hybrid))]
#[cfg(not(feature = "hybrid"))]
#[inline(always)]
pub unsafe fn compress_8(mut ptr: *const State, end_address: usize, hash_vector: State, len: usize) -> State {

Expand Down Expand Up @@ -100,7 +100,7 @@ pub unsafe fn compress_8(mut ptr: *const State, end_address: usize, hash_vector:
aes_encrypt(lane1, lane2)
}

#[cfg(hybrid)]
#[cfg(feature = "hybrid")]
#[inline(always)]
pub unsafe fn compress_8(ptr: *const State, end_address: usize, hash_vector: State, len: usize) -> State {
macro_rules! load_unaligned_x2 {
Expand Down
12 changes: 3 additions & 9 deletions src/hasher.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
use std::collections::{HashMap, HashSet};
use std::hash::{BuildHasher, Hasher};
use std::mem::MaybeUninit;

use rand::RngCore;

use crate::gxhash::platform::*;
use crate::gxhash::*;
Expand Down Expand Up @@ -138,13 +135,10 @@ pub struct GxBuildHasher(State);
impl Default for GxBuildHasher {
#[inline]
fn default() -> GxBuildHasher {
let mut uninit: MaybeUninit<State> = MaybeUninit::uninit();
let mut rng = rand::thread_rng();
let random_state = std::hash::RandomState::new();
unsafe {
let ptr = uninit.as_mut_ptr() as *mut u8;
let slice = std::slice::from_raw_parts_mut(ptr, VECTOR_SIZE);
rng.fill_bytes(slice);
GxBuildHasher(uninit.assume_init())
let state: State = std::mem::transmute(random_state);
GxBuildHasher(state)
}
}
}
Expand Down
8 changes: 7 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,11 @@
// Hybrid SIMD width usage currently requires unstable 'stdsimd'
#![cfg_attr(hybrid, feature(stdarch_x86_avx512))]
#![cfg_attr(feature = "hybrid", feature(stdarch_x86_avx512))]

#[cfg(all(feature = "hybrid", not(any(target_arch = "x86_64", target_feature = "aes", target_feature = "vaes", target_feature = "avx2"))))]
compile_error!{"Hybrid feature is only available on x86 processors with avx2 and vaes intrinsics."}

#[cfg(not(target_feature = "aes"))]
compile_error!{"Gxhash requires aes intrinsics. Make sure the processor supports it and build with RUSTFLAGS=\"-C target-cpu=native\" or RUSTFLAGS=\"-C target-feature=+aes\"."}

#[rustfmt::skip]
mod gxhash;
Expand Down

0 comments on commit b85cfb9

Please sign in to comment.