Skip to content

Commit 6c62ad4

Browse files
authored
[clang-repl] [codegen] Reduce the state in TBAA. NFC for static compilation. (#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: #95581. cc: @jeaye
1 parent 341d86d commit 6c62ad4

10 files changed

+56
-45
lines changed

clang/lib/CodeGen/CGCall.cpp

+15-16
Original file line numberDiff line numberDiff line change
@@ -314,7 +314,8 @@ 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 =
318+
getCXXABI().getThisArgumentTypeForMethod(MD);
318319
return arrangeCXXMethodType(ThisType, prototype.getTypePtr(), MD);
319320
}
320321

@@ -337,7 +338,7 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
337338
SmallVector<CanQualType, 16> argTypes;
338339
SmallVector<FunctionProtoType::ExtParameterInfo, 16> paramInfos;
339340

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

343344
bool PassParams = true;
@@ -356,7 +357,7 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
356357
appendParameterTypes(*this, argTypes, paramInfos, FTP);
357358

358359
CGCXXABI::AddedStructorArgCounts AddedArgs =
359-
TheCXXABI.buildStructorSignature(GD, argTypes);
360+
getCXXABI().buildStructorSignature(GD, argTypes);
360361
if (!paramInfos.empty()) {
361362
// Note: prefix implies after the first param.
362363
if (AddedArgs.Prefix)
@@ -372,11 +373,10 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
372373
: RequiredArgs::All);
373374

374375
FunctionType::ExtInfo extInfo = FTP->getExtInfo();
375-
CanQualType resultType = TheCXXABI.HasThisReturn(GD)
376-
? argTypes.front()
377-
: TheCXXABI.hasMostDerivedReturn(GD)
378-
? CGM.getContext().VoidPtrTy
379-
: Context.VoidTy;
376+
CanQualType resultType = getCXXABI().HasThisReturn(GD) ? argTypes.front()
377+
: getCXXABI().hasMostDerivedReturn(GD)
378+
? CGM.getContext().VoidPtrTy
379+
: Context.VoidTy;
380380
return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::IsInstanceMethod,
381381
argTypes, extInfo, paramInfos, required);
382382
}
@@ -437,11 +437,10 @@ CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
437437
: RequiredArgs::All;
438438

439439
GlobalDecl GD(D, CtorKind);
440-
CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
441-
? ArgTypes.front()
442-
: TheCXXABI.hasMostDerivedReturn(GD)
443-
? CGM.getContext().VoidPtrTy
444-
: Context.VoidTy;
440+
CanQualType ResultType = getCXXABI().HasThisReturn(GD) ? ArgTypes.front()
441+
: getCXXABI().hasMostDerivedReturn(GD)
442+
? CGM.getContext().VoidPtrTy
443+
: Context.VoidTy;
445444

446445
FunctionType::ExtInfo Info = FPT->getExtInfo();
447446
llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> ParamInfos;
@@ -806,7 +805,7 @@ const CGFunctionInfo &CodeGenTypes::arrangeLLVMFunctionInfo(
806805
} else if (info.getCC() == CC_Swift || info.getCC() == CC_SwiftAsync) {
807806
swiftcall::computeABIInfo(CGM, *FI);
808807
} else {
809-
getABIInfo().computeInfo(*FI);
808+
CGM.getABIInfo().computeInfo(*FI);
810809
}
811810

812811
// Loop over all of the computed argument and return value info. If any of
@@ -6033,6 +6032,6 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr,
60336032
: EmitVAListRef(VE->getSubExpr());
60346033
QualType Ty = VE->getType();
60356034
if (VE->isMicrosoftABI())
6036-
return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
6037-
return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
6035+
return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
6036+
return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
60386037
}

clang/lib/CodeGen/CGClass.cpp

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

211211
llvm::Type *PtrDiffTy =
212-
Types.ConvertType(getContext().getPointerDiffType());
212+
getTypes().ConvertType(getContext().getPointerDiffType());
213213

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

clang/lib/CodeGen/CodeGenModule.cpp

+9-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.
@@ -1465,12 +1466,12 @@ void CodeGenModule::EmitBackendOptionsMetadata(
14651466

14661467
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
14671468
// Make sure that this type is translated.
1468-
Types.UpdateCompletedType(TD);
1469+
getTypes().UpdateCompletedType(TD);
14691470
}
14701471

14711472
void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
14721473
// Make sure that this type is translated.
1473-
Types.RefreshTypeCacheForClass(RD);
1474+
getTypes().RefreshTypeCacheForClass(RD);
14741475
}
14751476

14761477
llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
@@ -5405,6 +5406,10 @@ void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
54055406
GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
54065407
}
54075408

5409+
const ABIInfo &CodeGenModule::getABIInfo() {
5410+
return getTargetCodeGenInfo().getABIInfo();
5411+
}
5412+
54085413
/// Pass IsTentative as true if you want to create a tentative definition.
54095414
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
54105415
bool IsTentative) {
@@ -7813,7 +7818,5 @@ void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) {
78137818

78147819
NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
78157820

7816-
NewBuilder->TBAA = std::move(TBAA);
7817-
78187821
NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
78197822
}

clang/lib/CodeGen/CodeGenModule.h

+3-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;
@@ -776,14 +776,15 @@ class CodeGenModule : public CodeGenTypeCache {
776776
bool supportsCOMDAT() const;
777777
void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);
778778

779+
const ABIInfo &getABIInfo();
779780
CGCXXABI &getCXXABI() const { return *ABI; }
780781
llvm::LLVMContext &getLLVMContext() { return VMContext; }
781782

782783
bool shouldUseTBAA() const { return TBAA != nullptr; }
783784

784785
const TargetCodeGenInfo &getTargetCodeGenInfo();
785786

786-
CodeGenTypes &getTypes() { return Types; }
787+
CodeGenTypes &getTypes() { return *Types; }
787788

788789
CodeGenVTables &getVTables() { return VTables; }
789790

clang/lib/CodeGen/CodeGenTBAA.cpp

+8-5
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
#include "CodeGenTBAA.h"
1818
#include "ABIInfoImpl.h"
19+
#include "CGCXXABI.h"
1920
#include "CGRecordLayout.h"
2021
#include "CodeGenTypes.h"
2122
#include "clang/AST/ASTContext.h"
@@ -36,10 +37,10 @@ using namespace CodeGen;
3637

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

4445
CodeGenTBAA::~CodeGenTBAA() {
4546
}
@@ -256,7 +257,8 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
256257

257258
SmallString<256> OutName;
258259
llvm::raw_svector_ostream Out(OutName);
259-
MContext.mangleCanonicalTypeName(QualType(ETy, 0), Out);
260+
CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(
261+
QualType(ETy, 0), Out);
260262
return createScalarTypeNode(OutName, getChar(), Size);
261263
}
262264

@@ -481,7 +483,8 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
481483
if (Features.CPlusPlus) {
482484
// Don't use the mangler for C code.
483485
llvm::raw_svector_ostream Out(OutName);
484-
MContext.mangleCanonicalTypeName(QualType(Ty, 0), Out);
486+
CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(
487+
QualType(Ty, 0), Out);
485488
} else {
486489
OutName = RD->getName();
487490
}

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

+4-3
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,8 @@ using namespace clang;
3131
using namespace CodeGen;
3232

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

46+
CGCXXABI &CodeGenTypes::getCXXABI() const { return getCGM().getCXXABI(); }
47+
4748
const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
4849
return CGM.getCodeGenOpts();
4950
}

clang/lib/CodeGen/CodeGenTypes.h

+1-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,8 @@ class CodeGenTypes {
106101
}
107102
CodeGenModule &getCGM() const { return CGM; }
108103
ASTContext &getContext() const { return Context; }
109-
const ABIInfo &getABIInfo() const { return TheABIInfo; }
110104
const TargetInfo &getTarget() const { return Target; }
111-
CGCXXABI &getCXXABI() const { return TheCXXABI; }
105+
CGCXXABI &getCXXABI() const;
112106
llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
113107
const CodeGenOptions &getCodeGenOpts() const;
114108

clang/lib/CodeGen/MicrosoftCXXABI.cpp

+1-1
Original file line numberDiff line numberDiff line change
@@ -1111,7 +1111,7 @@ static bool isTrivialForMSVC(const CXXRecordDecl *RD, QualType Ty,
11111111
const Type *Base = nullptr;
11121112
uint64_t NumElts = 0;
11131113
if (CGM.getTarget().getTriple().isAArch64() &&
1114-
CGM.getTypes().getABIInfo().isHomogeneousAggregate(Ty, Base, NumElts) &&
1114+
CGM.getABIInfo().isHomogeneousAggregate(Ty, Base, NumElts) &&
11151115
isa<VectorType>(Base)) {
11161116
return true;
11171117
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
// REQUIRES: host-supports-jit
2+
// UNSUPPORTED: system-aix
3+
//
4+
// RUN: cat %s | clang-repl | FileCheck %s
5+
// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s
6+
7+
struct box { box() = default; box(int *const data) : data{data} {} int *data{}; };
8+
9+
box foo() { box ret; ret = new int{}; return ret; }
10+
11+
extern "C" int printf(const char *, ...);
12+
printf("good");
13+
// CHECK: good

0 commit comments

Comments
 (0)