summaryrefslogtreecommitdiff
path: root/tools/llvm-lto
diff options
context:
space:
mode:
authorTeresa Johnson <tejohnson@google.com>2016-05-17 14:45:30 +0000
committerTeresa Johnson <tejohnson@google.com>2016-05-17 14:45:30 +0000
commita48638166058939a6d7ff5cccab122b6c0e450af (patch)
tree4f0d261f57146e158fac99909473e0f95d4be0ed /tools/llvm-lto
parent7537c45fbdb56cc0ef9f9319f039b8eda867ff2d (diff)
[ThinLTO] Option to control path of distributed backend files
Summary: Add support to control where files for a distributed backend (the individual index files and optional imports files) are created. This is invoked with a new thinlto-prefix-replace option in the gold plugin and llvm-lto. If specified, expects a string of the form "oldprefix:newprefix", and instead of generating these files in the same directory path as the corresponding bitcode file, will use a path formed by replacing the bitcode file's path prefix matching oldprefix with newprefix. Also add a new replace_path_prefix helper to Path.h in libSupport. Depends on D19636. Reviewers: joker.eph Subscribers: llvm-commits, joker.eph Differential Revision: http://reviews.llvm.org/D19644 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@269771 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'tools/llvm-lto')
-rw-r--r--tools/llvm-lto/llvm-lto.cpp47
1 files changed, 47 insertions, 0 deletions
diff --git a/tools/llvm-lto/llvm-lto.cpp b/tools/llvm-lto/llvm-lto.cpp
index 51767977dd0..fe3f1b7de91 100644
--- a/tools/llvm-lto/llvm-lto.cpp
+++ b/tools/llvm-lto/llvm-lto.cpp
@@ -26,6 +26,7 @@
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
+#include "llvm/Support/Path.h"
#include "llvm/Support/PrettyStackTrace.h"
#include "llvm/Support/Signals.h"
#include "llvm/Support/SourceMgr.h"
@@ -104,6 +105,13 @@ static cl::opt<std::string>
cl::desc("Provide the index produced by a ThinLink, required "
"to perform the promotion and/or importing."));
+static cl::opt<std::string> ThinLTOPrefixReplace(
+ "thinlto-prefix-replace",
+ cl::desc("Control where files for distributed backends are "
+ "created. Expects 'oldprefix:newprefix' and if path "
+ "prefix of output file is oldprefix it will be "
+ "replaced with newprefix."));
+
static cl::opt<std::string> ThinLTOModuleId(
"thinlto-module-id",
cl::desc("For the module ID for the file to process, useful to "
@@ -294,6 +302,37 @@ static void createCombinedModuleSummaryIndex() {
OS.close();
}
+/// Parse the thinlto_prefix_replace option into the \p OldPrefix and
+/// \p NewPrefix strings, if it was specified.
+static void getThinLTOOldAndNewPrefix(std::string &OldPrefix,
+ std::string &NewPrefix) {
+ assert(ThinLTOPrefixReplace.empty() ||
+ ThinLTOPrefixReplace.find(":") != StringRef::npos);
+ StringRef PrefixReplace = ThinLTOPrefixReplace;
+ std::pair<StringRef, StringRef> Split = PrefixReplace.split(":");
+ OldPrefix = Split.first.str();
+ NewPrefix = Split.second.str();
+}
+
+/// Given the original \p Path to an output file, replace any path
+/// prefix matching \p OldPrefix with \p NewPrefix. Also, create the
+/// resulting directory if it does not yet exist.
+static std::string getThinLTOOutputFile(const std::string &Path,
+ const std::string &OldPrefix,
+ const std::string &NewPrefix) {
+ if (OldPrefix.empty() && NewPrefix.empty())
+ return Path;
+ SmallString<128> NewPath(Path);
+ llvm::sys::path::replace_path_prefix(NewPath, OldPrefix, NewPrefix);
+ StringRef ParentPath = llvm::sys::path::parent_path(NewPath.str());
+ if (!ParentPath.empty()) {
+ // Make sure the new directory exists, creating it if necessary.
+ if (std::error_code EC = llvm::sys::fs::create_directories(ParentPath))
+ error(EC, "error creating the directory '" + ParentPath + "'");
+ }
+ return NewPath.str();
+}
+
namespace thinlto {
std::vector<std::unique_ptr<MemoryBuffer>>
@@ -421,6 +460,9 @@ private:
"the output files will be suffixed from the input "
"ones.");
+ std::string OldPrefix, NewPrefix;
+ getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
+
auto Index = loadCombinedIndex();
for (auto &Filename : InputFilenames) {
// Build a map of module to the GUIDs and summary objects that should
@@ -433,6 +475,7 @@ private:
if (OutputName.empty()) {
OutputName = Filename + ".thinlto.bc";
}
+ OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix);
std::error_code EC;
raw_fd_ostream OS(OutputName, EC, sys::fs::OpenFlags::F_None);
error(EC, "error opening the file '" + OutputName + "'");
@@ -449,12 +492,16 @@ private:
"the output files will be suffixed from the input "
"ones.");
+ std::string OldPrefix, NewPrefix;
+ getThinLTOOldAndNewPrefix(OldPrefix, NewPrefix);
+
auto Index = loadCombinedIndex();
for (auto &Filename : InputFilenames) {
std::string OutputName = OutputFilename;
if (OutputName.empty()) {
OutputName = Filename + ".imports";
}
+ OutputName = getThinLTOOutputFile(OutputName, OldPrefix, NewPrefix);
ThinLTOCodeGenerator::emitImports(Filename, OutputName, *Index);
}
}