blob: 847ba79c890fffe53eea2d29d1aec0b3092601be [file] [edit]
//===--- ModuleUtils.cpp - Shared module emission queries -----------------===//
//
// 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
//
//===----------------------------------------------------------------------===//
#include "clang/CodeGenUtils/ModuleUtils.h"
#include "clang/AST/Attr.h"
#include "clang/Basic/TargetInfo.h"
#include "llvm/ADT/SetVector.h"
namespace clang::CodeGenUtils {
LangAS getGlobalConstantAddressSpace(const LangOptions &LangOpts,
const TargetInfo &Target) {
// OpenCL v1.2 s6.5.3: a string literal is in the constant address space.
if (LangOpts.OpenCL)
return LangAS::opencl_constant;
if (LangOpts.SYCLIsDevice)
return LangAS::sycl_global;
if (LangOpts.HIP && LangOpts.CUDAIsDevice && Target.getTriple().isSPIRV())
// For HIPSPV map literals to cuda_device (maps to CrossWorkGroup in SPIR-V)
// instead of default AS (maps to Generic in SPIR-V). Otherwise, we end up
// with OpVariable instructions with Generic storage class which is not
// allowed (SPIR-V V1.6 s3.42.8). Also, mapping literals to SPIR-V
// UniformConstant storage class is not viable as pointers to it may not be
// casted to Generic pointers which are used to model HIP's "flat" pointers.
return LangAS::cuda_device;
if (auto AS = Target.getConstantAddressSpace())
return *AS;
return LangAS::Default;
}
bool hasUnwindExceptions(const LangOptions &LangOpts) {
// If exceptions are completely disabled, obviously this is false.
if (!LangOpts.Exceptions)
return false;
// If C++ exceptions are enabled, this is true.
if (LangOpts.CXXExceptions)
return true;
// If ObjC exceptions are enabled, this depends on the ABI.
if (LangOpts.ObjCExceptions) {
return LangOpts.ObjCRuntime.hasUnwindExceptions();
}
return true;
}
bool isVarDeclStrongDefinition(const ASTContext &Ctx, const VarDecl *D,
bool NoCommon) {
// Don't give variables common linkage if -fno-common was specified unless it
// was overridden by a NoCommon attribute.
if ((NoCommon || D->hasAttr<NoCommonAttr>()) && !D->hasAttr<CommonAttr>())
return true;
// C11 6.9.2/2:
// A declaration of an identifier for an object that has file scope without
// an initializer, and without a storage-class specifier or with the
// storage-class specifier static, constitutes a tentative definition.
if (D->getInit() || D->hasExternalStorage())
return true;
// A variable cannot be both common and exist in a section.
if (D->hasAttr<SectionAttr>())
return true;
// A variable cannot be both common and exist in a section.
// We don't try to determine which is the right section in the front-end.
// If no specialized section name is applicable, it will resort to default.
if (D->hasAttr<PragmaClangBSSSectionAttr>() ||
D->hasAttr<PragmaClangDataSectionAttr>() ||
D->hasAttr<PragmaClangRelroSectionAttr>() ||
D->hasAttr<PragmaClangRodataSectionAttr>())
return true;
// Thread local vars aren't considered common linkage.
if (D->getTLSKind())
return true;
// Tentative definitions marked with WeakImportAttr are true definitions.
if (D->hasAttr<WeakImportAttr>())
return true;
// A variable cannot be both common and exist in a comdat.
if (shouldBeInCOMDAT(Ctx, *D))
return true;
// Declarations with a required alignment do not have common linkage in MSVC
// mode.
if (Ctx.getTargetInfo().getCXXABI().isMicrosoft()) {
if (D->hasAttr<AlignedAttr>())
return true;
QualType VarType = D->getType();
if (Ctx.isAlignmentRequired(VarType))
return true;
if (const auto *RD = VarType->getAsRecordDecl()) {
for (const FieldDecl *FD : RD->fields()) {
if (FD->isBitField())
continue;
if (FD->hasAttr<AlignedAttr>())
return true;
if (Ctx.isAlignmentRequired(FD->getType()))
return true;
}
}
}
// Microsoft's link.exe doesn't support alignments greater than 32 bytes for
// common symbols, so symbols with greater alignment requirements cannot be
// common.
// Other COFF linkers (ld.bfd and LLD) support arbitrary power-of-two
// alignments for common symbols via the aligncomm directive, so this
// restriction only applies to MSVC environments.
if (Ctx.getTargetInfo().getTriple().isKnownWindowsMSVCEnvironment() &&
Ctx.getTypeAlignIfKnown(D->getType()) >
Ctx.toBits(CharUnits::fromQuantity(32)))
return true;
return false;
}
bool shouldBeInCOMDAT(const ASTContext &Ctx, const Decl &D) {
if (!Ctx.getTargetInfo().getTriple().supportsCOMDAT())
return false;
if (D.hasAttr<SelectAnyAttr>())
return true;
GVALinkage Linkage;
if (auto *VD = dyn_cast<VarDecl>(&D))
Linkage = Ctx.GetGVALinkageForVariable(VD);
else
Linkage = Ctx.GetGVALinkageForFunction(cast<FunctionDecl>(&D));
switch (Linkage) {
case GVA_Internal:
case GVA_AvailableExternally:
case GVA_StrongExternal:
return false;
case GVA_DiscardableODR:
case GVA_StrongODR:
return true;
}
llvm_unreachable("No such linkage");
}
llvm::SmallVector<Module *, 8>
importedModulesToInitialize(Module *Primary,
llvm::ArrayRef<Module *> Imported) {
llvm::SmallSetVector<Module *, 8> Candidates;
if (Primary && Primary->isInterfaceOrPartition()) {
// The modules the unit exports.
for (auto I : Primary->Exports)
Candidates.insert(I.first);
// The modules it only imports.
Candidates.insert_range(Primary->Imports);
// The modules it imports in the global module fragment or the private
// module fragment.
for (Module *SubM : Primary->submodules()) {
assert(
(SubM->isGlobalModule() || SubM->isPrivateModule()) &&
"The sub modules of C++20 module unit should only be global module "
"fragments or private module fragments.");
assert(SubM->Exports.empty() &&
"The global module fragments and the private module fragments are "
"not allowed to export import modules.");
Candidates.insert_range(SubM->Imports);
}
} else {
Candidates.insert_range(Imported);
}
llvm::SmallVector<Module *, 8> Result;
for (Module *M : Candidates) {
// No Itanium initializer in header like modules.
if (M->isHeaderLikeModule())
continue;
// The initialization may be skipped when it is sure to do nothing.
if (!M->isNamedModuleInterfaceHasInit())
continue;
Result.push_back(M);
}
return Result;
}
} // namespace clang::CodeGenUtils