|
| 1 | +function addsetv!(s::AbstractVector{T}, v::T) where {T} |
| 2 | + for sᵢ ∈ s |
| 3 | + sᵢ === v && return nothing |
| 4 | + end |
| 5 | + push!(s, v) |
| 6 | + nothing |
| 7 | +end |
| 8 | +function mergesetv!(s1::AbstractVector{T}, s2::AbstractVector{T}) where {T} |
| 9 | + for s ∈ s2 |
| 10 | + addsetv!(s1, s) |
| 11 | + end |
| 12 | + nothing |
| 13 | +end |
| 14 | +function mergesetdiffv!( |
| 15 | + s1::AbstractVector{T}, |
| 16 | + s2::AbstractVector{T}, |
| 17 | + s3::AbstractVector{T} |
| 18 | +) where {T} |
| 19 | + for s ∈ s2 |
| 20 | + s ∉ s3 && addsetv!(s1, s) |
| 21 | + end |
| 22 | + nothing |
| 23 | +end |
| 24 | +function setdiffv!(s3::AbstractVector{T}, s1::AbstractVector{T}, s2::AbstractVector{T}) where {T} |
| 25 | + for s ∈ s1 |
| 26 | + (s ∈ s2) || (s ∉ s3 && push!(s3, s)) |
| 27 | + end |
| 28 | +end |
| 29 | +function update_deps!(deps::Vector{Symbol}, reduceddeps::Vector{Symbol}, parent::Operation) |
| 30 | + mergesetdiffv!(deps, loopdependencies(parent), reduceddependencies(parent)) |
| 31 | + if !(isload(parent) || isconstant(parent)) && parent.instruction.instr ∉ (:reduced_add, :reduced_prod, :reduce_to_add, :reduce_to_prod) |
| 32 | + mergesetv!(reduceddeps, reduceddependencies(parent)) |
| 33 | + end |
| 34 | + nothing |
| 35 | +end |
| 36 | + |
| 37 | +function pushparent!(parents::Vector{Operation}, deps::Vector{Symbol}, reduceddeps::Vector{Symbol}, parent::Operation) |
| 38 | + push!(parents, parent) |
| 39 | + update_deps!(deps, reduceddeps, parent) |
| 40 | +end |
| 41 | +function pushparent!(mpref::ArrayReferenceMetaPosition, parent::Operation) |
| 42 | + pushparent!(mpref.parents, mpref.loopdependencies, mpref.reduceddeps, parent) |
| 43 | +end |
| 44 | +function add_parent!( |
| 45 | + parents::Vector{Operation}, deps::Vector{Symbol}, reduceddeps::Vector{Symbol}, ls::LoopSet, var, elementbytes::Int = 8 |
| 46 | +) |
| 47 | + parent = if var isa Symbol |
| 48 | + getop(ls, var, elementbytes) |
| 49 | + elseif var isa Expr #CSE candidate |
| 50 | + add_operation!(ls, gensym(:temporary), var, elementbytes) |
| 51 | + else # assumed constant |
| 52 | + add_constant!(ls, var, elementbytes) |
| 53 | + end |
| 54 | + pushparent!(parents, deps, reduceddeps, parent) |
| 55 | +end |
| 56 | +function add_reduction!( |
| 57 | + parents::Vector{Operation}, deps::Vector{Symbol}, reduceddeps::Vector{Symbol}, ls::LoopSet, var::Symbol, elementbytes::Int = 8 |
| 58 | +) |
| 59 | + get!(ls.opdict, var) do |
| 60 | + add_constant!(ls, var, elementbytes) |
| 61 | + end |
| 62 | + # pushparent!(parents, deps, reduceddeps, parent) |
| 63 | +end |
| 64 | +function add_reduction_update_parent!( |
| 65 | + parents::Vector{Operation}, deps::Vector{Symbol}, reduceddeps::Vector{Symbol}, ls::LoopSet, |
| 66 | + var::Symbol, instr::Symbol, elementbytes::Int = 8 |
| 67 | +) |
| 68 | + parent = getop(ls, var, elementbytes) |
| 69 | + isloopconstant = parent.instruction === LOOPCONSTANT |
| 70 | + Instr = Instruction(instr) |
| 71 | + # if parent is not an outer reduction... |
| 72 | + if !isloopconstant |
| 73 | + # and parent is not a reduction_zero |
| 74 | + reduct_zero = REDUCTION_ZERO[Instr] |
| 75 | + reductcombine = REDUCTION_SCALAR_COMBINE[Instr].name |
| 76 | + reductsym = gensym(:reduction) |
| 77 | + reductinit = add_constant!(ls, Expr(:call, reduct_zero, ls.T), loopdependencies(parent), reductsym, reduct_zero, elementbytes) |
| 78 | + if isconstant(parent) && reduct_zero === parent.instruction.mod #we can use parent op as initialization. |
| 79 | + reductcombine = REDUCTION_COMBINETO[reductcombine] |
| 80 | + # else # we cannot use parent op as initialization. |
| 81 | + end |
| 82 | + else |
| 83 | + reductinit = parent |
| 84 | + reductsym = var |
| 85 | + reductcombine = Symbol("") |
| 86 | + end |
| 87 | + # mergesetv!(reduceddeps, deps) |
| 88 | + # if length(reduceddependencies(reductinit)) == 0 |
| 89 | + # setdiffv!(reduceddeps, deps, loopdependencies(reductinit)) |
| 90 | + # else |
| 91 | + setdiffv!(reduceddeps, deps, loopdependencies(reductinit)) |
| 92 | + # end |
| 93 | + # mergesetv!(reduceddependencies(reductinit), reduceddeps) |
| 94 | + pushparent!(parents, deps, reduceddeps, reductinit)#parent) # deps and reduced deps will not be disjoint |
| 95 | + op = Operation(length(operations(ls)), reductsym, elementbytes, instr, compute, deps, reduceddeps, parents) |
| 96 | + parent.instruction === LOOPCONSTANT && push!(ls.outer_reductions, identifier(op)) |
| 97 | + opout = pushop!(ls, op, var) # note this overwrites the entry in the operations dict, but not the vector |
| 98 | + isloopconstant && return opout |
| 99 | + # create child |
| 100 | + childdeps = Symbol[]; childrdeps = Symbol[]; childparents = Operation[] |
| 101 | + pushparent!(childparents, childdeps, childrdeps, op) # reduce op |
| 102 | + pushparent!(childparents, childdeps, childrdeps, parent) # to |
| 103 | + child = Operation( |
| 104 | + length(operations(ls)), name(parent), elementbytes, reductcombine, compute, childdeps, childrdeps, childparents |
| 105 | + ) |
| 106 | + pushop!(ls, child, name(parent)) |
| 107 | +end |
| 108 | +function add_compute!( |
| 109 | + ls::LoopSet, var::Symbol, ex::Expr, elementbytes::Int = 8, |
| 110 | + mpref::Union{Nothing,ArrayReferenceMetaPosition} = nothing |
| 111 | +) |
| 112 | + @assert ex.head === :call |
| 113 | + instr = instruction(first(ex.args))::Symbol |
| 114 | + args = @view(ex.args[2:end]) |
| 115 | + parents = Operation[] |
| 116 | + deps = Symbol[] |
| 117 | + reduceddeps = Symbol[] |
| 118 | + reduction = false |
| 119 | + for arg ∈ args |
| 120 | + if var === arg |
| 121 | + reduction = true |
| 122 | + add_reduction!(parents, deps, reduceddeps, ls, arg, elementbytes) |
| 123 | + elseif arg isa Expr |
| 124 | + isref, argref = tryrefconvert(ls, arg, elementbytes) |
| 125 | + if isref |
| 126 | + if mpref == argref |
| 127 | + reduction = true |
| 128 | + add_load!(ls, var, mpref, elementbytes) |
| 129 | + else |
| 130 | + pushparent!(parents, deps, reduceddeps, add_load!(ls, gensym(:tempload), argref, elementbytes)) |
| 131 | + end |
| 132 | + else |
| 133 | + add_parent!(parents, deps, reduceddeps, ls, arg, elementbytes) |
| 134 | + end |
| 135 | + else |
| 136 | + add_parent!(parents, deps, reduceddeps, ls, arg, elementbytes) |
| 137 | + end |
| 138 | + end |
| 139 | + if reduction # arg[reduction] is the reduction |
| 140 | + add_reduction_update_parent!(parents, deps, reduceddeps, ls, var, instr, elementbytes) |
| 141 | + else |
| 142 | + op = Operation(length(operations(ls)), var, elementbytes, instr, compute, deps, reduceddeps, parents) |
| 143 | + pushop!(ls, op, var) |
| 144 | + end |
| 145 | +end |
| 146 | + |
| 147 | +function add_compute!( |
| 148 | + ls::LoopSet, LHS::Symbol, instr, parents::Vector{Operation}, elementbytes |
| 149 | +) |
| 150 | + deps = Symbol[] |
| 151 | + reduceddeps = Symbol[] |
| 152 | + foreach(parent -> update_deps!(deps, reduceddeps, parent), parents) |
| 153 | + op = Operation(length(operations(ls)), LHS, elementbytes, instr, compute, deps, reduceddeps, parents) |
| 154 | + pushop!(ls, op, LHS) |
| 155 | +end |
| 156 | + |
0 commit comments