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/DC.h"
15 #include "circt-c/Dialect/Debug.h"
16 #include "circt-c/Dialect/ESI.h"
17 #include "circt-c/Dialect/Emit.h"
18 #include "circt-c/Dialect/FSM.h"
19 #include "circt-c/Dialect/HW.h"
22 #include "circt-c/Dialect/LTL.h"
23 #include "circt-c/Dialect/MSFT.h"
24 #include "circt-c/Dialect/OM.h"
25 #include "circt-c/Dialect/RTG.h"
26 #ifdef CIRCT_INCLUDE_TESTS
28 #endif
29 #include "circt-c/Dialect/SV.h"
30 #include "circt-c/Dialect/Seq.h"
31 #include "circt-c/Dialect/Verif.h"
32 #include "circt-c/ExportVerilog.h"
33 #include "mlir-c/Bindings/Python/Interop.h"
34 #include "mlir-c/IR.h"
35 #include "mlir-c/Transforms.h"
36 #include "mlir/Bindings/Python/PybindAdaptors.h"
37 
38 #include "llvm-c/ErrorHandling.h"
39 #include "llvm/Support/Signals.h"
40 
41 #include "PybindUtils.h"
42 #include <pybind11/pybind11.h>
43 namespace py = pybind11;
44 
45 static void registerPasses() {
55  mlirRegisterConversionPasses();
56  mlirRegisterTransformsPasses();
57 }
58 
59 PYBIND11_MODULE(_circt, m) {
60  m.doc() = "CIRCT Python Native Extension";
62  llvm::sys::PrintStackTraceOnErrorSignal(/*argv=*/"");
63  LLVMEnablePrettyStackTrace();
64 
65  m.def(
66  "register_dialects",
67  [](py::object capsule) {
68  // Get the MlirContext capsule from PyMlirContext capsule.
69  auto wrappedCapsule = capsule.attr(MLIR_PYTHON_CAPI_PTR_ATTR);
70  MlirContext context = mlirPythonCapsuleToContext(wrappedCapsule.ptr());
71 
72  // Collect CIRCT dialects to register.
73  MlirDialectHandle comb = mlirGetDialectHandle__comb__();
74  mlirDialectHandleRegisterDialect(comb, context);
75  mlirDialectHandleLoadDialect(comb, context);
76 
77  MlirDialectHandle debug = mlirGetDialectHandle__debug__();
78  mlirDialectHandleRegisterDialect(debug, context);
79  mlirDialectHandleLoadDialect(debug, context);
80 
81  MlirDialectHandle emit = mlirGetDialectHandle__emit__();
82  mlirDialectHandleRegisterDialect(emit, context);
83  mlirDialectHandleLoadDialect(emit, context);
84 
85  MlirDialectHandle esi = mlirGetDialectHandle__esi__();
86  mlirDialectHandleRegisterDialect(esi, context);
87  mlirDialectHandleLoadDialect(esi, context);
88 
89  MlirDialectHandle msft = mlirGetDialectHandle__msft__();
90  mlirDialectHandleRegisterDialect(msft, context);
91  mlirDialectHandleLoadDialect(msft, context);
92 
93  MlirDialectHandle hw = mlirGetDialectHandle__hw__();
94  mlirDialectHandleRegisterDialect(hw, context);
95  mlirDialectHandleLoadDialect(hw, context);
96 
97  MlirDialectHandle hwarith = mlirGetDialectHandle__hwarith__();
98  mlirDialectHandleRegisterDialect(hwarith, context);
99  mlirDialectHandleLoadDialect(hwarith, context);
100 
101  MlirDialectHandle om = mlirGetDialectHandle__om__();
102  mlirDialectHandleRegisterDialect(om, context);
103  mlirDialectHandleLoadDialect(om, context);
104 
105  MlirDialectHandle rtg = mlirGetDialectHandle__rtg__();
106  mlirDialectHandleRegisterDialect(rtg, context);
107  mlirDialectHandleLoadDialect(rtg, context);
108 
109 #ifdef CIRCT_INCLUDE_TESTS
110  MlirDialectHandle rtgtest = mlirGetDialectHandle__rtgtest__();
111  mlirDialectHandleRegisterDialect(rtgtest, context);
112  mlirDialectHandleLoadDialect(rtgtest, context);
113 #endif
114 
115  MlirDialectHandle seq = mlirGetDialectHandle__seq__();
116  mlirDialectHandleRegisterDialect(seq, context);
117  mlirDialectHandleLoadDialect(seq, context);
118 
119  MlirDialectHandle sv = mlirGetDialectHandle__sv__();
120  mlirDialectHandleRegisterDialect(sv, context);
121  mlirDialectHandleLoadDialect(sv, context);
122 
123  MlirDialectHandle fsm = mlirGetDialectHandle__fsm__();
124  mlirDialectHandleRegisterDialect(fsm, context);
125  mlirDialectHandleLoadDialect(fsm, context);
126 
127  MlirDialectHandle handshake = mlirGetDialectHandle__handshake__();
128  mlirDialectHandleRegisterDialect(handshake, context);
129  mlirDialectHandleLoadDialect(handshake, context);
130 
131  MlirDialectHandle ltl = mlirGetDialectHandle__ltl__();
132  mlirDialectHandleRegisterDialect(ltl, context);
133  mlirDialectHandleLoadDialect(ltl, context);
134 
135  MlirDialectHandle verif = mlirGetDialectHandle__verif__();
136  mlirDialectHandleRegisterDialect(verif, context);
137  mlirDialectHandleLoadDialect(verif, context);
138  },
139  "Register CIRCT dialects on a PyMlirContext.");
140 
141  m.def("export_verilog", [](MlirModule mod, py::object fileObject) {
142  circt::python::PyFileAccumulator accum(fileObject, false);
143  py::gil_scoped_release();
144  mlirExportVerilog(mod, accum.getCallback(), accum.getUserData());
145  });
146 
147  m.def("export_split_verilog", [](MlirModule mod, std::string directory) {
148  auto cDirectory = mlirStringRefCreateFromCString(directory.c_str());
149  mlirExportSplitVerilog(mod, cDirectory);
150  });
151 
152  py::module esi = m.def_submodule("_esi", "ESI API");
154  py::module msft = m.def_submodule("_msft", "MSFT API");
156  py::module hw = m.def_submodule("_hw", "HW API");
158  py::module seq = m.def_submodule("_seq", "Seq API");
160  py::module om = m.def_submodule("_om", "OM API");
162  py::module rtg = m.def_submodule("_rtg", "RTG API");
164  py::module rtgtool = m.def_submodule("_rtgtool", "RTGTool API");
166 #ifdef CIRCT_INCLUDE_TESTS
167  py::module rtgtest = m.def_submodule("_rtgtest", "RTGTest API");
168  circt::python::populateDialectRTGTestSubmodule(rtgtest);
169 #endif
170  py::module sv = m.def_submodule("_sv", "SV API");
172  py::module support = m.def_submodule("_support", "CIRCT support");
174 }
MLIR_CAPI_EXPORTED void registerArcPasses(void)
Definition: Arc.cpp:18
static void registerPasses()
Definition: CIRCTModule.cpp:45
PYBIND11_MODULE(_circt, m)
Definition: CIRCTModule.cpp:59
MLIR_CAPI_EXPORTED void registerCombPasses(void)
Definition: Comb.cpp:16
MLIR_CAPI_EXPORTED void registerDCPasses(void)
Definition: DC.cpp:18
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 populateDialectRTGToolSubmodule(pybind11::module &m)
void populateDialectRTGSubmodule(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: rtg.py:1
Definition: seq.py:1
Definition: sv.py:1
Definition: verif.py:1