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/Dialect/OpenMP/OpenMPDialect.h"
16 #include "mlir/Target/LLVMIR/Dialect/LLVMIR/LLVMToLLVMIRTranslation.h"
17 #include "mlir/Target/LLVMIR/Dialect/OpenMP/OpenMPToLLVMIRTranslation.h"
18 #include "mlir/Target/LLVMIR/ModuleTranslation.h"
19 #include "mlir/Translation.h"
20 
21 #include "llvm/ADT/StringRef.h"
22 #include "llvm/IR/Module.h"
23 #include "llvm/IR/Verifier.h"
24 #include "llvm/Support/ToolOutputFile.h"
25 
26 using namespace mlir;
27 
28 std::unique_ptr<llvm::Module>
29 mlir::translateModuleToLLVMIR(ModuleOp m, llvm::LLVMContext &llvmContext,
30                               StringRef name) {
31   auto llvmModule =
32       LLVM::ModuleTranslation::translateModule<>(m, llvmContext, name);
33   if (!llvmModule)
34     emitError(m.getLoc(), "Fail to convert MLIR to LLVM IR");
35   else if (verifyModule(*llvmModule))
36     emitError(m.getLoc(), "LLVM IR fails to verify");
37   return llvmModule;
38 }
39 
40 void mlir::registerLLVMDialectTranslation(DialectRegistry &registry) {
41   registry.insert<LLVM::LLVMDialect>();
42   registry.addDialectInterface<LLVM::LLVMDialect,
43                                LLVMDialectLLVMIRTranslationInterface>();
44 }
45 
46 void mlir::registerLLVMDialectTranslation(MLIRContext &context) {
47   auto *dialect = context.getLoadedDialect<LLVM::LLVMDialect>();
48   if (!dialect || dialect->getRegisteredInterface<
49                       LLVMDialectLLVMIRTranslationInterface>() == nullptr) {
50     DialectRegistry registry;
51     registry.insert<LLVM::LLVMDialect>();
52     registry.addDialectInterface<LLVM::LLVMDialect,
53                                  LLVMDialectLLVMIRTranslationInterface>();
54     context.appendDialectRegistry(registry);
55   }
56 }
57 
58 namespace mlir {
59 void registerToLLVMIRTranslation() {
60   TranslateFromMLIRRegistration registration(
61       "mlir-to-llvmir",
62       [](ModuleOp module, raw_ostream &output) {
63         llvm::LLVMContext llvmContext;
64         auto llvmModule = LLVM::ModuleTranslation::translateModule<>(
65             module, llvmContext, "LLVMDialectModule");
66         if (!llvmModule)
67           return failure();
68 
69         llvmModule->print(output, nullptr);
70         return success();
71       },
72       [](DialectRegistry &registry) {
73         registry.insert<omp::OpenMPDialect>();
74         registry.addDialectInterface<omp::OpenMPDialect,
75                                      OpenMPDialectLLVMIRTranslationInterface>();
76         registerLLVMDialectTranslation(registry);
77       });
78 }
79 } // namespace mlir
80