Skip to content

Commit f2e6038

Browse files
committed
Keep track of size of integer literals. Do not promote bools unnecessarilly. Fixes #228.
1 parent e3aff00 commit f2e6038

File tree

5 files changed

+44
-11
lines changed

5 files changed

+44
-11
lines changed

src/codegen/lower_constant.jl

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -181,8 +181,14 @@ function lower_licm_constants!(ls::LoopSet)
181181
# setconstantop!(ls, ops[id], sym)
182182
# setconstantop!(ls, ops[id], Expr(:call, lv(:maybeconvert), ls.T, sym))
183183
end
184-
for (id,intval) ls.preamble_symint
185-
setop!(ls, ops[id], Expr(:call, lv(:sizeequivalentint), ELTYPESYMBOL, intval))
184+
for (id,(intval,intsz,signed)) ls.preamble_symint
185+
if intsz == 1
186+
setop!(ls, ops[id], intval % Bool)
187+
elseif signed
188+
setop!(ls, ops[id], Expr(:call, lv(:sizeequivalentint), ELTYPESYMBOL, intval))
189+
else
190+
setop!(ls, ops[id], Expr(:call, lv(:sizeequivalentint), ELTYPESYMBOL, intval % UInt))
191+
end
186192
end
187193
for (id,floatval) ls.preamble_symfloat
188194
setop!(ls, ops[id], Expr(:call, lv(:sizeequivalentfloat), ELTYPESYMBOL, floatval))

src/codegen/lowering.jl

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -586,8 +586,16 @@ function typeof_outer_reduction_init(ls::LoopSet, op::Operation)
586586
for (id, sym) ls.preamble_symsym
587587
opid == id && return Expr(:call, :typeof, sym)
588588
end
589-
for (id,intval) ls.preamble_symint
590-
opid == id && return :Int
589+
for (id,(intval,intsz,signed)) ls.preamble_symint
590+
if opid == id
591+
if intsz == 1
592+
return :Bool
593+
elseif signed
594+
return Symbol(:Int,intsz)
595+
else
596+
return Symbol(:UInt,intsz)
597+
end
598+
end
591599
end
592600
for (id,floatval) ls.preamble_symfloat
593601
opid == id && return :Float64

src/modeling/graphs.jl

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -415,7 +415,7 @@ struct LoopSet
415415
preamble::Expr
416416
prepreamble::Expr # performs extractions that must be performed first, and don't need further registering
417417
preamble_symsym::Vector{Tuple{Int,Symbol}}
418-
preamble_symint::Vector{Tuple{Int,Int}}
418+
preamble_symint::Vector{Tuple{Int,Tuple{Int,Int32,Bool}}}
419419
preamble_symfloat::Vector{Tuple{Int,Float64}}
420420
preamble_zeros::Vector{Tuple{Int,NumberType}}
421421
preamble_funcofeltypes::Vector{Tuple{Int,Float64}}
@@ -504,6 +504,15 @@ function pushpreamble!(ls::LoopSet, op::Operation, v::Symbol)
504504
end
505505
nothing
506506
end
507+
508+
function integer_description(@nospecialize(v::Integer))::Tuple{Int,Int32,Bool}
509+
if v isa Bool
510+
((v % Int)::Int, one(Int32), false)
511+
else
512+
((v % Int)::Int, ((8sizeof(v))%Int32)::Int32, (v isa Signed)::Bool)
513+
end
514+
end
515+
507516
function pushpreamble!(ls::LoopSet, op::Operation, v::Number)
508517
typ = v isa Integer ? HardInt : HardFloat
509518
id = identifier(op)
@@ -512,7 +521,7 @@ function pushpreamble!(ls::LoopSet, op::Operation, v::Number)
512521
elseif isone(v)
513522
push!(ls.preamble_funcofeltypes, (id, MULTIPLICATIVE_IN_REDUCTIONS))
514523
elseif v isa Integer
515-
push!(ls.preamble_symint, (id, convert(Int,v)))
524+
push!(ls.preamble_symint, (id, integer_description(v)))
516525
else
517526
push!(ls.preamble_symfloat, (id, convert(Float64,v)))
518527
end
@@ -549,7 +558,7 @@ function LoopSet(mod::Symbol)
549558
LoopOrder(),
550559
Expr(:block),Expr(:block),
551560
Tuple{Int,Symbol}[],
552-
Tuple{Int,Int}[],
561+
Tuple{Int,Tuple{Int,Int32,Bool}}[],
553562
Tuple{Int,Float64}[],
554563
Tuple{Int,NumberType}[],Tuple{Int,Symbol}[],
555564
Symbol[], Symbol[], Symbol[],

src/parse/add_constants.jl

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ function add_constant!(ls::LoopSet, var::Number, elementbytes::Int = 8)
2121
end
2222
push!(ls.preamble_zeros, (identifier(op),typ))
2323
elseif var isa Integer
24-
for (id,ivar) ls.preamble_symint
25-
(instruction(ops[id]) === LOOPCONSTANT && ivar == var) && return ops[id]
24+
idescript = integer_description(var)
25+
for (id,descript) ls.preamble_symint
26+
if (instruction(ops[id]) === LOOPCONSTANT) && (idescript == descript)
27+
return ops[id]
28+
end
2629
end
27-
push!(ls.preamble_symint, (identifier(op), var))
30+
push!(ls.preamble_symint, (identifier(op), idescript))
2831
else#if var isa FloatX
2932
for (id,fvar) ls.preamble_symfloat
3033
(instruction(ops[id]) === LOOPCONSTANT && fvar == var) && return ops[id]

src/reconstruct_loopset.jl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -303,7 +303,14 @@ function expandbyoffset!(indexpand::Vector{T}, inds, offsets::Vector{Int}, expan
303303
ind = T === Int ? _ind : first(_ind)
304304
base = offsets[ind] + 1
305305
for inda base:(expand ? offsets[ind+1] : base)
306-
T === Int ? push!(indexpand, inda) : push!(indexpand, (inda,last(_ind)))
306+
if T === Int
307+
push!(indexpand, inda)
308+
elseif T === Tuple{Int,Tuple{Int,Int32,Bool}}
309+
li = last(_ind)
310+
push!(indexpand, (inda,(li[1],li[2],li[3])))
311+
else
312+
push!(indexpand, (inda,last(_ind)))
313+
end
307314
end
308315
end
309316
indexpand

0 commit comments

Comments
 (0)