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

feat: Dataflow analysis framework #1476

Merged
merged 218 commits into from
Dec 4, 2024
Merged
Show file tree
Hide file tree
Changes from 96 commits
Commits
Show all changes
218 commits
Select commit Hold shift + click to select a range
c7a9d89
Just const_fold2 + inside that partial_value (taken from hugr_core)
acl-cqc Aug 6, 2024
ac45e53
merge/update+fmt (ValueName for ConstInt non-compiling as ConstInt no…
acl-cqc Sep 11, 2024
8adaa6e
Missing imports / lints. Now running, but failing w/StackOverflow
acl-cqc Aug 6, 2024
098c735
Fix tests...
acl-cqc Aug 6, 2024
706c892
ValueKey using MaybeHash
acl-cqc Aug 6, 2024
63bc944
tag() does not refer to self.is_compound
acl-cqc Aug 6, 2024
5fa7edb
ValueHandle::{is_compound,num_fields,index} => {variant_values, as_sum}
acl-cqc Aug 6, 2024
98bf94a
Rm ValueHandle::tag, use variant_values - inefficient, presume this i…
acl-cqc Aug 6, 2024
295ec32
add variant_values, rewrite one use of outputs_for_variant
acl-cqc Aug 6, 2024
5c8289e
...and the other two; remove outputs_for_variant
acl-cqc Aug 6, 2024
bf173ab
Rewrite tuple rule to avoid indexing
acl-cqc Aug 6, 2024
0ae4d19
GC unused (tuple,variant)_field_value, iter_with_ports
acl-cqc Aug 6, 2024
8608ba9
Common up via ValueRow.unpack_first
acl-cqc Aug 6, 2024
2dca3e9
No DeRef for ValueHandle, just add get_type()
acl-cqc Aug 6, 2024
51e68ea
ValueKey::{Select->Field,index->field}
acl-cqc Aug 6, 2024
8635474
(join/meet)_mut_unsafe => try_(join/meet)_mut with Err for conflictin…
acl-cqc Aug 7, 2024
80d5b86
Remove ValueHandle::variant_values - just have as_sum
acl-cqc Aug 8, 2024
1c8be99
Optimize as_sum() by returning impl Iterator not Vec
acl-cqc Aug 8, 2024
b0afa54
Machine uses PV not PartialValue
acl-cqc Aug 27, 2024
d09a1fe
Parametrize PartialValue+PV+Machine by AbstractValue/Into<Value>, Con…
acl-cqc Aug 28, 2024
af8827b
Move partial_value.rs inside datalog/
acl-cqc Aug 28, 2024
4b61436
Hide PartialSum/PartialValue
acl-cqc Aug 28, 2024
8b31d8c
refactor: ValueRow::single_among_bottoms
acl-cqc Aug 28, 2024
6a72961
Factor out propagate_leaf_op; add ValueRow::from_iter
acl-cqc Aug 28, 2024
780af9b
Add handling for Tag
acl-cqc Aug 28, 2024
cabcf04
Remove PV (use typedef in datalog.rs)
acl-cqc Aug 28, 2024
5e4a04f
Allow DFContext to interpret any leaf op (except MakeTuple/etc.); pub…
acl-cqc Aug 28, 2024
1453886
Also fold extension ops
acl-cqc Aug 28, 2024
221e96c
Comment as_sum
acl-cqc Aug 30, 2024
cd4e15c
Rename DataflowContext to HugrValueContext
acl-cqc Aug 30, 2024
c5ab2a9
Move {datalog=>value_handle}/context.rs - an impl, datalog uses only …
acl-cqc Aug 30, 2024
6c80acf
Comment re. propagate_leaf_op
acl-cqc Aug 30, 2024
636f14d
Hide PartialValue by abstracting DFContext::InterpretableVal: FromSum…
acl-cqc Aug 30, 2024
0acdcc5
Use Value::sum, adding FromSum::Err; fmt
acl-cqc Aug 30, 2024
1012e0a
Cargo.toml: use explicit git= tag for ascent
acl-cqc Sep 2, 2024
6bd8dba
Fix rebase: TryHash, UnpackTuple/MakeTuple now in prelude
acl-cqc Sep 2, 2024
f7d288f
pub ValueRow+Partial(Value/Sum); add TotalContext
acl-cqc Sep 2, 2024
a62eb0f
Remove DFContext::hugr(), as_ref() does just as well
acl-cqc Sep 2, 2024
f0ec237
Move partial_value out of datalog, combine tests; move ValueRow out o…
acl-cqc Sep 2, 2024
82a3f22
Move FromSum and try_into_value into total_context.rs
acl-cqc Sep 2, 2024
e6dc114
Separate mod dataflow from mod const_fold2
acl-cqc Sep 2, 2024
03ff165
fmt
acl-cqc Sep 2, 2024
25ed1fb
TailLoopTermination just examine whatever PartialValue's we have, rem…
acl-cqc Sep 2, 2024
09911df
Drop non-(Bounded)Lattice impls of (join/meet)(_mut),top,bottom
acl-cqc Sep 2, 2024
248fb07
doc fixes, remove missing-docs for partial_value...how does it still …
acl-cqc Sep 2, 2024
e2ad079
fix all warnings (inc Machine::new() -> impl Default)
acl-cqc Sep 2, 2024
95e2dd9
distribute utils.rs -> machine.rs
acl-cqc Sep 2, 2024
7f1e122
Move dataflow{/datalog=>}/test.rs
acl-cqc Sep 2, 2024
faff556
and more warnings
acl-cqc Sep 2, 2024
401354d
fix extension tests
acl-cqc Sep 2, 2024
c468387
Fix doclink, fix DefaultHasher pre-1.76
acl-cqc Sep 2, 2024
88db5b1
comment conditional test
acl-cqc Sep 11, 2024
738b61b
Clarify (TODO untested) branches of join_mut
acl-cqc Sep 11, 2024
8f9c1ed
Exploit invariant PartialValue::Value is not a sum (even single known…
acl-cqc Sep 11, 2024
a71ba97
Exploit invariant more, RIP join_mut_value_handle
acl-cqc Sep 11, 2024
05280a8
By similar logic, RIP meet_mut_value_handle; assert_(in)variants now …
acl-cqc Sep 11, 2024
96b0856
Rename TestSum(,Leaf)Type::assert_{invariants=>valid}
acl-cqc Sep 13, 2024
f21e278
Rename assert_(=>in)variants (i.e. to match); call in (join/meet)_mut…
acl-cqc Sep 13, 2024
ce53b1c
test unpacking constant tuple
acl-cqc Sep 13, 2024
13f29a9
try_into_value returns new enum ValueOrSum; TryFrom<ValueOrSum> repla…
acl-cqc Sep 12, 2024
9f1a5cd
Hide ValueRow (and move into datalog.rs)
acl-cqc Oct 2, 2024
15e642e
Remove PartialSum::unit()
acl-cqc Oct 2, 2024
514af13
PartialValue is private struct containing PVEnum (with ::Sum not ::Pa…
acl-cqc Oct 2, 2024
5d86f46
variant => new_variant, unit => new_unit
acl-cqc Oct 2, 2024
d8c8140
Simplify PartialOrd for PartialSum, keys(1,2) support cmp
acl-cqc Oct 2, 2024
1315685
PartialSum::variant_values does not take `len` (PartialValue:: still …
acl-cqc Oct 2, 2024
2aaaeb9
clippy
acl-cqc Oct 2, 2024
5619761
Machine::tail_loop_terminates + case_reachable return Option not panic
acl-cqc Oct 2, 2024
f3c175c
Machine::read_out_wire_value fails with ConstTypeError if there was one
acl-cqc Oct 2, 2024
aad2ef0
PartialValue::try_(join|meet)_mut are pub, don't mutate upon failure
acl-cqc Oct 2, 2024
0a8cc12
Remove some commented-out code
acl-cqc Oct 2, 2024
bfcd0a6
Expose PartialSum
acl-cqc Oct 2, 2024
248fb23
dataflow has docs! (enforced)
acl-cqc Oct 2, 2024
5b8654e
clippy
acl-cqc Oct 2, 2024
c40e718
Move PartialValue::join into impl Lattice for
acl-cqc Oct 7, 2024
8732a63
Machine::read_out_wire_value => PartialValue::try_into_wire_value
acl-cqc Oct 7, 2024
346187d
read_out_wire_partial_value => read_out_wire
acl-cqc Oct 7, 2024
a139f9e
Remove ValueOrSum (and add Sum) via complex parametrization of try_in…
acl-cqc Oct 7, 2024
7f2a91a
Datalog works on any AbstractValue; impl'd by PartialValue for a Base…
acl-cqc Oct 7, 2024
1680829
PartialValue proptests: rm TestSumLeafType, replace ValueHandle with …
acl-cqc Oct 8, 2024
2a57a15
tests: Rename type_check -> check_value
acl-cqc Oct 8, 2024
fcfcb6b
tidies
acl-cqc Oct 8, 2024
dcaa928
Add a couple more proptests, and a TEMPORARY FIX for a BUG pending be…
acl-cqc Oct 8, 2024
bcacbcc
Remove redundant test
acl-cqc Oct 8, 2024
5192ed8
Refactor TailLoopTermination::from_control_value
acl-cqc Oct 8, 2024
e21bbd7
pub TailLoopTermination, rename members, doc
acl-cqc Oct 8, 2024
22e0192
Test tidies (and some ALAN wtf? comments)
acl-cqc Oct 8, 2024
cae5e4f
Use Tag
acl-cqc Oct 8, 2024
3014827
Add TestContext (no interpret_leaf_op), propolutate, avoid HugrValueC…
acl-cqc Oct 8, 2024
64b9bb7
Avoid propolutate by interpreting LoadConstant (only)
acl-cqc Oct 8, 2024
8bc5e12
Revert "Avoid propolutate by interpreting LoadConstant (only)"
acl-cqc Oct 8, 2024
a3a6213
tiny const_fold2 doc tweaks
acl-cqc Oct 8, 2024
a96ab20
(TEMP) remove const_fold2 module
acl-cqc Oct 8, 2024
3051183
Merge remote-tracking branch 'origin/main' into acl/const_fold2
acl-cqc Oct 8, 2024
777694c
(TEMP) Rm total_context
acl-cqc Oct 8, 2024
5a16e6b
clippy
acl-cqc Oct 8, 2024
4f31178
Better fix for PartialSum::try_meet_mut
acl-cqc Oct 9, 2024
2b523c9
true_or_false uses pv_true+pv_false
acl-cqc Oct 9, 2024
1d2cb9b
Update to ascent 0.7.0, drop fn join/meet as these are now trait-default
acl-cqc Oct 9, 2024
ee91bbe
Cargo.toml: oops, remove obsolete comment
acl-cqc Oct 9, 2024
e67051f
ValueRow cleanups (remove misleading 'pub's)
acl-cqc Oct 9, 2024
94cee55
Refactor: rm tail_node, clone earlier in ValueRow::unpack_first, rm V…
acl-cqc Oct 9, 2024
5cf5ff0
Add datalog for CFG
acl-cqc Oct 9, 2024
7381087
refactor: follow unpack_first with enumerate
acl-cqc Oct 9, 2024
60e33db
Remove comments from test_tail_loop_(iterates_twice->two_iters)
acl-cqc Oct 9, 2024
ef4f433
Add a test of tail loop around conditional
acl-cqc Oct 9, 2024
5935489
improve that test - loop input is a sum and the variants have differe…
acl-cqc Oct 9, 2024
b198681
clippy/nth
acl-cqc Oct 10, 2024
ed30f80
revert accidental changes to hugr-core/src/types.rs (how?!)
acl-cqc Oct 10, 2024
3f7808a
Cleanup conditional, cfg, unpack_first
acl-cqc Oct 10, 2024
436b635
Complex CFG that does a not-XOR...but analysis generally says "true o…
acl-cqc Oct 9, 2024
0374d13
Propagate case results to conditional output only if case reached; so…
acl-cqc Oct 10, 2024
6a2dd9e
More test cases
acl-cqc Oct 10, 2024
a75fee9
refactor as fixture
acl-cqc Oct 10, 2024
151e571
clippy
acl-cqc Oct 10, 2024
e15b04d
Revert "Datalog works on any AbstractValue; impl'd by PartialValue fo…
acl-cqc Oct 14, 2024
dc08f0d
(Re-)remove PVEnum
acl-cqc Oct 14, 2024
436dcd2
Remove as_sum. AbstractValues are elements not sums
acl-cqc Oct 14, 2024
e817bbe
clippy
acl-cqc Oct 14, 2024
b06cfad
Refactor: remove 'fn input_count'
acl-cqc Oct 14, 2024
fd717be
Try to fix interpret_leaf_op: cannot use Bottom for output! But ascen…
acl-cqc Oct 14, 2024
9b17439
interpret_leaf_op for ExtensionOps only; LoadConstant via value_from_…
acl-cqc Oct 14, 2024
846d1ee
Correct comment BaseValue -> AbstractValue
acl-cqc Oct 15, 2024
328e7f8
test Hugr now returns (XOR, AND) of two inputs, one case wrongly prod…
acl-cqc Oct 15, 2024
da3c05c
BB reachability, fixes!
acl-cqc Oct 10, 2024
6930ad4
Test cases with true_or_false/top, standardize naming (->test_)condit…
acl-cqc Oct 15, 2024
0a3e281
Try to common up by using case_reachable in conditional outputs - 6 t…
acl-cqc Oct 11, 2024
b1e0bfd
Make case_reachable a relation (dropping bool), not lattice - fixes t…
acl-cqc Oct 11, 2024
355e814
Call (+test)
acl-cqc Oct 15, 2024
22f3ce8
propolutate_out_wires => prepopulate and set in wires in run
acl-cqc Oct 15, 2024
68b1d48
Rm/inline value_inputs/value_outputs, use UnpackTuple, comments
acl-cqc Oct 15, 2024
77a5fa3
Merge remote-tracking branch 'origin/main' into acl/const_fold2
acl-cqc Oct 15, 2024
2cc62f0
clippy
acl-cqc Oct 15, 2024
8254771
docs
acl-cqc Oct 15, 2024
7e81b15
Separate AnalysisResults from Machine, use context.exactly_one() not …
acl-cqc Oct 21, 2024
34e82ed
Move try_into_wire_value => AnalysisResults.try_read_wire_value
acl-cqc Oct 21, 2024
015707f
doc fixes and fix comment
acl-cqc Oct 21, 2024
ff39f7d
Try to make clippy happy
acl-cqc Oct 21, 2024
ada7ee1
Use ascent_run to drop context from all the relations. Lots cleanup t…
acl-cqc Oct 22, 2024
7c02d41
DFContext does not Deref, pass Hugr separately
acl-cqc Oct 22, 2024
3d4f016
Massively reduce scope of clippy-allow to inside run_datalog
acl-cqc Oct 22, 2024
8bab4d5
Move ValueRow into own file
acl-cqc Oct 22, 2024
3eccadf
Move Machine into datalog.rs, pub(super) fields in AnalysisResults, r…
acl-cqc Oct 22, 2024
0f4fa52
Move machine.rs to results.rs
acl-cqc Oct 22, 2024
caa8aca
Remove enum IO, replace io_node -> input_child/output_child
acl-cqc Oct 22, 2024
e7f61fc
move docs
acl-cqc Oct 22, 2024
811802c
Remove/inline/dedup dfb_block
acl-cqc Oct 22, 2024
3713ea7
relation doc
acl-cqc Oct 22, 2024
d317809
datalog docs (each relation), move _cfg_succ_dest
acl-cqc Oct 22, 2024
69c3270
comment, use exactly_one
acl-cqc Oct 22, 2024
dc56686
Allow to handle LoadFunction
acl-cqc Oct 23, 2024
33a8592
doc
acl-cqc Oct 23, 2024
b153ada
Separate out ConstLoader
acl-cqc Oct 23, 2024
5052ac0
value_from_(custom_const=>opaque), taking &OpaqueValue
acl-cqc Oct 23, 2024
ad0c6f2
Recombine DFContext with Hugr i.e. reinstate Deref constraint
acl-cqc Oct 28, 2024
16a18f4
Replace Deref with HugrView, trivially obtainable by implementing AsRef
acl-cqc Oct 28, 2024
a0f2b2c
ValueRow Debug; ops default to PartialValue::Top less aggressively
acl-cqc Oct 28, 2024
87eb700
And back to Deref, should allow using a region view not the whole Hugr
acl-cqc Oct 29, 2024
a49221b
fix doclink
acl-cqc Oct 30, 2024
71ea55d
PartialSum::try_into_value also uses Option<...> as error-type
acl-cqc Oct 30, 2024
ea9db2e
Proper errors from try_into_value, Option<Extr...> from try_read_wire…
acl-cqc Oct 30, 2024
df31523
fmt
acl-cqc Oct 30, 2024
a490874
Add test running on region
acl-cqc Oct 30, 2024
3af39aa
fix: provide PartialValue::Top for unspecified Hugr inputs
acl-cqc Oct 30, 2024
dc15999
try_into_value allows TryFrom by giving ExtractValueError *2* errorty…
acl-cqc Oct 30, 2024
2d81264
improve docs
acl-cqc Oct 30, 2024
b61d252
Parametrize Machine::try_read_wire_value the same way
acl-cqc Oct 30, 2024
8cac194
tweaks
acl-cqc Oct 30, 2024
fb3816e
Massively simplify xor_and_cfg, no need for conditionals
acl-cqc Nov 4, 2024
19571f6
Use tru/fals constants
acl-cqc Nov 4, 2024
69d0f5e
try_into_value: reorder type params, separate out where clause
acl-cqc Nov 8, 2024
cb60b5b
Merge remote-tracking branch 'origin/main' into HEAD
acl-cqc Nov 8, 2024
2624ee8
We don't actually use portgraph, nor downcast-rs
acl-cqc Nov 8, 2024
ec526e8
Import RandomState from std::collections::hash_map for rust 1.75
acl-cqc Nov 8, 2024
5650ee4
Use BREAK_TAG/CONTINUE_TAG
acl-cqc Nov 8, 2024
da2981c
No, use make_break/make_continue for easy cases
acl-cqc Nov 8, 2024
0b68454
Refactor bb_reachable using then
acl-cqc Nov 14, 2024
4916e9d
ConstLocation with Box - a lot of cloning
acl-cqc Nov 14, 2024
a4a64c0
No - Revert - just make ConstLocation store a reference
acl-cqc Nov 14, 2024
bc39b76
{value=>partial}_from_const, takes ConstLoc, inline traverse_value
acl-cqc Nov 14, 2024
92669db
Make ConstLocation Copy
acl-cqc Nov 14, 2024
33c8607
ConstLocation is From<Node>; move partial_from_const out to toplev, n…
acl-cqc Nov 14, 2024
97496f9
Merge commit 'origin/main^' into HEAD
acl-cqc Nov 18, 2024
8b76135
Generalize run to deal with Module(use main), and others; add run_lib
acl-cqc Nov 18, 2024
c18cbea
Shorten the got-all-required-inputs check (build got_inputs)
acl-cqc Nov 18, 2024
1b64b4b
Shorten further...not as easy to follow
acl-cqc Nov 18, 2024
39b8df1
Revert "Shorten further...not as easy to follow"
acl-cqc Nov 19, 2024
a5d987c
doc fixes, rename to run_library
acl-cqc Nov 19, 2024
3e718fd
Add PartialValue::contains_bottom, also row_contains_bottom
acl-cqc Nov 19, 2024
497686a
Don't call interpret_leaf_op if row_contains_bottom
acl-cqc Nov 19, 2024
e34c7be
Use row_contains_bottom for CFG+DFG, and augment unpack_first(=>_no_b…
acl-cqc Nov 19, 2024
69a69f3
run_library => publish_function
acl-cqc Nov 19, 2024
57ac432
Drop publish_function, pub prepopulate_wire
acl-cqc Nov 19, 2024
f9a9f24
ValueRow::single_known => singleton, set
acl-cqc Nov 19, 2024
9cc368d
try_join / try_meet return extra bool
acl-cqc Nov 19, 2024
a61fbdb
shorten/common-up meet_mut + join_mut
acl-cqc Nov 19, 2024
24cce0e
try_into_value: change bounds TryFrom -> TryInto; rename =>try_into_sum
acl-cqc Nov 19, 2024
a590766
Avoid a clone in try_into_sum
acl-cqc Nov 19, 2024
2b2c461
Optimize+shorten join_mut / meet_mut via std::mem::swap
acl-cqc Nov 19, 2024
124718d
refactor join_mut / meet_mut again, common-up assignment
acl-cqc Nov 19, 2024
93b1f4d
clippy
acl-cqc Nov 19, 2024
731a3b0
doclinks
acl-cqc Nov 19, 2024
7040e83
prepopulate_df_inputs
acl-cqc Nov 19, 2024
584327f
Revert "Use row_contains_bottom for CFG+DFG, and augment unpack_first…
acl-cqc Nov 20, 2024
1a56145
Merge remote-tracking branch 'origin/main' into HEAD
acl-cqc Nov 20, 2024
409f377
Fix FuncDefn::name, add test_module
acl-cqc Nov 20, 2024
39b754a
clippy
acl-cqc Nov 20, 2024
c076a2c
clippy test
acl-cqc Nov 21, 2024
efdf516
Merge remote-tracking branch 'origin/main' into HEAD
acl-cqc Nov 22, 2024
77b6739
try_into_{value=>concrete}
acl-cqc Nov 23, 2024
caa8882
try_read_wire_{value=>concrete}
acl-cqc Nov 23, 2024
c34bab7
Merge remote-tracking branch 'origin/main' into HEAD
acl-cqc Dec 4, 2024
d387cef
fix BOOL_T -> bool_t() and types needing extensions
acl-cqc Dec 4, 2024
df434e7
clippy
acl-cqc Dec 4, 2024
650cdfd
And separate DFContext from HugrView once again
acl-cqc Nov 26, 2024
071c7dd
Store HugrView (not DFContext) in Machine
acl-cqc Nov 26, 2024
c5bd7b0
Machine::run owns DFContext. Or &mut ?? (TODO update interpret_leaf_o…
acl-cqc Nov 26, 2024
eecdb22
interpret_leaf_op is &mut to allow impls that do caching etc.
acl-cqc Dec 4, 2024
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
7 changes: 7 additions & 0 deletions hugr-passes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ categories = ["compilers"]

[dependencies]
hugr-core = { path = "../hugr-core", version = "0.10.0" }
portgraph = { workspace = true }
# This ascent commit has a fix for unsoundness in release/tag 0.6.0:
ascent = {git = "https://github.com/s-arash/ascent", rev="9805d02cb830b6e66abcd4d48836a14cd98366f3"}
downcast-rs = { workspace = true }
itertools = { workspace = true }
lazy_static = { workspace = true }
paste = { workspace = true }
Expand All @@ -25,3 +29,6 @@ extension_inference = ["hugr-core/extension_inference"]

[dev-dependencies]
rstest = { workspace = true }
proptest = { workspace = true }
proptest-derive = { workspace = true }
proptest-recurse = { version = "0.5.0" }
14 changes: 14 additions & 0 deletions hugr-passes/src/dataflow.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#![warn(missing_docs)]
//! Dataflow analysis of Hugrs.

mod datalog;
pub use datalog::{AbstractValue, DFContext};

mod machine;
pub use machine::{Machine, TailLoopTermination};

mod partial_value;
pub use partial_value::{BaseValue, PVEnum, PartialSum, PartialValue, Sum};

#[cfg(test)]
mod test;
324 changes: 324 additions & 0 deletions hugr-passes/src/dataflow/datalog.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,324 @@
//! [ascent] datalog implementation of analysis.
//! Since ascent-(macro-)generated code generates a bunch of warnings,
//! keep code in here to a minimum.
#![allow(
clippy::clone_on_copy,
clippy::unused_enumerate_index,
clippy::collapsible_if
)]

use ascent::lattice::{BoundedLattice, Lattice};
use itertools::zip_eq;
use std::cmp::Ordering;
use std::hash::Hash;
use std::ops::{Index, IndexMut};

use hugr_core::extension::prelude::{MakeTuple, UnpackTuple};
use hugr_core::ops::OpType;
use hugr_core::types::Signature;
use hugr_core::{Hugr, HugrView, IncomingPort, Node, OutgoingPort, PortIndex as _};

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum IO {
Input,
Output,
}

/// Clients of the dataflow framework (particular analyses, such as constant folding)
/// must implement this trait (including providing an appropriate domain type `PV`).
pub trait DFContext<PV: AbstractValue>: Clone + Eq + Hash + std::ops::Deref<Target = Hugr> {
/// Given lattice values for each input, produce lattice values for (what we know of)
/// the outputs. Returning `None` indicates nothing can be deduced.
fn interpret_leaf_op(&self, node: Node, ins: &[PV]) -> Option<Vec<PV>>;
}

/// Values which can be the domain for dataflow analysis. Must be able to deconstructed
/// into (and constructed from) Sums as these determine control flow.
pub trait AbstractValue: BoundedLattice + Clone + Eq + Hash + std::fmt::Debug {
/// Create a new instance representing a Sum with a single known tag
/// and (recursive) representations of the elements within that tag.
fn new_variant(tag: usize, values: impl IntoIterator<Item = Self>) -> Self;

/// New instance of unit type (i.e. the only possible value, with no contents)
fn new_unit() -> Self {
Self::new_variant(0, [])
}

/// Test whether this value *might* be a Sum with the specified tag.
fn supports_tag(&self, tag: usize) -> bool;

/// If this value might be a Sum with the specified tag, return values
/// describing the elements of the Sum, otherwise `None`.
///
/// Implementations must hold the invariant that for all `x`, `tag` and `len`:
/// `x.variant_values(tag, len).is_some() == x.supports_tag(tag)`
fn variant_values(&self, tag: usize, len: usize) -> Option<Vec<Self>>;
}

ascent::ascent! {
pub(super) struct AscentProgram<PV: AbstractValue, C: DFContext<PV>>;
relation context(C);
relation out_wire_value_proto(Node, OutgoingPort, PV);

relation node(C, Node);
relation in_wire(C, Node, IncomingPort);
relation out_wire(C, Node, OutgoingPort);
relation parent_of_node(C, Node, Node);
relation io_node(C, Node, Node, IO);
lattice out_wire_value(C, Node, OutgoingPort, PV);
lattice node_in_value_row(C, Node, ValueRow<PV>);
lattice in_wire_value(C, Node, IncomingPort, PV);

node(c, n) <-- context(c), for n in c.nodes();

in_wire(c, n,p) <-- node(c, n), for p in value_inputs(c.as_ref(), *n);

out_wire(c, n,p) <-- node(c, n), for p in value_outputs(c.as_ref(), *n);

parent_of_node(c, parent, child) <--
node(c, child), if let Some(parent) = c.get_parent(*child);

io_node(c, parent, child, io) <-- node(c, parent),
if let Some([i,o]) = c.get_io(*parent),
for (child,io) in [(i,IO::Input),(o,IO::Output)];
// We support prepopulating out_wire_value via out_wire_value_proto.
//
// out wires that do not have prepopulation values are initialised to bottom.
out_wire_value(c, n,p, PV::bottom()) <-- out_wire(c, n,p);
out_wire_value(c, n, p, v) <-- out_wire(c,n,p) , out_wire_value_proto(n, p, v);

in_wire_value(c, n, ip, v) <-- in_wire(c, n, ip),
if let Some((m,op)) = c.single_linked_output(*n, *ip),
out_wire_value(c, m, op, v);


node_in_value_row(c, n, ValueRow::new(input_count(c.as_ref(), *n))) <-- node(c, n);
node_in_value_row(c, n, ValueRow::single_known(c.signature(*n).unwrap().input.len(), p.index(), v.clone())) <-- in_wire_value(c, n, p, v);

out_wire_value(c, n, p, v) <--
node(c, n),
if !c.get_optype(*n).is_container(),
node_in_value_row(c, n, vs),
if let Some(outs) = propagate_leaf_op(c, *n, &vs[..]),
for (p,v) in (0..).map(OutgoingPort::from).zip(outs);

// DFG
relation dfg_node(C, Node);
dfg_node(c,n) <-- node(c, n), if c.get_optype(*n).is_dfg();

out_wire_value(c, i, OutgoingPort::from(p.index()), v) <-- dfg_node(c,dfg),
io_node(c, dfg, i, IO::Input), in_wire_value(c, dfg, p, v);

out_wire_value(c, dfg, OutgoingPort::from(p.index()), v) <-- dfg_node(c,dfg),
io_node(c,dfg,o, IO::Output), in_wire_value(c, o, p, v);


// TailLoop
relation tail_loop_node(C, Node);
tail_loop_node(c,n) <-- node(c, n), if c.get_optype(*n).is_tail_loop();

// inputs of tail loop propagate to Input node of child region
out_wire_value(c, i, OutgoingPort::from(p.index()), v) <-- tail_loop_node(c, tl),
io_node(c,tl,i, IO::Input), in_wire_value(c, tl, p, v);

// Output node of child region propagate to Input node of child region
out_wire_value(c, in_n, out_p, v) <-- tail_loop_node(c, tl_n),
io_node(c,tl_n,in_n, IO::Input),
io_node(c,tl_n,out_n, IO::Output),
node_in_value_row(c, out_n, out_in_row), // get the whole input row for the output node
if let Some(tailloop) = c.get_optype(*tl_n).as_tail_loop(),
if let Some(fields) = out_in_row.unpack_first(0, tailloop.just_inputs.len()), // if it is possible for tag to be 0
for (out_p, v) in (0..).map(OutgoingPort::from).zip(fields);

// Output node of child region propagate to outputs of tail loop
out_wire_value(c, tl_n, out_p, v) <-- tail_loop_node(c, tl_n),
io_node(c,tl_n,out_n, IO::Output),
node_in_value_row(c, out_n, out_in_row), // get the whole input row for the output node
if let Some(tailloop) = c.get_optype(*tl_n).as_tail_loop(),
if let Some(fields) = out_in_row.unpack_first(1, tailloop.just_outputs.len()), // if it is possible for the tag to be 1
for (out_p, v) in (0..).map(OutgoingPort::from).zip(fields);

// Conditional
relation conditional_node(C, Node);
relation case_node(C,Node,usize, Node);

conditional_node (c,n)<-- node(c, n), if c.get_optype(*n).is_conditional();
case_node(c,cond,i, case) <-- conditional_node(c,cond),
for (i, case) in c.children(*cond).enumerate(),
if c.get_optype(case).is_case();

// inputs of conditional propagate into case nodes
out_wire_value(c, i_node, i_p, v) <--
case_node(c, cond, case_index, case),
io_node(c, case, i_node, IO::Input),
node_in_value_row(c, cond, in_row),
//in_wire_value(c, cond, cond_in_p, cond_in_v),
if let Some(conditional) = c.get_optype(*cond).as_conditional(),
if let Some(fields) = in_row.unpack_first(*case_index, conditional.sum_rows[*case_index].len()),
for (i_p, v) in (0..).map(OutgoingPort::from).zip(fields);

// outputs of case nodes propagate to outputs of conditional
out_wire_value(c, cond, OutgoingPort::from(o_p.index()), v) <--
case_node(c, cond, _, case),
io_node(c, case, o, IO::Output),
in_wire_value(c, o, o_p, v);

lattice case_reachable(C, Node, Node, bool);
case_reachable(c, cond, case, reachable) <-- case_node(c,cond,i,case),
in_wire_value(c, cond, IncomingPort::from(0), v),
let reachable = v.supports_tag(*i);

}

fn propagate_leaf_op<PV: AbstractValue>(
c: &impl DFContext<PV>,
n: Node,
ins: &[PV],
) -> Option<ValueRow<PV>> {
match c.get_optype(n) {
// Handle basics here. I guess (given the current interface) we could allow
// DFContext to handle these but at the least we'd want these impls to be
// easily available for reuse.
op if op.cast::<MakeTuple>().is_some() => Some(ValueRow::from_iter([PV::new_variant(
0,
ins.iter().cloned(),
)])),
op if op.cast::<UnpackTuple>().is_some() => {
let [tup] = ins.iter().collect::<Vec<_>>().try_into().unwrap();
tup.variant_values(0, value_outputs(c.as_ref(), n).count())
.map(ValueRow::from_iter)
}
OpType::Tag(t) => Some(ValueRow::from_iter([PV::new_variant(
t.tag,
ins.iter().cloned(),
)])),
OpType::Input(_) | OpType::Output(_) => None, // handled by parent
// It'd be nice to convert these to [(IncomingPort, Value)] to pass to the context,
// thus keeping PartialValue hidden, but AbstractValues
// are not necessarily convertible to Value!
_ => c.interpret_leaf_op(n, ins).map(ValueRow::from_iter),
}
}

fn input_count(h: &impl HugrView, n: Node) -> usize {
h.signature(n)
.as_ref()
.map(Signature::input_count)
.unwrap_or(0)
}

fn value_inputs(h: &impl HugrView, n: Node) -> impl Iterator<Item = IncomingPort> + '_ {
h.in_value_types(n).map(|x| x.0)
}

fn value_outputs(h: &impl HugrView, n: Node) -> impl Iterator<Item = OutgoingPort> + '_ {
h.out_value_types(n).map(|x| x.0)
}

// Wrap a (known-length) row of values into a lattice. Perhaps could be part of partial_value.rs?

#[derive(PartialEq, Clone, Eq, Hash)]
struct ValueRow<PV>(Vec<PV>);

impl<PV: AbstractValue> ValueRow<PV> {
pub fn new(len: usize) -> Self {
Self(vec![PV::bottom(); len])
}

pub fn single_known(len: usize, idx: usize, v: PV) -> Self {
assert!(idx < len);
let mut r = Self::new(len);
r.0[idx] = v;
r
}

pub fn iter(&self) -> impl Iterator<Item = &PV> {
self.0.iter()
}

pub fn unpack_first(
&self,
variant: usize,
len: usize,
) -> Option<impl Iterator<Item = PV> + '_> {
self[0]
.variant_values(variant, len)
.map(|vals| vals.into_iter().chain(self.iter().skip(1).cloned()))
}

// fn initialised(&self) -> bool {
// self.0.iter().all(|x| x != &PV::top())
// }
}

impl<PV> FromIterator<PV> for ValueRow<PV> {
fn from_iter<T: IntoIterator<Item = PV>>(iter: T) -> Self {
Self(iter.into_iter().collect())
}
}

impl<V: PartialEq + PartialOrd> PartialOrd for ValueRow<V> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
self.0.partial_cmp(&other.0)
}
}

impl<V: AbstractValue> Lattice for ValueRow<V> {
fn meet(mut self, other: Self) -> Self {
self.meet_mut(other);
self
}

fn join(mut self, other: Self) -> Self {
self.join_mut(other);
self
}

fn join_mut(&mut self, other: Self) -> bool {
assert_eq!(self.0.len(), other.0.len());
let mut changed = false;
for (v1, v2) in zip_eq(self.0.iter_mut(), other.0.into_iter()) {
changed |= v1.join_mut(v2);
}
changed
}

fn meet_mut(&mut self, other: Self) -> bool {
assert_eq!(self.0.len(), other.0.len());
let mut changed = false;
for (v1, v2) in zip_eq(self.0.iter_mut(), other.0.into_iter()) {
changed |= v1.meet_mut(v2);
}
changed
}
}

impl<PV> IntoIterator for ValueRow<PV> {
type Item = PV;

type IntoIter = <Vec<PV> as IntoIterator>::IntoIter;

fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

impl<PV, Idx> Index<Idx> for ValueRow<PV>
where
Vec<PV>: Index<Idx>,
{
type Output = <Vec<PV> as Index<Idx>>::Output;

fn index(&self, index: Idx) -> &Self::Output {
self.0.index(index)
}
}

impl<PV, Idx> IndexMut<Idx> for ValueRow<PV>
where
Vec<PV>: IndexMut<Idx>,
{
fn index_mut(&mut self, index: Idx) -> &mut Self::Output {
self.0.index_mut(index)
}
}
Loading
Loading