Skip to content

Commit 1315d1a

Browse files
authored
Merge pull request #5334 from jckarter/closure-sil-convention
2 parents 42cf4be + aac85cb commit 1315d1a

20 files changed

+58
-5
lines changed

include/swift/AST/Types.h

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2164,6 +2164,11 @@ enum class FunctionTypeRepresentation : uint8_t {
21642164
///
21652165
/// This is a superset of FunctionTypeRepresentation. The common representations
21662166
/// must share an enum value.
2167+
///
2168+
/// TODO: The overlap of SILFunctionTypeRepresentation and
2169+
/// FunctionTypeRepresentation is a total hack necessitated by the way SIL
2170+
/// TypeLowering is currently written. We ought to refactor TypeLowering so that
2171+
/// it is not necessary to distinguish these cases.
21672172
enum class SILFunctionTypeRepresentation : uint8_t {
21682173
/// A freestanding thick function.
21692174
Thick = uint8_t(FunctionTypeRepresentation::Swift),
@@ -2192,6 +2197,9 @@ enum class SILFunctionTypeRepresentation : uint8_t {
21922197

21932198
/// A Swift protocol witness.
21942199
WitnessMethod,
2200+
2201+
/// A closure invocation function that has not been bound to a context.
2202+
Closure,
21952203
};
21962204

21972205
/// Can this calling convention result in a function being called indirectly
@@ -2202,6 +2210,7 @@ inline bool canBeCalledIndirectly(SILFunctionTypeRepresentation rep) {
22022210
case SILFunctionTypeRepresentation::Thin:
22032211
case SILFunctionTypeRepresentation::CFunctionPointer:
22042212
case SILFunctionTypeRepresentation::Block:
2213+
case SILFunctionTypeRepresentation::Closure:
22052214
return false;
22062215
case SILFunctionTypeRepresentation::ObjCMethod:
22072216
case SILFunctionTypeRepresentation::Method:
@@ -2223,6 +2232,7 @@ getSILFunctionLanguage(SILFunctionTypeRepresentation rep) {
22232232
case SILFunctionTypeRepresentation::Thin:
22242233
case SILFunctionTypeRepresentation::Method:
22252234
case SILFunctionTypeRepresentation::WitnessMethod:
2235+
case SILFunctionTypeRepresentation::Closure:
22262236
return SILFunctionLanguage::Swift;
22272237
}
22282238
}
@@ -2302,6 +2312,7 @@ class AnyFunctionType : public TypeBase {
23022312
case SILFunctionTypeRepresentation::Block:
23032313
case SILFunctionTypeRepresentation::Thin:
23042314
case SILFunctionTypeRepresentation::CFunctionPointer:
2315+
case SILFunctionTypeRepresentation::Closure:
23052316
return false;
23062317
case SILFunctionTypeRepresentation::ObjCMethod:
23072318
case SILFunctionTypeRepresentation::Method:
@@ -2321,6 +2332,7 @@ class AnyFunctionType : public TypeBase {
23212332
case SILFunctionTypeRepresentation::ObjCMethod:
23222333
case SILFunctionTypeRepresentation::WitnessMethod:
23232334
case SILFunctionTypeRepresentation::CFunctionPointer:
2335+
case SILFunctionTypeRepresentation::Closure:
23242336
return false;
23252337
}
23262338
}
@@ -3023,6 +3035,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
30233035
case Representation::Block:
30243036
case Representation::Thin:
30253037
case Representation::CFunctionPointer:
3038+
case Representation::Closure:
30263039
return false;
30273040
case Representation::ObjCMethod:
30283041
case Representation::Method:
@@ -3038,6 +3051,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
30383051
case Representation::Thin:
30393052
case Representation::CFunctionPointer:
30403053
case Representation::ObjCMethod:
3054+
case Representation::Closure:
30413055
return false;
30423056
case Representation::Method:
30433057
case Representation::WitnessMethod:
@@ -3056,6 +3070,7 @@ class SILFunctionType final : public TypeBase, public llvm::FoldingSetNode,
30563070
case Representation::ObjCMethod:
30573071
case Representation::Method:
30583072
case Representation::WitnessMethod:
3073+
case Representation::Closure:
30593074
return false;
30603075
}
30613076
}

include/swift/SIL/SILInstruction.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5256,6 +5256,7 @@ class ApplySite {
52565256
case SILFunctionTypeRepresentation::Method:
52575257
case SILFunctionTypeRepresentation::ObjCMethod:
52585258
case SILFunctionTypeRepresentation::WitnessMethod:
5259+
case SILFunctionTypeRepresentation::Closure:
52595260
return true;
52605261
case SILFunctionTypeRepresentation::Block:
52615262
case SILFunctionTypeRepresentation::Thick:

include/swift/Serialization/ModuleFormat.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 276; // Last change: add load inst, store inst
57+
const uint16_t VERSION_MINOR = 277; // Last change: closure SIL @convention
5858

5959
using DeclID = PointerEmbeddedInt<unsigned, 31>;
6060
using DeclIDField = BCFixed<31>;
@@ -140,6 +140,7 @@ enum class SILFunctionTypeRepresentation : uint8_t {
140140
Method = FirstSIL,
141141
ObjCMethod,
142142
WitnessMethod,
143+
Closure,
143144
};
144145
using SILFunctionTypeRepresentationField = BCFixed<4>;
145146

lib/AST/ASTDumper.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2911,6 +2911,10 @@ namespace {
29112911
case SILFunctionType::Representation::WitnessMethod:
29122912
printField("representation", "witness_method");
29132913
break;
2914+
2915+
case SILFunctionType::Representation::Closure:
2916+
printField("representation", "closure");
2917+
break;
29142918
}
29152919

29162920
printFlag(T->isAutoClosure(), "autoclosure");

lib/AST/ASTPrinter.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3716,6 +3716,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37163716
case SILFunctionType::Representation::WitnessMethod:
37173717
Printer << "witness_method";
37183718
break;
3719+
case SILFunctionType::Representation::Closure:
3720+
Printer << "closure";
3721+
break;
37193722
}
37203723
Printer << ")";
37213724
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);
@@ -3733,7 +3736,6 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37333736
Printer.callPrintStructurePre(PrintStructureKind::BuiltinAttribute);
37343737
Printer.printAttrName("@convention");
37353738
Printer << "(";
3736-
// TODO: coalesce into a single convention attribute.
37373739
switch (info.getRepresentation()) {
37383740
case SILFunctionType::Representation::Thick:
37393741
llvm_unreachable("thick is not printed");
@@ -3755,6 +3757,9 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
37553757
case SILFunctionType::Representation::WitnessMethod:
37563758
Printer << "witness_method";
37573759
break;
3760+
case SILFunctionType::Representation::Closure:
3761+
Printer << "closure";
3762+
break;
37583763
}
37593764
Printer << ")";
37603765
Printer.printStructurePost(PrintStructureKind::BuiltinAttribute);

lib/AST/Mangle.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1032,6 +1032,9 @@ void Mangler::mangleType(Type type, unsigned uncurryLevel) {
10321032
case SILFunctionTypeRepresentation::Method:
10331033
Buffer << "Cm";
10341034
break;
1035+
case SILFunctionTypeRepresentation::Closure:
1036+
Buffer << "Ck";
1037+
break;
10351038
case SILFunctionTypeRepresentation::WitnessMethod:
10361039
Buffer << "Cw";
10371040
break;

lib/Basic/StringExtras.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -669,7 +669,7 @@ static StringRef omitNeedlessWords(StringRef name,
669669
NameRole::Partial, allPropertyNames, scratch);
670670
if (shortenedNameWord == newShortenedNameWord &&
671671
shortenedNameWord.back() == 'e') {
672-
shortenedNameWord.drop_back();
672+
(void)shortenedNameWord.drop_back();
673673
newShortenedNameWord =
674674
omitNeedlessWords(shortenedNameWord, typeName.CollectionElement,
675675
NameRole::Partial, allPropertyNames, scratch);

lib/IRGen/GenCall.cpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ llvm::CallingConv::ID irgen::expandCallingConv(IRGenModule &IGM,
150150

151151
case SILFunctionTypeRepresentation::Method:
152152
case SILFunctionTypeRepresentation::WitnessMethod:
153-
SWIFT_FALLTHROUGH;
153+
case SILFunctionTypeRepresentation::Closure:
154154
case SILFunctionTypeRepresentation::Thin:
155155
case SILFunctionTypeRepresentation::Thick:
156156
return getFreestandingConvention(IGM);
@@ -733,6 +733,7 @@ llvm::Type *SignatureExpansion::expandExternalSignatureTypes() {
733733
case SILFunctionTypeRepresentation::Thick:
734734
case SILFunctionTypeRepresentation::Method:
735735
case SILFunctionTypeRepresentation::WitnessMethod:
736+
case SILFunctionTypeRepresentation::Closure:
736737
llvm_unreachable("not a C representation");
737738
}
738739

@@ -965,6 +966,7 @@ void SignatureExpansion::expandParameters() {
965966
case SILFunctionType::Representation::WitnessMethod:
966967
case SILFunctionType::Representation::ObjCMethod:
967968
case SILFunctionType::Representation::Thin:
969+
case SILFunctionType::Representation::Closure:
968970
return FnType->hasErrorResult();
969971

970972
case SILFunctionType::Representation::Thick:
@@ -1858,6 +1860,7 @@ void CallEmission::setArgs(Explosion &arg, WitnessMetadata *witnessMetadata) {
18581860
Args.rbegin()[0] = witnessMetadata->SelfWitnessTable;
18591861
SWIFT_FALLTHROUGH;
18601862

1863+
case SILFunctionTypeRepresentation::Closure:
18611864
case SILFunctionTypeRepresentation::Method:
18621865
case SILFunctionTypeRepresentation::Thin:
18631866
case SILFunctionTypeRepresentation::Thick: {

lib/IRGen/GenClangType.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -575,6 +575,7 @@ clang::CanQualType GenClangType::visitSILFunctionType(CanSILFunctionType type) {
575575
case SILFunctionType::Representation::Method:
576576
case SILFunctionType::Representation::ObjCMethod:
577577
case SILFunctionType::Representation::WitnessMethod:
578+
case SILFunctionType::Representation::Closure:
578579
llvm_unreachable("not an ObjC-compatible function");
579580
}
580581

lib/IRGen/GenConstant.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,8 @@ static llvm::Constant *emitConstantValue(IRGenModule &IGM, SILValue operand) {
8484
llvm_unreachable("Unsupported SILInstruction in static initializer!");
8585
}
8686

87-
static template <typename InstTy, typename NextIndexFunc>
87+
namespace {
88+
template <typename InstTy, typename NextIndexFunc>
8889
llvm::Constant *emitConstantStructOrTuple(IRGenModule &IGM, InstTy inst,
8990
NextIndexFunc nextIndex) {
9091
auto type = inst->getType();
@@ -118,6 +119,7 @@ llvm::Constant *emitConstantStructOrTuple(IRGenModule &IGM, InstTy inst,
118119

119120
return llvm::ConstantStruct::get(sTy, elts);
120121
}
122+
} // end anonymous namespace
121123

122124
llvm::Constant *irgen::emitConstantStruct(IRGenModule &IGM, StructInst *SI) {
123125
// The only way to get a struct's stored properties (which we need to map to

lib/IRGen/GenFunc.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -555,6 +555,7 @@ const TypeInfo *TypeConverter::convertFunctionType(SILFunctionType *T) {
555555
case SILFunctionType::Representation::Method:
556556
case SILFunctionType::Representation::ObjCMethod:
557557
case SILFunctionType::Representation::CFunctionPointer:
558+
case SILFunctionType::Representation::Closure:
558559
return ThinFuncTypeInfo::create(CanSILFunctionType(T),
559560
IGM.FunctionPtrTy,
560561
IGM.getPointerSize(),
@@ -612,6 +613,7 @@ getFuncSignatureInfoForLowered(IRGenModule &IGM, CanSILFunctionType type) {
612613
case SILFunctionType::Representation::Method:
613614
case SILFunctionType::Representation::WitnessMethod:
614615
case SILFunctionType::Representation::ObjCMethod:
616+
case SILFunctionType::Representation::Closure:
615617
return ti.as<ThinFuncTypeInfo>();
616618
case SILFunctionType::Representation::Thick:
617619
return ti.as<FuncTypeInfo>();

lib/IRGen/GenMeta.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1666,6 +1666,7 @@ namespace {
16661666
case SILFunctionType::Representation::WitnessMethod:
16671667
case SILFunctionType::Representation::ObjCMethod:
16681668
case SILFunctionType::Representation::CFunctionPointer:
1669+
case SILFunctionType::Representation::Closure:
16691670
// A thin function looks like a plain pointer.
16701671
// FIXME: Except for extra inhabitants?
16711672
return emitDirectMetadataRef(C.TheRawPointerType);
@@ -1845,6 +1846,7 @@ namespace {
18451846
case SILFunctionType::Representation::WitnessMethod:
18461847
case SILFunctionType::Representation::ObjCMethod:
18471848
case SILFunctionType::Representation::CFunctionPointer:
1849+
case SILFunctionType::Representation::Closure:
18481850
// A thin function looks like a plain pointer.
18491851
// FIXME: Except for extra inhabitants?
18501852
return emitFromValueWitnessTable(C.TheRawPointerType);

lib/IRGen/GenProto.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1811,6 +1811,7 @@ bool irgen::hasPolymorphicParameters(CanSILFunctionType ty) {
18111811
case SILFunctionTypeRepresentation::Thick:
18121812
case SILFunctionTypeRepresentation::Thin:
18131813
case SILFunctionTypeRepresentation::Method:
1814+
case SILFunctionTypeRepresentation::Closure:
18141815
return ty->isPolymorphic();
18151816

18161817
case SILFunctionTypeRepresentation::CFunctionPointer:

lib/IRGen/IRGenSIL.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1939,6 +1939,7 @@ static CallEmission getCallEmissionForLoweredValue(IRGenSILFunction &IGF,
19391939
case SILFunctionType::Representation::Thin:
19401940
case SILFunctionType::Representation::CFunctionPointer:
19411941
case SILFunctionType::Representation::Method:
1942+
case SILFunctionType::Representation::Closure:
19421943
case SILFunctionType::Representation::ObjCMethod:
19431944
case SILFunctionType::Representation::WitnessMethod:
19441945
case SILFunctionType::Representation::Thick: {
@@ -2152,6 +2153,7 @@ getPartialApplicationFunction(IRGenSILFunction &IGF, SILValue v,
21522153
case SILFunctionTypeRepresentation::Thick:
21532154
case SILFunctionTypeRepresentation::Thin:
21542155
case SILFunctionTypeRepresentation::Method:
2156+
case SILFunctionTypeRepresentation::Closure:
21552157
break;
21562158
}
21572159
return std::make_tuple(lv.getStaticFunction().getFunction(),
@@ -2165,6 +2167,7 @@ getPartialApplicationFunction(IRGenSILFunction &IGF, SILValue v,
21652167
switch (fnType->getRepresentation()) {
21662168
case SILFunctionType::Representation::Thin:
21672169
case SILFunctionType::Representation::Method:
2170+
case SILFunctionType::Representation::Closure:
21682171
case SILFunctionType::Representation::ObjCMethod:
21692172
break;
21702173
case SILFunctionType::Representation::WitnessMethod: {

lib/SIL/Bridging.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,7 @@ Type TypeConverter::getLoweredBridgedType(AbstractionPattern pattern,
110110
case SILFunctionTypeRepresentation::Thin:
111111
case SILFunctionTypeRepresentation::Method:
112112
case SILFunctionTypeRepresentation::WitnessMethod:
113+
case SILFunctionTypeRepresentation::Closure:
113114
// No bridging needed for native CCs.
114115
return t;
115116
case SILFunctionTypeRepresentation::CFunctionPointer:
@@ -185,6 +186,7 @@ Type TypeConverter::getLoweredCBridgedType(AbstractionPattern pattern,
185186
case SILFunctionType::Representation::Method:
186187
case SILFunctionType::Representation::ObjCMethod:
187188
case SILFunctionType::Representation::WitnessMethod:
189+
case SILFunctionType::Representation::Closure:
188190
return t;
189191
case SILFunctionType::Representation::Thick: {
190192
// Thick functions (TODO: conditionally) get bridged to blocks.

lib/SIL/SILFunctionType.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -998,6 +998,7 @@ static CanSILFunctionType getNativeSILFunctionType(SILModule &M,
998998
case SILFunctionType::Representation::ObjCMethod:
999999
case SILFunctionType::Representation::Thick:
10001000
case SILFunctionType::Representation::Method:
1001+
case SILFunctionType::Representation::Closure:
10011002
case SILFunctionType::Representation::WitnessMethod: {
10021003
switch (kind) {
10031004
case SILDeclRef::Kind::Initializer:
@@ -2306,6 +2307,7 @@ TypeConverter::getBridgedFunctionType(AbstractionPattern pattern,
23062307
case SILFunctionTypeRepresentation::Thick:
23072308
case SILFunctionTypeRepresentation::Thin:
23082309
case SILFunctionTypeRepresentation::Method:
2310+
case SILFunctionTypeRepresentation::Closure:
23092311
case SILFunctionTypeRepresentation::WitnessMethod:
23102312
// No bridging needed for native functions.
23112313
if (t->getExtInfo() == extInfo)
@@ -2461,6 +2463,7 @@ TypeConverter::getLoweredASTFunctionType(CanAnyFunctionType fnType,
24612463
case SILFunctionTypeRepresentation::Thin:
24622464
case SILFunctionTypeRepresentation::Thick:
24632465
case SILFunctionTypeRepresentation::Method:
2466+
case SILFunctionTypeRepresentation::Closure:
24642467
case SILFunctionTypeRepresentation::WitnessMethod:
24652468
// Native functions don't need bridging.
24662469
break;

lib/SILGen/SILGenExpr.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1141,6 +1141,7 @@ static ManagedValue convertFunctionRepresentation(SILGenFunction &SGF,
11411141
SGF.getLoweredType(destTy).castTo<SILFunctionType>());
11421142
break;
11431143
case SILFunctionType::Representation::Method:
1144+
case SILFunctionType::Representation::Closure:
11441145
case SILFunctionType::Representation::ObjCMethod:
11451146
case SILFunctionType::Representation::WitnessMethod:
11461147
llvm_unreachable("should not do function conversion from method rep");
@@ -1168,6 +1169,7 @@ static ManagedValue convertFunctionRepresentation(SILGenFunction &SGF,
11681169
case SILFunctionType::Representation::Block:
11691170
llvm_unreachable("should not try block-to-block repr change");
11701171
case SILFunctionType::Representation::Method:
1172+
case SILFunctionType::Representation::Closure:
11711173
case SILFunctionType::Representation::ObjCMethod:
11721174
case SILFunctionType::Representation::WitnessMethod:
11731175
llvm_unreachable("should not do function conversion from method rep");

lib/SILOptimizer/Mandatory/MandatoryInlining.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,7 @@ getCalleeFunction(FullApplySite AI, bool &IsThick,
284284
case SILFunctionTypeRepresentation::Thick:
285285
case SILFunctionTypeRepresentation::Thin:
286286
case SILFunctionTypeRepresentation::Method:
287+
case SILFunctionTypeRepresentation::Closure:
287288
case SILFunctionTypeRepresentation::WitnessMethod:
288289
break;
289290

lib/SILOptimizer/Utils/FunctionSignatureOptUtils.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ bool swift::hasNonTrivialNonDebugUse(SILArgument *Arg) {
5252
static bool isSpecializableRepresentation(SILFunctionTypeRepresentation Rep) {
5353
switch (Rep) {
5454
case SILFunctionTypeRepresentation::Method:
55+
case SILFunctionTypeRepresentation::Closure:
5556
case SILFunctionTypeRepresentation::Thin:
5657
case SILFunctionTypeRepresentation::Thick:
5758
case SILFunctionTypeRepresentation::CFunctionPointer:

lib/Serialization/Serialization.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2671,6 +2671,7 @@ static uint8_t getRawStableSILFunctionTypeRepresentation(
26712671
SIMPLE_CASE(SILFunctionTypeRepresentation, Method)
26722672
SIMPLE_CASE(SILFunctionTypeRepresentation, ObjCMethod)
26732673
SIMPLE_CASE(SILFunctionTypeRepresentation, WitnessMethod)
2674+
SIMPLE_CASE(SILFunctionTypeRepresentation, Closure)
26742675
}
26752676
llvm_unreachable("bad calling convention");
26762677
}

0 commit comments

Comments
 (0)