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", [](ModuleOp module, raw_ostream &output) { 34 llvm::LLVMContext llvmContext; 35 auto llvmModule = LLVM::ModuleTranslation::translateModule<>( 36 module, llvmContext, "LLVMDialectModule"); 37 if (!llvmModule) 38 return failure(); 39 40 llvmModule->print(output, nullptr); 41 return success(); 42 }); 43 } 44 } // namespace mlir 45