[clang] [clang-repl] [codegen] Reduce the state in TBAA. NFC for static compilation. (PR #98138)
Vassil Vassilev via cfe-commits
cfe-commits at lists.llvm.org
Tue Jul 9 02:42:52 PDT 2024
https://github.com/vgvassilev created https://github.com/llvm/llvm-project/pull/98138
In incremental compilation clang works with multiple `llvm::Module`s. Our current approach is to create a CodeGenModule entity for every new module request (via StartModule). However, some of the state such as the mangle context needs to be preserved to keep the original semantics in the ever-growing TU.
Fixes: llvm/llvm-project#95581.
cc: @jeaye
>From ffb489b40979b48b017bb8c337f955ab7f4588b3 Mon Sep 17 00:00:00 2001
From: Vassil Vassilev <v.g.vassilev at gmail.com>
Date: Tue, 9 Jul 2024 09:30:19 +0000
Subject: [PATCH] [clang-repl] [codegen] Reduce the state in TBAA. NFC for
static compilation.
In incremental compilation clang works with multiple `llvm::Module`s. Our
current approach is to create a CodeGenModule entity for every new module
request (via StartModule). However, some of the state such as the mangle context
needs to be preserved to keep the original semantics in the ever-growing TU.
Fixes: llvm/llvm-project#95581.
---
clang/lib/CodeGen/CGCall.cpp | 14 +++++++-------
clang/lib/CodeGen/CGClass.cpp | 2 +-
clang/lib/CodeGen/CodeGenModule.cpp | 11 +++++------
clang/lib/CodeGen/CodeGenModule.h | 4 ++--
clang/lib/CodeGen/CodeGenTBAA.cpp | 11 ++++++-----
clang/lib/CodeGen/CodeGenTBAA.h | 5 +----
clang/lib/CodeGen/CodeGenTypes.cpp | 9 +++++++--
clang/lib/CodeGen/CodeGenTypes.h | 9 ++-------
8 files changed, 31 insertions(+), 34 deletions(-)
diff --git a/clang/lib/CodeGen/CGCall.cpp b/clang/lib/CodeGen/CGCall.cpp
index 7e7b2b395f7d6..458795756dd65 100644
--- a/clang/lib/CodeGen/CGCall.cpp
+++ b/clang/lib/CodeGen/CGCall.cpp
@@ -314,7 +314,7 @@ CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
if (MD->isImplicitObjectMemberFunction()) {
// The abstract case is perfectly fine.
- const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
+ const CXXRecordDecl *ThisType = getCXXABI().getThisArgumentTypeForMethod(MD);
return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
}
@@ -337,7 +337,7 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
SmallVector<CanQualType, 16> argTypes;
SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
- const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(GD);
+ const CXXRecordDecl *ThisType = getCXXABI().getThisArgumentTypeForMethod(GD);
argTypes.push_back(DeriveThisType(ThisType, MD));
bool PassParams = true;
@@ -356,7 +356,7 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
appendParameterTypes(*this, argTypes, paramInfos, FTP);
CGCXXABI::AddedStructorArgCounts AddedArgs =
- TheCXXABI.buildStructorSignature(GD, argTypes);
+ getCXXABI().buildStructorSignature(GD, argTypes);
if (!paramInfos.empty()) {
// Note: prefix implies after the first param.
if (AddedArgs.Prefix)
@@ -372,9 +372,9 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
: RequiredArgs::All);
FunctionType::ExtInfo extInfo = FTP->getExtInfo();
- CanQualType resultType = TheCXXABI.HasThisReturn(GD)
+ CanQualType resultType = getCXXABI().HasThisReturn(GD)
? argTypes.front()
- : TheCXXABI.hasMostDerivedReturn(GD)
+ : getCXXABI().hasMostDerivedReturn(GD)
? CGM.getContext().VoidPtrTy
: Context.VoidTy;
return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::IsInstanceMethod,
@@ -437,9 +437,9 @@ CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
: RequiredArgs::All;
GlobalDecl GD(D, CtorKind);
- CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
+ CanQualType ResultType = getCXXABI().HasThisReturn(GD)
? ArgTypes.front()
- : TheCXXABI.hasMostDerivedReturn(GD)
+ : getCXXABI().hasMostDerivedReturn(GD)
? CGM.getContext().VoidPtrTy
: Context.VoidTy;
diff --git a/clang/lib/CodeGen/CGClass.cpp b/clang/lib/CodeGen/CGClass.cpp
index 0a595bb998d26..c03a8570ba8cc 100644
--- a/clang/lib/CodeGen/CGClass.cpp
+++ b/clang/lib/CodeGen/CGClass.cpp
@@ -208,7 +208,7 @@ CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
return nullptr;
llvm::Type *PtrDiffTy =
- Types.ConvertType(getContext().getPointerDiffType());
+ getTypes().ConvertType(getContext().getPointerDiffType());
return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
}
diff --git a/clang/lib/CodeGen/CodeGenModule.cpp b/clang/lib/CodeGen/CodeGenModule.cpp
index 44bc7fbfdd37e..8104231fc2230 100644
--- a/clang/lib/CodeGen/CodeGenModule.cpp
+++ b/clang/lib/CodeGen/CodeGenModule.cpp
@@ -341,10 +341,11 @@ CodeGenModule::CodeGenModule(ASTContext &C,
: Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
- VMContext(M.getContext()), Types(*this), VTables(*this),
+ VMContext(M.getContext()), VTables(*this),
SanitizerMD(new SanitizerMetadata(*this)) {
// Initialize the type cache.
+ Types.reset(new CodeGenTypes(*this));
llvm::LLVMContext &LLVMContext = M.getContext();
VoidTy = llvm::Type::getVoidTy(LLVMContext);
Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
@@ -403,7 +404,7 @@ CodeGenModule::CodeGenModule(ASTContext &C,
if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
(!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
TBAA.reset(new CodeGenTBAA(Context, getTypes(), TheModule, CodeGenOpts,
- getLangOpts(), getCXXABI().getMangleContext()));
+ getLangOpts()));
// If debug info or coverage generation is enabled, create the CGDebugInfo
// object.
@@ -1445,12 +1446,12 @@ void CodeGenModule::EmitBackendOptionsMetadata(
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
// Make sure that this type is translated.
- Types.UpdateCompletedType(TD);
+ getTypes().UpdateCompletedType(TD);
}
void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
// Make sure that this type is translated.
- Types.RefreshTypeCacheForClass(RD);
+ getTypes().RefreshTypeCacheForClass(RD);
}
llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
@@ -7766,7 +7767,5 @@ void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) {
NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
- NewBuilder->TBAA = std::move(TBAA);
-
NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
}
diff --git a/clang/lib/CodeGen/CodeGenModule.h b/clang/lib/CodeGen/CodeGenModule.h
index 8b65348b879b6..bfb92ad3e5691 100644
--- a/clang/lib/CodeGen/CodeGenModule.h
+++ b/clang/lib/CodeGen/CodeGenModule.h
@@ -320,7 +320,7 @@ class CodeGenModule : public CodeGenTypeCache {
// This should not be moved earlier, since its initialization depends on some
// of the previous reference members being already initialized and also checks
// if TheTargetCodeGenInfo is NULL
- CodeGenTypes Types;
+ std::unique_ptr<CodeGenTypes> Types;
/// Holds information about C++ vtables.
CodeGenVTables VTables;
@@ -780,7 +780,7 @@ class CodeGenModule : public CodeGenTypeCache {
const TargetCodeGenInfo &getTargetCodeGenInfo();
- CodeGenTypes &getTypes() { return Types; }
+ CodeGenTypes &getTypes() { return *Types; }
CodeGenVTables &getVTables() { return VTables; }
diff --git a/clang/lib/CodeGen/CodeGenTBAA.cpp b/clang/lib/CodeGen/CodeGenTBAA.cpp
index 284421f494711..7c48b64098fab 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.cpp
+++ b/clang/lib/CodeGen/CodeGenTBAA.cpp
@@ -15,6 +15,7 @@
//===----------------------------------------------------------------------===//
#include "CodeGenTBAA.h"
+#include "CGCXXABI.h"
#include "CGRecordLayout.h"
#include "CodeGenTypes.h"
#include "clang/AST/ASTContext.h"
@@ -35,10 +36,10 @@ using namespace CodeGen;
CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes,
llvm::Module &M, const CodeGenOptions &CGO,
- const LangOptions &Features, MangleContext &MContext)
+ const LangOptions &Features)
: Context(Ctx), CGTypes(CGTypes), Module(M), CodeGenOpts(CGO),
- Features(Features), MContext(MContext), MDHelper(M.getContext()),
- Root(nullptr), Char(nullptr) {}
+ Features(Features), MDHelper(M.getContext()), Root(nullptr),
+ Char(nullptr) {}
CodeGenTBAA::~CodeGenTBAA() {
}
@@ -209,7 +210,7 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
SmallString<256> OutName;
llvm::raw_svector_ostream Out(OutName);
- MContext.mangleCanonicalTypeName(QualType(ETy, 0), Out);
+ CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(QualType(ETy, 0), Out);
return createScalarTypeNode(OutName, getChar(), Size);
}
@@ -434,7 +435,7 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
if (Features.CPlusPlus) {
// Don't use the mangler for C code.
llvm::raw_svector_ostream Out(OutName);
- MContext.mangleCanonicalTypeName(QualType(Ty, 0), Out);
+ CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(QualType(Ty, 0), Out);
} else {
OutName = RD->getName();
}
diff --git a/clang/lib/CodeGen/CodeGenTBAA.h b/clang/lib/CodeGen/CodeGenTBAA.h
index 5d9ecec3ff0fe..ba74a39a4d25e 100644
--- a/clang/lib/CodeGen/CodeGenTBAA.h
+++ b/clang/lib/CodeGen/CodeGenTBAA.h
@@ -24,7 +24,6 @@ namespace clang {
class ASTContext;
class CodeGenOptions;
class LangOptions;
- class MangleContext;
class QualType;
class Type;
@@ -120,7 +119,6 @@ class CodeGenTBAA {
llvm::Module &Module;
const CodeGenOptions &CodeGenOpts;
const LangOptions &Features;
- MangleContext &MContext;
// MDHelper - Helper for creating metadata.
llvm::MDBuilder MDHelper;
@@ -174,8 +172,7 @@ class CodeGenTBAA {
public:
CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes, llvm::Module &M,
- const CodeGenOptions &CGO, const LangOptions &Features,
- MangleContext &MContext);
+ const CodeGenOptions &CGO, const LangOptions &Features);
~CodeGenTBAA();
/// getTypeInfo - Get metadata used to describe accesses to objects of the
diff --git a/clang/lib/CodeGen/CodeGenTypes.cpp b/clang/lib/CodeGen/CodeGenTypes.cpp
index d823c336e39bf..0c41589230002 100644
--- a/clang/lib/CodeGen/CodeGenTypes.cpp
+++ b/clang/lib/CodeGen/CodeGenTypes.cpp
@@ -31,8 +31,7 @@ using namespace CodeGen;
CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
: CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
- Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
- TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
+ Target(cgm.getTarget()) {
SkippedLayout = false;
LongDoubleReferenced = false;
}
@@ -43,6 +42,12 @@ CodeGenTypes::~CodeGenTypes() {
delete &*I++;
}
+const ABIInfo &CodeGenTypes::getABIInfo() const {
+ return getCGM().getTargetCodeGenInfo().getABIInfo();
+}
+
+CGCXXABI &CodeGenTypes::getCXXABI() const { return getCGM().getCXXABI(); }
+
const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
return CGM.getCodeGenOpts();
}
diff --git a/clang/lib/CodeGen/CodeGenTypes.h b/clang/lib/CodeGen/CodeGenTypes.h
index 01c0c673795c0..9a474d6319b26 100644
--- a/clang/lib/CodeGen/CodeGenTypes.h
+++ b/clang/lib/CodeGen/CodeGenTypes.h
@@ -57,11 +57,6 @@ class CodeGenTypes {
ASTContext &Context;
llvm::Module &TheModule;
const TargetInfo &Target;
- CGCXXABI &TheCXXABI;
-
- // This should not be moved earlier, since its initialization depends on some
- // of the previous reference members being already initialized
- const ABIInfo &TheABIInfo;
/// The opaque type map for Objective-C interfaces. All direct
/// manipulation is done by the runtime interfaces, which are
@@ -106,9 +101,9 @@ class CodeGenTypes {
}
CodeGenModule &getCGM() const { return CGM; }
ASTContext &getContext() const { return Context; }
- const ABIInfo &getABIInfo() const { return TheABIInfo; }
+ const ABIInfo &getABIInfo() const;
const TargetInfo &getTarget() const { return Target; }
- CGCXXABI &getCXXABI() const { return TheCXXABI; }
+ CGCXXABI &getCXXABI() const;
llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
const CodeGenOptions &getCodeGenOpts() const;
More information about the cfe-commits
mailing list