| 529 | } |
| 530 | |
| 531 | pub fn run(&mut self, token: &NodeOwner, region: &mut Region, virt_map: &mut VirtualRegisterMap) { |
| 532 | let (mut edges, states) = self.edges_row(region); |
| 533 | let Region { nodes, ports, sinks, .. } = region; |
| 534 | |
| 535 | |
| 536 | use petgraph::visit::Walker; |
| 537 | let mut dfs_space = DfsSpace::new(&*nodes); |
| 538 | let mut visit_nodes: Vec<_> = petgraph::algo::toposort(&*nodes, Some(&mut dfs_space)).unwrap(); |
| 539 | let toposorted = visit_nodes.clone().drain(..).enumerate().map(|(i, n)| (n, i)).collect::<HashMap<_,_>>(); |
| 540 | let visit_nodes = visit_nodes.drain(..).map(|n| (n, RefEquality(nodes[n].variant.clone()))).collect(); |
| 541 | |
| 542 | let constants = nodes.node_indices().filter_map(|n| { |
| 543 | nodes[n].as_variant::<NodeVariant::Constant>(token).and_then(|c| { |
| 544 | let Some(Storage::Virtual(vreg)) = ports[nodes[n].sinks()[0]].storage.0 else { return None }; |
| 545 | Some((n, Pattern::Constant64(VReg(vreg)), Set::singleton(n))) |
| 546 | }) |
| 547 | //(n, 64, ConstPropagation::Constant(c.0 as i64))) |
| 548 | }).collect::<Vec<_>>(); |
| 549 | let outgoing = nodes.node_indices().map(|n| { |
| 550 | (n, nodes.edges(n).count().try_into().unwrap()) |
| 551 | }).collect::<Vec<_>>(); |
| 552 | //dbg!(constants.clone()); |
| 553 | let mut ascent: AscentProgram::<'_> = AscentProgram::default(); |
| 554 | ascent.pattern = constants; // feed in the constants as initial patterns |
| 555 | ascent.token = vec![(RefEquality(token),)]; |
| 556 | ascent.kind = visit_nodes; |
| 557 | ascent.edge = edges; |
| 558 | ascent.state = states; |
| 559 | ascent.restricted = virt_map.iter().flat_map(|(i,v)| v.backing.map(|back| ((VReg(*i as u16),back)))).collect::<Vec<_>>(); |
| 560 | //ascent.constant = constants; |
| 561 | ascent.outgoing = outgoing; |
| 562 | ascent.run(); |
| 563 | |
| 564 | println!("after ascent"); |
| 565 | for block in ascent.block { |
| 566 | println!("block {} - {:?}", block.0, block.1.iter()); |
| 567 | } |
| 568 | for cfg in ascent.cfg { |
| 569 | println!("cfg {} -> {:?}", cfg.0, cfg.1.unwrap().iter()); |
| 570 | } |
| 571 | let mut emitted: Option<BTreeSet<_>> = None; |
| 572 | ascent.pattern.sort_by(|a, b| |
| 573 | toposorted[&a.0].cmp(&toposorted[&b.0]).reverse() |
| 574 | // for now we just return the largest pattern that matches first |
| 575 | // later on this can be some shortest-path (lowest cost) thing ig |
| 576 | .then(a.2.len().cmp(&b.2.len()).reverse()) |
| 577 | .then(a.1.cmp(&b.1)) |
| 578 | ); |
| 579 | let mut roots = ascent.pattern.drain(..).flat_map(|(root, pat, include_set)| { |
| 580 | //dbg!(root, pat); |
| 581 | // for not we just get the first pattern that matches |
| 582 | |
| 583 | // If we already emitted the root, then it was a part of a pattern |
| 584 | // from higher topologically. (this can be done smarter) |
| 585 | if let Some(e) = emitted.as_ref() && e.contains(&root) { |
| 586 | return None |
| 587 | } |
| 588 | println!("pattern {:?} {:?} {:?} {:?}", |