-
Notifications
You must be signed in to change notification settings - Fork 325
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Release MLIRContext and Module before executing opt command for memory reduction #3000
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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<ModuleOp> &module, | ||
std::string outputNameNoExt, std::string optimizedBitcodeNameWithExt) { | ||
static int genLLVMBitcode(mlir::OwningOpRef<ModuleOp> &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,14 @@ static int genLLVMBitcode(const mlir::OwningOpRef<ModuleOp> &module, | |
llvm::WriteBitcodeToFile(*llvmModule, moduleBitcodeStream); | ||
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(); | ||
|
||
// Use the LLVM's 'opt' command to optimize the bitcode. | ||
std::string optPath = getToolPath("opt"); | ||
Command optBitcode(/*exePath=*/optPath); | ||
|
@@ -598,10 +607,12 @@ static int genJniJar(const mlir::OwningOpRef<ModuleOp> &module, | |
} | ||
|
||
// Return 0 on success, error code on failure | ||
static int compileModuleToObject(const mlir::OwningOpRef<ModuleOp> &module, | ||
std::string outputNameWithoutExt, std::string &objectNameWithExt) { | ||
static int compileModuleToObject(mlir::OwningOpRef<ModuleOp> &module, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Removing |
||
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 +622,12 @@ static int compileModuleToObject(const mlir::OwningOpRef<ModuleOp> &module, | |
} | ||
|
||
// Return 0 on success, error code on failure | ||
static int compileModuleToSharedLibrary( | ||
const mlir::OwningOpRef<ModuleOp> &module, std::string outputNameNoExt, | ||
std::string &libNameWithExt) { | ||
static int compileModuleToSharedLibrary(mlir::OwningOpRef<ModuleOp> &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 +639,11 @@ static int compileModuleToSharedLibrary( | |
} | ||
|
||
// Return 0 on success, error code on failure | ||
static int compileModuleToJniJar( | ||
const mlir::OwningOpRef<ModuleOp> &module, std::string outputNameNoExt) { | ||
static int compileModuleToJniJar(mlir::OwningOpRef<ModuleOp> &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 +803,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 +819,7 @@ static int emitOutputFiles(std::string outputNameNoExt, | |
case EmitLib: { | ||
std::string sharedLibNameWithExt; | ||
int rc = compileModuleToSharedLibrary( | ||
module, outputNameNoExt, sharedLibNameWithExt); | ||
module, outputNameNoExt, sharedLibNameWithExt, context); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @imaihal just be noticed that |
||
if (rc != CompilerSuccess) | ||
return rc; | ||
if (keepFiles(KeepFilesOfType::MLIR)) { | ||
|
@@ -819,7 +832,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)) { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you know what are freed/released with these statements? It looks not too much memory saved and not sure it is worth to make a change.