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

RFC: support external compiler passes #35015

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
24 changes: 24 additions & 0 deletions base/compiler/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,30 @@ end
function optimize(opt::OptimizationState, @nospecialize(result))
def = opt.linfo.def
nargs = Int(opt.nargs) - 1
# Execute external passes
modified = true
while modified
modified = false
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, i))
modified = true
break
end
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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

because this doesn't actually do anything to do with avx it would be clearer maybe to call this
demo_pass and @with_demo_pass ?

Copy link
Sponsor Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we go with the "comment out" option this will be deleted anyway. But yes, for the demo we need better names.

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)