Skip to content

Commit 4f84081

Browse files
committed
Add more if/else tests, and a bugfix for when a conditional appears as an argument to another function..
1 parent f1f5b89 commit 4f84081

File tree

3 files changed

+64
-9
lines changed

3 files changed

+64
-9
lines changed

src/add_ifelse.jl

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ end
2121

2222
function add_andblock!(ls::LoopSet, condop::Operation, LHS, rhsop::Operation, elementbytes::Int, position::Int)
2323
if LHS isa Symbol
24-
altop = getop(ls, LHS)
24+
altop = getop(ls, LHS, elementbytes)
2525
return add_compute!(ls, LHS, :vifelse, [condop, rhsop, altop], elementbytes)
2626
elseif LHS isa Expr && LHS.head === :ref
2727
return add_conditional_store!(ls, LHS, condop, rhsop, elementbytes)
2828
else
2929
throw("Don't know how to assign onto $LHS.")
30-
end
30+
end
3131
end
3232
function add_andblock!(ls::LoopSet, condop::Operation, LHS, RHS::Expr, elementbytes::Int, position::Int)
3333
rhsop = add_compute!(ls, gensym(:iftruerhs), RHS, elementbytes, position)
@@ -56,11 +56,15 @@ function add_andblock!(ls::LoopSet, ex::Expr, elementbytes::Int, position::Int)
5656
end
5757

5858
function add_orblock!(ls::LoopSet, condop::Operation, LHS, rhsop::Operation, elementbytes::Int, position::Int)
59+
negatedcondop = add_compute!(ls, gensym(:negated_mask), :~, [condop], elementbytes)
5960
if LHS isa Symbol
60-
altop = getop(ls, LHS)
61-
return add_compute!(ls, LHS, :vifelse, [condop, altop, rhsop], elementbytes)
61+
altop = getop(ls, LHS, elementbytes)
62+
# return add_compute!(ls, LHS, :vifelse, [condop, altop, rhsop], elementbytes)
63+
# Placing altop second seems to let LLVM fuse operations; but as of LLVM 9.0.1 it will not if altop is first
64+
# therefore, we negate the condition and switch order so that the altop is second.
65+
return add_compute!(ls, LHS, :vifelse, [negatedcondop, rhsop, altop], elementbytes)
6266
elseif LHS isa Expr && LHS.head === :ref
63-
negatedcondop = add_compute!(ls, gensym(:negated_mask), :~, [condop], elementbytes)
67+
# negatedcondop = add_compute!(ls, gensym(:negated_mask), :~, [condop], elementbytes)
6468
return add_conditional_store!(ls, LHS, negatedcondop, rhsop, elementbytes)
6569
else
6670
throw("Don't know how to assign onto $LHS.")

src/graphs.jl

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
11

2-
isdense(::Type{<:DenseArray}) = true
3-
42
# """
53
# ShortVector{T} simply wraps a Vector{T}, but uses a different hash function that is faster for short vectors to support using it as the keys of a Dict.
64
# This hash function scales O(N) with length of the vectors, so it is slow for long vectors.
@@ -464,7 +462,7 @@ function add_operation!(
464462
add_compute!(ls, LHS, RHS, elementbytes, position)
465463
end
466464
elseif RHS.head === :if
467-
add_if!(ls, LHS, RHS, elementbytes)
465+
add_if!(ls, LHS, RHS, elementbytes, position)
468466
else
469467
throw("Expression not recognized:\n$x")
470468
end

test/ifelsemasks.jl

Lines changed: 54 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,21 @@
1414
c[i] = a[i] > b[i] ? a[i] + b[i] : a[i] * b[i]
1515
end
1616
end
17+
function addormulp1!(c, a, b)
18+
for i eachindex(c,a,b)
19+
c[i] = 1 + (a[i] > b[i] ? a[i] + b[i] : a[i] * b[i])
20+
end
21+
end
22+
function addormulp1_avx!(c, a, b)
23+
@_avx for i eachindex(c,a,b)
24+
c[i] = 1 + (a[i] > b[i] ? a[i] + b[i] : a[i] * b[i])
25+
end
26+
end
27+
function addormulp1avx!(c, a, b)
28+
@avx for i eachindex(c,a,b)
29+
c[i] = 1 + (a[i] > b[i] ? a[i] + b[i] : a[i] * b[i])
30+
end
31+
end
1732

1833

1934
function maybewriteand!(c, a, b)
@@ -155,7 +170,34 @@
155170
(x1 < 60) || setindex!(x, x3, i)
156171
end
157172
end
158-
173+
function andorassignment!(x, y, z)
174+
@inbounds for i eachindex(x, y, z)
175+
yᵢ = y[i]
176+
zᵢ = z[i]
177+
(yᵢ > 0.5) || (yᵢ *= 2)
178+
(zᵢ < 0.5) && (zᵢ *= 2)
179+
x[i] = yᵢ * zᵢ
180+
end
181+
end
182+
function andorassignmentavx!(x, y, z)
183+
@avx for i eachindex(x, y, z)
184+
yᵢ = y[i]
185+
zᵢ = z[i]
186+
(yᵢ > 0.5) || (yᵢ *= 2)
187+
(zᵢ < 0.5) && (zᵢ *= 2)
188+
x[i] = yᵢ * zᵢ
189+
end
190+
end
191+
function andorassignment_avx!(x, y, z)
192+
@avx for i eachindex(x, y, z)
193+
yᵢ = y[i]
194+
zᵢ = z[i]
195+
(yᵢ > 0.5) || (yᵢ *= 2)
196+
(zᵢ < 0.5) && (zᵢ *= 2)
197+
x[i] = yᵢ * zᵢ
198+
end
199+
end
200+
159201
N = 117
160202
for T (Float32, Float64, Int32, Int64)
161203
@show T, @__LINE__
@@ -170,6 +212,11 @@
170212
@test c1 c2
171213
fill!(c2, -999999999); addormulavx!(c2, a, b)
172214
@test c1 c2
215+
addormulp1!(c1, a, b)
216+
addormulp1_avx!(c2, a, b)
217+
@test c1 c2
218+
fill!(c2, -999999999); addormulp1avx!(c2, a, b)
219+
@test c1 c2
173220

174221
fill!(c1, -999999999); maybewriteand!(c1, a, b)
175222
fill!(c2, -999999999); maybewriteand_avx!(c2, a, b)
@@ -183,6 +230,12 @@
183230
fill!(c2, -999999999); maybewriteoravx!(c2, a, b)
184231
@test c1 c2
185232

233+
andorassignment!(c1, a, b)
234+
andorassignmentavx!(c2, a, b)
235+
@test c1 c2
236+
fill!(c2, -999999999); andorassignment_avx!(c2, a, b)
237+
@test c1 c2
238+
186239
if T <: Union{Float32,Float64}
187240
a .*= 100;
188241
end

0 commit comments

Comments
 (0)