Skip to content

Various existential l-value fixes #9864

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion lib/SILGen/LValue.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class PathComponent {
TupleElementKind, // tuple_element_addr
StructElementKind, // struct_element_addr
OptionalObjectKind, // optional projection
OpenedExistentialKind, // opened opaque existential
OpenOpaqueExistentialKind, // opened opaque existential
AddressorKind, // var/subscript addressor
ValueKind, // random base pointer as an lvalue
KeyPathApplicationKind, // applying a key path
Expand All @@ -107,6 +107,7 @@ class PathComponent {
OwnershipKind, // weak pointer remapping
AutoreleasingWritebackKind, // autorelease pointer on set
WritebackPseudoKind, // a fake component to customize writeback
OpenNonOpaqueExistentialKind, // opened class or metatype existential
// Translation LValue kinds (a subtype of logical)
OrigToSubstKind, // generic type substitution
SubstToOrigKind, // generic type substitution
Expand Down
30 changes: 19 additions & 11 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4010,29 +4010,37 @@ void SILGenFunction::emitOpenExistentialExprImpl(
llvm::function_ref<void(Expr *)> emitSubExpr) {
Optional<FormalEvaluationScope> writebackScope;

Type opaqueValueType = E->getOpaqueValue()->getType()->getRValueType();

// Emit the existential value.
ManagedValue existentialValue;
SILGenFunction::OpaqueValueState state;

AccessKind accessKind;
if (E->getExistentialValue()->getType()->is<LValueType>()) {
// Create a writeback scope for the access to the existential lvalue.
writebackScope.emplace(*this);

Type formalRValueType =
E->getOpaqueValue()->getType()->getLValueOrInOutObjectType();

accessKind = E->getExistentialValue()->getLValueAccessKind();
existentialValue = emitAddressOfLValue(
E->getExistentialValue(),
emitLValue(E->getExistentialValue(), accessKind),
accessKind);
auto lv = emitLValue(E->getExistentialValue(), accessKind);
lv = emitOpenExistentialLValue(E, std::move(lv),
CanArchetypeType(E->getOpenedArchetype()),
formalRValueType->getCanonicalType(),
accessKind);
auto addr = emitAddressOfLValue(E, std::move(lv), accessKind);
state = {addr, false, false};
} else {
accessKind = AccessKind::Read;
existentialValue = emitRValueAsSingleValue(
E->getExistentialValue(),
SGFContext::AllowGuaranteedPlusZero);
}
auto existentialValue = emitRValueAsSingleValue(
E->getExistentialValue(),
SGFContext::AllowGuaranteedPlusZero);

Type opaqueValueType = E->getOpaqueValue()->getType()->getRValueType();
SILGenFunction::OpaqueValueState state = emitOpenExistential(
state = emitOpenExistential(
E, existentialValue, E->getOpenedArchetype(),
getLoweredType(opaqueValueType), accessKind);
}

// Register the opaque value for the projected existential.
SILGenFunction::OpaqueValueRAII opaqueValueRAII(
Expand Down
7 changes: 6 additions & 1 deletion lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -975,7 +975,7 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
/// \param openedArchetype The opened existential archetype.
/// \param loweredOpenedType The lowered type of the projection, which in
/// practice will be the openedArchetype, possibly wrapped in a metatype.
SILGenFunction::OpaqueValueState
OpaqueValueState
emitOpenExistential(SILLocation loc,
ManagedValue existentialValue,
ArchetypeType *openedArchetype,
Expand Down Expand Up @@ -1302,6 +1302,11 @@ class LLVM_LIBRARY_VISIBILITY SILGenFunction
ManagedValue emitAddressOfLValue(SILLocation loc, LValue &&src,
AccessKind accessKind,
TSanKind tsanKind = TSanKind::None);
LValue emitOpenExistentialLValue(SILLocation loc,
LValue &&existentialLV,
CanArchetypeType openedArchetype,
CanType formalRValueType,
AccessKind accessKind);

RValue emitLoadOfLValue(SILLocation loc, LValue &&src, SGFContext C,
bool isGuaranteedValid = false);
Expand Down
231 changes: 217 additions & 14 deletions lib/SILGen/SILGenLValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,6 +227,63 @@ ManagedValue LogicalPathComponent::getMaterialized(SILGenFunction &SGF,
SILLocation loc,
ManagedValue base,
AccessKind kind) && {
if (getTypeOfRValue().getSwiftRValueType()->hasOpenedExistential()) {
if (kind == AccessKind::Read) {
// Emit a 'get' into the temporary.
RValue value =
std::move(*this).get(SGF, loc, base, SGFContext());

// Create a temporary.
std::unique_ptr<TemporaryInitialization> temporaryInit =
SGF.emitFormalAccessTemporary(loc,
SGF.getTypeLowering(getTypeOfRValue()));

std::move(value).forwardInto(SGF, loc, temporaryInit.get());

return temporaryInit->getManagedAddress();
}

assert(SGF.InWritebackScope &&
"materializing l-value for modification without writeback scope");

// Clone anything else about the component that we might need in the
// writeback.
auto clonedComponent = clone(SGF, loc);

SILValue mv;
{
FormalEvaluationScope Scope(SGF);

// Otherwise, we need to emit a get and set. Borrow the base for
// the getter.
ManagedValue getterBase =
base ? base.formalAccessBorrow(SGF, loc) : ManagedValue();

// Emit a 'get' into a temporary and then pop the borrow of base.
RValue value =
std::move(*this).get(SGF, loc, getterBase, SGFContext());

mv = std::move(value).forwardAsSingleValue(SGF, loc);
}

auto &TL = SGF.getTypeLowering(getTypeOfRValue());

// Create a temporary.
std::unique_ptr<TemporaryInitialization> temporaryInit =
SGF.emitFormalAccessTemporary(loc, TL);

SGF.emitSemanticStore(loc, mv, temporaryInit->getAddress(),
TL, IsInitialization);
temporaryInit->finishInitialization(SGF);

auto temporary = temporaryInit->getManagedAddress();

// Push a writeback for the temporary.
pushWriteback(SGF, loc, std::move(clonedComponent), base,
MaterializedLValue(temporary));
return temporary.unmanagedBorrow();
}

// If this is just for a read, emit a load into a temporary memory
// location.
if (kind == AccessKind::Read) {
Expand Down Expand Up @@ -574,30 +631,44 @@ namespace {
/// A physical path component which projects out an opened archetype
/// from an existential.
class OpenOpaqueExistentialComponent : public PhysicalPathComponent {
static LValueTypeData getOpenedArchetypeTypeData(CanArchetypeType type) {
return {
AbstractionPattern::getOpaque(), type,
SILType::getPrimitiveObjectType(type)
};
CanArchetypeType getOpenedArchetype() const {
return cast<ArchetypeType>(getSubstFormalType());
}
public:
OpenOpaqueExistentialComponent(CanArchetypeType openedArchetype)
: PhysicalPathComponent(getOpenedArchetypeTypeData(openedArchetype),
OpenedExistentialKind) {}
OpenOpaqueExistentialComponent(CanArchetypeType openedArchetype,
LValueTypeData typeData)
: PhysicalPathComponent(typeData, OpenOpaqueExistentialKind) {
assert(getOpenedArchetype() == openedArchetype);
}

ManagedValue offset(SILGenFunction &SGF, SILLocation loc, ManagedValue base,
AccessKind accessKind) && override {
assert(base.getType().isExistentialType() &&
"base for open existential component must be an existential");
assert(base.getType().isAddress() &&
"base value of open-existential component was not an address?");
SILValue addr;

SILValue addr = SGF.B.createOpenExistentialAddr(
auto rep = base.getType().getPreferredExistentialRepresentation(SGF.SGM.M);
switch (rep) {
case ExistentialRepresentation::Opaque:
addr = SGF.B.createOpenExistentialAddr(
loc, base.getValue(), getTypeOfRValue().getAddressType(),
getOpenedExistentialAccessFor(accessKind));
break;
case ExistentialRepresentation::Boxed: {
auto &TL = SGF.getTypeLowering(base.getType());
auto error = SGF.emitLoad(loc, base.getValue(), TL,
SGFContext(), IsNotTake);
addr = SGF.B.createOpenExistentialBox(
loc, error.getValue(), getTypeOfRValue().getAddressType());
break;
}
default:
llvm_unreachable("Bad existential representation for address-only type");
}

SGF.setArchetypeOpeningSite(cast<ArchetypeType>(getSubstFormalType()),
addr);
SGF.setArchetypeOpeningSite(getOpenedArchetype(), addr);
return ManagedValue::forLValue(addr);
}

Expand All @@ -606,6 +677,102 @@ namespace {
}
};

/// A local path component for the payload of a class or metatype existential.
///
/// TODO: Could be physical if we had a way to project out the
/// payload.
class OpenNonOpaqueExistentialComponent : public LogicalPathComponent {
CanArchetypeType OpenedArchetype;
public:
OpenNonOpaqueExistentialComponent(CanArchetypeType openedArchetype,
LValueTypeData typeData)
: LogicalPathComponent(typeData, OpenNonOpaqueExistentialKind),
OpenedArchetype(openedArchetype) {}

AccessKind getBaseAccessKind(SILGenFunction &SGF,
AccessKind kind) const override {
// Always use the same access kind for the base.
return kind;
}

void diagnoseWritebackConflict(LogicalPathComponent *RHS,
SILLocation loc1, SILLocation loc2,
SILGenFunction &SGF) override {
// no useful writeback diagnostics at this point
}

RValue get(SILGenFunction &SGF, SILLocation loc,
ManagedValue base, SGFContext c) && override {
auto refType = base.getType().getObjectType();
auto &TL = SGF.getTypeLowering(refType);

// Load the original value.
auto result = SGF.emitLoad(loc, base.getValue(), TL,
SGFContext(), IsNotTake);

assert(refType.isAnyExistentialType() &&
"base for open existential component must be an existential");
ManagedValue ref;
if (refType.is<ExistentialMetatypeType>()) {
assert(refType.getPreferredExistentialRepresentation(SGF.SGM.M)
== ExistentialRepresentation::Metatype);
ref = ManagedValue::forUnmanaged(
SGF.B.createOpenExistentialMetatype(loc,
result.getUnmanagedValue(),
getTypeOfRValue()));
} else {
assert(refType.getPreferredExistentialRepresentation(SGF.SGM.M)
== ExistentialRepresentation::Class);
ref = SGF.B.createOpenExistentialRef(loc, result, getTypeOfRValue());
}

SGF.setArchetypeOpeningSite(OpenedArchetype, ref.getValue());

return RValue(SGF, loc, getSubstFormalType(), ref);
}

void set(SILGenFunction &SGF, SILLocation loc,
RValue &&value, ManagedValue base) && override {
auto payload = std::move(value).forwardAsSingleValue(SGF, loc);

SmallVector<ProtocolConformanceRef, 2> conformances;
for (auto proto : OpenedArchetype->getConformsTo())
conformances.push_back(ProtocolConformanceRef(proto));

SILValue ref;
if (base.getType().is<ExistentialMetatypeType>()) {
ref = SGF.B.createInitExistentialMetatype(
loc,
payload,
base.getType().getObjectType(),
SGF.getASTContext().AllocateCopy(conformances));
} else {
ref = SGF.B.createInitExistentialRef(
loc,
base.getType().getObjectType(),
getSubstFormalType(),
payload,
SGF.getASTContext().AllocateCopy(conformances));
}

auto &TL = SGF.getTypeLowering(base.getType());
SGF.emitSemanticStore(loc, ref,
base.getValue(), TL, IsNotInitialization);
}

std::unique_ptr<LogicalPathComponent>
clone(SILGenFunction &SGF, SILLocation loc) const override {
LogicalPathComponent *clone =
new OpenNonOpaqueExistentialComponent(OpenedArchetype, getTypeData());
return std::unique_ptr<LogicalPathComponent>(clone);
}

void print(raw_ostream &OS) const override {
OS << "OpenNonOpaqueExistentialComponent(" << OpenedArchetype
<< ", ...)\n";
}
};

/// A physical path component which returns a literal address.
class ValueComponent : public PhysicalPathComponent {
ManagedValue Value;
Expand Down Expand Up @@ -1916,9 +2083,12 @@ LValue SILGenLValue::visitOpaqueValueExpr(OpaqueValueExpr *e,
openedExistentials.erase(known);

// Do formal evaluation of the underlying existential lvalue.
LValue lv = visitRec(opened->getExistentialValue(), accessKind);
lv.add<OpenOpaqueExistentialComponent>(
cast<ArchetypeType>(opened->getOpenedArchetype()->getCanonicalType()));
auto lv = visitRec(opened->getExistentialValue(), accessKind);
lv = SGF.emitOpenExistentialLValue(
opened, std::move(lv),
CanArchetypeType(opened->getOpenedArchetype()),
e->getType()->getLValueOrInOutObjectType()->getCanonicalType(),
accessKind);
return lv;
}

Expand Down Expand Up @@ -2815,6 +2985,39 @@ ManagedValue SILGenFunction::emitAddressOfLValue(SILLocation loc,
return ManagedValue::forLValue(addr.getValue());
}

LValue
SILGenFunction::emitOpenExistentialLValue(SILLocation loc,
LValue &&lv,
CanArchetypeType openedArchetype,
CanType formalRValueType,
AccessKind accessKind) {
assert(!formalRValueType->isLValueType());
LValueTypeData typeData = {
AbstractionPattern::getOpaque(), formalRValueType,
getLoweredType(formalRValueType).getObjectType()
};

// Open up the existential.
auto rep = lv.getTypeOfRValue()
.getPreferredExistentialRepresentation(SGM.M);
switch (rep) {
case ExistentialRepresentation::Opaque:
case ExistentialRepresentation::Boxed: {
lv.add<OpenOpaqueExistentialComponent>(openedArchetype, typeData);
break;
}
case ExistentialRepresentation::Metatype:
case ExistentialRepresentation::Class: {
lv.add<OpenNonOpaqueExistentialComponent>(openedArchetype, typeData);
break;
}
case ExistentialRepresentation::None:
llvm_unreachable("cannot open non-existential");
}

return std::move(lv);
}

void SILGenFunction::emitAssignToLValue(SILLocation loc, RValue &&src,
LValue &&dest) {
FormalEvaluationScope scope(*this);
Expand Down
Loading