Defer global caching of `CodeInstance` to post-optimization step (#58343)
This PR extracts the caching improvements from
https://github.com/JuliaLang/julia/pull/56687, implemented by @aviatesk.
It essentially defers global caching to the post-optimization step,
giving a temporary cache to the optimizer instead of relying on the
global cache.
The issue with caching globally before optimization is that any errors
occurring within the optimizer may leave a partially initialized
`CodeInstance` in the cache, which was meant to be updated
post-optimization. Exceptions should not be thrown in the native
compilation pipeline, but abstract interpreters extending optimization
routines will frequently encounter some during an iterative development
(see https://github.com/CedarEDA/DAECompiler.jl/issues/25).
An extra benefit from deferring global caching is that the optimizer can
then more safely update `CodeInstance`s with new inference information,
as is the intent of https://github.com/JuliaLang/julia/pull/56687.
---------
Co-authored-by: Shuhei Kadowaki <aviatesk@gmail.com>
Co-authored-by: Cody Tapscott <84105208+topolarity@users.noreply.github.com>