Skip to content

Commit fffb6e5

Browse files
committed
Added some experimental unrolling code. Performance was very poor, so commented it out.
1 parent 3bbfcfb commit fffb6e5

File tree

3 files changed

+57
-24
lines changed

3 files changed

+57
-24
lines changed

src/lowering.jl

Lines changed: 32 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -156,17 +156,17 @@ end
156156
# # tc = terminatecondition(loop, us, n, loopsym, inclmask, 1)
157157
# looprange = if loop.startexact
158158
# if loop.stopexact
159-
# Expr(:(=), loopsym, Expr(:call, :(:), loop.starthint, loop.stophint))
159+
# Expr(:(=), loopsym, Expr(:call, :(:), loop.starthint-1, loop.stophint-1))
160160
# else
161161
# # expectation = expect(Expr(:call, :(>), loop.stopsym, loop.starthint-5))
162-
# Expr(:(=), loopsym, Expr(:call, :(:), loop.starthint, loop.stopsym))
162+
# Expr(:(=), loopsym, Expr(:call, :(:), loop.starthint-1, Expr(:call, lv(:staticm1), loop.stopsym)))
163163
# end
164164
# elseif loop.stopexact
165165
# # expectation = expect(Expr(:call, :(>), loop.stophint+5, loop.startsym))
166-
# Expr(:(=), loopsym, Expr(:call, :(:), loop.startsym, loop.stophint))
166+
# Expr(:(=), loopsym, Expr(:call, :(:), Expr(:call, lv(:staticm1), loop.startsym), loop.stophint - 1))
167167
# else
168168
# # expectation = expect(Expr(:call, :(>), loop.stopsym, Expr(:call, lv(:vsub), loop.startsym, 5)))
169-
# Expr(:(=), loopsym, Expr(:call, :(:), loop.startsym, loop.stopsym))
169+
# Expr(:(=), loopsym, Expr(:call, :(:), Expr(:call, lv(:staticm1), loop.startsym), Expr(:call, lv(:staticm1), loop.stopsym)))
170170
# end
171171
# body = lower_block(ls, us, n, false, 1)
172172
# push!(body.args, Expr(:loopinfo, (Symbol("llvm.loop.unroll.count"), 4)))
@@ -180,9 +180,28 @@ end
180180
# q
181181
# else
182182
# # Expr(:block, assumption, expectation, q)
183+
# q
183184
# Expr(:block, loopiteratesatleastonce(loop), q)
184185
# end
185186
# end
187+
# function lower_unroll_for_throughput(ls::LoopSet, us::UnrollSpecification, n::Int, loop::Loop, loopsym::Symbol)
188+
# sl = startloop(loop, false, loopsym)
189+
# UF = 4
190+
# tcc = terminatecondition(loop, us, n, loopsym, false, 1)
191+
# tcu = terminatecondition(loop, us, n, loopsym, false, UF)
192+
# body = lower_block(ls, us, n, false, 1)
193+
# # loopisstatic = isstaticloop(loop)
194+
# unrolledlabel = gensym(:unrolled)
195+
# cleanuplabel = gensym(:cleanup)
196+
# gotounrolled = Expr(:macrocall, Symbol("@goto"), LineNumberNode(@__LINE__, Symbol(@__FILE__)), unrolledlabel)
197+
# gotocleanup = Expr(:macrocall, Symbol("@goto"), LineNumberNode(@__LINE__, Symbol(@__FILE__)), cleanuplabel)
198+
# branch = Expr(:if, tcu, gotounrolled, gotocleanup)
199+
# unrolled = Expr(:block, Expr(:macrocall, Symbol("@label"), LineNumberNode(@__LINE__, Symbol(@__FILE__)), unrolledlabel))
200+
# foreach(_ -> push!(unrolled.args, body), 1:UF)
201+
# push!(unrolled.args, Expr(:if, tcu, gotounrolled, Expr(:if, tcc, gotocleanup)))
202+
# cleanup = Expr(:block, Expr(:macrocall, Symbol("@label"), LineNumberNode(@__LINE__, Symbol(@__FILE__)), cleanuplabel), body, Expr(:if, tcc, gotocleanup))
203+
# Expr(:let, sl, Expr(:block, branch, unrolled, cleanup))
204+
# end
186205

187206
function assume(ex)
188207
Expr(:call, Expr(:(.), Expr(:(.), :LoopVectorization, QuoteNode(:SIMDPirates)), QuoteNode(:assume)), ex)
@@ -207,24 +226,21 @@ function lower_no_unroll(ls::LoopSet, us::UnrollSpecification, n::Int, inclmask:
207226
nisvectorized = isvectorized(us, n)
208227
loopsym = names(ls)[n]
209228
loop = getloop(ls, loopsym)
210-
# if VERSION ≥ v"1.4" && !nisvectorized && !inclmask && isone(n) && !ls.loadelimination[] && (us.u₁ > 1) && (usorig.u₁ == us.u₁) && (usorig.u₂ == us.u₂) && length(loop) > 7
229+
# if !nisvectorized && !inclmask && isone(n) && !ls.loadelimination[] && (us.u₁ > 1) && (usorig.u₁ == us.u₁) && (usorig.u₂ == us.u₂) && length(loop) > 7
230+
# # return lower_unroll_for_throughput(ls, us, n, loop, loopsym)
211231
# return lower_llvm_unroll(ls, us, n, loop)
212232
# end
213233
sl = startloop(loop, nisvectorized, loopsym)
214234
tc = terminatecondition(loop, us, n, loopsym, inclmask, 1)
215235
body = lower_block(ls, us, n, inclmask, 1)
216-
q = if (usorig.u₁ == us.u₁) && (usorig.u₂ == us.u₂) && !isstaticloop(loop) && !inclmask# && !ls.loadelimination[]
217-
# Expr(:block, sl, assumeloopiteratesatleastonce(loop), Expr(:while, tc, body))
218-
if nisvectorized
219-
Expr(:block, loopiteratesatleastonce(loop, true), Expr(:while, expect(tc), body))
220-
else
221-
# Expr(:block, sl, assume(tc), Expr(:while, tc, body))
222-
push!(body.args, Expr(:||, expect(tc), Expr(:break)))
223-
# Expr(:block, sl, assume(tc), Expr(:while, true, body))
224-
Expr(:block, Expr(:while, true, body))
225-
end
236+
q = if nisvectorized
237+
# Expr(:block, loopiteratesatleastonce(loop, true), Expr(:while, expect(tc), body))
238+
Expr(:block, Expr(:while, expect(tc), body))
226239
else
227-
Expr(:block, Expr(:while, tc, body))
240+
# Expr(:block, sl, assume(tc), Expr(:while, tc, body))
241+
push!(body.args, Expr(:||, expect(tc), Expr(:break)))
242+
# Expr(:block, sl, assume(tc), Expr(:while, true, body))
243+
Expr(:block, Expr(:while, true, body))
228244
end
229245

230246
if nisvectorized

src/reconstruct_loopset.jl

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -124,22 +124,15 @@ function pushvarg′!(ls::LoopSet, ar::ArrayReferenceMeta, i, name)
124124
reverse!(ar.loopedindex); reverse!(getindices(ar)) # reverse the listed indices here, and transpose it to make it column major
125125
pushpreamble!(ls, Expr(:(=), name, Expr(:call, lv(:transpose), extract_varg(i))))
126126
end
127-
function assume_strides!(ls, name, N)
128-
for n 1:N
129-
pushpreamble!(ls, assume( Expr(:call, :>, Expr(:ref, Expr(:(.), name, QuoteNode(:strides)), n), 0) ))
130-
end
131-
end
132127
function add_mref!(
133128
ls::LoopSet, ar::ArrayReferenceMeta, i::Int, ::Type{S}, name = vptr(ar)
134129
) where {T, N, S <: AbstractColumnMajorStridedPointer{T,N}}
135130
pushvarg!(ls, ar, i, name)
136-
assume_strides!(ls, name, N)
137131
end
138132
function add_mref!(
139133
ls::LoopSet, ar::ArrayReferenceMeta, i::Int, ::Type{S}, name = vptr(ar)
140134
) where {T, N, S <: AbstractRowMajorStridedPointer{T, N}}
141135
pushvarg′!(ls, ar, i, name)
142-
assume_strides!(ls, name, N)
143136
end
144137
function add_mref!(
145138
ls::LoopSet, ar::ArrayReferenceMeta, i::Int, ::Type{S}, name = vptr(ar)
@@ -157,7 +150,6 @@ function add_mref!(
157150
li[i] = lib[S1[i]]
158151
inds[i] = indsb[S1[i]]
159152
end
160-
assume_strides!(ls, name, length(S1))
161153
end
162154
function add_mref!(
163155
ls::LoopSet, ar::ArrayReferenceMeta, i::Int, ::Type{OffsetStridedPointer{T,N,P}}, name = vptr(ar)

test/check_empty.jl

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
2+
function mysum_checked(x)
3+
s = zero(eltype(x))
4+
@avx check_empty = true for i eachindex(x)
5+
s += x[i]
6+
end
7+
s
8+
end
9+
function mysum_unchecked(x)
10+
s = zero(eltype(x))
11+
@avx for i eachindex(x)
12+
s += x[i]
13+
end
14+
s
15+
end
16+
17+
@testset "Check Empty" begin
18+
x = fill(9999, 100, 10, 10);
19+
xv = view(x, :, 1:0, :);
20+
@test mysum_checked(x) == mysum_unchecked(x) == sum(x)
21+
@test iszero(mysum_checked(xv))
22+
@test_broken !iszero(mysum_unchecked(xv))
23+
24+
end
25+

0 commit comments

Comments
 (0)