Skip to content

Commit 2af36a4

Browse files
committed
Fix for i in rng
1 parent 977528d commit 2af36a4

File tree

4 files changed

+74
-37
lines changed

4 files changed

+74
-37
lines changed

src/LoopVectorization.jl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ using SIMDPirates: VECTOR_SYMBOLS, evadd, evmul, vrange, reduced_add, reduced_pr
1212
vmullog2, vmullog10, vdivlog2, vdivlog10, vmullog2add!, vmullog10add!, vdivlog2add!, vdivlog10add!, vfmaddaddone
1313
using Base.Broadcast: Broadcasted, DefaultArrayStyle
1414
using LinearAlgebra: Adjoint, Transpose
15+
using Base.Meta: isexpr
1516

1617
const SUPPORTED_TYPES = Union{Float16,Float32,Float64,Integer}
1718

src/graphs.jl

Lines changed: 45 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -369,47 +369,55 @@ This function creates a loop, while switching from 1 to 0 based indices
369369
"""
370370
function register_single_loop!(ls::LoopSet, looprange::Expr)
371371
itersym = (looprange.args[1])::Symbol
372-
r = (looprange.args[2])::Expr
373-
@assert r.head === :call
374-
f = first(r.args)
375-
loop::Loop = if f === :(:)
376-
lower = r.args[2]
377-
upper = r.args[3]
378-
lii::Bool = lower isa Integer
379-
liiv::Int = lii ? (convert(Int, lower)-1) : 0
380-
uii::Bool = upper isa Integer
381-
if lii & uii # both are integers
382-
Loop(itersym, liiv, convert(Int, upper))
383-
elseif lii # only lower bound is an integer
384-
if upper isa Symbol
385-
Loop(itersym, liiv, upper)
386-
elseif upper isa Expr
387-
Loop(itersym, liiv, add_loop_bound!(ls, itersym, upper, true))
388-
else
389-
Loop(itersym, liiv, add_loop_bound!(ls, itersym, upper, true))
372+
r = looprange.args[2]
373+
if isexpr(r, :call)
374+
f = first(r.args)
375+
loop::Loop = if f === :(:)
376+
lower = r.args[2]
377+
upper = r.args[3]
378+
lii::Bool = lower isa Integer
379+
liiv::Int = lii ? (convert(Int, lower)-1) : 0
380+
uii::Bool = upper isa Integer
381+
if lii & uii # both are integers
382+
Loop(itersym, liiv, convert(Int, upper))
383+
elseif lii # only lower bound is an integer
384+
if upper isa Symbol
385+
Loop(itersym, liiv, upper)
386+
elseif upper isa Expr
387+
Loop(itersym, liiv, add_loop_bound!(ls, itersym, upper, true))
388+
else
389+
Loop(itersym, liiv, add_loop_bound!(ls, itersym, upper, true))
390+
end
391+
elseif uii # only upper bound is an integer
392+
uiiv = convert(Int, upper)
393+
Loop(itersym, add_loop_bound!(ls, itersym, lower, false), uiiv)
394+
else # neither are integers
395+
L = add_loop_bound!(ls, itersym, lower, false)
396+
U = add_loop_bound!(ls, itersym, upper, true)
397+
Loop(itersym, L, U)
390398
end
391-
elseif uii # only upper bound is an integer
392-
uiiv = convert(Int, upper)
393-
Loop(itersym, add_loop_bound!(ls, itersym, lower, false), uiiv)
394-
else # neither are integers
395-
L = add_loop_bound!(ls, itersym, lower, false)
396-
U = add_loop_bound!(ls, itersym, upper, true)
397-
Loop(itersym, L, U)
398-
end
399-
elseif f === :eachindex
400-
N = gensym(Symbol(:loop, itersym))
401-
pushpreamble!(ls, Expr(:(=), N, Expr(:call, lv(:maybestaticlength), r.args[2])))
402-
Loop(itersym, 0, N)
403-
elseif f === :OneTo || f == Expr(:(.), :Base, QuoteNode(:OneTo))
404-
otN = r.args[2]
405-
if otN isa Integer
406-
Loop(itersym, 0, otN)
407-
else
408-
otN isa Expr && maybestatic!(otN)
399+
elseif f === :eachindex
409400
N = gensym(Symbol(:loop, itersym))
410-
pushpreamble!(ls, Expr(:(=), N, otN))
401+
pushpreamble!(ls, Expr(:(=), N, Expr(:call, lv(:maybestaticlength), r.args[2])))
411402
Loop(itersym, 0, N)
403+
elseif f === :OneTo || f == Expr(:(.), :Base, QuoteNode(:OneTo))
404+
otN = r.args[2]
405+
if otN isa Integer
406+
Loop(itersym, 0, otN)
407+
else
408+
otN isa Expr && maybestatic!(otN)
409+
N = gensym(Symbol(:loop, itersym))
410+
pushpreamble!(ls, Expr(:(=), N, otN))
411+
Loop(itersym, 0, N)
412+
end
413+
else
414+
throw("Unrecognized loop range type: $r.")
412415
end
416+
elseif isa(r, Symbol)
417+
# Treat similar to `eachindex`
418+
N = gensym(Symbol(:loop, itersym))
419+
pushpreamble!(ls, Expr(:(=), N, Expr(:call, lv(:maybestaticlength), r)))
420+
loop = Loop(itersym, 0, N)
413421
else
414422
throw("Unrecognized loop range type: $r.")
415423
end

test/dot.jl

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using LoopVectorization
2+
using Test
3+
14
@testset "dot" begin
25
dotq = :(for i eachindex(a,b)
36
s += a[i]*b[i]
@@ -46,6 +49,14 @@
4649
end
4750
s
4851
end
52+
function myselfdotavx_range(a)
53+
s = zero(eltype(a))
54+
rng = axes(a, 1)
55+
@avx for i rng
56+
s += a[i]*a[i]
57+
end
58+
s
59+
end
4960
function myselfdot_avx(a)
5061
s = zero(eltype(a))
5162
@_avx for i eachindex(a)
@@ -190,6 +201,7 @@
190201
@test dot_unroll3avx_inline(a,b) s
191202
s = myselfdot(a)
192203
@test myselfdotavx(a) s
204+
@test myselfdotavx_range(a) s
193205
@test myselfdot_avx(a) s
194206
@test myselfdotavx(a) s
195207

test/gemv.jl

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
using LoopVectorization
2+
using Test
3+
14
@testset "GEMV" begin
25
gemvq = :(for i eachindex(y)
36
yᵢ = 0.0
@@ -27,6 +30,16 @@
2730
y[i] = yᵢ
2831
end
2932
end
33+
function mygemvavx_range!(y, A, x)
34+
rng1, rng2 = axes(A)
35+
@avx for i rng1
36+
yᵢ = zero(eltype(y))
37+
for j rng2
38+
yᵢ += A[i,j] * x[j]
39+
end
40+
y[i] = yᵢ
41+
end
42+
end
3043
q = :(for i eachindex(y)
3144
yᵢ = zero(eltype(y))
3245
for j eachindex(x)
@@ -150,6 +163,9 @@
150163
@test y1 y2
151164
fill!(y2, -999.9); mygemv_avx!(y2, A, x)
152165
@test y1 y2
166+
fill!(y2, -999.9)
167+
mygemvavx_range!(y2, A, x)
168+
@test y1 y2
153169

154170
B = rand(R, N, N);
155171
G1 = Matrix{TC}(undef, N, 1);

0 commit comments

Comments
 (0)