This repository was archived by the owner on Oct 11, 2025. It is now read-only.
File tree Expand file tree Collapse file tree 4 files changed +88
-1
lines changed Expand file tree Collapse file tree 4 files changed +88
-1
lines changed Original file line number Diff line number Diff line change 1+ //===-- LLVMIR.h - C Interface for MLIR LLVMIR Target -------------*- C -*-===//
2+ //
3+ // Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+ // Exceptions.
5+ // See https://llvm.org/LICENSE.txt for license information.
6+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+ //
8+ //===----------------------------------------------------------------------===//
9+ //
10+ // This header declares the C interface to target LLVMIR with MLIR.
11+ //
12+ //===----------------------------------------------------------------------===//
13+
14+ #ifndef MLIR_C_TARGET_LLVMIR_H
15+ #define MLIR_C_TARGET_LLVMIR_H
16+
17+ #include "mlir-c/IR.h"
18+ #include "mlir-c/Support.h"
19+ #include "llvm-c/Support.h"
20+
21+ #ifdef __cplusplus
22+ extern "C" {
23+ #endif
24+
25+ /// Translate operation that satisfies LLVM dialect module requirements into an
26+ /// LLVM IR module living in the given context. This translates operations from
27+ /// any dilalect that has a registered implementation of
28+ /// LLVMTranslationDialectInterface.
29+ ///
30+ /// \returns the generated LLVM IR Module from the translated MLIR module, it is
31+ /// owned by the caller.
32+ MLIR_CAPI_EXPORTED LLVMModuleRef
33+ mlirTranslateModuleToLLVMIR (MlirOperation module , LLVMContextRef context );
34+
35+ #ifdef __cplusplus
36+ }
37+ #endif
38+
39+ #endif // MLIR_C_TARGET_LLVMIR_H
Original file line number Diff line number Diff line change @@ -14,6 +14,7 @@ add_subdirectory(Interfaces)
1414add_subdirectory (IR)
1515add_subdirectory (RegisterEverything)
1616add_subdirectory (Transforms)
17+ add_subdirectory (Target )
1718
1819if (MLIR_ENABLE_EXECUTION_ENGINE)
1920 add_subdirectory (ExecutionEngine)
@@ -36,4 +37,3 @@ if(MLIR_BUILD_MLIR_C_DYLIB)
3637 endif ()
3738 endif ()
3839endif ()
39-
Original file line number Diff line number Diff line change 1+ add_mlir_upstream_c_api_library(MLIRCAPITarget
2+ LLVMIR.cpp
3+
4+ LINK_COMPONENTS
5+ Core
6+
7+ LINK_LIBS PUBLIC
8+ MLIRToLLVMIRTranslationRegistration
9+ MLIRCAPIIR
10+ MLIRLLVMToLLVMIRTranslation
11+ MLIRSupport
12+ )
Original file line number Diff line number Diff line change 1+ // ===-- LLVMIR.h - C Interface for MLIR LLVMIR Target ---------------------===//
2+ //
3+ // Part of the LLVM Project, under the Apache License v2.0 with LLVM
4+ // Exceptions.
5+ // See https://llvm.org/LICENSE.txt for license information.
6+ // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7+ //
8+ // ===----------------------------------------------------------------------===//
9+
10+ #include " mlir-c/Target/LLVMIR.h"
11+ #include " llvm-c/Support.h"
12+
13+ #include " llvm/IR/LLVMContext.h"
14+ #include " llvm/IR/Module.h"
15+ #include < memory>
16+
17+ #include " mlir/CAPI/IR.h"
18+ #include " mlir/CAPI/Support.h"
19+ #include " mlir/CAPI/Wrap.h"
20+ #include " mlir/Target/LLVMIR/ModuleTranslation.h"
21+
22+ using namespace mlir ;
23+
24+ LLVMModuleRef mlirTranslateModuleToLLVMIR (MlirOperation module ,
25+ LLVMContextRef context) {
26+ Operation *moduleOp = unwrap (module );
27+
28+ llvm::LLVMContext *ctx = llvm::unwrap (context);
29+
30+ std::unique_ptr<llvm::Module> llvmModule =
31+ mlir::translateModuleToLLVMIR (moduleOp, *ctx);
32+
33+ LLVMModuleRef moduleRef = llvm::wrap (llvmModule.release ());
34+
35+ return moduleRef;
36+ }
You can’t perform that action at this time.
0 commit comments