Skip to content

Commit

Permalink
Compute CFG (#158)
Browse files Browse the repository at this point in the history
* Compute CFG

* Fix typo

* Bump patch

* Remove dead comment
  • Loading branch information
willtebbutt authored May 16, 2024
1 parent c906419 commit 2e56d20
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Project.toml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
name = "Tapir"
uuid = "07d77754-e150-4737-8c94-cd238a1fb45b"
authors = ["Will Tebbutt, Hong Ge, and contributors"]
version = "0.2.14"
version = "0.2.15"

[deps]
ADTypes = "47edcb42-4c32-4615-8424-f2b9edc5f35b"
Expand Down
33 changes: 32 additions & 1 deletion src/interpreter/bbcode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,37 @@ end
concatenate_ids(bb_code::BBCode) = reduce(vcat, map(b -> b.inst_ids, bb_code.blocks))
concatenate_stmts(bb_code::BBCode) = reduce(vcat, map(b -> b.insts, bb_code.blocks))

"""
control_flow_graph(bb_code::BBCode)::Core.Compiler.CFG
Computes the `Core.Compiler.CFG` object associated to this `bb_code`.
"""
control_flow_graph(bb_code::BBCode)::Core.Compiler.CFG = _control_flow_graph(bb_code.blocks)

# Internal function, used to implement control_flow_graph, but easier to write test cases
# for because there is no need to construct an ensure BBCode object.
function _control_flow_graph(blks::Vector{BBlock})::Core.Compiler.CFG

# Get IDs of predecessors and successors.
preds_ids = _compute_all_predecessors(blks)
succs_ids = _compute_all_successors(blks)

# Construct map from block ID to block number.
block_ids = map(b -> b.id, blks)
id_to_num = Dict{ID, Int}(zip(block_ids, collect(eachindex(block_ids))))

# Convert predecessor and successor IDs to numbers.
preds = map(id -> sort(map(p -> id_to_num[p], preds_ids[id])), block_ids)
succs = map(id -> sort(map(s -> id_to_num[s], succs_ids[id])), block_ids)

index = vcat(0, cumsum(map(length, blks))) .+ 1
basic_blocks = map(eachindex(blks)) do n
stmt_range = Core.Compiler.StmtRange(index[n], index[n+1] - 1)
return Core.Compiler.BasicBlock(stmt_range, preds[n], succs[n])
end
return Core.Compiler.CFG(basic_blocks, index[2:end-1])
end

#
# Converting from IRCode to BBCode
#
Expand Down Expand Up @@ -454,7 +485,7 @@ function CC.IRCode(bb_code::BBCode)
bb_code = _lower_switch_statements(bb_code)
bb_code = _remove_double_edges(bb_code)
insts = _ids_to_line_positions(bb_code)
cfg = _compute_basic_blocks(insts)
cfg = control_flow_graph(bb_code)
insts = _lines_to_blocks(insts, cfg)
return IRCode(
CC.InstructionStream(
Expand Down
10 changes: 10 additions & 0 deletions test/interpreter/bbcode.jl
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,16 @@ end
@test length(Tapir.collect_stmts(bb_code)) == length(ir.stmts.inst)
@test Tapir.id_to_line_map(bb_code) isa Dict{ID, Int}
end
@testset "control_flow_graph" begin
ir = Base.code_ircode_by_type(Tuple{typeof(sin), Float64})[1][1]
bb = BBCode(ir)
new_ir = Core.Compiler.IRCode(bb)
cfg = Tapir.control_flow_graph(bb)
@test all(map((l, r) -> l.stmts == r.stmts, ir.cfg.blocks, cfg.blocks))
@test all(map((l, r) -> sort(l.preds) == sort(r.preds), ir.cfg.blocks, cfg.blocks))
@test all(map((l, r) -> sort(l.succs) == sort(r.succs), ir.cfg.blocks, cfg.blocks))
@test ir.cfg.index == cfg.index
end
@testset "_characterise_unique_predecessor_blocks" begin
@testset "single block" begin
blk_id = ID()
Expand Down

4 comments on commit 2e56d20

@willtebbutt
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/106932

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 v0.2.15 -m "<description of version>" 2e56d20724bae2f601f7ecc26e49c6c75012aade
git push origin v0.2.15

Also, note the warning: Version 0.2.15 skips over 0.2.13
This can be safely ignored. However, if you want to fix this you can do so. Call register() again after making the fix. This will update the Pull request.

@willtebbutt
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 updated: JuliaRegistries/General/106932

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 v0.2.15 -m "<description of version>" 2e56d20724bae2f601f7ecc26e49c6c75012aade
git push origin v0.2.15

Please sign in to comment.