Skip to content

Commit cfe7c63

Browse files
authored
[CIR][NFC] Cleanup: MLIRContext vs ASTContext (#1217)
ClangIR CodeGen makes use of both `mlir::MLIRContext` and `clang::ASTContext`. Referring to these as just "context" can be ambiguous. This change attempts to make the CodeGen code more clear by choosing better variable names. The change is almost entirely renaming: mostly change variables, parameters, and fields named `context` or `ctx` to `mlirContext` or `astContext`. There are some other renames having to do with contexts, and a small number of other drive-by fixes. (I considered also renaming functions named `getContext()`, but did not include that in this change.) This is entirely code cleanup. There should be no behavior changes.
1 parent 3189c0d commit cfe7c63

25 files changed

+282
-265
lines changed

clang/include/clang/CIR/CIRGenerator.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ namespace cir {
4545
class CIRGenerator : public clang::ASTConsumer {
4646
virtual void anchor();
4747
clang::DiagnosticsEngine &Diags;
48-
clang::ASTContext *astCtx;
48+
clang::ASTContext *astContext;
4949
llvm::IntrusiveRefCntPtr<llvm::vfs::FileSystem>
5050
fs; // Only used for debug info.
5151

@@ -71,7 +71,7 @@ class CIRGenerator : public clang::ASTConsumer {
7171
};
7272

7373
protected:
74-
std::unique_ptr<mlir::MLIRContext> mlirCtx;
74+
std::unique_ptr<mlir::MLIRContext> mlirContext;
7575
std::unique_ptr<clang::CIRGen::CIRGenModule> CGM;
7676

7777
private:
@@ -86,7 +86,7 @@ class CIRGenerator : public clang::ASTConsumer {
8686
bool EmitFunction(const clang::FunctionDecl *FD);
8787

8888
bool HandleTopLevelDecl(clang::DeclGroupRef D) override;
89-
void HandleTranslationUnit(clang::ASTContext &Ctx) override;
89+
void HandleTranslationUnit(clang::ASTContext &astContext) override;
9090
void HandleInlineFunctionDefinition(clang::FunctionDecl *D) override;
9191
void HandleTagDeclDefinition(clang::TagDecl *D) override;
9292
void HandleTagDeclRequiredDefinition(const clang::TagDecl *D) override;
@@ -96,7 +96,7 @@ class CIRGenerator : public clang::ASTConsumer {
9696

9797
mlir::ModuleOp getModule();
9898
std::unique_ptr<mlir::MLIRContext> takeContext() {
99-
return std::move(mlirCtx);
99+
return std::move(mlirContext);
100100
};
101101

102102
bool verifyModule();

clang/lib/CIR/CodeGen/CIRGenBuiltin.cpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -154,12 +154,12 @@ struct WidthAndSignedness {
154154
} // namespace
155155

156156
static WidthAndSignedness
157-
getIntegerWidthAndSignedness(const clang::ASTContext &context,
157+
getIntegerWidthAndSignedness(const clang::ASTContext &astContext,
158158
const clang::QualType Type) {
159159
assert(Type->isIntegerType() && "Given type is not an integer.");
160160
unsigned Width = Type->isBooleanType() ? 1
161-
: Type->isBitIntType() ? context.getIntWidth(Type)
162-
: context.getTypeInfo(Type).Width;
161+
: Type->isBitIntType() ? astContext.getIntWidth(Type)
162+
: astContext.getTypeInfo(Type).Width;
163163
bool Signed = Type->isSignedIntegerType();
164164
return {Width, Signed};
165165
}
@@ -224,11 +224,11 @@ static mlir::Value emitSignBit(mlir::Location loc, CIRGenFunction &CGF,
224224
}
225225

226226
static Address checkAtomicAlignment(CIRGenFunction &CGF, const CallExpr *E) {
227-
ASTContext &ctx = CGF.getContext();
227+
ASTContext &astContext = CGF.getContext();
228228
Address ptr = CGF.emitPointerWithAlignment(E->getArg(0));
229229
unsigned bytes =
230230
isa<cir::PointerType>(ptr.getElementType())
231-
? ctx.getTypeSizeInChars(ctx.VoidPtrTy).getQuantity()
231+
? astContext.getTypeSizeInChars(astContext.VoidPtrTy).getQuantity()
232232
: CGF.CGM.getDataLayout().getTypeSizeInBits(ptr.getElementType()) / 8;
233233
unsigned align = ptr.getAlignment().getQuantity();
234234
if (align % bytes != 0) {
@@ -331,10 +331,10 @@ static mlir::Value MakeAtomicCmpXchgValue(CIRGenFunction &cgf,
331331
}
332332

333333
static bool
334-
typeRequiresBuiltinLaunderImp(const ASTContext &ctx, QualType ty,
334+
typeRequiresBuiltinLaunderImp(const ASTContext &astContext, QualType ty,
335335
llvm::SmallPtrSetImpl<const Decl *> &seen) {
336-
if (const auto *arr = ctx.getAsArrayType(ty))
337-
ty = ctx.getBaseElementType(arr);
336+
if (const auto *arr = astContext.getAsArrayType(ty))
337+
ty = astContext.getBaseElementType(arr);
338338

339339
const auto *record = ty->getAsCXXRecordDecl();
340340
if (!record)
@@ -351,7 +351,7 @@ typeRequiresBuiltinLaunderImp(const ASTContext &ctx, QualType ty,
351351
return true;
352352

353353
for (FieldDecl *fld : record->fields()) {
354-
if (typeRequiresBuiltinLaunderImp(ctx, fld->getType(), seen))
354+
if (typeRequiresBuiltinLaunderImp(astContext, fld->getType(), seen))
355355
return true;
356356
}
357357
return false;
@@ -2641,7 +2641,7 @@ mlir::Value CIRGenFunction::evaluateOrEmitBuiltinObjectSize(
26412641
/// for "fabsf".
26422642
cir::FuncOp CIRGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
26432643
unsigned BuiltinID) {
2644-
assert(astCtx.BuiltinInfo.isLibFunction(BuiltinID));
2644+
assert(astContext.BuiltinInfo.isLibFunction(BuiltinID));
26452645

26462646
// Get the name, skip over the __builtin_ prefix (if necessary).
26472647
StringRef Name;
@@ -2703,7 +2703,7 @@ cir::FuncOp CIRGenModule::getBuiltinLibFunction(const FunctionDecl *FD,
27032703
AIXLongDouble64Builtins.end())
27042704
Name = AIXLongDouble64Builtins[BuiltinID];
27052705
else
2706-
Name = astCtx.BuiltinInfo.getName(BuiltinID).substr(10);
2706+
Name = astContext.BuiltinInfo.getName(BuiltinID).substr(10);
27072707
}
27082708

27092709
auto Ty = getTypes().ConvertType(FD->getType());

clang/lib/CIR/CodeGen/CIRGenCXX.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ bool CIRGenModule::tryEmitBaseDestructorAsAlias(const CXXDestructorDecl *D) {
9696
return true;
9797

9898
// If the base is at a non-zero offset, give up.
99-
const ASTRecordLayout &ClassLayout = astCtx.getASTRecordLayout(Class);
99+
const ASTRecordLayout &ClassLayout = astContext.getASTRecordLayout(Class);
100100
if (!ClassLayout.getBaseClassOffset(UniqueBase).isZero())
101101
return true;
102102

clang/lib/CIR/CodeGen/CIRGenCXXABI.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,10 +31,10 @@ class CIRGenFunctionInfo;
3131
class CIRGenCXXABI {
3232
protected:
3333
CIRGenModule &CGM;
34-
std::unique_ptr<clang::MangleContext> MangleCtx;
34+
std::unique_ptr<clang::MangleContext> MangleContext;
3535

3636
CIRGenCXXABI(CIRGenModule &CGM)
37-
: CGM{CGM}, MangleCtx(CGM.getASTContext().createMangleContext()) {}
37+
: CGM{CGM}, MangleContext(CGM.getASTContext().createMangleContext()) {}
3838

3939
clang::ASTContext &getContext() const { return CGM.getASTContext(); }
4040

@@ -115,7 +115,7 @@ class CIRGenCXXABI {
115115
virtual bool classifyReturnType(CIRGenFunctionInfo &FI) const = 0;
116116

117117
/// Gets the mangle context.
118-
clang::MangleContext &getMangleContext() { return *MangleCtx; }
118+
clang::MangleContext &getMangleContext() { return *MangleContext; }
119119

120120
clang::ImplicitParamDecl *&getStructorImplicitParamDecl(CIRGenFunction &CGF) {
121121
return CGF.CXXStructorImplicitParamDecl;

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 20 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,12 @@ class ClangToCIRArgMapping {
103103
SmallVector<CIRArgs, 8> ArgInfo;
104104

105105
public:
106-
ClangToCIRArgMapping(const ASTContext &Context, const CIRGenFunctionInfo &FI,
106+
ClangToCIRArgMapping(const ASTContext &astContext,
107+
const CIRGenFunctionInfo &FI,
107108
bool OnlyRequiredArgs = false)
108109
: InallocaArgNo(InvalidIndex), SRetArgNo(InvalidIndex), TotalCIRArgs(0),
109110
ArgInfo(OnlyRequiredArgs ? FI.getNumRequiredArgs() : FI.arg_size()) {
110-
construct(Context, FI, OnlyRequiredArgs);
111+
construct(astContext, FI, OnlyRequiredArgs);
111112
}
112113

113114
bool hasSRetArg() const { return SRetArgNo != InvalidIndex; }
@@ -130,11 +131,11 @@ class ClangToCIRArgMapping {
130131
}
131132

132133
private:
133-
void construct(const ASTContext &Context, const CIRGenFunctionInfo &FI,
134+
void construct(const ASTContext &astContext, const CIRGenFunctionInfo &FI,
134135
bool OnlyRequiredArgs);
135136
};
136137

137-
void ClangToCIRArgMapping::construct(const ASTContext &Context,
138+
void ClangToCIRArgMapping::construct(const ASTContext &astContext,
138139
const CIRGenFunctionInfo &FI,
139140
bool OnlyRequiredArgs) {
140141
unsigned CIRArgNo = 0;
@@ -314,7 +315,7 @@ static Address emitAddressAtOffset(CIRGenFunction &CGF, Address addr,
314315
}
315316

316317
static void AddAttributesFromFunctionProtoType(CIRGenBuilderTy &builder,
317-
ASTContext &Ctx,
318+
ASTContext &astContext,
318319
mlir::NamedAttrList &FuncAttrs,
319320
const FunctionProtoType *FPT) {
320321
if (!FPT)
@@ -366,7 +367,7 @@ void CIRGenModule::constructAttributeList(StringRef Name,
366367
// TODO: NoReturn, cmse_nonsecure_call
367368

368369
// Collect function CIR attributes from the callee prototype if we have one.
369-
AddAttributesFromFunctionProtoType(getBuilder(), astCtx, funcAttrs,
370+
AddAttributesFromFunctionProtoType(getBuilder(), astContext, funcAttrs,
370371
CalleeInfo.getCalleeFunctionProtoType());
371372

372373
const Decl *TargetDecl = CalleeInfo.getCalleeDecl().getDecl();
@@ -388,7 +389,7 @@ void CIRGenModule::constructAttributeList(StringRef Name,
388389

389390
if (const FunctionDecl *Fn = dyn_cast<FunctionDecl>(TargetDecl)) {
390391
AddAttributesFromFunctionProtoType(
391-
getBuilder(), astCtx, funcAttrs,
392+
getBuilder(), astContext, funcAttrs,
392393
Fn->getType()->getAs<FunctionProtoType>());
393394
if (AttrOnCallSite && Fn->isReplaceableGlobalAllocationFunction()) {
394395
// A sane operator new returns a non-aliasing pointer.
@@ -1054,9 +1055,9 @@ void CIRGenFunction::emitCallArgs(
10541055
if (PS == nullptr)
10551056
return;
10561057

1057-
const auto &Context = getContext();
1058-
auto SizeTy = Context.getSizeType();
1059-
auto T = builder.getUIntNTy(Context.getTypeSize(SizeTy));
1058+
const clang::ASTContext &astContext = getContext();
1059+
auto SizeTy = astContext.getSizeType();
1060+
auto T = builder.getUIntNTy(astContext.getTypeSize(SizeTy));
10601061
assert(EmittedArg.getScalarVal() && "We emitted nothing for the arg?");
10611062
auto V = evaluateOrEmitBuiltinObjectSize(
10621063
Arg, PS->getType(), T, EmittedArg.getScalarVal(), PS->isDynamic());
@@ -1212,8 +1213,8 @@ CIRGenTypes::arrangeCXXStructorDeclaration(GlobalDecl GD) {
12121213
FunctionType::ExtInfo extInfo = FTP->getExtInfo();
12131214
CanQualType resultType = TheCXXABI.HasThisReturn(GD) ? argTypes.front()
12141215
: TheCXXABI.hasMostDerivedReturn(GD)
1215-
? Context.VoidPtrTy
1216-
: Context.VoidTy;
1216+
? astContext.VoidPtrTy
1217+
: astContext.VoidTy;
12171218

12181219
assert(!TheCXXABI.HasThisReturn(GD) &&
12191220
"Please sent PR with a test and remove this");
@@ -1300,7 +1301,7 @@ const CIRGenFunctionInfo &CIRGenTypes::arrangeCXXConstructorCall(
13001301
// FIXME: Kill copy.
13011302
llvm::SmallVector<CanQualType, 16> ArgTypes;
13021303
for (const auto &Arg : Args)
1303-
ArgTypes.push_back(Context.getCanonicalParamType(Arg.Ty));
1304+
ArgTypes.push_back(astContext.getCanonicalParamType(Arg.Ty));
13041305

13051306
// +1 for implicit this, which should always be args[0]
13061307
unsigned TotalPrefixArgs = 1 + ExtraPrefixArgs;
@@ -1314,7 +1315,7 @@ const CIRGenFunctionInfo &CIRGenTypes::arrangeCXXConstructorCall(
13141315
GlobalDecl GD(D, CtorKind);
13151316
assert(!TheCXXABI.HasThisReturn(GD) && "ThisReturn NYI");
13161317
assert(!TheCXXABI.hasMostDerivedReturn(GD) && "Most derived return NYI");
1317-
CanQualType ResultType = Context.VoidTy;
1318+
CanQualType ResultType = astContext.VoidTy;
13181319

13191320
FunctionType::ExtInfo Info = FPT->getExtInfo();
13201321
llvm::SmallVector<FunctionProtoType::ExtParameterInfo, 16> ParamInfos;
@@ -1336,7 +1337,7 @@ bool CIRGenTypes::inheritingCtorHasParams(const InheritedConstructor &Inherited,
13361337
!Target.getCXXABI().hasConstructorVariants();
13371338
}
13381339

1339-
bool CIRGenModule::MayDropFunctionReturn(const ASTContext &Context,
1340+
bool CIRGenModule::MayDropFunctionReturn(const ASTContext &astContext,
13401341
QualType ReturnType) {
13411342
// We can't just disard the return value for a record type with a complex
13421343
// destructor or a non-trivially copyable type.
@@ -1345,7 +1346,7 @@ bool CIRGenModule::MayDropFunctionReturn(const ASTContext &Context,
13451346
llvm_unreachable("NYI");
13461347
}
13471348

1348-
return ReturnType.isTriviallyCopyableType(Context);
1349+
return ReturnType.isTriviallyCopyableType(astContext);
13491350
}
13501351

13511352
static bool isInAllocaArgument(CIRGenCXXABI &ABI, QualType type) {
@@ -1437,10 +1438,10 @@ arrangeFreeFunctionLikeCall(CIRGenTypes &CGT, CIRGenModule &CGM,
14371438
}
14381439

14391440
static llvm::SmallVector<CanQualType, 16>
1440-
getArgTypesForCall(ASTContext &ctx, const CallArgList &args) {
1441+
getArgTypesForCall(ASTContext &astContext, const CallArgList &args) {
14411442
llvm::SmallVector<CanQualType, 16> argTypes;
14421443
for (auto &arg : args)
1443-
argTypes.push_back(ctx.getCanonicalParamType(arg.Ty));
1444+
argTypes.push_back(astContext.getCanonicalParamType(arg.Ty));
14441445
return argTypes;
14451446
}
14461447

@@ -1469,7 +1470,7 @@ const CIRGenFunctionInfo &CIRGenTypes::arrangeCXXMethodCall(
14691470
getExtParameterInfosForCall(proto, numPrefixArgs + 1, args.size());
14701471

14711472
// FIXME: Kill copy.
1472-
auto argTypes = getArgTypesForCall(Context, args);
1473+
auto argTypes = getArgTypesForCall(astContext, args);
14731474

14741475
auto info = proto->getExtInfo();
14751476
return arrangeCIRFunctionInfo(GetReturnType(proto->getReturnType()),

clang/lib/CIR/CodeGen/CIRGenClass.cpp

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -127,15 +127,16 @@ class FieldMemcpyizer {
127127
}
128128

129129
CharUnits getMemcpySize(uint64_t FirstByteOffset) const {
130-
ASTContext &Ctx = CGF.getContext();
130+
ASTContext &astContext = CGF.getContext();
131131
unsigned LastFieldSize =
132132
LastField->isBitField()
133-
? LastField->getBitWidthValue(Ctx)
134-
: Ctx.toBits(
135-
Ctx.getTypeInfoDataSizeInChars(LastField->getType()).Width);
133+
? LastField->getBitWidthValue(astContext)
134+
: astContext.toBits(
135+
astContext.getTypeInfoDataSizeInChars(LastField->getType())
136+
.Width);
136137
uint64_t MemcpySizeBits = LastFieldOffset + LastFieldSize -
137-
FirstByteOffset + Ctx.getCharWidth() - 1;
138-
CharUnits MemcpySize = Ctx.toCharUnitsFromBits(MemcpySizeBits);
138+
FirstByteOffset + astContext.getCharWidth() - 1;
139+
CharUnits MemcpySize = astContext.toCharUnitsFromBits(MemcpySizeBits);
139140
return MemcpySize;
140141
}
141142

@@ -1096,12 +1097,12 @@ void CIRGenFunction::destroyCXXObject(CIRGenFunction &CGF, Address addr,
10961097
/*Delegating=*/false, addr, type);
10971098
}
10981099

1099-
static bool FieldHasTrivialDestructorBody(ASTContext &Context,
1100+
static bool FieldHasTrivialDestructorBody(ASTContext &astContext,
11001101
const FieldDecl *Field);
11011102

11021103
// FIXME(cir): this should be shared with traditional codegen.
11031104
static bool
1104-
HasTrivialDestructorBody(ASTContext &Context,
1105+
HasTrivialDestructorBody(ASTContext &astContext,
11051106
const CXXRecordDecl *BaseClassDecl,
11061107
const CXXRecordDecl *MostDerivedClassDecl) {
11071108
// If the destructor is trivial we don't have to check anything else.
@@ -1113,7 +1114,7 @@ HasTrivialDestructorBody(ASTContext &Context,
11131114

11141115
// Check fields.
11151116
for (const auto *Field : BaseClassDecl->fields())
1116-
if (!FieldHasTrivialDestructorBody(Context, Field))
1117+
if (!FieldHasTrivialDestructorBody(astContext, Field))
11171118
return false;
11181119

11191120
// Check non-virtual bases.
@@ -1123,7 +1124,7 @@ HasTrivialDestructorBody(ASTContext &Context,
11231124

11241125
const CXXRecordDecl *NonVirtualBase =
11251126
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1126-
if (!HasTrivialDestructorBody(Context, NonVirtualBase,
1127+
if (!HasTrivialDestructorBody(astContext, NonVirtualBase,
11271128
MostDerivedClassDecl))
11281129
return false;
11291130
}
@@ -1133,7 +1134,8 @@ HasTrivialDestructorBody(ASTContext &Context,
11331134
for (const auto &I : BaseClassDecl->vbases()) {
11341135
const CXXRecordDecl *VirtualBase =
11351136
cast<CXXRecordDecl>(I.getType()->castAs<RecordType>()->getDecl());
1136-
if (!HasTrivialDestructorBody(Context, VirtualBase, MostDerivedClassDecl))
1137+
if (!HasTrivialDestructorBody(astContext, VirtualBase,
1138+
MostDerivedClassDecl))
11371139
return false;
11381140
}
11391141
}
@@ -1142,9 +1144,10 @@ HasTrivialDestructorBody(ASTContext &Context,
11421144
}
11431145

11441146
// FIXME(cir): this should be shared with traditional codegen.
1145-
static bool FieldHasTrivialDestructorBody(ASTContext &Context,
1147+
static bool FieldHasTrivialDestructorBody(ASTContext &astContext,
11461148
const FieldDecl *Field) {
1147-
QualType FieldBaseElementType = Context.getBaseElementType(Field->getType());
1149+
QualType FieldBaseElementType =
1150+
astContext.getBaseElementType(Field->getType());
11481151

11491152
const RecordType *RT = FieldBaseElementType->getAs<RecordType>();
11501153
if (!RT)
@@ -1156,7 +1159,7 @@ static bool FieldHasTrivialDestructorBody(ASTContext &Context,
11561159
if (FieldClassDecl->isUnion() && FieldClassDecl->isAnonymousStructOrUnion())
11571160
return false;
11581161

1159-
return HasTrivialDestructorBody(Context, FieldClassDecl, FieldClassDecl);
1162+
return HasTrivialDestructorBody(astContext, FieldClassDecl, FieldClassDecl);
11601163
}
11611164

11621165
/// Check whether we need to initialize any vtable pointers before calling this

0 commit comments

Comments
 (0)