Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[BYOC][Verilator] Refactor Verilator runtime #7406

Merged
merged 10 commits into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
2 changes: 1 addition & 1 deletion src/relay/backend/contrib/verilator/codegen.cc
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ runtime::Module VerilatorBackend(const ObjectRef& ref) {
cfg = AttrsWithDefaultValues<VerilatorOptions>();
}

n->LoadLibrary(cfg.value()->lib_path);
n->SetLibrary(cfg.value()->lib_path);
n->SetResetCycles(cfg.value()->reset_cycles);

if (cfg.value()->profiler_enable) {
Expand Down
13 changes: 9 additions & 4 deletions src/runtime/contrib/verilator/verilator_runtime.cc
Original file line number Diff line number Diff line change
Expand Up @@ -76,19 +76,24 @@ VerilatorProfiler* VerilatorProfiler::ThreadLocal() {
return &inst;
}

void VerilatorRuntime::LoadLibrary(const std::string& lib_name) {
lib_ = new VerilatorLibrary();
lib_->Load(lib_name);
VerilatorRuntime::~VerilatorRuntime() {
auto dealloc = reinterpret_cast<VerilatorDeallocFunc>(lib_->GetSymbol("VerilatorDealloc"));
ICHECK(dealloc != nullptr);
dealloc(device_);
lib_->~VerilatorLibrary();
Copy link
Member

Choose a reason for hiding this comment

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

just out of curiosity, why do we explicitly call the deallocator instead of delete lib_?

}

void VerilatorRuntime::SetLibrary(const std::string& lib_path) { lib_path_ = lib_path; }

void VerilatorRuntime::SetResetCycles(const int cycles) { reset_cycles_ = cycles; }

void VerilatorRuntime::EnableProfiler() { prof_enable_ = true; }

void VerilatorRuntime::SetProfilerCycleCounterId(const int id) { prof_cycle_counter_id_ = id; }

void VerilatorRuntime::Init(const Array<NDArray>& consts) {
// get symbols
lib_ = new VerilatorLibrary();
lib_->Load(lib_path_);
auto alloc = reinterpret_cast<VerilatorAllocFunc>(lib_->GetSymbol("VerilatorAlloc"));
ICHECK(alloc != nullptr);
auto reset = reinterpret_cast<VerilatorResetFunc>(lib_->GetSymbol("VerilatorReset"));
Expand Down
9 changes: 7 additions & 2 deletions src/runtime/contrib/verilator/verilator_runtime.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ using namespace tvm::runtime::contrib;
using namespace tvm::runtime::json;

typedef VerilatorHandle (*VerilatorAllocFunc)();
typedef void (*VerilatorDeallocFunc)(VerilatorHandle);
typedef void (*VerilatorResetFunc)(VerilatorHandle, int);
typedef void (*VerilatorAddFunc)(VerilatorHandle, int*, int*, int*, int, int);
typedef int (*VerilatorReadFunc)(VerilatorHandle, int, int);
Expand Down Expand Up @@ -88,10 +89,12 @@ class VerilatorRuntime : public JSONRuntimeBase {
const Array<String> const_names)
: JSONRuntimeBase(symbol_name, graph_json, const_names) {}

~VerilatorRuntime();

const char* type_key() const { return "verilator"; }

/*! \brief load verilator library */
void LoadLibrary(const std::string& lib_name);
/*! \brief set verilator library */
void SetLibrary(const std::string& lib_name);

/*! \brief set the number of reset cycles */
void SetResetCycles(const int cycles);
Expand All @@ -109,6 +112,8 @@ class VerilatorRuntime : public JSONRuntimeBase {
void Run() override;

private:
/*! \brief the verilator library path */
String lib_path_;
/*! \brief the verilator device */
VerilatorHandle device_{nullptr};
/*! \brief the verilator library */
Expand Down