From 48907a669ce3312ae18078df1400a371115a99da Mon Sep 17 00:00:00 2001 From: Andy Kaylor Date: Tue, 18 Nov 2025 13:04:31 -0800 Subject: [PATCH] [CIR][NFC] Backport changes to use QualType::isStorageConstant 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. --- clang/lib/CIR/CodeGen/CIRGenDecl.cpp | 11 ++++++--- clang/lib/CIR/CodeGen/CIRGenExpr.cpp | 3 ++- clang/lib/CIR/CodeGen/CIRGenModule.cpp | 34 +++++++++----------------- clang/lib/CIR/CodeGen/CIRGenModule.h | 11 --------- 4 files changed, 21 insertions(+), 38 deletions(-) diff --git a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp index 6b5c942fa193..88d184df856b 100644 --- a/clang/lib/CIR/CodeGen/CIRGenDecl.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenDecl.cpp @@ -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. @@ -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); diff --git a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp index 4980e4ce7d11..cd43df929833 100644 --- a/clang/lib/CIR/CodeGen/CIRGenExpr.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenExpr.cpp @@ -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 diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.cpp b/clang/lib/CIR/CodeGen/CIRGenModule.cpp index 56121a721ae1..5bef8e41cab1 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.cpp +++ b/clang/lib/CIR/CodeGen/CIRGenModule.cpp @@ -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 @@ -393,7 +378,8 @@ bool CIRGenModule::MayBeEmittedEagerly(const ValueDecl *global) { if (langOpts.OpenMP && langOpts.OpenMPUseTLS && getASTContext().getTargetInfo().isTLSSupported() && isa(global) && - !global->getType().isConstantStorage(getASTContext(), false, false) && + !global->getType().isConstantStorage( + getASTContext(), /*ExcludeCtor=*/false, /*ExcludeDtor=*/false) && !OMPDeclareTargetDeclAttr::isDeclareTargetDeclaration(global)) return false; @@ -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()) { @@ -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; @@ -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() && langOpts.CUDAIsDevice) || - (!needsGlobalCtor && !needsGlobalDtor && - isTypeConstant(d->getType(), true, true))); + gv.setConstant( + (d->hasAttr() && 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()) @@ -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(initialValue).getType(); } else { diff --git a/clang/lib/CIR/CodeGen/CIRGenModule.h b/clang/lib/CIR/CodeGen/CIRGenModule.h index f130b007d2b2..5c505cddf723 100644 --- a/clang/lib/CIR/CodeGen/CIRGenModule.h +++ b/clang/lib/CIR/CodeGen/CIRGenModule.h @@ -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