Skip to content

Commit

Permalink
[luci/lang] Add remove graph method
Browse files Browse the repository at this point in the history
This pr adds removeGraphByIndex method to the Module.

ONE-DCO-1.0-Signed-off-by: Artem Balyshev <[email protected]>
  • Loading branch information
Artem Balyshev committed Aug 12, 2024
1 parent 5a6bc24 commit a347df5
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
7 changes: 7 additions & 0 deletions compiler/luci/lang/include/luci/IR/Module.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,13 @@ class Module final
*/
loco::Graph *graph(void) const;

/**
* @brief remove graph at index
*
* @note graph(0) is interpreted as a main graph and cannot be deleted
*/
void removeGraphByIndex(size_t idx);

/**
* @brief provide graph with an index
*
Expand Down
8 changes: 8 additions & 0 deletions compiler/luci/lang/src/Module.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,14 @@ loco::Graph *Module::graph(void) const
return graph.get();
}

void Module::removeGraphByIndex(size_t idx)
{
if (idx >= _graphs.size() or idx == 0)
throw std::invalid_argument("Module: Invalid graph index to be deleted");

_graphs.erase(_graphs.begin() + idx);
}

loco::Graph *Module::graph(size_t idx) const
{
auto &graph = _graphs.at(idx);
Expand Down
34 changes: 34 additions & 0 deletions compiler/luci/lang/src/Module.test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,33 @@ TEST(ModuleTest, add)
ASSERT_EQ(g_ptr, m->graph(0));
}

TEST(ModuleTest, remove)
{
auto m = luci::make_module();
auto g1 = loco::make_graph();
auto g2 = loco::make_graph();
auto g3 = loco::make_graph();
auto g1_ptr = g1.get();
auto g2_ptr = g2.get();
auto g3_ptr = g3.get();

m->add(std::move(g1));
m->add(std::move(g2));
m->add(std::move(g3));

ASSERT_EQ(3, m->size());
ASSERT_EQ(g1_ptr, m->graph());
ASSERT_EQ(g1_ptr, m->graph(0));
ASSERT_EQ(g2_ptr, m->graph(1));
ASSERT_EQ(g3_ptr, m->graph(2));

// Let's delete graph at second position
m->removeGraphByIndex(1);
ASSERT_EQ(2, m->size());
ASSERT_EQ(g1_ptr, m->graph(0));
ASSERT_EQ(g3_ptr, m->graph(1));
}

TEST(ModuleTest, add_more)
{
auto m = luci::make_module();
Expand Down Expand Up @@ -65,6 +92,13 @@ TEST(ModuleTest, add_nullptr_NEG)
EXPECT_THROW(m->add(nullptr), std::invalid_argument);
}

TEST(ModuleTest, remove_index_overflow_NEG)
{
auto m = luci::make_module();

EXPECT_THROW(m->removeGraphByIndex(10), std::invalid_argument);
}

TEST(ModuleTest, graph_index_overflow_NEG)
{
auto m = luci::make_module();
Expand Down

0 comments on commit a347df5

Please sign in to comment.