Skip to content

Commit

Permalink
change to GC of ExtNode nodes
Browse files Browse the repository at this point in the history
  • Loading branch information
martty committed Sep 15, 2024
1 parent 0669f88 commit 72f0e70
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 28 deletions.
3 changes: 2 additions & 1 deletion include/vuk/IR.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ namespace vuk {

plf::colony<Node /*, inline_alloc<Node, 4 * 1024>*/> op_arena;
std::vector<Node*> garbage;
std::unordered_map<Node*, size_t> potential_garbage;
plf::colony<UserCallbackType> ucbs;

// uint64_t current_hash = 0;
Expand Down Expand Up @@ -1309,7 +1310,7 @@ namespace vuk {
delete (ImageAttachment*)node->acquire.value;
}
}
source_module->destroy_node(node);
source_module->potential_garbage.emplace(node, 0);
}
}

Expand Down
18 changes: 16 additions & 2 deletions include/vuk/IRProcess.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,23 @@ namespace vuk {
}
}

template<class F>
auto apply_generic_args(F&& f, vuk::Node* node) {
auto count = node->generic_node.arg_count;
if (count != (uint8_t)~0u) {
for (int i = 0; i < count; i++) {
f(node->fixed_node.args[i]);
}
} else {
for (int i = 0; i < node->variable_node.args.size(); i++) {
f(node->variable_node.args[i]);
}
}
}

namespace errors { /*
RenderGraphException make_unattached_resource_exception(PassInfo& pass_info, Resource& resource);
RenderGraphException make_cbuf_references_unknown_resource(PassInfo& pass_info, Resource::Type type, Name name);
RenderGraphException make_cbuf_references_undeclared_resource(PassInfo& pass_info, Resource::Type type, Name name);*/
} // namespace errors
}; // namespace vuk
} // namespace errors
}; // namespace vuk
23 changes: 4 additions & 19 deletions src/IRPasses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,6 @@
#include <sstream>
#include <unordered_set>

namespace {
template<class F>
auto apply_generic_args(F&& f, vuk::Node* node) {
auto count = node->generic_node.arg_count;
if (count != (uint8_t)~0u) {
for (int i = 0; i < count; i++) {
f(node->fixed_node.args[i]);
}
} else {
for (int i = 0; i < node->variable_node.args.size(); i++) {
f(node->variable_node.args[i]);
}
}
}
} // namespace

namespace vuk {
template<class Allocator>
void _dump_graph(std::vector<Node*, Allocator> nodes) {
Expand Down Expand Up @@ -1106,13 +1090,14 @@ namespace vuk {

std::pmr::polymorphic_allocator allocator(&impl->mbr);
implicit_linking(current_module->op_arena.begin(), current_module->op_arena.end(), allocator);

// disable splices that are unwaited
for (auto& depnode : impl->depnodes) {
if (depnode.use_count() == 1 && depnode->acqrel->status == Signal::Status::eDisarmed) {
assert(depnode->get_node()->kind == Node::SPLICE);
depnode->get_node()->splice.rel_acq = nullptr;
}
}
//std::erase_if(impl->depnodes, [](std::shared_ptr<ExtNode>& sp) { return sp.use_count() == 1 && sp->acqrel->status == Signal::Status::eDisarmed; });

VUK_DO_OR_RETURN(impl->build_nodes());
VUK_DO_OR_RETURN(impl->build_links(impl->nodes, allocator));
Expand Down Expand Up @@ -1171,6 +1156,7 @@ namespace vuk {
}
}


// collect all args
for (auto node : impl->nodes) {
auto count = node->generic_node.arg_count;
Expand Down Expand Up @@ -1206,15 +1192,14 @@ namespace vuk {

VUK_DO_OR_RETURN(impl->build_nodes());
// SANITY: we have resolved all splices / releases
impl->dump_graph();
for (auto node : impl->nodes) {
switch (node->kind) {
case Node::SPLICE: {
assert(node->splice.rel_acq != nullptr);
}
}
}
// impl->dump_graph();
// _dump_graph(impl->nodes);
VUK_DO_OR_RETURN(impl->build_links(impl->nodes, allocator));

VUK_DO_OR_RETURN(impl->reify_inference());
Expand Down
24 changes: 23 additions & 1 deletion src/runtime/vk/Backend.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1746,7 +1746,17 @@ namespace vuk {
}

// post-run: checks and cleanup
impl->depnodes.clear();

for (auto& node : impl->nodes) {
apply_generic_args(
[](Ref parm) {
if (current_module->potential_garbage.contains(parm.node)) {
current_module->potential_garbage[parm.node]++;
}
},
node);

// reset any nodes we ran
node->execution_info = nullptr;
// if we ran any non-splice nodes: they are garbage now
Expand All @@ -1769,6 +1779,19 @@ namespace vuk {
}

// destroy garbage nodes
std::vector<Node*> to_garbage;
for (auto& [node, counts] : current_module->potential_garbage) {
if (counts == 0) {
to_garbage.push_back(node);
}
counts = 0;
}

for (auto& tg : to_garbage) {
current_module->potential_garbage.erase(tg);
}

impl->garbage_nodes.insert(impl->garbage_nodes.end(), to_garbage.begin(), to_garbage.end());
impl->garbage_nodes.insert(impl->garbage_nodes.end(), current_module->garbage.begin(), current_module->garbage.end());
std::sort(impl->garbage_nodes.begin(), impl->garbage_nodes.end());
impl->garbage_nodes.erase(std::unique(impl->garbage_nodes.begin(), impl->garbage_nodes.end()), impl->garbage_nodes.end());
Expand All @@ -1779,7 +1802,6 @@ namespace vuk {
current_module->garbage.clear();
impl->garbage_nodes.clear();

impl->depnodes.clear();
return { expected_value };
}
} // namespace vuk
10 changes: 5 additions & 5 deletions tests/01_semantics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -290,37 +290,37 @@ TEST_CASE("multi-queue buffers") {
execution = "";
}
{
CHECK(current_module->op_arena.size() == 0);
CHECK(current_module->op_arena.size() == 2);
auto written = write(declare_buf("src0", **buf0));
written.wait(*test_context.allocator, test_context.compiler);
read(std::move(written)).wait(*test_context.allocator, test_context.compiler);
CHECK(execution == "wr");
execution = "";
}
{
CHECK(current_module->op_arena.size() == 0);
CHECK(current_module->op_arena.size() == 1);
auto written = write(declare_buf("src0", **buf0));
written.wait(*test_context.allocator, test_context.compiler);
write(read(std::move(written))).wait(*test_context.allocator, test_context.compiler);
CHECK(execution == "wrw");
execution = "";
}
{
CHECK(current_module->op_arena.size() == 0);
CHECK(current_module->op_arena.size() == 1);
auto written = write(declare_buf("src0", **buf0));
read(written).wait(*test_context.allocator, test_context.compiler);
CHECK(execution == "wr");
execution = "";
}
{
CHECK(current_module->op_arena.size() == 0);
CHECK(current_module->op_arena.size() == 2);
auto written = write(declare_buf("src0", **buf0));
read(std::move(written)).wait(*test_context.allocator, test_context.compiler);
CHECK(execution == "wr");
execution = "";
}
{
CHECK(current_module->op_arena.size() == 0);
CHECK(current_module->op_arena.size() == 1);
auto written = write(declare_buf("src0", **buf0));
write(read(std::move(written))).wait(*test_context.allocator, test_context.compiler);
CHECK(execution == "wrw");
Expand Down

0 comments on commit 72f0e70

Please sign in to comment.