aboutsummaryrefslogtreecommitdiff
path: root/llvm/lib/LTO/LTO.cpp
diff options
context:
space:
mode:
authorDimitry Andric <dim@FreeBSD.org>2023-02-11 12:38:04 +0000
committerDimitry Andric <dim@FreeBSD.org>2023-02-11 12:38:11 +0000
commite3b557809604d036af6e00c60f012c2025b59a5e (patch)
tree8a11ba2269a3b669601e2fd41145b174008f4da8 /llvm/lib/LTO/LTO.cpp
parent08e8dd7b9db7bb4a9de26d44c1cbfd24e869c014 (diff)
downloadsrc-e3b557809604d036af6e00c60f012c2025b59a5e.tar.gz
src-e3b557809604d036af6e00c60f012c2025b59a5e.zip
Vendor import of llvm-project main llvmorg-16-init-18548-gb0daacf58f41,vendor/llvm-project/llvmorg-16-init-18548-gb0daacf58f41
the last commit before the upstream release/17.x branch was created.
Diffstat (limited to 'llvm/lib/LTO/LTO.cpp')
-rw-r--r--llvm/lib/LTO/LTO.cpp63
1 files changed, 50 insertions, 13 deletions
diff --git a/llvm/lib/LTO/LTO.cpp b/llvm/lib/LTO/LTO.cpp
index cc7be24c1dbd..1cd48adac3f0 100644
--- a/llvm/lib/LTO/LTO.cpp
+++ b/llvm/lib/LTO/LTO.cpp
@@ -49,14 +49,13 @@
#include "llvm/Support/ToolOutputFile.h"
#include "llvm/Support/VCSRevision.h"
#include "llvm/Support/raw_ostream.h"
-#include "llvm/Target/TargetMachine.h"
#include "llvm/Target/TargetOptions.h"
#include "llvm/Transforms/IPO.h"
-#include "llvm/Transforms/IPO/PassManagerBuilder.h"
#include "llvm/Transforms/IPO/WholeProgramDevirt.h"
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
#include "llvm/Transforms/Utils/SplitModule.h"
+#include <optional>
#include <set>
using namespace llvm;
@@ -69,10 +68,12 @@ static cl::opt<bool>
DumpThinCGSCCs("dump-thin-cg-sccs", cl::init(false), cl::Hidden,
cl::desc("Dump the SCCs in the ThinLTO index's callgraph"));
+namespace llvm {
/// Enable global value internalization in LTO.
cl::opt<bool> EnableLTOInternalization(
"enable-lto-internalization", cl::init(true), cl::Hidden,
cl::desc("Enable global value internalization in LTO"));
+}
// Computes a unique hash for the Module considering the current list of
// export/import and other global analysis results.
@@ -131,6 +132,8 @@ void llvm::computeLTOCacheKey(
AddUnsigned(*Conf.CodeModel);
else
AddUnsigned(-1);
+ for (const auto &S : Conf.MllvmArgs)
+ AddString(S);
AddUnsigned(Conf.CGOptLevel);
AddUnsigned(Conf.CGFileType);
AddUnsigned(Conf.OptLevel);
@@ -565,6 +568,22 @@ void LTO::addModuleToGlobalRes(ArrayRef<InputFile::Symbol> Syms,
GlobalRes.IRName = std::string(Sym.getIRName());
}
+ // In rare occasion, the symbol used to initialize GlobalRes has a different
+ // IRName from the inspected Symbol. This can happen on macOS + iOS, when a
+ // symbol is referenced through its mangled name, say @"\01_symbol" while
+ // the IRName is @symbol (the prefix underscore comes from MachO mangling).
+ // In that case, we have the same actual Symbol that can get two different
+ // GUID, leading to some invalid internalization. Workaround this by marking
+ // the GlobalRes external.
+
+ // FIXME: instead of this check, it would be desirable to compute GUIDs
+ // based on mangled name, but this requires an access to the Target Triple
+ // and would be relatively invasive on the codebase.
+ if (GlobalRes.IRName != Sym.getIRName()) {
+ GlobalRes.Partition = GlobalResolution::External;
+ GlobalRes.VisibleOutsideSummary = true;
+ }
+
// Set the partition to external if we know it is re-defined by the linker
// with -defsym or -wrap options, used elsewhere, e.g. it is visible to a
// regular object, is referenced from llvm.compiler.used/llvm.used, or was
@@ -694,11 +713,11 @@ handleNonPrevailingComdat(GlobalValue &GV,
if (!NonPrevailingComdats.count(C))
return;
- // Additionally need to drop externally visible global values from the comdat
- // to available_externally, so that there aren't multiply defined linker
- // errors.
- if (!GV.hasLocalLinkage())
- GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
+ // Additionally need to drop all global values from the comdat to
+ // available_externally, to satisfy the COMDAT requirement that all members
+ // are discarded as a unit. The non-local linkage global values avoid
+ // duplicate definition linker errors.
+ GV.setLinkage(GlobalValue::AvailableExternallyLinkage);
if (auto GO = dyn_cast<GlobalObject>(&GV))
GO->setComdat(nullptr);
@@ -893,9 +912,25 @@ Error LTO::linkRegularLTO(RegularLTOState::AddedModule Mod,
Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
const SymbolResolution *&ResI,
const SymbolResolution *ResE) {
+ const SymbolResolution *ResITmp = ResI;
+ for (const InputFile::Symbol &Sym : Syms) {
+ assert(ResITmp != ResE);
+ SymbolResolution Res = *ResITmp++;
+
+ if (!Sym.getIRName().empty()) {
+ auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
+ Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
+ if (Res.Prevailing)
+ ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
+ }
+ }
+
if (Error Err =
BM.readSummary(ThinLTO.CombinedIndex, BM.getModuleIdentifier(),
- ThinLTO.ModuleMap.size()))
+ ThinLTO.ModuleMap.size(), [&](GlobalValue::GUID GUID) {
+ return ThinLTO.PrevailingModuleForGUID[GUID] ==
+ BM.getModuleIdentifier();
+ }))
return Err;
for (const InputFile::Symbol &Sym : Syms) {
@@ -906,7 +941,8 @@ Error LTO::addThinLTO(BitcodeModule BM, ArrayRef<InputFile::Symbol> Syms,
auto GUID = GlobalValue::getGUID(GlobalValue::getGlobalIdentifier(
Sym.getIRName(), GlobalValue::ExternalLinkage, ""));
if (Res.Prevailing) {
- ThinLTO.PrevailingModuleForGUID[GUID] = BM.getModuleIdentifier();
+ assert(ThinLTO.PrevailingModuleForGUID[GUID] ==
+ BM.getModuleIdentifier());
// For linker redefined symbols (via --wrap or --defsym) we want to
// switch the linkage to `weak` to prevent IPOs from happening.
@@ -1154,7 +1190,7 @@ static const char *libcallRoutineNames[] = {
};
ArrayRef<const char*> LTO::getRuntimeLibcallSymbols() {
- return makeArrayRef(libcallRoutineNames);
+ return ArrayRef(libcallRoutineNames);
}
/// This class defines the interface to the ThinLTO backend.
@@ -1217,7 +1253,7 @@ class InProcessThinBackend : public ThinBackendProc {
std::set<GlobalValue::GUID> CfiFunctionDefs;
std::set<GlobalValue::GUID> CfiFunctionDecls;
- Optional<Error> Err;
+ std::optional<Error> Err;
std::mutex ErrMu;
bool ShouldEmitIndexFiles;
@@ -1278,7 +1314,7 @@ public:
computeLTOCacheKey(Key, Conf, CombinedIndex, ModuleID, ImportList,
ExportList, ResolvedODR, DefinedGlobals, CfiFunctionDefs,
CfiFunctionDecls);
- Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key);
+ Expected<AddStreamFn> CacheAddStreamOrErr = Cache(Task, Key, ModuleID);
if (Error Err = CacheAddStreamOrErr.takeError())
return Err;
AddStreamFn &CacheAddStream = *CacheAddStreamOrErr;
@@ -1436,6 +1472,7 @@ ThinBackend lto::createWriteIndexesThinBackend(
Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
const DenseSet<GlobalValue::GUID> &GUIDPreservedSymbols) {
+ ThinLTO.CombinedIndex.releaseTemporaryMemory();
timeTraceProfilerBegin("ThinLink", StringRef(""));
auto TimeTraceScopeExit = llvm::make_scope_exit([]() {
if (llvm::timeTraceProfilerEnabled())
@@ -1608,7 +1645,7 @@ Error LTO::runThinLTO(AddStreamFn AddStream, FileCache Cache,
Expected<std::unique_ptr<ToolOutputFile>> lto::setupLLVMOptimizationRemarks(
LLVMContext &Context, StringRef RemarksFilename, StringRef RemarksPasses,
StringRef RemarksFormat, bool RemarksWithHotness,
- Optional<uint64_t> RemarksHotnessThreshold, int Count) {
+ std::optional<uint64_t> RemarksHotnessThreshold, int Count) {
std::string Filename = std::string(RemarksFilename);
// For ThinLTO, file.opt.<format> becomes
// file.opt.<format>.thin.<num>.<format>.