//===----------------------------------------------------------------------===//
#include "llvm/ADT/StringSet.h"
+#include "llvm/Bitcode/ReaderWriter.h"
#include "llvm/CodeGen/CommandFlags.h"
+#include "llvm/IR/DiagnosticPrinter.h"
+#include "llvm/IR/LLVMContext.h"
#include "llvm/LTO/LTOCodeGenerator.h"
#include "llvm/LTO/LTOModule.h"
+#include "llvm/Object/FunctionIndexObjectFile.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/ManagedStatic.h"
UseDiagnosticHandler("use-diagnostic-handler", cl::init(false),
cl::desc("Use a diagnostic handler to test the handler interface"));
+static cl::opt<bool>
+ ThinLTO("thinlto", cl::init(false),
+ cl::desc("Only write combined global index for ThinLTO backends"));
+
+static cl::opt<bool>
+SaveModuleFile("save-merged-module", cl::init(false),
+ cl::desc("Write merged LTO module to file before CodeGen"));
+
static cl::list<std::string>
InputFilenames(cl::Positional, cl::OneOrMore,
cl::desc("<input bitcode files>"));
static void handleDiagnostics(lto_codegen_diagnostic_severity_t Severity,
const char *Msg, void *) {
+ errs() << "llvm-lto: ";
switch (Severity) {
case LTO_DS_NOTE:
errs() << "note: ";
errs() << Msg << "\n";
}
+static std::string CurrentActivity;
+static void diagnosticHandler(const DiagnosticInfo &DI) {
+ raw_ostream &OS = errs();
+ OS << "llvm-lto: ";
+ switch (DI.getSeverity()) {
+ case DS_Error:
+ OS << "error";
+ break;
+ case DS_Warning:
+ OS << "warning";
+ break;
+ case DS_Remark:
+ OS << "remark";
+ break;
+ case DS_Note:
+ OS << "note";
+ break;
+ }
+ if (!CurrentActivity.empty())
+ OS << ' ' << CurrentActivity;
+ OS << ": ";
+
+ DiagnosticPrinterRawOStream DP(OS);
+ DI.print(DP);
+ OS << '\n';
+
+ if (DI.getSeverity() == DS_Error)
+ exit(1);
+}
+
+static void diagnosticHandlerWithContenxt(const DiagnosticInfo &DI,
+ void *Context) {
+ diagnosticHandler(DI);
+}
+
+static void error(const Twine &Msg) {
+ errs() << "llvm-lto: " << Msg << '\n';
+ exit(1);
+}
+
+static void error(std::error_code EC, const Twine &Prefix) {
+ if (EC)
+ error(Prefix + ": " + EC.message());
+}
+
+template <typename T>
+static void error(const ErrorOr<T> &V, const Twine &Prefix) {
+ error(V.getError(), Prefix);
+}
+
static std::unique_ptr<LTOModule>
getLocalLTOModule(StringRef Path, std::unique_ptr<MemoryBuffer> &Buffer,
- const TargetOptions &Options, std::string &Error) {
+ const TargetOptions &Options) {
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFile(Path);
- if (std::error_code EC = BufferOrErr.getError()) {
- Error = EC.message();
- return nullptr;
- }
+ error(BufferOrErr, "error loading file '" + Path + "'");
Buffer = std::move(BufferOrErr.get());
- return std::unique_ptr<LTOModule>(LTOModule::createInLocalContext(
- Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Error, Path));
+ CurrentActivity = ("loading file '" + Path + "'").str();
+ ErrorOr<std::unique_ptr<LTOModule>> Ret = LTOModule::createInLocalContext(
+ Buffer->getBufferStart(), Buffer->getBufferSize(), Options, Path);
+ CurrentActivity = "";
+ return std::move(*Ret);
}
/// \brief List symbols in each IR file.
/// functionality that's exposed by the C API to list symbols. Moreover, this
/// provides testing coverage for modules that have been created in their own
/// contexts.
-static int listSymbols(StringRef Command, const TargetOptions &Options) {
+static void listSymbols(const TargetOptions &Options) {
for (auto &Filename : InputFilenames) {
- std::string Error;
std::unique_ptr<MemoryBuffer> Buffer;
std::unique_ptr<LTOModule> Module =
- getLocalLTOModule(Filename, Buffer, Options, Error);
- if (!Module) {
- errs() << Command << ": error loading file '" << Filename
- << "': " << Error << "\n";
- return 1;
- }
+ getLocalLTOModule(Filename, Buffer, Options);
// List the symbols.
outs() << Filename << ":\n";
for (int I = 0, E = Module->getSymbolCount(); I != E; ++I)
outs() << Module->getSymbolName(I) << "\n";
}
- return 0;
+}
+
+/// Create a combined index file from the input IR files and write it.
+///
+/// This is meant to enable testing of ThinLTO combined index generation,
+/// currently available via the gold plugin via -thinlto.
+static void createCombinedFunctionIndex() {
+ FunctionInfoIndex CombinedIndex;
+ uint64_t NextModuleId = 0;
+ for (auto &Filename : InputFilenames) {
+ CurrentActivity = "loading file '" + Filename + "'";
+ ErrorOr<std::unique_ptr<FunctionInfoIndex>> IndexOrErr =
+ llvm::getFunctionIndexForFile(Filename, diagnosticHandler);
+ std::unique_ptr<FunctionInfoIndex> Index = std::move(IndexOrErr.get());
+ CurrentActivity = "";
+ // Skip files without a function summary.
+ if (!Index)
+ continue;
+ CombinedIndex.mergeFrom(std::move(Index), ++NextModuleId);
+ }
+ std::error_code EC;
+ assert(!OutputFilename.empty());
+ raw_fd_ostream OS(OutputFilename + ".thinlto.bc", EC,
+ sys::fs::OpenFlags::F_None);
+ error(EC, "error opening the file '" + OutputFilename + ".thinlto.bc'");
+ WriteFunctionSummaryToFile(CombinedIndex, OS);
+ OS.close();
}
int main(int argc, char **argv) {
llvm_shutdown_obj Y; // Call llvm_shutdown() on exit.
cl::ParseCommandLineOptions(argc, argv, "llvm LTO linker\n");
- if (OptLevel < '0' || OptLevel > '3') {
- errs() << argv[0] << ": optimization level must be between 0 and 3\n";
- return 1;
- }
+ if (OptLevel < '0' || OptLevel > '3')
+ error("optimization level must be between 0 and 3");
// Initialize the configured targets.
InitializeAllTargets();
// set up the TargetOptions for the machine
TargetOptions Options = InitTargetOptionsFromCodeGenFlags();
- if (ListSymbolsOnly)
- return listSymbols(argv[0], Options);
+ if (ListSymbolsOnly) {
+ listSymbols(Options);
+ return 0;
+ }
+
+ if (ThinLTO) {
+ createCombinedFunctionIndex();
+ return 0;
+ }
unsigned BaseArg = 0;
- LTOCodeGenerator CodeGen;
+ LLVMContext Context;
+ Context.setDiagnosticHandler(diagnosticHandlerWithContenxt, nullptr, true);
+
+ LTOCodeGenerator CodeGen(Context);
if (UseDiagnosticHandler)
CodeGen.setDiagnosticHandler(handleDiagnostics, nullptr);
std::vector<std::string> KeptDSOSyms;
for (unsigned i = BaseArg; i < InputFilenames.size(); ++i) {
- std::string error;
- std::unique_ptr<LTOModule> Module(
- LTOModule::createFromFile(InputFilenames[i].c_str(), Options, error));
- if (!error.empty()) {
- errs() << argv[0] << ": error loading file '" << InputFilenames[i]
- << "': " << error << "\n";
- return 1;
- }
+ CurrentActivity = "loading file '" + InputFilenames[i] + "'";
+ ErrorOr<std::unique_ptr<LTOModule>> ModuleOrErr =
+ LTOModule::createFromFile(Context, InputFilenames[i].c_str(), Options);
+ error(ModuleOrErr, "error " + CurrentActivity);
+ std::unique_ptr<LTOModule> &Module = *ModuleOrErr;
+ CurrentActivity = "";
unsigned NumSyms = Module->getSymbolCount();
for (unsigned I = 0; I < NumSyms; ++I) {
if (!attrs.empty())
CodeGen.setAttr(attrs.c_str());
+ if (FileType.getNumOccurrences())
+ CodeGen.setFileType(FileType);
+
if (!OutputFilename.empty()) {
- std::string ErrorInfo;
if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
- DisableLTOVectorization, ErrorInfo)) {
- errs() << argv[0] << ": error optimizing the code: " << ErrorInfo << "\n";
+ DisableLTOVectorization)) {
+ // Diagnostic messages should have been printed by the handler.
+ errs() << argv[0] << ": error optimizing the code\n";
return 1;
}
+ if (SaveModuleFile) {
+ std::string ModuleFilename = OutputFilename;
+ ModuleFilename += ".merged.bc";
+ std::string ErrMsg;
+
+ if (!CodeGen.writeMergedModules(ModuleFilename.c_str())) {
+ errs() << argv[0] << ": writing merged module failed.\n";
+ return 1;
+ }
+ }
+
std::list<tool_output_file> OSs;
std::vector<raw_pwrite_stream *> OSPtrs;
for (unsigned I = 0; I != Parallelism; ++I) {
OSPtrs.push_back(&OSs.back().os());
}
- if (!CodeGen.compileOptimized(OSPtrs, ErrorInfo)) {
- errs() << argv[0] << ": error compiling the code: " << ErrorInfo << "\n";
+ if (!CodeGen.compileOptimized(OSPtrs)) {
+ // Diagnostic messages should have been printed by the handler.
+ errs() << argv[0] << ": error compiling the code\n";
return 1;
}
return 1;
}
- std::string ErrorInfo;
+ if (SaveModuleFile) {
+ errs() << argv[0] << ": -save-merged-module must be specified with -o\n";
+ return 1;
+ }
+
const char *OutputName = nullptr;
if (!CodeGen.compile_to_file(&OutputName, DisableVerify, DisableInline,
- DisableGVNLoadPRE, DisableLTOVectorization,
- ErrorInfo)) {
- errs() << argv[0]
- << ": error compiling the code: " << ErrorInfo
- << "\n";
+ DisableGVNLoadPRE, DisableLTOVectorization)) {
+ // Diagnostic messages should have been printed by the handler.
+ errs() << argv[0] << ": error compiling the code\n";
return 1;
}