#endif
}
-LTOCodeGenerator::LTOCodeGenerator()
- : Context(getGlobalContext()),
- MergedModule(new Module("ld-temp.o", Context)),
- IRLinker(MergedModule.get()) {
- initializeLTOPasses();
-}
-
-LTOCodeGenerator::LTOCodeGenerator(std::unique_ptr<LLVMContext> Context)
- : OwnedContext(std::move(Context)), Context(*OwnedContext),
- MergedModule(new Module("ld-temp.o", *OwnedContext)),
- IRLinker(MergedModule.get()) {
+LTOCodeGenerator::LTOCodeGenerator(LLVMContext &Context)
+ : Context(Context), MergedModule(new Module("ld-temp.o", Context)),
+ IRLinker(new Linker(*MergedModule)) {
initializeLTOPasses();
}
LTOCodeGenerator::~LTOCodeGenerator() {}
-// Initialize LTO passes. Please keep this funciton in sync with
+// Initialize LTO passes. Please keep this function in sync with
// PassManagerBuilder::populateLTOPassManager(), and make sure all LTO
// passes are initialized.
void LTOCodeGenerator::initializeLTOPasses() {
initializeGlobalDCEPass(R);
initializeArgPromotionPass(R);
initializeJumpThreadingPass(R);
- initializeSROAPass(R);
+ initializeSROALegacyPassPass(R);
initializeSROA_DTPass(R);
initializeSROA_SSAUpPass(R);
initializeFunctionAttrsPass(R);
- initializeGlobalsModRefPass(R);
+ initializeGlobalsAAWrapperPassPass(R);
initializeLICMPass(R);
initializeMergedLoadStoreMotionPass(R);
initializeGVNPass(R);
initializeCFGSimplifyPassPass(R);
}
-bool LTOCodeGenerator::addModule(LTOModule *mod) {
- assert(&mod->getModule().getContext() == &Context &&
+bool LTOCodeGenerator::addModule(LTOModule *Mod) {
+ assert(&Mod->getModule().getContext() == &Context &&
"Expected module in same context");
- bool ret = IRLinker.linkInModule(&mod->getModule());
+ bool ret = IRLinker->linkInModule(Mod->getModule());
- const std::vector<const char*> &undefs = mod->getAsmUndefinedRefs();
+ const std::vector<const char *> &undefs = Mod->getAsmUndefinedRefs();
for (int i = 0, e = undefs.size(); i != e; ++i)
AsmUndefinedRefs[undefs[i]] = 1;
AsmUndefinedRefs.clear();
MergedModule = Mod->takeModule();
- IRLinker.setModule(MergedModule.get());
+ IRLinker = make_unique<Linker>(*MergedModule);
const std::vector<const char*> &Undefs = Mod->getAsmUndefinedRefs();
for (int I = 0, E = Undefs.size(); I != E; ++I)
AsmUndefinedRefs[Undefs[I]] = 1;
}
-void LTOCodeGenerator::setTargetOptions(TargetOptions options) {
- Options = options;
+void LTOCodeGenerator::setTargetOptions(TargetOptions Options) {
+ this->Options = Options;
}
-void LTOCodeGenerator::setDebugInfo(lto_debug_model debug) {
- switch (debug) {
+void LTOCodeGenerator::setDebugInfo(lto_debug_model Debug) {
+ switch (Debug) {
case LTO_DEBUG_MODEL_NONE:
EmitDwarfDebugInfo = false;
return;
llvm_unreachable("Unknown debug format!");
}
-void LTOCodeGenerator::setOptLevel(unsigned level) {
- OptLevel = level;
+void LTOCodeGenerator::setOptLevel(unsigned Level) {
+ OptLevel = Level;
switch (OptLevel) {
case 0:
CGOptLevel = CodeGenOpt::None;
}
}
-bool LTOCodeGenerator::writeMergedModules(const char *path,
- std::string &errMsg) {
- if (!determineTarget(errMsg))
+bool LTOCodeGenerator::writeMergedModules(const char *Path) {
+ if (!determineTarget())
return false;
// mark which symbols can not be internalized
// create output file
std::error_code EC;
- tool_output_file Out(path, EC, sys::fs::F_None);
+ tool_output_file Out(Path, EC, sys::fs::F_None);
if (EC) {
- errMsg = "could not open bitcode file for writing: ";
- errMsg += path;
+ std::string ErrMsg = "could not open bitcode file for writing: ";
+ ErrMsg += Path;
+ emitError(ErrMsg);
return false;
}
Out.os().close();
if (Out.os().has_error()) {
- errMsg = "could not write bitcode file: ";
- errMsg += path;
+ std::string ErrMsg = "could not write bitcode file: ";
+ ErrMsg += Path;
+ emitError(ErrMsg);
Out.os().clear_error();
return false;
}
return true;
}
-bool LTOCodeGenerator::compileOptimizedToFile(const char **name,
- std::string &errMsg) {
- // make unique temp .o file to put generated object file
+bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
+ // make unique temp output file to put generated code
SmallString<128> Filename;
int FD;
+
+ const char *Extension =
+ (FileType == TargetMachine::CGFT_AssemblyFile ? "s" : "o");
+
std::error_code EC =
- sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
+ sys::fs::createTemporaryFile("lto-llvm", Extension, FD, Filename);
if (EC) {
- errMsg = EC.message();
+ emitError(EC.message());
return false;
}
// generate object file
tool_output_file objFile(Filename.c_str(), FD);
- bool genResult = compileOptimized(&objFile.os(), errMsg);
+ bool genResult = compileOptimized(&objFile.os());
objFile.os().close();
if (objFile.os().has_error()) {
objFile.os().clear_error();
}
NativeObjectPath = Filename.c_str();
- *name = NativeObjectPath.c_str();
+ *Name = NativeObjectPath.c_str();
return true;
}
std::unique_ptr<MemoryBuffer>
-LTOCodeGenerator::compileOptimized(std::string &errMsg) {
+LTOCodeGenerator::compileOptimized() {
const char *name;
- if (!compileOptimizedToFile(&name, errMsg))
+ if (!compileOptimizedToFile(&name))
return nullptr;
// read .o file into memory buffer
ErrorOr<std::unique_ptr<MemoryBuffer>> BufferOrErr =
MemoryBuffer::getFile(name, -1, false);
if (std::error_code EC = BufferOrErr.getError()) {
- errMsg = EC.message();
+ emitError(EC.message());
sys::fs::remove(NativeObjectPath);
return nullptr;
}
return std::move(*BufferOrErr);
}
-
-bool LTOCodeGenerator::compile_to_file(const char **name,
- bool disableInline,
- bool disableGVNLoadPRE,
- bool disableVectorization,
- std::string &errMsg) {
- if (!optimize(disableInline, disableGVNLoadPRE,
- disableVectorization, errMsg))
+bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
+ bool DisableInline,
+ bool DisableGVNLoadPRE,
+ bool DisableVectorization) {
+ if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
+ DisableVectorization))
return false;
- return compileOptimizedToFile(name, errMsg);
+ return compileOptimizedToFile(Name);
}
std::unique_ptr<MemoryBuffer>
-LTOCodeGenerator::compile(bool disableInline, bool disableGVNLoadPRE,
- bool disableVectorization, std::string &errMsg) {
- if (!optimize(disableInline, disableGVNLoadPRE,
- disableVectorization, errMsg))
+LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
+ bool DisableGVNLoadPRE, bool DisableVectorization) {
+ if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
+ DisableVectorization))
return nullptr;
- return compileOptimized(errMsg);
+ return compileOptimized();
}
-bool LTOCodeGenerator::determineTarget(std::string &errMsg) {
+bool LTOCodeGenerator::determineTarget() {
if (TargetMach)
return true;
llvm::Triple Triple(TripleStr);
// create target machine from info for merged modules
- const Target *march = TargetRegistry::lookupTarget(TripleStr, errMsg);
- if (!march)
+ std::string ErrMsg;
+ const Target *march = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
+ if (!march) {
+ emitError(ErrMsg);
return false;
+ }
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
// the default set of features.
}
/// Optimize merged modules using various IPO passes
-bool LTOCodeGenerator::optimize(bool DisableInline,
+bool LTOCodeGenerator::optimize(bool DisableVerify, bool DisableInline,
bool DisableGVNLoadPRE,
- bool DisableVectorization,
- std::string &errMsg) {
- if (!this->determineTarget(errMsg))
+ bool DisableVectorization) {
+ if (!this->determineTarget())
return false;
// Mark which symbols can not be internalized
PMB.Inliner = createFunctionInliningPass();
PMB.LibraryInfo = new TargetLibraryInfoImpl(TargetTriple);
PMB.OptLevel = OptLevel;
- PMB.VerifyInput = true;
- PMB.VerifyOutput = true;
+ PMB.VerifyInput = !DisableVerify;
+ PMB.VerifyOutput = !DisableVerify;
PMB.populateLTOPassManager(passes);
return true;
}
-bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> out,
- std::string &errMsg) {
- if (!this->determineTarget(errMsg))
+bool LTOCodeGenerator::compileOptimized(ArrayRef<raw_pwrite_stream *> Out) {
+ if (!this->determineTarget())
return false;
legacy::PassManager preCodeGenPasses;
// original module at parallelism level 1 which we then assign back to
// MergedModule.
MergedModule =
- splitCodeGen(std::move(MergedModule), out, MCpu, FeatureStr, Options,
- RelocModel, CodeModel::Default, CGOptLevel);
+ splitCodeGen(std::move(MergedModule), Out, MCpu, FeatureStr, Options,
+ RelocModel, CodeModel::Default, CGOptLevel, FileType);
return true;
}
/// setCodeGenDebugOptions - Set codegen debugging options to aid in debugging
/// LTO problems.
-void LTOCodeGenerator::setCodeGenDebugOptions(const char *options) {
- for (std::pair<StringRef, StringRef> o = getToken(options);
- !o.first.empty(); o = getToken(o.second))
+void LTOCodeGenerator::setCodeGenDebugOptions(const char *Options) {
+ for (std::pair<StringRef, StringRef> o = getToken(Options); !o.first.empty();
+ o = getToken(o.second))
CodegenOptions.push_back(o.first);
}
Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
/* RespectFilters */ true);
}
+
+namespace {
+class LTODiagnosticInfo : public DiagnosticInfo {
+ const Twine &Msg;
+public:
+ LTODiagnosticInfo(const Twine &DiagMsg, DiagnosticSeverity Severity=DS_Error)
+ : DiagnosticInfo(DK_Linker, Severity), Msg(DiagMsg) {}
+ void print(DiagnosticPrinter &DP) const override { DP << Msg; }
+};
+}
+
+void LTOCodeGenerator::emitError(const std::string &ErrMsg) {
+ if (DiagHandler)
+ (*DiagHandler)(LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
+ else
+ Context.diagnose(LTODiagnosticInfo(ErrMsg));
+}