Skip to content

Commit

Permalink
fix(tcc): add 'ref' scale input for tcc
Browse files Browse the repository at this point in the history
  • Loading branch information
dgyyy authored and xinyangli committed Nov 17, 2024
1 parent 685e69c commit 1b095a1
Show file tree
Hide file tree
Showing 11 changed files with 136 additions and 5 deletions.
11 changes: 11 additions & 0 deletions src/common/bench/bench.c
Original file line number Diff line number Diff line change
Expand Up @@ -67,3 +67,14 @@ uint32_t checksum(void *start, void *end) {
hash += hash << 5;
return hash;
}

static uint32_t seed = 1;

void bench_srand(uint32_t _seed) {
seed = _seed & 0x7fff;
}

uint32_t bench_rand() {
seed = (seed * (uint32_t)214013L + (uint32_t)2531011L);
return (seed >> 16) & 0x7fff;
}
2 changes: 2 additions & 0 deletions src/common/bench/include/bench.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ extern "C" {
uint64_t uptime();
char *format_time(uint64_t us);
uint32_t checksum(void *start, void *end);
void bench_srand(uint32_t _seed);
uint32_t bench_rand();

typedef struct {
void *sub_config;
Expand Down
3 changes: 2 additions & 1 deletion src/gemm/configs/ref-config.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
#include <gemm.h>

bench_gemm_config config = {110, 110, 110};
bench_gemm_config config = {.m = 110, .n = 110, .k = 110, .checksum = 0x503d0fe9};

2 changes: 1 addition & 1 deletion src/gemm/configs/test-config.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include <gemm.h>

bench_gemm_config config = {50, 50, 50};
bench_gemm_config config = {.m = 50, .n = 50, .k = 50, .checksum = 0x53c1fb28};
2 changes: 1 addition & 1 deletion src/gemm/configs/train-config.c
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
#include <gemm.h>

bench_gemm_config config = {40, 40, 40};
bench_gemm_config config = {.m = 40, .n = 40, .k = 40, .checksum = 0x1efebe93};
7 changes: 5 additions & 2 deletions src/gemm/gemm.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ void serial_init(int m, int n, double *a, int lda) {
void random_init(int m, int n, double *a, int lda) {
for (int j = 0; j < n; j++) {
for (int i = 0; i < m; i++)
A(i, j) = 2.0 * rand() - 1.0;
A(i, j) = 2.0 * bench_rand() - 1.0;
}
}

Expand All @@ -41,19 +41,22 @@ int main() {
memset(C, 0, m * n * sizeof(double));

uint64_t start_time, end_time;
srand(1556);
bench_srand(1556);

//Because we init A and B randomly, the checksum of C will be different.
random_init(m, k, A, m);
random_init(k, n, B, k);

start_time = uptime();
matmul(m, n, k, A, m, B, k, C, m);
end_time = uptime();


bench_free(A);
bench_free(B);
bench_free(C);


BENCH_LOG(INFO, "OpenPerf time: %s", format_time(end_time - start_time));
return 0;
}
1 change: 1 addition & 0 deletions src/gemm/include/gemm.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ typedef struct {
uint32_t m;
uint32_t n;
uint32_t k;
uint64_t checksum;
} bench_gemm_config;

void AddDot4x4(int, double *, int, double *, int, double *, int);
Expand Down
18 changes: 18 additions & 0 deletions src/tcc/configs/ref-config.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include "../config.h"
#include <fs.h>

Finfo file_table[] = {
{"/share/trm.c", 269, 0, NULL, NULL},
{"/share/trap.h", 110, 269, NULL, NULL},
{"/share/ref.c", 1844, 379, NULL, NULL},
{"/share/ref", 21332, 2223, NULL, NULL}
};

int tcc_argc1 = 10;
char *tcc_argv1[] = {
"./tcc", "/share/trm.c", "/share/ref.c", "-ffreestanding",
"-nostdlib", "-o", "/share/ref", "-Wl,-Ttext=0x80000000",
"-O2", "-static"
};

bench_tcc_config config = {.file_count = 4};
Binary file added src/tcc/input/ref
Binary file not shown.
87 changes: 87 additions & 0 deletions src/tcc/input/ref.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
#include "trap.h"

typedef unsigned int uint32_t;
typedef unsigned char uint8_t;
typedef uint32_t size_t;
#define NULL 0
#define LENGTH(x) sizeof(x) / sizeof(x[0])

void *memset(void *s, int c, size_t n) {
char *p = NULL;
if (s == NULL)
return s;

p = (char *)s;
while (n > 0) {
*(p++) = c;
n--;
}
return s;
}

int add(int a, int b) { return a + b; }
int sub(int a, int b) { return a - b; }
int mul(int a, int b) { return a * b; }
int div_safe(int a, int b) { return b != 0 ? a / b : 0; }

int test_data[] = {0, 1, 2, 3, 0x7fffffff, 0x80000000, 0x80000001, 0xffffffff};
int ans_data[1000] = {0};

#define NR_DATA 1000

int add_main() {
int i, j;
for (i = 0; i < NR_DATA; i++) {
for (j = 0; j < NR_DATA; j++) {
int expected = add(test_data[i % 8], test_data[j % 8]);
check(add(test_data[i % 8], test_data[j % 8]) == expected);
}
}
return 0;
}

int sub_main() {
int i, j;
for (i = 0; i < NR_DATA; i++) {
for (j = 0; j < NR_DATA; j++) {
int expected = sub(test_data[i % 8], test_data[j % 8]);
check(sub(test_data[i % 8], test_data[j % 8]) == expected);
}
}
return 0;
}

int mul_main() {
int i, j;
for (i = 0; i < NR_DATA; i++) {
for (j = 0; j < NR_DATA; j++) {
int expected = mul(test_data[i % 8], test_data[j % 8]);
check(mul(test_data[i % 8], test_data[j % 8]) == expected);
}
}
return 0;
}

int div_main() {
int i, j;
for (i = 0; i < NR_DATA; i++) {
for (j = 0; j < NR_DATA; j++) {
int expected = div_safe(test_data[i % 8], test_data[(j % 7) + 1]);
check(div_safe(test_data[i % 8], test_data[(j % 7) + 1]) == expected);
}
}
return 0;
}

int main() {
memset(ans_data, 0, sizeof(ans_data));

for (int k = 0; k < 10; k++) {
add_main();
sub_main();
mul_main();
div_main();
}
return 0;
}

8 changes: 8 additions & 0 deletions src/tcc/resources/resources-ref.S
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
.section .data
.global ramdisk_start, ramdisk_end
ramdisk_start:
.incbin "input/trm.c"
.incbin "input/trap.h"
.incbin "input/ref.c"
.incbin "input/ref"
ramdisk_end:

0 comments on commit 1b095a1

Please sign in to comment.