Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 3 additions & 6 deletions src/passes/analysis/verilog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1130,12 +1130,9 @@ Passes::Verilog::compileModuleBody(
for (auto instance : instances) {
Module* instance_module = instance.second->getModuleRef();
std::string module_name = instance_module->getLongName();
if (instance_module->isGenerated()) {
if (instance_module->getGenerator()->getMetaData().count("verilog") > 0) {
json verilog_json = instance_module->getGenerator()
->getMetaData()["verilog"];
module_name = make_name(instance_module->getName(), verilog_json);
}
if (instance_module->isGenerated() && instance_module->getGenerator()->getMetaData().count("verilog") > 0) {

@joyliu37 joyliu37 Jul 22, 2021

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

After testing with clockwork, I need to change this line of code into

if (instance_module->isGenerated() && instance_module->getMetaData().count("verilog") > 0)

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@rdaly525 I think this means you need to check two separate cases here:

  1. the instance_module has "verilog" metadata
  2. the instance_module->getGenerator() has "verilog" metadata

json verilog_json = instance_module->getGenerator()->getMetaData()["verilog"];
module_name = make_name(instance_module->getName(), verilog_json);
}
else if (instance_module->getMetaData().count("verilog") > 0) {
json verilog_json = instance_module->getMetaData()["verilog"];
Expand Down
4 changes: 4 additions & 0 deletions tests/gtest/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ add_executable(test_verilog test_verilog.cpp)
target_link_libraries(test_verilog gtest_main coreir coreir-commonlib)
add_test(NAME test_verilog COMMAND test_verilog WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests/gtest)

add_executable(test_verilog_metadata test_verilog_metadata.cpp)
target_link_libraries(test_verilog_metadata gtest_main coreir coreir-commonlib)
add_test(NAME test_verilog_metadata COMMAND test_verilog_metadata WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests/gtest)

add_executable(test_inline_wires test_inline_wires.cpp)
target_link_libraries(test_inline_wires gtest_main coreir coreir-commonlib)
add_test(NAME test_inline_wires COMMAND test_inline_wires WORKING_DIRECTORY ${CMAKE_SOURCE_DIR}/tests/gtest)
Expand Down
49 changes: 49 additions & 0 deletions tests/gtest/golds/Add5.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{"top":"global.top",
"namespaces":{
"global":{
"modules":{
"top":{
"type":["Record",[

]],
"instances":{
"inst":{
"genref":"global.addN",
"genargs":{"width":["Int",5]}
}
},
"connections":[
["inst.out","inst.in"]
]
}
},
"generators":{
"addN":{
"typegen":"global.wgen",
"genparams":{"width":"Int"},
"modules":[
[
{"width":["Int",5]},
{
"type":["Record",[
["in",["Array",5,"BitIn"]],
["out",["Array",5,"Bit"]]
]],
"metadata":{"verilog_name":"Add5"}
}
]
]
}
},
"typegens":{
"wgen":[
{"width":"Int"},
"sparse",
[
[{"width":["Int",5]},["Record",[["in",["Array",5,"BitIn"]],["out",["Array",5,"Bit"]]]]]
]
]
}
}
}
}
11 changes: 11 additions & 0 deletions tests/gtest/golds/Add5.v
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// Module `Add5` defined externally
module top (

);
wire [4:0] inst_out;
Add5 inst (
.in(inst_out),
.out(inst_out)
);
endmodule

55 changes: 55 additions & 0 deletions tests/gtest/test_verilog_metadata.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
#include "gtest/gtest.h"
#include "assert_pass.h"
#include "coreir.h"
#include "coreir/definitions/coreVerilog.hpp"
#include "coreir/definitions/corebitVerilog.hpp"
#include "coreir/libs/commonlib.h"

using namespace CoreIR;

namespace {

TEST(VerilogMetaData, TestGeneratedModule) {
Context* c = newContext();

Namespace* g = c->getGlobal();

// Declare a TypeGenerator (in global) for addN
g->newTypeGen(
"wgen", // name for the typegen
{{"width", c->Int()}},
[](Context* c, Values args) { // Function to compute type
Json width = args.at("width")->get<Json>();
return c->Record(
{{"in", c->BitIn()->Arr(width)}, {"out", c->Bit()->Arr(width)}});
}
);

g->newGeneratorDecl(
"addN",
g->getTypeGen("wgen"),
{{"width", c->Int()}});

Module* top = g->newModuleDecl("top", c->Record());
ModuleDef* def = top->newModuleDef();
auto inst = def->addInstance("inst", "global.addN", {{"width", Const::make(c, 5)}});
def->connect(inst->sel("in"), inst->sel("out"));
top->setDef(def);
c->setTop(top);

Module* gen_module = inst->getModuleRef();
gen_module->getMetaData()["verilog_name"] = "Add5";

serializeToFile(c, "build/Add5.json");
assertFileEq("build/Add5.json", "golds/Add5.json");

const std::vector<std::string> passes = {
"rungenerators",
"verilog"};
c->runPasses(passes, {});
assertPassEq(c, "verilog", "golds/Add5.v");
deleteContext(c);

}

}