Skip to content
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

Fix cycle removal bugs #441

Merged
merged 4 commits into from
Aug 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/generator/codegen.jl
Original file line number Diff line number Diff line change
Expand Up @@ -543,7 +543,7 @@ function emit!(dag::ExprDAG, node::ExprNode{StructMutualRef}, options::Dict; arg

if node_idx < field_idx
# this assumes that circular references were removed at pointers
@assert is_jl_pointer(jlty)
@assert is_jl_pointer(jlty) "Expected this field to be a pointer: $(struct_sym).$(field_sym)"

# also emit the original expressions, so we can add corresponding comments
# in the pretty-print pass
Expand Down
15 changes: 10 additions & 5 deletions src/generator/passes.jl
Original file line number Diff line number Diff line change
Expand Up @@ -361,10 +361,10 @@ function (x::RemoveCircularReference)(dag::ExprDAG, options::Dict)
count = 0
while any(x -> x != PERMANENT, marks) && (count += 1) < MAX_CIRCIR_DETECTION_COUNT
fill!(marks, UNMARKED)
for (i, node) in enumerate(dag.nodes)
marks[i] == UNMARKED || continue
for (node_idx, node) in enumerate(dag.nodes)
marks[node_idx] == UNMARKED || continue
cycle = Int[]
detect_cycle!(dag.nodes, marks, cycle, i)
detect_cycle!(dag.nodes, marks, cycle, node_idx)
isempty(cycle) && continue

# firstly remove cycle reference caused by mutually referenced structs
Expand All @@ -383,10 +383,14 @@ function (x::RemoveCircularReference)(dag::ExprDAG, options::Dict)
dag.nodes[nc] = ExprNode(id, ty, child.cursor, child.exprs, child.adj)
show_info &&
@info "[RemoveCircularReference]: removed $(child.id)'s dependency $(parent.id)"

# Now the cycle is broken and we don't need to look at
# any other nodes in the cycle path.
break
end
end
# exit earlier
(i + 1) == first(cycle) && break
(node_idx + 1) == first(cycle) && break
end

# there are cases where the circular reference can only be de-referenced at a
Expand All @@ -405,8 +409,9 @@ function (x::RemoveCircularReference)(dag::ExprDAG, options::Dict)
dag.nodes[nc] = ExprNode(id, ty, child.cursor, child.exprs, child.adj)
show_info &&
@info "[RemoveCircularReference]: removed $(child.id)'s dependency $(parent.id)"

# exit earlier
(i + 1) == first(cycle) && break
break
end
end

Expand Down
18 changes: 17 additions & 1 deletion test/generators.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ using Clang
using Clang.Generators
using Clang.LibClang.Clang_jll
using Test
using Clang.Generators: strip_comment_markers
using Clang.Generators: StructMutualRef, strip_comment_markers

include("rewriter.jl")

Expand Down Expand Up @@ -57,6 +57,22 @@ end
@test include("LibDependency.jl") isa Any
end

# See:
# - https://github.com/JuliaInterop/Clang.jl/discussions/440
# - https://github.com/JuliaInterop/Clang.jl/pull/441
@testset "Cycle detection" begin
args = get_default_args()
headers = joinpath(@__DIR__, "include", "cycle-detection.h")
ctx = create_context(headers, args)
build!(ctx)

# In this particular case there is only one cycle in B, so only B should be
# a StructMutualRef.
mutual_ref_nodes = [node for node in ctx.dag.nodes if node.type == StructMutualRef()]
@test length(mutual_ref_nodes) == 1
@test mutual_ref_nodes[1].id == :B
end

@testset "Issue 320" begin
args = get_default_args()
dir = joinpath(@__DIR__, "sys")
Expand Down
32 changes: 32 additions & 0 deletions test/include/cycle-detection.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
typedef struct A A;
typedef struct B B;
typedef struct C C;

struct B;
struct C;

struct B
{
B* x;
};

struct C
{
B y[10];
};

typedef struct vector_C
{
C* c;
} vector_C;

typedef struct pool_C
{
vector_C vc;
} pool_C;

struct A
{
pool_C pc;
C* c;
};