1010using namespace madspace ;
1111
1212InstructionDependencies::InstructionDependencies (const Function& function) :
13- size (function.instructions().size()), matrix(size * size ) {
13+ _size (function.instructions().size()), _matrix(_size * _size ) {
1414 std::vector<int > local_source (function.locals ().size (), -1 );
1515 int index = 0 ;
1616 for (auto & instr : function.instructions ()) {
@@ -20,26 +20,26 @@ InstructionDependencies::InstructionDependencies(const Function& function) :
2020 if (source_index == -1 ) {
2121 continue ;
2222 }
23- matrix .at (index * size + source_index) = true ;
24- for (int i = 0 ; i < size ; ++i) {
25- matrix .at (index * size + i) =
26- matrix .at (index * size + i) | matrix .at (source_index * size + i);
23+ _matrix .at (index * _size + source_index) = true ;
24+ for (int i = 0 ; i < _size ; ++i) {
25+ _matrix .at (index * _size + i) =
26+ _matrix .at (index * _size + i) | _matrix .at (source_index * _size + i);
2727 }
28- int source_rank = ranks .at (source_index);
28+ int source_rank = _ranks .at (source_index);
2929 if (rank < source_rank) {
3030 rank = source_rank;
3131 }
3232 }
3333 for (auto & output : instr.outputs ) {
3434 local_source.at (output.local_index ) = index;
3535 }
36- ranks .push_back (rank + 1 );
36+ _ranks .push_back (rank + 1 );
3737 ++index;
3838 }
3939}
4040
4141LastUseOfLocals::LastUseOfLocals (const Function& function) :
42- last_used (function.instructions().size()) {
42+ _last_used (function.instructions().size()) {
4343 std::vector<bool > seen_locals;
4444 for (auto & local : function.locals ()) {
4545 seen_locals.push_back (
@@ -50,7 +50,7 @@ LastUseOfLocals::LastUseOfLocals(const Function& function) :
5050 seen_locals.at (output.local_index ) = true ;
5151 }
5252 auto instr = function.instructions ().rbegin ();
53- auto indices = last_used .begin ();
53+ auto indices = _last_used .begin ();
5454 for (; instr != function.instructions ().rend (); ++instr, ++indices) {
5555 for (auto & input : instr->inputs ) {
5656 auto index = input.local_index ;
@@ -60,5 +60,22 @@ LastUseOfLocals::LastUseOfLocals(const Function& function) :
6060 }
6161 }
6262 }
63- std::reverse (last_used.begin (), last_used.end ());
63+ std::reverse (_last_used.begin (), _last_used.end ());
64+ }
65+
66+ Function madspace::sort_breadth_first (const Function& function) {
67+ Function func_out = function;
68+ InstructionDependencies dependencies (function);
69+ auto order = dependencies.ranks ();
70+ std::vector<std::size_t > instruction_perm (function.instructions ().size ());
71+ std::iota (instruction_perm.begin (), instruction_perm.end (), 0 );
72+ std::stable_sort (
73+ instruction_perm.begin (), instruction_perm.end (),
74+ [&](std::size_t i, std::size_t j) { return order.at (i) < order.at (j); }
75+ );
76+ func_out._instructions .clear ();
77+ for (std::size_t index : instruction_perm) {
78+ func_out._instructions .push_back (function._instructions .at (index));
79+ }
80+ return func_out;
6481}
0 commit comments