Skip to content

Commit efff21d

Browse files
committed
Add CI to run AVX-512 backend (from aes CI config) and fix build with RNG feature
1 parent 045a79e commit efff21d

File tree

5 files changed

+57
-1
lines changed

5 files changed

+57
-1
lines changed

.github/workflows/chacha20.yml

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,9 @@ defaults:
2020
env:
2121
CARGO_INCREMENTAL: 0
2222
RUSTFLAGS: "-Dwarnings"
23+
# NOTE: The mirror number changes with each version so keep these in sync
24+
SDE_FULL_VERSION_MIRROR: "859732"
25+
SDE_FULL_VERSION: "9.58.0-2025-06-16"
2326

2427
jobs:
2528
build:
@@ -79,6 +82,43 @@ jobs:
7982
- run: cargo check --target ${{ matrix.target }} --all-features
8083
- run: cargo hack test --feature-powerset --target ${{ matrix.target }}
8184

85+
# Tests for the AVX-512 backend
86+
avx512:
87+
runs-on: ubuntu-latest
88+
strategy:
89+
matrix:
90+
include:
91+
- target: x86_64-unknown-linux-gnu
92+
rust: stable
93+
RUSTFLAGS: "-Dwarnings --cfg chacha20_avx512"
94+
env:
95+
CARGO_INCREMENTAL: 0
96+
RUSTFLAGS: ${{ matrix.RUSTFLAGS }}
97+
steps:
98+
- uses: actions/checkout@v4
99+
- name: Install Intel SDE
100+
run: |
101+
curl -JLO "https://downloadmirror.intel.com/${{ env.SDE_FULL_VERSION_MIRROR }}/sde-external-${{ env.SDE_FULL_VERSION }}-lin.tar.xz"
102+
tar xvf sde-external-${{ env.SDE_FULL_VERSION }}-lin.tar.xz -C /opt
103+
echo "/opt/sde-external-${{ env.SDE_FULL_VERSION }}-lin" >> $GITHUB_PATH
104+
- uses: RustCrypto/actions/cargo-cache@master
105+
- uses: dtolnay/rust-toolchain@master
106+
with:
107+
toolchain: ${{ matrix.rust }}
108+
targets: ${{ matrix.target }}
109+
# NOTE: Write a `.cargo/config.toml` to configure the target for AVX-512
110+
# NOTE: We use intel-sde as the runner since not all GitHub CI hosts support AVX512
111+
- name: write .cargo/config.toml
112+
shell: bash
113+
run: |
114+
cd ../chacha20/..
115+
mkdir -p .cargo
116+
echo '[target.${{ matrix.target }}]' > .cargo/config.toml
117+
echo 'runner = "sde64 -future --"' >> .cargo/config.toml
118+
- run: ${{ matrix.deps }}
119+
- run: cargo test --target ${{ matrix.target }}
120+
- run: cargo test --target ${{ matrix.target }} --all-features
121+
82122
# Tests for the AVX2 backend
83123
avx2:
84124
runs-on: ubuntu-latest

Cargo.lock

Lines changed: 7 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

chacha20/src/backends.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ cfg_if! {
99
cfg_if! {
1010
if #[cfg(all(chacha20_avx512, chacha20_force_avx512))] {
1111
pub(crate) mod avx512;
12+
// AVX-2 backend needed for RNG if enabled
13+
#[cfg(feature = "rng")]
14+
pub(crate) mod avx2;
1215
} else if #[cfg(chacha20_force_avx2)] {
1316
pub(crate) mod avx2;
1417
} else if #[cfg(chacha20_force_sse2)] {

chacha20/src/backends/avx2.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ const N: usize = PAR_BLOCKS / 2;
2727
#[inline]
2828
#[target_feature(enable = "avx2")]
2929
#[cfg(feature = "cipher")]
30+
#[cfg_attr(chacha20_force_avx512, expect(unused))]
3031
pub(crate) unsafe fn inner<R, F, V>(state: &mut [u32; STATE_WORDS], f: F)
3132
where
3233
R: Rounds,

chacha20/src/rng.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,8 @@ impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
189189
backends::soft::Backend(self).gen_ks_blocks(buffer);
190190
} else if #[cfg(any(target_arch = "x86", target_arch = "x86_64"))] {
191191
cfg_if! {
192-
if #[cfg(chacha20_force_avx2)] {
192+
// AVX-512 doesn't support RNG, so use AVX-2 instead
193+
if #[cfg(any(chacha20_force_avx2, chacha20_force_avx512))] {
193194
unsafe {
194195
backends::avx2::rng_inner::<R, V>(self, buffer);
195196
}
@@ -198,7 +199,11 @@ impl<R: Rounds, V: Variant> ChaChaCore<R, V> {
198199
backends::sse2::rng_inner::<R, V>(self, buffer);
199200
}
200201
} else {
202+
#[cfg(chacha20_avx512)]
201203
let (_avx512_token, avx2_token, sse2_token) = self.tokens;
204+
#[cfg(not(chacha20_avx512))]
205+
let (avx2_token, sse2_token) = self.tokens;
206+
202207
if avx2_token.get() {
203208
unsafe {
204209
backends::avx2::rng_inner::<R, V>(self, buffer);

0 commit comments

Comments
 (0)