Skip to content

Commit 88103ef

Browse files
authored
Merge pull request #17 from MasonProtter/_avx
Refactor @avx with generated functions
2 parents 516216b + 8b9cd1b commit 88103ef

File tree

5 files changed

+578
-172
lines changed

5 files changed

+578
-172
lines changed

Manifest.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,9 +61,9 @@ uuid = "9a3f8284-a2c9-5f02-9a11-845980a1fd5c"
6161

6262
[[SIMDPirates]]
6363
deps = ["MacroTools", "VectorizationBase"]
64-
git-tree-sha1 = "910193d289b41e570118c4e444f0c05cc700a2f7"
64+
git-tree-sha1 = "500294a8b1001bdda2483fc6d675956798ad8764"
6565
uuid = "21efa798-c60a-11e8-04d3-e1a92915a26a"
66-
version = "0.1.5"
66+
version = "0.1.6"
6767

6868
[[SLEEFPirates]]
6969
deps = ["SIMDPirates", "VectorizationBase"]

Project.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name = "LoopVectorization"
22
uuid = "bdcacae8-1622-11e9-2a5c-532679323890"
33
authors = ["Chris Elrod <[email protected]>"]
4-
version = "0.3.6"
4+
version = "0.3.7"
55

66
[deps]
77
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
@@ -14,7 +14,7 @@ VectorizationBase = "3d5dd08c-fd9d-11e8-17fa-ed2836048c2f"
1414
[compat]
1515
MacroTools = "0.5"
1616
Parameters = "0.12.0"
17-
SIMDPirates = "0.1.5"
17+
SIMDPirates = "0.1.6"
1818
SLEEFPirates = "0.1.3"
1919
VectorizationBase = "0.1.9"
2020
julia = "1.3.0"

src/LoopVectorization.jl

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ include("determinestrategy.jl")
2222
include("lowering.jl")
2323
include("constructors.jl")
2424
include("map.jl")
25+
include("_avx.jl")
26+
27+
export @_avx, _avx
28+
2529
# include("precompile.jl")
2630
# _precompile_()
2731

src/_avx.jl

Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
using MacroTools: postwalk, prewalk
2+
using LoopVectorization: LoopVectorization, LoopSet, lower
3+
4+
#----------------------------------------------------------------------------------------------------
5+
6+
struct Ex{T, Tup} end
7+
8+
function to_type(ex::Expr)
9+
Ex{ex.head, Tuple{to_type.(ex.args)...}}
10+
end
11+
12+
to_type(x) = x
13+
to_type(::LineNumberNode) = nothing
14+
15+
#----------------------------------------------------------------------------------------------------
16+
17+
to_expr(ex::Type{Ex{Head, Tup}}) where {Head, Tup} = Expr(Head, (to_expr(x) for x in Tup.parameters)...)
18+
to_expr(x) = x
19+
20+
#----------------------------------------------------------------------------------------------------
21+
22+
function find_vars_and_gensym!(ex::Expr, vars::Set{Symbol}, ivars::Vector{Symbol})
23+
if ex.head == :(=) && ex.args[1] isa Symbol
24+
push!(ivars, ex.args[1])
25+
elseif ex.head == :call
26+
push!(ivars, ex.args[1])
27+
end
28+
ex
29+
end
30+
31+
function find_vars_and_gensym!(x::Symbol, vars::Set{Symbol}, ivars::Vector{Symbol})
32+
if (x vars) && (x ivars)
33+
push!(vars, x)
34+
x
35+
else
36+
x
37+
end
38+
end
39+
40+
find_vars_and_gensym!(x, vars::Set{Symbol}, ivars::Vector{Symbol}) = x
41+
42+
#----------------------------------------------------------------------------------------------------
43+
44+
nt(keys, vals) = NamedTuple{keys, typeof(vals)}(vals)
45+
46+
macro _avx(ex)
47+
D = Set{Symbol}()
48+
ivars = Symbol[]
49+
50+
gex = prewalk(x -> find_vars_and_gensym!(x, D, ivars), ex)
51+
52+
type_ex = to_type(gex)
53+
54+
tvars = Tuple(D)
55+
56+
quote
57+
kwargs = LoopVectorization.nt($(QuoteNode(tvars)), $(Expr(:tuple, tvars...)))
58+
$(Expr(:tuple, tvars...)) = LoopVectorization._avx($(QuoteNode(type_ex)), kwargs)
59+
# LoopVectorization._avx($(QuoteNode(type_ex)), kwargs) # comment out the above line, uncomment this one, and get rid of the `@generated` on _avx to see the function body.
60+
end |> esc
61+
end
62+
63+
@generated function _avx(::Type{ex_t}, var_nt::NamedTuple{keys, var_types}) where {ex_t <: Ex, keys, var_types}
64+
ex = to_expr(ex_t)
65+
66+
var_defs = Expr(:block, )
67+
for k in keys
68+
push!(var_defs.args, :($k = var_nt[$(QuoteNode(k))]))
69+
end
70+
71+
quote
72+
$(Expr(:meta,:inline))
73+
$var_defs
74+
$(lower(LoopSet(ex)))
75+
$(Expr(:tuple, keys...))
76+
#$(Expr(:tuple, (:($(keys[i]) :: $(var_types.parameters[i])) for i in eachindex(keys))...))
77+
end
78+
end

0 commit comments

Comments
 (0)