Skip to content

Commit 6d4588c

Browse files
committed
Change SIL to track all LocalArchetypes and not just OpenedArchetypes.
1 parent 2d5985b commit 6d4588c

File tree

11 files changed

+141
-139
lines changed

11 files changed

+141
-139
lines changed

include/swift/SIL/SILInstruction.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -685,13 +685,13 @@ class SILInstruction : public llvm::ilist_node<SILInstruction> {
685685
return getResultsImpl().getTypes();
686686
}
687687

688-
/// Run the given function for each opened archetype this instruction
688+
/// Run the given function for each local archetype this instruction
689689
/// defines, passing the value that should be used to record the
690690
/// dependency.
691-
void forEachDefinedOpenedArchetype(
692-
llvm::function_ref<void(CanOpenedArchetypeType archetype,
691+
void forEachDefinedLocalArchetype(
692+
llvm::function_ref<void(CanLocalArchetypeType archetype,
693693
SILValue typeDependency)> function) const;
694-
bool definesOpenedArchetypes() const;
694+
bool definesLocalArchetypes() const;
695695

696696
MemoryBehavior getMemoryBehavior() const;
697697
ReleasingBehavior getReleasingBehavior() const;

include/swift/SIL/SILModule.h

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -345,22 +345,22 @@ class SILModule {
345345
/// projections, shared between all functions in the module.
346346
std::unique_ptr<IndexTrieNode> indexTrieRoot;
347347

348-
/// A mapping from root opened archetypes to the instructions which define
348+
/// A mapping from root local archetypes to the instructions which define
349349
/// them.
350350
///
351-
/// The value is either a SingleValueInstruction or a PlaceholderValue, in case
352-
/// an opened archetype definition is looked up during parsing or
353-
/// deserializing SIL, where opened archetypes can be forward referenced.
351+
/// The value is either a SingleValueInstruction or a PlaceholderValue,
352+
/// in case a local archetype definition is looked up during parsing or
353+
/// deserializing SIL, where local archetypes can be forward referenced.
354354
///
355355
/// In theory we wouldn't need to have the SILFunction in the key, because
356-
/// opened archetypes \em should be unique across the module. But currently
357-
/// in some rare cases SILGen re-uses the same opened archetype for multiple
356+
/// local archetypes \em should be unique across the module. But currently
357+
/// in some rare cases SILGen re-uses the same local archetype for multiple
358358
/// functions.
359-
using OpenedArchetypeKey = std::pair<OpenedArchetypeType *, SILFunction *>;
360-
llvm::DenseMap<OpenedArchetypeKey, SILValue> RootOpenedArchetypeDefs;
359+
using LocalArchetypeKey = std::pair<LocalArchetypeType *, SILFunction *>;
360+
llvm::DenseMap<LocalArchetypeKey, SILValue> RootLocalArchetypeDefs;
361361

362-
/// The number of PlaceholderValues in RootOpenedArchetypeDefs.
363-
int numUnresolvedOpenedArchetypes = 0;
362+
/// The number of PlaceholderValues in RootLocalArchetypeDefs.
363+
int numUnresolvedLocalArchetypes = 0;
364364

365365
/// The options passed into this SILModule.
366366
const SILOptions &Options;
@@ -444,31 +444,31 @@ class SILModule {
444444
regDeserializationNotificationHandlerForAllFuncOME = true;
445445
}
446446

447-
/// Returns the instruction which defines the given root opened archetype,
447+
/// Returns the instruction which defines the given root local archetype,
448448
/// e.g. an open_existential_addr.
449449
///
450-
/// In case the opened archetype is not defined yet (e.g. during parsing or
450+
/// In case the local archetype is not defined yet (e.g. during parsing or
451451
/// deserialization), a PlaceholderValue is returned. This should not be the
452452
/// case outside of parsing or deserialization.
453-
SILValue getRootOpenedArchetypeDef(CanOpenedArchetypeType archetype,
454-
SILFunction *inFunction);
453+
SILValue getRootLocalArchetypeDef(CanLocalArchetypeType archetype,
454+
SILFunction *inFunction);
455455

456-
/// Returns the instruction which defines the given root opened archetype,
456+
/// Returns the instruction which defines the given root local archetype,
457457
/// e.g. an open_existential_addr.
458458
///
459-
/// In contrast to getOpenedArchetypeDef, it is required that all opened
459+
/// In contrast to getLocalArchetypeDef, it is required that all local
460460
/// archetypes are resolved.
461461
SingleValueInstruction *
462-
getRootOpenedArchetypeDefInst(CanOpenedArchetypeType archetype,
463-
SILFunction *inFunction) {
462+
getRootLocalArchetypeDefInst(CanLocalArchetypeType archetype,
463+
SILFunction *inFunction) {
464464
return cast<SingleValueInstruction>(
465-
getRootOpenedArchetypeDef(archetype, inFunction));
465+
getRootLocalArchetypeDef(archetype, inFunction));
466466
}
467467

468-
/// Returns true if there are unresolved opened archetypes in the module.
468+
/// Returns true if there are unresolved local archetypes in the module.
469469
///
470470
/// This should only be the case during parsing or deserialization.
471-
bool hasUnresolvedOpenedArchetypeDefinitions();
471+
bool hasUnresolvedLocalArchetypeDefinitions();
472472

473473
/// Get a unique index for a struct or class field in layout order.
474474
///

include/swift/SIL/SILType.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ namespace swift {
5050
/// this is the task of the type visitor invoking it.
5151
/// \returns The found opened archetype or empty type otherwise.
5252
CanOpenedArchetypeType getOpenedArchetypeOf(CanType Ty);
53+
CanLocalArchetypeType getLocalArchetypeOf(CanType Ty);
5354

5455
/// How an existential type container is represented.
5556
enum class ExistentialRepresentation {

lib/SIL/IR/SILInstruction.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1531,17 +1531,17 @@ const ValueBase *SILInstructionResultArray::back() const {
15311531
// Defined opened archetypes
15321532
//===----------------------------------------------------------------------===//
15331533

1534-
bool SILInstruction::definesOpenedArchetypes() const {
1534+
bool SILInstruction::definesLocalArchetypes() const {
15351535
bool definesAny = false;
1536-
forEachDefinedOpenedArchetype([&](CanOpenedArchetypeType type,
1537-
SILValue dependency) {
1536+
forEachDefinedLocalArchetype([&](CanLocalArchetypeType type,
1537+
SILValue dependency) {
15381538
definesAny = true;
15391539
});
15401540
return definesAny;
15411541
}
15421542

1543-
void SILInstruction::forEachDefinedOpenedArchetype(
1544-
llvm::function_ref<void(CanOpenedArchetypeType, SILValue)> fn) const {
1543+
void SILInstruction::forEachDefinedLocalArchetype(
1544+
llvm::function_ref<void(CanLocalArchetypeType, SILValue)> fn) const {
15451545
switch (getKind()) {
15461546
#define SINGLE_VALUE_SINGLE_OPEN(TYPE) \
15471547
case SILInstructionKind::TYPE: { \

lib/SIL/IR/SILInstructions.cpp

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ static void *allocateTrailingInst(SILFunction &F, CountTypes... counts) {
4545

4646
namespace {
4747
class TypeDependentOperandCollector {
48-
SmallVector<CanOpenedArchetypeType, 4> rootOpenedArchetypes;
48+
SmallVector<CanLocalArchetypeType, 4> rootLocalArchetypes;
4949
bool hasDynamicSelf = false;
5050
public:
5151
void collect(CanType type);
@@ -72,27 +72,27 @@ class TypeDependentOperandCollector {
7272

7373
}
7474

75-
/// Collect root open archetypes from a given type into \p RootOpenedArchetypes.
76-
/// \p RootOpenedArchetypes is being used as a set. We don't use a real set type
75+
/// Collect root open archetypes from a given type into \p RootLocalArchetypes.
76+
/// \p RootLocalArchetypes is being used as a set. We don't use a real set type
7777
/// here for performance reasons.
7878
void TypeDependentOperandCollector::collect(CanType type) {
7979
if (!type)
8080
return;
8181
if (type->hasDynamicSelfType())
8282
hasDynamicSelf = true;
83-
if (!type->hasOpenedExistential())
83+
if (!type->hasLocalArchetype())
8484
return;
8585
type.visit([&](CanType t) {
86-
if (const auto opened = dyn_cast<OpenedArchetypeType>(t)) {
87-
const auto root = opened.getRoot();
86+
if (const auto local = dyn_cast<LocalArchetypeType>(t)) {
87+
const auto root = local.getRoot();
8888

89-
// Add this root opened archetype if it was not seen yet.
89+
// Add this root local archetype if it was not seen yet.
9090
// We don't use a set here, because the number of open archetypes
9191
// is usually very small and using a real set may introduce too
9292
// much overhead.
93-
if (std::find(rootOpenedArchetypes.begin(), rootOpenedArchetypes.end(),
94-
root) == rootOpenedArchetypes.end())
95-
rootOpenedArchetypes.push_back(root);
93+
if (std::find(rootLocalArchetypes.begin(), rootLocalArchetypes.end(),
94+
root) == rootLocalArchetypes.end())
95+
rootLocalArchetypes.push_back(root);
9696
}
9797
});
9898
}
@@ -112,12 +112,12 @@ void TypeDependentOperandCollector::collect(SubstitutionMap subs) {
112112
void TypeDependentOperandCollector::addTo(SmallVectorImpl<SILValue> &operands,
113113
SILFunction &F) {
114114
size_t firstArchetypeOperand = operands.size();
115-
for (CanOpenedArchetypeType archetype : rootOpenedArchetypes) {
116-
SILValue def = F.getModule().getRootOpenedArchetypeDef(archetype, &F);
115+
for (CanLocalArchetypeType archetype : rootLocalArchetypes) {
116+
SILValue def = F.getModule().getRootLocalArchetypeDef(archetype, &F);
117117
assert(def->getFunction() == &F &&
118-
"def of root opened archetype is in wrong function");
118+
"def of root local archetype is in wrong function");
119119

120-
// The archetypes in rootOpenedArchetypes have already been uniqued,
120+
// The archetypes in rootLocalArchetypes have already been uniqued,
121121
// but a single instruction can open multiple archetypes (e.g.
122122
// open_pack_element), so we also unique the actual operand values.
123123
// As above, we assume there are very few values in practice and so
@@ -130,9 +130,9 @@ void TypeDependentOperandCollector::addTo(SmallVectorImpl<SILValue> &operands,
130130
operands.push_back(F.getDynamicSelfMetadata());
131131
}
132132

133-
/// Collects all root opened archetypes from a type and a substitution list, and
133+
/// Collects all root local archetypes from a type and a substitution list, and
134134
/// forms a corresponding list of operands.
135-
/// We need to know the number of root opened archetypes to estimate the number
135+
/// We need to know the number of root local archetypes to estimate the number
136136
/// of corresponding operands for the instruction being formed, because we need
137137
/// to reserve enough memory for these operands.
138138
template <class... Sources>

lib/SIL/IR/SILModule.cpp

Lines changed: 25 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ SILModule::~SILModule() {
124124
assert(numAllocatedSlabs == freeSlabs.size() && "leaking slabs in SILModule");
125125
#endif
126126

127-
assert(!hasUnresolvedOpenedArchetypeDefinitions());
127+
assert(!hasUnresolvedLocalArchetypeDefinitions());
128128

129129
// Decrement ref count for each SILGlobalVariable with static initializers.
130130
for (SILGlobalVariable &v : silGlobals) {
@@ -259,15 +259,15 @@ void *SILModule::allocateInst(unsigned Size, unsigned Align) const {
259259
}
260260

261261
void SILModule::willDeleteInstruction(SILInstruction *I) {
262-
// Update RootOpenedArchetypeDefs.
263-
I->forEachDefinedOpenedArchetype([&](CanOpenedArchetypeType archeTy,
264-
SILValue dependency) {
265-
OpenedArchetypeKey key = {archeTy, I->getFunction()};
262+
// Update RootLocalArchetypeDefs.
263+
I->forEachDefinedLocalArchetype([&](CanLocalArchetypeType archeTy,
264+
SILValue dependency) {
265+
LocalArchetypeKey key = {archeTy, I->getFunction()};
266266
// In case `willDeleteInstruction` is called twice for the
267267
// same instruction, we need to check if the archetype is really
268268
// still in the map for this instruction.
269-
if (RootOpenedArchetypeDefs.lookup(key) == dependency)
270-
RootOpenedArchetypeDefs.erase(key);
269+
if (RootLocalArchetypeDefs.lookup(key) == dependency)
270+
RootLocalArchetypeDefs.erase(key);
271271
});
272272
}
273273

@@ -704,21 +704,21 @@ void SILModule::registerDeserializationNotificationHandler(
704704
deserializationNotificationHandlers.add(std::move(handler));
705705
}
706706

707-
SILValue SILModule::getRootOpenedArchetypeDef(CanOpenedArchetypeType archetype,
708-
SILFunction *inFunction) {
707+
SILValue SILModule::getRootLocalArchetypeDef(CanLocalArchetypeType archetype,
708+
SILFunction *inFunction) {
709709
assert(archetype->isRoot());
710710

711-
SILValue &def = RootOpenedArchetypeDefs[{archetype, inFunction}];
711+
SILValue &def = RootLocalArchetypeDefs[{archetype, inFunction}];
712712
if (!def) {
713-
numUnresolvedOpenedArchetypes++;
713+
numUnresolvedLocalArchetypes++;
714714
def = ::new PlaceholderValue(SILType::getPrimitiveAddressType(archetype));
715715
}
716716

717717
return def;
718718
}
719719

720-
bool SILModule::hasUnresolvedOpenedArchetypeDefinitions() {
721-
return numUnresolvedOpenedArchetypes != 0;
720+
bool SILModule::hasUnresolvedLocalArchetypeDefinitions() {
721+
return numUnresolvedLocalArchetypes != 0;
722722
}
723723

724724
/// Get a unique index for a struct or class field in layout order.
@@ -764,40 +764,40 @@ unsigned SILModule::getCaseIndex(EnumElementDecl *enumElement) {
764764
}
765765

766766
void SILModule::notifyAddedInstruction(SILInstruction *inst) {
767-
inst->forEachDefinedOpenedArchetype([&](CanOpenedArchetypeType archeTy,
768-
SILValue dependency) {
769-
SILValue &val = RootOpenedArchetypeDefs[{archeTy, inst->getFunction()}];
767+
inst->forEachDefinedLocalArchetype([&](CanLocalArchetypeType archeTy,
768+
SILValue dependency) {
769+
SILValue &val = RootLocalArchetypeDefs[{archeTy, inst->getFunction()}];
770770
if (val) {
771771
if (!isa<PlaceholderValue>(val)) {
772772
// Print a useful error message (and not just abort with an assert).
773-
llvm::errs() << "re-definition of root opened archetype in function "
773+
llvm::errs() << "re-definition of root local archetype in function "
774774
<< inst->getFunction()->getName() << ":\n";
775775
inst->print(llvm::errs());
776776
llvm::errs() << "previously defined in function "
777777
<< val->getFunction()->getName() << ":\n";
778778
val->print(llvm::errs());
779779
abort();
780780
}
781-
// The opened archetype was unresolved so far. Replace the placeholder
781+
// The local archetype was unresolved so far. Replace the placeholder
782782
// by inst.
783783
auto *placeholder = cast<PlaceholderValue>(val);
784784
placeholder->replaceAllUsesWith(dependency);
785785
::delete placeholder;
786-
numUnresolvedOpenedArchetypes--;
786+
numUnresolvedLocalArchetypes--;
787787
}
788788
val = dependency;
789789
});
790790
}
791791

792792
void SILModule::notifyMovedInstruction(SILInstruction *inst,
793793
SILFunction *fromFunction) {
794-
inst->forEachDefinedOpenedArchetype([&](CanOpenedArchetypeType archeTy,
795-
SILValue dependency) {
796-
OpenedArchetypeKey key = {archeTy, fromFunction};
797-
assert(RootOpenedArchetypeDefs.lookup(key) == dependency &&
794+
inst->forEachDefinedLocalArchetype([&](CanLocalArchetypeType archeTy,
795+
SILValue dependency) {
796+
LocalArchetypeKey key = {archeTy, fromFunction};
797+
assert(RootLocalArchetypeDefs.lookup(key) == dependency &&
798798
"archetype def was not registered");
799-
RootOpenedArchetypeDefs.erase(key);
800-
RootOpenedArchetypeDefs[{archeTy, inst->getFunction()}] = dependency;
799+
RootLocalArchetypeDefs.erase(key);
800+
RootLocalArchetypeDefs[{archeTy, inst->getFunction()}] = dependency;
801801
});
802802
}
803803

lib/SIL/IR/SILType.cpp

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@
2727
using namespace swift;
2828
using namespace swift::Lowering;
2929

30-
/// Find an opened archetype represented by this type.
30+
/// Find a local archetype represented by this type.
3131
/// It is assumed by this method that the type contains
3232
/// at most one opened archetype.
3333
/// Typically, it would be called from a type visitor.
@@ -36,13 +36,14 @@ using namespace swift::Lowering;
3636
/// this is the task of the type visitor invoking it.
3737
/// \returns The found archetype or empty type otherwise.
3838
CanOpenedArchetypeType swift::getOpenedArchetypeOf(CanType Ty) {
39+
return dyn_cast_or_null<OpenedArchetypeType>(getLocalArchetypeOf(Ty));
40+
}
41+
CanLocalArchetypeType swift::getLocalArchetypeOf(CanType Ty) {
3942
if (!Ty)
40-
return CanOpenedArchetypeType();
43+
return CanLocalArchetypeType();
4144
while (auto MetaTy = dyn_cast<AnyMetatypeType>(Ty))
4245
Ty = MetaTy.getInstanceType();
43-
if (Ty->isOpenedExistential())
44-
return cast<OpenedArchetypeType>(Ty);
45-
return CanOpenedArchetypeType();
46+
return dyn_cast<LocalArchetypeType>(Ty);
4647
}
4748

4849
SILType SILType::getExceptionType(const ASTContext &C) {

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6739,11 +6739,11 @@ bool SILParserState::parseDeclSIL(Parser &P) {
67396739
P.parseMatchingToken(tok::r_brace, RBraceLoc, diag::expected_sil_rbrace,
67406740
LBraceLoc);
67416741

6742-
// Check that there are no unresolved forward definitions of opened
6742+
// Check that there are no unresolved forward definitions of local
67436743
// archetypes.
6744-
if (M.hasUnresolvedOpenedArchetypeDefinitions())
6744+
if (M.hasUnresolvedLocalArchetypeDefinitions())
67456745
llvm_unreachable(
6746-
"All forward definitions of opened archetypes should be resolved");
6746+
"All forward definitions of local archetypes should be resolved");
67476747
}
67486748
}
67496749

0 commit comments

Comments
 (0)