Skip to content

Commit

Permalink
Fix coalescing bug
Browse files Browse the repository at this point in the history
  • Loading branch information
pubby committed Mar 8, 2023
1 parent 27457af commit 7ddb2b7
Show file tree
Hide file tree
Showing 7 changed files with 72 additions and 24 deletions.
2 changes: 0 additions & 2 deletions src/asm_proc.cpp
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
#include "asm_proc.hpp"

#include <iostream> // TODO

#include "flat/small_set.hpp"

#include "format.hpp"
Expand Down
65 changes: 54 additions & 11 deletions src/cg.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,6 @@
#include "rom.hpp"
#include "asm_graph.hpp" // TODO

#include <iostream> // TODO

// TODO: make this way more efficient
/*
static bool _reaching(ssa_ht def, cfg_ht cfg, ssa_ht use, fc::vector_set<cfg_ht>& visited)
Expand Down Expand Up @@ -463,8 +461,11 @@ std::size_t code_gen(log_t* log, ir_t& ir, fn_t& fn)
// Estimate an upper bound for the number of nodes needed here:
unsigned reserve = 0;
for(auto& pair : global_loc_map)
for(auto& pair : pair.second.const_stores)
reserve += pair.second.size() - 1;
for(auto& pair : pair.second.const_stores)
{
assert(pair.second.size() > 0);
reserve += pair.second.size() - 1;
}

// Then reserve extra space:
ssa_data_pool::resize<ssa_cg_d>(ssa_pool::array_size() + reserve);
Expand Down Expand Up @@ -508,7 +509,7 @@ std::size_t code_gen(log_t* log, ir_t& ir, fn_t& fn)
///////////////////////////

calc_ssa_liveness(ir, ssa_pool::array_size() + reserve);

// Note: once the live sets have been built, the IR cannot be modified
// until all liveness checks are done.
// Otherwise, the intersection tests will be buggy.
Expand All @@ -519,7 +520,7 @@ std::size_t code_gen(log_t* log, ir_t& ir, fn_t& fn)

// Coalesce locators.

auto const prune_early_store = [&](ssa_ht store) -> ssa_ht
auto const prune_early_store = [&](ssa_ht store, ssa_ht* new_head = nullptr) -> ssa_ht
{
assert(store->op() == SSA_early_store);

Expand All @@ -541,6 +542,28 @@ std::size_t code_gen(log_t* log, ir_t& ir, fn_t& fn)
clear_liveness_for(ir, parent);
calc_ssa_liveness(parent);

// Because parent's liveness changed, it may no longer be compatible with its cset.
// Thus, we'll remove it if necessary:
for(ssa_ht i = cset_head(parent); i; i = cset_next(i))
{
if(i == parent)
continue;

for(ssa_ht s : cache.special)
if(special_interferes(fn.handle(), ir, cset_locator(i), s))
if(live_at_def(parent, s))
goto remove;

if(live_at_def(parent, i))
{
remove:
ssa_ht const nh = cset_remove(parent);
if(new_head)
*new_head = nh;
break;
}
}

return ret;
};

Expand Down Expand Up @@ -585,20 +608,40 @@ std::size_t code_gen(log_t* log, ir_t& ir, fn_t& fn)
// but also with some SSA_phi nodes.
for(auto& pair : global_loc_map)
{
locator_t const loc = pair.first;
auto& ld = pair.second;

// Prioritize less busy ranges over larger ones.
for(copy_t& copy : ld.copies)
copy.cost = live_range_busyness(ir, copy.node);
std::sort(ld.copies.begin(), ld.copies.end(),
[](copy_t const& a, copy_t const& b) { return a.cost < b.cost; });
}

// Do the coalescing for early stores first:
for(auto& pair : global_loc_map)
{
locator_t const loc = pair.first;
auto& ld = pair.second;

// Do the coalescing:
for(copy_t const& copy : ld.copies)
if(!coalesce_loc(loc, ld, copy.node))
if(copy.node->op() == SSA_early_store)
prune_early_store(copy.node);
{
if(copy.node->op() == SSA_early_store)
if(!coalesce_loc(loc, ld, copy.node))
prune_early_store(copy.node, &ld.cset);
}
}

// Then do the coalescing for the others:
for(auto& pair : global_loc_map)
{
locator_t const loc = pair.first;
auto& ld = pair.second;

for(copy_t const& copy : ld.copies)
{
if(copy.node->op() && copy.node->op() != SSA_early_store)
!coalesce_loc(loc, ld, copy.node);
}
}

// Now insert early_stores for constants, trying to minimize the amount of stores needed.
Expand Down
20 changes: 12 additions & 8 deletions src/cg_cset.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ ssa_ht cset_next(ssa_ht h)
ssa_ht cset_head(ssa_ht h)
{
assert(h);
assert(h->op());
passert(h->op(), h, h->op());
while(true)
{
// TODO: update pointers to point to head.
Expand All @@ -70,8 +70,6 @@ locator_t cset_locator(ssa_ht const h, bool convert_ssa)
if(d.cset_head.is_locator())
{
locator_t const loc = d.cset_head.locator();
//if(loc.mem_head() != loc)
//std::cout << loc << ' ' << loc.mem_head() << std::endl;
assert(loc.mem_head() == loc);
return loc;
}
Expand Down Expand Up @@ -111,35 +109,37 @@ static void cset_merge_locators(ssa_ht head_a, ssa_ht head_b)
assert(false);
}

void cset_remove(ssa_ht h)
ssa_ht cset_remove(ssa_ht h)
{
assert(h);
assert(h->op());

ssa_ht const head = cset_head(h);
ssa_ht ret;

assert(head);
assert(head->op());

if(h == head)
{
ssa_ht const next = cset_next(head);
ssa_ht const next = ret = cset_next(head);

if(!next)
{
cg_data(head).cset_head = {};
return;
return ret;
}

assert(next != head);
//std::cout << next.index << " : " << cg_data(head).cset_head << '\n';
for(ssa_ht it = next; it; it = cset_next(it))
cg_data(it).cset_head = next;
cg_data(next).cset_head = cg_data(head).cset_head;
assert(cg_data(next).cset_head != next);
}
else
{
ret = head;

// Re-write the head pointers in case 'h' is a head.
assert(cset_next(head));
for(ssa_ht it = cset_next(head); it; it = cset_next(it))
Expand All @@ -157,6 +157,8 @@ void cset_remove(ssa_ht h)
// Clear 'h' data:
cg_data(h).cset_head = {};
cg_data(h).cset_next = {};

return ret;
}

// Appends 'h' onto the set of 'last'.
Expand Down Expand Up @@ -227,7 +229,7 @@ bool special_interferes(fn_ht fn, ir_t const& ir, locator_t loc, ssa_ht fn_node)
}
case LOC_ARG:
case LOC_RETURN:
return loc.fn() != called || called->ir_calls().test(loc.fn().id);
return loc.fn() == called || called->ir_calls().test(loc.fn().id);
default:
return false;
}
Expand Down Expand Up @@ -259,6 +261,8 @@ ssa_ht csets_dont_interfere(fn_ht fn, ir_t const& ir, ssa_ht a, ssa_ht b, cset_i
assert(a && b);
assert(cset_is_head(a));
assert(cset_is_head(b));
assert(a->op());
assert(b->op());

if(a == b)
{
Expand Down
2 changes: 1 addition & 1 deletion src/cg_cset.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ locator_t cset_locator(ssa_ht h, bool convert_ssa = false);

bool cset_locators_mergable(locator_t a, locator_t b);

void cset_remove(ssa_ht h);
ssa_ht cset_remove(ssa_ht h);

ssa_ht cset_append(ssa_value_t last, ssa_ht h);

Expand Down
1 change: 1 addition & 0 deletions src/cg_liveness.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,7 @@ unsigned calc_ssa_liveness(ir_t const& ir, unsigned pool_size)
using namespace liveness_impl;
cg_data_resize();
bitset_pool.clear();
assert(pool_size >= ssa_pool::size());
set_size = ::bitset_size<>(pool_size);

for(cfg_ht cfg_it = ir.cfg_begin(); cfg_it; ++cfg_it)
Expand Down
4 changes: 3 additions & 1 deletion src/globals.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1148,14 +1148,16 @@ void fn_t::compile()
// Thus, they must occur sequentially.
reset_ai_prep();
save_graph(ir, fmt("pre_loop_%_%", post_byteified, iter).c_str());
//RUN_O(o_loop, log, ir, post_byteified);
RUN_O(o_loop, log, ir, post_byteified);
save_graph(ir, fmt("pre_ai_%_%", post_byteified, iter).c_str());
RUN_O(o_abstract_interpret, log, ir, post_byteified);
save_graph(ir, fmt("post_ai_%_%", post_byteified, iter).c_str());

RUN_O(o_remove_unused_ssa, log, ir);

save_graph(ir, fmt("pre_motion_%_%", post_byteified, iter).c_str());
RUN_O(o_motion, log, ir);
save_graph(ir, fmt("post_motion_%_%", post_byteified, iter).c_str());

if(post_byteified)
{
Expand Down
2 changes: 1 addition & 1 deletion src/o_motion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ class run_gvn_t
for(ssa_ht ssa_it = cfg_node.ssa_begin(); ssa_it; ++ssa_it)
{
if(ssa_it->in_daisy()
|| (ssa_flags(ssa_it->op()) & (SSAF_NO_GVN | SSAF_WRITE_ARRAY))
|| (ssa_flags(ssa_it->op()) & (SSAF_NO_GVN | SSAF_WRITE_ARRAY | SSAF_IO_IMPURE))
|| !pure(*ssa_it))
{
data(ssa_it).gvn = m_next_gvn++;
Expand Down

0 comments on commit 7ddb2b7

Please sign in to comment.