Skip to content

Commit 554e27e

Browse files
authored
[CIR][NFC] Eliminate ArgInfo structure (#140612)
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 d299242 commit 554e27e

File tree

3 files changed

+46
-51
lines changed

3 files changed

+46
-51
lines changed

clang/lib/CIR/CodeGen/CIRGenCall.cpp

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

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

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

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

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

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

5453
return cir::FuncType::get(argTypes,
5554
(resultType ? resultType : builder.getVoidTy()),
@@ -289,13 +288,13 @@ RValue CIRGenFunction::emitCall(const CIRGenFunctionInfo &funcInfo,
289288
assert(!cir::MissingFeatures::emitLifetimeMarkers());
290289

291290
// Translate all of the arguments as necessary to match the CIR lowering.
292-
for (auto [argNo, arg, argInfo] :
293-
llvm::enumerate(args, funcInfo.argInfos())) {
291+
for (auto [argNo, arg, canQualArgType] :
292+
llvm::enumerate(args, funcInfo.argTypes())) {
294293

295294
// Insert a padding argument to ensure proper alignment.
296295
assert(!cir::MissingFeatures::opCallPaddingArgs());
297296

298-
mlir::Type argType = convertType(argInfo.type);
297+
mlir::Type argType = convertType(canQualArgType);
299298
if (!mlir::isa<cir::RecordType>(argType)) {
300299
mlir::Value v;
301300
if (arg.isAggregate())

clang/lib/CIR/CodeGen/CIRGenFunctionInfo.h

Lines changed: 28 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -22,10 +22,6 @@
2222

2323
namespace clang::CIRGen {
2424

25-
struct CIRGenFunctionInfoArgInfo {
26-
CanQualType type;
27-
};
28-
2925
/// A class for recording the number of arguments that a function signature
3026
/// requires.
3127
class RequiredArgs {
@@ -71,18 +67,19 @@ class RequiredArgs {
7167
}
7268
};
7369

70+
// The TrailingObjects for this class contain the function return type in the
71+
// first CanQualType slot, followed by the argument types.
7472
class CIRGenFunctionInfo final
7573
: public llvm::FoldingSetNode,
76-
private llvm::TrailingObjects<CIRGenFunctionInfo,
77-
CIRGenFunctionInfoArgInfo> {
78-
using ArgInfo = CIRGenFunctionInfoArgInfo;
79-
74+
private llvm::TrailingObjects<CIRGenFunctionInfo, CanQualType> {
8075
RequiredArgs required;
8176

8277
unsigned numArgs;
8378

84-
ArgInfo *getArgsBuffer() { return getTrailingObjects<ArgInfo>(); }
85-
const ArgInfo *getArgsBuffer() const { return getTrailingObjects<ArgInfo>(); }
79+
CanQualType *getArgTypes() { return getTrailingObjects<CanQualType>(); }
80+
const CanQualType *getArgTypes() const {
81+
return getTrailingObjects<CanQualType>();
82+
}
8683

8784
CIRGenFunctionInfo() : required(RequiredArgs::All) {}
8885

@@ -97,8 +94,8 @@ class CIRGenFunctionInfo final
9794
// these have to be public.
9895
friend class TrailingObjects;
9996

100-
using const_arg_iterator = const ArgInfo *;
101-
using arg_iterator = ArgInfo *;
97+
using const_arg_iterator = const CanQualType *;
98+
using arg_iterator = CanQualType *;
10299

103100
// This function has to be CamelCase because llvm::FoldingSet requires so.
104101
// NOLINTNEXTLINE(readability-identifier-naming)
@@ -113,49 +110,41 @@ class CIRGenFunctionInfo final
113110

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

128-
llvm::ArrayRef<ArgInfo> arguments() const {
129-
return llvm::ArrayRef<ArgInfo>(argInfoBegin(), numArgs);
119+
llvm::ArrayRef<CanQualType> arguments() const {
120+
return llvm::ArrayRef<CanQualType>(argTypesBegin(), numArgs);
130121
}
131122

132-
llvm::ArrayRef<ArgInfo> requiredArguments() const {
133-
return llvm::ArrayRef<ArgInfo>(argInfoBegin(), getNumRequiredArgs());
123+
llvm::ArrayRef<CanQualType> requiredArguments() const {
124+
return llvm::ArrayRef<CanQualType>(argTypesBegin(), getNumRequiredArgs());
134125
}
135126

136-
CanQualType getReturnType() const { return getArgsBuffer()[0].type; }
127+
CanQualType getReturnType() const { return getArgTypes()[0]; }
137128

138-
const_arg_iterator argInfoBegin() const { return getArgsBuffer() + 1; }
139-
const_arg_iterator argInfoEnd() const {
140-
return getArgsBuffer() + 1 + numArgs;
141-
}
142-
arg_iterator argInfoBegin() { return getArgsBuffer() + 1; }
143-
arg_iterator argInfoEnd() { return getArgsBuffer() + 1 + numArgs; }
129+
const_arg_iterator argTypesBegin() const { return getArgTypes() + 1; }
130+
const_arg_iterator argTypesEnd() const { return getArgTypes() + 1 + numArgs; }
131+
arg_iterator argTypesBegin() { return getArgTypes() + 1; }
132+
arg_iterator argTypesEnd() { return getArgTypes() + 1 + numArgs; }
144133

145-
unsigned argInfoSize() const { return numArgs; }
134+
unsigned argTypeSize() const { return numArgs; }
146135

147-
llvm::MutableArrayRef<ArgInfo> argInfos() {
148-
return llvm::MutableArrayRef<ArgInfo>(argInfoBegin(), numArgs);
136+
llvm::MutableArrayRef<CanQualType> argTypes() {
137+
return llvm::MutableArrayRef<CanQualType>(argTypesBegin(), numArgs);
149138
}
150-
llvm::ArrayRef<ArgInfo> argInfos() const {
151-
return llvm::ArrayRef<ArgInfo>(argInfoBegin(), numArgs);
139+
llvm::ArrayRef<CanQualType> argTypes() const {
140+
return llvm::ArrayRef<CanQualType>(argTypesBegin(), numArgs);
152141
}
153142

154143
bool isVariadic() const { return required.allowsOptionalArgs(); }
155144
RequiredArgs getRequiredArgs() const { return required; }
156145
unsigned getNumRequiredArgs() const {
157146
return isVariadic() ? getRequiredArgs().getNumRequiredArgs()
158-
: argInfoSize();
147+
: argTypeSize();
159148
}
160149
};
161150

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)