Skip to content

Tail call optimization? #288

Answered by arshajii
mlmiller751 asked this question in Q&A
Discussion options

You must be logged in to vote

Hi @mlmiller751 -- yes, Codon will do this through LLVM, which has tail call optimization as part of it's standard optimization passes.

For example:

def factorial(n: int):
    return 1 if n <= 1 else n*factorial(n - 1)

compiles to:

; Function Attrs: nofree nosync nounwind readnone
define i64 @factorial(i64 %0) local_unnamed_addr #1 personality ptr @seq_personality {
entry:
  %tmp.i5 = icmp sgt i64 %0, 1
  br i1 %tmp.i5, label %ternary.false.preheader, label %ternary.exit

ternary.false.preheader:                          ; preds = %entry
  %1 = add nsw i64 %0, -1
  %min.iters.check = icmp eq i64 %0, 2
  br i1 %min.iters.check, label %ternary.false.preheader17, label %vector.ph

vector.ph:…

Replies: 3 comments

Comment options

You must be logged in to vote
0 replies
Answer selected by mlmiller751
Comment options

You must be logged in to vote
0 replies
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
3 participants