CQCL / hugr

Hierarchical Unified Graph Representation
https://crates.io/crates/hugr
Apache License 2.0
15 stars 4 forks source link

mermaid rendering child node in incorrect DataflowBlock #1197

Closed ss2165 closed 18 hours ago

ss2165 commented 2 weeks ago

When a Dom edge is connected to an output node, that node is being rendered inside the source dataflow block rather than the target.

Sample hugr:

{"version":"v1","nodes":[{"parent":0,"op":"CFG","signature":{"t":"G","input":[{"t":"Sum","s":"Unit","size":1}],"output":[],"extension_reqs":[]}},{"parent":0,"op":"DataflowBlock","inputs":[{"t":"Sum","s":"Unit","size":1}],"other_outputs":[],"sum_rows":[[]],"extension_delta":[]},{"parent":1,"op":"Input","types":[{"t":"Sum","s":"Unit","size":1}]},{"parent":1,"op":"Output","types":[{"t":"Sum","s":"Unit","size":1}]},{"parent":0,"op":"ExitBlock","cfg_outputs":[]},{"parent":0,"op":"DataflowBlock","inputs":[],"other_outputs":[],"sum_rows":[[]],"extension_delta":[]},{"parent":5,"op":"Input","types":[]},{"parent":5,"op":"Output","types":[{"t":"Sum","s":"Unit","size":1}]}],"edges":[[[2,0],[3,0]],[[2,0],[7,0]],[[1,0],[5,0]],[[5,0],[4,0]]],"metadata":null,"encoder":null}

(output node (7) correctly reports parent node DataflowBlock(5))

mermaid:

graph LR
    subgraph 0 ["(0) CFG"]
        direction LR
        subgraph 1 ["(1) DataflowBlock"]
            direction LR
            2["(2) Input"]
            2--"0:0<br>[]"-->3
            2--"0:0<br>[]"-->7
            3["(3) Output"]
        end
        1-."0:0".->5
        4["(4) ExitBlock"]
        subgraph 5 ["(5) DataflowBlock"]
            direction LR
            6["(6) Input"]
            7["(7) Output"]
        end
        5-."0:0".->4
    end

Node 7 is now being rendered inside block 1 even though though in the mermaid source it is inside subgraph 5. Maybe a mermaid bug?

Potential fix is adding an invisible edge between all input-output nodes to pin them to the same subgraph?

aborgna-q commented 2 weeks ago

I'm not sure it's a bug, the semantics are not too clear.

This can be fixed if write inter-graph edges outside the subgraph blocks. The invisible edge may still help with layout.

graph LR
    subgraph 0 ["(0) CFG"]
        direction LR
        subgraph 1 ["(1) DataflowBlock"]
            direction LR
            2["(2) Input"]
            2--"0:0<br>[]"-->3
            3["(3) Output"]
        end
        1-."0:0".->5
        2--"0:0<br>[]"-->7
        4["(4) ExitBlock"]
        subgraph 5 ["(5) DataflowBlock"]
            direction LR
            6["(6) Input"]
            7["(7) Output"]
        end
        5-."0:0".->4
    end
graph LR
    subgraph 0 ["(0) CFG"]
        direction LR
        subgraph 1 ["(1) DataflowBlock"]
            direction LR
            2["(2) Input"]
            2--"0:0<br>[]"-->3
            3["(3) Output"]
        end
        1-."0:0".->5
        2--"0:0<br>[]"-->7
        4["(4) ExitBlock"]
        subgraph 5 ["(5) DataflowBlock"]
            direction LR
            6["(6) Input"]
            7["(7) Output"]
        end
        5-."0:0".->4
    end