Skip to content

Fix IRGenSIL to always look through DynamicallyEnforcedAddress. #9237

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
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
57 changes: 28 additions & 29 deletions lib/IRGen/IRGenSIL.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -274,11 +274,6 @@ class LoweredValue {
return kind == Kind::BoxWithAddress;
}

Address getAddress() const {
assert(kind == Kind::Address && "not an allocated address");
return address.getAddress();
}

StackAddress getStackAddress() const {
assert(kind == Kind::Address && "not an allocated address");
return address;
Expand All @@ -301,6 +296,17 @@ class LoweredValue {
assert(kind == Kind::DynamicallyEnforcedAddress);
return dynamicallyEnforcedAddress;
}

Address getAnyAddress() const {
if (kind == LoweredValue::Kind::Address) {
return address.getAddress();
} else if (kind == LoweredValue::Kind::ContainedAddress) {
return getAddressInContainer();
} else {
assert(kind == LoweredValue::Kind::DynamicallyEnforcedAddress);
return getDynamicallyEnforcedAddress().Addr;
}
}

void getExplosion(IRGenFunction &IGF, Explosion &ex) const;

Expand Down Expand Up @@ -559,15 +565,7 @@ class IRGenSILFunction :
/// Get the Address of a SIL value of address type, which must have been
/// lowered.
Address getLoweredAddress(SILValue v) {
auto &&lv = getLoweredValue(v);
if (lv.kind == LoweredValue::Kind::Address) {
return lv.getAddress();
} else if (lv.kind == LoweredValue::Kind::ContainedAddress) {
return lv.getAddressInContainer();
} else {
assert(lv.kind == LoweredValue::Kind::DynamicallyEnforcedAddress);
return lv.getDynamicallyEnforcedAddress().Addr;
}
return getLoweredValue(v).getAnyAddress();
}

StackAddress getLoweredStackAddress(SILValue v) {
Expand Down Expand Up @@ -2623,14 +2621,13 @@ static void addIncomingSILArgumentsToPHINodes(IRGenSILFunction &IGF,
OperandValueArrayRef args) {
unsigned phiIndex = 0;
for (SILValue arg : args) {
const LoweredValue &lv = IGF.getLoweredValue(arg);

if (lv.isAddress()) {
addIncomingAddressToPHINodes(IGF, lbb, phiIndex, lv.getAddress());
if (arg->getType().isAddress()) {
addIncomingAddressToPHINodes(IGF, lbb, phiIndex,
IGF.getLoweredAddress(arg));
continue;
}

Explosion argValue = lv.getExplosion(IGF);
Explosion argValue = IGF.getLoweredExplosion(arg);
addIncomingExplosionToPHINodes(IGF, lbb, phiIndex, argValue);
}
}
Expand Down Expand Up @@ -4647,16 +4644,18 @@ void IRGenSILFunction::visitIsNonnullInst(swift::IsNonnullInst *i) {
// Get the value we're testing, which may be a function, an address or an
// instance pointer.
llvm::Value *val;
const LoweredValue &lv = getLoweredValue(i->getOperand());

if (i->getOperand()->getType().is<SILFunctionType>()) {
Explosion values = lv.getExplosion(*this);
val = values.claimNext(); // Function pointer.
values.claimNext(); // Ignore the data pointer.
} else if (lv.isAddress()) {
val = lv.getAddress().getAddress();

SILValue operand = i->getOperand();
auto type = operand->getType();
if (type.isAddress()) {
val = getLoweredAddress(operand).getAddress();
} else if (auto fnType = type.getAs<SILFunctionType>()) {
Explosion values = getLoweredExplosion(operand);
val = values.claimNext(); // Function pointer.
if (fnType->getRepresentation() == SILFunctionTypeRepresentation::Thick)
(void) values.claimNext(); // Ignore the data pointer.
} else {
Explosion values = lv.getExplosion(*this);
Explosion values = getLoweredExplosion(operand);
val = values.claimNext();
}

Expand Down Expand Up @@ -5049,7 +5048,7 @@ void IRGenSILFunction::visitCopyAddrInst(swift::CopyAddrInst *i) {
setAllocatedAddressForBuffer(i->getDest(), addr);
}
} else {
Address dest = loweredDest.getAddress();
Address dest = loweredDest.getAnyAddress();

if (i->isInitializationOfDest()) {
if (i->isTakeOfSrc()) {
Expand Down
16 changes: 16 additions & 0 deletions test/IRGen/access_markers.sil
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import Swift

class A {
@sil_stored var property: Int { get set }
@sil_stored var exProperty: Any { get set }
deinit
init()
}
Expand Down Expand Up @@ -77,3 +78,18 @@ bb0(%0 : $A):
%20 = tuple ()
return %20 : $()
}

// rdar://31964550
// Just check that this doesn't crash.
sil @testCopyAddr : $(@guaranteed A) -> () {
bb0(%0 : $A):
%1 = alloc_stack $Any
%2 = ref_element_addr %0 : $A, #A.exProperty
%3 = begin_access [dynamic] [read] %2 : $*Any
copy_addr %2 to [initialization] %1 : $*Any
end_access %3 : $*Any
destroy_addr %1 : $*Any
dealloc_stack %1 : $*Any
%20 = tuple ()
return %20 : $()
}