Skip to content

Commit

Permalink
Control flow: use domtree analysis (#98)
Browse files Browse the repository at this point in the history
Fixes #96

This vendors Julia 1.10's Core.Compiler's domtree code for earlier
Julia versions.
  • Loading branch information
timholy authored Jan 21, 2024
1 parent 0380f3c commit 069f7a6
Show file tree
Hide file tree
Showing 4 changed files with 449 additions and 52 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "LoweredCodeUtils"
uuid = "6f1432cf-f94c-5a45-995e-cdbf5db27b0b"
authors = ["Tim Holy <[email protected]>"]
version = "2.4.3"
version = "2.4.4"

[deps]
JuliaInterpreter = "aa1ae85d-cabe-5617-a682-6adf51b2e16a"
Expand Down
78 changes: 27 additions & 51 deletions src/codeedges.jl
Original file line number Diff line number Diff line change
Expand Up @@ -609,7 +609,8 @@ function lines_required!(isrequired::AbstractVector{Bool}, objs, src::CodeInfo,

# Compute basic blocks, which we'll use to make sure we mark necessary control-flow
cfg = Core.Compiler.compute_basic_blocks(src.code) # needed for control-flow analysis
paths = enumerate_paths(cfg)
domtree = construct_domtree(cfg.blocks)
postdomtree = construct_postdomtree(cfg.blocks)

# We'll mostly use generic graph traversal to discover all the lines we need,
# but structs are in a bit of a different category (especially on Julia 1.5+).
Expand All @@ -629,7 +630,7 @@ function lines_required!(isrequired::AbstractVector{Bool}, objs, src::CodeInfo,

# Add control-flow
changed |= add_loops!(isrequired, cfg)
changed |= add_control_flow!(isrequired, cfg, paths)
changed |= add_control_flow!(isrequired, cfg, domtree, postdomtree)

# So far, everything is generic graph traversal. Now we add some domain-specific information
changed |= add_typedefs!(isrequired, src, edges, typedefs, norequire)
Expand Down Expand Up @@ -706,17 +707,6 @@ end

## Add control-flow

struct Path
path::Vector{Int}
visited::BitSet
end
Path() = Path(Int[], BitSet())
Path(i::Int) = Path([i], BitSet([i]))
Path(path::Path) = copy(path)
Base.copy(path::Path) = Path(copy(path.path), copy(path.visited))
Base.in(node::Int, path::Path) = node path.visited
Base.push!(path::Path, node::Int) = (push!(path.path, node); push!(path.visited, node); return path)

# Mark loops that contain evaluated statements
function add_loops!(isrequired, cfg)
changed = false
Expand All @@ -741,26 +731,7 @@ function add_loops!(isrequired, cfg)
return changed
end

enumerate_paths(cfg) = enumerate_paths!(Path[], cfg, Path(1))
function enumerate_paths!(paths, cfg, path)
bb = cfg.blocks[path.path[end]]
if isempty(bb.succs)
push!(paths, copy(path))
return paths
end
for ibbs in bb.succs
if ibbs path
push!(paths, push!(copy(path), ibbs)) # close the loop
continue
end
enumerate_paths!(paths, cfg, push!(copy(path), ibbs))
end
return paths
end

# Mark exits of blocks that bifurcate execution paths in ways that matter for required statements
function add_control_flow!(isrequired, cfg, paths::AbstractVector{Path})
withnode, withoutnode, shared = BitSet(), BitSet(), BitSet()
function add_control_flow!(isrequired, cfg, domtree, postdomtree)
changed, _changed = false, true
blocks = cfg.blocks
nblocks = length(blocks)
Expand All @@ -769,28 +740,33 @@ function add_control_flow!(isrequired, cfg, paths::AbstractVector{Path})
for (ibb, bb) in enumerate(blocks)
r = rng(bb)
if any(view(isrequired, r))
# Check if the exit of this block is a GotoNode or `return`
if length(bb.succs) < 2 && ibb < nblocks
idxlast = r[end]
_changed |= !isrequired[idxlast]
isrequired[idxlast] = true
end
empty!(withnode)
empty!(withoutnode)
for path in paths
union!(ibb path ? withnode : withoutnode, path.visited)
# Walk up the dominators
jbb = ibb
while jbb != 1
jdbb = domtree.idoms_bb[jbb]
dbb = blocks[jdbb]
# Check the successors; if jbb doesn't post-dominate, mark the last statement
for s in dbb.succs
if !postdominates(postdomtree, jbb, s)
idxlast = rng(dbb)[end]
_changed |= !isrequired[idxlast]
isrequired[idxlast] = true
break
end
end
jbb = jdbb
end
empty!(shared)
union!(shared, withnode)
intersect!(shared, withoutnode)
for icfbb in shared
cfbb = blocks[icfbb]
if any((shared), cfbb.succs)
rcfbb = rng(blocks[icfbb])
idxlast = rcfbb[end]
# Walk down the post-dominators, including self
jbb = ibb
while jbb != 0 && jbb < nblocks
pdbb = blocks[jbb]
# Check if the exit of this block is a GotoNode or `return`
if length(pdbb.succs) < 2
idxlast = rng(pdbb)[end]
_changed |= !isrequired[idxlast]
isrequired[idxlast] = true
end
jbb = postdomtree.idoms_bb[jbb]
end
end
end
Expand Down
Loading

2 comments on commit 069f7a6

@timholy
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JuliaRegistrator
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Registration pull request created: JuliaRegistries/General/99252

Tip: Release Notes

Did you know you can add release notes too? Just add markdown formatted text underneath the comment after the text
"Release notes:" and it will be added to the registry PR, and if TagBot is installed it will also be added to the
release that TagBot creates. i.e.

@JuliaRegistrator register

Release notes:

## Breaking changes

- blah

To add them here just re-invoke and the PR will be updated.

Tagging

After the above pull request is merged, it is recommended that a tag is created on this repository for the registered package version.

This will be done automatically if the Julia TagBot GitHub Action is installed, or can be done manually through the github interface, or via:

git tag -a v2.4.4 -m "<description of version>" 069f7a601936c07238b30719ac85665606f00f46
git push origin v2.4.4

Please sign in to comment.