-
-
Notifications
You must be signed in to change notification settings - Fork 5.4k
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
optimizer: Julia-level escape analysis #43800
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,5 +1,35 @@ | ||
# This file is a part of Julia. License is MIT: https://julialang.org/license | ||
|
||
############# | ||
# constants # | ||
############# | ||
|
||
# The slot has uses that are not statically dominated by any assignment | ||
# This is implied by `SLOT_USEDUNDEF`. | ||
# If this is not set, all the uses are (statically) dominated by the defs. | ||
# In particular, if a slot has `AssignedOnce && !StaticUndef`, it is an SSA. | ||
const SLOT_STATICUNDEF = 1 # slot might be used before it is defined (structurally) | ||
const SLOT_ASSIGNEDONCE = 16 # slot is assigned to only once | ||
const SLOT_USEDUNDEF = 32 # slot has uses that might raise UndefVarError | ||
# const SLOT_CALLED = 64 | ||
|
||
# NOTE make sure to sync the flag definitions below with julia.h and `jl_code_info_set_ir` in method.c | ||
|
||
const IR_FLAG_NULL = 0x00 | ||
# This statement is marked as @inbounds by user. | ||
# Ff replaced by inlining, any contained boundschecks may be removed. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
const IR_FLAG_INBOUNDS = 0x01 << 0 | ||
# This statement is marked as @inline by user | ||
const IR_FLAG_INLINE = 0x01 << 1 | ||
# This statement is marked as @noinline by user | ||
const IR_FLAG_NOINLINE = 0x01 << 2 | ||
const IR_FLAG_THROW_BLOCK = 0x01 << 3 | ||
# This statement may be removed if its result is unused. In particular it must | ||
# thus be both pure and effect free. | ||
const IR_FLAG_EFFECT_FREE = 0x01 << 4 | ||
|
||
const TOP_TUPLE = GlobalRef(Core, :tuple) | ||
|
||
##################### | ||
# OptimizationState # | ||
##################### | ||
|
@@ -21,10 +51,10 @@ function push!(et::EdgeTracker, ci::CodeInstance) | |
push!(et, ci.def) | ||
end | ||
|
||
struct InliningState{S <: Union{EdgeTracker, Nothing}, T, I<:AbstractInterpreter} | ||
struct InliningState{S <: Union{EdgeTracker, Nothing}, MICache, I<:AbstractInterpreter} | ||
params::OptimizationParams | ||
et::S | ||
mi_cache::T | ||
mi_cache::MICache # TODO move this to `OptimizationState` (as used by EscapeAnalysis as well) | ||
interp::I | ||
end | ||
|
||
|
@@ -121,36 +151,6 @@ function ir_to_codeinf!(opt::OptimizationState) | |
return src | ||
end | ||
|
||
############# | ||
# constants # | ||
############# | ||
|
||
# The slot has uses that are not statically dominated by any assignment | ||
# This is implied by `SLOT_USEDUNDEF`. | ||
# If this is not set, all the uses are (statically) dominated by the defs. | ||
# In particular, if a slot has `AssignedOnce && !StaticUndef`, it is an SSA. | ||
const SLOT_STATICUNDEF = 1 # slot might be used before it is defined (structurally) | ||
const SLOT_ASSIGNEDONCE = 16 # slot is assigned to only once | ||
const SLOT_USEDUNDEF = 32 # slot has uses that might raise UndefVarError | ||
# const SLOT_CALLED = 64 | ||
|
||
# NOTE make sure to sync the flag definitions below with julia.h and `jl_code_info_set_ir` in method.c | ||
|
||
const IR_FLAG_NULL = 0x00 | ||
# This statement is marked as @inbounds by user. | ||
# Ff replaced by inlining, any contained boundschecks may be removed. | ||
const IR_FLAG_INBOUNDS = 0x01 << 0 | ||
# This statement is marked as @inline by user | ||
const IR_FLAG_INLINE = 0x01 << 1 | ||
# This statement is marked as @noinline by user | ||
const IR_FLAG_NOINLINE = 0x01 << 2 | ||
const IR_FLAG_THROW_BLOCK = 0x01 << 3 | ||
# This statement may be removed if its result is unused. In particular it must | ||
# thus be both pure and effect free. | ||
const IR_FLAG_EFFECT_FREE = 0x01 << 4 | ||
|
||
const TOP_TUPLE = GlobalRef(Core, :tuple) | ||
|
||
######### | ||
# logic # | ||
######### | ||
|
@@ -502,11 +502,37 @@ end | |
# run the optimization work | ||
function optimize(interp::AbstractInterpreter, opt::OptimizationState, | ||
params::OptimizationParams, caller::InferenceResult) | ||
@timeit "optimizer" ir = run_passes(opt.src, opt) | ||
@timeit "optimizer" ir = run_passes(opt.src, opt, caller) | ||
return finish(interp, opt, params, ir, caller) | ||
end | ||
|
||
function run_passes(ci::CodeInfo, sv::OptimizationState) | ||
using .EscapeAnalysis | ||
import .EscapeAnalysis: EscapeState, ArgEscapeCache, is_ipo_profitable | ||
|
||
""" | ||
cache_escapes!(caller::InferenceResult, estate::EscapeState) | ||
|
||
Transforms escape information of call arguments of `caller`, | ||
and then caches it into a global cache for later interprocedural propagation. | ||
""" | ||
cache_escapes!(caller::InferenceResult, estate::EscapeState) = | ||
caller.argescapes = ArgEscapeCache(estate) | ||
|
||
function ipo_escape_cache(mi_cache::MICache) where MICache | ||
return function (linfo::Union{InferenceResult,MethodInstance}) | ||
if isa(linfo, InferenceResult) | ||
argescapes = linfo.argescapes | ||
else | ||
codeinst = get(mi_cache, linfo, nothing) | ||
isa(codeinst, CodeInstance) || return nothing | ||
argescapes = codeinst.argescapes | ||
end | ||
return argescapes !== nothing ? argescapes::ArgEscapeCache : nothing | ||
end | ||
end | ||
null_escape_cache(linfo::Union{InferenceResult,MethodInstance}) = nothing | ||
|
||
function run_passes(ci::CodeInfo, sv::OptimizationState, caller::InferenceResult) | ||
@timeit "convert" ir = convert_to_ircode(ci, sv) | ||
@timeit "slot2reg" ir = slot2reg(ir, ci, sv) | ||
# TODO: Domsorting can produce an updated domtree - no need to recompute here | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we also do
0x01 << 0