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 | |
12 | #include "llvm/IR/LLVMContext.h" |
13 | #include "llvm/IR/Module.h" |
14 | #include "llvm/IR/Type.h" |
15 | |
16 | #include "mlir/CAPI/IR.h" |
17 | #include "mlir/CAPI/Wrap.h" |
18 | #include "mlir/Target/LLVMIR/ModuleTranslation.h" |
19 | #include "mlir/Target/LLVMIR/TypeFromLLVM.h" |
20 | |
21 | using namespace mlir; |
22 | |
23 | LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module, |
24 | LLVMContextRef context) { |
25 | Operation *moduleOp = unwrap(c: module); |
26 | |
27 | llvm::LLVMContext *ctx = llvm::unwrap(P: context); |
28 | |
29 | std::unique_ptr<llvm::Module> llvmModule = |
30 | mlir::translateModuleToLLVMIR(module: moduleOp, llvmContext&: *ctx); |
31 | |
32 | LLVMModuleRef moduleRef = llvm::wrap(P: llvmModule.release()); |
33 | |
34 | return moduleRef; |
35 | } |
36 | |
37 | DEFINE_C_API_PTR_METHODS(MlirTypeFromLLVMIRTranslator, |
38 | mlir::LLVM::TypeFromLLVMIRTranslator) |
39 | |
40 | MlirTypeFromLLVMIRTranslator |
41 | mlirTypeFromLLVMIRTranslatorCreate(MlirContext ctx) { |
42 | MLIRContext *context = unwrap(c: ctx); |
43 | auto *translator = new LLVM::TypeFromLLVMIRTranslator(*context); |
44 | return wrap(cpp: translator); |
45 | } |
46 | |
47 | void mlirTypeFromLLVMIRTranslatorDestroy( |
48 | MlirTypeFromLLVMIRTranslator translator) { |
49 | delete static_cast<LLVM::TypeFromLLVMIRTranslator *>(unwrap(c: translator)); |
50 | } |
51 | |
52 | MlirType mlirTypeFromLLVMIRTranslatorTranslateType( |
53 | MlirTypeFromLLVMIRTranslator translator, LLVMTypeRef llvmType) { |
54 | LLVM::TypeFromLLVMIRTranslator *translator_ = unwrap(c: translator); |
55 | mlir::Type type = translator_->translateType(type: llvm::unwrap(P: llvmType)); |
56 | return wrap(cpp: type); |
57 | } |
58 | |
59 | DEFINE_C_API_PTR_METHODS(MlirTypeToLLVMIRTranslator, |
60 | mlir::LLVM::TypeToLLVMIRTranslator) |
61 | |
62 | MlirTypeToLLVMIRTranslator |
63 | mlirTypeToLLVMIRTranslatorCreate(LLVMContextRef ctx) { |
64 | llvm::LLVMContext *context = llvm::unwrap(P: ctx); |
65 | auto *translator = new LLVM::TypeToLLVMIRTranslator(*context); |
66 | return wrap(cpp: translator); |
67 | } |
68 | |
69 | void mlirTypeToLLVMIRTranslatorDestroy(MlirTypeToLLVMIRTranslator translator) { |
70 | delete static_cast<LLVM::TypeToLLVMIRTranslator *>(unwrap(c: translator)); |
71 | } |
72 | |
73 | LLVMTypeRef |
74 | mlirTypeToLLVMIRTranslatorTranslateType(MlirTypeToLLVMIRTranslator translator, |
75 | MlirType mlirType) { |
76 | LLVM::TypeToLLVMIRTranslator *translator_ = unwrap(c: translator); |
77 | llvm::Type *type = translator_->translateType(type: unwrap(c: mlirType)); |
78 | return llvm::wrap(P: type); |
79 | } |
80 | |