Skip to content

Commit abb0850

Browse files
authored
benchmarking: improve cross compile and testing (#9)
* test: prepare cross compile automation * build: finally fix cross compile * test: run add and print thousands of times To support better benchmarking, loop the `main` function thousands of times. * test: improve benchmarking different architectures, remove arm asms
1 parent ad53e69 commit abb0850

18 files changed

Lines changed: 221 additions & 168 deletions

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,9 @@ Cargo.lock
2626

2727
# temporary files
2828
*.bin
29+
*.arm.s
30+
*.as
31+
a.out
2932

3033
.vscode/
3134
test_binary_translate*

run.sh

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,34 @@
11
#!/usr/bin/env bash
22

3-
ASM_FILE=$1
3+
ARCH=$1
4+
ASM_FILE=$2
45
PLATFORM=""
56
LD_FLAGS=""
6-
BENCHMARKING="false" # "true" to enable
7+
BENCHMARKING="true" # "true" to enable
8+
QEMU=""
79

8-
if [[ -z "$ASM_FILE" ]]; then
9-
echo "Error: Assembly (.S) file is not passed in."
10-
echo "Usage: ./run.sh test_binary_translate_add.S"
10+
if [[ -z "$ARCH" || -z "$ASM_FILE" ]]; then
11+
echo "Error: Architecture and assembly (.S) file must be provided."
12+
echo "Usage: ./run.sh [riscv|arm] <assembly_file.S>"
1113
exit 1
1214
fi
1315

14-
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
15-
PLATFORM="aarch64-linux-gnu"
16+
if [[ "$ARCH" != "riscv" && "$ARCH" != "arm" ]]; then
17+
echo "Error: Architecture must be either 'riscv' or 'arm'."
18+
echo "Usage: ./run.sh [riscv|arm] <assembly_file.S>"
19+
exit 1
20+
fi
21+
22+
if [[ "$ARCH" == "riscv" ]]; then
23+
QEMU="qemu-riscv64"
24+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
25+
PLATFORM="riscv64-unknown-linux-gnu-"
26+
fi
27+
elif [[ "$ARCH" == "arm" ]]; then
28+
QEMU="qemu-aarch64"
29+
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
30+
PLATFORM="aarch64-unknown-linux-gnu-"
31+
fi
1632
fi
1733

1834
if [[ "$OSTYPE" == "darwin"* ]]; then
@@ -23,9 +39,10 @@ fi
2339
"$PLATFORM"as "$ASM_FILE" -o "$ASM_FILE".as || { echo "Assembly compilation failed"; exit 1; }
2440
"$PLATFORM"ld "$ASM_FILE".as -o "$ASM_FILE".bin $LD_FLAGS || { echo "Linking failed"; exit 1; }
2541

26-
./"$ASM_FILE".bin
42+
"$QEMU" ./"$ASM_FILE".bin
2743
echo "$?"
2844

2945
if [ "$BENCHMARKING" = true ]; then
30-
hyperfine -r 1000 -w 100 -Ni ./"$ASM_FILE".bin
46+
hyperfine -r 1000 -w 100 -Ni ""$QEMU" ./"$ASM_FILE".bin"
3147
fi
48+

src/utils.rs

Lines changed: 62 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,68 @@ use std::fs;
22

33
use crate::{instruction::RiscVInstruction, translate::translate_instrs};
44

5-
pub const START: &str = r#"
5+
/// Loop main() 10,000 times. Uses a3.
6+
pub const RISCV_LOOP_START: &str = r#"
7+
.text
8+
9+
.global _start
10+
.global _main
11+
12+
.balign 4 # not sure if these are needed for RISC-V
13+
_start:
14+
# while i < 10,000
15+
li a3, 10000
16+
.loop:
17+
addi a3, a3, -1
18+
ble a3, x0, .end
19+
20+
# main()
21+
jal ra, main
22+
23+
# while loop
24+
j .loop
25+
.end:
26+
# exit(0)
27+
li a7,93
28+
ecall
29+
30+
.balign 4
31+
_main:
32+
main:
33+
"#;
34+
35+
/// Loop main() 10,000 times. Uses x3
36+
pub const ARM_LOOP_START: &str = r#"
37+
.text
38+
39+
.global _start
40+
.global _main
41+
42+
.balign 4
43+
_start:
44+
# i = 10,000
45+
mov x3, #10000
46+
# while i > 0
47+
.loop:
48+
sub x3, x3, 1
49+
50+
cmp x3, xzr
51+
ble .end
52+
53+
# main()
54+
bl main
55+
56+
b .loop
57+
.end:
58+
mov x8, #93
59+
svc #0
60+
61+
.balign 4
62+
_main:
63+
main:
64+
"#;
65+
66+
pub const ARM_START: &str = r#"
667
.text
768
869
.global _start

tests/add/add.arm.s

Lines changed: 0 additions & 34 deletions
This file was deleted.

tests/add/add.riscv.s

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,25 @@
1+
.text
2+
3+
.global _start
4+
.global _main
5+
6+
.balign 4 # not sure if these are needed for RISC-V
7+
_start:
8+
# while i < 10,000
9+
li a3, 10000
10+
.loop:
11+
addi a3, a3, -1
12+
ble a3, x0, .end
13+
14+
# main()
15+
jal ra, main
16+
17+
# while loop
18+
j .loop
19+
.end:
20+
# exit(0)
21+
li a7,93
22+
ecall
123
main:
224
addi sp,sp,-32
325
sd ra,24(sp)

tests/add/test_add.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,14 @@ mod tests {
44
use binary_room::translate::*;
55
use binary_room::utils;
66
use binary_room::utils::translate_to_file;
7-
use binary_room::utils::START;
7+
use binary_room::utils::ARM_LOOP_START;
88

99
#[test]
1010
fn test_binary_translate() {
1111
let riscv_asm: Vec<RiscVInstruction> = vec![
12-
RiscVInstruction::Verbatim { text: START.to_string() },
12+
RiscVInstruction::Verbatim {
13+
text: ARM_LOOP_START.to_string(),
14+
},
1315
RiscVInstruction::Addi {
1416
dest: RiscVRegister::SP,
1517
src: RiscVRegister::SP,

tests/binaries/flake.nix

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
# Use the same mkShell as documented above
2020
packages = with pkgs; [
2121
gcc
22+
# qemu
2223
# pkgs.clang-tools
2324
];
2425
};

tests/binaries/hello_world.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
#import <stdio.h>
1+
#include <stdio.h>
22

33
int main(void) {
44
printf("hello world\n");

tests/binaries/riscv/cross.arm.nix

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
with import <nixpkgs> {
2+
crossSystem = {
3+
config = "aarch64-unknown-linux-gnu";
4+
};
5+
};
6+
7+
mkShell {
8+
buildInputs = [ ]; # your dependencies here
9+
}

tests/binaries/riscv/cross.nix

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
with import <nixpkgs> {
2+
crossSystem = {
3+
config = "riscv64-unknown-linux-gnu";
4+
};
5+
};
6+
7+
mkShell {
8+
buildInputs = [ ]; # your dependencies here
9+
}

0 commit comments

Comments
 (0)