-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] Start implementing inlining #41
Open
fexolm
wants to merge
1
commit into
master
Choose a base branch
from
fexolm/optimizator
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
start implementing inlining
- Loading branch information
commit dd85280a66b56255cc193077f0de111cfc8b90a8
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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)) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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: