Skip to content

Commit 7ba7d8e

Browse files
vgvassilevtru
authored andcommitted
[clang-repl] [codegen] Reduce the state in TBAA. NFC for static compilation. (llvm#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#95581. cc: @jeaye (cherry picked from commit 6c62ad4)
1 parent 6892827 commit 7ba7d8e

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
@@ -6022,6 +6021,6 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr,
60226021
: EmitVAListRef(VE->getSubExpr());
60236022
QualType Ty = VE->getType();
60246023
if (VE->isMicrosoftABI())
6025-
return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
6026-
return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
6024+
return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
6025+
return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
60276026
}

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
@@ -343,10 +343,11 @@ CodeGenModule::CodeGenModule(ASTContext &C,
343343
: Context(C), LangOpts(C.getLangOpts()), FS(FS), HeaderSearchOpts(HSO),
344344
PreprocessorOpts(PPO), CodeGenOpts(CGO), TheModule(M), Diags(diags),
345345
Target(C.getTargetInfo()), ABI(createCXXABI(*this)),
346-
VMContext(M.getContext()), Types(*this), VTables(*this),
346+
VMContext(M.getContext()), VTables(*this),
347347
SanitizerMD(new SanitizerMetadata(*this)) {
348348

349349
// Initialize the type cache.
350+
Types.reset(new CodeGenTypes(*this));
350351
llvm::LLVMContext &LLVMContext = M.getContext();
351352
VoidTy = llvm::Type::getVoidTy(LLVMContext);
352353
Int8Ty = llvm::Type::getInt8Ty(LLVMContext);
@@ -405,7 +406,7 @@ CodeGenModule::CodeGenModule(ASTContext &C,
405406
if (LangOpts.Sanitize.has(SanitizerKind::Thread) ||
406407
(!CodeGenOpts.RelaxedAliasing && CodeGenOpts.OptimizationLevel > 0))
407408
TBAA.reset(new CodeGenTBAA(Context, getTypes(), TheModule, CodeGenOpts,
408-
getLangOpts(), getCXXABI().getMangleContext()));
409+
getLangOpts()));
409410

410411
// If debug info or coverage generation is enabled, create the CGDebugInfo
411412
// object.
@@ -1452,12 +1453,12 @@ void CodeGenModule::EmitBackendOptionsMetadata(
14521453

14531454
void CodeGenModule::UpdateCompletedType(const TagDecl *TD) {
14541455
// Make sure that this type is translated.
1455-
Types.UpdateCompletedType(TD);
1456+
getTypes().UpdateCompletedType(TD);
14561457
}
14571458

14581459
void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
14591460
// Make sure that this type is translated.
1460-
Types.RefreshTypeCacheForClass(RD);
1461+
getTypes().RefreshTypeCacheForClass(RD);
14611462
}
14621463

14631464
llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
@@ -5376,6 +5377,10 @@ void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
53765377
GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
53775378
}
53785379

5380+
const ABIInfo &CodeGenModule::getABIInfo() {
5381+
return getTargetCodeGenInfo().getABIInfo();
5382+
}
5383+
53795384
/// Pass IsTentative as true if you want to create a tentative definition.
53805385
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
53815386
bool IsTentative) {
@@ -7784,7 +7789,5 @@ void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) {
77847789

77857790
NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
77867791

7787-
NewBuilder->TBAA = std::move(TBAA);
7788-
77897792
NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
77907793
}

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
@@ -30,9 +30,8 @@ using namespace clang;
3030
using namespace CodeGen;
3131

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

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

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)