Skip to content

Commit 6987acf

Browse files
committed
Try to reduce recompilation in some circumstances by no longer gensyming symbols that appear as parameters in _avx_! calls
1 parent 3825b50 commit 6987acf

File tree

7 files changed

+70
-61
lines changed

7 files changed

+70
-61
lines changed

src/add_compute.jl

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ function add_parent!(
6565
opp
6666
end
6767
elseif var isa Expr #CSE candidate
68-
add_operation!(ls, gensym(:temporary), var, elementbytes, position)
68+
add_operation!(ls, gensym!(ls, "temp"), var, elementbytes, position)
6969
else # assumed constant
7070
add_constant!(ls, var, elementbytes)
7171
# add_constant!(ls, var, deps, gensym(:loopredefconst), elementbytes)
@@ -177,8 +177,10 @@ function add_reduction_update_parent!(
177177
# We add
178178
reductcombine = reduction_scalar_combine(instrclass)
179179
# reductcombine = :identity
180-
reductsym = gensym(:reduction)
181-
reductinit = add_constant!(ls, gensym(:reductzero), loopdependencies(parent), reductsym, elementbytes, :numericconstant)
180+
reductsym = gensym!(ls, "reduction")
181+
reductzero_sym = gensym!(ls, "reduction##zero")
182+
# reductsym = gensym(:reduction)
183+
reductinit = add_constant!(ls, reductzero_sym, loopdependencies(parent), reductsym, elementbytes, :numericconstant)
182184
if reduct_zero === :zero
183185
push!(ls.preamble_zeros, (identifier(reductinit), IntOrFloat))
184186
else
@@ -255,7 +257,7 @@ function add_compute!(
255257
pushparent!(vparents, deps, reduceddeps, add_load!(ls, argref, elementbytes))
256258
end
257259
else
258-
argref.varname = gensym(:tempload)
260+
argref.varname = gensym!(ls, "tempload")
259261
pushparent!(vparents, deps, reduceddeps, add_load!(ls, argref, elementbytes))
260262
end
261263
else
@@ -353,7 +355,7 @@ function add_pow!(
353355
ls::LoopSet, var::Symbol, @nospecialize(x), p::Real, elementbytes::Int, position::Int
354356
)
355357
xop::Operation = if x isa Expr
356-
add_operation!(ls, gensym(:xpow), x, elementbytes, position)
358+
add_operation!(ls, Symbol("###xpow###$(length(operations(ls)))###"), x, elementbytes, position)
357359
elseif x isa Symbol
358360
if x ls.loopsymbols
359361
add_loopvalue!(ls, x, elementbytes)
@@ -377,7 +379,7 @@ function add_pow!(
377379
if pint == -1
378380
return add_compute!(ls, var, :inv, [xop], elementbytes)
379381
elseif pint < 0
380-
xop = add_compute!(ls, gensym(:inverse), :inv, [xop], elementbytes)
382+
xop = add_compute!(ls, gensym!(ls, "inverse"), :inv, [xop], elementbytes)
381383
pint = - pint
382384
end
383385
if pint == 0
@@ -394,17 +396,17 @@ function add_pow!(
394396
t = trailing_zeros(pint) + 1
395397
pint >>= t
396398
while (t -= 1) > 0
397-
varname = (iszero(pint) && isone(t)) ? var : gensym(:pbs)
399+
varname = (iszero(pint) && isone(t)) ? var : gensym!(ls, "pbs")
398400
xop = add_compute!(ls, varname, :abs2, [xop], elementbytes)
399401
end
400402
yop = xop
401403
while pint > 0
402404
t = trailing_zeros(pint) + 1
403405
pint >>= t
404406
while (t -= 1) >= 0
405-
xop = add_compute!(ls, gensym(:pbs), :abs2, [xop], elementbytes)
407+
xop = add_compute!(ls, gensym!(ls, "pbs"), :abs2, [xop], elementbytes)
406408
end
407-
yop = add_compute!(ls, iszero(pint) ? var : gensym(:pbs), :(*), [xop, yop], elementbytes)
409+
yop = add_compute!(ls, iszero(pint) ? var : gensym!(ls, "pbs"), :(*), [xop, yop], elementbytes)
408410
end
409411
yop
410412
end

src/add_constants.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ end
1010
# add_constant!(ls, sym, elementbytes)
1111
# end
1212
function add_constant!(ls::LoopSet, var::Number, elementbytes::Int = 8)
13-
op = Operation(length(operations(ls)), gensym(:loopconstnumber), elementbytes, LOOPCONSTANT, constant, NODEPENDENCY, Symbol[], NOPARENTS)
13+
op = Operation(length(operations(ls)), gensym!(ls, "loopconstnumber"), elementbytes, LOOPCONSTANT, constant, NODEPENDENCY, Symbol[], NOPARENTS)
1414
ops = operations(ls)
1515
typ = var isa Integer ? HardInt : HardFloat
1616
rop = pushop!(ls, op)
@@ -36,7 +36,7 @@ end
3636
function add_constant!(ls::LoopSet, mpref::ArrayReferenceMetaPosition, elementbytes::Int)
3737
op = Operation(length(operations(ls)), varname(mpref), elementbytes, LOOPCONSTANT, constant, NODEPENDENCY, Symbol[], NOPARENTS, mpref.mref)
3838
add_vptr!(ls, op)
39-
temp = gensym(:intermediateconstref)
39+
temp = gensym!(ls, "intermediateconstref")
4040
vloadcall = Expr(:call, lv(:vload), mpref.mref.ptr)
4141
if length(getindices(op)) > 0
4242
push!(vloadcall.args, mem_offset(op, UnrollArgs(0, Symbol(""), Symbol(""), Symbol(""), 0, nothing), Bool[]))
@@ -69,7 +69,7 @@ end
6969
function add_constant!(
7070
ls::LoopSet, value::Number, deps::Vector{Symbol}, assignedsym::Symbol, elementbytes::Int
7171
)
72-
op = add_constant!(ls, gensym(string(value)), deps, assignedsym, elementbytes, :numericconstant)
72+
op = add_constant!(ls, gensym!(ls, string(value)), deps, assignedsym, elementbytes, :numericconstant)
7373
pushpreamble!(ls, op, value)
7474
op
7575
end

src/add_ifelse.jl

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
## Currently, if/else will create its own local scope
33
## Assignments will not register in the loop's main scope
44
## although stores and return values will.
5-
negateop!(ls::LoopSet, condop::Operation, elementbytes::Int) = add_compute!(ls, gensym(:negated_mask), :~, [condop], elementbytes)
5+
negateop!(ls::LoopSet, condop::Operation, elementbytes::Int) = add_compute!(ls, gensym!(ls, "negated#mask"), :~, [condop], elementbytes)
66

77
function add_if!(ls::LoopSet, LHS::Symbol, RHS::Expr, elementbytes::Int, position::Int, mpref::Union{Nothing,ArrayReferenceMetaPosition} = nothing)
88
# for now, just simple 1-liners
@@ -11,13 +11,13 @@ function add_if!(ls::LoopSet, LHS::Symbol, RHS::Expr, elementbytes::Int, positio
1111
condop = if condition isa Symbol
1212
getop(ls, condition, elementbytes)
1313
elseif isnothing(mpref)
14-
add_operation!(ls, gensym(:mask), condition, elementbytes, position)
14+
add_operation!(ls, gensym!(ls, "mask"), condition, elementbytes, position)
1515
else
16-
add_operation!(ls, gensym(:mask), condition, mpref, elementbytes, position)
16+
add_operation!(ls, gensym!(ls, "mask"), condition, mpref, elementbytes, position)
1717
end
1818
iftrue = RHS.args[2]
1919
if iftrue isa Expr
20-
trueop = add_operation!(ls, gensym(:iftrue), iftrue, elementbytes, position)
20+
trueop = add_operation!(ls, gensym!(ls, "iftrue"), iftrue, elementbytes, position)
2121
if iftrue.head === :ref && all(ld -> ld loopdependencies(trueop), loopdependencies(condop)) && !search_tree(parents(condop), trueop)
2222
trueop.instruction = Instruction(:conditionalload)
2323
push!(parents(trueop), condop)
@@ -27,7 +27,7 @@ function add_if!(ls::LoopSet, LHS::Symbol, RHS::Expr, elementbytes::Int, positio
2727
end
2828
iffalse = RHS.args[3]
2929
if iffalse isa Expr
30-
falseop = add_operation!(ls, gensym(:iffalse), iffalse, elementbytes, position)
30+
falseop = add_operation!(ls, gensym!(ls, "iffalse"), iffalse, elementbytes, position)
3131
if iffalse.head === :ref && all(ld -> ld loopdependencies(falseop), loopdependencies(condop)) && !search_tree(parents(condop), falseop)
3232
falseop.instruction = Instruction(:conditionalload)
3333
push!(parents(falseop), negateop!(ls, condop, elementbytes))
@@ -49,15 +49,15 @@ function add_andblock!(ls::LoopSet, condop::Operation, LHS, rhsop::Operation, el
4949
end
5050
end
5151
function add_andblock!(ls::LoopSet, condop::Operation, LHS, RHS::Expr, elementbytes::Int, position::Int)
52-
rhsop = add_compute!(ls, gensym(:iftruerhs), RHS, elementbytes, position)
52+
rhsop = add_compute!(ls, gensym!(ls, "iftruerhs"), RHS, elementbytes, position)
5353
add_andblock!(ls, condop, LHS, rhsop, elementbytes, position)
5454
end
5555
function add_andblock!(ls::LoopSet, condop::Operation, LHS, RHS, elementbytes::Int, position::Int)
5656
rhsop = getop(ls, RHS, elementbytes)
5757
add_andblock!(ls, condop, LHS, rhsop, elementbytes, position)
5858
end
5959
function add_andblock!(ls::LoopSet, condexpr::Expr, condeval::Expr, elementbytes::Int, position::Int)
60-
condop = add_operation!(ls, gensym(:mask), condexpr, elementbytes, position)
60+
condop = add_operation!(ls, gensym!(ls, "mask"), condexpr, elementbytes, position)
6161
if condeval.head === :call
6262
@assert first(condeval.args) === :setindex!
6363
array, raw_indices = ref_from_setindex!(ls, condeval)
@@ -90,15 +90,15 @@ function add_orblock!(ls::LoopSet, condop::Operation, LHS, rhsop::Operation, ele
9090
end
9191
end
9292
function add_orblock!(ls::LoopSet, condop::Operation, LHS, RHS::Expr, elementbytes::Int, position::Int)
93-
rhsop = add_compute!(ls, gensym(:iffalserhs), RHS, elementbytes, position)
93+
rhsop = add_compute!(ls, gensym!(ls, "iffalserhs"), RHS, elementbytes, position)
9494
add_orblock!(ls, condop, LHS, rhsop, elementbytes, position)
9595
end
9696
function add_orblock!(ls::LoopSet, condop::Operation, LHS, RHS, elementbytes::Int, position::Int)
9797
rhsop = getop(ls, RHS, elementbytes)
9898
add_orblock!(ls, condop, LHS, rhsop, elementbytes, position)
9999
end
100100
function add_orblock!(ls::LoopSet, condexpr::Expr, condeval::Expr, elementbytes::Int, position::Int)
101-
condop = add_operation!(ls, gensym(:mask), condexpr, elementbytes, position)
101+
condop = add_operation!(ls, gensym!(ls, "mask"), condexpr, elementbytes, position)
102102
if condeval.head === :call
103103
@assert first(condeval.args) === :setindex!
104104
array, raw_indices = ref_from_setindex!(ls, condeval)

src/add_stores.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ end
1919
function add_copystore!(
2020
ls::LoopSet, parent::Operation, mpref::ArrayReferenceMetaPosition, elementbytes::Int
2121
)
22-
op = add_compute!(ls, gensym(), :identity, [parent], elementbytes)
22+
op = add_compute!(ls, gensym!(ls, "identity"), :identity, [parent], elementbytes)
2323
# pushfirst!(mpref.parents, parent)
2424
add_store!(ls, mpref, elementbytes, op)
2525
end

src/broadcast.jl

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -102,13 +102,13 @@ function add_broadcast!(
102102
@nospecialize(prod::Type{<:Product}), elementbytes::Int
103103
)
104104
A, B = prod.parameters
105-
K = gensym(:K)
106-
mA = gensym(:Aₘₖ)
107-
mB = gensym(:Bₖₙ)
105+
K = gensym!(ls, "K")
106+
mA = gensym!(ls, "Aₘₖ")
107+
mB = gensym!(ls, "Bₖₙ")
108108
pushprepreamble!(ls, Expr(:(=), mA, Expr(:(.), bcname, QuoteNode(:a))))
109109
pushprepreamble!(ls, Expr(:(=), mB, Expr(:(.), bcname, QuoteNode(:b))))
110110
pushprepreamble!(ls, Expr(:(=), K, Expr(:macrocall, Symbol("@inbounds"), LineNumberNode(@__LINE__,Symbol(@__FILE__)), Expr(:ref, Expr(:call, :size, mB), 1))))
111-
k = gensym(:k)
111+
k = gensym!(ls, "k")
112112
add_loop!(ls, Loop(k, 1, K), k)
113113
m = loopsyms[1];
114114
if numdims(B) == 1
@@ -126,17 +126,17 @@ function add_broadcast!(
126126
throw("B must be a vector or matrix.")
127127
end
128128
# load A
129-
# loadA = add_load!(ls, gensym(:A), productref(A, mA, m, k), elementbytes)
130-
loadA = add_broadcast!(ls, gensym(:A), mA, Symbol[m,k], A, elementbytes)
129+
# loadA = add_load!(ls, gensym!(ls, :A), productref(A, mA, m, k), elementbytes)
130+
loadA = add_broadcast!(ls, gensym!(ls, "A"), mA, Symbol[m,k], A, elementbytes)
131131
# load B
132-
loadB = add_broadcast!(ls, gensym(:B), mB, bloopsyms, B, elementbytes)
132+
loadB = add_broadcast!(ls, gensym!(ls, "B"), mB, bloopsyms, B, elementbytes)
133133
# set Cₘₙ = 0
134134
# setC = add_constant!(ls, zero(promote_type(recursive_eltype(A), recursive_eltype(B))), cloopsyms, mC, elementbytes)
135135
# targetC will be used for reduce_to_add
136-
mCt = gensym(mC)
137-
targetC = add_constant!(ls, gensym(:zero), cloopsyms, mCt, elementbytes, :numericconstant)
136+
mCt = gensym!(ls, mC)
137+
targetC = add_constant!(ls, gensym!(ls, "zero"), cloopsyms, mCt, elementbytes, :numericconstant)
138138
push!(ls.preamble_zeros, (identifier(targetC), IntOrFloat))
139-
setC = add_constant!(ls, gensym(:zero), cloopsyms, mC, elementbytes, :numericconstant)
139+
setC = add_constant!(ls, gensym!(ls, "zero"), cloopsyms, mC, elementbytes, :numericconstant)
140140
push!(ls.preamble_zeros, (identifier(setC), IntOrFloat))
141141
setC.reduced_children = kvec
142142
# compute Cₘₙ += Aₘₖ * Bₖₙ
@@ -226,7 +226,7 @@ function LowDimArray{D}(data::A) where {D,T,N,A <: AbstractArray{T,N}}
226226
LowDimArray{D,T,N,A}(data)
227227
end
228228
function extract_all_1_array!(ls::LoopSet, bcname::Symbol, N::Int, elementbytes::Int)
229-
refextract = gensym(bcname)
229+
refextract = gensym!(ls, bcname)
230230
ref = Expr(:ref, bcname); foreach(_ -> push!(ref.args, :begin), 1:N)
231231
pushprepreamble!(ls, Expr(:(=), refextract, ref))
232232
return add_constant!(ls, refextract, elementbytes) # or replace elementbytes with sizeof(T) ? u
@@ -247,7 +247,7 @@ end
247247
function add_broadcast_adjoint_array!(
248248
ls::LoopSet, destname::Symbol, bcname::Symbol, loopsyms::Vector{Symbol}, ::Type{A}, elementbytes::Int
249249
) where {T,N,A<:AbstractArray{T,N}}
250-
parent = gensym(:parent)
250+
parent = gensym!(ls, "parent")
251251
pushprepreamble!(ls, Expr(:(=), parent, Expr(:call, :parent, bcname)))
252252
# isone(length(loopsyms)) && return extract_all_1_array!(ls, bcname, N, elementbytes)
253253
ref = ArrayReference(parent, Symbol[loopsyms[N + 1 - n] for n 1:N])
@@ -257,7 +257,7 @@ function add_broadcast_adjoint_array!(
257257
ls::LoopSet, destname::Symbol, bcname::Symbol, loopsyms::Vector{Symbol}, ::Type{<:AbstractVector}, elementbytes::Int
258258
)
259259
# isone(length(loopsyms)) && return extract_all_1_array!(ls, bcname, N, elementbytes)
260-
parent = gensym(:parent)
260+
parent = gensym!(ls, "parent")
261261
pushprepreamble!(ls, Expr(:(=), parent, Expr(:call, :parent, bcname)))
262262

263263
ref = ArrayReference(parent, Symbol[loopsyms[2]])
@@ -289,7 +289,7 @@ end
289289
function add_broadcast!(
290290
ls::LoopSet, ::Symbol, bcname::Symbol, loopsyms::Vector{Symbol}, ::Type{Base.RefValue{T}}, elementbytes::Int
291291
) where {T}
292-
refextract = gensym(bcname)
292+
refextract = gensym!(ls, bcname)
293293
pushprepreamble!(ls, Expr(:(=), refextract, Expr(:ref, bcname)))
294294
add_constant!(ls, refextract, elementbytes) # or replace elementbytes with sizeof(T) ? u
295295
end
@@ -310,7 +310,7 @@ function add_broadcast!(
310310
)
311311
S,_,F,A = B.parameters
312312
instr = get(FUNCTIONSYMBOLS, F) do
313-
f = gensym(:func)
313+
f = gensym!(ls, "func")
314314
pushprepreamble!(ls, Expr(:(=), f, Expr(:(.), bcname, QuoteNode(:f))))
315315
Instruction(bcname, f)
316316
end
@@ -322,10 +322,10 @@ function add_broadcast!(
322322
deps = Symbol[]
323323
# reduceddeps = Symbol[]
324324
for (i,arg) enumerate(args)
325-
argname = gensym(:arg)
325+
argname = gensym!(ls, "arg")
326326
pushprepreamble!(ls, Expr(:(=), argname, Expr(:macrocall, Symbol("@inbounds"), LineNumberNode(@__LINE__,Symbol(@__FILE__)), Expr(:ref, bcargs, i))))
327327
# dynamic dispatch
328-
parent = add_broadcast!(ls, gensym(:temp), argname, loopsyms, arg, elementbytes)::Operation
328+
parent = add_broadcast!(ls, gensym!(ls, "temp"), argname, loopsyms, arg, elementbytes)::Operation
329329
push!(parents, parent)
330330
mergesetdiffv!(deps, loopdependencies(parent), reduceddependencies(parent))
331331
end
@@ -343,12 +343,12 @@ end
343343
# we have an N dimensional loop.
344344
# need to construct the LoopSet
345345
# @show typeof(dest)
346-
loopsyms = [gensym(:n) for n 1:N]
347346
ls = LoopSet(Mod)
347+
loopsyms = [gensym!(ls, "n") for n 1:N]
348348
ls.isbroadcast[] = true
349349
sizes = Expr(:tuple)
350350
for (n,itersym) enumerate(loopsyms)
351-
Nsym = gensym(:N)
351+
Nsym = gensym!(ls, "N")
352352
add_loop!(ls, Loop(itersym, 1, Nsym), itersym)
353353
push!(sizes.args, Nsym)
354354
end
@@ -377,13 +377,13 @@ end
377377
) where {T <: NativeTypes, N, A <: AbstractArray{T,N}, BC <: Union{Broadcasted,Product}, Mod}
378378
# we have an N dimensional loop.
379379
# need to construct the LoopSet
380-
loopsyms = [gensym(:n) for n 1:N]
381380
ls = LoopSet(Mod)
381+
loopsyms = [gensym!(ls, "n") for n 1:N]
382382
ls.isbroadcast[] = true
383383
pushprepreamble!(ls, Expr(:(=), :dest, Expr(:call, :parent, :dest′)))
384384
sizes = Expr(:tuple)
385385
for (n,itersym) enumerate(loopsyms)
386-
Nsym = gensym(:N)
386+
Nsym = gensym!(ls, "N")
387387
add_loop!(ls, Loop(itersym, 1, Nsym), itersym)
388388
push!(sizes.args, Nsym)
389389
end

0 commit comments

Comments
 (0)