Skip to content

Commit b154a0b

Browse files
committed
IRGen: Start plumbing shapes through NecessaryBindings
1 parent 1020970 commit b154a0b

File tree

3 files changed

+88
-25
lines changed

3 files changed

+88
-25
lines changed

lib/IRGen/GenProto.cpp

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,10 @@ class PolymorphicConvention {
130130
/// nullptr if it's unfulfilled.
131131
const Fulfillment *getFulfillmentForTypeMetadata(CanType type) const;
132132

133+
/// Returns a Fulfillment for a pack shape, or nullptr if it's
134+
/// unfulfilled.
135+
const Fulfillment *getFulfillmentForShape(CanType type) const;
136+
133137
/// Return the source of type metadata at a particular source index.
134138
const MetadataSource &getSource(size_t SourceIndex) const {
135139
return Sources[SourceIndex];
@@ -471,6 +475,11 @@ PolymorphicConvention::getFulfillmentForTypeMetadata(CanType type) const {
471475
return Fulfillments.getTypeMetadata(type);
472476
}
473477

478+
const Fulfillment *
479+
PolymorphicConvention::getFulfillmentForShape(CanType type) const {
480+
return Fulfillments.getShape(type);
481+
}
482+
474483
void irgen::enumerateGenericParamFulfillments(IRGenModule &IGM,
475484
CanSILFunctionType fnType,
476485
GenericParamFulfillmentCallback callback) {
@@ -480,6 +489,17 @@ void irgen::enumerateGenericParamFulfillments(IRGenModule &IGM,
480489
// captured value.
481490
auto generics = fnType->getInvocationGenericSignature();
482491

492+
for (auto shapeClass : generics->getShapeClasses()) {
493+
auto fulfillment
494+
= convention.getFulfillmentForShape(shapeClass);
495+
if (fulfillment == nullptr)
496+
continue;
497+
498+
auto &source = convention.getSource(fulfillment->SourceIndex);
499+
callback(GenericRequirement::forShape(shapeClass),
500+
source, fulfillment->Path);
501+
}
502+
483503
for (auto genericParam : generics.getGenericParams()) {
484504
auto genericParamType = genericParam->getCanonicalType();
485505

@@ -489,7 +509,8 @@ void irgen::enumerateGenericParamFulfillments(IRGenModule &IGM,
489509
continue;
490510

491511
auto &source = convention.getSource(fulfillment->SourceIndex);
492-
callback(genericParamType, source, fulfillment->Path);
512+
callback(GenericRequirement::forMetadata(genericParamType),
513+
source, fulfillment->Path);
493514
}
494515
}
495516

lib/IRGen/GenProto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -198,7 +198,7 @@ namespace irgen {
198198
ProtocolConformanceRef conformance);
199199

200200
using GenericParamFulfillmentCallback =
201-
llvm::function_ref<void(CanType genericParamType,
201+
llvm::function_ref<void(GenericRequirement req,
202202
const MetadataSource &source,
203203
const MetadataPath &path)>;
204204

lib/IRGen/GenReflection.cpp

Lines changed: 65 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1213,15 +1213,37 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
12131213
Subs(Subs),
12141214
Layout(Layout) {}
12151215

1216-
using MetadataSourceMap
1217-
= std::vector<std::pair<CanType, const reflection::MetadataSource*>>;
1216+
struct Entry {
1217+
enum Kind {
1218+
Metadata,
1219+
Shape
1220+
};
12181221

1219-
void addMetadataSource(const reflection::MetadataSource *Source) {
1222+
Kind kind;
1223+
1224+
CanType type;
1225+
const reflection::MetadataSource *source;
1226+
1227+
Entry(Kind kind, CanType type, const reflection::MetadataSource *source)
1228+
: kind(kind), type(type), source(source) {}
1229+
};
1230+
1231+
using MetadataSourceMap = std::vector<Entry>;
1232+
1233+
void addMetadataSource(Entry::Kind Kind, const reflection::MetadataSource *Source) {
12201234
if (Source == nullptr) {
12211235
B.addInt32(0);
12221236
} else {
12231237
SmallString<16> EncodeBuffer;
12241238
llvm::raw_svector_ostream OS(EncodeBuffer);
1239+
switch (Kind) {
1240+
case Entry::Kind::Shape:
1241+
OS << "s";
1242+
break;
1243+
case Entry::Kind::Metadata:
1244+
break;
1245+
}
1246+
12251247
MetadataSourceEncoder Encoder(OS);
12261248
Encoder.visit(Source);
12271249

@@ -1287,24 +1309,31 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
12871309
// the bindings structure directly.
12881310
auto &Bindings = Layout.getBindings();
12891311
for (unsigned i = 0; i < Bindings.size(); ++i) {
1290-
// Skip protocol requirements (FIXME: for now?)
1291-
if (Bindings[i].isAnyWitnessTable())
1292-
continue;
1293-
1294-
// FIXME: bind pack counts in the source map
1295-
assert(Bindings[i].isAnyMetadata());
1296-
1297-
auto Source = SourceBuilder.createClosureBinding(i);
1298-
auto BindingType = Bindings[i].getTypeParameter();
1299-
auto InterfaceType = BindingType->mapTypeOutOfContext();
1300-
SourceMap.push_back({InterfaceType->getCanonicalType(), Source});
1312+
switch (Bindings[i].getKind()) {
1313+
case GenericRequirement::Kind::Shape:
1314+
case GenericRequirement::Kind::Metadata:
1315+
case GenericRequirement::Kind::MetadataPack: {
1316+
auto Kind = (Bindings[i].getKind() == GenericRequirement::Kind::Shape
1317+
? Entry::Kind::Shape
1318+
: Entry::Kind::Metadata);
1319+
auto Source = SourceBuilder.createClosureBinding(i);
1320+
auto BindingType = Bindings[i].getTypeParameter();
1321+
auto InterfaceType = BindingType->mapTypeOutOfContext();
1322+
SourceMap.emplace_back(Kind, InterfaceType->getCanonicalType(), Source);
1323+
break;
1324+
}
1325+
case GenericRequirement::Kind::WitnessTable:
1326+
case GenericRequirement::Kind::WitnessTablePack:
1327+
// Skip protocol requirements (FIXME: for now?)
1328+
break;
1329+
}
13011330
}
13021331

13031332
// Check if any requirements were fulfilled by metadata stored inside a
13041333
// captured value.
13051334

13061335
enumerateGenericParamFulfillments(IGM, OrigCalleeType,
1307-
[&](CanType GenericParam,
1336+
[&](GenericRequirement Req,
13081337
const irgen::MetadataSource &Source,
13091338
const MetadataPath &Path) {
13101339

@@ -1332,15 +1361,31 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
13321361
break;
13331362
}
13341363

1364+
Entry::Kind Kind;
1365+
switch (Req.getKind()) {
1366+
case GenericRequirement::Kind::Shape:
1367+
Kind = Entry::Kind::Shape;
1368+
break;
1369+
1370+
case GenericRequirement::Kind::Metadata:
1371+
case GenericRequirement::Kind::MetadataPack:
1372+
Kind = Entry::Kind::Metadata;
1373+
break;
1374+
1375+
case GenericRequirement::Kind::WitnessTable:
1376+
case GenericRequirement::Kind::WitnessTablePack:
1377+
llvm_unreachable("Bad kind");
1378+
}
1379+
13351380
// The metadata might be reached via a non-trivial path (eg,
13361381
// dereferencing an isa pointer or a generic argument). Record
13371382
// the path. We assume captured values map 1-1 with function
13381383
// parameters.
13391384
auto Src = Path.getMetadataSource(SourceBuilder, Root);
13401385

1341-
auto SubstType = GenericParam.subst(Subs);
1386+
auto SubstType = Req.getTypeParameter().subst(Subs);
13421387
auto InterfaceType = SubstType->mapTypeOutOfContext();
1343-
SourceMap.push_back({InterfaceType->getCanonicalType(), Src});
1388+
SourceMap.emplace_back(Kind, InterfaceType->getCanonicalType(), Src);
13441389
});
13451390

13461391
return SourceMap;
@@ -1398,12 +1443,9 @@ class CaptureDescriptorBuilder : public ReflectionMetadataBuilder {
13981443

13991444
// Add the pairs that make up the generic param -> metadata source map
14001445
// to the struct.
1401-
for (auto GenericAndSource : MetadataSources) {
1402-
auto GenericParam = GenericAndSource.first->getCanonicalType();
1403-
auto Source = GenericAndSource.second;
1404-
1405-
addTypeRef(GenericParam, sig);
1406-
addMetadataSource(Source);
1446+
for (auto entry : MetadataSources) {
1447+
addTypeRef(entry.type, sig);
1448+
addMetadataSource(entry.kind, entry.source);
14071449
}
14081450
}
14091451

0 commit comments

Comments
 (0)