CIRCT  20.0.0git
CIRCTModule.cpp
Go to the documentation of this file.
1 //===- CIRCTModule.cpp - Main pybind module -------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 #include "CIRCTModules.h"
10 
11 #include "circt-c/Conversion.h"
12 #include "circt-c/Dialect/Arc.h"
13 #include "circt-c/Dialect/Comb.h"
14 #include "circt-c/Dialect/Debug.h"
15 #include "circt-c/Dialect/ESI.h"
16 #include "circt-c/Dialect/Emit.h"
17 #include "circt-c/Dialect/FSM.h"
18 #include "circt-c/Dialect/HW.h"
21 #include "circt-c/Dialect/LTL.h"
22 #include "circt-c/Dialect/MSFT.h"
23 #include "circt-c/Dialect/OM.h"
24 #include "circt-c/Dialect/SV.h"
25 #include "circt-c/Dialect/Seq.h"
26 #include "circt-c/Dialect/Verif.h"
27 #include "circt-c/ExportVerilog.h"
28 #include "mlir-c/Bindings/Python/Interop.h"
29 #include "mlir-c/IR.h"
30 #include "mlir-c/Transforms.h"
31 #include "mlir/Bindings/Python/PybindAdaptors.h"
32 
33 #include "llvm-c/ErrorHandling.h"
34 #include "llvm/Support/Signals.h"
35 
36 #include "PybindUtils.h"
37 #include <pybind11/pybind11.h>
38 namespace py = pybind11;
39 
40 static void registerPasses() {
49  mlirRegisterConversionPasses();
50  mlirRegisterTransformsPasses();
51 }
52 
53 PYBIND11_MODULE(_circt, m) {
54  m.doc() = "CIRCT Python Native Extension";
56  llvm::sys::PrintStackTraceOnErrorSignal(/*argv=*/"");
57  LLVMEnablePrettyStackTrace();
58 
59  m.def(
60  "register_dialects",
61  [](py::object capsule) {
62  // Get the MlirContext capsule from PyMlirContext capsule.
63  auto wrappedCapsule = capsule.attr(MLIR_PYTHON_CAPI_PTR_ATTR);
64  MlirContext context = mlirPythonCapsuleToContext(wrappedCapsule.ptr());
65 
66  // Collect CIRCT dialects to register.
67  MlirDialectHandle comb = mlirGetDialectHandle__comb__();
68  mlirDialectHandleRegisterDialect(comb, context);
69  mlirDialectHandleLoadDialect(comb, context);
70 
71  MlirDialectHandle debug = mlirGetDialectHandle__debug__();
72  mlirDialectHandleRegisterDialect(debug, context);
73  mlirDialectHandleLoadDialect(debug, context);
74 
75  MlirDialectHandle emit = mlirGetDialectHandle__emit__();
76  mlirDialectHandleRegisterDialect(emit, context);
77  mlirDialectHandleLoadDialect(emit, context);
78 
79  MlirDialectHandle esi = mlirGetDialectHandle__esi__();
80  mlirDialectHandleRegisterDialect(esi, context);
81  mlirDialectHandleLoadDialect(esi, context);
82 
83  MlirDialectHandle msft = mlirGetDialectHandle__msft__();
84  mlirDialectHandleRegisterDialect(msft, context);
85  mlirDialectHandleLoadDialect(msft, context);
86 
87  MlirDialectHandle hw = mlirGetDialectHandle__hw__();
88  mlirDialectHandleRegisterDialect(hw, context);
89  mlirDialectHandleLoadDialect(hw, context);
90 
91  MlirDialectHandle hwarith = mlirGetDialectHandle__hwarith__();
92  mlirDialectHandleRegisterDialect(hwarith, context);
93  mlirDialectHandleLoadDialect(hwarith, context);
94 
95  MlirDialectHandle om = mlirGetDialectHandle__om__();
96  mlirDialectHandleRegisterDialect(om, context);
97  mlirDialectHandleLoadDialect(om, context);
98 
99  MlirDialectHandle seq = mlirGetDialectHandle__seq__();
100  mlirDialectHandleRegisterDialect(seq, context);
101  mlirDialectHandleLoadDialect(seq, context);
102 
103  MlirDialectHandle sv = mlirGetDialectHandle__sv__();
104  mlirDialectHandleRegisterDialect(sv, context);
105  mlirDialectHandleLoadDialect(sv, context);
106 
107  MlirDialectHandle fsm = mlirGetDialectHandle__fsm__();
108  mlirDialectHandleRegisterDialect(fsm, context);
109  mlirDialectHandleLoadDialect(fsm, context);
110 
111  MlirDialectHandle handshake = mlirGetDialectHandle__handshake__();
112  mlirDialectHandleRegisterDialect(handshake, context);
113  mlirDialectHandleLoadDialect(handshake, context);
114 
115  MlirDialectHandle ltl = mlirGetDialectHandle__ltl__();
116  mlirDialectHandleRegisterDialect(ltl, context);
117  mlirDialectHandleLoadDialect(ltl, context);
118 
119  MlirDialectHandle verif = mlirGetDialectHandle__verif__();
120  mlirDialectHandleRegisterDialect(verif, context);
121  mlirDialectHandleLoadDialect(verif, context);
122  },
123  "Register CIRCT dialects on a PyMlirContext.");
124 
125  m.def("export_verilog", [](MlirModule mod, py::object fileObject) {
126  circt::python::PyFileAccumulator accum(fileObject, false);
127  py::gil_scoped_release();
128  mlirExportVerilog(mod, accum.getCallback(), accum.getUserData());
129  });
130 
131  m.def("export_split_verilog", [](MlirModule mod, std::string directory) {
132  auto cDirectory = mlirStringRefCreateFromCString(directory.c_str());
133  mlirExportSplitVerilog(mod, cDirectory);
134  });
135 
136  py::module esi = m.def_submodule("_esi", "ESI API");
138  py::module msft = m.def_submodule("_msft", "MSFT API");
140  py::module hw = m.def_submodule("_hw", "HW API");
142  py::module seq = m.def_submodule("_seq", "Seq API");
144  py::module om = m.def_submodule("_om", "OM API");
146  py::module sv = m.def_submodule("_sv", "SV API");
148  py::module support = m.def_submodule("_support", "CIRCT support");
150 }
MLIR_CAPI_EXPORTED void registerArcPasses(void)
Definition: Arc.cpp:18
static void registerPasses()
Definition: CIRCTModule.cpp:40
PYBIND11_MODULE(_circt, m)
Definition: CIRCTModule.cpp:53
MLIR_CAPI_EXPORTED void registerCombPasses(void)
Definition: Comb.cpp:16
MLIR_CAPI_EXPORTED void registerFSMPasses(void)
Definition: FSM.cpp:19
MLIR_CAPI_EXPORTED void registerHWArithPasses(void)
Definition: HWArith.cpp:16
MLIR_CAPI_EXPORTED void registerHWPasses(void)
Definition: HW.cpp:32
MLIR_CAPI_EXPORTED void registerHandshakePasses(void)
Definition: Handshake.cpp:18
MLIR_CAPI_EXPORTED void registerSVPasses(void)
Definition: SV.cpp:19
MLIR_CAPI_EXPORTED void registerSeqPasses(void)
Definition: Seq.cpp:20
MLIR_CAPI_EXPORTED MlirLogicalResult mlirExportSplitVerilog(MlirModule, MlirStringRef)
Emits split Verilog files for the specified module into the given directory.
MLIR_CAPI_EXPORTED MlirLogicalResult mlirExportVerilog(MlirModule, MlirStringCallback, void *userData)
Emits verilog for the specified module using the provided callback and user data.
Taken from PybindUtils.h in MLIR.
Definition: PybindUtils.h:36
MlirStringCallback getCallback()
Definition: PybindUtils.h:43
void populateDialectMSFTSubmodule(pybind11::module &m)
void populateDialectOMSubmodule(pybind11::module &m)
void populateSupportSubmodule(pybind11::module &m)
void populateDialectHWSubmodule(pybind11::module &m)
void populateDialectESISubmodule(pybind11::module &m)
void populateDialectSeqSubmodule(pybind11::module &m)
void populateDialectSVSubmodule(pybind11::module &m)
Definition: comb.py:1
Definition: debug.py:1
Definition: emit.py:1
Definition: esi.py:1
Definition: fsm.py:1
Definition: hw.py:1
Definition: ltl.py:1
Definition: msft.py:1
Definition: om.py:1
Definition: seq.py:1
Definition: sv.py:1
Definition: verif.py:1