Skip to content

Commit

Permalink
RFC: support external compiler passes
Browse files Browse the repository at this point in the history
  • Loading branch information
timholy committed Mar 5, 2020
1 parent 4d9a334 commit 598eeea
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 0 deletions.
18 changes: 18 additions & 0 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,24 @@ end
function optimize(opt::OptimizationState, @nospecialize(result))
def = opt.linfo.def
nargs = Int(opt.nargs) - 1
# Execute external passes
for i = 1:length(opt.src.code)
stmt = opt.src.code[i]
if isexpr(stmt, :meta)
for j = 1:length(stmt.args)
a = stmt.args[j]
if isexpr(a, :external_pass)
f = a.args[1]
if f === :start
f = a.args[2]
end
if isa(f, Function)
Core._apply_latest(f, (opt,))
end
end
end
end
end
@timeit "optimizer" ir = run_passes(opt.src, nargs, opt)
force_noinline = _any(@nospecialize(x) -> isexpr(x, :meta) && x.args[1] === :noinline, ir.meta)

Expand Down
17 changes: 17 additions & 0 deletions test/compiler/irpasses.jl
Original file line number Diff line number Diff line change
Expand Up @@ -295,3 +295,20 @@ end
const _some_coeffs = (1,[2],3,4)
splat_from_globalref(x) = (x, _some_coeffs...,)
@test splat_from_globalref(0) == (0, 1, [2], 3, 4)

# External passes
const _opt = Ref{Any}(nothing)
avx_pass(opt) = (_opt[] = opt; opt)
macro avx(ex)
esc(Expr(:block, Expr(:meta, Expr(:external_pass, :start, avx_pass)), ex, Expr(:meta, Expr(:external_pass, :stop))))
end
function myselfdot(a)
s = zero(eltype(a))
@avx for item in a
s += item^2
end
return s
end
a = [0.1, 0.2, 0.3]
@test myselfdot(a) 0.14
@test isa(_opt[], Core.Compiler.OptimizationState)

0 comments on commit 598eeea

Please sign in to comment.