Browse Source

compiler: run some optimizations after interface lowering

By running these interprocedural optimizations after interface lowering,
in particular the heap-to-stack transformation pass, interfaces can be
zero cost in some more cases.

For example, say you have the following interface:

    type Writer interface {
        Write([]byte) (int, error)
    }

and you do something with it:

    func foo(w io.Writer) {
        w.Write([]byte("foo"))
    }

this commit enables escape analysis across interface boundaries, which
means that the Write call does not cause an allocation if all
implementations of io.Writer do not let the slice escape. This enables
broader uses of interfaces, as they are now a zero-cost abstraction in
more cases.
pull/109/head
Ayke van Laethem 6 years ago
parent
commit
18b16fc151
No known key found for this signature in database GPG Key ID: E97FF5335DFDFDED
  1. 9
      compiler/optimizer.go

9
compiler/optimizer.go

@ -43,6 +43,15 @@ func (c *Compiler) Optimize(optLevel, sizeLevel int, inlinerThreshold uint) erro
c.OptimizeStringToBytes() c.OptimizeStringToBytes()
c.OptimizeAllocs() c.OptimizeAllocs()
c.LowerInterfaces() c.LowerInterfaces()
// After interfaces are lowered, there are many more opportunities for
// interprocedural optimizations. To get them to work, function
// attributes have to be updated first.
goPasses.Run(c.mod)
// Run TinyGo-specific interprocedural optimizations.
c.OptimizeAllocs()
c.OptimizeStringToBytes()
} else { } else {
// Must be run at any optimization level. // Must be run at any optimization level.
c.LowerInterfaces() c.LowerInterfaces()

Loading…
Cancel
Save