From f537e0faba1c8bdfa1ae36ebfb4bf878d17c8f0e Mon Sep 17 00:00:00 2001 From: John Demme Date: Tue, 17 Aug 2021 15:40:14 -0700 Subject: [PATCH] [ESI] [Python] Move functionality through the C API (#1595) Necessary to avoid linking issues on Windows. --- include/circt-c/Dialect/ESI.h | 5 +++++ lib/Bindings/Python/ESIModule.cpp | 28 ++++------------------------ lib/CAPI/Dialect/ESI.cpp | 20 ++++++++++++++++++++ 3 files changed, 29 insertions(+), 24 deletions(-) diff --git a/include/circt-c/Dialect/ESI.h b/include/circt-c/Dialect/ESI.h index 5ab0d2ab98..973ec0c73c 100644 --- a/include/circt-c/Dialect/ESI.h +++ b/include/circt-c/Dialect/ESI.h @@ -32,6 +32,11 @@ MLIR_CAPI_EXPORTED MlirOperation circtESIWrapModule(MlirOperation cModOp, long numPorts, const MlirStringRef *ports); +MLIR_CAPI_EXPORTED void circtESIAppendMlirFile(MlirModule, + MlirStringRef fileName); +MLIR_CAPI_EXPORTED MlirOperation circtESILookup(MlirModule, + MlirStringRef symbol); + #ifdef __cplusplus } #endif diff --git a/lib/Bindings/Python/ESIModule.cpp b/lib/Bindings/Python/ESIModule.cpp index 495880a1bc..f6c27e81bc 100644 --- a/lib/Bindings/Python/ESIModule.cpp +++ b/lib/Bindings/Python/ESIModule.cpp @@ -9,17 +9,11 @@ #include "DialectModules.h" #include "circt-c/Dialect/ESI.h" -#include "circt/Dialect/ESI/ESIDialect.h" -#include "circt/Dialect/ESI/ESITypes.h" -#include "circt/Support/LLVM.h" #include "mlir-c/Bindings/Python/Interop.h" #include "mlir/Bindings/Python/PybindAdaptors.h" #include "mlir/CAPI/IR.h" #include "mlir/CAPI/Support.h" -#include "mlir/IR/Builders.h" -#include "mlir/Parser.h" -#include "mlir/Support/FileUtilities.h" #include "llvm/ADT/SmallVector.h" @@ -28,9 +22,6 @@ #include namespace py = pybind11; -using namespace circt; -using namespace circt::esi; - //===----------------------------------------------------------------------===// // The main entry point into the ESI Assembly API. //===----------------------------------------------------------------------===// @@ -45,21 +36,13 @@ public: /// Load the contents of an MLIR asm file into the system module. void loadMlir(std::string filename) { - auto loadedMod = mlir::parseSourceFile(filename, ctxt()); - Block *loadedBlock = loadedMod->getBody(); - ModuleOp modOp = mod(); - assert(!modOp->getRegions().empty()); - if (modOp.body().empty()) { - modOp.body().push_back(loadedBlock); - return; - } - auto &ops = modOp.getBody()->getOperations(); - ops.splice(ops.end(), loadedBlock->getOperations()); + circtESIAppendMlirFile(cModuleOp, + mlirStringRefCreateFromCString(filename.c_str())); } MlirOperation lookup(std::string symbol) { - Operation *found = SymbolTable::lookupSymbolIn(mod(), symbol); - return wrap(found); + return circtESILookup(cModuleOp, + mlirStringRefCreateFromCString(symbol.c_str())); } void printCapnpSchema(py::object fileObject) { @@ -70,9 +53,6 @@ public: } private: - MLIRContext *ctxt() { return unwrap(cCtxt); } - ModuleOp mod() { return unwrap(cModuleOp); } - MlirContext cCtxt; MlirModule cModuleOp; }; diff --git a/lib/CAPI/Dialect/ESI.cpp b/lib/CAPI/Dialect/ESI.cpp index 4711217953..468a47cd33 100644 --- a/lib/CAPI/Dialect/ESI.cpp +++ b/lib/CAPI/Dialect/ESI.cpp @@ -9,8 +9,12 @@ #include "mlir/CAPI/Support.h" #include "mlir/CAPI/Utils.h" #include "mlir/IR/Builders.h" +#include "mlir/IR/SymbolTable.h" +#include "mlir/Parser.h" +#include "mlir/Support/FileUtilities.h" using namespace circt::esi; +using namespace mlir; MLIR_DEFINE_CAPI_DIALECT_REGISTRATION(ESI, esi, circt::esi::ESIDialect) @@ -48,3 +52,19 @@ MlirOperation circtESIWrapModule(MlirOperation cModOp, long numPorts, mlir::Operation *wrapper = buildESIWrapper(b, modOp, portTriples); return wrap(wrapper); } + +void circtESIAppendMlirFile(MlirModule cMod, MlirStringRef filename) { + ModuleOp modOp = unwrap(cMod); + auto loadedMod = parseSourceFile(unwrap(filename), modOp.getContext()); + Block *loadedBlock = loadedMod->getBody(); + assert(!modOp->getRegions().empty()); + if (modOp.body().empty()) { + modOp.body().push_back(loadedBlock); + return; + } + auto &ops = modOp.getBody()->getOperations(); + ops.splice(ops.end(), loadedBlock->getOperations()); +} +MlirOperation circtESILookup(MlirModule mod, MlirStringRef symbol) { + return wrap(SymbolTable::lookupSymbolIn(unwrap(mod), unwrap(symbol))); +}