From 1b095a11de098c506207d56843525008e813101d Mon Sep 17 00:00:00 2001 From: dgyyy Date: Mon, 11 Nov 2024 14:51:35 +0800 Subject: [PATCH] fix(tcc): add 'ref' scale input for tcc --- src/common/bench/bench.c | 11 ++++ src/common/bench/include/bench.h | 2 + src/gemm/configs/ref-config.c | 3 +- src/gemm/configs/test-config.c | 2 +- src/gemm/configs/train-config.c | 2 +- src/gemm/gemm.c | 7 ++- src/gemm/include/gemm.h | 1 + src/tcc/configs/ref-config.c | 18 +++++++ src/tcc/input/ref | Bin 0 -> 21332 bytes src/tcc/input/ref.c | 87 ++++++++++++++++++++++++++++++ src/tcc/resources/resources-ref.S | 8 +++ 11 files changed, 136 insertions(+), 5 deletions(-) create mode 100755 src/tcc/input/ref create mode 100644 src/tcc/input/ref.c diff --git a/src/common/bench/bench.c b/src/common/bench/bench.c index 524032d..13e426c 100644 --- a/src/common/bench/bench.c +++ b/src/common/bench/bench.c @@ -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; +} diff --git a/src/common/bench/include/bench.h b/src/common/bench/include/bench.h index b96a8eb..9028a60 100644 --- a/src/common/bench/include/bench.h +++ b/src/common/bench/include/bench.h @@ -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; diff --git a/src/gemm/configs/ref-config.c b/src/gemm/configs/ref-config.c index 14987ff..fe17f25 100644 --- a/src/gemm/configs/ref-config.c +++ b/src/gemm/configs/ref-config.c @@ -1,3 +1,4 @@ #include -bench_gemm_config config = {110, 110, 110}; +bench_gemm_config config = {.m = 110, .n = 110, .k = 110, .checksum = 0x503d0fe9}; + diff --git a/src/gemm/configs/test-config.c b/src/gemm/configs/test-config.c index e1d725c..cbcf50b 100644 --- a/src/gemm/configs/test-config.c +++ b/src/gemm/configs/test-config.c @@ -1,3 +1,3 @@ #include -bench_gemm_config config = {50, 50, 50}; +bench_gemm_config config = {.m = 50, .n = 50, .k = 50, .checksum = 0x53c1fb28}; diff --git a/src/gemm/configs/train-config.c b/src/gemm/configs/train-config.c index b20fa67..17b3714 100644 --- a/src/gemm/configs/train-config.c +++ b/src/gemm/configs/train-config.c @@ -1,3 +1,3 @@ #include -bench_gemm_config config = {40, 40, 40}; +bench_gemm_config config = {.m = 40, .n = 40, .k = 40, .checksum = 0x1efebe93}; diff --git a/src/gemm/gemm.c b/src/gemm/gemm.c index d7c6a4d..a8432c4 100644 --- a/src/gemm/gemm.c +++ b/src/gemm/gemm.c @@ -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; } } @@ -41,8 +41,9 @@ 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); @@ -50,10 +51,12 @@ int main() { 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; } diff --git a/src/gemm/include/gemm.h b/src/gemm/include/gemm.h index 6ace54b..795b34f 100644 --- a/src/gemm/include/gemm.h +++ b/src/gemm/include/gemm.h @@ -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); diff --git a/src/tcc/configs/ref-config.c b/src/tcc/configs/ref-config.c index e69de29..e8277d8 100644 --- a/src/tcc/configs/ref-config.c +++ b/src/tcc/configs/ref-config.c @@ -0,0 +1,18 @@ +#include "../config.h" +#include + +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}; diff --git a/src/tcc/input/ref b/src/tcc/input/ref new file mode 100755 index 0000000000000000000000000000000000000000..f8cc33e39367499ad564ec84375b7347098beba9 GIT binary patch literal 21332 zcmeHP3v`@Soxk(=hPITxrfGTFq0@=9lnR6pUcv}7T~|1WrBwvAmb8L^2HFNe(SCI@ zaXsLQXDAVlNTCfPt%$azyj+Je(^V7?D~9z2RiIP~me3~BHkr)+e*b&F@0({5#ND%d z&dxcv_x->B`+whizjRgdg84q5PdPuo+NW4sdY-aRVZJ~~{S*~ZL3Nx8DNE{Ryp7zx z^*p7REH+Nkdi>HjKZmRoP7UPf+TX2Csj?&Xr5+%_p3&&o>mEH(5pJ zu2$io&mTrxbk_QzK&)e^HE5|el~rMD5^@{4QmM$3zQC;2KFsr9VaxnNv~jXOI%~Bb z^8?QOXk_zHbZ%~tbE0$B50=7XxeZ%nZu$!0<_4wLiw6R+L?L5YSg*m%4Gu-^ZiVr> zJ=9xlvhu)r{a{<~Vu203{J_f(yaFTP#dTA$;82sbMMdWkw_Ksd{3^Q0US_TzlDv^L z$_<5MY1KNFbW#&RBOlvb+%cxDcSk|S&(bvY#@plc_zrJ8#&EpAZHTF^WlCkz$*GyN zrJB;+Dh0eVYhr5pXF&JR_9@{|NNutnR-`relj^om<~l(nOLc*lyX@?nsaRqt+-U(8 z=s9!qP^O{L!04RhzzE!jHLQG`d@%D~1a1FDT7|82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2Z1geC>G;X$OP=_k+6%H zH||WO7Gd0EGA`AGZ{Xt07dEO#^Si+~+~So1myC z0mlrK^kLxrI%u#D@E*iIe+8zryny-dQ>qhq9{C3JZn9GE18y&a#_fH@VmBE23f7qb zLAVPHyAiPG0N?X5b~?r_z+C=(NZ1r{2F#BikwRX2g54rfj17n`ub@o{1th1f{9LMpT$N7Xqc!W#%T!-s%-DOCO zM9`%^hjbkh3710pI?~NZYmg{gP~m@Aob-=|xA0_Tk7-i&BS_brBK!lkN__>${~kHb z$Ul0`8$No@;|81_LCLLr*A~MH#j6B|WU_GbkMD#bfcTMlR|kh+lA#jGc zd&0c;op{p8O{X-U+H%_IXPkM~`_DcnzU1=OrOQ5Z#g(7E>gsDg_xZMKuUo!iW&8DC zxZ%bxUiOJkeroZj>-T@@rZ0bG6>$#%7sd)|-pXY+tW@)sE?K!m|2uDao0@m|iWO?! ziYr&FT)uM2 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; +} + diff --git a/src/tcc/resources/resources-ref.S b/src/tcc/resources/resources-ref.S index e69de29..53c68f1 100644 --- a/src/tcc/resources/resources-ref.S +++ b/src/tcc/resources/resources-ref.S @@ -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: