25 #include "llvm/Config/config.h"
63 #include <system_error>
67 #ifdef LLVM_VERSION_INFO
68 return PACKAGE_NAME
" version " PACKAGE_VERSION
", " LLVM_VERSION_INFO;
70 return PACKAGE_NAME
" version " PACKAGE_VERSION;
76 "lto-discard-value-names",
77 cl::desc(
"Strip names from Value during LTO (other than GlobalValue)."),
86 "lto-strip-invalid-debug-info",
87 cl::desc(
"Strip invalid debug info metadata during LTO instead of aborting."),
97 cl::desc(
"Output filename for pass remarks"),
101 "lto-pass-remarks-with-hotness",
102 cl::desc(
"With PGO, include profile count in optimization remarks"),
107 : Context(Context), MergedModule(new
Module(
"ld-temp.o", Context)),
108 TheLinker(new
Linker(*MergedModule)) {
111 initializeLTOPasses();
119 void LTOCodeGenerator::initializeLTOPasses() {
147 for (
int i = 0, e = undefs.size();
i != e; ++
i)
148 AsmUndefinedRefs[undefs[
i]] = 1;
153 "Expected module in same context");
155 bool ret = TheLinker->linkInModule(Mod->
takeModule());
159 HasVerifiedInput =
false;
165 assert(&Mod->getModule().getContext() == &Context &&
166 "Expected module in same context");
168 AsmUndefinedRefs.
clear();
170 MergedModule = Mod->takeModule();
171 TheLinker = make_unique<Linker>(*MergedModule);
175 HasVerifiedInput =
false;
179 this->Options = Options;
185 EmitDwarfDebugInfo =
false;
189 EmitDwarfDebugInfo =
true;
215 if (!determineTarget())
219 verifyMergedModuleOnce();
222 applyScopeRestrictions();
228 std::string ErrMsg =
"could not open bitcode file for writing: ";
239 std::string ErrMsg =
"could not write bitcode file: ";
250 bool LTOCodeGenerator::compileOptimizedToFile(
const char **
Name) {
261 emitError(EC.message());
269 objFile.os().close();
270 if (objFile.os().has_error()) {
271 emitError((
Twine(
"could not write object file: ") + Filename).str());
272 objFile.os().clear_error();
283 NativeObjectPath = Filename.
c_str();
284 *Name = NativeObjectPath.c_str();
288 std::unique_ptr<MemoryBuffer>
291 if (!compileOptimizedToFile(&name))
297 if (std::error_code EC = BufferOrErr.
getError()) {
298 emitError(EC.message());
306 return std::move(*BufferOrErr);
311 bool DisableGVNLoadPRE,
312 bool DisableVectorization) {
313 if (!
optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
314 DisableVectorization))
317 return compileOptimizedToFile(Name);
320 std::unique_ptr<MemoryBuffer>
322 bool DisableGVNLoadPRE,
bool DisableVectorization) {
323 if (!
optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
324 DisableVectorization))
330 bool LTOCodeGenerator::determineTarget() {
334 TripleStr = MergedModule->getTargetTriple();
335 if (TripleStr.empty()) {
337 MergedModule->setTargetTriple(TripleStr);
364 TargetMach = createTargetMachine();
368 std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
369 return std::unique_ptr<TargetMachine>(
377 void LTOCodeGenerator::preserveDiscardableGVs(
380 std::vector<GlobalValue *> Used;
382 if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
385 if (GV.hasAvailableExternallyLinkage())
387 (
Twine(
"Linker asked to preserve available_externally global: '") +
388 GV.getName() +
"'").str());
389 if (GV.hasInternalLinkage())
390 return emitWarning((
Twine(
"Linker asked to preserve internal global: '") +
391 GV.getName() +
"'").str());
394 for (
auto &GV : TheModule)
395 mayPreserveGlobal(GV);
396 for (
auto &GV : TheModule.globals())
397 mayPreserveGlobal(GV);
398 for (
auto &GV : TheModule.aliases())
399 mayPreserveGlobal(GV);
407 void LTOCodeGenerator::applyScopeRestrictions() {
408 if (ScopeRestrictionsDone)
415 auto mustPreserveGV = [&](
const GlobalValue &GV) ->
bool {
424 MangledName.
reserve(GV.getName().size() + 1);
426 return MustPreserveSymbols.
count(MangledName);
430 preserveDiscardableGVs(*MergedModule, mustPreserveGV);
432 if (!ShouldInternalize)
435 if (ShouldRestoreGlobalsLinkage) {
440 if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
442 ExternalSymbols.insert(std::make_pair(GV.getName(), GV.getLinkage()));
444 for (
auto &GV : *MergedModule)
446 for (
auto &GV : MergedModule->globals())
448 for (
auto &GV : MergedModule->aliases())
458 ScopeRestrictionsDone =
true;
462 void LTOCodeGenerator::restoreLinkageForExternals() {
463 if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
466 assert(ScopeRestrictionsDone &&
467 "Cannot externalize without internalization!");
469 if (ExternalSymbols.empty())
473 if (!GV.hasLocalLinkage() || !GV.hasName())
476 auto I = ExternalSymbols.find(GV.getName());
477 if (
I == ExternalSymbols.end())
480 GV.setLinkage(
I->second);
483 std::for_each(MergedModule->begin(), MergedModule->end(),
externalize);
484 std::for_each(MergedModule->global_begin(), MergedModule->global_end(),
486 std::for_each(MergedModule->alias_begin(), MergedModule->alias_end(),
490 void LTOCodeGenerator::verifyMergedModuleOnce() {
492 if (HasVerifiedInput)
494 HasVerifiedInput =
true;
497 bool BrokenDebugInfo =
false;
500 if (BrokenDebugInfo) {
501 emitWarning(
"Invalid debug info found, debug info will be stripped");
509 bool LTOCodeGenerator::setupOptimizationRemarks() {
512 DiagnosticOutputFile = llvm::make_unique<tool_output_file>(
515 emitError(EC.message());
519 llvm::make_unique<yaml::Output>(DiagnosticOutputFile->os()));
528 void LTOCodeGenerator::finishOptimizationRemarks() {
529 if (DiagnosticOutputFile) {
530 DiagnosticOutputFile->keep();
532 DiagnosticOutputFile->os().flush();
538 bool DisableGVNLoadPRE,
539 bool DisableVectorization) {
540 if (!this->determineTarget())
543 if (!setupOptimizationRemarks())
548 verifyMergedModuleOnce();
551 this->applyScopeRestrictions();
557 MergedModule->setDataLayout(TargetMach->createDataLayout());
562 Triple TargetTriple(TargetMach->getTargetTriple());
565 PMB.LoopVectorize = !DisableVectorization;
566 PMB.SLPVectorize = !DisableVectorization;
570 PMB.OptLevel = OptLevel;
571 PMB.VerifyInput = !DisableVerify;
572 PMB.VerifyOutput = !DisableVerify;
574 PMB.populateLTOPassManager(passes);
577 passes.
run(*MergedModule);
583 if (!this->determineTarget())
588 verifyMergedModuleOnce();
595 preCodeGenPasses.
run(*MergedModule);
599 restoreLinkageForExternals();
606 MergedModule =
splitCodeGen(std::move(MergedModule), Out, {},
607 [&]() {
return createTargetMachine(); }, FileType,
608 ShouldRestoreGlobalsLinkage);
614 finishOptimizationRemarks();
622 for (std::pair<StringRef, StringRef> o =
getToken(Options); !o.first.empty();
624 CodegenOptions.push_back(o.first);
629 if (!CodegenOptions.empty()) {
631 std::vector<const char *> CodegenArgv(1,
"libLLVMLTO");
632 for (std::string &Arg : CodegenOptions)
633 CodegenArgv.push_back(Arg.c_str());
638 void LTOCodeGenerator::DiagnosticHandler(
const DiagnosticInfo &DI,
643 void LTOCodeGenerator::DiagnosticHandler2(
const DiagnosticInfo &DI) {
661 std::string MsgStorage;
669 assert(DiagHandler &&
"Invalid diagnostic handler");
670 (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
676 this->DiagHandler = DiagHandler;
677 this->DiagContext = Ctxt;
679 return Context.setDiagnosticHandler(
nullptr,
nullptr);
682 Context.setDiagnosticHandler(LTOCodeGenerator::DiagnosticHandler,
this,
696 void LTOCodeGenerator::emitError(
const std::string &ErrMsg) {
698 (*DiagHandler)(
LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
700 Context.diagnose(LTODiagnosticInfo(ErrMsg));
703 void LTOCodeGenerator::emitWarning(
const std::string &ErrMsg) {
707 Context.diagnose(LTODiagnosticInfo(ErrMsg,
DS_Warning));
void initializeDCELegacyPassPass(PassRegistry &)
std::error_code getError() const
void initializeGlobalOptLegacyPassPass(PassRegistry &)
Represents either an error or a value T.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
void initializeInternalizeLegacyPassPass(PassRegistry &)
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue * > Values)
Adds global values to the llvm.compiler.used list.
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
void setDiagnosticsOutputFile(std::unique_ptr< yaml::Output > F)
Set the diagnostics output file used for optimization diagnostics.
cl::opt< std::string > LTORemarksFilename("lto-pass-remarks-output", cl::desc("Output filename for pass remarks"), cl::value_desc("filename"))
std::unique_ptr< MemoryBuffer > compileOptimized()
Compiles the merged optimized module into a single output file.
A Module instance is used to store all the information related to an LLVM module. ...
void initializeSimpleInlinerPass(PassRegistry &)
void initializeJumpThreadingPass(PassRegistry &)
std::error_code remove(const Twine &path, bool IgnoreNonExisting=true)
Remove path.
lto_codegen_diagnostic_severity_t
Diagnostic severity.
std::string getDefaultTargetTriple()
getDefaultTargetTriple() - Return the default target triple the compiler has been configured to produ...
void enableDebugTypeODRUniquing()
ImmutablePass * createTargetTransformInfoWrapperPass(TargetIRAnalysis TIRA)
Create an analysis pass wrapper around a TTI object.
An efficient, type-erasing, non-owning reference to a callable.
void initializeGlobalDCELegacyPassPass(PassRegistry &)
bool addModule(struct LTOModule *)
Merge given module.
Implementation of the target library information.
bool optimize(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
Optimizes the merged module.
static const Target * lookupTarget(const std::string &Triple, std::string &Error)
lookupTarget - Lookup a target based on a target triple.
void reserve(size_type N)
std::unique_ptr< Module > takeModule()
void setDiscardValueNames(bool Discard)
Set the Context runtime configuration to discard all value name (but GlobalValue).
void initializeReversePostOrderFunctionAttrsLegacyPassPass(PassRegistry &)
static void externalize(GlobalValue *GV)
void initializePruneEHPass(PassRegistry &)
void add(Pass *P) override
Add a pass to the queue of passes to run.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
void setDiagnosticHotnessRequested(bool Requested)
Set if a code hotness metric should be included in optimization diagnostics.
void initializeArgPromotionPass(PassRegistry &)
std::unique_ptr< MemoryBuffer > compile(bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
As with compile_to_file(), this function compiles the merged module into single output file...
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
Interface for custom diagnostic printing.
const Module & getModule() const
bool writeMergedModules(StringRef Path)
Write the merged module to the file specified by the given path.
cl::opt< bool > LTOPassRemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
Pass * createObjCARCContractPass()
static const char * getVersionString()
bool StripDebugInfo(Module &M)
Strip debug info in the module if it exists.
This class provides the core functionality of linking in LLVM.
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory)...
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
DiagnosticSeverity getSeverity() const
bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview="", bool IgnoreErrors=false)
void initializeGVNLegacyPassPass(PassRegistry &)
PassManager manages ModulePassManagers.
initializer< Ty > init(const Ty &Val)
This is the base abstract class for diagnostic reporting in the backend.
This is an important class for using LLVM in a threaded context.
This file contains the declarations for the subclasses of Constant, which represent the different fla...
void initializeSROALegacyPassPass(PassRegistry &)
void setOptLevel(unsigned OptLevel)
void setTargetOptions(const TargetOptions &Options)
C++ class which implements the opaque lto_module_t type.
void setCodeGenDebugOptions(StringRef Opts)
Pass options to the driver and optimization passes.
void initializeMemCpyOptLegacyPassPass(PassRegistry &)
void initializeConstantMergeLegacyPassPass(PassRegistry &)
bool run(Module &M)
run - Execute all of the passes scheduled for execution.
virtual void print(DiagnosticPrinter &DP) const =0
Print using the given DP a user-friendly message.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
void(* lto_diagnostic_handler_t)(lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt)
Diagnostic handler type.
Triple - Helper class for working with autoconf configuration names.
C++ class which implements the opaque lto_code_gen_t type.
void initializeGlobalsAAWrapperPassPass(PassRegistry &)
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
const std::vector< StringRef > & getAsmUndefinedRefs()
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, CodeModel::Model CM=CodeModel::Default, CodeGenOpt::Level OL=CodeGenOpt::Default) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
Module.h This file contains the declarations for the Module class.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
SubtargetFeatures - Manages the enabling and disabling of subtarget specific features.
void initializeDAHPass(PassRegistry &)
bool verifyModule(const Module &M, raw_ostream *OS=nullptr, bool *BrokenDebugInfo=nullptr)
Check a module for errors.
Basic diagnostic printer that uses an underlying raw_ostream.
cl::opt< bool > LTODiscardValueNames("lto-discard-value-names", cl::desc("Strip names from Value during LTO (other than GlobalValue)."), cl::init(false), cl::Hidden)
void WriteBitcodeToFile(const Module *M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false)
Write the specified module to the specified raw output stream.
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatileSize=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
std::error_code createTemporaryFile(const Twine &Prefix, StringRef Suffix, int &ResultFD, SmallVectorImpl< char > &ResultPath)
Create a file in the system temporary directory.
void initializePostOrderFunctionAttrsLegacyPassPass(PassRegistry &)
void initializeIPSCCPLegacyPassPass(PassRegistry &)
bool compile_to_file(const char **Name, bool DisableVerify, bool DisableInline, bool DisableGVNLoadPRE, bool DisableVectorization)
Compile the merged module into a single output file; the path to output file is returned to the calle...
void initializeInstructionCombiningPassPass(PassRegistry &)
void close()
Manually flush the stream and close the file.
void updateCompilerUsed(Module &TheModule, const TargetMachine &TM, const StringSet<> &AsmUndefinedRefs)
Find all globals in TheModule that are referenced in AsmUndefinedRefs, as well as the user-supplied f...
void setAsmUndefinedRefs(struct LTOModule *)
void initializeCFGSimplifyPassPass(PassRegistry &)
void PrintStatistics()
Print statistics to the file returned by CreateInfoOutputFile().
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
A raw_ostream that writes to an std::string.
Pass * createFunctionInliningPass()
createFunctionInliningPass - Return a new pass object that uses a heuristic to inline direct function...
LTOCodeGenerator(LLVMContext &Context)
static cl::opt< bool, true > Debug("debug", cl::desc("Enable debug output"), cl::Hidden, cl::location(DebugFlag))
const FeatureBitset Features
void initializeLegacyLICMPassPass(PassRegistry &)
StringRef - Represent a constant reference to a string, i.e.
PassRegistry - This class manages the registration and intitialization of the pass subsystem as appli...
void parseCodeGenDebugOptions()
Parse the options set in setCodeGenDebugOptions.
void setModule(std::unique_ptr< LTOModule > M)
Set the destination module.
void initializeMergedLoadStoreMotionLegacyPassPass(PassRegistry &)
std::unique_ptr< Module > splitCodeGen(std::unique_ptr< Module > M, ArrayRef< raw_pwrite_stream * > OSs, ArrayRef< llvm::raw_pwrite_stream * > BCOSs, const std::function< std::unique_ptr< TargetMachine >()> &TMFactory, TargetMachine::CodeGenFileType FT=TargetMachine::CGFT_ObjectFile, bool PreserveLocals=false)
Split M into OSs.size() partitions, and generate code for each.
bool internalizeModule(Module &TheModule, std::function< bool(const GlobalValue &)> MustPreserveGV, CallGraph *CG=nullptr)
Helper function to internalize functions and variables in a Module.
bool AreStatisticsEnabled()
Check if statistics are enabled.
cl::opt< bool > LTOStripInvalidDebugInfo("lto-strip-invalid-debug-info", cl::desc("Strip invalid debug info metadata during LTO instead of aborting."), cl::init(false), cl::Hidden)
void setDiagnosticHandler(lto_diagnostic_handler_t, void *)
LLVMContext & getContext() const
Get the global data context.
This file describes how to lower LLVM code to machine code.
void clear_error()
Set the flag read by has_error() to false.
void setDebugInfo(lto_debug_model)