This patch removes the std::string& argument from a number of C++ LTO API calls
and instead makes them use the installed diagnostic handler. This would also
improve consistency of diagnostic handling infrastructure: if an LTO client used
lto_codegen_set_diagnostic_handler() to install a custom error handler, we do
not want some error messages to go through the custom error handler, and some
other error messages to go into sLastErrorString.
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@253367
91177308-0d34-0410-b5e6-
96231b3b80d8
/// Write the merged module to the file specified by the given path. Return
/// true on success.
/// Write the merged module to the file specified by the given path. Return
/// true on success.
- bool writeMergedModules(const char *Path, std::string &ErrMsg);
+ bool writeMergedModules(const char *Path);
/// Compile the merged module into a *single* object file; the path to object
/// file is returned to the caller via argument "name". Return true on
/// Compile the merged module into a *single* object file; the path to object
/// file is returned to the caller via argument "name". Return true on
/// don't who (LTOCodeGenerator or the obj file) will last longer.
bool compile_to_file(const char **Name, bool DisableVerify,
bool DisableInline, bool DisableGVNLoadPRE,
/// don't who (LTOCodeGenerator or the obj file) will last longer.
bool compile_to_file(const char **Name, bool DisableVerify,
bool DisableInline, bool DisableGVNLoadPRE,
- bool DisableVectorization, std::string &ErrMsg);
+ bool DisableVectorization);
/// As with compile_to_file(), this function compiles the merged module into
/// single object file. Instead of returning the object-file-path to the
/// As with compile_to_file(), this function compiles the merged module into
/// single object file. Instead of returning the object-file-path to the
/// successful.
std::unique_ptr<MemoryBuffer> compile(bool DisableVerify, bool DisableInline,
bool DisableGVNLoadPRE,
/// successful.
std::unique_ptr<MemoryBuffer> compile(bool DisableVerify, bool DisableInline,
bool DisableGVNLoadPRE,
- bool DisableVectorization,
- std::string &errMsg);
+ bool DisableVectorization);
/// Optimizes the merged module. Returns true on success.
bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE,
/// Optimizes the merged module. Returns true on success.
bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE,
- bool DisableVectorization, std::string &ErrMsg);
+ bool DisableVectorization);
/// Compiles the merged optimized module into a single object file. It brings
/// the object to a buffer, and returns the buffer to the caller. Return NULL
/// if the compilation was not successful.
/// Compiles the merged optimized module into a single object file. It brings
/// the object to a buffer, and returns the buffer to the caller. Return NULL
/// if the compilation was not successful.
- std::unique_ptr<MemoryBuffer> compileOptimized(std::string &ErrMsg);
+ std::unique_ptr<MemoryBuffer> compileOptimized();
/// Compile the merged optimized module into out.size() object files each
/// representing a linkable partition of the module. If out contains more
/// than one element, code generation is done in parallel with out.size()
/// threads. Object files will be written to members of out. Returns true on
/// success.
/// Compile the merged optimized module into out.size() object files each
/// representing a linkable partition of the module. If out contains more
/// than one element, code generation is done in parallel with out.size()
/// threads. Object files will be written to members of out. Returns true on
/// success.
- bool compileOptimized(ArrayRef<raw_pwrite_stream *> Out, std::string &ErrMsg);
+ bool compileOptimized(ArrayRef<raw_pwrite_stream *> Out);
void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
void setDiagnosticHandler(lto_diagnostic_handler_t, void *);
private:
void initializeLTOPasses();
private:
void initializeLTOPasses();
- bool compileOptimizedToFile(const char **Name, std::string &ErrMsg);
+ bool compileOptimizedToFile(const char **Name);
void applyScopeRestrictions();
void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls,
std::vector<const char *> &MustPreserveList,
SmallPtrSetImpl<GlobalValue *> &AsmUsed,
Mangler &Mangler);
void applyScopeRestrictions();
void applyRestriction(GlobalValue &GV, ArrayRef<StringRef> Libcalls,
std::vector<const char *> &MustPreserveList,
SmallPtrSetImpl<GlobalValue *> &AsmUsed,
Mangler &Mangler);
- bool determineTarget(std::string &ErrMsg);
+ bool determineTarget();
static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
void DiagnosticHandler2(const DiagnosticInfo &DI);
static void DiagnosticHandler(const DiagnosticInfo &DI, void *Context);
void DiagnosticHandler2(const DiagnosticInfo &DI);
+ void emitError(const std::string &ErrMsg);
+
typedef StringMap<uint8_t> StringSet;
std::unique_ptr<LLVMContext> OwnedContext;
typedef StringMap<uint8_t> StringSet;
std::unique_ptr<LLVMContext> OwnedContext;
-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
return false;
// mark which symbols can not be internalized
std::error_code EC;
tool_output_file Out(Path, EC, sys::fs::F_None);
if (EC) {
std::error_code EC;
tool_output_file Out(Path, EC, sys::fs::F_None);
if (EC) {
- ErrMsg = "could not open bitcode file for writing: ";
+ std::string ErrMsg = "could not open bitcode file for writing: ";
Out.os().close();
if (Out.os().has_error()) {
Out.os().close();
if (Out.os().has_error()) {
- ErrMsg = "could not write bitcode file: ";
+ std::string ErrMsg = "could not write bitcode file: ";
Out.os().clear_error();
return false;
}
Out.os().clear_error();
return false;
}
-bool LTOCodeGenerator::compileOptimizedToFile(const char **Name,
- std::string &ErrMsg) {
+bool LTOCodeGenerator::compileOptimizedToFile(const char **Name) {
// make unique temp .o file to put generated object file
SmallString<128> Filename;
int FD;
std::error_code EC =
sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
if (EC) {
// make unique temp .o file to put generated object file
SmallString<128> Filename;
int FD;
std::error_code EC =
sys::fs::createTemporaryFile("lto-llvm", "o", FD, Filename);
if (EC) {
+ emitError(EC.message());
return false;
}
// generate object file
tool_output_file objFile(Filename.c_str(), FD);
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();
objFile.os().close();
if (objFile.os().has_error()) {
objFile.os().clear_error();
}
std::unique_ptr<MemoryBuffer>
}
std::unique_ptr<MemoryBuffer>
-LTOCodeGenerator::compileOptimized(std::string &ErrMsg) {
+LTOCodeGenerator::compileOptimized() {
- 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()) {
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()) {
+ emitError(EC.message());
sys::fs::remove(NativeObjectPath);
return nullptr;
}
sys::fs::remove(NativeObjectPath);
return nullptr;
}
bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
bool DisableInline,
bool DisableGVNLoadPRE,
bool LTOCodeGenerator::compile_to_file(const char **Name, bool DisableVerify,
bool DisableInline,
bool DisableGVNLoadPRE,
- bool DisableVectorization,
- std::string &ErrMsg) {
+ bool DisableVectorization) {
if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
- DisableVectorization, ErrMsg))
- return compileOptimizedToFile(Name, ErrMsg);
+ return compileOptimizedToFile(Name);
}
std::unique_ptr<MemoryBuffer>
LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
}
std::unique_ptr<MemoryBuffer>
LTOCodeGenerator::compile(bool DisableVerify, bool DisableInline,
- bool DisableGVNLoadPRE, bool DisableVectorization,
- std::string &ErrMsg) {
+ bool DisableGVNLoadPRE, bool DisableVectorization) {
if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
if (!optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
- DisableVectorization, ErrMsg))
- return compileOptimized(ErrMsg);
+ return compileOptimized();
-bool LTOCodeGenerator::determineTarget(std::string &ErrMsg) {
+bool LTOCodeGenerator::determineTarget() {
if (TargetMach)
return true;
if (TargetMach)
return true;
llvm::Triple Triple(TripleStr);
// create target machine from info for merged modules
llvm::Triple Triple(TripleStr);
// create target machine from info for merged modules
const Target *march = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
const Target *march = TargetRegistry::lookupTarget(TripleStr, ErrMsg);
+ if (!march) {
+ emitError(ErrMsg);
// Construct LTOModule, hand over ownership of module and target. Use MAttr as
// the default set of features.
// 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 DisableVerify, bool DisableInline,
bool DisableGVNLoadPRE,
/// Optimize merged modules using various IPO passes
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
return false;
// Mark which symbols can not be internalized
-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;
return false;
legacy::PassManager preCodeGenPasses;
Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler, this,
/* RespectFilters */ true);
}
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));
+}
CodeGen.setAttr(attrs.c_str());
if (!OutputFilename.empty()) {
CodeGen.setAttr(attrs.c_str());
if (!OutputFilename.empty()) {
if (!CodeGen.optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
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";
OSPtrs.push_back(&OSs.back().os());
}
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";
const char *OutputName = nullptr;
if (!CodeGen.compile_to_file(&OutputName, DisableVerify, DisableInline,
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";
bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
maybeParseOptions(cg);
bool lto_codegen_write_merged_modules(lto_code_gen_t cg, const char *path) {
maybeParseOptions(cg);
- return !unwrap(cg)->writeMergedModules(path, sLastErrorString);
+ return !unwrap(cg)->writeMergedModules(path);
}
const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
}
const void *lto_codegen_compile(lto_code_gen_t cg, size_t *length) {
LibLTOCodeGenerator *CG = unwrap(cg);
CG->NativeObjectFile =
CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
LibLTOCodeGenerator *CG = unwrap(cg);
CG->NativeObjectFile =
CG->compile(DisableVerify, DisableInline, DisableGVNLoadPRE,
- DisableLTOVectorization, sLastErrorString);
+ DisableLTOVectorization);
if (!CG->NativeObjectFile)
return nullptr;
*length = CG->NativeObjectFile->getBufferSize();
if (!CG->NativeObjectFile)
return nullptr;
*length = CG->NativeObjectFile->getBufferSize();
bool lto_codegen_optimize(lto_code_gen_t cg) {
maybeParseOptions(cg);
return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
bool lto_codegen_optimize(lto_code_gen_t cg) {
maybeParseOptions(cg);
return !unwrap(cg)->optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
- DisableLTOVectorization, sLastErrorString);
+ DisableLTOVectorization);
}
const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
maybeParseOptions(cg);
LibLTOCodeGenerator *CG = unwrap(cg);
}
const void *lto_codegen_compile_optimized(lto_code_gen_t cg, size_t *length) {
maybeParseOptions(cg);
LibLTOCodeGenerator *CG = unwrap(cg);
- CG->NativeObjectFile = CG->compileOptimized(sLastErrorString);
+ CG->NativeObjectFile = CG->compileOptimized();
if (!CG->NativeObjectFile)
return nullptr;
*length = CG->NativeObjectFile->getBufferSize();
if (!CG->NativeObjectFile)
return nullptr;
*length = CG->NativeObjectFile->getBufferSize();
maybeParseOptions(cg);
return !unwrap(cg)->compile_to_file(
name, DisableVerify, DisableInline, DisableGVNLoadPRE,
maybeParseOptions(cg);
return !unwrap(cg)->compile_to_file(
name, DisableVerify, DisableInline, DisableGVNLoadPRE,
- DisableLTOVectorization, sLastErrorString);
+ DisableLTOVectorization);
}
void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {
}
void lto_codegen_debug_options(lto_code_gen_t cg, const char *opt) {