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

Overloading all functions #60

Open
MilesCranmer opened this issue Jan 12, 2025 · 2 comments
Open

Overloading all functions #60

MilesCranmer opened this issue Jan 12, 2025 · 2 comments

Comments

@MilesCranmer
Copy link

MilesCranmer commented Jan 12, 2025

@aviatesk

I'm trying to move BorrowChecker.jl to use this over Cassette.jl but am having some difficulties figuring out how to do generic overlay passes where I overlay behavior on all functions that get hit.

Here's my current attempt, from studying the code:

using CassetteOverlay
using CassetteOverlay: CassetteOverlayGenerator
using Test

@MethodTable AllMethods

pass = @overlaypass AllMethods
# ^Just to initialize the object

_sin(x) = sin(x)

# Now, we attempt to do a generic override of it:
@eval function (p::typeof(pass))(f, args...)
    $(Expr(:meta, :generated, CassetteOverlayGenerator(:pass, :fargs)))
    println("Hello from ", f, " with args: ", args)
    return f(args...)
end

pass(() -> _sin(0.5));

This prints:

Hello from #2 with args: ()

So it seems this captures the first function (the closure), but the f(args...) does not hit the same overlay pass again.

Another thing I tried was

@overlay AllMethods (f::Function)(args...) = ...

but faced similar issues.

How can I do this? I basically want to overload all functions matching a specific signature.

@aviatesk
Copy link
Member

CassetteOverlay rewrites all function calls (via the mechanism implemented in CassetteBase.jl) but the rewriting mechanism relies on method matching against the overlay table. So it's probably not suitable for what you want to achieve.

It might be possible to implement something similar to what Cassette.jl does with CassetteBase.jl.

@MilesCranmer
Copy link
Author

Thanks, how would I use CassetteBase for that?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants