Skip to content

Commit

Permalink
Merge branch 'master' into stabilize-quicksort
Browse files Browse the repository at this point in the history
  • Loading branch information
LilithHafner authored May 10, 2022
2 parents 3318c34 + 6d7bc40 commit 1a30832
Show file tree
Hide file tree
Showing 72 changed files with 934 additions and 335 deletions.
7 changes: 7 additions & 0 deletions .git-blame-ignore-revs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# .git-blame-ignore-revs
# whitespace: end text files with single newlines
3903fa54a638d4546ef50e56f91f0705a8ab11ef
# whitespace: use only UNIX line endings (\n)
e66bfa5dd32f93e76068c00ad882c1fc839c5af8
# whitespace: replace non-breaking space => space
100a741e7ab38c91d48cc929bb001afc8e09261f
3 changes: 3 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ New library functions
---------------------

* `Iterators.flatmap` was added ([#44792]).
* New helper `Splat(f)` which acts like `x -> f(x...)`, with pretty printing for
inspecting which function `f` was originally wrapped. ([#42717])

Library changes
---------------
Expand Down Expand Up @@ -120,6 +122,7 @@ Standard library changes
Deprecated or removed
---------------------

* Unexported `splat` is deprecated in favor of exported `Splat`, which has pretty printing of the wrapped function. ([#42717])

External dependencies
---------------------
Expand Down
4 changes: 1 addition & 3 deletions base/Enums.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,8 +151,7 @@ macro enum(T::Union{Symbol,Expr}, syms...)
values = Vector{basetype}()
seen = Set{Symbol}()
namemap = Dict{basetype,Symbol}()
lo = hi = 0
i = zero(basetype)
lo = hi = i = zero(basetype)
hasexpr = false

if length(syms) == 1 && syms[1] isa Expr && syms[1].head === :block
Expand Down Expand Up @@ -193,7 +192,6 @@ macro enum(T::Union{Symbol,Expr}, syms...)
if length(values) == 1
lo = hi = i
else
lo = min(lo, i)
hi = max(hi, i)
end
i += oneunit(i)
Expand Down
14 changes: 11 additions & 3 deletions base/abstractarray.jl
Original file line number Diff line number Diff line change
Expand Up @@ -914,9 +914,17 @@ end

function copyto!(dest::AbstractArray, dstart::Integer, src)
i = Int(dstart)
for x in src
dest[i] = x
i += 1
if haslength(src) && length(dest) > 0
@boundscheck checkbounds(dest, i:(i + length(src) - 1))
for x in src
@inbounds dest[i] = x
i += 1
end
else
for x in src
dest[i] = x
i += 1
end
end
return dest
end
Expand Down
6 changes: 3 additions & 3 deletions base/boot.jl
Original file line number Diff line number Diff line change
Expand Up @@ -111,8 +111,8 @@
# module::Module
# method::Symbol
# file::Symbol
# line::Int
# inlined_at::Int
# line::Int32
# inlined_at::Int32
#end

#struct GotoNode
Expand Down Expand Up @@ -410,7 +410,7 @@ eval(Core, quote
isa(f, String) && (f = Symbol(f))
return $(Expr(:new, :LineNumberNode, :l, :f))
end
LineInfoNode(mod::Module, @nospecialize(method), file::Symbol, line::Int, inlined_at::Int) =
LineInfoNode(mod::Module, @nospecialize(method), file::Symbol, line::Int32, inlined_at::Int32) =
$(Expr(:new, :LineInfoNode, :mod, :method, :file, :line, :inlined_at))
GlobalRef(m::Module, s::Symbol) = $(Expr(:new, :GlobalRef, :m, :s))
SlotNumber(n::Int) = $(Expr(:new, :SlotNumber, :n))
Expand Down
8 changes: 8 additions & 0 deletions base/checked.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

# Support for checked integer arithmetic

"""
Checked
The Checked module provides arithmetic functions for the built-in signed and unsigned
Integer types which throw an error when an overflow occurs. They are named like `checked_sub`,
`checked_div`, etc. In addition, `add_with_overflow`, `sub_with_overflow`, `mul_with_overflow`
return both the unchecked results and a boolean value denoting the presence of an overflow.
"""
module Checked

export checked_neg, checked_abs, checked_add, checked_sub, checked_mul,
Expand Down
6 changes: 4 additions & 2 deletions base/compiler/compiler.jl
Original file line number Diff line number Diff line change
Expand Up @@ -141,8 +141,10 @@ include("compiler/abstractinterpretation.jl")
include("compiler/typeinfer.jl")
include("compiler/optimize.jl") # TODO: break this up further + extract utilities

# required for bootstrap
# TODO: find why this is needed and remove it.
# required for bootstrap because sort.jl uses extrema
# to decide whether to dispatch to counting sort.
#
# TODO: remove it.
function extrema(x::Array)
isempty(x) && throw(ArgumentError("collection must be non-empty"))
vmin = vmax = x[1]
Expand Down
89 changes: 48 additions & 41 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -554,29 +554,36 @@ function run_passes(ci::CodeInfo, sv::OptimizationState, caller::InferenceResult
end

function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
code = copy_exprargs(ci.code)
linetable = ci.linetable
if !isa(linetable, Vector{LineInfoNode})
linetable = collect(LineInfoNode, linetable::Vector{Any})::Vector{LineInfoNode}
end

# check if coverage mode is enabled
coverage = coverage_enabled(sv.mod)
# Go through and add an unreachable node after every
# Union{} call. Then reindex labels.
idx = 1
oldidx = 1
changemap = fill(0, length(code))
prevloc = zero(eltype(ci.codelocs))
stmtinfo = sv.stmt_info
codelocs = ci.codelocs
ssavaluetypes = ci.ssavaluetypes::Vector{Any}
ssaflags = ci.ssaflags
if !coverage && JLOptions().code_coverage == 3 # path-specific coverage mode
for line in ci.linetable
line = line::LineInfoNode
for line in linetable
if is_file_tracked(line.file)
# if any line falls in a tracked file enable coverage for all
coverage = true
break
end
end
end
labelmap = coverage ? fill(0, length(code)) : changemap

# Go through and add an unreachable node after every
# Union{} call. Then reindex labels.
code = copy_exprargs(ci.code)
stmtinfo = sv.stmt_info
codelocs = ci.codelocs
ssavaluetypes = ci.ssavaluetypes::Vector{Any}
ssaflags = ci.ssaflags
meta = Expr[]
idx = 1
oldidx = 1
ssachangemap = fill(0, length(code))
labelchangemap = coverage ? fill(0, length(code)) : ssachangemap
prevloc = zero(eltype(ci.codelocs))
while idx <= length(code)
codeloc = codelocs[idx]
if coverage && codeloc != prevloc && codeloc != 0
Expand All @@ -586,9 +593,9 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
insert!(ssavaluetypes, idx, Nothing)
insert!(stmtinfo, idx, nothing)
insert!(ssaflags, idx, IR_FLAG_NULL)
changemap[oldidx] += 1
if oldidx < length(labelmap)
labelmap[oldidx + 1] += 1
ssachangemap[oldidx] += 1
if oldidx < length(labelchangemap)
labelchangemap[oldidx + 1] += 1
end
idx += 1
prevloc = codeloc
Expand All @@ -601,30 +608,27 @@ function convert_to_ircode(ci::CodeInfo, sv::OptimizationState)
insert!(ssavaluetypes, idx + 1, Union{})
insert!(stmtinfo, idx + 1, nothing)
insert!(ssaflags, idx + 1, ssaflags[idx])
if oldidx < length(changemap)
changemap[oldidx + 1] += 1
coverage && (labelmap[oldidx + 1] += 1)
if oldidx < length(ssachangemap)
ssachangemap[oldidx + 1] += 1
coverage && (labelchangemap[oldidx + 1] += 1)
end
idx += 1
end
end
idx += 1
oldidx += 1
end
renumber_ir_elements!(code, changemap, labelmap)

meta = Expr[]
renumber_ir_elements!(code, ssachangemap, labelchangemap)

for i = 1:length(code)
code[i] = process_meta!(meta, code[i])
end
strip_trailing_junk!(ci, code, stmtinfo)
cfg = compute_basic_blocks(code)
types = Any[]
stmts = InstructionStream(code, types, stmtinfo, codelocs, ssaflags)
linetable = ci.linetable
isa(linetable, Vector{LineInfoNode}) || (linetable = collect(LineInfoNode, linetable::Vector{Any}))
ir = IRCode(stmts, cfg, linetable, sv.slottypes, meta, sv.sptypes)
return ir
cfg = compute_basic_blocks(code)
return IRCode(stmts, cfg, linetable, sv.slottypes, meta, sv.sptypes)
end

function process_meta!(meta::Vector{Expr}, @nospecialize stmt)
Expand Down Expand Up @@ -788,31 +792,33 @@ function statement_costs!(cost::Vector{Int}, body::Vector{Any}, src::Union{CodeI
return maxcost
end

function renumber_ir_elements!(body::Vector{Any}, changemap::Vector{Int})
return renumber_ir_elements!(body, changemap, changemap)
function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int})
return renumber_ir_elements!(body, ssachangemap, ssachangemap)
end

function cumsum_ssamap!(ssamap::Vector{Int})
function cumsum_ssamap!(ssachangemap::Vector{Int})
any_change = false
rel_change = 0
for i = 1:length(ssamap)
rel_change += ssamap[i]
if ssamap[i] == -1
for i = 1:length(ssachangemap)
val = ssachangemap[i]
any_change |= val 0
rel_change += val
if val == -1
# Keep a marker that this statement was deleted
ssamap[i] = typemin(Int)
ssachangemap[i] = typemin(Int)
else
ssamap[i] = rel_change
ssachangemap[i] = rel_change
end
end
return any_change
end

function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int}, labelchangemap::Vector{Int})
cumsum_ssamap!(labelchangemap)
any_change = cumsum_ssamap!(labelchangemap)
if ssachangemap !== labelchangemap
cumsum_ssamap!(ssachangemap)
end
if labelchangemap[end] == 0 && ssachangemap[end] == 0
return
any_change |= cumsum_ssamap!(ssachangemap)
end
any_change || return
for i = 1:length(body)
el = body[i]
if isa(el, GotoNode)
Expand All @@ -822,7 +828,8 @@ function renumber_ir_elements!(body::Vector{Any}, ssachangemap::Vector{Int}, lab
if isa(cond, SSAValue)
cond = SSAValue(cond.id + ssachangemap[cond.id])
end
body[i] = GotoIfNot(cond, el.dest + labelchangemap[el.dest])
was_deleted = labelchangemap[el.dest] == typemin(Int)
body[i] = was_deleted ? cond : GotoIfNot(cond, el.dest + labelchangemap[el.dest])
elseif isa(el, ReturnNode)
if isdefined(el, :val)
val = el.val
Expand Down
4 changes: 2 additions & 2 deletions base/compiler/ssair/inlining.jl
Original file line number Diff line number Diff line change
Expand Up @@ -315,11 +315,11 @@ function ir_inline_item!(compact::IncrementalCompact, idx::Int, argexprs::Vector
def = item.mi.def::Method
linetable_offset::Int32 = length(linetable)
# Append the linetable of the inlined function to our line table
inlined_at = Int(compact.result[idx][:line])
inlined_at = compact.result[idx][:line]
topline::Int32 = linetable_offset + Int32(1)
coverage = coverage_enabled(def.module)
coverage_by_path = JLOptions().code_coverage == 3
push!(linetable, LineInfoNode(def.module, def.name, def.file, Int(def.line), inlined_at))
push!(linetable, LineInfoNode(def.module, def.name, def.file, def.line, inlined_at))
oldlinetable = spec.ir.linetable
for oldline in 1:length(oldlinetable)
entry = oldlinetable[oldline]
Expand Down
33 changes: 19 additions & 14 deletions base/compiler/ssair/ir.jl
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,20 @@ function first_insert_for_bb(code, cfg::CFG, block::Int)
error("any insert position isn't found")
end

# SSA values that need renaming
struct OldSSAValue
id::Int
end

# SSA values that are in `new_new_nodes` of an `IncrementalCompact` and are to
# be actually inserted next time (they become `new_nodes` next time)
struct NewSSAValue
id::Int
end

const AnySSAValue = Union{SSAValue, OldSSAValue, NewSSAValue}


# SSA-indexed nodes

struct NewInstruction
Expand Down Expand Up @@ -253,6 +267,10 @@ function setindex!(is::InstructionStream, newval::Instruction, idx::Int)
is.flag[idx] = newval[:flag]
return is
end
function setindex!(is::InstructionStream, newval::AnySSAValue, idx::Int)
is.inst[idx] = newval
return is
end
function setindex!(node::Instruction, newval::Instruction)
node.data[node.idx] = newval
return node
Expand Down Expand Up @@ -312,7 +330,7 @@ function getindex(x::IRCode, s::SSAValue)
end
end

function setindex!(x::IRCode, repl::Instruction, s::SSAValue)
function setindex!(x::IRCode, repl::Union{Instruction, AnySSAValue}, s::SSAValue)
if s.id <= length(x.stmts)
x.stmts[s.id] = repl
else
Expand All @@ -321,19 +339,6 @@ function setindex!(x::IRCode, repl::Instruction, s::SSAValue)
return x
end

# SSA values that need renaming
struct OldSSAValue
id::Int
end

# SSA values that are in `new_new_nodes` of an `IncrementalCompact` and are to
# be actually inserted next time (they become `new_nodes` next time)
struct NewSSAValue
id::Int
end

const AnySSAValue = Union{SSAValue, OldSSAValue, NewSSAValue}

mutable struct UseRefIterator
stmt::Any
relevant::Bool
Expand Down
24 changes: 15 additions & 9 deletions base/compiler/typeinfer.jl
Original file line number Diff line number Diff line change
Expand Up @@ -670,20 +670,27 @@ function type_annotate!(sv::InferenceState, run_optimizer::Bool)
body = src.code::Array{Any,1}
nexpr = length(body)

# replace GotoIfNot with its condition if the branch target is unreachable
for i = 1:nexpr
expr = body[i]
if isa(expr, GotoIfNot)
if !isa(states[expr.dest], VarTable)
body[i] = Expr(:call, GlobalRef(Core, :typeassert), expr.cond, GlobalRef(Core, :Bool))
# eliminate GotoIfNot if either of branch target is unreachable
if run_optimizer
for idx = 1:nexpr
stmt = body[idx]
if isa(stmt, GotoIfNot) && widenconst(argextype(stmt.cond, src, sv.sptypes)) === Bool
# replace live GotoIfNot with:
# - GotoNode if the fallthrough target is unreachable
# - no-op if the branch target is unreachable
if states[idx+1] === nothing
body[idx] = GotoNode(stmt.dest)
elseif states[stmt.dest] === nothing
body[idx] = nothing
end
end
end
end

# dead code elimination for unreachable regions
i = 1
oldidx = 0
changemap = fill(0, nexpr)

while i <= nexpr
oldidx += 1
st_i = states[i]
Expand Down Expand Up @@ -718,7 +725,6 @@ function type_annotate!(sv::InferenceState, run_optimizer::Bool)
end
i += 1
end

if run_optimizer
renumber_ir_elements!(body, changemap)
end
Expand Down Expand Up @@ -944,7 +950,7 @@ function typeinf_ext(interp::AbstractInterpreter, mi::MethodInstance)
tree.slotflags = fill(IR_FLAG_NULL, nargs)
tree.ssavaluetypes = 1
tree.codelocs = Int32[1]
tree.linetable = [LineInfoNode(method.module, method.name, method.file, Int(method.line), 0)]
tree.linetable = [LineInfoNode(method.module, method.name, method.file, method.line, Int32(0))]
tree.inferred = true
tree.ssaflags = UInt8[0]
tree.pure = true
Expand Down
Loading

0 comments on commit 1a30832

Please sign in to comment.