MLIR  21.0.0git
LLVMIR.cpp
Go to the documentation of this file.
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"
20 
21 using namespace mlir;
22 
23 LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module,
24  LLVMContextRef context) {
25  Operation *moduleOp = unwrap(module);
26 
27  llvm::LLVMContext *ctx = llvm::unwrap(context);
28 
29  std::unique_ptr<llvm::Module> llvmModule =
30  mlir::translateModuleToLLVMIR(moduleOp, *ctx);
31 
32  LLVMModuleRef moduleRef = llvm::wrap(llvmModule.release());
33 
34  return moduleRef;
35 }
36 
39 
41 mlirTypeFromLLVMIRTranslatorCreate(MlirContext ctx) {
42  MLIRContext *context = unwrap(ctx);
43  auto *translator = new LLVM::TypeFromLLVMIRTranslator(*context);
44  return wrap(translator);
45 }
46 
48  MlirTypeFromLLVMIRTranslator translator) {
49  delete static_cast<LLVM::TypeFromLLVMIRTranslator *>(unwrap(translator));
50 }
51 
53  MlirTypeFromLLVMIRTranslator translator, LLVMTypeRef llvmType) {
54  LLVM::TypeFromLLVMIRTranslator *translator_ = unwrap(translator);
55  mlir::Type type = translator_->translateType(llvm::unwrap(llvmType));
56  return wrap(type);
57 }
58 
61 
63 mlirTypeToLLVMIRTranslatorCreate(LLVMContextRef ctx) {
64  llvm::LLVMContext *context = llvm::unwrap(ctx);
65  auto *translator = new LLVM::TypeToLLVMIRTranslator(*context);
66  return wrap(translator);
67 }
68 
70  delete static_cast<LLVM::TypeToLLVMIRTranslator *>(unwrap(translator));
71 }
72 
73 LLVMTypeRef
75  MlirType mlirType) {
76  LLVM::TypeToLLVMIRTranslator *translator_ = unwrap(translator);
77  llvm::Type *type = translator_->translateType(unwrap(mlirType));
78  return llvm::wrap(type);
79 }
MlirType mlirTypeFromLLVMIRTranslatorTranslateType(MlirTypeFromLLVMIRTranslator translator, LLVMTypeRef llvmType)
Translates the given LLVM IR type to the MLIR LLVM dialect.
Definition: LLVMIR.cpp:52
DEFINE_C_API_PTR_METHODS(MlirTypeFromLLVMIRTranslator, mlir::LLVM::TypeFromLLVMIRTranslator) MlirTypeFromLLVMIRTranslator mlirTypeFromLLVMIRTranslatorCreate(MlirContext ctx)
Definition: LLVMIR.cpp:37
void mlirTypeToLLVMIRTranslatorDestroy(MlirTypeToLLVMIRTranslator translator)
Takes an LLVM::TypeToLLVMIRTranslator owned by the caller and destroys it.
Definition: LLVMIR.cpp:69
LLVMTypeRef mlirTypeToLLVMIRTranslatorTranslateType(MlirTypeToLLVMIRTranslator translator, MlirType mlirType)
Translates the given MLIR LLVM dialect to the LLVM IR type.
Definition: LLVMIR.cpp:74
void mlirTypeFromLLVMIRTranslatorDestroy(MlirTypeFromLLVMIRTranslator translator)
Takes an LLVM::TypeFromLLVMIRTranslator owned by the caller and destroys it.
Definition: LLVMIR.cpp:47
LLVMModuleRef mlirTranslateModuleToLLVMIR(MlirOperation module, LLVMContextRef context)
Translate operation that satisfies LLVM dialect module requirements into an LLVM IR module living in ...
Definition: LLVMIR.cpp:23
MLIR_CAPI_EXPORTED MlirTypeFromLLVMIRTranslator mlirTypeFromLLVMIRTranslatorCreate(MlirContext ctx)
Create an LLVM::TypeFromLLVMIRTranslator and transfer ownership to the caller.
MLIR_CAPI_EXPORTED MlirTypeToLLVMIRTranslator mlirTypeToLLVMIRTranslatorCreate(LLVMContextRef ctx)
Create an LLVM::TypeToLLVMIRTranslator and transfer ownership to the caller.
Utility class to translate LLVM IR types to the MLIR LLVM dialect.
Definition: TypeFromLLVM.h:39
Type translateType(llvm::Type *type)
Translates the given LLVM IR type to the MLIR LLVM dialect.
Utility class to translate MLIR LLVM dialect types to LLVM IR.
Definition: TypeToLLVM.h:39
llvm::Type * translateType(Type type)
Translates the given MLIR LLVM dialect type to LLVM IR.
Definition: TypeToLLVM.cpp:192
MLIRContext is the top-level object for a collection of MLIR operations.
Definition: MLIRContext.h:60
Operation is the basic unit of execution within MLIR.
Definition: Operation.h:88
Instances of the Type class are uniqued, have an immutable identifier and an optional mutable compone...
Definition: Types.h:74
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition: Diagnostics.h:19
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition: Diagnostics.h:24
Include the generated interface declarations.
std::unique_ptr< llvm::Module > translateModuleToLLVMIR(Operation *module, llvm::LLVMContext &llvmContext, llvm::StringRef name="LLVMDialectModule", bool disableVerification=false)
Translates a given LLVM dialect module into an LLVM IR module living in the given context.