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