tinygo-org / tinygo

Go compiler for small places. Microcontrollers, WebAssembly (WASM/WASI), and command-line tools. Based on LLVM.
https://tinygo.org
Other
15.49k stars 913 forks source link

support `//go:noescape` #4584

Open dgryski opened 2 weeks ago

dgryski commented 2 weeks ago

So this patch

diff --git a/compiler/symbol.go b/compiler/symbol.go
index 9b9b1d10..5ef6924d 100644
--- a/compiler/symbol.go
+++ b/compiler/symbol.go
@@ -33,6 +33,7 @@ type functionInfo struct {
        exported      bool       // go:export, CGo
        interrupt     bool       // go:interrupt
        nobounds      bool       // go:nobounds
+       noescape      bool       // go:noescape
        variadic      bool       // go:variadic (CGo only)
        inline        inlineType // go:inline
 }
@@ -202,7 +203,7 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)

        // External/exported functions may not retain pointer values.
        // https://golang.org/cmd/cgo/#hdr-Passing_pointers
-       if info.exported {
+       if info.exported || info.noescape {
                if c.archFamily() == "wasm32" && len(fn.Blocks) == 0 {
                        // We need to add the wasm-import-module and the wasm-import-name
                        // attributes.
@@ -394,6 +395,10 @@ func (c *compilerContext) parsePragmas(info *functionInfo, f *ssa.Function) {
                        if hasUnsafeImport(f.Pkg.Pkg) {
                                info.nobounds = true
                        }
+               case "//go:noescape":
+                       //if hasUnsafeImport(f.Pkg.Pkg) {
+                       info.noescape = true
+                       //              }
                case "//go:variadic":
                        // The //go:variadic pragma is emitted by the CGo preprocessing
                        // pass for C variadic functions. This includes both explicit

"should" add support for //go:noescape, but looking through the runtime I feel like I need to add those annotations all through the hashmap code in order to actually tag things as non-escaping. Looking through there still seem to be a lot of places our escape analysis needs help.

(btw, the commented out code in the patch to check for hasUnsafeImport was causing a segfault in the compiler; not sure why..)

aykevl commented 3 days ago

I worked on this previously: https://github.com/tinygo-org/tinygo/pull/3887