Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 7 additions & 4 deletions clang/lib/CIR/CodeGen/CIRGenDecl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -88,11 +88,13 @@ CIRGenFunction::emitAutoVarAlloca(const VarDecl &D,
// TODO: deal with CGM.getCodeGenOpts().MergeAllConstants
// TODO: perhaps we don't need this at all at CIR since this can
// be done as part of lowering down to LLVM.
bool NeedsDtor =
D.needsDestruction(getContext()) == QualType::DK_cxx_destructor;
if ((!getContext().getLangOpts().OpenCL ||
Ty.getAddressSpace() == LangAS::opencl_constant) &&
(!NRVO && !D.isEscapingByref() &&
CGM.isTypeConstant(Ty, /*ExcludeCtor=*/true,
/*ExcludeDtor=*/false))) {
Ty.isConstantStorage(getContext(), /*ExcludeCtor=*/true,
!NeedsDtor))) {
emitStaticVarDecl(D, cir::GlobalLinkageKind::InternalLinkage);

// Signal this condition to later callbacks.
Expand Down Expand Up @@ -600,8 +602,9 @@ cir::GlobalOp CIRGenFunction::addInitializerToStaticVarDecl(
bool NeedsDtor =
D.needsDestruction(getContext()) == QualType::DK_cxx_destructor;

GV.setConstant(
CGM.isTypeConstant(D.getType(), /*ExcludeCtor=*/true, !NeedsDtor));
GV.setConstant(D.getType().isConstantStorage(
getContext(), /*ExcludeCtor=*/true, !NeedsDtor));

GV.setInitialValueAttr(Init);

emitter.finalize(GV);
Expand Down
3 changes: 2 additions & 1 deletion clang/lib/CIR/CodeGen/CIRGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2264,7 +2264,8 @@ static Address createReferenceTemporary(CIRGenFunction &CGF,
QualType Ty = Inner->getType();
if (CGF.CGM.getCodeGenOpts().MergeAllConstants &&
(Ty->isArrayType() || Ty->isRecordType()) &&
CGF.CGM.isTypeConstant(Ty, /*ExcludeCtor=*/true, /*ExcludeDtor=*/false))
Ty.isConstantStorage(CGF.CGM.getASTContext(), /*ExcludeCtor=*/true,
/*ExcludeDtor=*/false))
assert(0 && "NYI");

// The temporary memory should be created in the same scope as the extending
Expand Down
34 changes: 12 additions & 22 deletions clang/lib/CIR/CodeGen/CIRGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -237,21 +237,6 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,

CIRGenModule::~CIRGenModule() = default;

bool CIRGenModule::isTypeConstant(QualType ty, bool excludeCtor,
bool excludeDtor) {
if (!ty.isConstant(astContext) && !ty->isReferenceType())
return false;

if (astContext.getLangOpts().CPlusPlus) {
if (const CXXRecordDecl *record =
astContext.getBaseElementType(ty)->getAsCXXRecordDecl())
return excludeCtor && !record->hasMutableFields() &&
(record->hasTrivialDestructor() || excludeDtor);
}

return true;
}

/// FIXME: this could likely be a common helper and not necessarily related
/// with codegen.
/// Return the best known alignment for an unknown pointer to a
Expand Down Expand Up @@ -393,7 +378,8 @@ bool CIRGenModule::MayBeEmittedEagerly(const ValueDecl *global) {
if (langOpts.OpenMP && langOpts.OpenMPUseTLS &&
getASTContext().getTargetInfo().isTLSSupported() &&
isa<VarDecl>(global) &&
!global->getType().isConstantStorage(getASTContext(), false, false) &&
!global->getType().isConstantStorage(
getASTContext(), /*ExcludeCtor=*/false, /*ExcludeDtor=*/false) &&
!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(global))
return false;

Expand Down Expand Up @@ -1205,7 +1191,8 @@ CIRGenModule::getOrCreateCIRGlobal(StringRef mangledName, mlir::Type ty,
// FIXME: This code is overly simple and should be merged with other global
// handling.
gv.setAlignmentAttr(getSize(astContext.getDeclAlign(d)));
gv.setConstant(isTypeConstant(d->getType(), false, false));
gv.setConstant(d->getType().isConstantStorage(
astContext, /*ExcludeCtor=*/false, /*ExcludeDtor=*/false));
// TODO(cir): setLinkageForGV(GV, D);

if (d->getTLSKind()) {
Expand Down Expand Up @@ -1399,7 +1386,8 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d,
// TODO: Update this when we have interface to check constexpr
// destructor.
d->needsDestruction(getASTContext()) ||
!d->getType().isConstantStorage(getASTContext(), true, true)))
!d->getType().isConstantStorage(getASTContext(), /*ExcludeCtor=*/true,
/*ExcludeDtor=*/true)))
return;

const VarDecl *initDecl;
Expand Down Expand Up @@ -1569,9 +1557,11 @@ void CIRGenModule::emitGlobalVarDefinition(const clang::VarDecl *d,
emitter->finalize(gv);

// TODO(cir): If it is safe to mark the global 'constant', do so now.
gv.setConstant((d->hasAttr<CUDAConstantAttr>() && langOpts.CUDAIsDevice) ||
(!needsGlobalCtor && !needsGlobalDtor &&
isTypeConstant(d->getType(), true, true)));
gv.setConstant(
(d->hasAttr<CUDAConstantAttr>() && langOpts.CUDAIsDevice) ||
(!needsGlobalCtor && !needsGlobalDtor &&
d->getType().isConstantStorage(getASTContext(), /*ExcludeCtor=*/true,
/*ExcludeDtor=*/true)));

// If it is in a read-only section, mark it 'constant'.
if (const SectionAttr *sa = d->getAttr<SectionAttr>())
Expand Down Expand Up @@ -1963,7 +1953,7 @@ CIRGenModule::getAddrOfGlobalTemporary(const MaterializeTemporaryExpr *expr,
emitter->emitForInitializer(*value, addrSpace, materializedType);

isConstant = materializedType.isConstantStorage(
getASTContext(), /*ExcludeCtor*/ value, /*ExcludeDtor*/ false);
getASTContext(), /*ExcludeCtor=*/value, /*ExcludeDtor=*/false);

type = mlir::cast<mlir::TypedAttr>(initialValue).getType();
} else {
Expand Down
11 changes: 0 additions & 11 deletions clang/lib/CIR/CodeGen/CIRGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -535,17 +535,6 @@ class CIRGenModule : public CIRGenTypeCache {
/// optimization.
bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);

/// Determine whether an object of this type can be emitted
/// as a constant.
///
/// If ExcludeCtor is true, the duration when the object's constructor runs
/// will not be considered. The caller will need to verify that the object is
/// not written to during its construction.
/// FIXME: in LLVM codegen path this is part of CGM, which doesn't seem
/// like necessary, since (1) it doesn't use CGM at all and (2) is AST type
/// query specific.
bool isTypeConstant(clang::QualType Ty, bool ExcludeCtor, bool ExcludeDtor);

/// FIXME: this could likely be a common helper and not necessarily related
/// with codegen.
/// Return the best known alignment for an unknown pointer to a
Expand Down
Loading