Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
58 changes: 58 additions & 0 deletions ir/optimizer/callgraph.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package optimizer

import (
"github.com/quasilyte/go-jdk/ir"
"github.com/quasilyte/go-jdk/symbol"
)

func contains(a []symbol.ID, s symbol.ID) bool {
for _, el := range a {
if el == s {
return true
}
}
return false
}

type CallGraph struct {
graph map[symbol.ID][]symbol.ID
accessableNodes []symbol.ID
}

func (g *CallGraph) Nodes() []symbol.ID {
return g.accessableNodes
}

func (g *CallGraph) Neighbours(node symbol.ID) []symbol.ID {
return g.graph[node]
}

func BuildCallGraph(pkg *ir.Package) *CallGraph {
g := CallGraph{map[symbol.ID][]symbol.ID{}, []symbol.ID{}}
for _, c := range pkg.Classes {
g.walkClass(&c)
}
return &g
}

func (g *CallGraph) walkClass(c *ir.Class) {
for _, m := range c.Methods {
g.accessableNodes = append(g.accessableNodes, m.Out.ID)
}
for _, m := range c.Methods {
g.walkMethod(&m)
}
}

func (g *CallGraph) walkMethod(m *ir.Method) {
neighbours := []symbol.ID{}
for _, inst := range m.Code {
if inst.Kind == ir.InstCallStatic {
callee := inst.Args[0].SymbolID()
if !contains(neighbours, callee) && contains(g.accessableNodes, callee) {
neighbours = append(neighbours, callee)
}
}
}
g.graph[m.Out.ID] = neighbours
}
104 changes: 104 additions & 0 deletions ir/optimizer/inliner.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,104 @@
package optimizer

import (
"github.com/quasilyte/go-jdk/ir"
"github.com/quasilyte/go-jdk/symbol"
)

type inliner struct {
pkg *ir.Package
callGraph *CallGraph
candidats []symbol.ID
}

func RunInliner(pkg *ir.Package, g *CallGraph) {
inliner := inliner{pkg, g, []symbol.ID{}}
inliner.findInlineCandidats()
for _, c := range pkg.Classes {
for _, m := range c.Methods {
inliner.tryInline(&m)
}
}
}

func (inl *inliner) findMethod(id symbol.ID) *ir.Method {
return &inl.pkg.Classes[id.ClassIndex()].Methods[id.MemberIndex()]
}

func (inl *inliner) findRecursion(visited map[symbol.ID]bool, cur symbol.ID, original symbol.ID) bool {
visited[cur] = true
if cur == original {
return true
}
for _, n := range inl.callGraph.Neighbours(cur) {
if visited[n] {
continue
}
if inl.findRecursion(visited, n, original) {
return true
}
}
return false
}

func (inl *inliner) hasCyclicDependency(s symbol.ID) bool {
visited := map[symbol.ID]bool{}
for _, n := range inl.callGraph.Neighbours(s) {
if visited[n] {
return true
}
if inl.findRecursion(visited, n, s) {
return true
}
}
return false
}

func (inl *inliner) tryInline(m *ir.Method) {
for ind, inst := range m.Code {
if inst.Kind == ir.InstCallStatic {
callee := inst.Args[0]
if contains(inl.candidats, callee.SymbolID()) {
inl.inline(m, inl.findMethod(callee.SymbolID()), ind)
}
}
}
}

func (inl *inliner) inline(m, m2 *ir.Method, ind int) {
insts2 := make([]ir.Inst, len(m2.Code))
copy(insts2, m2.Code)
for i := range insts2 {
if insts2[i].Kind == ir.InstRet {
insts2[i] = ir.Inst{
Kind: ir.InstJump,
Args: []ir.Arg{
ir.Arg{
Kind: ir.ArgBranch,
Value: int64(ind),
},
},
}
}
}
m.Code = append(m.Code[:ind], append(insts2, m.Code[ind+1:]...)...)

for _, inst := range m.Code {
if inst.Kind == ir.InstJump {
if inst.Args[0].Value >= int64(ind) {
inst.Args[0].Value += int64(len(insts2))
}
}
}
inl.tryInline(m)
}

func (inl *inliner) findInlineCandidats() {
for _, c := range inl.pkg.Classes {
for _, m := range c.Methods {
if !inl.hasCyclicDependency(m.Out.ID) {
inl.candidats = append(inl.candidats, m.Out.ID)
}
}
}
}
24 changes: 24 additions & 0 deletions ir/optimizer/testpass.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package optimizer

import (
"fmt"

"github.com/quasilyte/go-jdk/ir"
"github.com/quasilyte/go-jdk/symbol"
)

type testPass struct {
pkg *ir.Package
callGraph *CallGraph
}

func ExecuteTestPass(pkg *ir.Package, g *CallGraph) {
pass := testPass{pkg, g}
for _, node := range pass.callGraph.Nodes() {
pass.printNeighbours(node)
}
}

func (p *testPass) printNeighbours(id symbol.ID) {
fmt.Printf("%v\n", p.callGraph.Neighbours(id))
}
4 changes: 4 additions & 0 deletions jit/compiler/x64/compiler.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"unsafe"

"github.com/quasilyte/go-jdk/ir"
"github.com/quasilyte/go-jdk/ir/optimizer"
"github.com/quasilyte/go-jdk/jclass"
"github.com/quasilyte/go-jdk/jit"
"github.com/quasilyte/go-jdk/jit/x64"
Expand Down Expand Up @@ -63,6 +64,9 @@ func (cl *Compiler) link() {
}

func (cl *Compiler) compilePackage(p *ir.Package) error {
callGraph := optimizer.BuildCallGraph(p)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please don't add optimizer code into compiler.
It should work like this:

  • irgen generates the IR
  • optimizer optimizes IR
  • compiler generates the code for the IR


optimizer.RunInliner(p, callGraph)
for i := range p.Classes {
cl.classID = uint64(i)
c := &p.Classes[i]
Expand Down