|
| 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