106f32e7eSjoerg //===- lib/Transforms/Utils/FunctionImportUtils.cpp - Importing utilities -===//
206f32e7eSjoerg //
306f32e7eSjoerg // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
406f32e7eSjoerg // See https://llvm.org/LICENSE.txt for license information.
506f32e7eSjoerg // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
606f32e7eSjoerg //
706f32e7eSjoerg //===----------------------------------------------------------------------===//
806f32e7eSjoerg //
906f32e7eSjoerg // This file implements the FunctionImportGlobalProcessing class, used
1006f32e7eSjoerg // to perform the necessary global value handling for function importing.
1106f32e7eSjoerg //
1206f32e7eSjoerg //===----------------------------------------------------------------------===//
1306f32e7eSjoerg
1406f32e7eSjoerg #include "llvm/Transforms/Utils/FunctionImportUtils.h"
15*da58b97aSjoerg #include "llvm/IR/Constants.h"
1606f32e7eSjoerg #include "llvm/IR/InstIterator.h"
1706f32e7eSjoerg using namespace llvm;
1806f32e7eSjoerg
1906f32e7eSjoerg /// Checks if we should import SGV as a definition, otherwise import as a
2006f32e7eSjoerg /// declaration.
doImportAsDefinition(const GlobalValue * SGV)2106f32e7eSjoerg bool FunctionImportGlobalProcessing::doImportAsDefinition(
22*da58b97aSjoerg const GlobalValue *SGV) {
23*da58b97aSjoerg if (!isPerformingImport())
24*da58b97aSjoerg return false;
2506f32e7eSjoerg
2606f32e7eSjoerg // Only import the globals requested for importing.
2706f32e7eSjoerg if (!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)))
2806f32e7eSjoerg return false;
2906f32e7eSjoerg
3006f32e7eSjoerg assert(!isa<GlobalAlias>(SGV) &&
3106f32e7eSjoerg "Unexpected global alias in the import list.");
3206f32e7eSjoerg
3306f32e7eSjoerg // Otherwise yes.
3406f32e7eSjoerg return true;
3506f32e7eSjoerg }
3606f32e7eSjoerg
shouldPromoteLocalToGlobal(const GlobalValue * SGV,ValueInfo VI)3706f32e7eSjoerg bool FunctionImportGlobalProcessing::shouldPromoteLocalToGlobal(
38*da58b97aSjoerg const GlobalValue *SGV, ValueInfo VI) {
3906f32e7eSjoerg assert(SGV->hasLocalLinkage());
4006f32e7eSjoerg // Both the imported references and the original local variable must
4106f32e7eSjoerg // be promoted.
4206f32e7eSjoerg if (!isPerformingImport() && !isModuleExporting())
4306f32e7eSjoerg return false;
4406f32e7eSjoerg
4506f32e7eSjoerg if (isPerformingImport()) {
4606f32e7eSjoerg assert((!GlobalsToImport->count(const_cast<GlobalValue *>(SGV)) ||
4706f32e7eSjoerg !isNonRenamableLocal(*SGV)) &&
4806f32e7eSjoerg "Attempting to promote non-renamable local");
4906f32e7eSjoerg // We don't know for sure yet if we are importing this value (as either
5006f32e7eSjoerg // a reference or a def), since we are simply walking all values in the
5106f32e7eSjoerg // module. But by necessity if we end up importing it and it is local,
5206f32e7eSjoerg // it must be promoted, so unconditionally promote all values in the
5306f32e7eSjoerg // importing module.
5406f32e7eSjoerg return true;
5506f32e7eSjoerg }
5606f32e7eSjoerg
5706f32e7eSjoerg // When exporting, consult the index. We can have more than one local
5806f32e7eSjoerg // with the same GUID, in the case of same-named locals in different but
5906f32e7eSjoerg // same-named source files that were compiled in their respective directories
6006f32e7eSjoerg // (so the source file name and resulting GUID is the same). Find the one
6106f32e7eSjoerg // in this module.
6206f32e7eSjoerg auto Summary = ImportIndex.findSummaryInModule(
63*da58b97aSjoerg VI, SGV->getParent()->getModuleIdentifier());
6406f32e7eSjoerg assert(Summary && "Missing summary for global value when exporting");
6506f32e7eSjoerg auto Linkage = Summary->linkage();
6606f32e7eSjoerg if (!GlobalValue::isLocalLinkage(Linkage)) {
6706f32e7eSjoerg assert(!isNonRenamableLocal(*SGV) &&
6806f32e7eSjoerg "Attempting to promote non-renamable local");
6906f32e7eSjoerg return true;
7006f32e7eSjoerg }
7106f32e7eSjoerg
7206f32e7eSjoerg return false;
7306f32e7eSjoerg }
7406f32e7eSjoerg
7506f32e7eSjoerg #ifndef NDEBUG
isNonRenamableLocal(const GlobalValue & GV) const7606f32e7eSjoerg bool FunctionImportGlobalProcessing::isNonRenamableLocal(
7706f32e7eSjoerg const GlobalValue &GV) const {
7806f32e7eSjoerg if (!GV.hasLocalLinkage())
7906f32e7eSjoerg return false;
8006f32e7eSjoerg // This needs to stay in sync with the logic in buildModuleSummaryIndex.
8106f32e7eSjoerg if (GV.hasSection())
8206f32e7eSjoerg return true;
8306f32e7eSjoerg if (Used.count(const_cast<GlobalValue *>(&GV)))
8406f32e7eSjoerg return true;
8506f32e7eSjoerg return false;
8606f32e7eSjoerg }
8706f32e7eSjoerg #endif
8806f32e7eSjoerg
89*da58b97aSjoerg std::string
getPromotedName(const GlobalValue * SGV)90*da58b97aSjoerg FunctionImportGlobalProcessing::getPromotedName(const GlobalValue *SGV) {
91*da58b97aSjoerg assert(SGV->hasLocalLinkage());
9206f32e7eSjoerg // For locals that must be promoted to global scope, ensure that
9306f32e7eSjoerg // the promoted name uniquely identifies the copy in the original module,
94*da58b97aSjoerg // using the ID assigned during combined index creation.
9506f32e7eSjoerg return ModuleSummaryIndex::getGlobalNameForLocal(
9606f32e7eSjoerg SGV->getName(),
9706f32e7eSjoerg ImportIndex.getModuleHash(SGV->getParent()->getModuleIdentifier()));
9806f32e7eSjoerg }
9906f32e7eSjoerg
10006f32e7eSjoerg GlobalValue::LinkageTypes
getLinkage(const GlobalValue * SGV,bool DoPromote)10106f32e7eSjoerg FunctionImportGlobalProcessing::getLinkage(const GlobalValue *SGV,
10206f32e7eSjoerg bool DoPromote) {
10306f32e7eSjoerg // Any local variable that is referenced by an exported function needs
10406f32e7eSjoerg // to be promoted to global scope. Since we don't currently know which
10506f32e7eSjoerg // functions reference which local variables/functions, we must treat
10606f32e7eSjoerg // all as potentially exported if this module is exporting anything.
10706f32e7eSjoerg if (isModuleExporting()) {
10806f32e7eSjoerg if (SGV->hasLocalLinkage() && DoPromote)
10906f32e7eSjoerg return GlobalValue::ExternalLinkage;
11006f32e7eSjoerg return SGV->getLinkage();
11106f32e7eSjoerg }
11206f32e7eSjoerg
11306f32e7eSjoerg // Otherwise, if we aren't importing, no linkage change is needed.
11406f32e7eSjoerg if (!isPerformingImport())
11506f32e7eSjoerg return SGV->getLinkage();
11606f32e7eSjoerg
11706f32e7eSjoerg switch (SGV->getLinkage()) {
11806f32e7eSjoerg case GlobalValue::LinkOnceODRLinkage:
11906f32e7eSjoerg case GlobalValue::ExternalLinkage:
12006f32e7eSjoerg // External and linkonce definitions are converted to available_externally
12106f32e7eSjoerg // definitions upon import, so that they are available for inlining
12206f32e7eSjoerg // and/or optimization, but are turned into declarations later
12306f32e7eSjoerg // during the EliminateAvailableExternally pass.
12406f32e7eSjoerg if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
12506f32e7eSjoerg return GlobalValue::AvailableExternallyLinkage;
12606f32e7eSjoerg // An imported external declaration stays external.
12706f32e7eSjoerg return SGV->getLinkage();
12806f32e7eSjoerg
12906f32e7eSjoerg case GlobalValue::AvailableExternallyLinkage:
13006f32e7eSjoerg // An imported available_externally definition converts
13106f32e7eSjoerg // to external if imported as a declaration.
13206f32e7eSjoerg if (!doImportAsDefinition(SGV))
13306f32e7eSjoerg return GlobalValue::ExternalLinkage;
13406f32e7eSjoerg // An imported available_externally declaration stays that way.
13506f32e7eSjoerg return SGV->getLinkage();
13606f32e7eSjoerg
13706f32e7eSjoerg case GlobalValue::LinkOnceAnyLinkage:
13806f32e7eSjoerg case GlobalValue::WeakAnyLinkage:
13906f32e7eSjoerg // Can't import linkonce_any/weak_any definitions correctly, or we might
14006f32e7eSjoerg // change the program semantics, since the linker will pick the first
14106f32e7eSjoerg // linkonce_any/weak_any definition and importing would change the order
14206f32e7eSjoerg // they are seen by the linker. The module linking caller needs to enforce
14306f32e7eSjoerg // this.
14406f32e7eSjoerg assert(!doImportAsDefinition(SGV));
14506f32e7eSjoerg // If imported as a declaration, it becomes external_weak.
14606f32e7eSjoerg return SGV->getLinkage();
14706f32e7eSjoerg
14806f32e7eSjoerg case GlobalValue::WeakODRLinkage:
14906f32e7eSjoerg // For weak_odr linkage, there is a guarantee that all copies will be
15006f32e7eSjoerg // equivalent, so the issue described above for weak_any does not exist,
15106f32e7eSjoerg // and the definition can be imported. It can be treated similarly
15206f32e7eSjoerg // to an imported externally visible global value.
15306f32e7eSjoerg if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
15406f32e7eSjoerg return GlobalValue::AvailableExternallyLinkage;
15506f32e7eSjoerg else
15606f32e7eSjoerg return GlobalValue::ExternalLinkage;
15706f32e7eSjoerg
15806f32e7eSjoerg case GlobalValue::AppendingLinkage:
15906f32e7eSjoerg // It would be incorrect to import an appending linkage variable,
16006f32e7eSjoerg // since it would cause global constructors/destructors to be
16106f32e7eSjoerg // executed multiple times. This should have already been handled
16206f32e7eSjoerg // by linkIfNeeded, and we will assert in shouldLinkFromSource
16306f32e7eSjoerg // if we try to import, so we simply return AppendingLinkage.
16406f32e7eSjoerg return GlobalValue::AppendingLinkage;
16506f32e7eSjoerg
16606f32e7eSjoerg case GlobalValue::InternalLinkage:
16706f32e7eSjoerg case GlobalValue::PrivateLinkage:
16806f32e7eSjoerg // If we are promoting the local to global scope, it is handled
16906f32e7eSjoerg // similarly to a normal externally visible global.
17006f32e7eSjoerg if (DoPromote) {
17106f32e7eSjoerg if (doImportAsDefinition(SGV) && !isa<GlobalAlias>(SGV))
17206f32e7eSjoerg return GlobalValue::AvailableExternallyLinkage;
17306f32e7eSjoerg else
17406f32e7eSjoerg return GlobalValue::ExternalLinkage;
17506f32e7eSjoerg }
17606f32e7eSjoerg // A non-promoted imported local definition stays local.
17706f32e7eSjoerg // The ThinLTO pass will eventually force-import their definitions.
17806f32e7eSjoerg return SGV->getLinkage();
17906f32e7eSjoerg
18006f32e7eSjoerg case GlobalValue::ExternalWeakLinkage:
18106f32e7eSjoerg // External weak doesn't apply to definitions, must be a declaration.
18206f32e7eSjoerg assert(!doImportAsDefinition(SGV));
18306f32e7eSjoerg // Linkage stays external_weak.
18406f32e7eSjoerg return SGV->getLinkage();
18506f32e7eSjoerg
18606f32e7eSjoerg case GlobalValue::CommonLinkage:
18706f32e7eSjoerg // Linkage stays common on definitions.
18806f32e7eSjoerg // The ThinLTO pass will eventually force-import their definitions.
18906f32e7eSjoerg return SGV->getLinkage();
19006f32e7eSjoerg }
19106f32e7eSjoerg
19206f32e7eSjoerg llvm_unreachable("unknown linkage type");
19306f32e7eSjoerg }
19406f32e7eSjoerg
processGlobalForThinLTO(GlobalValue & GV)19506f32e7eSjoerg void FunctionImportGlobalProcessing::processGlobalForThinLTO(GlobalValue &GV) {
19606f32e7eSjoerg
19706f32e7eSjoerg ValueInfo VI;
19806f32e7eSjoerg if (GV.hasName()) {
19906f32e7eSjoerg VI = ImportIndex.getValueInfo(GV.getGUID());
20006f32e7eSjoerg // Set synthetic function entry counts.
20106f32e7eSjoerg if (VI && ImportIndex.hasSyntheticEntryCounts()) {
20206f32e7eSjoerg if (Function *F = dyn_cast<Function>(&GV)) {
20306f32e7eSjoerg if (!F->isDeclaration()) {
20406f32e7eSjoerg for (auto &S : VI.getSummaryList()) {
20506f32e7eSjoerg auto *FS = cast<FunctionSummary>(S->getBaseObject());
20606f32e7eSjoerg if (FS->modulePath() == M.getModuleIdentifier()) {
20706f32e7eSjoerg F->setEntryCount(Function::ProfileCount(FS->entryCount(),
20806f32e7eSjoerg Function::PCT_Synthetic));
20906f32e7eSjoerg break;
21006f32e7eSjoerg }
21106f32e7eSjoerg }
21206f32e7eSjoerg }
21306f32e7eSjoerg }
21406f32e7eSjoerg }
21506f32e7eSjoerg }
216*da58b97aSjoerg
217*da58b97aSjoerg // We should always have a ValueInfo (i.e. GV in index) for definitions when
218*da58b97aSjoerg // we are exporting, and also when importing that value.
219*da58b97aSjoerg assert(VI || GV.isDeclaration() ||
220*da58b97aSjoerg (isPerformingImport() && !doImportAsDefinition(&GV)));
22106f32e7eSjoerg
22206f32e7eSjoerg // Mark read/write-only variables which can be imported with specific
22306f32e7eSjoerg // attribute. We can't internalize them now because IRMover will fail
22406f32e7eSjoerg // to link variable definitions to their external declarations during
22506f32e7eSjoerg // ThinLTO import. We'll internalize read-only variables later, after
22606f32e7eSjoerg // import is finished. See internalizeGVsAfterImport.
22706f32e7eSjoerg //
22806f32e7eSjoerg // If global value dead stripping is not enabled in summary then
22906f32e7eSjoerg // propagateConstants hasn't been run. We can't internalize GV
23006f32e7eSjoerg // in such case.
231*da58b97aSjoerg if (!GV.isDeclaration() && VI && ImportIndex.withAttributePropagation()) {
23206f32e7eSjoerg if (GlobalVariable *V = dyn_cast<GlobalVariable>(&GV)) {
23306f32e7eSjoerg // We can have more than one local with the same GUID, in the case of
23406f32e7eSjoerg // same-named locals in different but same-named source files that were
23506f32e7eSjoerg // compiled in their respective directories (so the source file name
23606f32e7eSjoerg // and resulting GUID is the same). Find the one in this module.
237*da58b97aSjoerg // Handle the case where there is no summary found in this module. That
238*da58b97aSjoerg // can happen in the distributed ThinLTO backend, because the index only
239*da58b97aSjoerg // contains summaries from the source modules if they are being imported.
240*da58b97aSjoerg // We might have a non-null VI and get here even in that case if the name
241*da58b97aSjoerg // matches one in this module (e.g. weak or appending linkage).
242*da58b97aSjoerg auto *GVS = dyn_cast_or_null<GlobalVarSummary>(
24306f32e7eSjoerg ImportIndex.findSummaryInModule(VI, M.getModuleIdentifier()));
244*da58b97aSjoerg if (GVS &&
245*da58b97aSjoerg (ImportIndex.isReadOnly(GVS) || ImportIndex.isWriteOnly(GVS))) {
24606f32e7eSjoerg V->addAttribute("thinlto-internalize");
247*da58b97aSjoerg // Objects referenced by writeonly GV initializer should not be
248*da58b97aSjoerg // promoted, because there is no any kind of read access to them
249*da58b97aSjoerg // on behalf of this writeonly GV. To avoid promotion we convert
250*da58b97aSjoerg // GV initializer to 'zeroinitializer'. This effectively drops
251*da58b97aSjoerg // references in IR module (not in combined index), so we can
252*da58b97aSjoerg // ignore them when computing import. We do not export references
253*da58b97aSjoerg // of writeonly object. See computeImportForReferencedGlobals
254*da58b97aSjoerg if (ImportIndex.isWriteOnly(GVS))
255*da58b97aSjoerg V->setInitializer(Constant::getNullValue(V->getValueType()));
256*da58b97aSjoerg }
25706f32e7eSjoerg }
25806f32e7eSjoerg }
25906f32e7eSjoerg
260*da58b97aSjoerg if (GV.hasLocalLinkage() && shouldPromoteLocalToGlobal(&GV, VI)) {
26106f32e7eSjoerg // Save the original name string before we rename GV below.
26206f32e7eSjoerg auto Name = GV.getName().str();
263*da58b97aSjoerg GV.setName(getPromotedName(&GV));
264*da58b97aSjoerg GV.setLinkage(getLinkage(&GV, /* DoPromote */ true));
265*da58b97aSjoerg assert(!GV.hasLocalLinkage());
26606f32e7eSjoerg GV.setVisibility(GlobalValue::HiddenVisibility);
26706f32e7eSjoerg
26806f32e7eSjoerg // If we are renaming a COMDAT leader, ensure that we record the COMDAT
26906f32e7eSjoerg // for later renaming as well. This is required for COFF.
27006f32e7eSjoerg if (const auto *C = GV.getComdat())
27106f32e7eSjoerg if (C->getName() == Name)
27206f32e7eSjoerg RenamedComdats.try_emplace(C, M.getOrInsertComdat(GV.getName()));
27306f32e7eSjoerg } else
27406f32e7eSjoerg GV.setLinkage(getLinkage(&GV, /* DoPromote */ false));
27506f32e7eSjoerg
276*da58b97aSjoerg // When ClearDSOLocalOnDeclarations is true, clear dso_local if GV is
277*da58b97aSjoerg // converted to a declaration, to disable direct access. Don't do this if GV
278*da58b97aSjoerg // is implicitly dso_local due to a non-default visibility.
279*da58b97aSjoerg if (ClearDSOLocalOnDeclarations && GV.isDeclarationForLinker() &&
280*da58b97aSjoerg !GV.isImplicitDSOLocal()) {
281*da58b97aSjoerg GV.setDSOLocal(false);
282*da58b97aSjoerg } else if (VI && VI.isDSOLocal(ImportIndex.withDSOLocalPropagation())) {
283*da58b97aSjoerg // If all summaries are dso_local, symbol gets resolved to a known local
284*da58b97aSjoerg // definition.
285*da58b97aSjoerg GV.setDSOLocal(true);
286*da58b97aSjoerg if (GV.hasDLLImportStorageClass())
287*da58b97aSjoerg GV.setDLLStorageClass(GlobalValue::DefaultStorageClass);
288*da58b97aSjoerg }
289*da58b97aSjoerg
29006f32e7eSjoerg // Remove functions imported as available externally defs from comdats,
29106f32e7eSjoerg // as this is a declaration for the linker, and will be dropped eventually.
29206f32e7eSjoerg // It is illegal for comdats to contain declarations.
29306f32e7eSjoerg auto *GO = dyn_cast<GlobalObject>(&GV);
29406f32e7eSjoerg if (GO && GO->isDeclarationForLinker() && GO->hasComdat()) {
29506f32e7eSjoerg // The IRMover should not have placed any imported declarations in
29606f32e7eSjoerg // a comdat, so the only declaration that should be in a comdat
29706f32e7eSjoerg // at this point would be a definition imported as available_externally.
29806f32e7eSjoerg assert(GO->hasAvailableExternallyLinkage() &&
29906f32e7eSjoerg "Expected comdat on definition (possibly available external)");
30006f32e7eSjoerg GO->setComdat(nullptr);
30106f32e7eSjoerg }
30206f32e7eSjoerg }
30306f32e7eSjoerg
processGlobalsForThinLTO()30406f32e7eSjoerg void FunctionImportGlobalProcessing::processGlobalsForThinLTO() {
30506f32e7eSjoerg for (GlobalVariable &GV : M.globals())
30606f32e7eSjoerg processGlobalForThinLTO(GV);
30706f32e7eSjoerg for (Function &SF : M)
30806f32e7eSjoerg processGlobalForThinLTO(SF);
30906f32e7eSjoerg for (GlobalAlias &GA : M.aliases())
31006f32e7eSjoerg processGlobalForThinLTO(GA);
31106f32e7eSjoerg
31206f32e7eSjoerg // Replace any COMDATS that required renaming (because the COMDAT leader was
31306f32e7eSjoerg // promoted and renamed).
31406f32e7eSjoerg if (!RenamedComdats.empty())
31506f32e7eSjoerg for (auto &GO : M.global_objects())
31606f32e7eSjoerg if (auto *C = GO.getComdat()) {
31706f32e7eSjoerg auto Replacement = RenamedComdats.find(C);
31806f32e7eSjoerg if (Replacement != RenamedComdats.end())
31906f32e7eSjoerg GO.setComdat(Replacement->second);
32006f32e7eSjoerg }
32106f32e7eSjoerg }
32206f32e7eSjoerg
run()32306f32e7eSjoerg bool FunctionImportGlobalProcessing::run() {
32406f32e7eSjoerg processGlobalsForThinLTO();
32506f32e7eSjoerg return false;
32606f32e7eSjoerg }
32706f32e7eSjoerg
renameModuleForThinLTO(Module & M,const ModuleSummaryIndex & Index,bool ClearDSOLocalOnDeclarations,SetVector<GlobalValue * > * GlobalsToImport)32806f32e7eSjoerg bool llvm::renameModuleForThinLTO(Module &M, const ModuleSummaryIndex &Index,
329*da58b97aSjoerg bool ClearDSOLocalOnDeclarations,
33006f32e7eSjoerg SetVector<GlobalValue *> *GlobalsToImport) {
331*da58b97aSjoerg FunctionImportGlobalProcessing ThinLTOProcessing(M, Index, GlobalsToImport,
332*da58b97aSjoerg ClearDSOLocalOnDeclarations);
33306f32e7eSjoerg return ThinLTOProcessing.run();
33406f32e7eSjoerg }
335