Skip to content
Snippets Groups Projects
Commit c63b72a6 authored by Russel Arbore's avatar Russel Arbore
Browse files

cleanup fork_guard_elim

parent 0bad572f
No related branches found
No related tags found
1 merge request!103Forkify + Loop Canonicalization + Initial Fork Fission
Pipeline #201251 passed
...@@ -7,7 +7,8 @@ use crate::*; ...@@ -7,7 +7,8 @@ use crate::*;
/* /*
* Split multi-dimensional fork-joins into separate one-dimensional fork-joins. * Split multi-dimensional fork-joins into separate one-dimensional fork-joins.
* Useful for code generation. * Useful for code generation. A single iteration of `fork_split` only splits
* at most one fork-join, it must be called repeatedly to split all fork-joins.
*/ */
pub fn fork_split( pub fn fork_split(
editor: &mut FunctionEditor, editor: &mut FunctionEditor,
......
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use either::Either; use either::Either;
use hercules_ir::get_uses_mut;
use hercules_ir::ir::*;
use hercules_ir::ImmutableDefUseMap;
use crate::FunctionEditor; use hercules_ir::*;
use crate::*;
/* /*
* This is a Hercules IR transformation that: * This is a Hercules IR transformation that:
...@@ -20,20 +19,6 @@ use crate::FunctionEditor; ...@@ -20,20 +19,6 @@ use crate::FunctionEditor;
* guard remains and in these cases the guard is no longer needed. * guard remains and in these cases the guard is no longer needed.
*/ */
/* Given a node index and the node itself, return None if the node is not
* a guarded fork where we can eliminate the guard.
* If the node is a fork with a guard we can eliminate returns a tuple of
* - This node's NodeID
* - The replication factor of the fork
* - The ID of the if of the guard
* - The ID of the projections of the if
* - The guard's predecessor
* - A map of NodeIDs for the phi nodes to the reduce they should be replaced
* with, and also the region that joins the guard's branches mapping to the
* fork's join NodeID
* - If the replication factor is a max that can be eliminated.
*/
// Simplify factors through max // Simplify factors through max
enum Factor { enum Factor {
Max(usize, DynamicConstantID), Max(usize, DynamicConstantID),
...@@ -61,6 +46,19 @@ struct GuardedFork { ...@@ -61,6 +46,19 @@ struct GuardedFork {
factor: Factor, // The factor that matches the guard factor: Factor, // The factor that matches the guard
} }
/* Given a node index and the node itself, return None if the node is not
* a guarded fork where we can eliminate the guard.
* If the node is a fork with a guard we can eliminate returns a tuple of
* - This node's NodeID
* - The replication factor of the fork
* - The ID of the if of the guard
* - The ID of the projections of the if
* - The guard's predecessor
* - A map of NodeIDs for the phi nodes to the reduce they should be replaced
* with, and also the region that joins the guard's branches mapping to the
* fork's join NodeID
* - If the replication factor is a max that can be eliminated.
*/
fn guarded_fork( fn guarded_fork(
editor: &mut FunctionEditor, editor: &mut FunctionEditor,
fork_join_map: &HashMap<NodeID, NodeID>, fork_join_map: &HashMap<NodeID, NodeID>,
...@@ -73,8 +71,7 @@ fn guarded_fork( ...@@ -73,8 +71,7 @@ fn guarded_fork(
return None; return None;
}; };
let factors = factors.iter().enumerate().map(|(idx, dc)| { let mut factors = factors.iter().enumerate().map(|(idx, dc)| {
// FIXME: Can we hide .idx() in an impl Index or something so we don't index Vec<Nodes> iwht DynamicConstantId.idx()
let DynamicConstant::Max(l, r) = *editor.get_dynamic_constant(*dc) else { let DynamicConstant::Max(l, r) = *editor.get_dynamic_constant(*dc) else {
return Factor::Normal(idx, *dc); return Factor::Normal(idx, *dc);
}; };
...@@ -140,24 +137,22 @@ fn guarded_fork( ...@@ -140,24 +137,22 @@ fn guarded_fork(
} }
// Match Factor // Match Factor
let factor = factors.clone().find(|factor| { let factor = factors.find(|factor| {
// This clone on the dc is painful.
match ( match (
&function.nodes[pattern_factor.idx()], &function.nodes[pattern_factor.idx()],
editor.get_dynamic_constant(factor.get_id()).clone(), &*editor.get_dynamic_constant(factor.get_id()),
) { ) {
(Node::Constant { id }, DynamicConstant::Constant(v)) => { (Node::Constant { id }, DynamicConstant::Constant(v)) => {
let Constant::UnsignedInteger64(pattern_v) = *editor.get_constant(*id) let Constant::UnsignedInteger64(pattern_v) = *editor.get_constant(*id)
else { else {
return false; return false;
}; };
pattern_v == (v as u64) pattern_v == (*v as u64)
} }
(Node::DynamicConstant { id }, _) => *id == factor.get_id(), (Node::DynamicConstant { id }, _) => *id == factor.get_id(),
_ => false, _ => false,
} }
}); });
// return Factor
factor factor
}) })
} }
...@@ -184,12 +179,10 @@ fn guarded_fork( ...@@ -184,12 +179,10 @@ fn guarded_fork(
} }
// Match Factor // Match Factor
// FIXME: Implement dc / constant matching as in case where branch_idx == 1 let factor = factors.find(|factor| {
let factor = factors.clone().find(|factor| {
function.nodes[pattern_factor.idx()].try_dynamic_constant() function.nodes[pattern_factor.idx()].try_dynamic_constant()
== Some(factor.get_id()) == Some(factor.get_id())
}); });
// return Factor
factor factor
}) })
} else { } else {
...@@ -229,7 +222,7 @@ fn guarded_fork( ...@@ -229,7 +222,7 @@ fn guarded_fork(
} else { } else {
return None; return None;
}; };
// Other predecessor needs to be the other read from the guard's if // Other predecessor needs to be the other projection from the guard's if
let Node::Projection { let Node::Projection {
control: if_node2, control: if_node2,
ref selection, ref selection,
...@@ -317,8 +310,6 @@ fn guarded_fork( ...@@ -317,8 +310,6 @@ fn guarded_fork(
/* /*
* Top level function to run fork guard elimination, as described above. * Top level function to run fork guard elimination, as described above.
* Deletes nodes by setting nodes to gravestones. Works with a function already
* containing gravestones.
*/ */
pub fn fork_guard_elim(editor: &mut FunctionEditor, fork_join_map: &HashMap<NodeID, NodeID>) { pub fn fork_guard_elim(editor: &mut FunctionEditor, fork_join_map: &HashMap<NodeID, NodeID>) {
let guard_info = editor let guard_info = editor
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment