Why don't macros and modules also have backedges to allow "invalidating" dependent code?

Benny
Sat, 21 May 2022 10:07:32 +0000
Internals & Design

Something cool that Julia does is method invalidation. When a method is compiled to a MethodInstance upon a call, assumptions like what methods are dispatched to and compile-time constants are baked in. When a function is edited by adding or replacing a method, some of those assumptions will no longer be valid, so Julia invalidates the affected MethodInstances to allow future calls to recompile. Moreover, MethodInstances store backedges (links) to dependent MethodInstances, so those will also be invalidated (if needed).
Minimal example of how replacing a callee method causes an invalidation of the callee and a caller. (click for more details) Analogous macro counterexample. (click for more details) Analogous module counterexample. Note that you can accomplish the intended effect by editing the function in the module instead of replacing the module. (click for more details)
Analogous invalidation of callers via backedges doesn’t happen when macros are redefined or when modules are replaced. Of course, the things that would be “invalidated” are completely different from compiled MethodInstances; in the macro case, the caller method itself would need to be reevaluated, and in the module case, the entire Main module and its constants would need to be reevaluated.

But is there a reason this isn’t done? The number of module definitions and macro calls don’t seem to be significantly larger than method calls, so making backedges from macros and modules to methods and other modules seems plausible. Reevaluating modules can also replace global instances after modifying type definitions (though unlike macros, I’m not sure if it’s necessary to reevaluate dependent methods because I don’t know if the actual type or just the symbol is stored for calls in the method body or annotations of the arguments). Although a lot can be reevaluated, it seems less than or equal to a full restart. There has to be a hazard to reevaluation I’m not seeing.

1 post - 1 participant