Skip to content

Commit 2310c8a

Browse files
committed
[CIR][NFC] Eliminate ArgInfo structure
A previous refactoring had reduced the ArgInfo structure to contain a single member, the argument type. This change eliminates the ArgInfo structure entirely, instead just storing the argument type directly in places where ArgInfo had previously been used. This also updates the place where the arg types were previously being copied for a call to CIRGenFunctionInfo::Profile to instead use the stored argument types buffer directly and adds assertions where the calculated folding set ID is used to verify that any match was correct.
1 parent 7a24238 commit 2310c8a

File tree

3 files changed

+44
-51
lines changed

3 files changed

+44
-51
lines changed

clang/lib/CIR/CodeGen/CIRGenCall.cpp

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,9 @@ CIRGenFunctionInfo *
2323
CIRGenFunctionInfo::create(CanQualType resultType,
2424
llvm::ArrayRef<CanQualType> argTypes,
2525
RequiredArgs required) {
26-
// The first slot allocated for ArgInfo is for the return value.
27-
void *buffer = operator new(totalSizeToAlloc<ArgInfo>(argTypes.size() + 1));
26+
// The first slot allocated for arg type slot is for the return value.
27+
void *buffer = operator new(
28+
totalSizeToAlloc<CanQualType>(argTypes.size() + 1));
2829

2930
assert(!cir::MissingFeatures::opCallCIRGenFuncInfoParamInfo());
3031

@@ -33,10 +34,8 @@ CIRGenFunctionInfo::create(CanQualType resultType,
3334
fi->required = required;
3435
fi->numArgs = argTypes.size();
3536

36-
ArgInfo *argsBuffer = fi->getArgsBuffer();
37-
(argsBuffer++)->type = resultType;
38-
for (CanQualType ty : argTypes)
39-
(argsBuffer++)->type = ty;
37+
fi->getArgTypes()[0] = resultType;
38+
std::copy(argTypes.begin(), argTypes.end(), fi->argTypesBegin());
4039
assert(!cir::MissingFeatures::opCallCIRGenFuncInfoExtParamInfo());
4140

4241
return fi;
@@ -47,8 +46,8 @@ cir::FuncType CIRGenTypes::getFunctionType(const CIRGenFunctionInfo &fi) {
4746
SmallVector<mlir::Type, 8> argTypes;
4847
argTypes.reserve(fi.getNumRequiredArgs());
4948

50-
for (const CIRGenFunctionInfoArgInfo &argInfo : fi.requiredArguments())
51-
argTypes.push_back(convertType(argInfo.type));
49+
for (const CanQualType &argType : fi.requiredArguments())
50+
argTypes.push_back(convertType(argType));
5251

5352
return cir::FuncType::get(argTypes,
5453
(resultType ? resultType : builder.getVoidTy()),
@@ -189,13 +188,13 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &funcInfo,
189188
assert(!cir::MissingFeatures::emitLifetimeMarkers());
190189

191190
// Translate all of the arguments as necessary to match the CIR lowering.
192-
for (auto [argNo, arg, argInfo] :
193-
llvm::enumerate(args, funcInfo.argInfos())) {
191+
for (auto [argNo, arg, canQualArgType] :
192+
llvm::enumerate(args, funcInfo.argTypes())) {
194193

195194
// Insert a padding argument to ensure proper alignment.
196195
assert(!cir::MissingFeatures::opCallPaddingArgs());
197196

198-
mlir::Type argType = convertType(argInfo.type);
197+
mlir::Type argType = convertType(canQualArgType);
199198
if (!mlir::isa<cir::RecordType>(argType)) {
200199
mlir::Value v;
201200
if (arg.isAggregate())

clang/lib/CIR/CodeGen/CIRGenFunctionInfo.h

Lines changed: 26 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,6 @@
2121

2222
namespace clang::CIRGen {
2323

24-
struct CIRGenFunctionInfoArgInfo {
25-
CanQualType type;
26-
};
27-
2824
/// A class for recording the number of arguments that a function signature
2925
/// requires.
3026
class RequiredArgs {
@@ -72,16 +68,15 @@ class RequiredArgs {
7268

7369
class CIRGenFunctionInfo final
7470
: public llvm::FoldingSetNode,
75-
private llvm::TrailingObjects<CIRGenFunctionInfo,
76-
CIRGenFunctionInfoArgInfo> {
77-
using ArgInfo = CIRGenFunctionInfoArgInfo;
78-
71+
private llvm::TrailingObjects<CIRGenFunctionInfo, CanQualType> {
7972
RequiredArgs required;
8073

8174
unsigned numArgs;
8275

83-
ArgInfo *getArgsBuffer() { return getTrailingObjects<ArgInfo>(); }
84-
const ArgInfo *getArgsBuffer() const { return getTrailingObjects<ArgInfo>(); }
76+
CanQualType *getArgTypes() { return getTrailingObjects<CanQualType>(); }
77+
const CanQualType *getArgTypes() const {
78+
return getTrailingObjects<CanQualType>();
79+
}
8580

8681
CIRGenFunctionInfo() : required(RequiredArgs::All) {}
8782

@@ -96,8 +91,8 @@ class CIRGenFunctionInfo final
9691
// these have to be public.
9792
friend class TrailingObjects;
9893

99-
using const_arg_iterator = const ArgInfo *;
100-
using arg_iterator = ArgInfo *;
94+
using const_arg_iterator = const CanQualType *;
95+
using arg_iterator = CanQualType *;
10196

10297
// This function has to be CamelCase because llvm::FoldingSet requires so.
10398
// NOLINTNEXTLINE(readability-identifier-naming)
@@ -112,49 +107,41 @@ class CIRGenFunctionInfo final
112107

113108
// NOLINTNEXTLINE(readability-identifier-naming)
114109
void Profile(llvm::FoldingSetNodeID &id) {
115-
// It's unfortunate that we are looping over the arguments twice (here and
116-
// in the static Profile function we call from here), but if the Profile
117-
// functions get out of sync, we can end up with incorrect function
118-
// signatures, and we don't have the argument types in the format that the
119-
// static Profile function requires.
120-
llvm::SmallVector<CanQualType, 16> argTypes;
121-
for (const ArgInfo &argInfo : arguments())
122-
argTypes.push_back(argInfo.type);
123-
124-
Profile(id, required, getReturnType(), argTypes);
110+
// If the Profile functions get out of sync, we can end up with incorrect
111+
// function signatures, so we call the static Profile function here rather
112+
// than duplicating the logic.
113+
Profile(id, required, getReturnType(), arguments());
125114
}
126115

127-
llvm::ArrayRef<ArgInfo> arguments() const {
128-
return llvm::ArrayRef<ArgInfo>(argInfoBegin(), numArgs);
116+
llvm::ArrayRef<CanQualType> arguments() const {
117+
return llvm::ArrayRef<CanQualType>(argTypesBegin(), numArgs);
129118
}
130119

131-
llvm::ArrayRef<ArgInfo> requiredArguments() const {
132-
return llvm::ArrayRef<ArgInfo>(argInfoBegin(), getNumRequiredArgs());
120+
llvm::ArrayRef<CanQualType> requiredArguments() const {
121+
return llvm::ArrayRef<CanQualType>(argTypesBegin(), getNumRequiredArgs());
133122
}
134123

135-
CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
124+
CanQualType getReturnType() const { return getArgTypes()[0]; }
136125

137-
const_arg_iterator argInfoBegin() const { return getArgsBuffer() + 1; }
138-
const_arg_iterator argInfoEnd() const {
139-
return getArgsBuffer() + 1 + numArgs;
140-
}
141-
arg_iterator argInfoBegin() { return getArgsBuffer() + 1; }
142-
arg_iterator argInfoEnd() { return getArgsBuffer() + 1 + numArgs; }
126+
const_arg_iterator argTypesBegin() const { return getArgTypes() + 1; }
127+
const_arg_iterator argTypesEnd() const { return getArgTypes() + 1 + numArgs; }
128+
arg_iterator argTypesBegin() { return getArgTypes() + 1; }
129+
arg_iterator argTypesEnd() { return getArgTypes() + 1 + numArgs; }
143130

144-
unsigned argInfoSize() const { return numArgs; }
131+
unsigned argTypeSize() const { return numArgs; }
145132

146-
llvm::MutableArrayRef<ArgInfo> argInfos() {
147-
return llvm::MutableArrayRef<ArgInfo>(argInfoBegin(), numArgs);
133+
llvm::MutableArrayRef<CanQualType> argTypes() {
134+
return llvm::MutableArrayRef<CanQualType>(argTypesBegin(), numArgs);
148135
}
149-
llvm::ArrayRef<ArgInfo> argInfos() const {
150-
return llvm::ArrayRef<ArgInfo>(argInfoBegin(), numArgs);
136+
llvm::ArrayRef<CanQualType> argTypes() const {
137+
return llvm::ArrayRef<CanQualType>(argTypesBegin(), numArgs);
151138
}
152139

153140
bool isVariadic() const { return required.allowsOptionalArgs(); }
154141
RequiredArgs getRequiredArgs() const { return required; }
155142
unsigned getNumRequiredArgs() const {
156143
return isVariadic() ? getRequiredArgs().getNumRequiredArgs()
157-
: argInfoSize();
144+
: argTypeSize();
158145
}
159146
};
160147

clang/lib/CIR/CodeGen/CIRGenTypes.cpp

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -542,8 +542,15 @@ CIRGenTypes::arrangeCIRFunctionInfo(CanQualType returnType,
542542

543543
void *insertPos = nullptr;
544544
CIRGenFunctionInfo *fi = functionInfos.FindNodeOrInsertPos(id, insertPos);
545-
if (fi)
545+
if (fi) {
546+
// We found a matching function info based on id. These asserts verify that
547+
// it really is a match.
548+
assert(
549+
fi->getReturnType() == returnType &&
550+
std::equal(fi->argTypesBegin(), fi->argTypesEnd(), argTypes.begin()) &&
551+
"Bad match based on CIRGenFunctionInfo folding set id");
546552
return *fi;
553+
}
547554

548555
assert(!cir::MissingFeatures::opCallCallConv());
549556

0 commit comments

Comments
 (0)