forked from swiftlang/swift
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLocalTypeData.cpp
471 lines (397 loc) · 15.4 KB
/
LocalTypeData.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
//===--- LocalTypeData.cpp - Local type data search -----------------------===//
//
// This source file is part of the Swift.org open source project
//
// Copyright (c) 2014 - 2016 Apple Inc. and the Swift project authors
// Licensed under Apache License v2.0 with Runtime Library Exception
//
// See https://swift.org/LICENSE.txt for license information
// See https://swift.org/CONTRIBUTORS.txt for the list of Swift project authors
//
//===----------------------------------------------------------------------===//
//
// This file implements routines for finding and caching local type data
// for a search.
//
//===----------------------------------------------------------------------===//
#include "LocalTypeData.h"
#include "Fulfillment.h"
#include "GenMeta.h"
#include "GenProto.h"
#include "IRGenDebugInfo.h"
#include "IRGenFunction.h"
#include "IRGenModule.h"
#include "swift/AST/IRGenOptions.h"
#include "swift/SIL/SILModule.h"
using namespace swift;
using namespace irgen;
LocalTypeDataKey LocalTypeDataKey::getCachingKey() const {
return { Type, Kind.getCachingKind() };
}
LocalTypeDataKind LocalTypeDataKind::getCachingKind() const {
// Most local type data kinds are already canonical.
if (!isConcreteProtocolConformance()) return *this;
// Map protocol conformances to their root normal conformance.
auto conformance = getConcreteProtocolConformance();
return forConcreteProtocolWitnessTable(
conformance->getRootNormalConformance());
}
LocalTypeDataCache &IRGenFunction::getOrCreateLocalTypeData() {
// Lazily allocate it.
if (LocalTypeData) return *LocalTypeData;
LocalTypeData = new LocalTypeDataCache();
return *LocalTypeData;
}
void IRGenFunction::destroyLocalTypeData() {
delete LocalTypeData;
}
unsigned LocalTypeDataCache::CacheEntry::cost() const {
switch (getKind()) {
case Kind::Concrete:
return static_cast<const ConcreteCacheEntry*>(this)->cost();
case Kind::Abstract:
return static_cast<const AbstractCacheEntry*>(this)->cost();
}
llvm_unreachable("bad cache entry kind");
}
void LocalTypeDataCache::CacheEntry::erase() const {
switch (getKind()) {
case Kind::Concrete:
delete static_cast<const ConcreteCacheEntry*>(this);
return;
case Kind::Abstract:
delete static_cast<const AbstractCacheEntry*>(this);
return;
}
llvm_unreachable("bad cache entry kind");
}
llvm::Value *IRGenFunction::getLocalTypeData(CanType type,
LocalTypeDataKind kind) {
assert(LocalTypeData);
return LocalTypeData->get(*this, LocalTypeDataCache::getKey(type, kind));
}
llvm::Value *IRGenFunction::tryGetConcreteLocalTypeData(LocalTypeDataKey key) {
if (!LocalTypeData) return nullptr;
return LocalTypeData->tryGet(*this, key, /*allow abstract*/ false);
}
llvm::Value *IRGenFunction::tryGetLocalTypeData(LocalTypeDataKey key) {
if (!LocalTypeData) return nullptr;
return LocalTypeData->tryGet(*this, key);
}
llvm::Value *LocalTypeDataCache::tryGet(IRGenFunction &IGF, Key key,
bool allowAbstract) {
auto it = Map.find(key);
if (it == Map.end()) return nullptr;
auto &chain = it->second;
CacheEntry *best = nullptr, *bestPrev = nullptr;
Optional<unsigned> bestCost;
CacheEntry *next = chain.Root, *nextPrev = nullptr;
while (next) {
CacheEntry *cur = next, *curPrev = nextPrev;
nextPrev = cur;
next = cur->getNext();
// Ignore abstract entries if so requested.
if (!allowAbstract && cur->getKind() != CacheEntry::Kind::Concrete)
continue;
// Ignore unacceptable entries.
if (!IGF.isActiveDominancePointDominatedBy(cur->DefinitionPoint))
continue;
// If there's a collision, compare by cost, ignoring higher-cost entries.
if (best) {
// Compute the cost of the best entry if we haven't done so already.
// If that's zero, go ahead and short-circuit out.
if (!bestCost) {
bestCost = best->cost();
if (*bestCost == 0) break;
}
auto curCost = cur->cost();
if (curCost >= *bestCost) continue;
// Replace the best cost and fall through.
bestCost = curCost;
}
best = cur;
bestPrev = curPrev;
}
// If we didn't find anything, we're done.
if (!best) return nullptr;
// Okay, we've found the best entry available.
switch (best->getKind()) {
// For concrete caches, this is easy.
case CacheEntry::Kind::Concrete:
return static_cast<ConcreteCacheEntry*>(best)->Value;
// For abstract caches, we need to follow a path.
case CacheEntry::Kind::Abstract: {
auto entry = static_cast<AbstractCacheEntry*>(best);
// Follow the path.
auto &source = AbstractSources[entry->SourceIndex];
auto result = entry->follow(IGF, source);
// Following the path automatically caches at every point along it,
// including the end.
assert(chain.Root->DefinitionPoint == IGF.getActiveDominancePoint());
assert(chain.Root->getKind() == CacheEntry::Kind::Concrete);
return result;
}
}
llvm_unreachable("bad cache entry kind");
}
llvm::Value *
LocalTypeDataCache::AbstractCacheEntry::follow(IRGenFunction &IGF,
AbstractSource &source) const {
switch (source.getKind()) {
case AbstractSource::Kind::TypeMetadata:
return Path.followFromTypeMetadata(IGF, source.getType(),
source.getValue(), nullptr);
case AbstractSource::Kind::ProtocolWitnessTable:
return Path.followFromWitnessTable(IGF, source.getType(),
source.getProtocolConformance(),
source.getValue(), nullptr);
}
llvm_unreachable("bad source kind");
}
static void maybeEmitDebugInfoForLocalTypeData(IRGenFunction &IGF,
LocalTypeDataKey key,
llvm::Value *data) {
// Only if debug info is enabled.
if (!IGF.IGM.DebugInfo) return;
// Only for type metadata.
if (key.Kind != LocalTypeDataKind::forTypeMetadata()) return;
// Only for archetypes, and not for opened archetypes.
auto type = dyn_cast<ArchetypeType>(key.Type);
if (!type) return;
if (type->getOpenedExistentialType()) return;
// At -O0, create an alloca to keep the type alive.
auto name = type->getFullName();
if (!IGF.IGM.IRGen.Opts.Optimize) {
auto temp = IGF.createAlloca(data->getType(), IGF.IGM.getPointerAlignment(),
name);
IGF.Builder.CreateStore(data, temp);
data = temp.getAddress();
}
// Emit debug info for the metadata.
IGF.IGM.DebugInfo->emitTypeMetadata(IGF, data, name);
}
void IRGenFunction::setScopedLocalTypeData(LocalTypeDataKey key,
llvm::Value *data) {
maybeEmitDebugInfoForLocalTypeData(*this, key, data);
// Register with the active ConditionalDominanceScope if necessary.
bool isConditional = isConditionalDominancePoint();
if (isConditional) {
registerConditionalLocalTypeDataKey(key);
}
getOrCreateLocalTypeData().addConcrete(getActiveDominancePoint(),
isConditional, key, data);
}
void IRGenFunction::setUnscopedLocalTypeData(LocalTypeDataKey key,
llvm::Value *data) {
maybeEmitDebugInfoForLocalTypeData(*this, key, data);
// This is supportable, but it would require ensuring that we add the
// entry after any conditional entries; otherwise the stack discipline
// will get messed up.
assert(!isConditionalDominancePoint() &&
"adding unscoped local type data while in conditional scope");
getOrCreateLocalTypeData().addConcrete(DominancePoint::universal(),
/*conditional*/ false, key, data);
}
void IRGenFunction::bindLocalTypeDataFromTypeMetadata(CanType type,
IsExact_t isExact,
llvm::Value *metadata) {
// Remember that we have this type metadata concretely.
if (isExact) {
if (!metadata->hasName()) setTypeMetadataName(IGM, metadata, type);
setScopedLocalTypeData(type, LocalTypeDataKind::forTypeMetadata(), metadata);
}
// Don't bother adding abstract fulfillments at a conditional dominance
// point; we're too likely to throw them all away.
if (isConditionalDominancePoint())
return;
getOrCreateLocalTypeData()
.addAbstractForTypeMetadata(*this, type, isExact, metadata);
}
void LocalTypeDataCache::addAbstractForTypeMetadata(IRGenFunction &IGF,
CanType type,
IsExact_t isExact,
llvm::Value *metadata) {
// Look for anything at all that's fulfilled by this. If we don't find
// anything, stop.
FulfillmentMap fulfillments;
if (!fulfillments.searchTypeMetadata(IGF.IGM, type, isExact,
/*source*/ 0, MetadataPath(),
FulfillmentMap::Everything())) {
return;
}
addAbstractForFulfillments(IGF, std::move(fulfillments),
[&]() -> AbstractSource {
return AbstractSource(type, metadata);
});
}
void LocalTypeDataCache::
addAbstractForFulfillments(IRGenFunction &IGF, FulfillmentMap &&fulfillments,
llvm::function_ref<AbstractSource()> createSource) {
// Add the source lazily.
Optional<unsigned> sourceIndex;
auto getSourceIndex = [&]() -> unsigned {
if (!sourceIndex) {
AbstractSources.emplace_back(createSource());
sourceIndex = AbstractSources.size() - 1;
}
return *sourceIndex;
};
for (auto &fulfillment : fulfillments) {
CanType type = CanType(fulfillment.first.first);
LocalTypeDataKind localDataKind;
// For now, ignore witness-table fulfillments when they're not for
// archetypes.
if (ProtocolDecl *protocol = fulfillment.first.second) {
if (auto archetype = dyn_cast<ArchetypeType>(type)) {
auto conformsTo = archetype->getConformsTo();
auto it = std::find(conformsTo.begin(), conformsTo.end(), protocol);
if (it == conformsTo.end()) continue;
localDataKind = LocalTypeDataKind::forAbstractProtocolWitnessTable(*it);
} else {
continue;
}
} else {
// Ignore type metadata fulfillments for non-dependent types that
// we can produce very cheaply. We don't want to end up emitting
// the type metadata for Int by chasing through N layers of metadata
// just because that path happens to be in the cache.
if (!type->hasArchetype() &&
isTypeMetadataAccessTrivial(IGF.IGM, type)) {
continue;
}
localDataKind = LocalTypeDataKind::forTypeMetadata();
}
// Find the chain for the key.
auto key = getKey(type, localDataKind);
auto &chain = Map[key];
// Check whether there's already an entry that's at least as good as the
// fulfillment.
Optional<unsigned> fulfillmentCost;
auto getFulfillmentCost = [&]() -> unsigned {
if (!fulfillmentCost)
fulfillmentCost = fulfillment.second.Path.cost();
return *fulfillmentCost;
};
bool isConditional = IGF.isConditionalDominancePoint();
bool foundBetter = false;
for (CacheEntry *cur = chain.Root, *last = nullptr; cur;
last = cur, cur = cur->getNext()) {
// Ensure the entry is acceptable.
if (!IGF.isActiveDominancePointDominatedBy(cur->DefinitionPoint))
continue;
// Ensure that the entry isn't better than the fulfillment.
auto curCost = cur->cost();
if (curCost == 0 || curCost <= getFulfillmentCost()) {
foundBetter = true;
break;
}
// If the entry is defined at the current point, (1) we know there
// won't be a better entry and (2) we should remove it.
if (cur->DefinitionPoint == IGF.getActiveDominancePoint() &&
!isConditional) {
// Splice it out of the chain.
assert(!cur->isConditional());
chain.eraseEntry(last, cur);
break;
}
}
if (foundBetter) continue;
// Okay, make a new entry.
// Register with the conditional dominance scope if necessary.
if (isConditional) {
IGF.registerConditionalLocalTypeDataKey(key);
}
// Allocate the new entry.
auto newEntry = new AbstractCacheEntry(IGF.getActiveDominancePoint(),
isConditional,
getSourceIndex(),
std::move(fulfillment.second.Path));
// Add it to the front of the chain.
chain.push_front(newEntry);
}
}
void LocalTypeDataCache::dump() const {
auto &out = llvm::errs();
if (Map.empty()) {
out << "(empty)\n";
return;
}
for (auto &mapEntry : Map) {
mapEntry.first.print(out);
out << " => [";
if (mapEntry.second.Root) out << "\n";
for (auto cur = mapEntry.second.Root; cur; cur = cur->getNext()) {
out << " (";
if (cur->DefinitionPoint.isUniversal()) out << "universal";
else out << cur->DefinitionPoint.as<void>();
out << ") ";
if (cur->isConditional()) out << "conditional ";
switch (cur->getKind()) {
case CacheEntry::Kind::Concrete: {
auto entry = static_cast<const ConcreteCacheEntry*>(cur);
out << "concrete: " << entry->Value << "\n ";
if (!isa<llvm::Instruction>(entry->Value)) out << " ";
entry->Value->dump();
break;
}
case CacheEntry::Kind::Abstract: {
auto entry = static_cast<const AbstractCacheEntry*>(cur);
out << "abstract: source=" << entry->SourceIndex << "\n";
break;
}
}
}
out << "]\n";
}
}
void LocalTypeDataKey::dump() const {
print(llvm::errs());
}
void LocalTypeDataKey::print(llvm::raw_ostream &out) const {
out << "(" << Type.getPointer()
<< " (" << Type << "), ";
Kind.print(out);
out << ")";
}
void LocalTypeDataKind::dump() const {
print(llvm::errs());
}
void LocalTypeDataKind::print(llvm::raw_ostream &out) const {
if (isConcreteProtocolConformance()) {
out << "ConcreteConformance(";
getConcreteProtocolConformance()->printName(out);
out << ")";
} else if (isAbstractProtocolConformance()) {
out << "AbstractConformance("
<< getAbstractProtocolConformance()->getName()
<< ")";
} else if (Value == TypeMetadata) {
out << "TypeMetadata";
} else if (Value == ValueWitnessTable) {
out << "ValueWitnessTable";
} else {
assert(isSingletonKind());
ValueWitness witness = ValueWitness(Value - ValueWitnessBase);
out << getValueWitnessName(witness);
}
}
IRGenFunction::ConditionalDominanceScope::~ConditionalDominanceScope() {
IGF.ConditionalDominance = OldScope;
// Remove any conditional entries from the chains that were added in this
// scope.
for (auto &key : RegisteredKeys) {
IGF.LocalTypeData->eraseConditional(key);
}
}
void LocalTypeDataCache::eraseConditional(ArrayRef<LocalTypeDataKey> keys) {
for (auto &key : keys) {
auto &chain = Map[key];
// Our ability to simply delete the front of the chain relies on an
// assumption that (1) conditional additions always go to the front of
// the chain and (2) we never add something unconditionally while in
// an unconditional scope.
assert(chain.Root);
assert(chain.Root->isConditional());
chain.eraseEntry(nullptr, chain.Root);
}
}