Skip to content

Commit

Permalink
Merge 6ef078a into 55cc41f
Browse files Browse the repository at this point in the history
  • Loading branch information
leonardt committed Feb 21, 2020
2 parents 55cc41f + 6ef078a commit 9a680e8
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 33 deletions.
6 changes: 6 additions & 0 deletions include/verilogAST/assign_inliner.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -59,14 +59,20 @@ class AssignInliner : public Transformer {
std::set<std::string> output_ports;
std::set<std::string> input_ports;
std::set<std::string> inlined_outputs;
std::set<std::string> wire_blacklist;

std::vector<std::variant<std::unique_ptr<StructuralStatement>,
std::unique_ptr<Declaration>>>
do_inline(std::vector<std::variant<std::unique_ptr<StructuralStatement>,
std::unique_ptr<Declaration>>>
body);

bool can_inline(std::string key);

public:
AssignInliner() : wire_blacklist() {};
AssignInliner(std::set<std::string> wire_blacklist) :
wire_blacklist(wire_blacklist) {};
using Transformer::visit;
virtual std::unique_ptr<Expression> visit(std::unique_ptr<Expression> node);
virtual std::unique_ptr<ContinuousAssign> visit(
Expand Down
52 changes: 19 additions & 33 deletions src/assign_inliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -54,19 +54,26 @@ std::unique_ptr<ContinuousAssign> AssignMapBuilder::visit(
return node;
}

bool AssignInliner::can_inline(std::string key) {
if (this->wire_blacklist.count(key)) {
return false;
}
std::map<std::string, std::unique_ptr<Expression>>::iterator it =
assign_map.find(key);
return it != assign_map.end() && (this->assign_count[key] == 1) &&
(this->read_count[key] == 1 ||
dynamic_cast<Identifier*>(it->second.get()) ||
dynamic_cast<NumericLiteral*>(it->second.get()));
}

std::unique_ptr<Expression> AssignInliner::visit(
std::unique_ptr<Expression> node) {
if (auto ptr = dynamic_cast<Identifier*>(node.get())) {
node.release();
std::unique_ptr<Identifier> id(ptr);
std::string key = id->toString();
std::map<std::string, std::unique_ptr<Expression>>::iterator it =
assign_map.find(key);
if (it != assign_map.end() && (this->assign_count[key] == 1) &&
(this->read_count[id->toString()] == 1 ||
dynamic_cast<Identifier*>(it->second.get()) ||
dynamic_cast<NumericLiteral*>(it->second.get()))) {
return this->visit(it->second->clone());
if (this->can_inline(key)) {
return this->visit(assign_map[key]->clone());
}
return id;
}
Expand All @@ -78,25 +85,9 @@ std::unique_ptr<Wire> AssignInliner::visit(std::unique_ptr<Wire> node) {
std::visit(
[&](auto&& value) {
if (auto ptr = dynamic_cast<Identifier*>(value.get())) {
std::string key = ptr->toString();
std::map<std::string, std::unique_ptr<Expression>>::iterator it =
this->assign_map.find(key);
if (it != assign_map.end() && (this->assign_count[key] == 1) &&
(this->read_count[key] == 1 ||
dynamic_cast<Identifier*>(it->second.get()) ||
dynamic_cast<NumericLiteral*>(it->second.get()))) {
remove = true;
};
remove = this->can_inline(ptr->toString());
} else if (auto ptr = dynamic_cast<Vector*>(value.get())) {
std::string key = ptr->id->toString();
std::map<std::string, std::unique_ptr<Expression>>::iterator it =
this->assign_map.find(key);
if (it != assign_map.end() && (this->assign_count[key] == 1) &&
(this->read_count[key] == 1 ||
dynamic_cast<Identifier*>(it->second.get()) ||
dynamic_cast<NumericLiteral*>(it->second.get()))) {
remove = true;
};
remove = this->can_inline(ptr->id->toString());
}
},
node->value);
Expand All @@ -117,13 +108,7 @@ std::unique_ptr<ContinuousAssign> AssignInliner::visit(
std::visit(
[&](auto&& value) {
if (auto ptr = dynamic_cast<Identifier*>(value.get())) {
std::map<std::string, std::unique_ptr<Expression>>::iterator it =
this->assign_map.find(ptr->toString());
if (it != assign_map.end() && (this->assign_count[key] == 1) &&
(this->read_count[ptr->toString()] == 1 ||
dynamic_cast<Identifier*>(it->second.get()) ||
dynamic_cast<NumericLiteral*>(it->second.get())) &&
this->non_input_ports.count(ptr->toString()) == 0) {
if (this->can_inline(key) && this->non_input_ports.count(key) == 0) {
remove = true;
} else if (this->inlined_outputs.count(ptr->toString())) {
remove = true;
Expand Down Expand Up @@ -189,7 +174,8 @@ std::unique_ptr<Module> AssignInliner::visit(std::unique_ptr<Module> node) {
this->assign_map.erase(output);
if (dynamic_cast<Identifier*>(value.get()) &&
this->assign_count[value->toString()] == 0 &&
this->input_ports.count(value->toString()) == 0) {
this->input_ports.count(value->toString()) == 0 &&
this->wire_blacklist.count(value->toString()) == 0) {
this->assign_map[value->toString()] = make_id(output);
this->assign_count[value->toString()]++;
this->inlined_outputs.insert(output);
Expand Down
22 changes: 22 additions & 0 deletions tests/assign_inliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,29 @@ TEST(InlineAssignTests, TestBasic) {

EXPECT_EQ(module->toString(), raw_str);

std::set<std::string> blacklist = {"x", "x_vec"};
vAST::AssignInliner transformer_blacklist(blacklist);
module = transformer_blacklist.visit(std::move(module));
EXPECT_EQ(module->toString(), raw_str);

std::string expected_str =
"module test_module (\n"
" input i,\n"
" output o,\n"
" output [1:0] o_vec\n"
");\n"
"wire [1:0] x_vec;\n"
"assign o = i;\n"
"assign x_vec = {i,i};\n"
"assign o_vec = x_vec;\n"
"endmodule\n";

blacklist = {"x_vec"};
vAST::AssignInliner transformer_blacklist2(blacklist);
module = transformer_blacklist2.visit(std::move(module));
EXPECT_EQ(module->toString(), expected_str);

expected_str =
"module test_module (\n"
" input i,\n"
" output o,\n"
Expand Down

0 comments on commit 9a680e8

Please sign in to comment.