MLIR 22.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
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
21using namespace mlir;
22
23LLVMModuleRef 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
37char *mlirTranslateModuleToLLVMIRToString(MlirOperation module) {
38 LLVMContextRef llvmCtx = LLVMContextCreate();
39 LLVMModuleRef llvmModule = mlirTranslateModuleToLLVMIR(module, llvmCtx);
40 char *llvmir = LLVMPrintModuleToString(llvmModule);
41 LLVMDisposeModule(llvmModule);
42 LLVMContextDispose(llvmCtx);
43 return llvmir;
44}
45
55
58 delete static_cast<LLVM::TypeFromLLVMIRTranslator *>(unwrap(translator));
59}
60
62 MlirTypeFromLLVMIRTranslator translator, LLVMTypeRef llvmType) {
63 LLVM::TypeFromLLVMIRTranslator *translator_ = unwrap(translator);
64 mlir::Type type = translator_->translateType(llvm::unwrap(llvmType));
65 return wrap(type);
66}
67
70
72mlirTypeToLLVMIRTranslatorCreate(LLVMContextRef ctx) {
73 llvm::LLVMContext *context = llvm::unwrap(ctx);
74 auto *translator = new LLVM::TypeToLLVMIRTranslator(*context);
75 return wrap(translator);
76}
77
79 delete static_cast<LLVM::TypeToLLVMIRTranslator *>(unwrap(translator));
80}
81
82LLVMTypeRef
84 MlirType mlirType) {
85 LLVM::TypeToLLVMIRTranslator *translator_ = unwrap(translator);
86 llvm::Type *type = translator_->translateType(unwrap(mlirType));
87 return llvm::wrap(type);
88}
MlirType mlirTypeFromLLVMIRTranslatorTranslateType(MlirTypeFromLLVMIRTranslator translator, LLVMTypeRef llvmType)
Translates the given LLVM IR type to the MLIR LLVM dialect.
Definition LLVMIR.cpp:61
char * mlirTranslateModuleToLLVMIRToString(MlirOperation module)
Definition LLVMIR.cpp:37
void mlirTypeToLLVMIRTranslatorDestroy(MlirTypeToLLVMIRTranslator translator)
Takes an LLVM::TypeToLLVMIRTranslator owned by the caller and destroys it.
Definition LLVMIR.cpp:78
LLVMTypeRef mlirTypeToLLVMIRTranslatorTranslateType(MlirTypeToLLVMIRTranslator translator, MlirType mlirType)
Translates the given MLIR LLVM dialect to the LLVM IR type.
Definition LLVMIR.cpp:83
void mlirTypeFromLLVMIRTranslatorDestroy(MlirTypeFromLLVMIRTranslator translator)
Takes an LLVM::TypeFromLLVMIRTranslator owned by the caller and destroys it.
Definition LLVMIR.cpp:56
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.
#define DEFINE_C_API_PTR_METHODS(name, cpptype)
Definition Wrap.h:25
Utility class to translate LLVM IR types to the MLIR LLVM dialect.
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.
MLIRContext is the top-level object for a collection of MLIR operations.
Definition MLIRContext.h:63
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
MlirDiagnostic wrap(mlir::Diagnostic &diagnostic)
Definition Diagnostics.h:24
mlir::Diagnostic & unwrap(MlirDiagnostic diagnostic)
Definition Diagnostics.h:19
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.