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/IR/Verifier.h"
21 #include "llvm/Support/ToolOutputFile.h"
22 
23 using namespace mlir;
24 
25 std::unique_ptr<llvm::Module>
26 mlir::translateModuleToLLVMIR(ModuleOp m, llvm::LLVMContext &llvmContext,
27                               StringRef name) {
28   auto llvmModule =
29       LLVM::ModuleTranslation::translateModule<>(m, llvmContext, name);
30   if (verifyModule(*llvmModule))
31     emitError(m.getLoc(), "LLVM IR fails to verify");
32   return llvmModule;
33 }
34 
35 namespace mlir {
36 void registerToLLVMIRTranslation() {
37   TranslateFromMLIRRegistration registration(
38       "mlir-to-llvmir",
39       [](ModuleOp module, raw_ostream &output) {
40         llvm::LLVMContext llvmContext;
41         auto llvmModule = LLVM::ModuleTranslation::translateModule<>(
42             module, llvmContext, "LLVMDialectModule");
43         if (!llvmModule)
44           return failure();
45 
46         llvmModule->print(output, nullptr);
47         return success();
48       },
49       [](DialectRegistry &registry) {
50         registry.insert<LLVM::LLVMDialect, omp::OpenMPDialect>();
51       });
52 }
53 } // namespace mlir
54