Skip to content

SILGen plumbing for existential lvalue access fixes #9637

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
OpenClassExistentialKind, // opened class existential
// Translation LValue kinds (a subtype of logical)
OrigToSubstKind, // generic type substitution
SubstToOrigKind, // generic type substitution
Expand Down
26 changes: 15 additions & 11 deletions lib/SILGen/SILGenExpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4047,29 +4047,33 @@ 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);

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()),
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
6 changes: 5 additions & 1 deletion lib/SILGen/SILGenFunction.h
Original file line number Diff line number Diff line change
Expand Up @@ -957,7 +957,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 @@ -1284,6 +1284,10 @@ 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,
AccessKind accessKind);

RValue emitLoadOfLValue(SILLocation loc, LValue &&src, SGFContext C,
bool isGuaranteedValid = false);
Expand Down
196 changes: 189 additions & 7 deletions lib/SILGen/SILGenLValue.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -223,6 +223,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 @@ -579,20 +636,34 @@ namespace {
public:
OpenOpaqueExistentialComponent(CanArchetypeType openedArchetype)
: PhysicalPathComponent(getOpenedArchetypeTypeData(openedArchetype),
OpenedExistentialKind) {}
OpenOpaqueExistentialKind) {}

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?");
assert(base.getType().getPreferredExistentialRepresentation(SGF.SGM.M)
== ExistentialRepresentation::Opaque);
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);
Expand All @@ -604,6 +675,89 @@ namespace {
}
};

/// A local path component for the payload of a class existential.
///
/// TODO: Could be physical if we had a way to project out the
/// payload.
class OpenClassExistentialComponent : public LogicalPathComponent {
static LValueTypeData getOpenedArchetypeTypeData(CanArchetypeType type) {
return {
AbstractionPattern::getOpaque(), type,
SILType::getPrimitiveObjectType(type)
};
}
public:
OpenClassExistentialComponent(CanArchetypeType openedArchetype)
: LogicalPathComponent(getOpenedArchetypeTypeData(openedArchetype),
OpenClassExistentialKind) {}

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.isExistentialType() &&
"base for open existential component must be an existential");
assert(refType.getPreferredExistentialRepresentation(SGF.SGM.M)
== ExistentialRepresentation::Class);
auto ref = SGF.B.createOpenExistentialRef(
loc, result, getTypeOfRValue());

SGF.setArchetypeOpeningSite(cast<ArchetypeType>(getSubstFormalType()),
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 : cast<ArchetypeType>(getSubstFormalType())->getConformsTo())
conformances.push_back(ProtocolConformanceRef(proto));

auto 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 OpenClassExistentialComponent(
cast<ArchetypeType>(getSubstFormalType()));
return std::unique_ptr<LogicalPathComponent>(clone);
}

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

/// A physical path component which returns a literal address.
class ValueComponent : public PhysicalPathComponent {
ManagedValue Value;
Expand Down Expand Up @@ -1914,9 +2068,11 @@ LValue SILGenLValue::visitOpaqueValueExpr(OpaqueValueExpr *e,
SGF.OpaqueValueExprs.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()),
accessKind);
return lv;
}

Expand Down Expand Up @@ -2817,6 +2973,32 @@ ManagedValue SILGenFunction::emitAddressOfLValue(SILLocation loc,
return ManagedValue::forLValue(addr.getValue());
}

LValue
SILGenFunction::emitOpenExistentialLValue(SILLocation loc,
LValue &&lv,
CanArchetypeType openedArchetype,
AccessKind accessKind) {
// Open up the existential.
auto rep = lv.getTypeOfRValue()
.getPreferredExistentialRepresentation(SGM.M);
switch (rep) {
case ExistentialRepresentation::Opaque:
case ExistentialRepresentation::Boxed: {
lv.add<OpenOpaqueExistentialComponent>(openedArchetype);
break;
}
case ExistentialRepresentation::Class: {
lv.add<OpenClassExistentialComponent>(openedArchetype);
break;
}
default:
llvm_unreachable("Cannot perform lvalue access of "
"non-opaque, non-class existential");
}

return std::move(lv);
}

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