CIRCT  19.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 "DialectModules.h"
10 
11 #include "circt-c/Conversion.h"
12 #include "circt-c/Dialect/Comb.h"
13 #include "circt-c/Dialect/Debug.h"
14 #include "circt-c/Dialect/ESI.h"
15 #include "circt-c/Dialect/FSM.h"
16 #include "circt-c/Dialect/HW.h"
19 #include "circt-c/Dialect/LTL.h"
20 #include "circt-c/Dialect/MSFT.h"
21 #include "circt-c/Dialect/OM.h"
22 #include "circt-c/Dialect/SV.h"
23 #include "circt-c/Dialect/Seq.h"
24 #include "circt-c/Dialect/Verif.h"
25 #include "circt-c/ExportVerilog.h"
26 #include "mlir-c/Bindings/Python/Interop.h"
27 #include "mlir-c/IR.h"
28 #include "mlir-c/Transforms.h"
29 #include "mlir/Bindings/Python/PybindAdaptors.h"
30 
31 #include "llvm-c/ErrorHandling.h"
32 #include "llvm/Support/Signals.h"
33 
34 #include "PybindUtils.h"
35 #include <pybind11/pybind11.h>
36 namespace py = pybind11;
37 
38 static void registerPasses() {
45  mlirRegisterConversionPasses();
46  mlirRegisterTransformsPasses();
47 }
48 
49 PYBIND11_MODULE(_circt, m) {
50  m.doc() = "CIRCT Python Native Extension";
52  llvm::sys::PrintStackTraceOnErrorSignal(/*argv=*/"");
53  LLVMEnablePrettyStackTrace();
54 
55  m.def(
56  "register_dialects",
57  [](py::object capsule) {
58  // Get the MlirContext capsule from PyMlirContext capsule.
59  auto wrappedCapsule = capsule.attr(MLIR_PYTHON_CAPI_PTR_ATTR);
60  MlirContext context = mlirPythonCapsuleToContext(wrappedCapsule.ptr());
61 
62  // Collect CIRCT dialects to register.
63  MlirDialectHandle comb = mlirGetDialectHandle__comb__();
64  mlirDialectHandleRegisterDialect(comb, context);
65  mlirDialectHandleLoadDialect(comb, context);
66 
67  MlirDialectHandle debug = mlirGetDialectHandle__debug__();
68  mlirDialectHandleRegisterDialect(debug, context);
69  mlirDialectHandleLoadDialect(debug, context);
70 
71  MlirDialectHandle esi = mlirGetDialectHandle__esi__();
72  mlirDialectHandleRegisterDialect(esi, context);
73  mlirDialectHandleLoadDialect(esi, context);
74 
75  MlirDialectHandle msft = mlirGetDialectHandle__msft__();
76  mlirDialectHandleRegisterDialect(msft, context);
77  mlirDialectHandleLoadDialect(msft, context);
78 
79  MlirDialectHandle hw = mlirGetDialectHandle__hw__();
80  mlirDialectHandleRegisterDialect(hw, context);
81  mlirDialectHandleLoadDialect(hw, context);
82 
83  MlirDialectHandle hwarith = mlirGetDialectHandle__hwarith__();
84  mlirDialectHandleRegisterDialect(hwarith, context);
85  mlirDialectHandleLoadDialect(hwarith, context);
86 
87  MlirDialectHandle om = mlirGetDialectHandle__om__();
88  mlirDialectHandleRegisterDialect(om, context);
89  mlirDialectHandleLoadDialect(om, context);
90 
91  MlirDialectHandle seq = mlirGetDialectHandle__seq__();
92  mlirDialectHandleRegisterDialect(seq, context);
93  mlirDialectHandleLoadDialect(seq, context);
94 
95  MlirDialectHandle sv = mlirGetDialectHandle__sv__();
96  mlirDialectHandleRegisterDialect(sv, context);
97  mlirDialectHandleLoadDialect(sv, context);
98 
99  MlirDialectHandle fsm = mlirGetDialectHandle__fsm__();
100  mlirDialectHandleRegisterDialect(fsm, context);
101  mlirDialectHandleLoadDialect(fsm, context);
102 
103  MlirDialectHandle handshake = mlirGetDialectHandle__handshake__();
104  mlirDialectHandleRegisterDialect(handshake, context);
105  mlirDialectHandleLoadDialect(handshake, context);
106 
107  MlirDialectHandle ltl = mlirGetDialectHandle__ltl__();
108  mlirDialectHandleRegisterDialect(ltl, context);
109  mlirDialectHandleLoadDialect(ltl, context);
110 
111  MlirDialectHandle verif = mlirGetDialectHandle__verif__();
112  mlirDialectHandleRegisterDialect(verif, context);
113  mlirDialectHandleLoadDialect(verif, context);
114  },
115  "Register CIRCT dialects on a PyMlirContext.");
116 
117  m.def("export_verilog", [](MlirModule mod, py::object fileObject) {
118  circt::python::PyFileAccumulator accum(fileObject, false);
119  py::gil_scoped_release();
120  mlirExportVerilog(mod, accum.getCallback(), accum.getUserData());
121  });
122 
123  m.def("export_split_verilog", [](MlirModule mod, std::string directory) {
124  auto cDirectory = mlirStringRefCreateFromCString(directory.c_str());
125  mlirExportSplitVerilog(mod, cDirectory);
126  });
127 
128  py::module esi = m.def_submodule("_esi", "ESI API");
130  py::module msft = m.def_submodule("_msft", "MSFT API");
132  py::module hw = m.def_submodule("_hw", "HW API");
134  py::module seq = m.def_submodule("_seq", "Seq API");
136  py::module om = m.def_submodule("_om", "OM API");
138  py::module sv = m.def_submodule("_sv", "SV API");
140 }
static void registerPasses()
Definition: CIRCTModule.cpp:38
PYBIND11_MODULE(_circt, m)
Definition: CIRCTModule.cpp:49
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 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 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: 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