-
-
Notifications
You must be signed in to change notification settings - Fork 5.7k
Support other forms of quote node in allocated macro function elision #58292
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
Conversation
|
Is this the right fix? x-ref #58057 (comment) julia> import MathOptInterface as MOI
julia> using Test
julia> function my_is_simply_call(Base.@nospecialize ex)
Meta.isexpr(ex, :call) || return false
for a in ex.args
a isa QuoteNode && continue
a isa Symbol && continue
Base.is_self_quoting(a) && continue
Base.is_quoted(a) && continue
return false
end
return true
end
my_is_simply_call (generic function with 1 method)
julia> macro my_allocated(ex)
ex = macroexpand(__module__, ex)
if !my_is_simply_call(ex)
ex = :((() -> $ex)())
end
pushfirst!(ex.args, Base.GlobalRef(Base, :allocated))
return esc(ex)
end
@my_allocated (macro with 1 method)
julia> function test_main_captured_x()
r = MOI.Nonlinear.OperatorRegistry()
x = [1.1, 1.0]
H = zeros(2, 2)
ret = @my_allocated MOI.Nonlinear.eval_multivariate_hessian(r, :^, H, x)
x = [1.1, 2.0]
return ret
end
test_main_captured_x (generic function with 1 method)
julia> test_main_captured_x()
112
julia> test_main_captured_x()
112 |
|
Yes, but you're still not allowed to have highly complicated expressions with arbitrary side-effects ( |
I'm not sure I understand this part. The code does not allocate if I remove the |
|
Maybe we should just make |
|
Perhaps most of the time, but it would be a breaking change for several packages (not all packages measure a call expression) |
… macro optimization Refs #58057
|
Fair enough. But the unintuitive thing here is that you get 0 as expected. Maybe the right heuristic is just "is the expression a call"? Then if you really want to measure everything, all you need to do is wrap it in begin...end. |
Cause it to capture all values of any expression (even values of globals), not just values passed to a single call.
|
Shoot, if it needs a test and all that, this PR isn't worth it anymore. There's too many cases in the wild already doing various things. The PR currently breaks because we don't know that things are globals, and capturing those as arguments fails very badly for inference if they turn out to have been a Module. |
Refs #58057