Skip to content

Commit 06bb948

Browse files
committed
[ThinLTO] Restructure promotion / internalization decisions (NFC)
Restructures the combined index based promotion and internalization decision code so that it is a bit easier to follow. This is in preparation for a bugfix to this code that will modify one part of the logic.
1 parent 0b8c8bc commit 06bb948

File tree

1 file changed

+38
-25
lines changed

1 file changed

+38
-25
lines changed

llvm/lib/LTO/LTO.cpp

+38-25
Original file line numberDiff line numberDiff line change
@@ -446,39 +446,52 @@ void llvm::thinLTOResolvePrevailingInIndex(
446446
recordNewLinkage, GUIDPreservedSymbols);
447447
}
448448

449-
static bool isWeakObjectWithRWAccess(GlobalValueSummary *GVS) {
450-
if (auto *VarSummary = dyn_cast<GlobalVarSummary>(GVS->getBaseObject()))
451-
return !VarSummary->maybeReadOnly() && !VarSummary->maybeWriteOnly() &&
452-
(VarSummary->linkage() == GlobalValue::WeakODRLinkage ||
453-
VarSummary->linkage() == GlobalValue::LinkOnceODRLinkage);
454-
return false;
455-
}
456-
457449
static void thinLTOInternalizeAndPromoteGUID(
458450
ValueInfo VI, function_ref<bool(StringRef, ValueInfo)> isExported,
459451
function_ref<bool(GlobalValue::GUID, const GlobalValueSummary *)>
460452
isPrevailing) {
461453
for (auto &S : VI.getSummaryList()) {
454+
// First see if we need to promote an internal value because it is not
455+
// exported.
462456
if (isExported(S->modulePath(), VI)) {
463457
if (GlobalValue::isLocalLinkage(S->linkage()))
464458
S->setLinkage(GlobalValue::ExternalLinkage);
465-
} else if (EnableLTOInternalization &&
466-
// Ignore local and appending linkage values since the linker
467-
// doesn't resolve them.
468-
!GlobalValue::isLocalLinkage(S->linkage()) &&
469-
(!GlobalValue::isInterposableLinkage(S->linkage()) ||
470-
isPrevailing(VI.getGUID(), S.get())) &&
471-
S->linkage() != GlobalValue::AppendingLinkage &&
472-
// We can't internalize available_externally globals because this
473-
// can break function pointer equality.
474-
S->linkage() != GlobalValue::AvailableExternallyLinkage &&
475-
// Functions and read-only variables with linkonce_odr and
476-
// weak_odr linkage can be internalized. We can't internalize
477-
// linkonce_odr and weak_odr variables which are both modified
478-
// and read somewhere in the program because reads and writes
479-
// will become inconsistent.
480-
!isWeakObjectWithRWAccess(S.get()))
481-
S->setLinkage(GlobalValue::InternalLinkage);
459+
continue;
460+
}
461+
462+
// Otherwise, see if we can internalize.
463+
if (!EnableLTOInternalization)
464+
continue;
465+
466+
// Ignore local and appending linkage values since the linker
467+
// doesn't resolve them (and there is no need to internalize if this is
468+
// already internal).
469+
if (GlobalValue::isLocalLinkage(S->linkage()) ||
470+
S->linkage() == GlobalValue::AppendingLinkage)
471+
continue;
472+
473+
// We can't internalize available_externally globals because this
474+
// can break function pointer equality.
475+
if (S->linkage() == GlobalValue::AvailableExternallyLinkage)
476+
continue;
477+
478+
bool IsPrevailing = isPrevailing(VI.getGUID(), S.get());
479+
480+
if (GlobalValue::isInterposableLinkage(S->linkage()) && !IsPrevailing)
481+
continue;
482+
483+
// Functions and read-only variables with linkonce_odr and weak_odr linkage
484+
// can be internalized. We can't internalize linkonce_odr and weak_odr
485+
// variables which are both modified and read somewhere in the program
486+
// because reads and writes will become inconsistent.
487+
auto *VarSummary = dyn_cast<GlobalVarSummary>(S->getBaseObject());
488+
if (VarSummary && !VarSummary->maybeReadOnly() &&
489+
!VarSummary->maybeWriteOnly() &&
490+
(VarSummary->linkage() == GlobalValue::WeakODRLinkage ||
491+
VarSummary->linkage() == GlobalValue::LinkOnceODRLinkage))
492+
continue;
493+
494+
S->setLinkage(GlobalValue::InternalLinkage);
482495
}
483496
}
484497

0 commit comments

Comments
 (0)