Skip to content

Commit ffb489b

Browse files
committed
[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#95581.
1 parent aa9e4f0 commit ffb489b

File tree

8 files changed

+31
-34
lines changed

8 files changed

+31
-34
lines changed

clang/lib/CodeGen/CGCall.cpp

+7-7
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,7 @@ CodeGenTypes::arrangeCXXMethodDeclaration(const CXXMethodDecl *MD) {
314314

315315
if (MD->isImplicitObjectMemberFunction()) {
316316
// The abstract case is perfectly fine.
317-
const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(MD);
317+
const CXXRecordDecl *ThisType = getCXXABI().getThisArgumentTypeForMethod(MD);
318318
return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
319319
}
320320

@@ -337,7 +337,7 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
337337
SmallVector<CanQualType, 16> argTypes;
338338
SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
339339

340-
const CXXRecordDecl *ThisType = TheCXXABI.getThisArgumentTypeForMethod(GD);
340+
const CXXRecordDecl *ThisType = getCXXABI().getThisArgumentTypeForMethod(GD);
341341
argTypes.push_back(DeriveThisType(ThisType, MD));
342342

343343
bool PassParams = true;
@@ -356,7 +356,7 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
356356
appendParameterTypes(*this, argTypes, paramInfos, FTP);
357357

358358
CGCXXABI::AddedStructorArgCounts AddedArgs =
359-
TheCXXABI.buildStructorSignature(GD, argTypes);
359+
getCXXABI().buildStructorSignature(GD, argTypes);
360360
if (!paramInfos.empty()) {
361361
// Note: prefix implies after the first param.
362362
if (AddedArgs.Prefix)
@@ -372,9 +372,9 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
372372
: RequiredArgs::All);
373373

374374
FunctionType::ExtInfo extInfo = FTP->getExtInfo();
375-
CanQualType resultType = TheCXXABI.HasThisReturn(GD)
375+
CanQualType resultType = getCXXABI().HasThisReturn(GD)
376376
? argTypes.front()
377-
: TheCXXABI.hasMostDerivedReturn(GD)
377+
: getCXXABI().hasMostDerivedReturn(GD)
378378
? CGM.getContext().VoidPtrTy
379379
: Context.VoidTy;
380380
return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::IsInstanceMethod,
@@ -437,9 +437,9 @@ CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
437437
: RequiredArgs::All;
438438

439439
GlobalDecl GD(D, CtorKind);
440-
CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
440+
CanQualType ResultType = getCXXABI().HasThisReturn(GD)
441441
? ArgTypes.front()
442-
: TheCXXABI.hasMostDerivedReturn(GD)
442+
: getCXXABI().hasMostDerivedReturn(GD)
443443
? CGM.getContext().VoidPtrTy
444444
: Context.VoidTy;
445445

clang/lib/CodeGen/CGClass.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -208,7 +208,7 @@ CodeGenModule::GetNonVirtualBaseClassOffset(const CXXRecordDecl *ClassDecl,
208208
return nullptr;
209209

210210
llvm::Type *PtrDiffTy =
211-
Types.ConvertType(getContext().getPointerDiffType());
211+
getTypes().ConvertType(getContext().getPointerDiffType());
212212

213213
return llvm::ConstantInt::get(PtrDiffTy, Offset.getQuantity());
214214
}

clang/lib/CodeGen/CodeGenModule.cpp

+5-6
Original file line numberDiff line numberDiff line change
@@ -341,10 +341,11 @@ CodeGenModule::CodeGenModule(ASTContext &C,
341341
: Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
342342
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
343343
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
344-
VMContext(M.getContext()), Types(*this), VTables(*this),
344+
VMContext(M.getContext()), VTables(*this),
345345
SanitizerMD(new SanitizerMetadata(*this)) {
346346

347347
// Initialize the type cache.
348+
Types.reset(new CodeGenTypes(*this));
348349
llvm::LLVMContext &LLVMContext = M.getContext();
349350
VoidTy = llvm::Type::getVoidTy(LLVMContext);
350351
Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
@@ -403,7 +404,7 @@ CodeGenModule::CodeGenModule(ASTContext &C,
403404
if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
404405
(!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
405406
TBAA.reset(new CodeGenTBAA(Context, getTypes(), TheModule, CodeGenOpts,
406-
getLangOpts(), getCXXABI().getMangleContext()));
407+
getLangOpts()));
407408

408409
// If debug info or coverage generation is enabled, create the CGDebugInfo
409410
// object.
@@ -1445,12 +1446,12 @@ void CodeGenModule::EmitBackendOptionsMetadata(
14451446

14461447
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
14471448
// Make sure that this type is translated.
1448-
Types.UpdateCompletedType(TD);
1449+
getTypes().UpdateCompletedType(TD);
14491450
}
14501451

14511452
void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
14521453
// Make sure that this type is translated.
1453-
Types.RefreshTypeCacheForClass(RD);
1454+
getTypes().RefreshTypeCacheForClass(RD);
14541455
}
14551456

14561457
llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
@@ -7766,7 +7767,5 @@ void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) {
77667767

77677768
NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
77687769

7769-
NewBuilder->TBAA = std::move(TBAA);
7770-
77717770
NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
77727771
}

clang/lib/CodeGen/CodeGenModule.h

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ class CodeGenModule : public CodeGenTypeCache {
320320
// This should not be moved earlier, since its initialization depends on some
321321
// of the previous reference members being already initialized and also checks
322322
// if TheTargetCodeGenInfo is NULL
323-
CodeGenTypes Types;
323+
std::unique_ptr<CodeGenTypes> Types;
324324

325325
/// Holds information about C++ vtables.
326326
CodeGenVTables VTables;
@@ -780,7 +780,7 @@ class CodeGenModule : public CodeGenTypeCache {
780780

781781
const TargetCodeGenInfo &getTargetCodeGenInfo();
782782

783-
CodeGenTypes &getTypes() { return Types; }
783+
CodeGenTypes &getTypes() { return *Types; }
784784

785785
CodeGenVTables &getVTables() { return VTables; }
786786

clang/lib/CodeGen/CodeGenTBAA.cpp

+6-5
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
//===----------------------------------------------------------------------===//
1616

1717
#include "CodeGenTBAA.h"
18+
#include "CGCXXABI.h"
1819
#include "CGRecordLayout.h"
1920
#include "CodeGenTypes.h"
2021
#include "clang/AST/ASTContext.h"
@@ -35,10 +36,10 @@ using namespace CodeGen;
3536

3637
CodeGenTBAA::CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes,
3738
llvm::Module &M, const CodeGenOptions &CGO,
38-
const LangOptions &Features, MangleContext &MContext)
39+
const LangOptions &Features)
3940
: Context(Ctx), CGTypes(CGTypes), Module(M), CodeGenOpts(CGO),
40-
Features(Features), MContext(MContext), MDHelper(M.getContext()),
41-
Root(nullptr), Char(nullptr) {}
41+
Features(Features), MDHelper(M.getContext()), Root(nullptr),
42+
Char(nullptr) {}
4243

4344
CodeGenTBAA::~CodeGenTBAA() {
4445
}
@@ -209,7 +210,7 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
209210

210211
SmallString<256> OutName;
211212
llvm::raw_svector_ostream Out(OutName);
212-
MContext.mangleCanonicalTypeName(QualType(ETy, 0), Out);
213+
CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(QualType(ETy, 0), Out);
213214
return createScalarTypeNode(OutName, getChar(), Size);
214215
}
215216

@@ -434,7 +435,7 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
434435
if (Features.CPlusPlus) {
435436
// Don't use the mangler for C code.
436437
llvm::raw_svector_ostream Out(OutName);
437-
MContext.mangleCanonicalTypeName(QualType(Ty, 0), Out);
438+
CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(QualType(Ty, 0), Out);
438439
} else {
439440
OutName = RD->getName();
440441
}

clang/lib/CodeGen/CodeGenTBAA.h

+1-4
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ namespace clang {
2424
class ASTContext;
2525
class CodeGenOptions;
2626
class LangOptions;
27-
class MangleContext;
2827
class QualType;
2928
class Type;
3029

@@ -120,7 +119,6 @@ class CodeGenTBAA {
120119
llvm::Module &Module;
121120
const CodeGenOptions &CodeGenOpts;
122121
const LangOptions &Features;
123-
MangleContext &MContext;
124122

125123
// MDHelper - Helper for creating metadata.
126124
llvm::MDBuilder MDHelper;
@@ -174,8 +172,7 @@ class CodeGenTBAA {
174172

175173
public:
176174
CodeGenTBAA(ASTContext &Ctx, CodeGenTypes &CGTypes, llvm::Module &M,
177-
const CodeGenOptions &CGO, const LangOptions &Features,
178-
MangleContext &MContext);
175+
const CodeGenOptions &CGO, const LangOptions &Features);
179176
~CodeGenTBAA();
180177

181178
/// getTypeInfo - Get metadata used to describe accesses to objects of the

clang/lib/CodeGen/CodeGenTypes.cpp

+7-2
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,7 @@ using namespace CodeGen;
3131

3232
CodeGenTypes::CodeGenTypes(CodeGenModule &cgm)
3333
: CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
34-
Target(cgm.getTarget()), TheCXXABI(cgm.getCXXABI()),
35-
TheABIInfo(cgm.getTargetCodeGenInfo().getABIInfo()) {
34+
Target(cgm.getTarget()) {
3635
SkippedLayout = false;
3736
LongDoubleReferenced = false;
3837
}
@@ -43,6 +42,12 @@ CodeGenTypes::~CodeGenTypes() {
4342
delete &*I++;
4443
}
4544

45+
const ABIInfo &CodeGenTypes::getABIInfo() const {
46+
return getCGM().getTargetCodeGenInfo().getABIInfo();
47+
}
48+
49+
CGCXXABI &CodeGenTypes::getCXXABI() const { return getCGM().getCXXABI(); }
50+
4651
const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
4752
return CGM.getCodeGenOpts();
4853
}

clang/lib/CodeGen/CodeGenTypes.h

+2-7
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,6 @@ class CodeGenTypes {
5757
ASTContext &Context;
5858
llvm::Module &TheModule;
5959
const TargetInfo &Target;
60-
CGCXXABI &TheCXXABI;
61-
62-
// This should not be moved earlier, since its initialization depends on some
63-
// of the previous reference members being already initialized
64-
const ABIInfo &TheABIInfo;
6560

6661
/// The opaque type map for Objective-C interfaces. All direct
6762
/// manipulation is done by the runtime interfaces, which are
@@ -106,9 +101,9 @@ class CodeGenTypes {
106101
}
107102
CodeGenModule &getCGM() const { return CGM; }
108103
ASTContext &getContext() const { return Context; }
109-
const ABIInfo &getABIInfo() const { return TheABIInfo; }
104+
const ABIInfo &getABIInfo() const;
110105
const TargetInfo &getTarget() const { return Target; }
111-
CGCXXABI &getCXXABI() const { return TheCXXABI; }
106+
CGCXXABI &getCXXABI() const;
112107
llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
113108
const CodeGenOptions &getCodeGenOpts() const;
114109

0 commit comments

Comments
 (0)