1 //===- ConvertToLLVMIR.cpp - MLIR to LLVM IR conversion -------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements a translation between the MLIR LLVM dialect and LLVM IR.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #include "mlir/Target/LLVMIR.h"
14 
15 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
16 #include "mlir/Translation.h"
17 
18 #include "llvm/ADT/StringRef.h"
19 #include "llvm/IR/Module.h"
20 #include "llvm/Support/ToolOutputFile.h"
21 
22 using namespace mlir;
23 
24 std::unique_ptr<llvm::Module>
25 mlir::translateModuleToLLVMIR(ModuleOp m, llvm::LLVMContext &llvmContext,
26                               StringRef name) {
27   return LLVM::ModuleTranslation::translateModule<>(m, llvmContext, name);
28 }
29 
30 namespace mlir {
31 void registerToLLVMIRTranslation() {
32   TranslateFromMLIRRegistration registration(
33       "mlir-to-llvmir",
34       [](ModuleOp module, raw_ostream &output) {
35         llvm::LLVMContext llvmContext;
36         auto llvmModule = LLVM::ModuleTranslation::translateModule<>(
37             module, llvmContext, "LLVMDialectModule");
38         if (!llvmModule)
39           return failure();
40 
41         llvmModule->print(output, nullptr);
42         return success();
43       },
44       [](DialectRegistry &registry) { registry.insert<LLVM::LLVMDialect>(); });
45 }
46 } // namespace mlir
47