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