24 #include "llvm/Config/config.h" 61 #include <system_error> 65 #ifdef LLVM_VERSION_INFO 66 return PACKAGE_NAME
" version " PACKAGE_VERSION
", " LLVM_VERSION_INFO;
68 return PACKAGE_NAME
" version " PACKAGE_VERSION;
74 "lto-discard-value-names",
75 cl::desc(
"Strip names from Value during LTO (other than GlobalValue)."),
85 cl::desc(
"Output filename for pass remarks"),
89 "lto-pass-remarks-with-hotness",
90 cl::desc(
"With PGO, include profile count in optimization remarks"),
95 : Context(Context), MergedModule(new
Module(
"ld-temp.o", Context)),
96 TheLinker(new
Linker(*MergedModule)) {
99 initializeLTOPasses();
107 void LTOCodeGenerator::initializeLTOPasses() {
135 for (
int i = 0, e = undefs.size(); i != e; ++i)
136 AsmUndefinedRefs[undefs[i]] = 1;
141 "Expected module in same context");
143 bool ret = TheLinker->linkInModule(Mod->
takeModule());
147 HasVerifiedInput =
false;
153 assert(&Mod->getModule().getContext() == &Context &&
154 "Expected module in same context");
156 AsmUndefinedRefs.
clear();
158 MergedModule = Mod->takeModule();
159 TheLinker = make_unique<Linker>(*MergedModule);
163 HasVerifiedInput =
false;
167 this->Options = Options;
173 EmitDwarfDebugInfo =
false;
177 EmitDwarfDebugInfo =
true;
203 if (!determineTarget())
207 verifyMergedModuleOnce();
210 applyScopeRestrictions();
216 std::string ErrMsg =
"could not open bitcode file for writing: ";
217 ErrMsg += Path.
str() +
": " + EC.message();
227 std::string ErrMsg =
"could not write bitcode file: ";
228 ErrMsg += Path.
str() +
": " + Out.
os().
error().message();
238 bool LTOCodeGenerator::compileOptimizedToFile(
const char **
Name) {
249 emitError(EC.message());
259 emitError((
Twine(
"could not write object file: ") + Filename +
": " +
260 objFile.
os().
error().message())
273 NativeObjectPath = Filename.
c_str();
274 *Name = NativeObjectPath.c_str();
278 std::unique_ptr<MemoryBuffer>
281 if (!compileOptimizedToFile(&name))
287 if (std::error_code EC = BufferOrErr.
getError()) {
288 emitError(EC.message());
296 return std::move(*BufferOrErr);
301 bool DisableGVNLoadPRE,
302 bool DisableVectorization) {
303 if (!
optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
304 DisableVectorization))
307 return compileOptimizedToFile(Name);
310 std::unique_ptr<MemoryBuffer>
312 bool DisableGVNLoadPRE,
bool DisableVectorization) {
313 if (!
optimize(DisableVerify, DisableInline, DisableGVNLoadPRE,
314 DisableVectorization))
320 bool LTOCodeGenerator::determineTarget() {
324 TripleStr = MergedModule->getTargetTriple();
325 if (TripleStr.empty()) {
327 MergedModule->setTargetTriple(TripleStr);
354 TargetMach = createTargetMachine();
358 std::unique_ptr<TargetMachine> LTOCodeGenerator::createTargetMachine() {
360 TripleStr, MCpu, FeatureStr, Options, RelocModel,
None, CGOptLevel));
366 void LTOCodeGenerator::preserveDiscardableGVs(
369 std::vector<GlobalValue *> Used;
371 if (!GV.isDiscardableIfUnused() || GV.isDeclaration() ||
374 if (GV.hasAvailableExternallyLinkage())
376 (
Twine(
"Linker asked to preserve available_externally global: '") +
377 GV.getName() +
"'").str());
378 if (GV.hasInternalLinkage())
379 return emitWarning((
Twine(
"Linker asked to preserve internal global: '") +
380 GV.getName() +
"'").str());
383 for (
auto &GV : TheModule)
384 mayPreserveGlobal(GV);
385 for (
auto &GV : TheModule.globals())
386 mayPreserveGlobal(GV);
387 for (
auto &GV : TheModule.aliases())
388 mayPreserveGlobal(GV);
396 void LTOCodeGenerator::applyScopeRestrictions() {
397 if (ScopeRestrictionsDone)
413 MangledName.
reserve(GV.getName().size() + 1);
415 return MustPreserveSymbols.
count(MangledName);
421 if (!ShouldInternalize)
424 if (ShouldRestoreGlobalsLinkage) {
429 if (!GV.hasAvailableExternallyLinkage() && !GV.hasLocalLinkage() &&
431 ExternalSymbols.
insert(std::make_pair(GV.getName(), GV.getLinkage()));
433 for (
auto &GV : *MergedModule)
435 for (
auto &GV : MergedModule->globals())
437 for (
auto &GV : MergedModule->aliases())
447 ScopeRestrictionsDone =
true;
451 void LTOCodeGenerator::restoreLinkageForExternals() {
452 if (!ShouldInternalize || !ShouldRestoreGlobalsLinkage)
455 assert(ScopeRestrictionsDone &&
456 "Cannot externalize without internalization!");
458 if (ExternalSymbols.
empty())
462 if (!GV.hasLocalLinkage() || !GV.hasName())
465 auto I = ExternalSymbols.
find(GV.getName());
466 if (
I == ExternalSymbols.
end())
469 GV.setLinkage(
I->second);
477 void LTOCodeGenerator::verifyMergedModuleOnce() {
479 if (HasVerifiedInput)
481 HasVerifiedInput =
true;
483 bool BrokenDebugInfo =
false;
486 if (BrokenDebugInfo) {
487 emitWarning(
"Invalid debug info found, debug info will be stripped");
492 void LTOCodeGenerator::finishOptimizationRemarks() {
493 if (DiagnosticOutputFile) {
494 DiagnosticOutputFile->keep();
496 DiagnosticOutputFile->os().flush();
502 bool DisableGVNLoadPRE,
503 bool DisableVectorization) {
504 if (!this->determineTarget())
509 if (!DiagFileOrErr) {
510 errs() <<
"Error: " <<
toString(DiagFileOrErr.takeError()) <<
"\n";
513 DiagnosticOutputFile = std::move(*DiagFileOrErr);
517 verifyMergedModuleOnce();
520 this->applyScopeRestrictions();
526 MergedModule->setDataLayout(TargetMach->createDataLayout());
531 Triple TargetTriple(TargetMach->getTargetTriple());
534 PMB.LoopVectorize = !DisableVectorization;
535 PMB.SLPVectorize = !DisableVectorization;
540 PMB.LibraryInfo->disableAllFunctions();
541 PMB.OptLevel = OptLevel;
542 PMB.VerifyInput = !DisableVerify;
543 PMB.VerifyOutput = !DisableVerify;
545 PMB.populateLTOPassManager(passes);
548 passes.
run(*MergedModule);
554 if (!this->determineTarget())
559 verifyMergedModuleOnce();
566 preCodeGenPasses.
run(*MergedModule);
570 restoreLinkageForExternals();
577 MergedModule =
splitCodeGen(std::move(MergedModule), Out, {},
578 [&]() {
return createTargetMachine(); }, FileType,
579 ShouldRestoreGlobalsLinkage);
586 finishOptimizationRemarks();
594 for (std::pair<StringRef, StringRef> o =
getToken(Options); !o.first.empty();
596 CodegenOptions.push_back(o.first);
601 if (!CodegenOptions.empty()) {
603 std::vector<const char *> CodegenArgv(1,
"libLLVMLTO");
604 for (std::string &
Arg : CodegenOptions)
605 CodegenArgv.push_back(
Arg.c_str());
629 std::string MsgStorage;
637 assert(DiagHandler &&
"Invalid diagnostic handler");
638 (*DiagHandler)(Severity, MsgStorage.c_str(), DiagContext);
645 : CodeGenerator(CodeGenPtr) {}
647 CodeGenerator->DiagnosticHandler(DI);
656 this->DiagHandler = DiagHandler;
657 this->DiagContext = Ctxt;
676 void LTOCodeGenerator::emitError(
const std::string &ErrMsg) {
678 (*DiagHandler)(
LTO_DS_ERROR, ErrMsg.c_str(), DiagContext);
683 void LTOCodeGenerator::emitWarning(
const std::string &ErrMsg) {
void setDiagnosticHandler(std::unique_ptr< DiagnosticHandler > &&DH, bool RespectFilters=false)
setDiagnosticHandler - This method sets unique_ptr to object of DiagnosticHandler to provide custom d...
void initializeDCELegacyPassPass(PassRegistry &)
void appendToCompilerUsed(Module &M, ArrayRef< GlobalValue *> Values)
Adds global values to the llvm.compiler.used list.
bool isOSDarwin() const
isOSDarwin - Is this a "Darwin" OS (OS X, iOS, or watchOS).
This is the base class for diagnostic handling in LLVM.
void initializeGlobalOptLegacyPassPass(PassRegistry &)
Represents either an error or a value T.
raw_ostream & errs()
This returns a reference to a raw_ostream for standard error.
Expected< std::unique_ptr< ToolOutputFile > > setupOptimizationRemarks(LLVMContext &Context, StringRef LTORemarksFilename, bool LTOPassRemarksWithHotness, int Count=-1)
Setup optimization remarks.
static PassRegistry * getPassRegistry()
getPassRegistry - Access the global registry object, which is automatically initialized at applicatio...
LLVM_NODISCARD std::string str() const
str - Get the contents as an std::string.
PassManagerBuilder - This class is used to set up a standard optimization sequence for languages like...
void initializeInternalizeLegacyPassPass(PassRegistry &)
LLVM_ATTRIBUTE_NORETURN void report_fatal_error(Error Err, bool gen_crash_diag=true)
Report a serious error, calling any installed error handler.
This class represents lattice values for constants.
void getDefaultSubtargetFeatures(const Triple &Triple)
Adds the default features for the specified target triple.
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. ...
TargetMachine * createTargetMachine(StringRef TT, StringRef CPU, StringRef Features, const TargetOptions &Options, Optional< Reloc::Model > RM, Optional< CodeModel::Model > CM=None, CodeGenOpt::Level OL=CodeGenOpt::Default, bool JIT=false) const
createTargetMachine - Create a target specific machine implementation for the specified Triple...
amdgpu Simplify well known AMD library false FunctionCallee Value const Twine & Name
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.
std::string getString() const
Returns features as a string.
const FeatureBitset Features
An efficient, type-erasing, non-owning reference to a callable.
void initializeGlobalDCELegacyPassPass(PassRegistry &)
iterator find(StringRef Key)
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.
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 FileType=TargetMachine::CGFT_ObjectFile, bool PreserveLocals=false)
Split M into OSs.size() partitions, and generate code for each.
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)
void reportAndResetTimings()
If -time-passes has been specified, report the timings immediately and then reset the timers to zero...
std::unique_ptr< Module > takeModule()
std::error_code error() const
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 &)
std::string toString(Error E)
Write all error messages (if any) in E to a string.
void add(Pass *P) override
Add a pass to the queue of passes to run.
DiagnosticSeverity
Defines the different supported severity of a diagnostic.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
LLVMContext & getContext() const
Get the global data context.
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...
Interface for custom diagnostic printing.
This header defines classes/functions to handle pass execution timing information with interfaces for...
bool writeMergedModules(StringRef Path)
Write the merged module to the file specified by the given path.
std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \\\)
getToken - This function extracts one token from source, ignoring any leading characters that appear ...
cl::opt< bool > LTOPassRemarksWithHotness("lto-pass-remarks-with-hotness", cl::desc("With PGO, include profile count in optimization remarks"), cl::Hidden)
ArchType getArch() const
getArch - Get the parsed architecture type of this triple.
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)...
void initializeGVNLegacyPassPass(PassRegistry &)
PassManager manages ModulePassManagers.
initializer< Ty > init(const Ty &Val)
This is the base abstract class for diagnostic reporting in the backend.
size_type count(StringRef Key) const
count - Return 1 if the element is in the map, 0 otherwise.
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...
std::error_code getError() const
void WriteBitcodeToFile(const Module &M, raw_ostream &Out, bool ShouldPreserveUseListOrder=false, const ModuleSummaryIndex *Index=nullptr, bool GenerateHash=false, ModuleHash *ModHash=nullptr)
Write the specified module to the specified raw output stream.
void initializeSROALegacyPassPass(PassRegistry &)
bool ParseCommandLineOptions(int argc, const char *const *argv, StringRef Overview="", raw_ostream *Errs=nullptr, const char *EnvVar=nullptr)
void setOptLevel(unsigned OptLevel)
amdgpu Simplify well known AMD library false FunctionCallee Value * Arg
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
void setTargetOptions(const TargetOptions &Options)
const Module & getModule() const
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.
Triple - Helper class for working with autoconf configuration names.
bool has_error() const
Return the value of the flag in this raw_fd_ostream indicating whether an output error has been encou...
C++ class which implements the opaque lto_code_gen_t type.
void initializeGlobalsAAWrapperPassPass(PassRegistry &)
void(* lto_diagnostic_handler_t)(lto_codegen_diagnostic_severity_t severity, const char *diag, void *ctxt)
Diagnostic handler type.
const std::vector< StringRef > & getAsmUndefinedRefs()
Module.h This file contains the declarations for the Module class.
bool insert(MapEntryTy *KeyValue)
insert - Insert the specified key/value pair into the map.
raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
The access may modify the value stored in memory.
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.
void DiagnosticHandler(const DiagnosticInfo &DI)
cl::opt< bool > LTODiscardValueNames("lto-discard-value-names", cl::desc("Strip names from Value during LTO (other than GlobalValue)."), cl::init(false), cl::Hidden)
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...
DiagnosticSeverity getSeverity() const
static bool mustPreserveGV(const GlobalValue &GV)
Predicate for Internalize pass.
void diagnose(const DiagnosticInfo &DI)
Report a message to the currently installed diagnostic handler.
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...
static ErrorOr< std::unique_ptr< MemoryBuffer > > getFile(const Twine &Filename, int64_t FileSize=-1, bool RequiresNullTerminator=true, bool IsVolatile=false)
Open the specified file as a MemoryBuffer, returning a new MemoryBuffer if successful, otherwise returning null.
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))
void initializeLegacyLICMPassPass(PassRegistry &)
void getNameWithPrefix(raw_ostream &OS, const GlobalValue *GV, bool CannotUsePrivateLabel) const
Print the appropriate prefix and the specified global variable's name.
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.
UnaryPredicate for_each(R &&Range, UnaryPredicate P)
Provide wrappers to std::for_each which take ranges instead of having to pass begin/end explicitly...
void setModule(std::unique_ptr< LTOModule > M)
Set the destination module.
void initializeMergedLoadStoreMotionLegacyPassPass(PassRegistry &)
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.
void setDiagnosticHandler(lto_diagnostic_handler_t, void *)
void clear_error()
Set the flag read by has_error() to false.
void setDebugInfo(lto_debug_model)