Skip to content

Commit

Permalink
Support finding instances from the model.
Browse files Browse the repository at this point in the history
Updated tests to reflect that.  This will be consistent with how the
Xlat plugin works as well.
  • Loading branch information
rmrf committed Jan 15, 2020
1 parent fa452ba commit 6aba1b3
Show file tree
Hide file tree
Showing 8 changed files with 114 additions and 58 deletions.
30 changes: 27 additions & 3 deletions src/Model.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,17 @@ Model::~Model() {
Model::Model(const Model &from) { modules_ = from.modules_; }

void Model::addModuleDecl(ModuleDecl *md) {
// class name, instance name.
modules_.push_back(Model::modulePairType(md->getName(), md));
}

void Model::addModuleDeclInstances(ModuleDecl *md, vector<ModuleDecl *> mdVec) {
module_instance_map_.insert(moduleInstancePairType(md, mdVec));

llvm::outs() << "[HDP] To add instances: " << md << "=" << mdVec.size()
<< "\n";
llvm::outs() << "[HDP] Added module instances: "
<< module_instance_map_.size() << "\n";
}

void Model::addSimulationTime(FindSimTime::simulationTimeMapType simTime) {
Expand Down Expand Up @@ -112,7 +118,24 @@ void Model::updateModuleDecl() {
// }
// }
//
Model::moduleMapType Model::getModuleDecl() { return modules_; }
const Model::moduleMapType &Model::getModuleDecl() { return modules_; }

ModuleDecl *Model::getInstance(const std::string &instance_name) {
for (auto const &element : module_instance_map_) {
auto instance_list{element.second};

auto test_module_it =
std::find_if(instance_list.begin(), instance_list.end(),
[instance_name](const auto &instance) {
return (instance->getInstanceName() == instance_name);
});

if (test_module_it != instance_list.end()) {
return *test_module_it;
}
}
return nullptr;
}

Model::entryFunctionGPUMacroMapType Model::getEntryFunctionGPUMacroMap() {
llvm::errs() << "\n return Size : " << entry_function_gpu_macro_map_.size();
Expand All @@ -128,8 +151,9 @@ Model::eventMapType Model::getEventMapType() { return event_map_; }
unsigned int Model::getNumEvents() { return (event_map_.size() - 3); }

void Model::dump(llvm::raw_ostream &os) {
os << "-- Number of sc_module instances: " << modules_.size();
os << "\n";
os << "-- Number of sc_module instances: " << modules_.size() << "\n";
os << "-- Number of sc_module instances in map: "
<< module_instance_map_.size() << "\n";

for (const auto &mod : modules_) {
// <string, ModuleDecl*>
Expand Down
3 changes: 2 additions & 1 deletion src/Model.h
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ class Model {
void addEntryFunctionGPUMacroMap(entryFunctionGPUMacroMapType);
void updateModuleDecl();

moduleMapType getModuleDecl();
const moduleMapType & getModuleDecl();
ModuleDecl *getInstance(const std::string &instance_name);
entryFunctionGPUMacroMapType getEntryFunctionGPUMacroMap();
eventMapType getEventMapType();
moduleInstanceMapType & getModuleInstanceMap();
Expand Down
33 changes: 9 additions & 24 deletions src/SystemCClang.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,10 +149,11 @@ bool SystemCConsumer::fire() {
//
// FIXME: This has to be replaced once xlat is fixed.
vector<ModuleDecl *> module_decl_instances;
ModuleDecl *p_dummy_module_decl{
ModuleDecl *p_dummy_module_decl{found_module_declarations[cxx_decl]};
//
// new ModuleDecl{found_module_declarations[cxx_decl], cxx_decl}};
// TODO: Remove deference pointer copy constructor
new ModuleDecl{*found_module_declarations[cxx_decl]}};
// new ModuleDecl{*found_module_declarations[cxx_decl]}};
// ==================

os_ << "CXXRecordDecl* " << cxx_decl
Expand Down Expand Up @@ -185,27 +186,6 @@ bool SystemCConsumer::fire() {
// 4. Find ports
//
//
// cxx_decl->dump();


/*
FindPorts ports{static_cast<CXXRecordDecl *>(cxx_decl), os_};
// ports.dump();
// auto port_matcher{ module_declaration_handler.getPortMatcher() };
// add_module_decl->addPorts(port_matcher.getInputPorts(), "sc_in");
// add_module_decl->addPorts(port_matcher.getOutputPorts(), "sc_out");
add_module_decl->addInputPorts(ports.getInputPorts());
add_module_decl->addOutputPorts(ports.getOutputPorts());
add_module_decl->addInputOutputPorts(ports.getInputOutputPorts());
add_module_decl->addOtherVars(ports.getOtherVars());
add_module_decl->addInputStreamPorts(ports.getInStreamPorts());
add_module_decl->addOutputStreamPorts(ports.getOutStreamPorts());
// 5. Find signals
FindSignals signals{add_module_decl->getModuleClassDecl(), os_};
add_module_decl->addSignals(signals.getSignals());
*/

// 5. Find entry functions
FindEntryFunctions findEntries{add_module_decl->getModuleClassDecl(),
os_};
Expand Down Expand Up @@ -238,7 +218,11 @@ bool SystemCConsumer::fire() {
os_ << "============== END DUMP the MODULEDECL ==================\n";
// Insert the module into the model.
// All modules are also instances.
systemcModel_->addModuleDecl(add_module_decl);

// Make the dummy equal to the updated add_module_decl
// This will make module declarations be one of the module instances.
*p_dummy_module_decl = *add_module_decl;
systemcModel_->addModuleDecl(p_dummy_module_decl);
module_decl_instances.push_back(add_module_decl);
}
os_ << "\n";
Expand All @@ -247,6 +231,7 @@ bool SystemCConsumer::fire() {
//
// FIXME: Only there to make sure xlat still compiles.
// This should be removed.
llvm::outs() << "[HDP] Add instances to model\n";
systemcModel_->addModuleDeclInstances(p_dummy_module_decl,
module_decl_instances);
}
Expand Down
22 changes: 15 additions & 7 deletions tests/sreg-test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,13 @@ TEST_CASE("sreg example", "[llnl-examples]") {
}

// Testing instances.

auto test_module{model->getInstance("testing")};
auto sreg_bypass{model->getInstance("sreg_bypass")};
auto sreg_fwd{model->getInstance("sreg_fwd")};
auto sreg_fwd_rev{model->getInstance("sreg_fwd_rev")};

/*
auto test_module{std::find_if(
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "testing";
Expand All @@ -62,6 +69,7 @@ TEST_CASE("sreg example", "[llnl-examples]") {
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "sreg_fwd_rev";
})};
*/

//
// Begin the tests.
Expand All @@ -72,16 +80,16 @@ TEST_CASE("sreg example", "[llnl-examples]") {
<< module_decl.size());
CHECK(module_decl.size() == 4);

REQUIRE(test_module != module_decl.end());
REQUIRE(sreg_bypass != module_decl.end());
REQUIRE(sreg_fwd != module_decl.end());
REQUIRE(sreg_fwd_rev != module_decl.end());
REQUIRE(test_module != nullptr);
REQUIRE(sreg_bypass != nullptr);
REQUIRE(sreg_fwd != nullptr);
REQUIRE(sreg_fwd_rev != nullptr);

//
//
//
INFO("Checking sreg_bypass ports.");
auto sreg_bypass_decl{sreg_bypass->second};
auto sreg_bypass_decl{sreg_bypass};

REQUIRE(sreg_bypass_decl->getIPorts().size() == 2);
REQUIRE(sreg_bypass_decl->getOPorts().size() == 0);
Expand All @@ -95,7 +103,7 @@ TEST_CASE("sreg example", "[llnl-examples]") {
//
//
INFO("Checking sreg_fwd ports.");
auto sreg_fwd_decl{sreg_fwd->second};
auto sreg_fwd_decl{sreg_fwd};

REQUIRE(sreg_fwd_decl->getIPorts().size() == 2);
REQUIRE(sreg_fwd_decl->getOPorts().size() == 0);
Expand All @@ -110,7 +118,7 @@ TEST_CASE("sreg example", "[llnl-examples]") {
//
INFO("Checking sreg_fwd_rev ports.");

auto sreg_fwd_rev_decl{sreg_fwd_rev->second};
auto sreg_fwd_rev_decl{sreg_fwd_rev};

REQUIRE(sreg_fwd_rev_decl->getIPorts().size() == 2);
REQUIRE(sreg_fwd_rev_decl->getOPorts().size() == 0);
Expand Down
52 changes: 38 additions & 14 deletions tests/t1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,28 +95,52 @@ int sc_main(int argc, char *argv[]) {
sc.HandleTranslationUnit(from_ast->getASTContext());

auto model{sc.getSystemCModel()};
// model->dump(llvm::outs());

// This provides the module declarations.
auto module_decl{model->getModuleDecl()};
auto module_instance_map{model->getModuleInstanceMap()};

// Want to find an instance named "testing".

ModuleDecl *test_module{model->getInstance("testing")};;
ModuleDecl *simple_module{model->getInstance("simple_module_instance")};

/*
for (auto const &element : module_instance_map) {
auto instance_list{element.second};
auto test_module{std::find_if(
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "testing";
})};
auto test_module_it = std::find_if(
instance_list.begin(), instance_list.end(), [](const auto &instance) {
return instance->getInstanceName() == "testing";
});
test_module = *test_module_it;
if (test_module_it != instance_list.end()) {
break;
}
}
auto simple_module{std::find_if(
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "simple_module_instance";
})};
for (auto const &element : module_instance_map) {
auto instance_list{element.second};
auto simple_module_it = std::find_if(
instance_list.begin(), instance_list.end(), [](const auto &instance) {
return instance->getInstanceName() == "simple_module_instance";
});
simple_module = *simple_module_it;
if (simple_module_it != instance_list.end()) {
break;
}
}
*/

SECTION("Found sc_module instances", "[instances]") {
// There should be 2 modules identified.
INFO("ERROR: number of sc_module declarations found: "
INFO("Checking number of sc_module declarations found: "
<< module_decl.size());

REQUIRE(module_decl.size() == 2);

REQUIRE(test_module != module_decl.end());
REQUIRE(simple_module != module_decl.end());
REQUIRE(test_module != nullptr);
REQUIRE(simple_module != nullptr);

INFO("Checking member ports for test instance.");
// These checks should be performed on the declarations.
Expand All @@ -128,7 +152,7 @@ int sc_main(int argc, char *argv[]) {
// auto p_module{module_decl.find("test")};
//
//
auto test_module_inst{test_module->second};
auto test_module_inst{test_module};

// Check if the proper number of ports are found.
REQUIRE(test_module_inst->getIPorts().size() == 3);
Expand All @@ -140,7 +164,7 @@ int sc_main(int argc, char *argv[]) {
REQUIRE(test_module_inst->getOutputStreamPorts().size() == 0);

INFO("Checking member ports for simple module instance.");
auto simple_module_inst{simple_module->second};
auto simple_module_inst{simple_module};

// Check if the proper number of ports are found.
REQUIRE(simple_module_inst->getIPorts().size() == 3);
Expand Down
5 changes: 4 additions & 1 deletion tests/t2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,13 +69,16 @@ int sc_main(int argc, char *argv[]) {
// The module instances have all the information.
// auto test_module{module_decl.find("test")};

auto test_module{ model->getInstance("testing") };
/*
auto test_module{std::find_if(
module_decl.begin(), module_decl.end(),
[](const auto &element) { return element.second->getInstanceName() == "testing"; })};
// There is only one module instance
// */

// Check if the proper number of ports are found.
INFO("FAIL_TEST: A module must have a port bound for it to be recognized.");
REQUIRE(test_module != module_decl.end());
REQUIRE(test_module != nullptr);
}
}
12 changes: 8 additions & 4 deletions tests/t3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ TEST_CASE("Read SystemC model from file for testing", "[parsing]") {
SECTION("No ports bound for test declaration", "[ports]") {
// The module instances have all the information.
// auto test_module{module_decl.find("test")};

auto test_module{model->getInstance("testing")};

/*
auto test_module{std::find_if(
module_decl.begin(), module_decl.end(), [](const auto &element) {
return element.second->getInstanceName() == "testing";
})};
*/
// There is only one module instance

// Check if the proper number of ports are found.
INFO(
"FAIL_TEST: A module must have a port bound for it to be "
INFO("FAIL_TEST: A module must have a port bound for it to be "
"recognized.");
REQUIRE(test_module != module_decl.end());
auto module_ptr{ test_module->second};
REQUIRE(test_module != nullptr);
auto module_ptr{ test_module};

REQUIRE(module_ptr->getInstanceName() == "testing");

Expand Down
15 changes: 11 additions & 4 deletions tests/t5-template-matching.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -129,30 +129,37 @@ TEST_CASE("Testing top-level module: test", "[top-module]") {
auto model{sc.getSystemCModel()};
auto module_decl{model->getModuleDecl()};

auto found_module_testing{model->getInstance("testing")};

/*
auto found_module_testing{std::find_if(
module_decl.begin(), module_decl.end(), [&top](const auto &element) {
// Get the declaration name.
return ((element.second->getName() == top) &&
(element.second->getInstanceName() == "testing"));
})};
*/

auto found_module_testing_float{model->getInstance("testing_float_double")};
/*
auto found_module_testing_float{std::find_if(
module_decl.begin(), module_decl.end(), [&top](const auto &element) {
// Get the declaration name.
return ((element.second->getName() == top) &&
(element.second->getInstanceName() == "testing_float_double"));
})};
*/

SECTION("Testing top-level module: test", "[top module]") {
// There should be two modules because there are two instances.
INFO("Top-level module specified as test.");
REQUIRE(module_decl.size() == 2);

// Actually found the module.
REQUIRE(found_module_testing != module_decl.end());
REQUIRE(found_module_testing_float != module_decl.end());
REQUIRE(found_module_testing != nullptr);
REQUIRE(found_module_testing_float != nullptr);

auto found_decl{found_module_testing->second};
auto found_decl{found_module_testing};
REQUIRE(found_decl->getIPorts().size() == 4);
REQUIRE(found_decl->getOPorts().size() == 1);
// This is 4 because sc_buffer is also inheriting from the signal interface.
Expand All @@ -162,7 +169,7 @@ TEST_CASE("Testing top-level module: test", "[top-module]") {
// TODO: Check the template parameters.
//

auto found_decl2{found_module_testing_float->second};
auto found_decl2{found_module_testing_float};
REQUIRE(found_decl2->getIPorts().size() == 4);
REQUIRE(found_decl2->getOPorts().size() == 1);
// 1 regular signal, 2 array signals, 1 sc_buffer, which is a signal too.
Expand Down

0 comments on commit 6aba1b3

Please sign in to comment.