Motivation for Loop Unrolling
The iteration utilities in Base and Base.Iterators perform well for most uses, but low-level optimization often runs into type instabilities in unexpected places. Here is a simple example:
julia> Test.@inferred map(one, Tuple(1:31));julia> Test.@inferred map(one, Tuple(1:32));ERROR: return type NTuple{32, Int64} does not match inferred return type Tuple
This type instability is present in all maps over iterators with lengths greater than 31, regardless of whether they are statically sized. As with most type instabilities in Julia, this leads to memory allocations every time map is called with sufficiently long iterators.
Test.@inferred checks whether the return type of a call is stable. The generated LLVM code shows how different the two calls above are:
julia> @code_llvm debuginfo=:none map(one, Tuple(1:31)); Function Signature: map(typeof(Base.one), NTuple{31, Int64}) define void @julia_map_190811(ptr noalias nocapture noundef nonnull sret([31 x i64]) align 8 dereferenceable(248) %sret_return, ptr nocapture noundef nonnull readonly align 8 dereferenceable(248) %"t::Tuple") local_unnamed_addr #0 { top: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(248) %sret_return, ptr noundef nonnull align 8 dereferenceable(248) @"_j_const#1", i64 248, i1 false) ret void }
julia> @code_llvm debuginfo=:none map(one, Tuple(1:32))
julia> @code_llvm debuginfo=:none map(one, Tuple(1:32)); Function Signature: map(typeof(Base.one), NTuple{32, Int64})
define nonnull ptr @julia_map_190818(ptr nocapture noundef nonnull readonly align 8 dereferenceable(256) %"t::Tuple") local_unnamed_addr #0 {
top:
%jlcallframe1 = alloca [3 x ptr], align 8
%gcframe2 = alloca [3 x ptr], align 16
call void @llvm.memset.p0.i64(ptr align 16 %gcframe2, i8 0, i64 24, i1 true)
%thread_ptr = call ptr asm "movq %fs:0, $0", "=r"() #12
%tls_ppgcstack = getelementptr inbounds i8, ptr %thread_ptr, i64 -8
%tls_pgcstack = load ptr, ptr %tls_ppgcstack, align 8
store i64 4, ptr %gcframe2, align 8
%frame.prev = getelementptr inbounds nuw i8, ptr %gcframe2, i64 8
%task.gcstack = load ptr, ptr %tls_pgcstack, align 8
store ptr %task.gcstack, ptr %frame.prev, align 8
store ptr %gcframe2, ptr %tls_pgcstack, align 8
%ptls_field = getelementptr inbounds nuw i8, ptr %tls_pgcstack, i64 16
%ptls_load = load ptr, ptr %ptls_field, align 8
%"Memory{Any}[]" = call noalias nonnull align 8 dereferenceable(288) ptr @ijl_gc_small_alloc(ptr %ptls_load, i32 960, i32 288, i64 139366302304400) #8
%"Memory{Any}[].tag_addr" = getelementptr inbounds i8, ptr %"Memory{Any}[]", i64 -8
store atomic i64 139366302304400, ptr %"Memory{Any}[].tag_addr" unordered, align 8
%0 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 16
call void @llvm.memset.p0.i64(ptr noundef nonnull align 8 dereferenceable(256) %0, i8 0, i64 256, i1 false)
%memory_ptr = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 8
store ptr %0, ptr %memory_ptr, align 8
store i64 32, ptr %"Memory{Any}[]", align 8
%gc_slot_addr_0 = getelementptr inbounds nuw i8, ptr %gcframe2, i64 16
store ptr %"Memory{Any}[]", ptr %gc_slot_addr_0, align 8
%ptls_load25 = load ptr, ptr %ptls_field, align 8
%"new::Array" = call noalias nonnull align 8 dereferenceable(32) ptr @ijl_gc_small_alloc(ptr %ptls_load25, i32 408, i32 32, i64 139366375671088) #8
%"new::Array.tag_addr" = getelementptr inbounds i8, ptr %"new::Array", i64 -8
store atomic i64 139366375671088, ptr %"new::Array.tag_addr" unordered, align 8
store ptr %0, ptr %"new::Array", align 8
%.repack10 = getelementptr inbounds nuw i8, ptr %"new::Array", i64 8
store ptr %"Memory{Any}[]", ptr %.repack10, align 8
%"new::Array.size_ptr" = getelementptr inbounds nuw i8, ptr %"new::Array", i64 16
store i64 32, ptr %"new::Array.size_ptr", align 8
store atomic ptr @"jl_global#190823.jit", ptr %0 release, align 8
%gep.1 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 24
store atomic ptr @"jl_global#190823.jit", ptr %gep.1 release, align 8
%gep.2 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 32
store atomic ptr @"jl_global#190823.jit", ptr %gep.2 release, align 8
%gep.3 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 40
store atomic ptr @"jl_global#190823.jit", ptr %gep.3 release, align 8
%gep.4 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 48
store atomic ptr @"jl_global#190823.jit", ptr %gep.4 release, align 8
%gep.5 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 56
store atomic ptr @"jl_global#190823.jit", ptr %gep.5 release, align 8
%gep.6 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 64
store atomic ptr @"jl_global#190823.jit", ptr %gep.6 release, align 8
%gep.7 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 72
store atomic ptr @"jl_global#190823.jit", ptr %gep.7 release, align 8
%gep.8 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 80
store atomic ptr @"jl_global#190823.jit", ptr %gep.8 release, align 8
%gep.9 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 88
store atomic ptr @"jl_global#190823.jit", ptr %gep.9 release, align 8
%gep.10 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 96
store atomic ptr @"jl_global#190823.jit", ptr %gep.10 release, align 8
%gep.11 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 104
store atomic ptr @"jl_global#190823.jit", ptr %gep.11 release, align 8
%gep.12 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 112
store atomic ptr @"jl_global#190823.jit", ptr %gep.12 release, align 8
%gep.13 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 120
store atomic ptr @"jl_global#190823.jit", ptr %gep.13 release, align 8
%gep.14 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 128
store atomic ptr @"jl_global#190823.jit", ptr %gep.14 release, align 8
%gep.15 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 136
store atomic ptr @"jl_global#190823.jit", ptr %gep.15 release, align 8
%gep.16 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 144
store atomic ptr @"jl_global#190823.jit", ptr %gep.16 release, align 8
%gep.17 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 152
store atomic ptr @"jl_global#190823.jit", ptr %gep.17 release, align 8
%gep.18 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 160
store atomic ptr @"jl_global#190823.jit", ptr %gep.18 release, align 8
%gep.19 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 168
store atomic ptr @"jl_global#190823.jit", ptr %gep.19 release, align 8
%gep.20 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 176
store atomic ptr @"jl_global#190823.jit", ptr %gep.20 release, align 8
%gep.21 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 184
store atomic ptr @"jl_global#190823.jit", ptr %gep.21 release, align 8
%gep.22 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 192
store atomic ptr @"jl_global#190823.jit", ptr %gep.22 release, align 8
%gep.23 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 200
store atomic ptr @"jl_global#190823.jit", ptr %gep.23 release, align 8
%gep.24 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 208
store atomic ptr @"jl_global#190823.jit", ptr %gep.24 release, align 8
%gep.25 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 216
store atomic ptr @"jl_global#190823.jit", ptr %gep.25 release, align 8
%gep.26 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 224
store atomic ptr @"jl_global#190823.jit", ptr %gep.26 release, align 8
%gep.27 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 232
store atomic ptr @"jl_global#190823.jit", ptr %gep.27 release, align 8
%gep.28 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 240
store atomic ptr @"jl_global#190823.jit", ptr %gep.28 release, align 8
%gep.29 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 248
store atomic ptr @"jl_global#190823.jit", ptr %gep.29 release, align 8
%gep.30 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 256
store atomic ptr @"jl_global#190823.jit", ptr %gep.30 release, align 8
%gep.31 = getelementptr inbounds nuw i8, ptr %"Memory{Any}[]", i64 264
store atomic ptr @"jl_global#190823.jit", ptr %gep.31 release, align 8
store ptr %"new::Array", ptr %gc_slot_addr_0, align 8
store ptr @"jl_global#190824.jit", ptr %jlcallframe1, align 8
%1 = getelementptr inbounds nuw i8, ptr %jlcallframe1, i64 8
store ptr @"jl_global#190825.jit", ptr %1, align 8
%2 = getelementptr inbounds nuw i8, ptr %jlcallframe1, i64 16
store ptr %"new::Array", ptr %2, align 8
%jl_f__apply_iterate_ret = call nonnull ptr @jl_f__apply_iterate(ptr null, ptr nonnull %jlcallframe1, i32 3)
%frame.prev28 = load ptr, ptr %frame.prev, align 8
store ptr %frame.prev28, ptr %tls_pgcstack, align 8
ret ptr %jl_f__apply_iterate_ret
}The type instability (and all of the resulting LLVM code complexity) in the second function call can be eliminated by replacing map with unrolled_map:
julia> Test.@inferred unrolled_map(one, Tuple(1:32));julia> @code_llvm debuginfo=:none unrolled_map(one, Tuple(1:32)); Function Signature: unrolled_map(typeof(Base.one), NTuple{32, Int64}) define void @julia_unrolled_map_190827(ptr noalias nocapture noundef nonnull sret([32 x i64]) align 8 dereferenceable(256) %sret_return, ptr nocapture noundef nonnull readonly align 8 dereferenceable(256) %"itr::Tuple") local_unnamed_addr #0 { top: call void @llvm.memcpy.p0.p0.i64(ptr noundef nonnull align 8 dereferenceable(256) %sret_return, ptr noundef nonnull align 8 dereferenceable(256) @"_j_const#1", i64 256, i1 false) ret void }
The minimum iterator length for type instability is not always 32; for instance, it can also be 14:
julia> first_11(itr) = itr[1:11]first_11 (generic function with 1 method)julia> Test.@inferred first_11(Tuple(1:13));julia> Test.@inferred first_11(Tuple(1:14));ERROR: return type NTuple{11, Int64} does not match inferred return type Tuple{Vararg{Int64}}
Why is the function definition needed in this example?
On the first line of the example above, [1:11] is enclosed in a function so that it does not get evaluated in global scope. This turns the range 1:11 into a Core.Const, which the compiler can propagate into the call to getindex in order to infer the length of the result:
julia> @code_warntype first_11(Tuple(1:13))MethodInstance for Main.first_11(::NTuple{13, Int64}) from first_11(itr) @ Main string:2 Arguments #self#::Core.Const(Main.first_11) itr::NTuple{13, Int64} Body::NTuple{11, Int64} 1 ─ %1 = Main.:(:)::Core.Const(Colon()) │ %2 = (%1)(1, 11)::Core.Const(1:11) │ %3 = Base.getindex(itr, %2)::NTuple{11, Int64} └── return %3
In contrast, running Test.@inferred Tuple(1:13)[1:11] would amount to checking whether the compiler can compute the result type of getindex given only the argument types NTuple{13, Int64} and UnitRange{Int64}, which it cannot do:
julia> @code_warntype Tuple(1:13)[1:11]
julia> @code_warntype Tuple(1:13)[1:11]MethodInstance for getindex(::NTuple{13, Int64}, ::UnitRange{Int64})
from getindex(t::Tuple, r::AbstractUnitRange) @ Base range.jl:439
Arguments
#self#::Core.Const(getindex)
t::NTuple{13, Int64}
r::UnitRange{Int64}
Locals
#32::Base.var"#getindex##0#getindex##1"{UnitRange{Int64}}
@_5::UNION{NOTHING, TUPLE{INT64, INT64}}
@_6::Int64
ri::Int64
Body::TUPLE
1 ── nothing
│ nothing
│ Core.NewvarNode(:(#32))
│ %4 = Base.require_one_based_indexing::Core.Const(Base.require_one_based_indexing)
│ (%4)(r)
│ %6 = Base.:<=::Core.Const(<=)
│ %7 = Base.length::Core.Const(length)
│ %8 = (%7)(r)::Int64
│ %9 = (%6)(%8, 10)::Bool
└─── goto #4 if not %9
2 ── %11 = Base.ntuple::Core.Const(ntuple)
│ %12 = Base.:(var"#getindex##0#getindex##1")::Core.Const(Base.var"#getindex##0#getindex##1")
│ %13 = Core._typeof_captured_variable(r)::Core.Const(UnitRange{Int64})
│ %14 = Core.apply_type(%12, %13)::Core.Const(Base.var"#getindex##0#getindex##1"{UnitRange{Int64}})
│ (#32 = %new(%14, t, r))
│ %16 = #32::Core.PartialStruct(Base.var"#getindex##0#getindex##1"{UnitRange{Int64}}, Union{Nothing, Bool}[0, 0], Any[NTuple{13, Int64}, UnitRange{Int64}])
│ %17 = Base.length::Core.Const(length)
│ %18 = (%17)(r)::Int64
│ %19 = (%11)(%16, %18)::TUPLE
└─── return %19
3 ── Core.Const(:(goto %93))
4 ┄─ %22 = Base.:(==)::Core.Const(==)
│ %23 = Base.first::Core.Const(first)
│ %24 = (%23)(r)::Int64
│ %25 = (%22)(%24, 1)::Bool
└─── goto #15 if not %25
5 ── %27 = Base.:(==)::Core.Const(==)
│ %28 = Base.last::Core.Const(last)
│ %29 = (%28)(r)::Int64
│ %30 = Base.length::Core.Const(length)
│ %31 = (%30)(t)::Core.Const(13)
│ %32 = (%27)(%29, %31)::Bool
└─── goto #8 if not %32
6 ── %34 = t::NTuple{13, Int64}
└─── return %34
7 ── Core.Const(:(goto %37))
8 ┄─ %37 = Base.:(==)::Core.Const(==)
│ %38 = Base.last::Core.Const(last)
│ %39 = (%38)(r)::Int64
│ %40 = Base.:-::Core.Const(-)
│ %41 = Base.length::Core.Const(length)
│ %42 = (%41)(t)::Core.Const(13)
│ %43 = (%40)(%42, 1)::Core.Const(12)
│ %44 = (%37)(%39, %43)::Bool
└─── goto #11 if not %44
9 ── %46 = Base.front::Core.Const(Base.front)
│ %47 = (%46)(t)::NTuple{12, Int64}
└─── return %47
10 ─ Core.Const(:(goto %50))
11 ┄ %50 = Base.:(==)::Core.Const(==)
│ %51 = Base.last::Core.Const(last)
│ %52 = (%51)(r)::Int64
│ %53 = Base.:-::Core.Const(-)
│ %54 = Base.length::Core.Const(length)
│ %55 = (%54)(t)::Core.Const(13)
│ %56 = (%53)(%55, 2)::Core.Const(11)
│ %57 = (%50)(%52, %56)::Bool
└─── goto #14 if not %57
12 ─ %59 = Base.front::Core.Const(Base.front)
│ %60 = Base.front::Core.Const(Base.front)
│ %61 = (%60)(t)::NTuple{12, Int64}
│ %62 = (%59)(%61)::NTuple{11, Int64}
└─── return %62
13 ─ Core.Const(:(goto %65))
14 ┄ goto #22
15 ─ %66 = Base.:(==)::Core.Const(==)
│ %67 = Base.last::Core.Const(last)
│ %68 = (%67)(r)::Int64
│ %69 = Base.length::Core.Const(length)
│ %70 = (%69)(t)::Core.Const(13)
│ %71 = (%66)(%68, %70)::Bool
└─── goto #22 if not %71
16 ─ %73 = Base.:(==)::Core.Const(==)
│ %74 = Base.first::Core.Const(first)
│ %75 = (%74)(r)::Int64
│ %76 = (%73)(%75, 2)::Bool
└─── goto #19 if not %76
17 ─ %78 = Base.tail::Core.Const(Base.tail)
│ %79 = (%78)(t)::NTuple{12, Int64}
└─── return %79
18 ─ Core.Const(:(goto %82))
19 ┄ %82 = Base.:(==)::Core.Const(==)
│ %83 = Base.first::Core.Const(first)
│ %84 = (%83)(r)::Int64
│ %85 = (%82)(%84, 3)::Bool
└─── goto #22 if not %85
20 ─ %87 = Base.tail::Core.Const(Base.tail)
│ %88 = Base.tail::Core.Const(Base.tail)
│ %89 = (%88)(t)::NTuple{12, Int64}
│ %90 = (%87)(%89)::NTuple{11, Int64}
└─── return %90
21 ─ Core.Const(:(goto %93))
22 ┄ %93 = r::UnitRange{Int64}
│ %94 = Base.IteratorSize(%93)::Core.Const(Base.HasShape{1}())
│ %95 = (%94 isa Base.SizeUnknown)::Core.Const(false)
│ %96 = Base.eltype::Core.Const(eltype)
│ %97 = (%96)(t)::Core.Const(Int64)
│ %98 = Base._array_for(%97, %93, %94)::Vector{Int64}
│ %99 = Base.LinearIndices(%98)::LinearIndices{1, Tuple{Base.OneTo{Int64}}}
│ (@_6 = Base.first(%99))
│ (@_5 = Base.iterate(%93))
│ %102 = @_5::UNION{NOTHING, TUPLE{INT64, INT64}}
│ %103 = (%102 === nothing)::Bool
│ %104 = Base.not_int(%103)::Bool
└─── goto #27 if not %104
23 ┄ %106 = @_5::Tuple{Int64, Int64}
│ (ri = Core.getfield(%106, 1))
│ %108 = Core.getfield(%106, 2)::Int64
│ %109 = ri::Int64
│ %110 = Base.getindex(t, %109)::Int64
│ nothing
└─── goto #25 if not %95
24 ─ Core.Const(:(Base.push!(%98, %110)))
└─── Core.Const(:(goto %117))
25 ┄ %115 = @_6::Int64
│ Base.setindex!(%98, %110, %115)
│ nothing
│ %118 = @_6::Int64
│ (@_6 = Base.add_int(%118, 1))
│ (@_5 = Base.iterate(%93, %108))
│ %121 = @_5::UNION{NOTHING, TUPLE{INT64, INT64}}
│ %122 = (%121 === nothing)::Bool
│ %123 = Base.not_int(%122)::Bool
└─── goto #27 if not %123
26 ─ goto #23
27 ┄ %126 = Core._apply_iterate(Base.iterate, Core.tuple, %98)::TUPLE{VARARG{INT64}}
└─── return %126Although itr[1:10] is always inferrable when itr is a Tuple, itr[1:11] has a type instability whenever itr contains more than 13 items. More generally, itr[1:N] seems to be unstable for all N > 10 whenever itr contains more than N + 2 items. This type instability can be fixed by replacing getindex with unrolled_take:
julia> unrolled_first_11(itr) = unrolled_take(itr, Val(11))unrolled_first_11 (generic function with 1 method)julia> Test.@inferred unrolled_first_11(Tuple(1:14));
Even when the final result of a function is inferred, there can be intermediate steps in the function with type instabilities that trigger allocations:
julia> function add_lengths(itr) length_sum = 0 for n in 1:length(itr) length_sum += length(itr[n]) end endadd_lengths (generic function with 1 method)julia> Test.@inferred add_lengths(((1, 2), (1, 2, 3)))julia> @allocated add_lengths(((1, 2), (1, 2, 3)))64julia> @code_warntype add_lengths(((1, 2), (1, 2, 3)))MethodInstance for Main.add_lengths(::Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64, Int64}}) from add_lengths(itr) @ Main REPL[1]:1 Arguments #self#::Core.Const(Main.add_lengths) itr::Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64, Int64}} Locals @_3::UNION{NOTHING, TUPLE{INT64, INT64}} length_sum::Int64 n::Int64 Body::Nothing 1 ─ (length_sum = 0) │ %2 = Main.:(:)::Core.Const(Colon()) │ %3 = Main.length::Core.Const(length) │ %4 = (%3)(itr)::Core.Const(2) │ %5 = (%2)(1, %4)::Core.Const(1:2) │ (@_3 = Base.iterate(%5)) │ %7 = @_3::Core.Const((1, 1)) │ %8 = (%7 === nothing)::Core.Const(false) │ %9 = Base.not_int(%8)::Core.Const(true) └── goto #4 if not %9 2 ┄ %11 = @_3::Tuple{Int64, Int64} │ (n = Core.getfield(%11, 1)) │ %13 = Core.getfield(%11, 2)::Int64 │ %14 = Main.:+::Core.Const(+) │ %15 = length_sum::Int64 │ %16 = Main.length::Core.Const(length) │ %17 = n::Int64 │ %18 = Base.getindex(itr, %17)::UNION{TUPLE{INT64, INT64}, TUPLE{INT64, INT64, INT64}} │ %19 = (%16)(%18)::Int64 │ (length_sum = (%14)(%15, %19)) │ (@_3 = Base.iterate(%5, %13)) │ %22 = @_3::UNION{NOTHING, TUPLE{INT64, INT64}} │ %23 = (%22 === nothing)::Bool │ %24 = Base.not_int(%23)::Bool └── goto #4 if not %24 3 ─ goto #2 4 ┄ return nothing
The key detail in the output of @code_warntype is that the call to getindex is not inferred, because it can return either a Tuple of length 2 or a Tuple of length 3. This type instability can be fixed by replacing getindex with unrolled_applyat:
julia> function unrolled_add_lengths(itr) length_sum = 0 for n in 1:length(itr) length_sum += unrolled_applyat(length, n, itr) end endunrolled_add_lengths (generic function with 1 method)julia> @allocated unrolled_add_lengths(((1, 2), (1, 2, 3)))0julia> @code_warntype unrolled_add_lengths(((1, 2), (1, 2, 3)))MethodInstance for Main.unrolled_add_lengths(::Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64, Int64}}) from unrolled_add_lengths(itr) @ Main REPL[1]:1 Arguments #self#::Core.Const(Main.unrolled_add_lengths) itr::Tuple{Tuple{Int64, Int64}, Tuple{Int64, Int64, Int64}} Locals @_3::UNION{NOTHING, TUPLE{INT64, INT64}} length_sum::Int64 n::Int64 Body::Nothing 1 ─ (length_sum = 0) │ %2 = Main.:(:)::Core.Const(Colon()) │ %3 = Main.length::Core.Const(length) │ %4 = (%3)(itr)::Core.Const(2) │ %5 = (%2)(1, %4)::Core.Const(1:2) │ (@_3 = Base.iterate(%5)) │ %7 = @_3::Core.Const((1, 1)) │ %8 = (%7 === nothing)::Core.Const(false) │ %9 = Base.not_int(%8)::Core.Const(true) └── goto #4 if not %9 2 ┄ %11 = @_3::Tuple{Int64, Int64} │ (n = Core.getfield(%11, 1)) │ %13 = Core.getfield(%11, 2)::Int64 │ %14 = Main.:+::Core.Const(+) │ %15 = length_sum::Int64 │ %16 = Main.unrolled_applyat::Core.Const(UnrolledUtilities.unrolled_applyat) │ %17 = Main.length::Core.Const(length) │ %18 = n::Int64 │ %19 = (%16)(%17, %18, itr)::Int64 │ (length_sum = (%14)(%15, %19)) │ (@_3 = Base.iterate(%5, %13)) │ %22 = @_3::UNION{NOTHING, TUPLE{INT64, INT64}} │ %23 = (%22 === nothing)::Bool │ %24 = Base.not_int(%23)::Bool └── goto #4 if not %24 3 ─ goto #2 4 ┄ return nothing
For a detailed breakdown of when the tools provided by this package can improve performance, see the User Guide.
What Does Loop Unrolling Do
When a loop over N indices is unrolled, it gets compiled into N lines of LLVM code, where each line has a constant (Core.Const) index. For example, an unrolled loop that prints every integer from 1 to 33 is compiled into the following:
julia> @code_llvm debuginfo=:none unrolled_foreach(println, Tuple(1:33))
julia> @code_llvm debuginfo=:none unrolled_foreach(println, Tuple(1:33)); Function Signature: unrolled_foreach(typeof(Base.println), NTuple{33, Int64})
define void @julia_unrolled_foreach_192668(ptr nocapture noundef nonnull readonly align 8 dereferenceable(264) %"itr::Tuple") local_unnamed_addr #0 {
top:
%"itr::Tuple.unbox" = load i64, ptr %"itr::Tuple", align 8
call void @j_println_192670(i64 signext %"itr::Tuple.unbox")
%"itr::Tuple[2]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 8
%"itr::Tuple[2]_ptr.unbox" = load i64, ptr %"itr::Tuple[2]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[2]_ptr.unbox")
%"itr::Tuple[3]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 16
%"itr::Tuple[3]_ptr.unbox" = load i64, ptr %"itr::Tuple[3]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[3]_ptr.unbox")
%"itr::Tuple[4]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 24
%"itr::Tuple[4]_ptr.unbox" = load i64, ptr %"itr::Tuple[4]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[4]_ptr.unbox")
%"itr::Tuple[5]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 32
%"itr::Tuple[5]_ptr.unbox" = load i64, ptr %"itr::Tuple[5]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[5]_ptr.unbox")
%"itr::Tuple[6]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 40
%"itr::Tuple[6]_ptr.unbox" = load i64, ptr %"itr::Tuple[6]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[6]_ptr.unbox")
%"itr::Tuple[7]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 48
%"itr::Tuple[7]_ptr.unbox" = load i64, ptr %"itr::Tuple[7]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[7]_ptr.unbox")
%"itr::Tuple[8]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 56
%"itr::Tuple[8]_ptr.unbox" = load i64, ptr %"itr::Tuple[8]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[8]_ptr.unbox")
%"itr::Tuple[9]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 64
%"itr::Tuple[9]_ptr.unbox" = load i64, ptr %"itr::Tuple[9]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[9]_ptr.unbox")
%"itr::Tuple[10]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 72
%"itr::Tuple[10]_ptr.unbox" = load i64, ptr %"itr::Tuple[10]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[10]_ptr.unbox")
%"itr::Tuple[11]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 80
%"itr::Tuple[11]_ptr.unbox" = load i64, ptr %"itr::Tuple[11]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[11]_ptr.unbox")
%"itr::Tuple[12]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 88
%"itr::Tuple[12]_ptr.unbox" = load i64, ptr %"itr::Tuple[12]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[12]_ptr.unbox")
%"itr::Tuple[13]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 96
%"itr::Tuple[13]_ptr.unbox" = load i64, ptr %"itr::Tuple[13]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[13]_ptr.unbox")
%"itr::Tuple[14]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 104
%"itr::Tuple[14]_ptr.unbox" = load i64, ptr %"itr::Tuple[14]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[14]_ptr.unbox")
%"itr::Tuple[15]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 112
%"itr::Tuple[15]_ptr.unbox" = load i64, ptr %"itr::Tuple[15]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[15]_ptr.unbox")
%"itr::Tuple[16]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 120
%"itr::Tuple[16]_ptr.unbox" = load i64, ptr %"itr::Tuple[16]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[16]_ptr.unbox")
%"itr::Tuple[17]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 128
%"itr::Tuple[17]_ptr.unbox" = load i64, ptr %"itr::Tuple[17]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[17]_ptr.unbox")
%"itr::Tuple[18]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 136
%"itr::Tuple[18]_ptr.unbox" = load i64, ptr %"itr::Tuple[18]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[18]_ptr.unbox")
%"itr::Tuple[19]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 144
%"itr::Tuple[19]_ptr.unbox" = load i64, ptr %"itr::Tuple[19]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[19]_ptr.unbox")
%"itr::Tuple[20]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 152
%"itr::Tuple[20]_ptr.unbox" = load i64, ptr %"itr::Tuple[20]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[20]_ptr.unbox")
%"itr::Tuple[21]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 160
%"itr::Tuple[21]_ptr.unbox" = load i64, ptr %"itr::Tuple[21]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[21]_ptr.unbox")
%"itr::Tuple[22]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 168
%"itr::Tuple[22]_ptr.unbox" = load i64, ptr %"itr::Tuple[22]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[22]_ptr.unbox")
%"itr::Tuple[23]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 176
%"itr::Tuple[23]_ptr.unbox" = load i64, ptr %"itr::Tuple[23]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[23]_ptr.unbox")
%"itr::Tuple[24]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 184
%"itr::Tuple[24]_ptr.unbox" = load i64, ptr %"itr::Tuple[24]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[24]_ptr.unbox")
%"itr::Tuple[25]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 192
%"itr::Tuple[25]_ptr.unbox" = load i64, ptr %"itr::Tuple[25]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[25]_ptr.unbox")
%"itr::Tuple[26]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 200
%"itr::Tuple[26]_ptr.unbox" = load i64, ptr %"itr::Tuple[26]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[26]_ptr.unbox")
%"itr::Tuple[27]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 208
%"itr::Tuple[27]_ptr.unbox" = load i64, ptr %"itr::Tuple[27]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[27]_ptr.unbox")
%"itr::Tuple[28]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 216
%"itr::Tuple[28]_ptr.unbox" = load i64, ptr %"itr::Tuple[28]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[28]_ptr.unbox")
%"itr::Tuple[29]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 224
%"itr::Tuple[29]_ptr.unbox" = load i64, ptr %"itr::Tuple[29]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[29]_ptr.unbox")
%"itr::Tuple[30]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 232
%"itr::Tuple[30]_ptr.unbox" = load i64, ptr %"itr::Tuple[30]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[30]_ptr.unbox")
%"itr::Tuple[31]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 240
%"itr::Tuple[31]_ptr.unbox" = load i64, ptr %"itr::Tuple[31]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[31]_ptr.unbox")
%"itr::Tuple[32]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 248
%"itr::Tuple[32]_ptr.unbox" = load i64, ptr %"itr::Tuple[32]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[32]_ptr.unbox")
%"itr::Tuple[33]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 256
%"itr::Tuple[33]_ptr.unbox" = load i64, ptr %"itr::Tuple[33]_ptr", align 8
call void @j_println_192670(i64 signext %"itr::Tuple[33]_ptr.unbox")
ret void
}This LLVM code consists of 33 getelementptr instructions (each of which extracts a value from a Tuple at a particular index), 33 load instructions, and 33 call instructions (each of which switches execution to println). Every getelementptr instruction has a constant index between 0 and 32; in more complex examples where the call instructions get inlined, this constant index can be propagated into the LLVM code of the function being called. On the other hand, here is the LLVM code for the non-unrolled version of this loop:
julia> @code_llvm debuginfo=:none foreach(println, Tuple(1:33)); Function Signature: foreach(typeof(Base.println), NTuple{33, Int64}) define void @julia_foreach_192808(ptr nocapture noundef nonnull readonly align 8 dereferenceable(264) %"itr::Tuple") local_unnamed_addr #0 { top: %"itr::Tuple.unbox" = load i64, ptr %"itr::Tuple", align 8 call void @j_println_192810(i64 signext %"itr::Tuple.unbox") %"itr::Tuple[2]_ptr" = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 8 %"itr::Tuple[2]_ptr.unbox" = load i64, ptr %"itr::Tuple[2]_ptr", align 8 call void @j_println_192810(i64 signext %"itr::Tuple[2]_ptr.unbox") %0 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 16 %.unbox = load i64, ptr %0, align 8 call void @j_println_192810(i64 signext %.unbox) %1 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 24 %.unbox.1 = load i64, ptr %1, align 8 call void @j_println_192810(i64 signext %.unbox.1) %2 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 32 %.unbox.2 = load i64, ptr %2, align 8 call void @j_println_192810(i64 signext %.unbox.2) %3 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 40 %.unbox.3 = load i64, ptr %3, align 8 call void @j_println_192810(i64 signext %.unbox.3) %4 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 48 %.unbox.4 = load i64, ptr %4, align 8 call void @j_println_192810(i64 signext %.unbox.4) %5 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 56 %.unbox.5 = load i64, ptr %5, align 8 call void @j_println_192810(i64 signext %.unbox.5) %6 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 64 %.unbox.6 = load i64, ptr %6, align 8 call void @j_println_192810(i64 signext %.unbox.6) %7 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 72 %.unbox.7 = load i64, ptr %7, align 8 call void @j_println_192810(i64 signext %.unbox.7) %8 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 80 %.unbox.8 = load i64, ptr %8, align 8 call void @j_println_192810(i64 signext %.unbox.8) %9 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 88 %.unbox.9 = load i64, ptr %9, align 8 call void @j_println_192810(i64 signext %.unbox.9) %10 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 96 %.unbox.10 = load i64, ptr %10, align 8 call void @j_println_192810(i64 signext %.unbox.10) %11 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 104 %.unbox.11 = load i64, ptr %11, align 8 call void @j_println_192810(i64 signext %.unbox.11) %12 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 112 %.unbox.12 = load i64, ptr %12, align 8 call void @j_println_192810(i64 signext %.unbox.12) %13 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 120 %.unbox.13 = load i64, ptr %13, align 8 call void @j_println_192810(i64 signext %.unbox.13) %14 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 128 %.unbox.14 = load i64, ptr %14, align 8 call void @j_println_192810(i64 signext %.unbox.14) %15 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 136 %.unbox.15 = load i64, ptr %15, align 8 call void @j_println_192810(i64 signext %.unbox.15) %16 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 144 %.unbox.16 = load i64, ptr %16, align 8 call void @j_println_192810(i64 signext %.unbox.16) %17 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 152 %.unbox.17 = load i64, ptr %17, align 8 call void @j_println_192810(i64 signext %.unbox.17) %18 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 160 %.unbox.18 = load i64, ptr %18, align 8 call void @j_println_192810(i64 signext %.unbox.18) %19 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 168 %.unbox.19 = load i64, ptr %19, align 8 call void @j_println_192810(i64 signext %.unbox.19) %20 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 176 %.unbox.20 = load i64, ptr %20, align 8 call void @j_println_192810(i64 signext %.unbox.20) %21 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 184 %.unbox.21 = load i64, ptr %21, align 8 call void @j_println_192810(i64 signext %.unbox.21) %22 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 192 %.unbox.22 = load i64, ptr %22, align 8 call void @j_println_192810(i64 signext %.unbox.22) %23 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 200 %.unbox.23 = load i64, ptr %23, align 8 call void @j_println_192810(i64 signext %.unbox.23) %24 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 208 %.unbox.24 = load i64, ptr %24, align 8 call void @j_println_192810(i64 signext %.unbox.24) %25 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 216 %.unbox.25 = load i64, ptr %25, align 8 call void @j_println_192810(i64 signext %.unbox.25) %26 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 224 %.unbox.26 = load i64, ptr %26, align 8 call void @j_println_192810(i64 signext %.unbox.26) %27 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 232 %.unbox.27 = load i64, ptr %27, align 8 call void @j_println_192810(i64 signext %.unbox.27) %28 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 240 %.unbox.28 = load i64, ptr %28, align 8 call void @j_println_192810(i64 signext %.unbox.28) %29 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 248 %.unbox.29 = load i64, ptr %29, align 8 call void @j_println_192810(i64 signext %.unbox.29) %30 = getelementptr inbounds nuw i8, ptr %"itr::Tuple", i64 256 %.unbox.30 = load i64, ptr %30, align 8 call void @j_println_192810(i64 signext %.unbox.30) ret void }
This LLVM code has a load instruction to get the first value and a getelementptr instruction with a non-constant integer index to get all other values. It also has conditional jump instructions for checking whether the last index has been reached after each load and getelementptr instruction.
Downsides of Loop Unrolling
Loop unrolling has a cost: unrolled functions usually take longer to compile than their counterparts in the standard library. Much of the code written with the standard library runs only once or a few times, so compilation dominates its total execution time, and run-time savings make no practical difference there. Replacing standard library functions with unrolled functions in such code can increase total execution time:
julia> tup32 = ntuple(Returns((1, 2)), 32);julia> @elapsed map(first, tup32)0.009298303julia> @elapsed unrolled_map(first, tup32)0.011274917
The increase in compilation time is usually no more than a factor of 5 for small iterators, but it grows as iterator length increases:
julia> tup320 = ntuple(Returns((1, 2)), 320);julia> @elapsed map(first, tup320)0.009169467julia> @elapsed unrolled_map(first, tup320)0.294437667
Loop unrolling can also theoretically increase the run time of a function in addition to its compilation time, since unrolled assembly code requires more space and takes longer to load than non-unrolled code. In practice, though, the constant propagation enabled by unrolling usually compensates for this slowdown.
Unrolled functions are worthwhile when type instabilities and allocations must be removed, as they must be for GPU kernels and for static compilation, and when run time matters more than compilation time. Standard library functions are the better choice when compilation time dominates.
It is usually a good idea to compare the performance of unrolled code against non-unrolled code before settling on a particular design. Many examples of such comparisons can be found in the tables of benchmarks that are automatically generated for this package.