Skip to content

Commit

Permalink
polkawasm: implement actual GC that frees memory via extfree
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed Nov 29, 2023
1 parent ad0db4b commit 1e3d060
Show file tree
Hide file tree
Showing 11 changed files with 452 additions and 502 deletions.
28 changes: 20 additions & 8 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,21 +816,33 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
args = append(args, "--asyncify")
}

args = append(args,
opt,
"-g",
result.Executable,
"--output", result.Executable,
)

if config.Target.Triple == "wasm32-unknown-polkawasm" {
args = append(args, "--signext-lowering")
args = append(args,
opt,
"--signext-lowering",
// "--signature-pruning",
// "--const-hoisting",
// "--mvp-features",
result.Executable,
"--output",
result.Executable,
)
} else {
args = append(args,
opt,
"-g",
result.Executable,
"--output",
result.Executable,
)
}

cmd := exec.Command(goenv.Get("WASMOPT"), args...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr

println(cmd.String())

err := cmd.Run()
if err != nil {
return fmt.Errorf("wasm-opt failed: %w", err)
Expand Down
2 changes: 2 additions & 0 deletions compileopts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ func (c *Config) OptLevel() (level string, speedLevel, sizeLevel int) {
return "O1", 1, 0
case "2":
return "O2", 2, 0
case "3":
return "O3", 2, 0
case "s":
return "Os", 2, 1
case "z":
Expand Down
2 changes: 1 addition & 1 deletion compileopts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ var (
validSerialOptions = []string{"none", "uart", "usb"}
validPrintSizeOptions = []string{"none", "short", "full"}
validPanicStrategyOptions = []string{"print", "trap"}
validOptOptions = []string{"none", "0", "1", "2", "s", "z"}
validOptOptions = []string{"none", "0", "1", "2", "3", "s", "z"}
)

// Options contains extra options to give to the compiler. These options are
Expand Down
2 changes: 1 addition & 1 deletion lib/binaryen
Submodule binaryen updated 746 files
2 changes: 1 addition & 1 deletion main.go
Original file line number Diff line number Diff line change
Expand Up @@ -1406,7 +1406,7 @@ func main() {
}
command := os.Args[1]

opt := flag.String("opt", "z", "optimization level: 0, 1, 2, s, z")
opt := flag.String("opt", "z", "optimization level: 0, 1, 2, 3, s, z")
gc := flag.String("gc", "", "garbage collector to use (none, leaking, conservative)")
panicStrategy := flag.String("panic", "print", "panic strategy (print, trap)")
scheduler := flag.String("scheduler", "", "which scheduler to use (none, tasks, asyncify)")
Expand Down
28 changes: 6 additions & 22 deletions src/runtime/gc_custom.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//go:build gc.custom
//go:build gc.custom_wip

package runtime

Expand Down Expand Up @@ -61,31 +61,21 @@ func setHeapEnd(newHeapEnd uintptr) {
//
//go:noinline
func alloc(size uintptr, layout unsafe.Pointer) unsafe.Pointer {
printstr("call alloc(")
printnum(int(size))
printstr(")\n")

if size == 0 {
return unsafe.Pointer(&zeroSizedAlloc)
}

printstr("\ttotal memory ")
printnum(int(gcTotalAlloc))
printstr("\n")

size = align(size)
size += align(unsafe.Sizeof(layout))

// Try to bound heap growth.
if gcTotalAlloc+uint64(size) < gcTotalAlloc {
printstr("\tout of memory\n")
abort()
}

// Allocate the memory.
pointer := extalloc(size)
if pointer == nil {
printstr("\textalloc call failed\n")
abort()
gcAllocPanic()
}

// Zero-out the allocated memory
Expand All @@ -105,19 +95,13 @@ func free(ptr unsafe.Pointer) {

// markRoots is called with the start and end addresses to scan for references.
// It is currently only called with the top and bottom of the stack.
func markRoots(start, end uintptr) {

}
func markRoots(start, end uintptr) {}

// GC is called to explicitly run garbage collection.
func GC() {

}
func GC() {}

// SetFinalizer registers a finalizer.
func SetFinalizer(obj interface{}, finalizer interface{}) {

}
func SetFinalizer(obj interface{}, finalizer interface{}) {}

// ReadMemStats populates m with memory statistics.
func ReadMemStats(ms *MemStats) {
Expand Down
Loading

0 comments on commit 1e3d060

Please # to comment.