From e3d173d34cb43d8a798f0744cce1e474b19b8f5f Mon Sep 17 00:00:00 2001 From: Haruki Imai Date: Tue, 5 Nov 2024 02:09:22 -0500 Subject: [PATCH 1/2] Release modle before opt command. Signed-off-by: Haruki Imai Co-authored-by: Yasushi Negishi --- src/Compiler/CompilerUtils.cpp | 42 +++++++++++++++++++++------------- 1 file changed, 26 insertions(+), 16 deletions(-) diff --git a/src/Compiler/CompilerUtils.cpp b/src/Compiler/CompilerUtils.cpp index 105763107e..72f8476634 100644 --- a/src/Compiler/CompilerUtils.cpp +++ b/src/Compiler/CompilerUtils.cpp @@ -396,8 +396,9 @@ std::string getTargetFilename( // Write LLVM optimized bitcode. // Returns 0 on success, error code on failure. -static int genLLVMBitcode(const mlir::OwningOpRef &module, - std::string outputNameNoExt, std::string optimizedBitcodeNameWithExt) { +static int genLLVMBitcode(mlir::OwningOpRef &module, + std::string outputNameNoExt, std::string optimizedBitcodeNameWithExt, + mlir::MLIRContext &context) { std::string msg = "Translating MLIR Module to LLVM and Generating LLVM Optimized Bitcode"; showCompilePhase(msg); @@ -452,6 +453,11 @@ static int genLLVMBitcode(const mlir::OwningOpRef &module, llvm::WriteBitcodeToFile(*llvmModule, moduleBitcodeStream); moduleBitcodeStream.flush(); + // Free memory before using LLVM `opt` command + llvmModule.reset(); + module.release(); + context.~MLIRContext(); + // Use the LLVM's 'opt' command to optimize the bitcode. std::string optPath = getToolPath("opt"); Command optBitcode(/*exePath=*/optPath); @@ -598,10 +604,12 @@ static int genJniJar(const mlir::OwningOpRef &module, } // Return 0 on success, error code on failure -static int compileModuleToObject(const mlir::OwningOpRef &module, - std::string outputNameWithoutExt, std::string &objectNameWithExt) { +static int compileModuleToObject(mlir::OwningOpRef &module, + std::string outputNameWithoutExt, std::string &objectNameWithExt, + mlir::MLIRContext &context) { std::string bitcodeNameWithExt = outputNameWithoutExt + ".bc"; - int rc = genLLVMBitcode(module, outputNameWithoutExt, bitcodeNameWithExt); + int rc = + genLLVMBitcode(module, outputNameWithoutExt, bitcodeNameWithExt, context); if (rc != CompilerSuccess) return rc; llvm::FileRemover bitcodeRemover( @@ -611,11 +619,12 @@ static int compileModuleToObject(const mlir::OwningOpRef &module, } // Return 0 on success, error code on failure -static int compileModuleToSharedLibrary( - const mlir::OwningOpRef &module, std::string outputNameNoExt, - std::string &libNameWithExt) { +static int compileModuleToSharedLibrary(mlir::OwningOpRef &module, + std::string outputNameNoExt, std::string &libNameWithExt, + mlir::MLIRContext &context) { std::string modelObjNameWithExt; - int rc = compileModuleToObject(module, outputNameNoExt, modelObjNameWithExt); + int rc = compileModuleToObject( + module, outputNameNoExt, modelObjNameWithExt, context); if (rc != CompilerSuccess) return rc; llvm::FileRemover modelObjRemover( @@ -627,10 +636,11 @@ static int compileModuleToSharedLibrary( } // Return 0 on success, error code on failure -static int compileModuleToJniJar( - const mlir::OwningOpRef &module, std::string outputNameNoExt) { +static int compileModuleToJniJar(mlir::OwningOpRef &module, + std::string outputNameNoExt, mlir::MLIRContext &context) { std::string modelObjNameWithExt; - int rc = compileModuleToObject(module, outputNameNoExt, modelObjNameWithExt); + int rc = compileModuleToObject( + module, outputNameNoExt, modelObjNameWithExt, context); if (rc != CompilerSuccess) return rc; llvm::FileRemover modelObjRemover( @@ -790,8 +800,8 @@ static int emitOutputFiles(std::string outputNameNoExt, switch (emissionTarget) { case EmitObj: { std::string modelObjNameWithExt; - int rc = - compileModuleToObject(module, outputNameNoExt, modelObjNameWithExt); + int rc = compileModuleToObject( + module, outputNameNoExt, modelObjNameWithExt, context); if (rc != CompilerSuccess) return rc; if (keepFiles(KeepFilesOfType::MLIR)) { @@ -806,7 +816,7 @@ static int emitOutputFiles(std::string outputNameNoExt, case EmitLib: { std::string sharedLibNameWithExt; int rc = compileModuleToSharedLibrary( - module, outputNameNoExt, sharedLibNameWithExt); + module, outputNameNoExt, sharedLibNameWithExt, context); if (rc != CompilerSuccess) return rc; if (keepFiles(KeepFilesOfType::MLIR)) { @@ -819,7 +829,7 @@ static int emitOutputFiles(std::string outputNameNoExt, << "' has been compiled.\n"; } break; case EmitJNI: { - int rc = compileModuleToJniJar(module, outputNameNoExt); + int rc = compileModuleToJniJar(module, outputNameNoExt, context); if (rc != CompilerSuccess) return rc; if (keepFiles(KeepFilesOfType::MLIR)) { From 2d43a637e272d3c39d61a1922f689784bcb21b2d Mon Sep 17 00:00:00 2001 From: Haruki Imai Date: Thu, 7 Nov 2024 03:53:41 -0500 Subject: [PATCH 2/2] Add comments. Signed-off-by: Haruki Imai --- src/Compiler/CompilerUtils.cpp | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Compiler/CompilerUtils.cpp b/src/Compiler/CompilerUtils.cpp index 96ace2ca6d..78ca19babd 100644 --- a/src/Compiler/CompilerUtils.cpp +++ b/src/Compiler/CompilerUtils.cpp @@ -454,6 +454,9 @@ static int genLLVMBitcode(mlir::OwningOpRef &module, moduleBitcodeStream.flush(); // Free memory before using LLVM `opt` command + // TODO: It might be unexpected that the module is released in this function. + // it is better to restructure functions so that the module is + // automatically released. llvmModule.reset(); module.release(); context.~MLIRContext();