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.
This commit is contained in:
Ayke van Laethem
2018-12-22 17:11:39 +01:00
parent 5a15d4162d
commit 18b16fc151
+9
View File
@@ -43,6 +43,15 @@ func (c *Compiler) Optimize(optLevel, sizeLevel int, inlinerThreshold uint) erro
c.OptimizeStringToBytes()
c.OptimizeAllocs()
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 {
// Must be run at any optimization level.
c.LowerInterfaces()