Skip to content

Commit 40c9c66

Browse files
dgryskideadprogram
authored andcommitted
compiler: mark stringFromBytes as nocapture/readonly to help escape analysis
Fixes #4525
1 parent 01dac8b commit 40c9c66

File tree

2 files changed

+8
-0
lines changed

2 files changed

+8
-0
lines changed

compiler/symbol.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,9 @@ func (c *compilerContext) getFunction(fn *ssa.Function) (llvm.Type, llvm.Value)
172172
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
173173
llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0))
174174
llvmFn.AddAttributeAtIndex(2, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
175+
case "runtime.stringFromBytes":
176+
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("nocapture"), 0))
177+
llvmFn.AddAttributeAtIndex(1, c.ctx.CreateEnumAttribute(llvm.AttributeKindID("readonly"), 0))
175178
case "runtime.trackPointer":
176179
// This function is necessary for tracking pointers on the stack in a
177180
// portable way (see gc_stack_portable.go). Indicate to the optimizer

transform/testdata/allocs2.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,11 @@ func main() {
4949
n4 = n5
5050
}()
5151
println(n4, n5)
52+
53+
// This shouldn't escape.
54+
var buf [32]byte
55+
s := string(buf[:])
56+
println(len(s))
5257
}
5358

5459
func derefInt(x *int) int {

0 commit comments

Comments
 (0)