- Notifications
You must be signed in to change notification settings - Fork 15k
[MLIR][Python] expose translate_module_to_llvmir #163881
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
140aa66 to 8d6d1b9 Compare | ✅ With the latest revision this PR passed the C/C++ code formatter. |
a27cd49 to 92cc48c Compare 92cc48c to 5193c35 Compare | @llvm/pr-subscribers-mlir-llvm @llvm/pr-subscribers-mlir Author: Maksim Levental (makslevental) ChangesThis PR exposes m.def( "translate_module_to_llvmir", [](MlirOperation module) { LLVMContextRef llvmCtx = LLVMContextCreate(); LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(module, llvmCtx); char *llvmir = LLVMPrintModuleToString(llvmModule); LLVMDisposeModule(llvmModule); LLVMContextDispose(llvmCtx); return llvmir; },because there seems to be absolutely no way to prevent symbol DCE in the transitive dep MLIRCAPITarget which is the target that actually links Full diff: https://github.com/llvm/llvm-project/pull/163881.diff 5 Files Affected:
diff --git a/mlir/include/mlir-c/Target/LLVMIR.h b/mlir/include/mlir-c/Target/LLVMIR.h index b5f948961e898..ec5ae88d485dc 100644 --- a/mlir/include/mlir-c/Target/LLVMIR.h +++ b/mlir/include/mlir-c/Target/LLVMIR.h @@ -33,6 +33,9 @@ extern "C" { MLIR_CAPI_EXPORTED LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context); +MLIR_CAPI_EXPORTED char * +mlirTranslateModuleToLLVMIRToString(MlirOperation module); + struct MlirTypeFromLLVMIRTranslator { void *ptr; }; diff --git a/mlir/lib/Bindings/Python/DialectLLVM.cpp b/mlir/lib/Bindings/Python/DialectLLVM.cpp index 38de4a0e329a0..870a713b8edcb 100644 --- a/mlir/lib/Bindings/Python/DialectLLVM.cpp +++ b/mlir/lib/Bindings/Python/DialectLLVM.cpp @@ -11,6 +11,7 @@ #include "mlir-c/Dialect/LLVM.h" #include "mlir-c/IR.h" #include "mlir-c/Support.h" +#include "mlir-c/Target/LLVMIR.h" #include "mlir/Bindings/Python/Diagnostics.h" #include "mlir/Bindings/Python/Nanobind.h" #include "mlir/Bindings/Python/NanobindAdaptors.h" @@ -24,7 +25,7 @@ using namespace mlir; using namespace mlir::python; using namespace mlir::python::nanobind_adaptors; -static void populateDialectLLVMSubmodule(const nanobind::module_ &m) { +static void populateDialectLLVMSubmodule(nanobind::module_ &m) { //===--------------------------------------------------------------------===// // StructType @@ -154,6 +155,16 @@ static void populateDialectLLVMSubmodule(const nanobind::module_ &m) { .def_property_readonly("address_space", [](MlirType type) { return mlirLLVMPointerTypeGetAddressSpace(type); }); + + m.def( + "translate_module_to_llvmir", + [](MlirOperation module) { + return mlirTranslateModuleToLLVMIRToString(module); + }, + // clang-format off + nb::sig("def translate_module_to_llvmir(module: " MAKE_MLIR_PYTHON_QUALNAME("ir.Operation") ") -> str"), + // clang-format on + "module"_a, nb::rv_policy::take_ownership); } NB_MODULE(_mlirDialectsLLVM, m) { diff --git a/mlir/lib/CAPI/Target/LLVMIR.cpp b/mlir/lib/CAPI/Target/LLVMIR.cpp index 1c1912aec0f2f..00229dffafb61 100644 --- a/mlir/lib/CAPI/Target/LLVMIR.cpp +++ b/mlir/lib/CAPI/Target/LLVMIR.cpp @@ -34,6 +34,15 @@ LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module, return moduleRef; } +char *mlirTranslateModuleToLLVMIRToString(MlirOperation module) { + LLVMContextRef llvmCtx = LLVMContextCreate(); + LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(module, llvmCtx); + char *llvmir = LLVMPrintModuleToString(llvmModule); + LLVMDisposeModule(llvmModule); + LLVMContextDispose(llvmCtx); + return llvmir; +} + DEFINE_C_API_PTR_METHODS(MlirTypeFromLLVMIRTranslator, mlir::LLVM::TypeFromLLVMIRTranslator) diff --git a/mlir/python/CMakeLists.txt b/mlir/python/CMakeLists.txt index 9f5246de6bda0..a64d2e49c974f 100644 --- a/mlir/python/CMakeLists.txt +++ b/mlir/python/CMakeLists.txt @@ -591,6 +591,8 @@ declare_mlir_python_extension(MLIRPythonExtension.Dialects.LLVM.Pybind EMBED_CAPI_LINK_LIBS MLIRCAPIIR MLIRCAPILLVM + # Misnomer - this is only the LLVMIR translation target. + MLIRCAPITarget ) declare_mlir_python_extension(MLIRPythonExtension.Dialects.Quant.Pybind diff --git a/mlir/test/python/dialects/llvm.py b/mlir/test/python/dialects/llvm.py index d9ffdeb65bfd4..8ea0fddee3f7c 100644 --- a/mlir/test/python/dialects/llvm.py +++ b/mlir/test/python/dialects/llvm.py @@ -150,3 +150,22 @@ def testIntrinsics(): result = llvm.intr_memset(alloca, c_0, c_128, False) # CHECK: "llvm.intr.memset"(%[[ALLOCA]], %[[CST0]], %[[CST128]]) <{isVolatile = false}> : (!llvm.ptr, i8, i32) -> () print(result) + + +# CHECK-LABEL: testTranslateToLLVMIR +@constructAndPrintInModule +def testTranslateToLLVMIR(): + with Context(), Location.unknown(): + module = Module.parse( + """\ + llvm.func @add(%arg0: i64, %arg1: i64) -> i64 { + %0 = llvm.add %arg0, %arg1 : i64 + llvm.return %0 : i64 + } + """ + ) + # CHECK: define i64 @add(i64 %0, i64 %1) { + # CHECK: %3 = add i64 %0, %1 + # CHECK: ret i64 %3 + # CHECK: } + print(llvm.translate_module_to_llvmir(module.operation)) |
| Note, I added m.def( "translate_module_to_llvmir", [](MlirOperation module) { LLVMContextRef llvmCtx = LLVMContextCreate(); LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(module, llvmCtx); char *llvmir = LLVMPrintModuleToString(llvmModule); LLVMDisposeModule(llvmModule); LLVMContextDispose(llvmCtx); return llvmir; },because there seems to be absolutely no way to prevent symbol DCE (of e.g. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It might be easier if we had an official LLVM Python bindings for this.
Lol I agree but I'm not gonna even start to try to propose anything along the lines of that - and anyway I have bindings in eudsl: https://github.com/llvm/eudsl/tree/main/projects/eudsl-llvmpy |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM, but it does feel a bit ad-hoc.
Signed-off-by: Muzammiluddin Syed <muzasyed@amd.com>
Signed-off-by: Muzammiluddin Syed <muzasyed@amd.com>
Signed-off-by: Muzammiluddin Syed <muzasyed@amd.com>
Still carrying these reverts: - llvm/llvm-project#160615 (See #22171) - llvm/llvm-project#163440 (See #22354 (comment)) Fixes: - Remove deprecated references to `.CaseLowers` - Added dependencies required due to new added dependency to LLVMIR llvm/llvm-project#163881 --------- Signed-off-by: Muzammiluddin Syed <muzasyed@amd.com>
This PR exposes `translate_module_to_llvmir` in the Python bindings.
This PR exposes `translate_module_to_llvmir` in the Python bindings.
This PR exposes
translate_module_to_llvmirin the Python bindings.