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

Split should_rewrite_ft for call and invoke expressions, and overlay Base._unique_dims #505

Merged
merged 9 commits into from
Jan 15, 2025
25 changes: 21 additions & 4 deletions src/utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ function has_ancestor(query::Module, target::Module)
end
end

function should_rewrite_ft(@nospecialize(ft))
function should_rewrite_call(@nospecialize(ft))
# Don't rewrite builtin or intrinsics
if ft <: Core.IntrinsicFunction || ft <: Core.Builtin
return false
Expand Down Expand Up @@ -171,6 +171,20 @@ function should_rewrite_ft(@nospecialize(ft))
return true
end

# by default, same as `should_rewrite_call`
function should_rewrite_invoke(@nospecialize(ft), @nospecialize(args))
Core.println(Core.stderr, "[should_rewrite_invoke] ft = $ft, parameters = $(args)")
Copy link
Contributor

Choose a reason for hiding this comment

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

[JuliaFormatter] reported by reviewdog 🐶

Suggested change
function should_rewrite_invoke(@nospecialize(ft), @nospecialize(args))
Core.println(Core.stderr, "[should_rewrite_invoke] ft = $ft, parameters = $(args)")

Copy link
Member

Choose a reason for hiding this comment

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

probably remove the print?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

yeah, i'm using the prints for debugging. invokelatest is just calling the generic should_rewrite_invoke method, and not the specialized one for Base.unique.

will remove them once i fix it.

Copy link
Member

Choose a reason for hiding this comment

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

yeah I'm just suggesting to use https://github.com/JuliaLang/julia/blob/37b8b61ee0bd4b8db1b210a5389cf794a1358ed1/base/essentials.jl#L1062 instead where world is interp.world

return should_rewrite_call(ft)
end

function should_rewrite_invoke(::typeof(Base.unique), @nospecialize(args))
Core.println(Core.stderr, "[should_rewrite_invoke] Base.unique catched!")
if args === Tuple{Vector{Symbol}}
return false
end
return true
end

# Avoid recursively interpreting into methods we define explicitly
# as overloads, which we assume should handle the entirety of the
# translation (and if not they can use call_in_reactant).
Expand Down Expand Up @@ -231,7 +245,7 @@ function rewrite_inst(inst, ir, interp, RT, guaranteed_error)
end
if ft == typeof(Core._apply_iterate)
ft = Core.Compiler.widenconst(maybe_argextype(inst.args[3], ir))
if Base.invokelatest(should_rewrite_ft, ft)
if Base.invokelatest(should_rewrite_call, ft)
if RT === Union{}
rep = Expr(
:call,
Expand All @@ -245,7 +259,7 @@ function rewrite_inst(inst, ir, interp, RT, guaranteed_error)
return true, rep, Any
end
end
elseif Base.invokelatest(should_rewrite_ft, ft)
elseif Base.invokelatest(should_rewrite_call, ft)
if RT === Union{}
rep = Expr(:call, call_with_reactant, MustThrowError(), inst.args...)
return true, rep, Union{}
Expand All @@ -259,10 +273,13 @@ function rewrite_inst(inst, ir, interp, RT, guaranteed_error)
omi = inst.args[1]::Core.MethodInstance
sig = omi.specTypes
ft = sig.parameters[1]
argsig = sig.parameters[2:end]
if ft == typeof(Core.kwcall)
ft = sig.parameters[3]
argsig = sig.parameters[4:end]
end
if Base.invokelatest(should_rewrite_ft, ft) && !is_reactant_method(omi)
argsig = Core.apply_type(Core.Tuple, argsig...)
Copy link
Member

Choose a reason for hiding this comment

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

I'd probably be lazy and do Tuple{argsize...} but sure

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

it's the same right?

Copy link
Member

Choose a reason for hiding this comment

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

yeah

if Base.invokelatest(should_rewrite_invoke, ft, argsig) && !is_reactant_method(omi)
method = omi.def::Core.Method

min_world = Ref{UInt}(typemin(UInt))
Expand Down
2 changes: 1 addition & 1 deletion test/compile.jl
Original file line number Diff line number Diff line change
Expand Up @@ -128,7 +128,7 @@ end
@test !occursin("add", repr(hlo))
end

# While a bit specific, the following is used to check for a bug in `should_rewrite_ft`
# While a bit specific, the following is used to check for a bug in `should_rewrite_call`
function sinusoidal_embedding(
x::AbstractArray{T,4}, min_freq, max_freq, embedding_dims::Int
) where {T}
Expand Down
Loading