Skip to content

Commit 25387b0

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 c5f402f commit 25387b0

File tree

9 files changed

+52
-41
lines changed

9 files changed

+52
-41
lines changed

clang/lib/CodeGen/CGCall.cpp

Lines changed: 12 additions & 13 deletions
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;

clang/lib/CodeGen/CGClass.cpp

Lines changed: 1 addition & 1 deletion
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

Lines changed: 5 additions & 6 deletions
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.
@@ -1450,12 +1451,12 @@ void CodeGenModule::EmitBackendOptionsMetadata(
14501451

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

14561457
void CodeGenModule::RefreshTypeCacheForClass(const CXXRecordDecl *RD) {
14571458
// Make sure that this type is translated.
1458-
Types.RefreshTypeCacheForClass(RD);
1459+
getTypes().RefreshTypeCacheForClass(RD);
14591460
}
14601461

14611462
llvm::MDNode *CodeGenModule::getTBAATypeInfo(QualType QTy) {
@@ -7779,7 +7780,5 @@ void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) {
77797780

77807781
NewBuilder->WeakRefReferences = std::move(WeakRefReferences);
77817782

7782-
NewBuilder->TBAA = std::move(TBAA);
7783-
77847783
NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
77857784
}

clang/lib/CodeGen/CodeGenModule.h

Lines changed: 2 additions & 2 deletions
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;
@@ -783,7 +783,7 @@ class CodeGenModule : public CodeGenTypeCache {
783783

784784
const TargetCodeGenInfo &getTargetCodeGenInfo();
785785

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

788788
CodeGenVTables &getVTables() { return VTables; }
789789

clang/lib/CodeGen/CodeGenTBAA.cpp

Lines changed: 8 additions & 5 deletions
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
}
@@ -210,7 +211,8 @@ llvm::MDNode *CodeGenTBAA::getTypeInfoHelper(const Type *Ty) {
210211

211212
SmallString<256> OutName;
212213
llvm::raw_svector_ostream Out(OutName);
213-
MContext.mangleCanonicalTypeName(QualType(ETy, 0), Out);
214+
CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(
215+
QualType(ETy, 0), Out);
214216
return createScalarTypeNode(OutName, getChar(), Size);
215217
}
216218

@@ -435,7 +437,8 @@ llvm::MDNode *CodeGenTBAA::getBaseTypeInfoHelper(const Type *Ty) {
435437
if (Features.CPlusPlus) {
436438
// Don't use the mangler for C code.
437439
llvm::raw_svector_ostream Out(OutName);
438-
MContext.mangleCanonicalTypeName(QualType(Ty, 0), Out);
440+
CGTypes.getCXXABI().getMangleContext().mangleCanonicalTypeName(
441+
QualType(Ty, 0), Out);
439442
} else {
440443
OutName = RD->getName();
441444
}

clang/lib/CodeGen/CodeGenTBAA.h

Lines changed: 1 addition & 4 deletions
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

Lines changed: 8 additions & 3 deletions
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,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

Lines changed: 2 additions & 7 deletions
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

Lines changed: 13 additions & 0 deletions
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)