Skip to content

Commit 96a0551

Browse files
authored
[CIR][NFC] Backport changes to use QualType::isStorageConstant (#1992)
We had a comment in the code suggesting that CIRGenModule::isTypeConstant could be shared with OGCG. The code there was moved into QualType::isConstantStorage quite a while ago, but the incubator code never got updated to use it. This change does that.
1 parent 690e9d8 commit 96a0551

File tree

4 files changed

+21
-38
lines changed

4 files changed

+21
-38
lines changed

clang/lib/CIR/CodeGen/CIRGenDecl.cpp

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -88,11 +88,13 @@ CIRGenFunction::emitAutoVarAlloca(const VarDecl &D,
8888
// TODO: deal with CGM.getCodeGenOpts().MergeAllConstants
8989
// TODO: perhaps we don't need this at all at CIR since this can
9090
// be done as part of lowering down to LLVM.
91+
bool NeedsDtor =
92+
D.needsDestruction(getContext()) == QualType::DK_cxx_destructor;
9193
if ((!getContext().getLangOpts().OpenCL ||
9294
Ty.getAddressSpace() == LangAS::opencl_constant) &&
9395
(!NRVO && !D.isEscapingByref() &&
94-
CGM.isTypeConstant(Ty, /*ExcludeCtor=*/true,
95-
/*ExcludeDtor=*/false))) {
96+
Ty.isConstantStorage(getContext(), /*ExcludeCtor=*/true,
97+
!NeedsDtor))) {
9698
emitStaticVarDecl(D, cir::GlobalLinkageKind::InternalLinkage);
9799

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

603-
GV.setConstant(
604-
CGM.isTypeConstant(D.getType(), /*ExcludeCtor=*/true, !NeedsDtor));
605+
GV.setConstant(D.getType().isConstantStorage(
606+
getContext(), /*ExcludeCtor=*/true, !NeedsDtor));
607+
605608
GV.setInitialValueAttr(Init);
606609

607610
emitter.finalize(GV);

clang/lib/CIR/CodeGen/CIRGenExpr.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2260,7 +2260,8 @@ static Address createReferenceTemporary(CIRGenFunction &CGF,
22602260
QualType Ty = Inner->getType();
22612261
if (CGF.CGM.getCodeGenOpts().MergeAllConstants &&
22622262
(Ty->isArrayType() || Ty->isRecordType()) &&
2263-
CGF.CGM.isTypeConstant(Ty, /*ExcludeCtor=*/true, /*ExcludeDtor=*/false))
2263+
Ty.isConstantStorage(CGF.CGM.getASTContext(), /*ExcludeCtor=*/true,
2264+
/*ExcludeDtor=*/false))
22642265
assert(0 && "NYI");
22652266

22662267
// The temporary memory should be created in the same scope as the extending

clang/lib/CIR/CodeGen/CIRGenModule.cpp

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -237,21 +237,6 @@ CIRGenModule::CIRGenModule(mlir::MLIRContext &mlirContext,
237237

238238
CIRGenModule::~CIRGenModule() = default;
239239

240-
bool CIRGenModule::isTypeConstant(QualType ty, bool excludeCtor,
241-
bool excludeDtor) {
242-
if (!ty.isConstant(astContext) && !ty->isReferenceType())
243-
return false;
244-
245-
if (astContext.getLangOpts().CPlusPlus) {
246-
if (const CXXRecordDecl *record =
247-
astContext.getBaseElementType(ty)->getAsCXXRecordDecl())
248-
return excludeCtor && !record->hasMutableFields() &&
249-
(record->hasTrivialDestructor() || excludeDtor);
250-
}
251-
252-
return true;
253-
}
254-
255240
/// FIXME: this could likely be a common helper and not necessarily related
256241
/// with codegen.
257242
/// Return the best known alignment for an unknown pointer to a
@@ -393,7 +378,8 @@ bool CIRGenModule::MayBeEmittedEagerly(const ValueDecl *global) {
393378
if (langOpts.OpenMP && langOpts.OpenMPUseTLS &&
394379
getASTContext().getTargetInfo().isTLSSupported() &&
395380
isa<VarDecl>(global) &&
396-
!global->getType().isConstantStorage(getASTContext(), false, false) &&
381+
!global->getType().isConstantStorage(
382+
getASTContext(), /*ExcludeCtor=*/false, /*ExcludeDtor=*/false) &&
397383
!OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(global))
398384
return false;
399385

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

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

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

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

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

19651955
isConstant = materializedType.isConstantStorage(
1966-
getASTContext(), /*ExcludeCtor*/ value, /*ExcludeDtor*/ false);
1956+
getASTContext(), /*ExcludeCtor=*/value, /*ExcludeDtor=*/false);
19671957

19681958
type = mlir::cast<mlir::TypedAttr>(initialValue).getType();
19691959
} else {

clang/lib/CIR/CodeGen/CIRGenModule.h

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -535,17 +535,6 @@ class CIRGenModule : public CIRGenTypeCache {
535535
/// optimization.
536536
bool HasHiddenLTOVisibility(const CXXRecordDecl *RD);
537537

538-
/// Determine whether an object of this type can be emitted
539-
/// as a constant.
540-
///
541-
/// If ExcludeCtor is true, the duration when the object's constructor runs
542-
/// will not be considered. The caller will need to verify that the object is
543-
/// not written to during its construction.
544-
/// FIXME: in LLVM codegen path this is part of CGM, which doesn't seem
545-
/// like necessary, since (1) it doesn't use CGM at all and (2) is AST type
546-
/// query specific.
547-
bool isTypeConstant(clang::QualType Ty, bool ExcludeCtor, bool ExcludeDtor);
548-
549538
/// FIXME: this could likely be a common helper and not necessarily related
550539
/// with codegen.
551540
/// Return the best known alignment for an unknown pointer to a

0 commit comments

Comments
 (0)