Skip to content
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

cps/xfrm: experiment at inlining next body #130

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
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
47 changes: 39 additions & 8 deletions cps/xfrm.nim
Original file line number Diff line number Diff line change
Expand Up @@ -205,23 +205,54 @@ macro cpsMayJump(cont, n, after: typed): untyped =
var n = normalizingRewrites:
# we always wrap the input because there's no reason not to
newStmtList n
var after = normalizingRewrites:
# we always wrap the input because there's no reason not to
newStmtList after

let
afterProc = makeContProc(genSym(nskProc, "done"), cont, after)
afterTail = tailCall(desym cont, afterProc.name)
proc countToTwo(n: NimNode): int =
## Count the amount of cpsPending in `n`, stopping as soon
## as two or more cpsPending is found
if n.isCpsPending:
inc result
else:
for child in n.items:
result += child.countToTwo
if result >= 2:
return

# whether a jump is needed to leave `n`
let jumpCount = n.countToTwo + int(n.firstReturn.isNil)

result = newStmtList()
var resolvedBody: NimNode

# FIXME: fix this to use makeReturn
if jumpCount > 1:
let
afterProc = makeContProc(genSym(nskProc, "done"), cont, after)
afterTail = tailCall(desym cont, afterProc.name)

# FIXME: fix this to use makeReturn

resolvedBody =
n.replace(isCpsPending):
afterTail

if resolvedBody.firstReturn.isNil:
resolvedBody.add afterTail
if resolvedBody.firstReturn.isNil:
resolvedBody.add afterTail

result.add afterProc
else:
# If no jump is needed, we inline instead
let nextBody = after.resym(cont, desym cont)
if nextBody.firstReturn.isNil:
nextBody.add newCpsPending()

resolvedBody =
n.replace(isCpsPending):
nextBody

result = newStmtList()
result.add afterProc
result.add resolvedBody

result = workaroundRewrites result

#debug("cpsMayJump", result, Transformed, n)
Expand Down