Skip to content

Commit 4ad1034

Browse files
committed
initial commit
1 parent f55123e commit 4ad1034

File tree

2 files changed

+82
-0
lines changed

2 files changed

+82
-0
lines changed

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::Dict{Symbol, 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::Dict{Symbol, Symbol}, ivars::Vector{Symbol})
32+
if (x keys(vars)) && (x ivars)
33+
gx = gensym(x)
34+
push!(vars, x => gx)
35+
gx
36+
else
37+
x
38+
end
39+
end
40+
41+
find_vars_and_gensym!(x, vars::Dict{Symbol, Symbol}, ivars::Vector{Symbol}) = x
42+
43+
#----------------------------------------------------------------------------------------------------
44+
45+
nt(keys, vals) = NamedTuple{keys, typeof(vals)}(vals)
46+
47+
macro _avx(ex)
48+
D = Dict{Symbol, Symbol}()
49+
ivars = Symbol[]
50+
51+
gex = prewalk(x -> find_vars_and_gensym!(x, D, ivars), ex)
52+
53+
type_ex = to_type(gex)
54+
55+
tvars = Tuple(keys(D))
56+
tgvars = Tuple(values(D))
57+
58+
quote
59+
kwargs = nt($(QuoteNode(tgvars)), $(Expr(:tuple, tvars...)))
60+
$(Expr(:tuple, tvars...)) = _avx($(QuoteNode(type_ex)), kwargs)
61+
end |> esc
62+
end
63+
64+
@generated function _avx(::Type{ex_t}, var_nt::NamedTuple{keys, var_types}) where {ex_t <: Ex, keys, var_types}
65+
ex = to_expr(ex_t)
66+
67+
var_defs = Expr(:block, )
68+
for k in keys
69+
push!(var_defs.args, :($k = var_nt[$(QuoteNode(k))]))
70+
end
71+
72+
quote
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)