Skip to content

Commit 09b73bc

Browse files
committed
gc
1 parent e1ae85f commit 09b73bc

File tree

22 files changed

+394
-230
lines changed

22 files changed

+394
-230
lines changed

.gitmodules

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,3 +7,6 @@
77
[submodule "vendor/mimalloc"]
88
path = vendor/mimalloc
99
url = https://github.com/microsoft/mimalloc
10+
[submodule "vendor/light"]
11+
path = vendor/light
12+
url = https://git.savannah.gnu.org/git/lightning.git

main/minivm.c

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,17 @@
77
#include "../vm/std.h"
88
#include "../vm/lua/repl.h"
99

10-
#include <stdarg.h>
11-
#include <stdio.h>
12-
#include <string.h>
13-
#include <time.h>
14-
1510
#if VM_USE_SPALL
1611
#define SPALL_AUTO_IMPLEMENTATION
1712
#include "../vendor/spall/auto.h"
1813
#endif
1914

20-
__attribute__((no_instrument_function)) int main(int argc, char **argv) {
15+
#if VM_USE_SPALL
16+
int __attribute__((no_instrument_function))
17+
#else
18+
int
19+
#endif
20+
main(int argc, char **argv) {
2121
#if VM_USE_SPALL
2222
spall_auto_init("out.spall");
2323
spall_auto_thread_init(0, SPALL_DEFAULT_BUFFER_SIZE);
@@ -42,8 +42,6 @@ __attribute__((no_instrument_function)) int main(int argc, char **argv) {
4242
} else {
4343
isrepl = false;
4444

45-
clock_t start = clock();
46-
4745
const char *name = NULL;
4846
const char *src;
4947
bool f_flag = true;
@@ -86,6 +84,10 @@ __attribute__((no_instrument_function)) int main(int argc, char **argv) {
8684
}
8785
}
8886

87+
if (isrepl) {
88+
vm_lang_lua_repl(vm);
89+
}
90+
8991
vm_state_delete(vm);
9092

9193
#if VM_USE_SPALL

main/primes.py

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,37 @@
11
import sympy
2+
import math
23

34
max_width = 60
4-
n = 32
5-
ls = [2, 4, 8, 16]
5+
n = 53
6+
ls = [1, 3, 7, 13, 29]
67

78
with open('vm/primes.inc', 'w') as f:
89
p32 = 2 ** 32
910
while True:
1011
p = sympy.prevprime(int(n)+1)
1112
if p >= p32:
1213
break
14+
if len(ls) >= 32:
15+
break
1316
ls.append(p)
1417
n *= 1.75
1518
ml = max(map(len, map(str, ls)))
1619
n = (max_width - 5) // (ml + 2)
1720
f.write(f'// generated by: python3 main/primes.py\n\n')
18-
f.write(f'static const uint64_t vm_primes_table[{len(ls)}] = ' + '{\n ')
21+
f.write(f'static uint32_t vm_primes_table[{len(ls)}] = ' + '{\n ')
1922
for i, p in enumerate(ls):
2023
s = str(p)
2124
s = ' ' * (ml - len(s)) + s
2225
f.write(f' {s},')
2326
if i % n == n - 1:
2427
f.write('\n ')
2528
f.write('\n};\n\n')
26-
f.write('static const uint64_t vm_primes_mod(uint8_t p, size_t n) {\n')
29+
f.write('static uint32_t vm_primes_mod(uint8_t p, uint32_t n) {\n')
2730
f.write(' switch (p) {\n')
2831
f.write(' default:\n')
2932
f.write(' __builtin_unreachable();\n')
3033
for i, p in enumerate(ls):
3134
f.write(f' case {i}:\n')
3235
f.write(f' return n % {p};\n')
3336
f.write(' }\n')
34-
f.write('}\n')
37+
f.write('}\n')

makefile

Lines changed: 11 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
# Must be a GCC or Clang
33
CC ?= cc
44

5-
OPT ?= -O2 -flto
5+
OPT ?= -O3 -ffast-math -flto -DNDEBUG
66

77
EXE ?=
88

99
TIME_CMD ?= $(shell which gdate || which date) +%s%3N
1010

11+
LLVM_PROFDATA ?= llvm-profdata
12+
1113
LIBM ?= YES
1214
LIBM_NO :=
1315
LIBM_YES := -lm
@@ -18,6 +20,8 @@ BASE_CFLAGS := ${OPT} -Ivendor/tree-sitter/lib/include -Ivendor/mimalloc/include
1820
BASE_LDFLAGS := ${OPT} ${LDFLAGS} ${LIBM_FLAGS}
1921

2022
# mimalloc
23+
24+
# BASE_LDFLAGS += -lSynchronization
2125
MI_PRIM_SRC := vendor/mimalloc/src/prim/unix/prim.c
2226

2327
# object files and depends
@@ -31,8 +35,6 @@ MAIN_OBJS = ${MAIN_SRCS:%.c=build/obj/%.o}
3135

3236
MAIN_DEPS = ${MAIN_SRCS:%.c=build/dep/%.dep}
3337

34-
MAKE_INCLUDE ?=
35-
3638
# tests
3739

3840
TEST_SRCS := $(wildcard test/lua/*/*.lua)
@@ -53,18 +55,19 @@ clean: .dummy
5355
rm -rf build
5456

5557
gcc-pgo: .dummy
56-
$(MAKE) -Bj minivm OPT="-O3 -flto=auto -fgcse-sm -fgcse-las -fipa-pta -fdevirtualize-at-ltrans -fdevirtualize-speculatively -fno-exceptions -fomit-frame-pointer -fprofile-generate -DNDEBUG"
58+
$(MAKE) -Bj minivm OPT="$(OPT) -fgcse-sm -fgcse-las -fipa-pta -fdevirtualize-at-ltrans -fdevirtualize-speculatively -fno-exceptions -fomit-frame-pointer -fprofile-generate"
5759
build/bin/minivm test/lua/fib/fib.lua
5860
build/bin/minivm test/lua/tables/trees.lua
5961
build/bin/minivm test/lua/closure/funcret.lua
60-
$(MAKE) -Bj minivm OPT="-O3 -flto=auto -fgcse-sm -fgcse-las -fipa-pta -fdevirtualize-at-ltrans -fdevirtualize-speculatively -fno-exceptions -fomit-frame-pointer -fprofile-use -DNDEBUG"
62+
$(MAKE) -Bj minivm OPT="$(OPT) -fgcse-sm -fgcse-las -fipa-pta -fdevirtualize-at-ltrans -fdevirtualize-speculatively -fno-exceptions -fomit-frame-pointer -fprofile-use"
6163

6264
clang-pgo: .dummy
63-
$(MAKE) -Bj minivm OPT="-O3 -flto=auto -fno-exceptions -fomit-frame-pointer -fprofile-generate -DNDEBUG"
65+
$(MAKE) -Bj minivm OPT="$(OPT) -mllvm -polly -fno-exceptions -fprofile-instr-generate"
6466
build/bin/minivm test/lua/fib/fib.lua
6567
build/bin/minivm test/lua/tables/trees.lua
6668
build/bin/minivm test/lua/closure/funcret.lua
67-
$(MAKE) -Bj minivm OPT="-O3 -flto=auto -fno-exceptions -fomit-frame-pointer -fprofile-use -DNDEBUG"
69+
$(LLVM_PROFDATA) merge default.profraw -o default.profdata
70+
$(MAKE) -Bj minivm OPT="$(OPT) -mllvm -polly -fno-exceptions -fprofile-use"
6871

6972
wasm: .dummy
7073
$(MAKE) -Bj CC=emcc EXE=.wasm \
@@ -87,7 +90,7 @@ ${TEST_TXTS}: ${@:build/test/%.log=%.lua} minivm
8790
${MAIN_OBJS}:
8891
@mkdir -p ${dir ${@}}
8992
@mkdir -p ${dir ${@:build/obj/%.o=build/dep/%.dep}}
90-
${CC} ${@:build/obj/%.o=%.c} -c -o ${@} ${BASE_CFLAGS}
93+
${CC} ${@:build/obj/%.o=%.c} -c -o ${@} ${BASE_CFLAGS}
9194
${CC} ${@:build/obj/%.o=%.c} -MM -MF ${@:build/obj/%.o=build/dep/%.dep} -MT ${@} ${BASE_CFLAGS}
9295

9396
include ${wildcard ${MAIN_DEPS}}

test/lua/basic/elseif.lua

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
for x=1, 3 do
3+
if x == 1 then
4+
print(1)
5+
elseif x == 2 then
6+
print(2)
7+
elseif x == 3 then
8+
print(3)
9+
end
10+
end

test/lua/tables/trees-lambda.lua

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
2+
local function BottomUpTree(item, depth)
3+
if depth > 0 then
4+
local i = item + item
5+
depth = depth - 1
6+
local left = BottomUpTree(i - 1, depth)
7+
local right = BottomUpTree(i, depth)
8+
return function(n)
9+
if n == 1 then
10+
return item
11+
elseif n == 2 then
12+
return left
13+
elseif n == 3 then
14+
return right
15+
else
16+
return nil
17+
end
18+
end
19+
else
20+
return function(n)
21+
if n == 1 then
22+
return item
23+
else
24+
return nil
25+
end
26+
end
27+
end
28+
end
29+
30+
local function ItemCheck(tree)
31+
if tree(2) then
32+
return tree(1) + ItemCheck(tree(2)) - ItemCheck(tree(3))
33+
else
34+
return tree(1)
35+
end
36+
end
37+
38+
local function pow2(n)
39+
if n == 0 then
40+
return 1
41+
else
42+
return pow2(n - 1) * 2
43+
end
44+
end
45+
46+
local mindepth = 4
47+
local maxdepth = tonumber(arg and arg[1]) or 12
48+
49+
local stretchdepth = maxdepth + 1
50+
51+
local stretchtree = BottomUpTree(0, stretchdepth)
52+
53+
local longlivedtree = BottomUpTree(0, maxdepth)
54+
55+
local depth = mindepth
56+
while depth <= maxdepth do
57+
local iterations = pow2(maxdepth - depth + mindepth)
58+
local check = 0
59+
local i = 1
60+
while i <= iterations do
61+
local x = ItemCheck(BottomUpTree(1, depth))
62+
check = check + x + ItemCheck(BottomUpTree(-1, depth))
63+
i = i + 1
64+
end
65+
print(depth, check)
66+
depth = depth + 2
67+
-- if vm then vm.gc() end
68+
end
69+
70+
print(maxdepth, ItemCheck(longlivedtree))

0 commit comments

Comments
 (0)