Newer
Older
use std::collections::{HashMap, HashSet};
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
use bimap::BiMap;
use itertools::Itertools;
use hercules_ir::*;
use crate::*;
type ForkID = usize;
/** Places each reduce node into its own fork */
pub fn default_reduce_partition(
editor: &FunctionEditor,
_fork: NodeID,
join: NodeID,
) -> SparseNodeMap<ForkID> {
let mut map = SparseNodeMap::new();
editor
.get_users(join)
.filter(|id| editor.func().nodes[id.idx()].is_reduce())
.enumerate()
.for_each(|(fork, reduce)| {
map.insert(reduce, fork);
});
map
}
// TODO: Refine these conditions.
/** */
pub fn find_reduce_dependencies<'a>(
function: &'a Function,
reduce: NodeID,
fork: NodeID,
) -> impl IntoIterator<Item = NodeID> + 'a {
let len = function.nodes.len();
let mut visited: DenseNodeMap<bool> = vec![false; len];
let mut depdendent: DenseNodeMap<bool> = vec![false; len];
// Does `fork` need to be a parameter here? It never changes. If this was a closure could it just capture it?
fn recurse(
function: &Function,
node: NodeID,
fork: NodeID,
dependent_map: &mut DenseNodeMap<bool>,
visited: &mut DenseNodeMap<bool>,
) -> () {
// return through dependent_map {
if visited[node.idx()] {
return;
}
visited[node.idx()] = true;
if node == fork {
dependent_map[node.idx()] = true;
return;
}
let binding = get_uses(&function.nodes[node.idx()]);
let uses = binding.as_ref();
for used in uses {
recurse(function, *used, fork, dependent_map, visited);
}
dependent_map[node.idx()] = uses.iter().map(|id| dependent_map[id.idx()]).any(|a| a);
return;
}
// Note: HACKY, the condition wwe want is 'all nodes on any path from the fork to the reduce (in the forward graph), or the reduce to the fork (in the directed graph)
// cycles break this, but we assume for now that the only cycles are ones that involve the reduce node
// NOTE: (control may break this (i.e loop inside fork) is a cycle that isn't the reduce)
// the current solution is just to mark the reduce as dependent at the start of traversing the graph.
depdendent[reduce.idx()] = true;
recurse(function, reduce, fork, &mut depdendent, &mut visited);
// Return node IDs that are dependent
let ret_val: Vec<_> = depdendent
.iter()
.enumerate()
.filter_map(|(idx, dependent)| {
if *dependent {
Some(NodeID::new(idx))
} else {
None
}
})
.collect();
ret_val
}
pub fn copy_subgraph(
editor: &mut FunctionEditor,
subgraph: HashSet<NodeID>,
) -> (
HashSet<NodeID>,
HashMap<NodeID, NodeID>,
Vec<(NodeID, NodeID)>,
) // returns all new nodes, a map from old nodes to new nodes, and
// a vec of pairs of nodes (old node, outside node) s.t old node -> outside node,
// outside means not part of the original subgraph.
{
let mut map: HashMap<NodeID, NodeID> = HashMap::new();
let mut new_nodes: HashSet<NodeID> = HashSet::new();
// Copy nodes
for old_id in subgraph.iter() {
editor.edit(|mut edit| {
let new_id = edit.copy_node(*old_id);
map.insert(*old_id, new_id);
new_nodes.insert(new_id);
Ok(edit)
});
}
// Update edges to new nodes
for old_id in subgraph.iter() {
// Replace all uses of old_id w/ new_id, where the use is in new_node
editor.edit(|edit| {
edit.replace_all_uses_where(*old_id, map[old_id], |node_id| new_nodes.contains(node_id))
});
}
// Get all users that aren't in new_nodes.
let mut outside_users = Vec::new();
for node in new_nodes.iter() {
for user in editor.get_users(*node) {
if !new_nodes.contains(&user) {
outside_users.push((*node, user));
}
}
}
(new_nodes, map, outside_users)
}
pub fn fork_fission<'a>(
editor: &'a mut FunctionEditor,
_control_subgraph: &Subgraph,
_types: &Vec<TypeID>,
_loop_tree: &LoopTree,
fork_join_map: &HashMap<NodeID, NodeID>,
) -> () {
let forks: Vec<_> = editor
.func()
.nodes
.iter()
.enumerate()
.filter_map(|(idx, node)| {
if node.is_fork() {
Some(NodeID::new(idx))
} else {
None
}
})
.collect();
let control_pred = NodeID::new(0);
// This does the reduction fission:
for fork in forks.clone() {
// FIXME: If there is control in between fork and join, don't just give up.
let join = fork_join_map[&fork];
let join_pred = editor.func().nodes[join.idx()].try_join().unwrap();
if join_pred != fork {
todo!("Can't do fork fission on nodes with internal control")
// Inner control LOOPs are hard
// inner control in general *should* work right now without modifications.
}
let reduce_partition = default_reduce_partition(editor, fork, join);
fork_reduce_fission_helper(editor, fork_join_map, reduce_partition, control_pred, fork);
}
}
/** Split a 1D fork into two forks, placing select intermediate data into buffers. */
pub fn fork_bufferize_fission_helper<'a>(
editor: &'a mut FunctionEditor,
fork_join_map: &HashMap<NodeID, NodeID>,
bufferized_edges: HashSet<(NodeID, NodeID)>, // Describes what intermediate data should be bufferized.
_original_control_pred: NodeID, // What the new fork connects to.
types: &Vec<TypeID>,
fork: NodeID,
) -> (NodeID, NodeID) {
// Returns the two forks that it generates.
// TODO: Check that bufferized edges src doesn't depend on anything that comes after the fork.
// Copy fork + control intermediates + join to new fork + join,
// How does control get partitioned?
// (depending on how it affects the data nodes on each side of the bufferized_edges)
// may end up in each loop, fix me later.
// place new fork + join after join of first.
// Only handle fork+joins with no inner control for now.
// Create fork + join + Thread control
let join = fork_join_map[&fork];
let mut new_fork_id = NodeID::new(0);
let mut new_join_id = NodeID::new(0);
editor.edit(|mut edit| {
new_join_id = edit.add_node(Node::Join { control: fork });
let factors = edit.get_node(fork).try_fork().unwrap().1;
new_fork_id = edit.add_node(Node::Fork {
control: new_join_id,
factors: factors.into(),
});
edit.replace_all_uses_where(fork, new_fork_id, |usee| *usee == join)
});
for (src, dst) in bufferized_edges {
// FIXME: Disgusting cloning and allocationing and iterators.
let factors: Vec<_> = editor.func().nodes[fork.idx()]
.try_fork()
.unwrap()
.1
.iter()
.cloned()
.collect();
editor.edit(|mut edit| {
// Create write to buffer
let thread_stuff_it = factors.into_iter().enumerate();
// FIxme: try to use unzip here? Idk why it wasn't working.
let tids = thread_stuff_it.clone().map(|(dim, _)| {
edit.add_node(Node::ThreadID {
control: fork,
dimension: dim,
})
});
let array_dims = thread_stuff_it.clone().map(|(_, factor)| (factor));
// Assume 1-d fork only for now.
// let tid = edit.add_node(Node::ThreadID { control: fork, dimension: 0 });
let position_idx = Index::Position(tids.collect::<Vec<_>>().into_boxed_slice());
let write = edit.add_node(Node::Write {
collect: NodeID::new(0),
data: src,
indices: vec![position_idx].into(),
});
let ele_type = types[src.idx()];
let empty_buffer = edit.add_type(hercules_ir::Type::Array(
ele_type,
array_dims.collect::<Vec<_>>().into_boxed_slice(),
));
let empty_buffer = edit.add_zero_constant(empty_buffer);
let empty_buffer = edit.add_node(Node::Constant { id: empty_buffer });
let reduce = Node::Reduce {
control: new_join_id,
init: empty_buffer,
reduct: write,
};
let reduce = edit.add_node(reduce);
// Fix write node
edit = edit.replace_all_uses_where(NodeID::new(0), reduce, |usee| *usee == write)?;
// Create read from buffer
let tids = thread_stuff_it.clone().map(|(dim, _)| {
edit.add_node(Node::ThreadID {
control: new_fork_id,
dimension: dim,
})
});
let position_idx = Index::Position(tids.collect::<Vec<_>>().into_boxed_slice());
let read = edit.add_node(Node::Read {
collect: reduce,
indices: vec![position_idx].into(),
});
edit = edit.replace_all_uses_where(src, read, |usee| *usee == dst)?;
Ok(edit)
});
}
(fork, new_fork_id)
}
/** Split a 1D fork into a separate fork for each reduction. */
pub fn fork_reduce_fission_helper<'a>(
editor: &'a mut FunctionEditor,
fork_join_map: &HashMap<NodeID, NodeID>,
reduce_partition: SparseNodeMap<ForkID>, // Describes how the reduces of the fork should be split,
original_control_pred: NodeID, // What the new fork connects to.
fork: NodeID,
) -> (NodeID, NodeID) {
let join = fork_join_map[&fork];
let mut new_control_pred: NodeID = original_control_pred;
// Important edges are: Reduces,
// NOTE:
// Say two reduce are in a fork, s.t reduce A depends on reduce B
// If user wants A and B in separate forks:
// - we can simply refuse
// - or we can duplicate B
let mut new_fork = NodeID::new(0);
let mut new_join = NodeID::new(0);
// Gets everything between fork & join that this reduce needs. (ALL CONTROL)
for reduce in reduce_partition {
let reduce = reduce.0;
let function = editor.func();
let subgraph = find_reduce_dependencies(function, reduce, fork);
let mut subgraph: HashSet<NodeID> = subgraph.into_iter().collect();
subgraph.insert(join);
subgraph.insert(fork);
subgraph.insert(reduce);
let (_, mapping, _) = copy_subgraph(editor, subgraph);
new_fork = mapping[&fork];
new_join = mapping[&join];
editor.edit(|mut edit| {
// Atttach new_fork after control_pred
let (old_control_pred, _) = edit.get_node(new_fork).try_fork().unwrap().clone();
edit = edit.replace_all_uses_where(old_control_pred, new_control_pred, |usee| {
*usee == new_fork
})?;
// Replace uses of reduce
edit = edit.replace_all_uses(reduce, mapping[&reduce])?;
Ok(edit)
});
new_control_pred = new_join;
}
editor.edit(|mut edit| {
// Replace original join w/ new final join
edit = edit.replace_all_uses_where(join, new_join, |_| true)?;
// Delete original join (all reduce users have been moved)
edit = edit.delete_node(join)?;
// Replace all users of original fork, and then delete it, leftover users will be DCE'd.
edit = edit.replace_all_uses(fork, new_fork)?;
edit.delete_node(fork)
});
(new_fork, new_join)
}
pub fn fork_coalesce(
editor: &mut FunctionEditor,
loops: &LoopTree,
fork_join_map: &HashMap<NodeID, NodeID>,
) -> bool {
let fork_joins = loops.bottom_up_loops().into_iter().filter_map(|(k, _)| {
if editor.func().nodes[k.idx()].is_fork() {
Some(k)
} else {
None
}
});
let fork_joins: Vec<_> = fork_joins.collect();
// FIXME: Add a postorder traversal to optimize this.
// FIXME: This could give us two forks that aren't actually ancestors / related, but then the helper will just return false early.
// something like: `fork_joins.postorder_iter().windows(2)` is ideal here.
for (inner, outer) in fork_joins.iter().cartesian_product(fork_joins.iter()) {
if fork_coalesce_helper(editor, *outer, *inner, fork_join_map) {
return true;
}
}
return false;
}
/** Opposite of fork split, takes two fork-joins
with no control between them, and merges them into a single fork-join.
*/
pub fn fork_coalesce_helper(
editor: &mut FunctionEditor,
outer_fork: NodeID,
inner_fork: NodeID,
fork_join_map: &HashMap<NodeID, NodeID>,
) -> bool {
// Check that all reduces in the outer fork are in *simple* cycles with a unique reduce of the inner fork.
let outer_join = fork_join_map[&outer_fork];
let inner_join = fork_join_map[&inner_fork];
let mut pairs: BiMap<NodeID, NodeID> = BiMap::new(); // Outer <-> Inner
// FIXME: Iterate all control uses of joins to really collect all reduces
// (reduces can be attached to inner control)
for outer_reduce in editor
.get_users(outer_join)
.filter(|node| editor.func().nodes[node.idx()].is_reduce())
{
// check that inner reduce is of the inner join
let (_, _, outer_reduct) = editor.func().nodes[outer_reduce.idx()]
.try_reduce()
.unwrap();
let inner_reduce = outer_reduct;
let inner_reduce_node = &editor.func().nodes[outer_reduct.idx()];
let Node::Reduce {
control: inner_control,
init: inner_init,
reduct: _,
} = inner_reduce_node
else {
return false;
};
// FIXME: check this condition better (i.e reduce might not be attached to join)
if *inner_control != inner_join {
return false;
};
if *inner_init != outer_reduce {
return false;
};
if pairs.contains_left(&outer_reduce) || pairs.contains_right(&inner_reduce) {
return false;
} else {
pairs.insert(outer_reduce, inner_reduce);
}
}
// Check for control between join-join and fork-fork
let Some(user) = editor
.get_users(outer_fork)
.filter(|node| editor.func().nodes[node.idx()].is_control())
.next()
else {
return false;
};
if user != inner_fork {
return false;
}
let Some(user) = editor
.get_users(inner_join)
.filter(|node| editor.func().nodes[node.idx()].is_control())
.next()
else {
return false;
};
if user != outer_join {
return false;
}
// Checklist:
// Increment inner TIDs
// Add outer fork's dimension to front of inner fork.
// Fuse reductions
// - Initializer becomes outer initializer
// Replace uses of outer fork w/ inner fork.
// Replace uses of outer join w/ inner join.
// Delete outer fork-join
let inner_tids: Vec<NodeID> = editor
.get_users(inner_fork)
.filter(|node| editor.func().nodes[node.idx()].is_thread_id())
.collect();
let (outer_pred, outer_dims) = editor.func().nodes[outer_fork.idx()].try_fork().unwrap();
let (_, inner_dims) = editor.func().nodes[inner_fork.idx()].try_fork().unwrap();
let num_outer_dims = outer_dims.len();
let mut new_factors = outer_dims.to_vec();
// CHECKME / FIXME: Might need to be added the other way.
new_factors.append(&mut inner_dims.to_vec());
for tid in inner_tids {
let (fork, dim) = editor.func().nodes[tid.idx()].try_thread_id().unwrap();
let new_tid = Node::ThreadID {
control: fork,
dimension: dim + num_outer_dims,
};
editor.edit(|mut edit| {
let new_tid = edit.add_node(new_tid);
let edit = edit.replace_all_uses(tid, new_tid)?;
Ok(edit)
});
}
// Fuse Reductions
for (outer_reduce, inner_reduce) in pairs {
let (_, outer_init, _) = editor.func().nodes[outer_reduce.idx()]
.try_reduce()
.unwrap();
let (_, inner_init, _) = editor.func().nodes[inner_reduce.idx()]
.try_reduce()
.unwrap();
editor.edit(|mut edit| {
// Set inner init to outer init.
edit =
edit.replace_all_uses_where(inner_init, outer_init, |usee| *usee == inner_reduce)?;
edit = edit.replace_all_uses(outer_reduce, inner_reduce)?;
edit = edit.delete_node(outer_reduce)?;
Ok(edit)
});
}
editor.edit(|mut edit| {
let new_fork = Node::Fork {
control: outer_pred,
factors: new_factors.into(),
};
let new_fork = edit.add_node(new_fork);
edit = edit.replace_all_uses(inner_fork, new_fork)?;
edit = edit.replace_all_uses(outer_fork, new_fork)?;
edit = edit.replace_all_uses(outer_join, inner_join)?;
edit = edit.delete_node(outer_join)?;
edit = edit.delete_node(inner_fork)?;
edit = edit.delete_node(outer_fork)?;
Ok(edit)
});
true
}
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
pub fn split_all_forks(
editor: &mut FunctionEditor,
fork_join_map: &HashMap<NodeID, NodeID>,
reduce_cycles: &HashMap<NodeID, HashSet<NodeID>>,
) {
for (fork, join) in fork_join_map {
if let Some((forks, _)) = split_fork(editor, *fork, *join, reduce_cycles)
&& forks.len() > 1
{
break;
}
}
}
/*
* Split multi-dimensional fork-joins into separate one-dimensional fork-joins.
* 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(crate) fn split_fork(
editor: &mut FunctionEditor,
fork: NodeID,
join: NodeID,
reduce_cycles: &HashMap<NodeID, HashSet<NodeID>>,
) -> Option<(Vec<NodeID>, Vec<NodeID>)> {
// A single multi-dimensional fork becomes multiple forks, a join becomes
// multiple joins, a thread ID becomes a thread ID on the correct
// fork, and a reduce becomes multiple reduces to shuffle the reduction
// value through the fork-join nest.
let nodes = &editor.func().nodes;
let (fork_control, factors) = nodes[fork.idx()].try_fork().unwrap();
if factors.len() < 2 {
return Some((vec![fork], vec![join]));
}
let factors: Box<[DynamicConstantID]> = factors.into();
let join_control = nodes[join.idx()].try_join().unwrap();
let tids: Vec<_> = editor
.get_users(fork)
.filter(|id| nodes[id.idx()].is_thread_id())
.collect();
let reduces: Vec<_> = editor
.get_users(join)
.filter(|id| nodes[id.idx()].is_reduce())
.collect();
let data_in_reduce_cycle: HashSet<(NodeID, NodeID)> = reduces
.iter()
.map(|reduce| editor.get_users(*reduce).map(move |user| (user, *reduce)))
.flatten()
.filter(|(user, reduce)| reduce_cycles[&reduce].contains(&user))
.collect();
let mut new_forks = vec![];
let mut new_joins = vec![];
let success = editor.edit(|mut edit| {
// Create the forks and a thread ID per fork.
let mut acc_fork = fork_control;
let mut new_tids = vec![];
for factor in factors {
acc_fork = edit.add_node(Node::Fork {
control: acc_fork,
factors: Box::new([factor]),
});
new_forks.push(acc_fork);
edit.sub_edit(fork, acc_fork);
new_tids.push(edit.add_node(Node::ThreadID {
control: acc_fork,
dimension: 0,
}));
}
// Create the joins.
let mut acc_join = if join_control == fork {
acc_fork
} else {
join_control
};
for _ in new_tids.iter() {
acc_join = edit.add_node(Node::Join { control: acc_join });
edit.sub_edit(join, acc_join);
new_joins.push(acc_join);
}
// Create the reduces.
let mut new_reduces = vec![];
for reduce in reduces.iter() {
let (_, init, reduct) = edit.get_node(*reduce).try_reduce().unwrap();
let num_nodes = edit.num_node_ids();
let mut inner_reduce = NodeID::new(0);
let mut outer_reduce = NodeID::new(0);
for (join_idx, join) in new_joins.iter().enumerate() {
let init = if join_idx == new_joins.len() - 1 {
init
} else {
NodeID::new(num_nodes + join_idx + 1)
};
let reduct = if join_idx == 0 {
reduct
} else {
NodeID::new(num_nodes + join_idx - 1)
};
let new_reduce = edit.add_node(Node::Reduce {
control: *join,
init,
reduct,
});
assert_eq!(new_reduce, NodeID::new(num_nodes + join_idx));
edit.sub_edit(*reduce, new_reduce);
if join_idx == 0 {
inner_reduce = new_reduce;
}
if join_idx == new_joins.len() - 1 {
outer_reduce = new_reduce;
}
}
new_reduces.push((inner_reduce, outer_reduce));
}
// Replace everything.
edit = edit.replace_all_uses(fork, acc_fork)?;
edit = edit.replace_all_uses(join, acc_join)?;
for tid in tids.iter() {
let dim = edit.get_node(*tid).try_thread_id().unwrap().1;
edit.sub_edit(*tid, new_tids[dim]);
edit = edit.replace_all_uses(*tid, new_tids[dim])?;
}
for (reduce, (inner_reduce, outer_reduce)) in zip(reduces.iter(), new_reduces) {
edit = edit.replace_all_uses_where(*reduce, inner_reduce, |id| {
data_in_reduce_cycle.contains(&(*id, *reduce))
})?;
edit = edit.replace_all_uses_where(*reduce, outer_reduce, |id| {
!data_in_reduce_cycle.contains(&(*id, *reduce))
})?;
}
// Delete all the old stuff.
edit = edit.delete_node(fork)?;
edit = edit.delete_node(join)?;
for tid in tids {
edit = edit.delete_node(tid)?;
}
for reduce in reduces {
edit = edit.delete_node(reduce)?;
}
Ok(edit)
});
if success {
Some((new_forks, new_joins))
} else {
None
}
}
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
pub fn chunk_all_forks_unguarded(
editor: &mut FunctionEditor,
fork_join_map: &HashMap<NodeID, NodeID>,
dim_idx: usize,
tile_size: usize,
) -> () {
// Add dc
let mut dc_id = DynamicConstantID::new(0);
editor.edit(|mut edit| {
dc_id = edit.add_dynamic_constant(DynamicConstant::Constant(tile_size));
Ok(edit)
});
for (fork, _) in fork_join_map {
chunk_fork_unguarded(editor, *fork, dim_idx, dc_id);
}
}
// Splits a dimension of a single fork join into multiple.
// Iterates an outer loop original_dim / tile_size times
// adds a tile_size loop as the inner loop
// Assumes that tile size divides original dim evenly.
pub fn chunk_fork_unguarded(
editor: &mut FunctionEditor,
fork: NodeID,
dim_idx: usize,
tile_size: DynamicConstantID,
) -> () {
// tid_dim_idx = tid_dim_idx * tile_size + tid_(dim_idx + 1)
let Node::Fork {
control: old_control,
factors: ref old_factors,
} = *editor.node(fork)
else {
return;
};
assert!(dim_idx < old_factors.len());
let mut new_factors: Vec<_> = old_factors.to_vec();
let fork_users: Vec<_> = editor
.get_users(fork)
.map(|f| (f, editor.node(f).clone()))
.collect();
editor.edit(|mut edit| {
let outer = DynamicConstant::div(new_factors[dim_idx], tile_size);
new_factors.insert(dim_idx + 1, tile_size);
new_factors[dim_idx] = edit.add_dynamic_constant(outer);
let new_fork = Node::Fork {
control: old_control,
factors: new_factors.into(),
};
let new_fork = edit.add_node(new_fork);
edit = edit.replace_all_uses(fork, new_fork)?;
for (tid, node) in fork_users {
let Node::ThreadID {
control: _,
dimension: tid_dim,
} = node
else {
continue;
};
if tid_dim > dim_idx {
let new_tid = Node::ThreadID {
control: new_fork,
dimension: tid_dim + 1,
};
let new_tid = edit.add_node(new_tid);
edit = edit.replace_all_uses(tid, new_tid)?;
edit = edit.delete_node(tid)?;
} else if tid_dim == dim_idx {
let tile_tid = Node::ThreadID {
control: new_fork,
dimension: tid_dim + 1,
};
let tile_tid = edit.add_node(tile_tid);
let tile_size = edit.add_node(Node::DynamicConstant { id: tile_size });
let mul = edit.add_node(Node::Binary {
left: tid,
right: tile_size,
op: BinaryOperator::Mul,
});
let add = edit.add_node(Node::Binary {
left: mul,
right: tile_tid,
op: BinaryOperator::Add,
});
edit = edit.replace_all_uses_where(tid, add, |usee| *usee != mul)?;
}
}
edit = edit.delete_node(fork)?;
Ok(edit)
});
}
pub fn merge_all_fork_dims(editor: &mut FunctionEditor, fork_join_map: &HashMap<NodeID, NodeID>) {
for (fork, _) in fork_join_map {
let Node::Fork {
control: _,
factors: dims,
} = editor.node(fork)
else {
unreachable!();
};
let mut fork = *fork;
for _ in 0..dims.len() - 1 {
let outer = 0;
let inner = 1;
fork = fork_dim_merge(editor, fork, outer, inner);
}
}
}
pub fn fork_dim_merge(
editor: &mut FunctionEditor,
fork: NodeID,
dim_idx1: usize,
dim_idx2: usize,
) -> NodeID {
// tid_dim_idx1 (replaced w/) <- dim_idx1 / dim(dim_idx2)
// tid_dim_idx2 (replaced w/) <- dim_idx1 % dim(dim_idx2)
assert_ne!(dim_idx1, dim_idx2);
// Outer is smaller, and also closer to the left of the factors array.
let (outer_idx, inner_idx) = if dim_idx2 < dim_idx1 {
(dim_idx2, dim_idx1)
} else {
(dim_idx1, dim_idx2)
};
let Node::Fork {
control: old_control,
factors: ref old_factors,
} = *editor.node(fork)
else {
return fork;
};
let mut new_factors: Vec<_> = old_factors.to_vec();
let fork_users: Vec<_> = editor
.get_users(fork)
.map(|f| (f, editor.node(f).clone()))
.collect();
let mut new_nodes = vec![];
let outer_dc_id = new_factors[outer_idx];
let inner_dc_id = new_factors[inner_idx];
let mut new_fork = NodeID::new(0);
editor.edit(|mut edit| {
new_factors[outer_idx] = edit.add_dynamic_constant(DynamicConstant::mul(
new_factors[outer_idx],
new_factors[inner_idx],
));
new_factors.remove(inner_idx);
new_fork = edit.add_node(Node::Fork {
control: old_control,
factors: new_factors.into(),
});
edit.sub_edit(fork, new_fork);
edit = edit.replace_all_uses(fork, new_fork)?;
edit = edit.delete_node(fork)?;
for (tid, node) in fork_users {
let Node::ThreadID {
control: _,
dimension: tid_dim,
} = node
else {
continue;
};
if tid_dim > inner_idx {
let new_tid = Node::ThreadID {
control: new_fork,
dimension: tid_dim - 1,
};
let new_tid = edit.add_node(new_tid);
edit = edit.replace_all_uses(tid, new_tid)?;
edit.sub_edit(tid, new_tid);
} else if tid_dim == outer_idx {
let outer_tid = Node::ThreadID {
control: new_fork,
dimension: outer_idx,
};
let outer_tid = edit.add_node(outer_tid);
let outer_dc = edit.add_node(Node::DynamicConstant { id: outer_dc_id });
new_nodes.push(outer_tid);
// inner_idx % dim(outer_idx)
let rem = edit.add_node(Node::Binary {
left: outer_tid,
right: outer_dc,
op: BinaryOperator::Rem,
});
edit.sub_edit(tid, rem);
edit = edit.replace_all_uses(tid, rem)?;
} else if tid_dim == inner_idx {
let outer_tid = Node::ThreadID {
control: new_fork,
dimension: outer_idx,
};
let outer_tid = edit.add_node(outer_tid);
let outer_dc = edit.add_node(Node::DynamicConstant { id: outer_dc_id });
// inner_idx / dim(outer_idx)
let div = edit.add_node(Node::Binary {
left: outer_tid,
right: outer_dc,
op: BinaryOperator::Div,
});
edit.sub_edit(tid, div);
edit = edit.replace_all_uses(tid, div)?;
}
}
Ok(edit)
});
new_fork
}