Call LLVM opt to optimize bitcode (#245)

* Call LLVM opt to optimize bitcode

* Rename variables
This commit is contained in:
Tung D. Le 2020-08-03 12:46:21 +09:00 committed by GitHub
parent 58ee62fb49
commit dbe0d734b5
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 18 additions and 5 deletions

View File

@ -5,6 +5,7 @@
namespace onnx_mlir { namespace onnx_mlir {
std::string kExecPath = "@CMAKE_INSTALL_PREFIX@/bin/onnx-mlir"; /* fallback if not set by main */ std::string kExecPath = "@CMAKE_INSTALL_PREFIX@/bin/onnx-mlir"; /* fallback if not set by main */
const std::string kInstPath = "@CMAKE_INSTALL_PREFIX@"; const std::string kInstPath = "@CMAKE_INSTALL_PREFIX@";
const std::string kOptPath = "@LLVM_PROJ_BUILD@/bin/opt";
const std::string kLlcPath = "@LLVM_PROJ_BUILD@/bin/llc"; const std::string kLlcPath = "@LLVM_PROJ_BUILD@/bin/llc";
const std::string kCxxPath = "@CMAKE_CXX_COMPILER@"; const std::string kCxxPath = "@CMAKE_CXX_COMPILER@";
const std::string kLinkerPath = "@CMAKE_LINKER@"; const std::string kLinkerPath = "@CMAKE_LINKER@";

View File

@ -250,16 +250,28 @@ void genConstPackObj(const mlir::OwningModuleRef &module,
#endif #endif
} }
// Write LLVM bitcode. // Write LLVM optimized bitcode.
void genLLVMBitcode(const mlir::OwningModuleRef &module, string bitcodePath) { void genLLVMBitcode(const mlir::OwningModuleRef &module,
string optimizedBitcodePath, string outputBaseName) {
error_code error; error_code error;
// Write bitcode to a file.
string unoptimizedBitcodePath = outputBaseName + ".unoptimized.bc";
llvm::FileRemover unoptimzedBitcodeRemover(unoptimizedBitcodePath);
llvm::raw_fd_ostream moduleBitcodeStream( llvm::raw_fd_ostream moduleBitcodeStream(
bitcodePath, error, llvm::sys::fs::F_None); unoptimizedBitcodePath, error, llvm::sys::fs::F_None);
llvm::WriteBitcodeToFile( llvm::WriteBitcodeToFile(
*mlir::translateModuleToLLVMIR(*module), moduleBitcodeStream); *mlir::translateModuleToLLVMIR(*module), moduleBitcodeStream);
moduleBitcodeStream.flush(); moduleBitcodeStream.flush();
// Use the LLVM's 'opt' command to optimize the bitcode.
Command optBitcode(/*exePath=*/kOptPath);
optBitcode.appendStr("-O3")
.appendList({"-o", optimizedBitcodePath})
.appendStr(unoptimizedBitcodePath)
.exec();
} }
// Compile LLVM bitcode to object file. // Compile LLVM bitcode to object file.
@ -319,7 +331,7 @@ void compileModuleToSharedLibrary(
llvm::FileRemover constPackObjRemover(constPackObjPath.getValue()); llvm::FileRemover constPackObjRemover(constPackObjPath.getValue());
string bitcodePath = outputBaseName + ".bc"; string bitcodePath = outputBaseName + ".bc";
genLLVMBitcode(module, bitcodePath); genLLVMBitcode(module, bitcodePath, outputBaseName);
llvm::FileRemover bitcodeRemover(bitcodePath); llvm::FileRemover bitcodeRemover(bitcodePath);
string modelObjPath = outputBaseName + ".o"; string modelObjPath = outputBaseName + ".o";
@ -340,7 +352,7 @@ void compileModuleToJniJar(
llvm::FileRemover constPackObjRemover(constPackObjPath.getValue()); llvm::FileRemover constPackObjRemover(constPackObjPath.getValue());
string bitcodePath = outputBaseName + ".bc"; string bitcodePath = outputBaseName + ".bc";
genLLVMBitcode(module, bitcodePath); genLLVMBitcode(module, bitcodePath, outputBaseName);
llvm::FileRemover bitcodeRemover(bitcodePath); llvm::FileRemover bitcodeRemover(bitcodePath);
string modelObjPath = outputBaseName + ".o"; string modelObjPath = outputBaseName + ".o";