Skip to content

Commit 3d569d6

Browse files
committed
Have OffsetArray tests look for out of bounds stores. Allow LowDimArrays to only specify leading dimensions.
1 parent 9537e6a commit 3d569d6

File tree

3 files changed

+39
-35
lines changed

3 files changed

+39
-35
lines changed

src/broadcast.jl

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,8 @@ end
118118
Base.@propagate_inbounds Base.getindex(A::LowDimArray, i...) = getindex(A.data, i...)
119119
Base.size(A::LowDimArray) = Base.size(A.data)
120120
@generated function VectorizationBase.stridedpointer(A::LowDimArray{D,T,N}) where {D,T,N}
121-
s = Expr(:call, Expr(:(.), Expr(:(.), :LoopVectorization, QuoteNode(:VectorizationBase)), QuoteNode(:staticmul)), T, Expr(:tuple, [Expr(:ref, :strideA, n) for n 1+D[1]:N if D[n]]...))
121+
smul = Expr(:(.), Expr(:(.), :LoopVectorization, QuoteNode(:VectorizationBase)), QuoteNode(:staticmul))
122+
s = Expr(:call, smul, T, Expr(:tuple, [Expr(:ref, :strideA, n) for n 1+D[1]:N if ((length(D) < n) || D[n])]...))
122123
f = D[1] ? :PackedStridedPointer : :SparseStridedPointer
123124
Expr(:block, Expr(:meta,:inline), Expr(:(=), :strideA, Expr(:call, :strides, Expr(:(.), :A, QuoteNode(:data)))),
124125
Expr(:call, Expr(:(.), :VectorizationBase, QuoteNode(f)), Expr(:call, :pointer, Expr(:(.), :A, QuoteNode(:data))), s))
@@ -137,10 +138,11 @@ function add_broadcast!(
137138
@nospecialize(LDA::Type{<:LowDimArray}), elementbytes::Int
138139
)
139140
D,T,N::Int,_ = LDA.parameters
140-
if !any(D)
141+
Dlen = length(D)
142+
if Dlen == N && !any(D)
141143
return extract_all_1_array!(ls, bcname, N, elementbytes)
142144
end
143-
fulldims = Symbol[loopsyms[n] for n 1:N if D[n]::Bool]
145+
fulldims = Symbol[loopsyms[n] for n 1:N if ((Dlen < n) || D[n]::Bool)]
144146
ref = ArrayReference(bcname, fulldims)
145147
add_simple_load!(ls, destname, ref, elementbytes, true, false )::Operation
146148
end

test/broadcast.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
fill!(c2, 99999); @avx @. c2 = a + br;
2424
@test c1 c2
2525
br = reshape(b, (1,99,99));
26-
bl = LowDimArray{(false,true,true)}(br);
26+
bl = LowDimArray{(false,)}(br);
2727
@. c1 = a + br;
2828
fill!(c2, 99999);
2929
@avx @. c2 = a + bl;

test/offsetarrays.jl

Lines changed: 33 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -188,19 +188,20 @@ using LoopVectorization.VectorizationBase: StaticUnitRange
188188
out
189189
end
190190
function avxgeneric2!(out, A, kern)
191-
@avx for I in CartesianIndices(out)
192-
tmp = zero(eltype(out))
193-
for J in CartesianIndices(kern)
194-
tmp += A[I+J]*kern[J]
195-
end
196-
out[I] = tmp
197-
end
198-
out
199-
end
191+
@avx for I in CartesianIndices(out)
192+
tmp = zero(eltype(out))
193+
for J in CartesianIndices(kern)
194+
tmp += A[I+J]*kern[J]
195+
end
196+
out[I] = tmp
197+
end
198+
out
199+
end
200200

201201
for T (Float32, Float64)
202202
@show T, @__LINE__
203203
Abase = fill(T(NaN), 200, 200);
204+
# out of bounds reads load NaNs, poisoning results leading to test failure.
204205
A = view(Abase, 51:150, 51:150);
205206
A .= rand.();
206207
Atbase = copy(Abase');
@@ -209,56 +210,57 @@ using LoopVectorization.VectorizationBase: StaticUnitRange
209210
@show r
210211
fr = first(r); lr = last(r);
211212
kern = OffsetArray(rand(T, length(r), length(r)), r, r);
212-
out1 = OffsetArray(view(similar(A, size(A) .+ 32), (1+lr:100-lr) .+ 32, (1+lr:100-lr) .+ 32), lr, lr); # stay away from the edges of A
213+
# We test parent equality so that an accidental write out of bounds leading to test failure.
214+
out1 = OffsetArray(view(fill(T(-123456.789), size(A) .+ 32), (1+lr:100-lr) .+ 32, (1+lr:100-lr) .+ 32), lr, lr); # stay away from the edges of A
213215
# out1 = OffsetArray(similar(A, size(A).-2), 1, 1); # stay away from the edges of A
214-
out2 = similar(out1); out3 = similar(out1); out4 = similar(out1);
216+
out2 = deepcopy(out1); out3 = deepcopy(out1); out4 = deepcopy(out1);
215217
skern = SizedOffsetMatrix{T,fr,lr,fr,lr}(parent(kern));
216218

217219
old2d!(out1, A, kern);
218220
avx2d!(out2, A, kern);
219-
@test out1 out2
221+
@test parent(out1) parent(out2)
220222

221223
avx2douter!(out3, A, kern);
222-
@test out1 out3
224+
@test parent(out1) parent(out3)
223225

224226
fill!(out2, NaN); avx2d!(out2, A, skern);
225-
@test out1 out2
227+
@test parent(out1) parent(out2)
226228

227229
fill!(out2, NaN); avx2douter!(out2, At', kern);
228-
@test out1 out2
230+
@test parent(out1) parent(out2)
229231

230232
fill!(out2, NaN); avx2douter!(out2', A, kern);
231-
@test out1 out2'
233+
@test parent(out1) parent(out2)'
232234

233235
fill!(out2, NaN); avx2douter!(out2', At', kern);
234-
@test out1 out2'
236+
@test parent(out1) parent(out2)'
235237

236238
fill!(out3, NaN); avx2douter!(out3, A, skern);
237-
@test out1 out3
239+
@test parent(out1) parent(out3)
238240

239241
if r == -1:1
240242
fill!(out3, NaN); avx2dunrolled!(out3, A, skern);
241-
@test out1 out3
243+
@test parent(out1) parent(out3)
242244

243245
fill!(out3, NaN); avx2dunrolled2x2!(out3, A, skern);
244-
@test out1 out3
246+
@test parent(out1) parent(out3)
245247

246248
fill!(out3, NaN); avx2dunrolled3x3!(out3, A, skern);
247-
@test out1 out3
249+
@test parent(out1) parent(out3)
248250
end
249251

250-
@test avxgeneric!(out4, A, kern) out1
252+
@test parent(avxgeneric!(out4, A, kern)) parent(out1)
251253
fill!(out4, NaN);
252-
@test avxgeneric!(out4, A, skern) out1
254+
@test parent(avxgeneric!(out4, A, skern)) parent(out1)
253255

254-
fill!(out4, NaN); @test avxgeneric2!(out4, A, kern) out1
255-
fill!(out4, NaN); @test avxgeneric2!(out4, A, skern) out1
256-
fill!(out4, NaN); @test avxgeneric2!(out4, At', kern) out1
257-
fill!(out4, NaN); @test avxgeneric2!(out4, At', skern) out1
258-
fill!(out4, NaN); @test avxgeneric2!(out4', A, kern) out1
259-
fill!(out4, NaN); @test avxgeneric2!(out4', A, skern) out1
260-
fill!(out4, NaN); @test avxgeneric2!(out4', At', kern) out1
261-
fill!(out4, NaN); @test avxgeneric2!(out4', At', skern) out1
256+
fill!(out4, NaN); @test parent(avxgeneric2!(out4, A, kern)) parent(out1)
257+
fill!(out4, NaN); @test parent(avxgeneric2!(out4, A, skern)) parent(out1)
258+
fill!(out4, NaN); @test parent(avxgeneric2!(out4, At', kern)) parent(out1)
259+
fill!(out4, NaN); @test parent(avxgeneric2!(out4, At', skern)) parent(out1)
260+
fill!(out4, NaN); @test parent(avxgeneric2!(out4', A, kern)')' parent(out1)
261+
fill!(out4, NaN); @test parent(avxgeneric2!(out4', A, skern)')' parent(out1)
262+
fill!(out4, NaN); @test parent(avxgeneric2!(out4', At', kern)')' parent(out1)
263+
fill!(out4, NaN); @test parent(avxgeneric2!(out4', At', skern)')' parent(out1)
262264
end
263265
end
264266

0 commit comments

Comments
 (0)