Skip to content

Commit

Permalink
polkawasm: add custom gc that relies on an external allocator
Browse files Browse the repository at this point in the history
  • Loading branch information
radkomih committed Dec 1, 2023
1 parent dac6696 commit 8670e4a
Show file tree
Hide file tree
Showing 15 changed files with 735 additions and 29 deletions.
26 changes: 18 additions & 8 deletions builder/build.go
Original file line number Diff line number Diff line change
Expand Up @@ -816,15 +816,25 @@ 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...)
Expand Down
4 changes: 3 additions & 1 deletion compileopts/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func (c *Config) GC() string {
// that can be traced by the garbage collector.
func (c *Config) NeedsStackObjects() bool {
switch c.GC() {
case "conservative", "custom", "precise":
case "conservative", "custom", "extalloc", "extalloc_leaking", "precise":
for _, tag := range c.BuildTags() {
if tag == "tinygo.wasm" {
return true
Expand Down 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
4 changes: 2 additions & 2 deletions compileopts/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ import (
)

var (
validGCOptions = []string{"none", "leaking", "conservative", "custom", "precise"}
validGCOptions = []string{"none", "leaking", "conservative", "custom", "extalloc", "extalloc_leaking", "precise"}
validSchedulerOptions = []string{"none", "tasks", "asyncify"}
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 compileopts/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (

func TestVerifyOptions(t *testing.T) {

expectedGCError := errors.New(`invalid gc option 'incorrect': valid values are none, leaking, conservative, custom, precise`)
expectedGCError := errors.New(`invalid gc option 'incorrect': valid values are none, leaking, conservative, custom, extalloc, extalloc_leaking, precise`)
expectedSchedulerError := errors.New(`invalid scheduler option 'incorrect': valid values are none, tasks, asyncify`)
expectedPrintSizeError := errors.New(`invalid size option 'incorrect': valid values are none, short, full`)
expectedPanicStrategyError := errors.New(`invalid panic option 'incorrect': valid values are print, trap`)
Expand Down
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
2 changes: 1 addition & 1 deletion src/runtime/arch_tinygowasm.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ var (
func align(ptr uintptr) uintptr {
// Align to 16, which is the alignment of max_align_t:
// https://godbolt.org/z/dYqTsWrGq
const heapAlign = 16
const heapAlign = 8
return (ptr + heapAlign - 1) &^ (heapAlign - 1)
}

Expand Down
1 change: 0 additions & 1 deletion src/runtime/gc_custom.go
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
//go:build gc.custom
// +build gc.custom

package runtime

Expand Down
47 changes: 47 additions & 0 deletions src/runtime/gc_debug.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
//go:build gc.extalloc || gc.extalloc_leaking

package runtime

const gcDebug = false

func printnum(num int) {
if num == 0 {
printstr("0")
return
}

digits := [16]int{} // store up to 16 digits
digitStrings := [10]string{"0", "1", "2", "3", "4", "5", "6", "7", "8", "9"}
count := 0 // count of digits

// extract digits from the number
for ; num > 0; num /= 10 {
digits[count] = num % 10
count++
}

// reverse the digits
for i := 0; i < count/2; i++ {
j := count - i - 1
digits[i], digits[j] = digits[j], digits[i]
}

// print each digit
for i := 0; i < count; i++ {
printstr(digitStrings[digits[i]])
}
}

func printstr(str string) {
if !gcDebug {
return
}

for i := 0; i < len(str); i++ {
if putcharPosition >= putcharBufferSize {
break
}

putchar(str[i])
}
}
Loading

0 comments on commit 8670e4a

Please sign in to comment.