Skip to content

Commit ab80b03

Browse files
committed
Add code generation tests
Introduces a suite of code generation tests to the `benches` folder. Each tests consists of a pair of files: a Rust source containing a function named `codegen_test`, and a `.x86-64.mca` file containing the llvm-mca analysis of `codegen_test`. In practice, these tests operate similarly to UI tests; CI checks a fresh llvm-mca analysis against that in the file, and fails if they mismatch. Changes can be expressly blessed via `BLESS=1`. gherrit-pr-id: G5964d13c9ffb7a47ed4662892ef92c90ddc51e12
1 parent b7fc71d commit ab80b03

33 files changed

+1364
-3
lines changed

.github/workflows/ci.yml

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -379,7 +379,8 @@ jobs:
379379
$FEATURES \
380380
--verbose \
381381
-- \
382-
--skip ui
382+
--skip ui \
383+
--skip codegen
383384
384385
# Only run tests when targetting Linux x86 (32- or 64-bit) - we're
385386
# executing on Linux x86_64, so we can't run tests for any non-x86 target.
@@ -388,6 +389,23 @@ jobs:
388389
# Run compile tests when building for other targets.
389390
if: contains(matrix.target, 'linux') && (contains(matrix.target, 'x86_64') || contains(matrix.target, 'i686'))
390391

392+
- name: Run codegen tests
393+
env:
394+
TOOLCHAIN: ${{ matrix.toolchain }}
395+
CRATE: ${{ matrix.crate }}
396+
TARGET: ${{ matrix.target }}
397+
FEATURES: ${{ matrix.features }}
398+
run: |
399+
sudo apt install llvm > /dev/null
400+
./cargo.sh +nightly install cargo-show-asm > /dev/null
401+
./cargo.sh +$TOOLCHAIN test \
402+
--package $CRATE \
403+
--target $TARGET \
404+
$FEATURES \
405+
--verbose \
406+
--test codegen
407+
if: matrix.crate == 'zerocopy' && matrix.toolchain == 'nightly' && matrix.target == 'x86_64-unknown-linux-gnu' && matrix.features == '--all-features'
408+
391409
- name: Run UI tests
392410
env:
393411
TOOLCHAIN: ${{ matrix.toolchain }}
@@ -631,7 +649,9 @@ jobs:
631649
--lcov \
632650
--output-path lcov.info \
633651
--verbose \
634-
-- --skip ui
652+
-- \
653+
--skip ui \
654+
--skip codegen
635655
- name: Upload coverage to Codecov
636656
uses: codecov/codecov-action@671740ac38dd9b0130fbe1cec585b89eea48d3de # v5.5.2
637657
with:

benches/formats/coco.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
// Copyright 2026 The Fuchsia Authors
2+
//
3+
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
// This file may not be copied, modified, or distributed except according to
7+
// those terms.
8+
9+
use zerocopy_derive::*;
10+
11+
// The only valid value of this type is the byte `0xC0`
12+
#[derive(TryFromBytes, KnownLayout, Immutable)]
13+
#[repr(u8)]
14+
enum C0 {
15+
_XC0 = 0xC0,
16+
}
17+
18+
// The only valid value of this type is the bytes `0xC0C0`.
19+
#[derive(TryFromBytes, KnownLayout, Immutable)]
20+
#[repr(C)]
21+
struct C0C0(C0, C0);
22+
23+
#[derive(TryFromBytes, KnownLayout, Immutable)]
24+
#[repr(C)]
25+
pub struct Packet {
26+
magic_number: C0C0,
27+
mug_size: u8,
28+
temperature: u8,
29+
marshmallows: [[u8; 2]],
30+
}

benches/formats/loco.rs

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
// Copyright 2026 The Fuchsia Authors
2+
//
3+
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
// This file may not be copied, modified, or distributed except according to
7+
// those terms.
8+
9+
use zerocopy_derive::*;
10+
11+
#[derive(FromBytes, KnownLayout, Immutable)]
12+
#[repr(C)]
13+
pub struct Packet {
14+
magic_number: [u8; 2],
15+
mug_size: u8,
16+
temperature: u8,
17+
marshmallows: [[u8; 2]],
18+
}

benches/ref_from_bytes.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2026 The Fuchsia Authors
2+
//
3+
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
// This file may not be copied, modified, or distributed except according to
7+
// those terms.
8+
9+
use zerocopy::FromBytes;
10+
11+
#[path = "formats/loco.rs"]
12+
mod loco;
13+
14+
#[unsafe(no_mangle)]
15+
fn codegen_test(source: &[u8]) -> Option<&loco::Packet> {
16+
FromBytes::ref_from_bytes(source).ok()
17+
}

benches/ref_from_bytes.x86-64.mca

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
Iterations: 100
2+
Instructions: 1500
3+
Total Cycles: 699
4+
Total uOps: 1700
5+
6+
Dispatch Width: 4
7+
uOps Per Cycle: 2.43
8+
IPC: 2.15
9+
Block RThroughput: 4.3
10+
11+
12+
Instruction Info:
13+
[1]: #uOps
14+
[2]: Latency
15+
[3]: RThroughput
16+
[4]: MayLoad
17+
[5]: MayStore
18+
[6]: HasSideEffects (U)
19+
20+
[1] [2] [3] [4] [5] [6] Instructions:
21+
1 1 0.33 mov rdx, rsi
22+
1 1 0.33 cmp rsi, 4
23+
1 1 1.00 jae .LBB5_2
24+
1 0 0.25 xor eax, eax
25+
1 1 1.00 U ret
26+
1 1 0.50 lea rcx, [rdx - 4]
27+
1 1 0.33 mov rsi, rcx
28+
1 1 0.33 and rsi, -2
29+
1 1 0.33 add rsi, 4
30+
1 1 0.50 shr rcx
31+
1 0 0.25 xor eax, eax
32+
1 1 0.33 cmp rdx, rsi
33+
2 2 0.67 cmove rdx, rcx
34+
2 2 0.67 cmove rax, rdi
35+
1 1 1.00 U ret
36+
37+
38+
Resources:
39+
[0] - SBDivider
40+
[1] - SBFPDivider
41+
[2] - SBPort0
42+
[3] - SBPort1
43+
[4] - SBPort4
44+
[5] - SBPort5
45+
[6.0] - SBPort23
46+
[6.1] - SBPort23
47+
48+
49+
Resource pressure per iteration:
50+
[0] [1] [2] [3] [4] [5] [6.0] [6.1]
51+
- - 4.96 4.99 - 5.05 - -
52+
53+
Resource pressure by instruction:
54+
[0] [1] [2] [3] [4] [5] [6.0] [6.1] Instructions:
55+
- - 0.94 0.04 - 0.02 - - mov rdx, rsi
56+
- - 0.01 0.02 - 0.97 - - cmp rsi, 4
57+
- - - - - 1.00 - - jae .LBB5_2
58+
- - - - - - - - xor eax, eax
59+
- - - - - 1.00 - - ret
60+
- - 0.92 0.08 - - - - lea rcx, [rdx - 4]
61+
- - 0.06 0.92 - 0.02 - - mov rsi, rcx
62+
- - 0.01 0.12 - 0.87 - - and rsi, -2
63+
- - 0.06 0.91 - 0.03 - - add rsi, 4
64+
- - 0.95 - - 0.05 - - shr rcx
65+
- - - - - - - - xor eax, eax
66+
- - 0.04 0.94 - 0.02 - - cmp rdx, rsi
67+
- - 0.99 0.98 - 0.03 - - cmove rdx, rcx
68+
- - 0.98 0.98 - 0.04 - - cmove rax, rdi
69+
- - - - - 1.00 - - ret
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// Copyright 2026 The Fuchsia Authors
2+
//
3+
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
// This file may not be copied, modified, or distributed except according to
7+
// those terms.
8+
9+
use zerocopy::FromBytes;
10+
11+
#[path = "formats/loco.rs"]
12+
mod loco;
13+
14+
#[unsafe(no_mangle)]
15+
fn codegen_test(source: &[u8], count: usize) -> Option<&loco::Packet> {
16+
FromBytes::ref_from_bytes_with_elems(source, count).ok()
17+
}
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
Iterations: 100
2+
Instructions: 1300
3+
Total Cycles: 504
4+
Total uOps: 1500
5+
6+
Dispatch Width: 4
7+
uOps Per Cycle: 2.98
8+
IPC: 2.58
9+
Block RThroughput: 3.8
10+
11+
12+
Instruction Info:
13+
[1]: #uOps
14+
[2]: Latency
15+
[3]: RThroughput
16+
[4]: MayLoad
17+
[5]: MayStore
18+
[6]: HasSideEffects (U)
19+
20+
[1] [2] [3] [4] [5] [6] Instructions:
21+
1 1 0.33 mov rcx, rdx
22+
1 1 0.33 mov rdx, rsi
23+
1 1 0.33 movabs rax, 9223372036854775805
24+
1 1 0.33 cmp rcx, rax
25+
1 1 1.00 ja .LBB5_1
26+
1 1 0.50 lea rsi, [2*rcx + 4]
27+
1 0 0.25 xor eax, eax
28+
1 1 0.33 cmp rdx, rsi
29+
2 2 0.67 cmove rdx, rcx
30+
2 2 0.67 cmove rax, rdi
31+
1 1 1.00 U ret
32+
1 0 0.25 xor eax, eax
33+
1 1 1.00 U ret
34+
35+
36+
Resources:
37+
[0] - SBDivider
38+
[1] - SBFPDivider
39+
[2] - SBPort0
40+
[3] - SBPort1
41+
[4] - SBPort4
42+
[5] - SBPort5
43+
[6.0] - SBPort23
44+
[6.1] - SBPort23
45+
46+
47+
Resource pressure per iteration:
48+
[0] [1] [2] [3] [4] [5] [6.0] [6.1]
49+
- - 4.09 4.10 - 4.81 - -
50+
51+
Resource pressure by instruction:
52+
[0] [1] [2] [3] [4] [5] [6.0] [6.1] Instructions:
53+
- - 0.04 0.94 - 0.02 - - mov rcx, rdx
54+
- - 0.17 0.83 - - - - mov rdx, rsi
55+
- - 0.19 0.79 - 0.02 - - movabs rax, 9223372036854775805
56+
- - 0.75 0.18 - 0.07 - - cmp rcx, rax
57+
- - - - - 1.00 - - ja .LBB5_1
58+
- - 0.18 0.82 - - - - lea rsi, [2*rcx + 4]
59+
- - - - - - - - xor eax, eax
60+
- - 0.82 0.18 - - - - cmp rdx, rsi
61+
- - 0.97 0.17 - 0.86 - - cmove rdx, rcx
62+
- - 0.97 0.19 - 0.84 - - cmove rax, rdi
63+
- - - - - 1.00 - - ret
64+
- - - - - - - - xor eax, eax
65+
- - - - - 1.00 - - ret

benches/ref_from_prefix.rs

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2026 The Fuchsia Authors
2+
//
3+
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
// This file may not be copied, modified, or distributed except according to
7+
// those terms.
8+
9+
use zerocopy::FromBytes;
10+
11+
#[path = "formats/loco.rs"]
12+
mod loco;
13+
14+
#[unsafe(no_mangle)]
15+
fn codegen_test(source: &[u8]) -> Option<&loco::Packet> {
16+
match FromBytes::ref_from_prefix(source) {
17+
Ok((packet, _rest)) => Some(packet),
18+
_ => None,
19+
}
20+
}

benches/ref_from_prefix.x86-64.mca

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
Iterations: 100
2+
Instructions: 600
3+
Total Cycles: 206
4+
Total uOps: 700
5+
6+
Dispatch Width: 4
7+
uOps Per Cycle: 3.40
8+
IPC: 2.91
9+
Block RThroughput: 1.8
10+
11+
12+
Instruction Info:
13+
[1]: #uOps
14+
[2]: Latency
15+
[3]: RThroughput
16+
[4]: MayLoad
17+
[5]: MayStore
18+
[6]: HasSideEffects (U)
19+
20+
[1] [2] [3] [4] [5] [6] Instructions:
21+
1 1 0.33 mov rdx, rsi
22+
1 0 0.25 xor eax, eax
23+
1 1 0.33 sub rdx, 4
24+
2 2 0.67 cmovae rax, rdi
25+
1 1 0.50 shr rdx
26+
1 1 1.00 U ret
27+
28+
29+
Resources:
30+
[0] - SBDivider
31+
[1] - SBFPDivider
32+
[2] - SBPort0
33+
[3] - SBPort1
34+
[4] - SBPort4
35+
[5] - SBPort5
36+
[6.0] - SBPort23
37+
[6.1] - SBPort23
38+
39+
40+
Resource pressure per iteration:
41+
[0] [1] [2] [3] [4] [5] [6.0] [6.1]
42+
- - 1.99 2.00 - 2.01 - -
43+
44+
Resource pressure by instruction:
45+
[0] [1] [2] [3] [4] [5] [6.0] [6.1] Instructions:
46+
- - 0.99 - - 0.01 - - mov rdx, rsi
47+
- - - - - - - - xor eax, eax
48+
- - - 1.00 - - - - sub rdx, 4
49+
- - 0.02 1.00 - 0.98 - - cmovae rax, rdi
50+
- - 0.98 - - 0.02 - - shr rdx
51+
- - - - - 1.00 - - ret
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
// Copyright 2026 The Fuchsia Authors
2+
//
3+
// Licensed under a BSD-style license <LICENSE-BSD>, Apache License, Version 2.0
4+
// <LICENSE-APACHE or https://www.apache.org/licenses/LICENSE-2.0>, or the MIT
5+
// license <LICENSE-MIT or https://opensource.org/licenses/MIT>, at your option.
6+
// This file may not be copied, modified, or distributed except according to
7+
// those terms.
8+
9+
use zerocopy::FromBytes;
10+
11+
#[path = "formats/loco.rs"]
12+
mod loco;
13+
14+
#[unsafe(no_mangle)]
15+
fn codegen_test(source: &[u8], count: usize) -> Option<&loco::Packet> {
16+
match FromBytes::ref_from_prefix_with_elems(source, count) {
17+
Ok((packet, _rest)) => Some(packet),
18+
_ => None,
19+
}
20+
}

0 commit comments

Comments
 (0)