From: Teresa Johnson Date: Mon, 23 Nov 2015 19:19:11 +0000 (+0000) Subject: [ThinLTO] Deduplicate function index loading into shared helper (NFC) X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;h=846661215edff55c46f0c3fad192289936e16e41;p=oota-llvm.git [ThinLTO] Deduplicate function index loading into shared helper (NFC) Add a shared helper routine to read the function index from a file and create/return the function index object. Use it in llvm-link and llvm-lto. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253903 91177308-0d34-0410-b5e6-96231b3b80d8 --- diff --git a/include/llvm/Object/FunctionIndexObjectFile.h b/include/llvm/Object/FunctionIndexObjectFile.h index 92b670db49a..511a237881e 100644 --- a/include/llvm/Object/FunctionIndexObjectFile.h +++ b/include/llvm/Object/FunctionIndexObjectFile.h @@ -99,6 +99,13 @@ public: StringRef FunctionName); }; } + +/// Parse the function index out of an IR file and return the function +/// index object if found, or nullptr if not. +ErrorOr> +getFunctionIndexForFile(StringRef Path, + DiagnosticHandlerFunction DiagnosticHandler, + const Module *ExportingModule = nullptr); } #endif diff --git a/lib/Object/FunctionIndexObjectFile.cpp b/lib/Object/FunctionIndexObjectFile.cpp index f4b2b5562a3..717c56bc901 100644 --- a/lib/Object/FunctionIndexObjectFile.cpp +++ b/lib/Object/FunctionIndexObjectFile.cpp @@ -120,3 +120,26 @@ std::error_code FunctionIndexObjectFile::findFunctionSummaryInMemBuffer( return object_error::invalid_file_type; } } + +// Parse the function index out of an IR file and return the function +// index object if found, or nullptr if not. +ErrorOr> +llvm::getFunctionIndexForFile(StringRef Path, + DiagnosticHandlerFunction DiagnosticHandler, + const Module *ExportingModule) { + ErrorOr> FileOrErr = + MemoryBuffer::getFileOrSTDIN(Path); + std::error_code EC = FileOrErr.getError(); + if (EC) + return EC; + MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef(); + ErrorOr> ObjOrErr = + object::FunctionIndexObjectFile::create(BufferRef, DiagnosticHandler, + ExportingModule); + EC = ObjOrErr.getError(); + if (EC) + return EC; + + object::FunctionIndexObjectFile &Obj = **ObjOrErr; + return Obj.takeIndex(); +} diff --git a/tools/llvm-link/llvm-link.cpp b/tools/llvm-link/llvm-link.cpp index 9291e9b5862..984dfdcf70e 100644 --- a/tools/llvm-link/llvm-link.cpp +++ b/tools/llvm-link/llvm-link.cpp @@ -141,27 +141,6 @@ static void diagnosticHandler(const DiagnosticInfo &DI) { errs() << '\n'; } -/// Load a function index if requested by the -functionindex option. -static ErrorOr> -loadIndex(const Module *ExportingModule = nullptr) { - assert(!FunctionIndex.empty()); - ErrorOr> FileOrErr = - MemoryBuffer::getFileOrSTDIN(FunctionIndex); - std::error_code EC = FileOrErr.getError(); - if (EC) - return EC; - MemoryBufferRef BufferRef = (FileOrErr.get())->getMemBufferRef(); - ErrorOr> ObjOrErr = - object::FunctionIndexObjectFile::create(BufferRef, diagnosticHandler, - ExportingModule); - EC = ObjOrErr.getError(); - if (EC) - return EC; - - object::FunctionIndexObjectFile &Obj = **ObjOrErr; - return Obj.takeIndex(); -} - /// Import any functions requested via the -import option. static bool importFunctions(const char *argv0, LLVMContext &Context, Linker &L) { @@ -208,7 +187,8 @@ static bool importFunctions(const char *argv0, LLVMContext &Context, std::unique_ptr Index; if (!FunctionIndex.empty()) { - ErrorOr> IndexOrErr = loadIndex(); + ErrorOr> IndexOrErr = + llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler); std::error_code EC = IndexOrErr.getError(); if (EC) { errs() << EC.message() << '\n'; @@ -245,7 +225,8 @@ static bool linkFiles(const char *argv0, LLVMContext &Context, Linker &L, // local functions/variables as exported and promote if necessary. std::unique_ptr Index; if (!FunctionIndex.empty()) { - ErrorOr> IndexOrErr = loadIndex(&*M); + ErrorOr> IndexOrErr = + llvm::getFunctionIndexForFile(FunctionIndex, diagnosticHandler, &*M); std::error_code EC = IndexOrErr.getError(); if (EC) { errs() << EC.message() << '\n'; diff --git a/tools/llvm-lto/llvm-lto.cpp b/tools/llvm-lto/llvm-lto.cpp index e580a5df578..64e0ae31d06 100644 --- a/tools/llvm-lto/llvm-lto.cpp +++ b/tools/llvm-lto/llvm-lto.cpp @@ -190,32 +190,6 @@ static int listSymbols(StringRef Command, const TargetOptions &Options) { return 0; } -/// Parse the function index out of an IR file and return the function -/// index object if found, or nullptr if not. -static ErrorOr> -getFunctionIndexForFile(StringRef Path, - DiagnosticHandlerFunction DiagnosticHandler) { - std::unique_ptr Buffer; - ErrorOr> BufferOrErr = - MemoryBuffer::getFile(Path); - if (std::error_code EC = BufferOrErr.getError()) - return EC; - Buffer = std::move(BufferOrErr.get()); - - // Don't bother trying to build an index if there is no summary information - // in this bitcode file. - if (!object::FunctionIndexObjectFile::hasFunctionSummaryInMemBuffer( - Buffer->getMemBufferRef(), DiagnosticHandler)) - return std::unique_ptr(nullptr); - - ErrorOr> ObjOrErr = - object::FunctionIndexObjectFile::create(Buffer->getMemBufferRef(), - DiagnosticHandler); - if (std::error_code EC = ObjOrErr.getError()) - return EC; - return (*ObjOrErr)->takeIndex(); -} - /// Create a combined index file from the input IR files and write it. /// /// This is meant to enable testing of ThinLTO combined index generation, @@ -225,7 +199,7 @@ static int createCombinedFunctionIndex(StringRef Command) { uint64_t NextModuleId = 0; for (auto &Filename : InputFilenames) { ErrorOr> IndexOrErr = - getFunctionIndexForFile(Filename, diagnosticHandler); + llvm::getFunctionIndexForFile(Filename, diagnosticHandler); if (std::error_code EC = IndexOrErr.getError()) { std::string Error = EC.message(); errs() << Command << ": error loading file '" << Filename