Skip to content

Commit

Permalink
cleanup cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
michael-kenzel committed Aug 4, 2023
1 parent d360324 commit a6db674
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 33 deletions.
45 changes: 22 additions & 23 deletions src/thorin/transform/cleanup_world.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@ class Cleaner {
: world_(world)
{}

World& world() { return world_; }
void cleanup();
void eliminate_tail_rec();
void eta_conversion();
Expand Down Expand Up @@ -49,7 +48,7 @@ void Cleaner::eliminate_tail_rec() {
} else if (use->isa_nom<Param>())
continue; // ignore params

world().ELOG("non-recursive usage of {} index:{} use:{}", scope.entry()->name(), use.index(), use.def()->to_string());
world_.ELOG("non-recursive usage of {} index:{} use:{}", scope.entry()->name(), use.index(), use.def()->to_string());
only_tail_calls = false;
break;
}
Expand Down Expand Up @@ -88,7 +87,7 @@ void Cleaner::eliminate_tail_rec() {
}

if (new_args.size() != n) {
world().DLOG("tail recursive: {}", entry);
world_.DLOG("tail recursive: {}", entry);
auto dropped = drop(scope, args);

entry->jump(dropped, new_args);
Expand All @@ -102,7 +101,7 @@ void Cleaner::eliminate_tail_rec() {
void Cleaner::eta_conversion() {
for (bool todo = true; todo;) {
todo = false;
for (auto def : world().copy_defs()) {
for (auto def : world_.copy_defs()) {
auto continuation = def->isa_nom<Continuation>();
if (!continuation || !continuation->has_body()) continue;

Expand All @@ -111,7 +110,7 @@ void Cleaner::eta_conversion() {
auto body = continuation->body();
if (callee == continuation) break;

if (callee->has_body() && !world().is_external(callee) && callee->can_be_inlined()) {
if (callee->has_body() && !world_.is_external(callee) && callee->can_be_inlined()) {
auto callee_body = callee->body();
for (size_t i = 0, e = body->num_args(); i != e; ++i)
callee->param(i)->replace_uses(body->arg(i));
Expand All @@ -131,7 +130,7 @@ void Cleaner::eta_conversion() {
// try to subsume continuations which call a parameter
// (that is free within that continuation) with that parameter
if (auto param = body->callee()->isa<Param>()) {
if (param->continuation() == continuation || world().is_external(continuation))
if (param->continuation() == continuation || world_.is_external(continuation))
continue;

if (body->args() == continuation->params_as_defs()) {
Expand Down Expand Up @@ -181,11 +180,11 @@ void Cleaner::eta_conversion() {

void Cleaner::eliminate_params() {
// TODO
for (auto ocontinuation : world().copy_continuations()) {
for (auto ocontinuation : world_.copy_continuations()) {
std::vector<size_t> proxy_idx;
std::vector<size_t> param_idx;

if (ocontinuation->has_body() && !world().is_external(ocontinuation)) {
if (ocontinuation->has_body() && !world_.is_external(ocontinuation)) {
auto obody = ocontinuation->body();
for (auto use : ocontinuation->uses()) {
if (use.index() != 0 || !use->isa_nom<Continuation>())
Expand All @@ -201,8 +200,8 @@ void Cleaner::eliminate_params() {
}

if (!proxy_idx.empty()) {
auto ncontinuation = world().continuation(
world().fn_type(ocontinuation->type()->ops().cut(proxy_idx)),
auto ncontinuation = world_.continuation(
world_.fn_type(ocontinuation->type()->ops().cut(proxy_idx)),
ocontinuation->attributes(), ocontinuation->debug_history());
size_t j = 0;
for (auto i : param_idx) {
Expand Down Expand Up @@ -236,14 +235,14 @@ void Cleaner::rebuild() {
importer.type_old2new_.rehash(world_.types().capacity());
importer.def_old2new_.rehash(world_.defs().capacity());

for (auto&& [_, cont] : world().externals()) {
for (auto&& [_, cont] : world_.externals()) {
if (cont->is_exported())
importer.import(cont);
}

swap(importer.world(), world_);

// verify(world());
// verify(world_);

todo_ |= importer.todo();
}
Expand All @@ -262,20 +261,20 @@ void Cleaner::verify_closedness() {
}
};

for (auto def : world().defs())
for (auto def : world_.defs())
check(def);
}

void Cleaner::within(const Def* def) {
if (def->isa<Param>()) return; // TODO remove once Params are within World's sea of nodes
assert(world().types().contains(def->type()));
assert_unused(world().defs().contains(def));
assert(world_.types().contains(def->type()));
assert_unused(world_.defs().contains(def));
}

void Cleaner::clean_pe_info(std::queue<Continuation*> queue, Continuation* cur) {
assert(cur->has_body());
auto body = cur->body();
assert(body->arg(1)->type() == world().ptr_type(world().indefinite_array_type(world().type_pu8())));
assert(body->arg(1)->type() == world_.ptr_type(world_.indefinite_array_type(world_.type_pu8())));
auto next = body->arg(3);
auto msg = body->arg(1)->as<Bitcast>()->from()->as<Global>()->init()->as<DefiniteArray>();

Expand All @@ -296,7 +295,7 @@ void Cleaner::clean_pe_infos() {
queue.push(continuation);
};

for (auto&& [_, cont] : world().externals())
for (auto&& [_, cont] : world_.externals())
if (cont->has_body()) enqueue(cont);

while (!queue.empty()) {
Expand Down Expand Up @@ -326,9 +325,9 @@ void Cleaner::cleanup_fix_point() {
eta_conversion();
eliminate_params();
rebuild(); // resolve replaced defs before going to resolve_loads
todo_ |= resolve_loads(world());
todo_ |= resolve_loads(world_);
rebuild();
if (!world().is_pe_done())
if (!world_.is_pe_done())
todo_ |= partial_evaluation(world_);
else
clean_pe_infos();
Expand All @@ -339,9 +338,9 @@ void Cleaner::cleanup() {
world_.VLOG("start cleanup");
cleanup_fix_point();

if (!world().is_pe_done()) {
world().mark_pe_done();
for (auto def : world().defs()) {
if (!world_.is_pe_done()) {
world_.mark_pe_done();
for (auto def : world_.defs()) {
if (auto cont = def->isa_nom<Continuation>())
cont->destroy_filter();
}
Expand All @@ -353,7 +352,7 @@ void Cleaner::cleanup() {
world_.VLOG("end cleanup");
#if THORIN_ENABLE_CHECKS
verify_closedness();
debug_verify(world());
debug_verify(world_);
#endif
}

Expand Down
20 changes: 10 additions & 10 deletions src/thorin/transform/importer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ const Def* Importer::import(const Def* odef) {
Array<const Def*> new_conditions(ofilter->num_ops());
for (size_t i = 0, e = ofilter->size(); i != e; ++i)
new_conditions[i] = import(ofilter->condition(i));
auto nfilter = world().filter(new_conditions, ofilter->debug());
auto nfilter = world_.filter(new_conditions, ofilter->debug());
return nfilter;
}

Expand All @@ -56,13 +56,13 @@ const Def* Importer::import(const Def* odef) {
assert(!ocontinuation->dead_);
// TODO maybe we want to deal with intrinsics in a more streamlined way
if (ocontinuation == ocontinuation->world().branch())
return def_old2new_[ocontinuation] = world().branch();
return def_old2new_[ocontinuation] = world_.branch();
if (ocontinuation == ocontinuation->world().end_scope())
return def_old2new_[ocontinuation] = world().end_scope();
return def_old2new_[ocontinuation] = world_.end_scope();
auto npi = import(ocontinuation->type())->as<FnType>();
ncontinuation = world().continuation(npi, ocontinuation->attributes(), ocontinuation->debug_history());
assert(&ncontinuation->world() == &world());
assert(&npi->table() == &world());
ncontinuation = world_.continuation(npi, ocontinuation->attributes(), ocontinuation->debug_history());
assert(&ncontinuation->world() == &world_);
assert(&npi->table() == &world_);
for (size_t i = 0, e = ocontinuation->num_params(); i != e; ++i) {
ncontinuation->param(i)->set_name(ocontinuation->param(i)->debug_history().name);
def_old2new_[ocontinuation->param(i)] = ncontinuation->param(i);
Expand All @@ -71,24 +71,24 @@ const Def* Importer::import(const Def* odef) {
def_old2new_[ocontinuation] = ncontinuation;

if (ocontinuation->is_external())
world().make_external(ncontinuation);
world_.make_external(ncontinuation);
}

size_t size = odef->num_ops();
Array<const Def*> nops(size);
for (size_t i = 0; i != size; ++i) {
assert(odef->op(i) != odef);
nops[i] = import(odef->op(i));
assert(&nops[i]->world() == &world());
assert(&nops[i]->world() == &world_);
}

if (odef->isa_structural()) {
auto ndef = odef->rebuild(world(), ntype, nops);
auto ndef = odef->rebuild(world_, ntype, nops);
todo_ |= odef->tag() != ndef->tag();
return def_old2new_[odef] = ndef;
}

assert(ncontinuation && &ncontinuation->world() == &world());
assert(ncontinuation && &ncontinuation->world() == &world_);
auto napp = nops[0]->isa<App>();
if (napp)
ncontinuation->set_body(napp);
Expand Down

0 comments on commit a6db674

Please sign in to comment.