Skip to content

Commit

Permalink
polkawasm: implement actual GC that calls extalloc/extfree
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed Nov 28, 2023
1 parent b637ba9 commit c21153f
Show file tree
Hide file tree
Showing 11 changed files with 477 additions and 514 deletions.
76 changes: 43 additions & 33 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,10 @@ import (
"io/fs"
"math/bits"
"os"
"os/exec"
"path/filepath"
"runtime"
"sort"
"strconv"
"strings"

"github.com/gofrs/flock"
"github.com/tinygo-org/tinygo/compileopts"
Expand Down Expand Up @@ -805,37 +803,49 @@ func Build(pkgName, outpath, tmpdir string, config *compileopts.Config) (BuildRe
}
}

// Run wasm-opt for wasm binaries
if arch := strings.Split(config.Triple(), "-")[0]; arch == "wasm32" {
optLevel, _, _ := config.OptLevel()
opt := "-" + optLevel

var args []string

if config.Scheduler() == "asyncify" {
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")
}

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

err := cmd.Run()
if err != nil {
return fmt.Errorf("wasm-opt failed: %w", err)
}
}
// // Run wasm-opt for wasm binaries
// if arch := strings.Split(config.Triple(), "-")[0]; arch == "wasm32" {
// optLevel, _, _ := config.OptLevel()
// opt := "-" + optLevel

// var args []string

// if config.Scheduler() == "asyncify" {
// args = append(args, "--asyncify")
// }

// if config.Target.Triple == "wasm32-unknown-polkawasm" {
// 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)
// }
// }

// Print code size if requested.
if config.Options.PrintSizes == "short" || config.Options.PrintSizes == "full" {
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
14 changes: 4 additions & 10 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 @@ -105,19 +105,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 c21153f

Please sign in to comment.