Skip to content

Commit 5fd7bc7

Browse files
committed
Simplify packCount IRGen
1 parent d0143b9 commit 5fd7bc7

File tree

2 files changed

+22
-31
lines changed

2 files changed

+22
-31
lines changed

lib/AST/Builtins.cpp

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -247,13 +247,13 @@ createGenericParam(ASTContext &ctx, const char *name, unsigned index,
247247
/// Create a generic parameter list with multiple generic parameters.
248248
static GenericParamList *getGenericParams(ASTContext &ctx,
249249
unsigned numParameters,
250-
bool areParametersPacks = false) {
250+
bool areParameterPacks = false) {
251251
assert(numParameters <= std::size(GenericParamNames));
252252

253253
SmallVector<GenericTypeParamDecl *, 2> genericParams;
254254
for (unsigned i = 0; i != numParameters; ++i)
255255
genericParams.push_back(createGenericParam(ctx, GenericParamNames[i], i,
256-
areParametersPacks));
256+
areParameterPacks));
257257

258258
auto paramList = GenericParamList::create(ctx, SourceLoc(), genericParams,
259259
SourceLoc());
@@ -754,16 +754,9 @@ namespace {
754754
};
755755
struct ParameterGenerator {
756756
unsigned Index;
757-
bool isPackExpansion;
758757
Type build(BuiltinFunctionBuilder &builder) const {
759-
auto ty = builder.TheGenericParamList->getParams()[Index]
758+
return builder.TheGenericParamList->getParams()[Index]
760759
->getDeclaredInterfaceType();
761-
762-
if (isPackExpansion) {
763-
return PackExpansionType::get(ty, ty);
764-
}
765-
766-
return ty;
767760
}
768761
};
769762
struct LambdaGenerator {
@@ -780,6 +773,15 @@ namespace {
780773
return MetatypeType::get(Object.build(builder), Repr);
781774
}
782775
};
776+
struct ParameterPackGenerator {
777+
unsigned Index;
778+
Type build(BuiltinFunctionBuilder &builder) const {
779+
auto ty = builder.TheGenericParamList->getParams()[Index]
780+
->getDeclaredInterfaceType();
781+
782+
return PackExpansionType::get(ty, ty);
783+
}
784+
};
783785
};
784786
} // end anonymous namespace
785787

@@ -789,8 +791,8 @@ makeConcrete(Type type) {
789791
}
790792

791793
static BuiltinFunctionBuilder::ParameterGenerator
792-
makeGenericParam(unsigned index = 0, bool isPackExpansion = false) {
793-
return { index, isPackExpansion };
794+
makeGenericParam(unsigned index = 0) {
795+
return { index };
794796
}
795797

796798
template <class... Gs>
@@ -827,6 +829,11 @@ makeMetatype(const T &object,
827829
return { object, repr };
828830
}
829831

832+
static BuiltinFunctionBuilder::ParameterPackGenerator
833+
makeParameterPack(unsigned index = 0) {
834+
return { index };
835+
}
836+
830837
/// Create a function with type <T> T -> ().
831838
static ValueDecl *getRefCountingOperation(ASTContext &ctx, Identifier id) {
832839
return getBuiltinFunction(ctx, id, _thin,
@@ -1952,8 +1959,7 @@ static ValueDecl *getPackCount(ASTContext &ctx, Identifier id) {
19521959
/* anyObject */ false,
19531960
/* areParametersPack */ true);
19541961

1955-
auto packEx = makeGenericParam(/* index */ 0, /* isPackExpansion */ true);
1956-
auto paramTy = makeMetatype(makeTuple(packEx));
1962+
auto paramTy = makeMetatype(makeTuple(makeParameterPack()));
19571963
builder.addParameter(paramTy);
19581964
builder.setResult(makeConcrete(BuiltinIntegerType::getWordType(ctx)));
19591965

lib/IRGen/GenBuiltin.cpp

Lines changed: 2 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1359,23 +1359,8 @@ void irgen::emitBuiltinCall(IRGenFunction &IGF, const BuiltinInfo &Builtin,
13591359
if (Builtin.ID == BuiltinValueKind::PackCount) {
13601360
(void) args.claimAll();
13611361

1362-
auto metaTy = argTypes[0].getASTType()->castTo<MetatypeType>();
1363-
auto tupleTy = metaTy->getInstanceType()->castTo<TupleType>();
1364-
1365-
llvm::Value *count = llvm::ConstantInt::get(IGF.IGM.SizeTy, 0);
1366-
1367-
// Accumulate the count of the pack.
1368-
for (auto eltTy : tupleTy->getElementTypes()) {
1369-
if (auto packExpTy = dyn_cast<PackExpansionType>(eltTy)) {
1370-
auto countTy = packExpTy->getCountType()->getCanonicalType();
1371-
auto packCount = IGF.emitPackShapeExpression(countTy);
1372-
count = IGF.Builder.CreateAdd(count, packCount);
1373-
continue;
1374-
}
1375-
1376-
count = IGF.Builder.CreateAdd(count,
1377-
llvm::ConstantInt::get(IGF.IGM.SizeTy, 1));
1378-
}
1362+
auto packTy = substitutions.getReplacementTypes()[0]->castTo<PackType>();
1363+
auto count = IGF.emitPackShapeExpression(packTy->getCanonicalType());
13791364

13801365
out.add(count);
13811366
return;

0 commit comments

Comments
 (0)