diff options
author | Damian Gryski <[email protected]> | 2024-10-18 08:00:42 -0700 |
---|---|---|
committer | Ron Evans <[email protected]> | 2024-10-19 11:02:54 +0100 |
commit | 40c9c66c1d6d758157fbc8163cc48e8cdecd85b9 (patch) | |
tree | 9d127cb283643e67867ea7ee2e43496a6bc9c289 | |
parent | 01dac8ba8e3efb801f560987bfe11b7882c342ca (diff) | |
download | tinygo-40c9c66c1d6d758157fbc8163cc48e8cdecd85b9.tar.gz tinygo-40c9c66c1d6d758157fbc8163cc48e8cdecd85b9.zip |
compiler: mark stringFromBytes as nocapture/readonly to help escape analysis
Fixes #4525
-rw-r--r-- | compiler/symbol.go | 3 | ||||
-rw-r--r-- | transform/testdata/allocs2.go | 5 |
2 files changed, 8 insertions, 0 deletions
diff --git a/compiler/symbol.go b/compiler/symbol.go index c2007cfd4..5447e0dba 100644 --- a/compiler/symbol.go +++ b/compiler/symbol.go @@ -172,6 +172,9 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value) llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0)) llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0)) llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0)) + case "runtime.stringFromBytes": + llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0)) + llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0)) case "runtime.trackPointer": // This function is necessary for tracking pointers on the stack in a // portable way (see gc_stack_portable.go). Indicate to the optimizer diff --git a/transform/testdata/allocs2.go b/transform/testdata/allocs2.go index 3b08fbc9e..13c460231 100644 --- a/transform/testdata/allocs2.go +++ b/transform/testdata/allocs2.go @@ -49,6 +49,11 @@ func main() { n4 = n5 }() println(n4, n5) + + // This shouldn't escape. + var buf [32]byte + s := string(buf[:]) + println(len(s)) } func derefInt(x *int) int { |