From 1327dfec807fa00782311f02d095b8e37ea55f2a Mon Sep 17 00:00:00 2001 From: Shuhei Kadowaki <40514306+aviatesk@users.noreply.github.com> Date: Wed, 8 Nov 2023 09:51:16 +0900 Subject: [PATCH] allow destructive inlining only when the source is volatile (#52062) Destructive inlining introduced by JuliaLang/julia#51934 implicitly presupposes that inferred `CodeInfo` source has been compressed to the `String` format when cached, which is not generally true for external `AbstractInterpreter`s that may disable the `may_compress` and/or `may_discard_trees` settings. This commit adds a safeguard to ensure the eligibility of such `CodeInfo` source propagated by `VolatileInferenceResult` for destructive inlining. --- base/compiler/ssair/inlining.jl | 2 +- base/compiler/typeinfer.jl | 2 ++ base/compiler/types.jl | 3 ++- 3 files changed, 5 insertions(+), 2 deletions(-) diff --git a/base/compiler/ssair/inlining.jl b/base/compiler/ssair/inlining.jl index db50c5142f46f..318b21b09bb16 100644 --- a/base/compiler/ssair/inlining.jl +++ b/base/compiler/ssair/inlining.jl @@ -884,7 +884,7 @@ function resolve_todo(mi::MethodInstance, result::Union{Nothing,InferenceResult, elseif isa(result, VolatileInferenceResult) inferred_result = get_local_result(result.inf_result) # volatile inference result can be inlined destructively - preserve_local_sources = OptimizationParams(state.interp).preserve_local_sources + preserve_local_sources = !result.inf_result.is_src_volatile | OptimizationParams(state.interp).preserve_local_sources else inferred_result = get_cached_result(state, mi) end diff --git a/base/compiler/typeinfer.jl b/base/compiler/typeinfer.jl index bf81c989c3155..9eef6615521ca 100644 --- a/base/compiler/typeinfer.jl +++ b/base/compiler/typeinfer.jl @@ -361,7 +361,9 @@ function transform_result_for_cache(interp::AbstractInterpreter, linfo::MethodInstance, valid_worlds::WorldRange, result::InferenceResult) inferred_result = result.src if inferred_result isa CodeInfo + uncompressed = inferred_result inferred_result = maybe_compress_codeinfo(interp, linfo, inferred_result) + result.is_src_volatile |= uncompressed !== inferred_result end # The global cache can only handle objects that codegen understands if !isa(inferred_result, MaybeCompressed) diff --git a/base/compiler/types.jl b/base/compiler/types.jl index 5c7445aec0386..db1d85cdddc40 100644 --- a/base/compiler/types.jl +++ b/base/compiler/types.jl @@ -76,12 +76,13 @@ mutable struct InferenceResult ipo_effects::Effects # if inference is finished effects::Effects # if optimization is finished argescapes # ::ArgEscapeCache if optimized, nothing otherwise + is_src_volatile::Bool # `src` has been cached globally as the compressed format already, allowing `src` to be used destructively function InferenceResult(linfo::MethodInstance, cache_argtypes::Vector{Any}, overridden_by_const::BitVector) # def = linfo.def # nargs = def isa Method ? Int(def.nargs) : 0 # @assert length(cache_argtypes) == nargs return new(linfo, cache_argtypes, overridden_by_const, nothing, nothing, - WorldRange(), Effects(), Effects(), nothing) + WorldRange(), Effects(), Effects(), nothing, false) end end InferenceResult(linfo::MethodInstance, 𝕃::AbstractLattice=fallback_lattice) =