Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow user to override the default values for optimize #37

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions src/analysis.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,8 @@ end

# global variables

function globals(warn, call)
c = code(call)[1]
function globals(warn, call; optimize::Bool=false)
c = code(call; optimize=optimize)[1]
eachline(c) do line, ex
ex isa Expr || return
for ref in ex.args
Expand All @@ -104,8 +104,8 @@ end

# fields

function fields(warn, call)
c = code(call)[1]
function fields(warn, call; optimize::Bool=false)
c = code(call; optimize=optimize)[1]
eachline(c) do line, x
(isexpr(x, :(=)) && isexpr(x.args[2], :call) &&
rebuild(c, x.args[2].args[1]) == GlobalRef(Core,:getfield)) ||
Expand All @@ -132,8 +132,8 @@ function assignments(code, l = -1)
return assigns
end

function locals(warn, call)
c = code(call)[1]
function locals(warn, call; optimize::Bool=false)
c = code(call; optimize=optimize)[1]
as = assignments(c)
for (x, as) in as
(length(unique(map(x->x[2],as))) == 1 && (isconcretetype(as[1][2]) || istype(as[1][2]))) && continue
Expand All @@ -149,8 +149,8 @@ end

rebuild(code, x::Core.SSAValue) = rebuild(code, code.code[x.id])

function dispatch(warn, call)
c = code(call, optimize = true)[1]
function dispatch(warn, call; optimize::Bool=true)
c = code(call, optimize = optimize)[1]
eachline(c) do line, ex
isexpr(ex, :(=)) && (ex = ex.args[2])
isexpr(ex, :call) || return
Expand All @@ -175,8 +175,8 @@ istype(::Type{T}) where T = true
istype(::Union) = false
istype(x) = false

function rettype(warn, call)
c, out = code(call)
function rettype(warn, call; optimize::Bool=false)
c, out = code(call, optimize = optimize)

if out == Any || !(issmallunion(out) || isconcretetype(out) || istype(out))
warn(call, method(call).line, "$(call.f) returns $out")
Expand All @@ -185,10 +185,17 @@ end

# overall analysis

function analyse(warn, call)
globals(warn, call)
locals(warn, call)
fields(warn, call)
dispatch(warn, call)
rettype(warn, call)
function analyse(warn,
call;
optimize::Bool=false,
optimize_globals::Bool=optimize,
optimize_locals::Bool=optimize,
optimize_fields::Bool=optimize,
optimize_dispatch::Bool=true,
optimize_rettype::Bool=optimize,)
globals(warn, call; optimize=optimize_globals)
locals(warn, call; optimize=optimize_locals)
fields(warn, call; optimize=optimize_fields)
dispatch(warn, call; optimize=optimize_dispatch)
rettype(warn, call; optimize=optimize_rettype)
end
35 changes: 32 additions & 3 deletions src/trace.jl
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,32 @@ struct Trace
stack::Vector{Call}
warn
maxdepth::Int
optimize::Bool
optimize_globals::Bool
optimize_locals::Bool
optimize_fields::Bool
optimize_dispatch::Bool
optimize_rettype::Bool
end

function Trace(w; maxdepth=typemax(Int))
Trace(Set(), Vector{Call}(), w, maxdepth)
function Trace(w;
maxdepth=typemax(Int),
optimize::Bool=false,
optimize_globals::Bool=optimize,
optimize_locals::Bool=optimize,
optimize_fields::Bool=optimize,
optimize_dispatch::Bool=true,
optimize_rettype::Bool=optimize)
Trace(Set(),
Vector{Call}(),
w,
maxdepth,
optimize,
optimize_globals,
optimize_locals,
optimize_fields,
optimize_dispatch,
optimize_rettype)
end

isprimitive(f) = f isa Core.Builtin || f isa Core.IntrinsicFunction
Expand All @@ -33,7 +55,14 @@ function Cassette.posthook(ctx::TraceurCtx, out, f, args...)
if !(f ∈ ignored_functions || T ∈ tra.seen || isprimitive(f) ||
method(C) ∈ ignored_methods || method(C).module ∈ (Core, Core.Compiler))
push!(tra.seen, T)
analyse((a...) -> tra.warn(Warning(a..., copy(tra.stack))), C)
analyse((a...) -> tra.warn(Warning(a..., copy(tra.stack))),
C;
optimize = tra.optimize,
optimize_globals = tra.optimize_globals,
optimize_locals = tra.optimize_locals,
optimize_fields = tra.optimize_fields,
optimize_dispatch = tra.optimize_dispatch,
optimize_rettype = tra.optimize_rettype)
end
pop!(tra.stack)
nothing
Expand Down