diff options
author | Rafael Espindola <rafael.espindola@gmail.com> | 2011-03-22 20:57:13 +0000 |
---|---|---|
committer | Rafael Espindola <rafael.espindola@gmail.com> | 2011-03-22 20:57:13 +0000 |
commit | 6421a8815e14189121a5e20731fd005ea08793e1 (patch) | |
tree | 9bac7d62f3d88e0b1902b72db8b951782135aabe /tools/lto | |
parent | 97ab5803df4a549db9cd93e80426971e64562672 (diff) | |
download | external_llvm-6421a8815e14189121a5e20731fd005ea08793e1.zip external_llvm-6421a8815e14189121a5e20731fd005ea08793e1.tar.gz external_llvm-6421a8815e14189121a5e20731fd005ea08793e1.tar.bz2 |
Add a lto_codegen_compile_to_file to avoid producing a file, reading it to
memory and writing it back to disk.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@128108 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/lto')
-rw-r--r-- | tools/lto/LTOCodeGenerator.cpp | 95 | ||||
-rw-r--r-- | tools/lto/LTOCodeGenerator.h | 2 | ||||
-rw-r--r-- | tools/lto/lto.cpp | 6 | ||||
-rw-r--r-- | tools/lto/lto.exports | 1 |
4 files changed, 61 insertions, 43 deletions
diff --git a/tools/lto/LTOCodeGenerator.cpp b/tools/lto/LTOCodeGenerator.cpp index 372cb31..d95f354 100644 --- a/tools/lto/LTOCodeGenerator.cpp +++ b/tools/lto/LTOCodeGenerator.cpp @@ -176,54 +176,63 @@ bool LTOCodeGenerator::writeMergedModules(const char *path, } -const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) +bool LTOCodeGenerator::compile_to_file(const char** name, std::string& errMsg) { - // make unique temp .o file to put generated object file - sys::PathWithStatus uniqueObjPath("lto-llvm.o"); - if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) { - uniqueObjPath.eraseFromDisk(); - return NULL; - } - sys::RemoveFileOnSignal(uniqueObjPath); - - // generate object file - bool genResult = false; - tool_output_file objFile(uniqueObjPath.c_str(), errMsg); - if (!errMsg.empty()) - return NULL; - genResult = this->generateObjectFile(objFile.os(), errMsg); - objFile.os().close(); - if (objFile.os().has_error()) { - objFile.os().clear_error(); - return NULL; - } - objFile.keep(); - if ( genResult ) { - uniqueObjPath.eraseFromDisk(); - return NULL; - } + // make unique temp .o file to put generated object file + sys::PathWithStatus uniqueObjPath("lto-llvm.o"); + if ( uniqueObjPath.createTemporaryFileOnDisk(false, &errMsg) ) { + uniqueObjPath.eraseFromDisk(); + return true; + } + sys::RemoveFileOnSignal(uniqueObjPath); + + // generate object file + bool genResult = false; + tool_output_file objFile(uniqueObjPath.c_str(), errMsg); + if (!errMsg.empty()) + return NULL; + genResult = this->generateObjectFile(objFile.os(), errMsg); + objFile.os().close(); + if (objFile.os().has_error()) { + objFile.os().clear_error(); + return true; + } + objFile.keep(); + if ( genResult ) { + uniqueObjPath.eraseFromDisk(); + return true; + } - const std::string& uniqueObjStr = uniqueObjPath.str(); - // remove old buffer if compile() called twice - delete _nativeObjectFile; + _nativeObjectPath = uniqueObjPath.str(); + *name = _nativeObjectPath.c_str(); + return false; +} - // read .o file into memory buffer - OwningPtr<MemoryBuffer> BuffPtr; - if (error_code ec = MemoryBuffer::getFile(uniqueObjStr.c_str(), BuffPtr, - -1, false)) { - errMsg = ec.message(); - return NULL; - } - _nativeObjectFile = BuffPtr.take(); +const void* LTOCodeGenerator::compile(size_t* length, std::string& errMsg) +{ + const char *name; + if (compile_to_file(&name, errMsg)) + return NULL; + + // remove old buffer if compile() called twice + delete _nativeObjectFile; + + // read .o file into memory buffer + OwningPtr<MemoryBuffer> BuffPtr; + if (error_code ec = MemoryBuffer::getFile(name, BuffPtr, -1, false)) { + errMsg = ec.message(); + return NULL; + } + _nativeObjectFile = BuffPtr.take(); - // remove temp files - uniqueObjPath.eraseFromDisk(); + // remove temp files + sys::Path(_nativeObjectPath).eraseFromDisk(); - // return buffer, unless error - if ( _nativeObjectFile == NULL ) - return NULL; - *length = _nativeObjectFile->getBufferSize(); - return _nativeObjectFile->getBufferStart(); + // return buffer, unless error + if ( _nativeObjectFile == NULL ) + return NULL; + *length = _nativeObjectFile->getBufferSize(); + return _nativeObjectFile->getBufferStart(); } bool LTOCodeGenerator::determineTarget(std::string& errMsg) diff --git a/tools/lto/LTOCodeGenerator.h b/tools/lto/LTOCodeGenerator.h index 7798db9..f8fd357 100644 --- a/tools/lto/LTOCodeGenerator.h +++ b/tools/lto/LTOCodeGenerator.h @@ -41,6 +41,7 @@ struct LTOCodeGenerator { void addMustPreserveSymbol(const char* sym); bool writeMergedModules(const char* path, std::string& errMsg); + bool compile_to_file(const char** name, std::string& errMsg); const void* compile(size_t* length, std::string& errMsg); void setCodeGenDebugOptions(const char *opts); private: @@ -66,6 +67,7 @@ private: llvm::MemoryBuffer* _nativeObjectFile; std::vector<const char*> _codegenOptions; std::string _mCpu; + std::string _nativeObjectPath; }; #endif // LTO_CODE_GENERATOR_H diff --git a/tools/lto/lto.cpp b/tools/lto/lto.cpp index cbac047..fe19921 100644 --- a/tools/lto/lto.cpp +++ b/tools/lto/lto.cpp @@ -293,6 +293,12 @@ lto_codegen_compile(lto_code_gen_t cg, size_t* length) return cg->compile(length, sLastErrorString); } +extern bool +lto_codegen_compile_to_file(lto_code_gen_t cg, const char **name) +{ + return cg->compile_to_file(name, sLastErrorString); +} + // // Used to pass extra options to the code generator diff --git a/tools/lto/lto.exports b/tools/lto/lto.exports index 04b37e1..0fcfee5 100644 --- a/tools/lto/lto.exports +++ b/tools/lto/lto.exports @@ -26,3 +26,4 @@ lto_codegen_debug_options lto_codegen_set_assembler_args lto_codegen_set_assembler_path lto_codegen_set_cpu +lto_codegen_compile_to_file |