+bool ModuleLinker::doImportAsDefinition(const GlobalValue *SGV) {
+ if (!isPerformingImport())
+ return false;
+ // Always import GlobalVariable definitions. The linkage changes
+ // described in ModuleLinker::getLinkage ensure the correct behavior (e.g.
+ // global variables with external linkage are transformed to
+ // available_externally defintions, which are ultimately turned into
+ // declaratios after the EliminateAvailableExternally pass).
+ if (dyn_cast<GlobalVariable>(SGV) && !SGV->isDeclaration())
+ return true;
+ // Only import the function requested for importing.
+ auto *SF = dyn_cast<Function>(SGV);
+ if (SF && SF == ImportFunction)
+ return true;
+ // Otherwise no.
+ return false;
+}
+
+bool ModuleLinker::doPromoteLocalToGlobal(const GlobalValue *SGV) {
+ assert(SGV->hasLocalLinkage());
+ // Both the imported references and the original local variable must
+ // be promoted.
+ if (!isPerformingImport() && !isModuleExporting())
+ return false;
+
+ // Local const variables never need to be promoted unless they are address
+ // taken. The imported uses can simply use the clone created in this module.
+ // For now we are conservative in determining which variables are not
+ // address taken by checking the unnamed addr flag. To be more aggressive,
+ // the address taken information must be checked earlier during parsing
+ // of the module and recorded in the function index for use when importing
+ // from that module.
+ auto *GVar = dyn_cast<GlobalVariable>(SGV);
+ if (GVar && GVar->isConstant() && GVar->hasUnnamedAddr())
+ return false;
+
+ // Eventually we only need to promote functions in the exporting module that
+ // are referenced by a potentially exported function (i.e. one that is in the
+ // function index).
+ return true;
+}
+
+std::string ModuleLinker::getName(const GlobalValue *SGV) {
+ // For locals that must be promoted to global scope, ensure that
+ // the promoted name uniquely identifies the copy in the original module,
+ // using the ID assigned during combined index creation. When importing,
+ // we rename all locals (not just those that are promoted) in order to
+ // avoid naming conflicts between locals imported from different modules.
+ if (SGV->hasLocalLinkage() &&
+ (doPromoteLocalToGlobal(SGV) || isPerformingImport()))
+ return FunctionInfoIndex::getGlobalNameForLocal(
+ SGV->getName(),
+ ImportIndex->getModuleId(SGV->getParent()->getModuleIdentifier()));
+ return SGV->getName();
+}
+
+GlobalValue::LinkageTypes ModuleLinker::getLinkage(const GlobalValue *SGV) {
+ // 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()) {
+ if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
+ 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()) {
+ case GlobalValue::ExternalLinkage:
+ // External defnitions are converted to available_externally
+ // definitions upon import, so that they are available for inlining
+ // and/or optimization, but are turned into declarations later
+ // during the EliminateAvailableExternally pass.
+ if (doImportAsDefinition(SGV))
+ 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();
+
+ case GlobalValue::LinkOnceAnyLinkage:
+ case GlobalValue::LinkOnceODRLinkage:
+ // These both stay the same when importing the definition.
+ // The ThinLTO pass will eventually force-import their definitions.
+ return SGV->getLinkage();
+
+ case GlobalValue::WeakAnyLinkage:
+ // Can't import weak_any definitions correctly, or we might change the
+ // program semantics, since the linker will pick the first weak_any
+ // definition and importing would change the order they are seen by the
+ // linker. The module linking caller needs to enforce this.
+ assert(!doImportAsDefinition(SGV));
+ // If imported as a declaration, it becomes external_weak.
+ return GlobalValue::ExternalWeakLinkage;
+
+ 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.
+ if (doImportAsDefinition(SGV))
+ 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 linkGlobalValueProto.
+ assert(false && "Cannot import appending linkage variable");
+
+ 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.
+ if (doPromoteLocalToGlobal(SGV)) {
+ if (doImportAsDefinition(SGV))
+ 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");
+}
+
+/// Loop through the global variables in the src module and merge them into the
+/// dest module.
+GlobalVariable *
+ModuleLinker::copyGlobalVariableProto(TypeMapTy &TypeMap,
+ const GlobalVariable *SGVar) {
+ // No linking to be performed or linking from the source: simply create an
+ // identical version of the symbol over in the dest module... the
+ // initializer will be filled in later by LinkGlobalInits.
+ GlobalVariable *NewDGV = new GlobalVariable(
+ *DstM, TypeMap.get(SGVar->getType()->getElementType()),
+ SGVar->isConstant(), getLinkage(SGVar), /*init*/ nullptr, getName(SGVar),
+ /*insertbefore*/ nullptr, SGVar->getThreadLocalMode(),
+ SGVar->getType()->getAddressSpace());
+
+ return NewDGV;
+}
+
+/// Link the function in the source module into the destination module if
+/// needed, setting up mapping information.
+Function *ModuleLinker::copyFunctionProto(TypeMapTy &TypeMap,
+ const Function *SF) {
+ // If there is no linkage to be performed or we are linking from the source,
+ // bring SF over.
+ return Function::Create(TypeMap.get(SF->getFunctionType()), getLinkage(SF),
+ getName(SF), DstM);
+}
+
+/// Set up prototypes for any aliases that come over from the source module.
+GlobalValue *ModuleLinker::copyGlobalAliasProto(TypeMapTy &TypeMap,
+ const GlobalAlias *SGA) {
+ // If we are importing and encounter a weak_any alias, or an alias to
+ // an object being imported as a declaration, we must import the alias
+ // as a declaration as well, which involves converting it to a non-alias.
+ // See comments in ModuleLinker::getLinkage for why we cannot import
+ // weak_any defintions.
+ if (isPerformingImport() && (SGA->hasWeakAnyLinkage() ||
+ !doImportAsDefinition(SGA->getBaseObject()))) {
+ // Need to convert to declaration. All aliases must be definitions.
+ const GlobalValue *GVal = SGA->getBaseObject();
+ GlobalValue *NewGV;
+ if (auto *GVar = dyn_cast<GlobalVariable>(GVal))
+ NewGV = copyGlobalVariableProto(TypeMap, GVar);
+ else {
+ auto *F = dyn_cast<Function>(GVal);
+ assert(F);
+ NewGV = copyFunctionProto(TypeMap, F);
+ }
+ // Set the linkage to External or ExternalWeak (see comments in
+ // ModuleLinker::getLinkage for why WeakAny is converted to ExternalWeak).
+ if (SGA->hasWeakAnyLinkage())
+ NewGV->setLinkage(GlobalValue::ExternalWeakLinkage);
+ else
+ NewGV->setLinkage(GlobalValue::ExternalLinkage);
+ // Don't attempt to link body, needs to be a declaration.
+ DoNotLinkFromSource.insert(SGA);
+ return NewGV;
+ }
+ // If there is no linkage to be performed or we're linking from the source,
+ // bring over SGA.
+ auto *Ty = TypeMap.get(SGA->getValueType());
+ return GlobalAlias::create(Ty, SGA->getType()->getPointerAddressSpace(),
+ getLinkage(SGA), getName(SGA), DstM);
+}
+
+void ModuleLinker::setVisibility(GlobalValue *NewGV, const GlobalValue *SGV,
+ const GlobalValue *DGV) {
+ GlobalValue::VisibilityTypes Visibility = SGV->getVisibility();
+ if (DGV)
+ Visibility = isLessConstraining(Visibility, DGV->getVisibility())
+ ? DGV->getVisibility()
+ : Visibility;
+ // For promoted locals, mark them hidden so that they can later be
+ // stripped from the symbol table to reduce bloat.
+ if (SGV->hasLocalLinkage() && doPromoteLocalToGlobal(SGV))
+ Visibility = GlobalValue::HiddenVisibility;
+ NewGV->setVisibility(Visibility);
+}
+
+GlobalValue *ModuleLinker::copyGlobalValueProto(TypeMapTy &TypeMap,
+ const GlobalValue *SGV,
+ const GlobalValue *DGV) {
+ GlobalValue *NewGV;
+ if (auto *SGVar = dyn_cast<GlobalVariable>(SGV))
+ NewGV = copyGlobalVariableProto(TypeMap, SGVar);
+ else if (auto *SF = dyn_cast<Function>(SGV))
+ NewGV = copyFunctionProto(TypeMap, SF);
+ else
+ NewGV = copyGlobalAliasProto(TypeMap, cast<GlobalAlias>(SGV));
+ copyGVAttributes(NewGV, SGV);
+ setVisibility(NewGV, SGV, DGV);
+ return NewGV;
+}
+