Skip to content

Commit

Permalink
Add Python module for Support library, including backedge helpers.
Browse files Browse the repository at this point in the history
  • Loading branch information
mikeurbach committed Apr 22, 2021
1 parent 151ef53 commit 1b9fc38
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/Bindings/Python/CIRCTModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
//===----------------------------------------------------------------------===//

#include "DialectModules.h"
#include "SupportModule.h"

#include "circt-c/Dialect/Comb.h"
#include "circt-c/Dialect/ESI.h"
Expand Down Expand Up @@ -70,4 +71,6 @@ PYBIND11_MODULE(_circt, m) {
circt::python::populateDialectESISubmodule(esi);
py::module msft = m.def_submodule("msft", "MSFT API");
circt::python::populateDialectMSFTSubmodule(msft);
py::module support = m.def_submodule("_support", "Support API");
circt::python::populateSupportSubmodule(support);
}
2 changes: 2 additions & 0 deletions lib/Bindings/Python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@ add_mlir_python_extension(CIRCTBindingsPythonExtension _circt
CIRCTModule.cpp
ESIModule.cpp
MSFTModule.cpp
SupportModule.cpp
LINK_LIBS
CIRCTCAPIComb
CIRCTCAPIESI
CIRCTCAPIMSFT
CIRCTCAPIRTL
CIRCTCAPISV
CIRCTCAPIExportVerilog
CIRCTSupport
)
add_dependencies(CIRCTBindingsPython CIRCTBindingsPythonExtension)

Expand Down
69 changes: 69 additions & 0 deletions lib/Bindings/Python/SupportModule.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
//===- SupportModule.cpp - Support API pybind module ----------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#include "SupportModule.h"

#include "circt/Support/BackedgeBuilder.h"
#include "circt/Support/LLVM.h"

#include "mlir-c/IR.h"
#include "mlir/CAPI/IR.h"
#include "mlir/IR/Builders.h"
#include "mlir/IR/Dialect.h"

#include "PybindUtils.h"
#include "pybind11/pybind11.h"
namespace py = pybind11;

using namespace circt;

//===----------------------------------------------------------------------===//
// Classes that translate from something Pybind11 understands to MLIR C++.
//===----------------------------------------------------------------------===//

namespace {

class PyBackedge {
public:
PyBackedge(Backedge backedge) : backedge(backedge) {}

MlirValue value() { return wrap(backedge); }

void setValue(MlirValue newValue) { backedge.setValue(unwrap(newValue)); }

private:
Backedge backedge;
};

class PyBackedgeBuilder {
public:
PyBackedgeBuilder(MlirLocation loc)
: builder(OpBuilder(unwrap(mlirLocationGetContext(loc)))),
backedgeBuilder(BackedgeBuilder(builder, unwrap(loc))) {}

PyBackedge get(MlirType type) {
return PyBackedge(backedgeBuilder.get(unwrap(type)));
}

private:
OpBuilder builder;
BackedgeBuilder backedgeBuilder;
};

} // namespace

void circt::python::populateSupportSubmodule(py::module &m) {
py::class_<PyBackedge>(m, "Backedge")
.def(py::init<Backedge>())
.def("set_value", &PyBackedge::setValue, "Set the backedge value.")
.def_property_readonly("value", &PyBackedge::value,
"Get the value from the backedge.");
py::class_<PyBackedgeBuilder>(m, "BackedgeBuilder")
.def(py::init<MlirLocation>())
.def("get", &PyBackedgeBuilder::get, "Get a backedge builder.");
}
22 changes: 22 additions & 0 deletions lib/Bindings/Python/SupportModule.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
//===- SupportModule.h - Support API submodule ----------------------------===//
//
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
// See https://llvm.org/LICENSE.txt for license information.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
//
//===----------------------------------------------------------------------===//

#ifndef CIRCT_BINDINGS_PYTHON_SUPPORTMODULE_H
#define CIRCT_BINDINGS_PYTHON_SUPPORTMODULE_H

#include <pybind11/pybind11.h>

namespace circt {
namespace python {

void populateSupportSubmodule(pybind11::module &m);

} // namespace python
} // namespace circt

#endif // CIRCT_BINDINGS_PYTHON_SUPPORTMODULE_H
5 changes: 5 additions & 0 deletions lib/Bindings/Python/circt/support.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
# See https://llvm.org/LICENSE.txt for license information.
# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

from _circt._support import *

0 comments on commit 1b9fc38

Please sign in to comment.