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

Note that the compiler performs loop unrolling #46001

Merged
merged 2 commits into from
Jul 12, 2022
Merged

Conversation

LilithHafner
Copy link
Member

This makes the @generated example obsolete, though it is still illustrative. I drew this conclusion from copying the doc examples into a 1.8.0-rc1 REPL and running @code_llvm:

julia> function sub2ind_loop(dims::NTuple{N}, I::Integer...) where N
           ind = I[N] - 1
           for i = N-1:-1:1
               ind = I[i]-1 + dims[i]*ind
           end
           return ind + 1
       end
sub2ind_loop (generic function with 1 method)

julia> sub2ind_loop((3, 5), 1, 2)
4

julia> @code_llvm sub2ind_loop((3, 5), 1, 2)
;  @ REPL[19]:1 within `sub2ind_loop`
define i64 @julia_sub2ind_loop_1609([2 x i64]* nocapture nonnull readonly align 8 dereferenceable(16) %0, i64 signext %1, i64 signext %2) #0 {
top:
;  @ REPL[19]:2 within `sub2ind_loop`
; ┌ @ int.jl:86 within `-`
   %3 = add i64 %2, -1
; └
;  @ REPL[19]:4 within `sub2ind_loop`
; ┌ @ tuple.jl:29 within `getindex`
   %4 = getelementptr inbounds [2 x i64], [2 x i64]* %0, i64 0, i64 0
; └
; ┌ @ int.jl:88 within `*`
   %5 = load i64, i64* %4, align 8
   %6 = mul i64 %5, %3
; └
;  @ REPL[19]:6 within `sub2ind_loop`
; ┌ @ int.jl:87 within `+`
   %7 = add i64 %6, %1
; └
  ret i64 %7
}

julia> @generated function sub2ind_gen(dims::NTuple{N}, I::Integer...) where N
           ex = :(I[$N] - 1)
           for i = (N - 1):-1:1
               ex = :(I[$i] - 1 + dims[$i] * $ex)
           end
           return :($ex + 1)
       end
sub2ind_gen (generic function with 1 method)

julia> sub2ind_gen((3, 5), 1, 2)
4

julia> @code_llvm sub2ind_gen((3, 5), 1, 2)
;  @ REPL[22]:1 within `sub2ind_gen`
define i64 @julia_sub2ind_gen_1624([2 x i64]* nocapture nonnull readonly align 8 dereferenceable(16) %0, i64 signext %1, i64 signext %2) #0 {
top:
; ┌ @ REPL[22] within `macro expansion`
; │┌ @ tuple.jl:29 within `getindex`
    %3 = getelementptr inbounds [2 x i64], [2 x i64]* %0, i64 0, i64 0
; │└
; │┌ @ int.jl:86 within `-`
    %4 = add i64 %2, -1
; │└
; │┌ @ int.jl:88 within `*`
    %5 = load i64, i64* %3, align 8
    %6 = mul i64 %5, %4
; │└
; │┌ @ int.jl:87 within `+`
    %7 = add i64 %6, %1
; │└
   ret i64 %7
; └
}

This makes the `@generated` example obsolete, though it is still illustrative.
@kshyatt kshyatt added the docs This change adds or pertains to documentation label Jul 11, 2022
Copy link
Member

@aviatesk aviatesk left a comment

Choose a reason for hiding this comment

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

jfyi, the loop unrolling here is done at Julia-level compilation, and so the optimization is visible on @code_typed. EDIT: I missed to understand that this change corresponds to the fact that now LLVM seems to perform loop unrolling for this example.

@aviatesk aviatesk merged commit 558fbb6 into master Jul 12, 2022
@aviatesk aviatesk deleted the LilithHafner-patch-2 branch July 12, 2022 02:34
ffucci pushed a commit to ffucci/julia that referenced this pull request Aug 11, 2022
This makes the `@generated` example obsolete, though it is still illustrative.
pcjentsch pushed a commit to pcjentsch/julia that referenced this pull request Aug 18, 2022
This makes the `@generated` example obsolete, though it is still illustrative.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
docs This change adds or pertains to documentation
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants