2016-02-10 19:11:31 +01:00
|
|
|
//===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2016-02-10 19:11:31 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements the FunctionImportGlobalProcessing class, used
|
|
|
|
// to perform the necessary global value handling for function importing.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/Transforms/Utils/FunctionImportUtils.h"
|
2019-11-08 18:50:34 +01:00
|
|
|
#include "llvm/IR/Constants.h"
|
2016-04-27 16:19:38 +02:00
|
|
|
#include "llvm/IR/InstIterator.h"
|
2016-02-10 19:11:31 +01:00
|
|
|
using namespace llvm;
|
|
|
|
|
|
|
|
/// Checks if we should import SGV as a definition, otherwise import as a
|
|
|
|
/// declaration.
|
|
|
|
bool FunctionImportGlobalProcessing::doImportAsDefinition(
|
[ThinLTO] Avoid extra index lookup during promotion
Summary:
Pass down the already accessed ValueInfo to shouldPromoteLocalToGlobal,
to avoid an unnecessary extra index lookup.
Add some assertion checking to confirm we have a non-empty VI when
expected.
Also some misc cleanup, merging the two versions of
doImportAsDefinition, since one was only called by the other, and
unnecessarily passed in a member variable.
Reviewers: steven_wu, pcc, evgeny777
Reviewed By: evgeny777
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70337
2019-11-18 21:03:58 +01:00
|
|
|
const GlobalValue *SGV) {
|
|
|
|
if (!isPerformingImport())
|
|
|
|
return false;
|
2016-03-19 01:40:31 +01:00
|
|
|
|
|
|
|
// Only import the globals requested for importing.
|
2017-07-27 17:28:10 +02:00
|
|
|
if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
|
|
|
|
return false;
|
2017-07-27 17:09:06 +02:00
|
|
|
|
|
|
|
assert(!isa<GlobalAlias>(SGV) &&
|
|
|
|
"Unexpected global alias in the import list.");
|
|
|
|
|
2017-07-27 17:28:10 +02:00
|
|
|
// Otherwise yes.
|
|
|
|
return true;
|
2016-02-10 19:11:31 +01:00
|
|
|
}
|
|
|
|
|
2016-10-29 23:52:23 +02:00
|
|
|
bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
|
[ThinLTO] Avoid extra index lookup during promotion
Summary:
Pass down the already accessed ValueInfo to shouldPromoteLocalToGlobal,
to avoid an unnecessary extra index lookup.
Add some assertion checking to confirm we have a non-empty VI when
expected.
Also some misc cleanup, merging the two versions of
doImportAsDefinition, since one was only called by the other, and
unnecessarily passed in a member variable.
Reviewers: steven_wu, pcc, evgeny777
Reviewed By: evgeny777
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70337
2019-11-18 21:03:58 +01:00
|
|
|
const GlobalValue *SGV, ValueInfo VI) {
|
2016-02-10 19:11:31 +01:00
|
|
|
assert(SGV->hasLocalLinkage());
|
|
|
|
// Both the imported references and the original local variable must
|
|
|
|
// be promoted.
|
|
|
|
if (!isPerformingImport() && !isModuleExporting())
|
|
|
|
return false;
|
|
|
|
|
2016-11-14 20:21:41 +01:00
|
|
|
if (isPerformingImport()) {
|
2017-02-03 17:56:27 +01:00
|
|
|
assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
|
|
|
|
!isNonRenamableLocal(*SGV)) &&
|
2017-01-05 16:10:10 +01:00
|
|
|
"Attempting to promote non-renamable local");
|
2016-11-14 20:21:41 +01:00
|
|
|
// We don't know for sure yet if we are importing this value (as either
|
|
|
|
// a reference or a def), since we are simply walking all values in the
|
|
|
|
// module. But by necessity if we end up importing it and it is local,
|
|
|
|
// it must be promoted, so unconditionally promote all values in the
|
|
|
|
// importing module.
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2017-01-07 00:38:41 +01:00
|
|
|
// When exporting, consult the index. We can have more than one local
|
|
|
|
// with the same GUID, in the case of same-named locals in different but
|
|
|
|
// same-named source files that were compiled in their respective directories
|
|
|
|
// (so the source file name and resulting GUID is the same). Find the one
|
|
|
|
// in this module.
|
|
|
|
auto Summary = ImportIndex.findSummaryInModule(
|
[ThinLTO] Avoid extra index lookup during promotion
Summary:
Pass down the already accessed ValueInfo to shouldPromoteLocalToGlobal,
to avoid an unnecessary extra index lookup.
Add some assertion checking to confirm we have a non-empty VI when
expected.
Also some misc cleanup, merging the two versions of
doImportAsDefinition, since one was only called by the other, and
unnecessarily passed in a member variable.
Reviewers: steven_wu, pcc, evgeny777
Reviewed By: evgeny777
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70337
2019-11-18 21:03:58 +01:00
|
|
|
VI, SGV->getParent()->getModuleIdentifier());
|
2017-01-07 00:38:41 +01:00
|
|
|
assert(Summary && "Missing summary for global value when exporting");
|
|
|
|
auto Linkage = Summary->linkage();
|
2016-11-14 20:21:41 +01:00
|
|
|
if (!GlobalValue::isLocalLinkage(Linkage)) {
|
2017-01-05 15:32:16 +01:00
|
|
|
assert(!isNonRenamableLocal(*SGV) &&
|
|
|
|
"Attempting to promote non-renamable local");
|
2016-11-14 20:21:41 +01:00
|
|
|
return true;
|
2016-11-03 02:07:16 +01:00
|
|
|
}
|
2016-04-27 02:32:13 +02:00
|
|
|
|
2016-11-14 20:21:41 +01:00
|
|
|
return false;
|
2016-02-10 19:11:31 +01:00
|
|
|
}
|
|
|
|
|
2017-01-05 15:32:16 +01:00
|
|
|
#ifndef NDEBUG
|
|
|
|
bool FunctionImportGlobalProcessing::isNonRenamableLocal(
|
|
|
|
const GlobalValue &GV) const {
|
|
|
|
if (!GV.hasLocalLinkage())
|
|
|
|
return false;
|
|
|
|
// This needs to stay in sync with the logic in buildModuleSummaryIndex.
|
|
|
|
if (GV.hasSection())
|
|
|
|
return true;
|
|
|
|
if (Used.count(const_cast<GlobalValue *>(&GV)))
|
|
|
|
return true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
[ThinLTO] Promotion handling cleanup (NFC)
Summary:
Clean up the code that does GV promotion in the ThinLTO backends.
Specifically, we don't need to check whether we are importing since that
is already checked and handled correctly in shouldPromoteLocalToGlobal.
Simply call shouldPromoteLocalToGlobal, and if it returns true we are
guaranteed that we are promoting, whether or not we are importing (or in
the exporting module). This also makes the handling in getName()
consistent with that in getLinkage(), which checks the DoPromote parameter
regardless of whether we are importing or exporting.
Reviewers: steven_wu, pcc, evgeny777
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70327
2019-11-18 16:05:05 +01:00
|
|
|
std::string
|
|
|
|
FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
|
|
|
|
assert(SGV->hasLocalLinkage());
|
2016-02-10 19:11:31 +01:00
|
|
|
// For locals that must be promoted to global scope, ensure that
|
|
|
|
// the promoted name uniquely identifies the copy in the original module,
|
[ThinLTO] Promotion handling cleanup (NFC)
Summary:
Clean up the code that does GV promotion in the ThinLTO backends.
Specifically, we don't need to check whether we are importing since that
is already checked and handled correctly in shouldPromoteLocalToGlobal.
Simply call shouldPromoteLocalToGlobal, and if it returns true we are
guaranteed that we are promoting, whether or not we are importing (or in
the exporting module). This also makes the handling in getName()
consistent with that in getLinkage(), which checks the DoPromote parameter
regardless of whether we are importing or exporting.
Reviewers: steven_wu, pcc, evgeny777
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70327
2019-11-18 16:05:05 +01:00
|
|
|
// using the ID assigned during combined index creation.
|
|
|
|
return ModuleSummaryIndex::getGlobalNameForLocal(
|
|
|
|
SGV->getName(),
|
|
|
|
ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
|
2016-02-10 19:11:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
GlobalValue::LinkageTypes
|
2016-10-29 23:31:48 +02:00
|
|
|
FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
|
|
|
|
bool DoPromote) {
|
2016-02-10 19:11:31 +01:00
|
|
|
// Any local variable that is referenced by an exported function needs
|
|
|
|
// to be promoted to global scope. Since we don't currently know which
|
|
|
|
// functions reference which local variables/functions, we must treat
|
|
|
|
// all as potentially exported if this module is exporting anything.
|
|
|
|
if (isModuleExporting()) {
|
2016-10-29 23:31:48 +02:00
|
|
|
if (SGV->hasLocalLinkage() && DoPromote)
|
2016-02-10 19:11:31 +01:00
|
|
|
return GlobalValue::ExternalLinkage;
|
|
|
|
return SGV->getLinkage();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Otherwise, if we aren't importing, no linkage change is needed.
|
|
|
|
if (!isPerformingImport())
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
switch (SGV->getLinkage()) {
|
2017-07-27 17:09:06 +02:00
|
|
|
case GlobalValue::LinkOnceODRLinkage:
|
2016-02-10 19:11:31 +01:00
|
|
|
case GlobalValue::ExternalLinkage:
|
2017-07-27 17:09:06 +02:00
|
|
|
// External and linkonce definitions are converted to available_externally
|
2016-02-10 19:11:31 +01:00
|
|
|
// definitions upon import, so that they are available for inlining
|
|
|
|
// and/or optimization, but are turned into declarations later
|
|
|
|
// during the EliminateAvailableExternally pass.
|
2019-04-05 18:16:23 +02:00
|
|
|
if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
|
2016-02-10 19:11:31 +01:00
|
|
|
return GlobalValue::AvailableExternallyLinkage;
|
|
|
|
// An imported external declaration stays external.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
case GlobalValue::AvailableExternallyLinkage:
|
|
|
|
// An imported available_externally definition converts
|
|
|
|
// to external if imported as a declaration.
|
|
|
|
if (!doImportAsDefinition(SGV))
|
|
|
|
return GlobalValue::ExternalLinkage;
|
|
|
|
// An imported available_externally declaration stays that way.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
2018-11-28 16:16:35 +01:00
|
|
|
case GlobalValue::LinkOnceAnyLinkage:
|
2016-02-10 19:11:31 +01:00
|
|
|
case GlobalValue::WeakAnyLinkage:
|
2018-11-28 16:16:35 +01:00
|
|
|
// Can't import linkonce_any/weak_any definitions correctly, or we might
|
|
|
|
// change the program semantics, since the linker will pick the first
|
|
|
|
// linkonce_any/weak_any definition and importing would change the order
|
|
|
|
// they are seen by the linker. The module linking caller needs to enforce
|
|
|
|
// this.
|
2016-02-10 19:11:31 +01:00
|
|
|
assert(!doImportAsDefinition(SGV));
|
|
|
|
// If imported as a declaration, it becomes external_weak.
|
2016-04-20 06:18:11 +02:00
|
|
|
return SGV->getLinkage();
|
2016-02-10 19:11:31 +01:00
|
|
|
|
|
|
|
case GlobalValue::WeakODRLinkage:
|
|
|
|
// For weak_odr linkage, there is a guarantee that all copies will be
|
|
|
|
// equivalent, so the issue described above for weak_any does not exist,
|
|
|
|
// and the definition can be imported. It can be treated similarly
|
|
|
|
// to an imported externally visible global value.
|
2019-04-05 18:16:23 +02:00
|
|
|
if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
|
2016-02-10 19:11:31 +01:00
|
|
|
return GlobalValue::AvailableExternallyLinkage;
|
|
|
|
else
|
|
|
|
return GlobalValue::ExternalLinkage;
|
|
|
|
|
|
|
|
case GlobalValue::AppendingLinkage:
|
|
|
|
// It would be incorrect to import an appending linkage variable,
|
|
|
|
// since it would cause global constructors/destructors to be
|
|
|
|
// executed multiple times. This should have already been handled
|
|
|
|
// by linkIfNeeded, and we will assert in shouldLinkFromSource
|
|
|
|
// if we try to import, so we simply return AppendingLinkage.
|
|
|
|
return GlobalValue::AppendingLinkage;
|
|
|
|
|
|
|
|
case GlobalValue::InternalLinkage:
|
|
|
|
case GlobalValue::PrivateLinkage:
|
|
|
|
// If we are promoting the local to global scope, it is handled
|
|
|
|
// similarly to a normal externally visible global.
|
2016-10-29 23:31:48 +02:00
|
|
|
if (DoPromote) {
|
2019-04-05 18:16:23 +02:00
|
|
|
if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
|
2016-02-10 19:11:31 +01:00
|
|
|
return GlobalValue::AvailableExternallyLinkage;
|
|
|
|
else
|
|
|
|
return GlobalValue::ExternalLinkage;
|
|
|
|
}
|
|
|
|
// A non-promoted imported local definition stays local.
|
|
|
|
// The ThinLTO pass will eventually force-import their definitions.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
case GlobalValue::ExternalWeakLinkage:
|
|
|
|
// External weak doesn't apply to definitions, must be a declaration.
|
|
|
|
assert(!doImportAsDefinition(SGV));
|
|
|
|
// Linkage stays external_weak.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
|
|
|
|
case GlobalValue::CommonLinkage:
|
|
|
|
// Linkage stays common on definitions.
|
|
|
|
// The ThinLTO pass will eventually force-import their definitions.
|
|
|
|
return SGV->getLinkage();
|
|
|
|
}
|
|
|
|
|
|
|
|
llvm_unreachable("unknown linkage type");
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
|
2017-11-04 18:04:39 +01:00
|
|
|
|
2018-11-16 08:08:00 +01:00
|
|
|
ValueInfo VI;
|
2017-11-04 18:04:39 +01:00
|
|
|
if (GV.hasName()) {
|
2018-11-16 08:08:00 +01:00
|
|
|
VI = ImportIndex.getValueInfo(GV.getGUID());
|
2018-12-13 20:54:27 +01:00
|
|
|
// Set synthetic function entry counts.
|
|
|
|
if (VI && ImportIndex.hasSyntheticEntryCounts()) {
|
|
|
|
if (Function *F = dyn_cast<Function>(&GV)) {
|
|
|
|
if (!F->isDeclaration()) {
|
|
|
|
for (auto &S : VI.getSummaryList()) {
|
2019-09-27 17:49:19 +02:00
|
|
|
auto *FS = cast<FunctionSummary>(S->getBaseObject());
|
2018-12-13 20:54:27 +01:00
|
|
|
if (FS->modulePath() == M.getModuleIdentifier()) {
|
|
|
|
F->setEntryCount(Function::ProfileCount(FS->entryCount(),
|
|
|
|
Function::PCT_Synthetic));
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-11-04 18:04:39 +01:00
|
|
|
}
|
|
|
|
|
[ThinLTO] Avoid extra index lookup during promotion
Summary:
Pass down the already accessed ValueInfo to shouldPromoteLocalToGlobal,
to avoid an unnecessary extra index lookup.
Add some assertion checking to confirm we have a non-empty VI when
expected.
Also some misc cleanup, merging the two versions of
doImportAsDefinition, since one was only called by the other, and
unnecessarily passed in a member variable.
Reviewers: steven_wu, pcc, evgeny777
Reviewed By: evgeny777
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70337
2019-11-18 21:03:58 +01:00
|
|
|
// We should always have a ValueInfo (i.e. GV in index) for definitions when
|
|
|
|
// we are exporting, and also when importing that value.
|
|
|
|
assert(VI || GV.isDeclaration() ||
|
|
|
|
(isPerformingImport() && !doImportAsDefinition(&GV)));
|
|
|
|
|
2019-07-05 17:25:05 +02:00
|
|
|
// Mark read/write-only variables which can be imported with specific
|
|
|
|
// attribute. We can't internalize them now because IRMover will fail
|
|
|
|
// to link variable definitions to their external declarations during
|
|
|
|
// ThinLTO import. We'll internalize read-only variables later, after
|
|
|
|
// import is finished. See internalizeGVsAfterImport.
|
2018-11-16 08:08:00 +01:00
|
|
|
//
|
|
|
|
// If global value dead stripping is not enabled in summary then
|
|
|
|
// propagateConstants hasn't been run. We can't internalize GV
|
|
|
|
// in such case.
|
2019-11-07 13:13:35 +01:00
|
|
|
if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
|
2019-11-01 18:31:02 +01:00
|
|
|
if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
|
|
|
|
// We can have more than one local with the same GUID, in the case of
|
|
|
|
// same-named locals in different but same-named source files that were
|
|
|
|
// compiled in their respective directories (so the source file name
|
|
|
|
// and resulting GUID is the same). Find the one in this module.
|
|
|
|
// Handle the case where there is no summary found in this module. That
|
|
|
|
// can happen in the distributed ThinLTO backend, because the index only
|
|
|
|
// contains summaries from the source modules if they are being imported.
|
|
|
|
// We might have a non-null VI and get here even in that case if the name
|
|
|
|
// matches one in this module (e.g. weak or appending linkage).
|
2019-11-15 14:13:19 +01:00
|
|
|
auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
|
|
|
|
ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
|
2019-11-08 18:50:34 +01:00
|
|
|
if (GVS &&
|
|
|
|
(ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
|
2019-11-01 18:31:02 +01:00
|
|
|
V->addAttribute("thinlto-internalize");
|
2019-11-15 14:13:19 +01:00
|
|
|
// Objects referenced by writeonly GV initializer should not be
|
2019-11-08 18:50:34 +01:00
|
|
|
// promoted, because there is no any kind of read access to them
|
|
|
|
// on behalf of this writeonly GV. To avoid promotion we convert
|
|
|
|
// GV initializer to 'zeroinitializer'. This effectively drops
|
|
|
|
// references in IR module (not in combined index), so we can
|
|
|
|
// ignore them when computing import. We do not export references
|
|
|
|
// of writeonly object. See computeImportForReferencedGlobals
|
[ThinLTO] Fix importing of writeonly variables in distributed ThinLTO
Summary:
D69561/dde5893 enabled importing of readonly variables with references,
however, it introduced a bug relating to importing/internalization of
writeonly variables with references.
A fix for this was added in D70006/7f92d66. But this didn't work in
distributed ThinLTO mode. The reason is that the fix (importing the
writeonly var with a zeroinitializer) was only applied when there were
references on the writeonly var summary. In distributed ThinLTO mode,
where we only have a small slice of the index, we will not have the
references on the importing side if we are not importing those
referenced values. Rather than changing this handshaking (which will
require a lot of other changes, since that's how we know what to import
in the distributed backend clang invocation), we can simply always give
the writeonly variable a zero initializer.
Reviewers: evgeny777, steven_wu
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70977
2019-12-03 21:22:51 +01:00
|
|
|
if (ImportIndex.isWriteOnly(GVS))
|
2019-11-08 18:50:34 +01:00
|
|
|
V->setInitializer(Constant::getNullValue(V->getValueType()));
|
|
|
|
}
|
2019-11-01 18:31:02 +01:00
|
|
|
}
|
2018-11-16 08:08:00 +01:00
|
|
|
}
|
|
|
|
|
[ThinLTO] Avoid extra index lookup during promotion
Summary:
Pass down the already accessed ValueInfo to shouldPromoteLocalToGlobal,
to avoid an unnecessary extra index lookup.
Add some assertion checking to confirm we have a non-empty VI when
expected.
Also some misc cleanup, merging the two versions of
doImportAsDefinition, since one was only called by the other, and
unnecessarily passed in a member variable.
Reviewers: steven_wu, pcc, evgeny777
Reviewed By: evgeny777
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, arphaman, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70337
2019-11-18 21:03:58 +01:00
|
|
|
if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) {
|
2019-01-31 18:18:11 +01:00
|
|
|
// Save the original name string before we rename GV below.
|
|
|
|
auto Name = GV.getName().str();
|
[ThinLTO] Promotion handling cleanup (NFC)
Summary:
Clean up the code that does GV promotion in the ThinLTO backends.
Specifically, we don't need to check whether we are importing since that
is already checked and handled correctly in shouldPromoteLocalToGlobal.
Simply call shouldPromoteLocalToGlobal, and if it returns true we are
guaranteed that we are promoting, whether or not we are importing (or in
the exporting module). This also makes the handling in getName()
consistent with that in getLinkage(), which checks the DoPromote parameter
regardless of whether we are importing or exporting.
Reviewers: steven_wu, pcc, evgeny777
Subscribers: mehdi_amini, inglorion, hiraditya, dexonsmith, llvm-commits
Tags: #llvm
Differential Revision: https://reviews.llvm.org/D70327
2019-11-18 16:05:05 +01:00
|
|
|
GV.setName(getPromotedName(&GV));
|
|
|
|
GV.setLinkage(getLinkage(&GV, /* DoPromote */ true));
|
|
|
|
assert(!GV.hasLocalLinkage());
|
|
|
|
GV.setVisibility(GlobalValue::HiddenVisibility);
|
2019-01-31 18:18:11 +01:00
|
|
|
|
|
|
|
// If we are renaming a COMDAT leader, ensure that we record the COMDAT
|
|
|
|
// for later renaming as well. This is required for COFF.
|
|
|
|
if (const auto *C = GV.getComdat())
|
|
|
|
if (C->getName() == Name)
|
|
|
|
RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
|
2016-02-10 19:11:31 +01:00
|
|
|
} else
|
2016-10-29 23:31:48 +02:00
|
|
|
GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
|
2016-02-10 19:11:31 +01:00
|
|
|
|
2020-02-16 02:23:18 +01:00
|
|
|
// When ClearDSOLocalOnDeclarations is true, clear dso_local if GV is
|
|
|
|
// converted to a declaration, to disable direct access. Don't do this if GV
|
|
|
|
// is implicitly dso_local due to a non-default visibility.
|
|
|
|
if (ClearDSOLocalOnDeclarations && GV.isDeclarationForLinker() &&
|
|
|
|
!GV.isImplicitDSOLocal()) {
|
|
|
|
GV.setDSOLocal(false);
|
|
|
|
} else if (VI && VI.isDSOLocal()) {
|
|
|
|
// If all summaries are dso_local, symbol gets resolved to a known local
|
|
|
|
// definition.
|
|
|
|
GV.setDSOLocal(true);
|
|
|
|
if (GV.hasDLLImportStorageClass())
|
|
|
|
GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
|
|
|
|
}
|
|
|
|
|
2016-02-10 19:11:31 +01:00
|
|
|
// Remove functions imported as available externally defs from comdats,
|
|
|
|
// as this is a declaration for the linker, and will be dropped eventually.
|
|
|
|
// It is illegal for comdats to contain declarations.
|
2018-11-16 08:08:00 +01:00
|
|
|
auto *GO = dyn_cast<GlobalObject>(&GV);
|
2016-02-10 19:11:31 +01:00
|
|
|
if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
|
|
|
|
// The IRMover should not have placed any imported declarations in
|
|
|
|
// a comdat, so the only declaration that should be in a comdat
|
|
|
|
// at this point would be a definition imported as available_externally.
|
|
|
|
assert(GO->hasAvailableExternallyLinkage() &&
|
|
|
|
"Expected comdat on definition (possibly available external)");
|
|
|
|
GO->setComdat(nullptr);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
|
|
|
|
for (GlobalVariable &GV : M.globals())
|
|
|
|
processGlobalForThinLTO(GV);
|
|
|
|
for (Function &SF : M)
|
|
|
|
processGlobalForThinLTO(SF);
|
|
|
|
for (GlobalAlias &GA : M.aliases())
|
|
|
|
processGlobalForThinLTO(GA);
|
2019-01-31 18:18:11 +01:00
|
|
|
|
|
|
|
// Replace any COMDATS that required renaming (because the COMDAT leader was
|
|
|
|
// promoted and renamed).
|
|
|
|
if (!RenamedComdats.empty())
|
|
|
|
for (auto &GO : M.global_objects())
|
|
|
|
if (auto *C = GO.getComdat()) {
|
|
|
|
auto Replacement = RenamedComdats.find(C);
|
|
|
|
if (Replacement != RenamedComdats.end())
|
|
|
|
GO.setComdat(Replacement->second);
|
|
|
|
}
|
2016-02-10 19:11:31 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
bool FunctionImportGlobalProcessing::run() {
|
|
|
|
processGlobalsForThinLTO();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-02-03 17:56:27 +01:00
|
|
|
bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
|
2020-02-16 02:23:18 +01:00
|
|
|
bool ClearDSOLocalOnDeclarations,
|
2017-02-03 17:56:27 +01:00
|
|
|
SetVector<GlobalValue *> *GlobalsToImport) {
|
2020-02-16 02:23:18 +01:00
|
|
|
FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport,
|
|
|
|
ClearDSOLocalOnDeclarations);
|
2016-02-10 19:11:31 +01:00
|
|
|
return ThinLTOProcessing.run();
|
|
|
|
}
|