Skip to content

Commit

Permalink
[clang-repl] [codegen] Reduce the state in TBAA. NFC for static compi…
Browse files Browse the repository at this point in the history
…lation. (#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
(cherry picked from commit 6c62ad4)
  • Loading branch information
vgvassilev authored and tru committed Oct 15, 2024
1 parent 6892827 commit 7ba7d8e
Show file tree
Hide file tree
Showing 10 changed files with 56 additions and 45 deletions.
31 changes: 15 additions & 16 deletions clang/lib/CodeGen/CGCall.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,7 +314,8 @@ 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);
}

Expand All @@ -337,7 +338,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;
Expand All @@ -356,7 +357,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)
Expand All @@ -372,11 +373,10 @@ CodeGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
: RequiredArgs::All);

FunctionType::ExtInfo extInfo = FTP->getExtInfo();
CanQualType resultType = TheCXXABI.HasThisReturn(GD)
? argTypes.front()
: TheCXXABI.hasMostDerivedReturn(GD)
? CGM.getContext().VoidPtrTy
: Context.VoidTy;
CanQualType resultType = getCXXABI().HasThisReturn(GD) ? argTypes.front()
: getCXXABI().hasMostDerivedReturn(GD)
? CGM.getContext().VoidPtrTy
: Context.VoidTy;
return arrangeLLVMFunctionInfo(resultType, FnInfoOpts::IsInstanceMethod,
argTypes, extInfo, paramInfos, required);
}
Expand Down Expand Up @@ -437,11 +437,10 @@ CodeGenTypes::arrangeCXXConstructorCall(const CallArgList &args,
: RequiredArgs::All;

GlobalDecl GD(D, CtorKind);
CanQualType ResultType = TheCXXABI.HasThisReturn(GD)
? ArgTypes.front()
: TheCXXABI.hasMostDerivedReturn(GD)
? CGM.getContext().VoidPtrTy
: Context.VoidTy;
CanQualType ResultType = getCXXABI().HasThisReturn(GD) ? ArgTypes.front()
: getCXXABI().hasMostDerivedReturn(GD)
? CGM.getContext().VoidPtrTy
: Context.VoidTy;

FunctionType::ExtInfo Info = FPT->getExtInfo();
llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> ParamInfos;
Expand Down Expand Up @@ -806,7 +805,7 @@ const CGFunctionInfo &CodeGenTypes::arrangeLLVMFunctionInfo(
} else if (info.getCC() == CC_Swift || info.getCC() == CC_SwiftAsync) {
swiftcall::computeABIInfo(CGM, *FI);
} else {
getABIInfo().computeInfo(*FI);
CGM.getABIInfo().computeInfo(*FI);
}

// Loop over all of the computed argument and return value info. If any of
Expand Down Expand Up @@ -6022,6 +6021,6 @@ RValue CodeGenFunction::EmitVAArg(VAArgExpr *VE, Address &VAListAddr,
: EmitVAListRef(VE->getSubExpr());
QualType Ty = VE->getType();
if (VE->isMicrosoftABI())
return CGM.getTypes().getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
return CGM.getTypes().getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
return CGM.getABIInfo().EmitMSVAArg(*this, VAListAddr, Ty, Slot);
return CGM.getABIInfo().EmitVAArg(*this, VAListAddr, Ty, Slot);
}
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/CGClass.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -209,7 +209,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());
}
Expand Down
15 changes: 9 additions & 6 deletions clang/lib/CodeGen/CodeGenModule.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -343,10 +343,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);
Expand Down Expand Up @@ -405,7 +406,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.
Expand Down Expand Up @@ -1452,12 +1453,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) {
Expand Down Expand Up @@ -5376,6 +5377,10 @@ void CodeGenModule::maybeSetTrivialComdat(const Decl &D,
GO.setComdat(TheModule.getOrInsertComdat(GO.getName()));
}

const ABIInfo &CodeGenModule::getABIInfo() {
return getTargetCodeGenInfo().getABIInfo();
}

/// Pass IsTentative as true if you want to create a tentative definition.
void CodeGenModule::EmitGlobalVarDefinition(const VarDecl *D,
bool IsTentative) {
Expand Down Expand Up @@ -7784,7 +7789,5 @@ void CodeGenModule::moveLazyEmissionStates(CodeGenModule *NewBuilder) {

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

NewBuilder->TBAA = std::move(TBAA);

NewBuilder->ABI->MangleCtx = std::move(ABI->MangleCtx);
}
5 changes: 3 additions & 2 deletions clang/lib/CodeGen/CodeGenModule.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -776,14 +776,15 @@ class CodeGenModule : public CodeGenTypeCache {
bool supportsCOMDAT() const;
void maybeSetTrivialComdat(const Decl &D, llvm::GlobalObject &GO);

const ABIInfo &getABIInfo();
CGCXXABI &getCXXABI() const { return *ABI; }
llvm::LLVMContext &getLLVMContext() { return VMContext; }

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

const TargetCodeGenInfo &getTargetCodeGenInfo();

CodeGenTypes &getTypes() { return Types; }
CodeGenTypes &getTypes() { return *Types; }

CodeGenVTables &getVTables() { return VTables; }

Expand Down
13 changes: 8 additions & 5 deletions clang/lib/CodeGen/CodeGenTBAA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#include "CodeGenTBAA.h"
#include "ABIInfoImpl.h"
#include "CGCXXABI.h"
#include "CGRecordLayout.h"
#include "CodeGenTypes.h"
#include "clang/AST/ASTContext.h"
Expand All @@ -36,10 +37,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() {
}
Expand Down Expand Up @@ -256,7 +257,8 @@ 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);
}

Expand Down Expand Up @@ -481,7 +483,8 @@ 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();
}
Expand Down
5 changes: 1 addition & 4 deletions clang/lib/CodeGen/CodeGenTBAA.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace clang {
class ASTContext;
class CodeGenOptions;
class LangOptions;
class MangleContext;
class QualType;
class Type;

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions clang/lib/CodeGen/CodeGenTypes.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@ using namespace clang;
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()) {
: CGM(cgm), Context(cgm.getContext()), TheModule(cgm.getModule()),
Target(cgm.getTarget()) {
SkippedLayout = false;
LongDoubleReferenced = false;
}
Expand All @@ -43,6 +42,8 @@ CodeGenTypes::~CodeGenTypes() {
delete &*I++;
}

CGCXXABI &CodeGenTypes::getCXXABI() const { return getCGM().getCXXABI(); }

const CodeGenOptions &CodeGenTypes::getCodeGenOpts() const {
return CGM.getCodeGenOpts();
}
Expand Down
8 changes: 1 addition & 7 deletions clang/lib/CodeGen/CodeGenTypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -106,9 +101,8 @@ class CodeGenTypes {
}
CodeGenModule &getCGM() const { return CGM; }
ASTContext &getContext() const { return Context; }
const ABIInfo &getABIInfo() const { return TheABIInfo; }
const TargetInfo &getTarget() const { return Target; }
CGCXXABI &getCXXABI() const { return TheCXXABI; }
CGCXXABI &getCXXABI() const;
llvm::LLVMContext &getLLVMContext() { return TheModule.getContext(); }
const CodeGenOptions &getCodeGenOpts() const;

Expand Down
2 changes: 1 addition & 1 deletion clang/lib/CodeGen/MicrosoftCXXABI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1111,7 +1111,7 @@ static bool isTrivialForMSVC(const CXXRecordDecl *RD, QualType Ty,
const Type *Base = nullptr;
uint64_t NumElts = 0;
if (CGM.getTarget().getTriple().isAArch64() &&
CGM.getTypes().getABIInfo().isHomogeneousAggregate(Ty, Base, NumElts) &&
CGM.getABIInfo().isHomogeneousAggregate(Ty, Base, NumElts) &&
isa<VectorType>(Base)) {
return true;
}
Expand Down
13 changes: 13 additions & 0 deletions clang/test/Interpreter/assigment-with-implicit-ctor.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// REQUIRES: host-supports-jit
// UNSUPPORTED: system-aix
//
// RUN: cat %s | clang-repl | FileCheck %s
// RUN: cat %s | clang-repl -Xcc -O2 | FileCheck %s

struct box { box() = default; box(int *const data) : data{data} {} int *data{}; };

box foo() { box ret; ret = new int{}; return ret; }

extern "C" int printf(const char *, ...);
printf("good");
// CHECK: good

0 comments on commit 7ba7d8e

Please sign in to comment.