Skip to content

Implement a few silcombine transformations for arrays #13652

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 2 commits into from
Dec 31, 2017
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
7 changes: 7 additions & 0 deletions include/swift/SIL/SILInstruction.h
Original file line number Diff line number Diff line change
Expand Up @@ -6000,6 +6000,13 @@ class MarkDependenceInst
SILValue getValue() const { return Operands[Value].get(); }
SILValue getBase() const { return Operands[Base].get(); }

void setValue(SILValue newVal) {
Operands[Value].set(newVal);
}
void setBase(SILValue newVal) {
Operands[Base].set(newVal);
}

ArrayRef<Operand> getAllOperands() const { return Operands.asArray(); }
MutableArrayRef<Operand> getAllOperands() { return Operands.asArray(); }
};
Expand Down
26 changes: 15 additions & 11 deletions lib/SILOptimizer/LoopTransforms/COWArrayOpt.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -797,7 +797,7 @@ bool COWArrayOpt::checkSafeElementValueUses(UserOperList &ElementValueUsers) {
static bool isArrayEltStore(StoreInst *SI) {
SILValue Dest = stripAddressProjections(SI->getDest());
if (auto *MD = dyn_cast<MarkDependenceInst>(Dest))
Dest = MD->getOperand(0);
Dest = MD->getValue();

if (auto *PtrToAddr =
dyn_cast<PointerToAddressInst>(stripAddressProjections(Dest)))
Expand Down Expand Up @@ -1095,17 +1095,21 @@ struct HoistableMakeMutable {
DepInsts.push_back(StructExtractArrayAddr);

// Check the base the array element address is dependent on.
auto *EnumArrayAddr = dyn_cast<EnumInst>(MarkDependence->getBase());
if (!EnumArrayAddr)
return false;
DepInsts.push_back(EnumArrayAddr);
auto *UncheckedRefCast =
dyn_cast<UncheckedRefCastInst>(EnumArrayAddr->getOperand());
if (!UncheckedRefCast)
return false;
DepInsts.push_back(UncheckedRefCast);
SILValue base = MarkDependence->getBase();

// We can optionally have an enum instruction here.
if (auto *EnumArrayAddr = dyn_cast<EnumInst>(base)) {
DepInsts.push_back(EnumArrayAddr);
base = EnumArrayAddr->getOperand();
}

// We can optionally have an unchecked cast.
if (auto *UncheckedRefCast = dyn_cast<UncheckedRefCastInst>(base)) {
DepInsts.push_back(UncheckedRefCast);
base = UncheckedRefCast->getOperand();
}

SILValue ArrayBuffer = stripValueProjections(UncheckedRefCast->getOperand(), DepInsts);
SILValue ArrayBuffer = stripValueProjections(base, DepInsts);
auto *BaseLoad = dyn_cast<LoadInst>(ArrayBuffer);
if (!BaseLoad || Loop->contains(BaseLoad->getOperand()->getParentBlock()))
return false;
Expand Down
3 changes: 3 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombiner.h
Original file line number Diff line number Diff line change
Expand Up @@ -240,6 +240,9 @@ class SILCombiner :
SILInstruction *visitUnreachableInst(UnreachableInst *UI);
SILInstruction *visitAllocRefDynamicInst(AllocRefDynamicInst *ARDI);
SILInstruction *visitEnumInst(EnumInst *EI);

SILInstruction *visitMarkDependenceInst(MarkDependenceInst *MDI);
SILInstruction *visitInitExistentialRefInst(InitExistentialRefInst *IER);
SILInstruction *visitConvertFunctionInst(ConvertFunctionInst *CFI);

/// Instruction visitor helpers.
Expand Down
69 changes: 69 additions & 0 deletions lib/SILOptimizer/SILCombiner/SILCombinerMiscVisitors.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1134,6 +1134,15 @@ SILInstruction *SILCombiner::visitStrongReleaseInst(StrongReleaseInst *SRI) {
isa<ObjCMetatypeToObjectInst>(SRI->getOperand()))
return eraseInstFromFunction(*SRI);

// Release of a classbound existential converted from a class is just a
// release of the class, squish the conversion.
if (auto ier = dyn_cast<InitExistentialRefInst>(SRI->getOperand()))
if (ier->hasOneUse()) {
SRI->setOperand(ier->getOperand());
eraseInstFromFunction(*ier);
return SRI;
}

return nullptr;
}

Expand Down Expand Up @@ -1406,3 +1415,63 @@ SILInstruction *SILCombiner::visitEnumInst(EnumInst *EI) {
return nullptr;
}

SILInstruction *SILCombiner::visitMarkDependenceInst(MarkDependenceInst *MDI) {
// Simplify the base operand of a MarkDependenceInst to eliminate unnecessary
// instructions that aren't adding value.
//
// Conversions to Optional.Some(x) often happen here, this isn't important
// for us, we can just depend on 'x' directly.
if (auto eiBase = dyn_cast<EnumInst>(MDI->getBase())) {
if (eiBase->hasOperand() && eiBase->hasOneUse()) {
MDI->setBase(eiBase->getOperand());
eraseInstFromFunction(*eiBase);
return MDI;
}
}

// Conversions from a class to AnyObject also happen a lot, we can just depend
// on the class reference.
if (auto ier = dyn_cast<InitExistentialRefInst>(MDI->getBase())) {
MDI->setBase(ier->getOperand());
if (ier->use_empty())
eraseInstFromFunction(*ier);
return MDI;
}

return nullptr;
}


SILInstruction *SILCombiner::
visitInitExistentialRefInst(InitExistentialRefInst *IER) {
// Arrays in particular end up with chains of init/open existential refs,
// which convert back and forth between a class reference and an existential
// reference e.g. like this:
//
// %a = init_existential_ref %x : $_ContiguousArrayStorageBase :
// $_ContiguousArrayStorageBase, $_NSArrayCore
// %b = open_existential_ref %a : $_NSArrayCore to
// $@opened("EA85...") _NSArrayCore
//
// %c = init_existential_ref %b : $@opened("EA85...") _NSArrayCore :
// $@opened("EA85...") _NSArrayCore, $AnyObject
// we can simplify this by having %c initialize itself from the %x reference
// directly.
if (auto *ORE = dyn_cast<OpenExistentialRefInst>(IER->getOperand())) {
if (auto *IER2 = dyn_cast<InitExistentialRefInst>(ORE->getOperand())) {

// We create a new instruction, instead of modifying the existing one
// in place, because we need the result type of "%c" but the operand list
// of "%a", and the number of dependent types could disagree.
return Builder.createInitExistentialRef(IER->getLoc(), IER->getType(),
IER2->getFormalConcreteType(),
IER2->getOperand(),
IER->getConformances());

}
}

return nullptr;
}


46 changes: 34 additions & 12 deletions test/SILOptimizer/pointer_conversion.swift
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,8 @@ func get<T>() -> T
public func testArray() {
let array: [Int] = get()
takesConstRawPointer(array)
// CHECK: [[OWNER:%.+]] = enum $Optional<AnyObject>, #Optional.some!enumelt.1,
// CHECK-NEXT: [[POINTER:%.+]] = struct $UnsafeRawPointer (
// CHECK-NEXT: [[DEP_POINTER:%.+]] = mark_dependence [[POINTER]] : $UnsafeRawPointer on [[OWNER]] : $Optional<AnyObject>
// CHECK: [[POINTER:%.+]] = struct $UnsafeRawPointer (
// CHECK-NEXT: [[DEP_POINTER:%.+]] = mark_dependence [[POINTER]] : $UnsafeRawPointer on {{.*}} : $_ContiguousArrayStorageBase
// CHECK: [[FN:%.+]] = function_ref @takesConstRawPointer
// CHECK-NEXT: apply [[FN]]([[DEP_POINTER]])
// CHECK-NOT: release
Expand All @@ -38,9 +37,8 @@ public func testArray() {
public func testArrayToOptional() {
let array: [Int] = get()
takesOptConstRawPointer(array)
// CHECK: [[OWNER:%.+]] = enum $Optional<AnyObject>, #Optional.some!enumelt.1,
// CHECK-NEXT: [[POINTER:%.+]] = struct $UnsafeRawPointer (
// CHECK-NEXT: [[DEP_POINTER:%.+]] = mark_dependence [[POINTER]] : $UnsafeRawPointer on [[OWNER]] : $Optional<AnyObject>
// CHECK: [[POINTER:%.+]] = struct $UnsafeRawPointer (
// CHECK-NEXT: [[DEP_POINTER:%.+]] = mark_dependence [[POINTER]] : $UnsafeRawPointer on {{.*}} : $_ContiguousArrayStorageBase
// CHECK-NEXT: [[OPT_POINTER:%.+]] = enum $Optional<UnsafeRawPointer>, #Optional.some!enumelt.1, [[DEP_POINTER]]
// CHECK: [[FN:%.+]] = function_ref @takesOptConstRawPointer
// CHECK-NEXT: apply [[FN]]([[OPT_POINTER]])
Expand All @@ -55,9 +53,8 @@ public func testArrayToOptional() {
public func testMutableArray() {
var array: [Int] = get()
takesMutableRawPointer(&array)
// CHECK: [[OWNER:%.+]] = enum $Optional<AnyObject>, #Optional.some!enumelt.1,
// CHECK-NEXT: [[POINTER:%.+]] = struct $UnsafeMutableRawPointer (
// CHECK-NEXT: [[DEP_POINTER:%.+]] = mark_dependence [[POINTER]] : $UnsafeMutableRawPointer on [[OWNER]] : $Optional<AnyObject>
// CHECK: [[POINTER:%.+]] = struct $UnsafeMutableRawPointer (
// CHECK-NEXT: [[DEP_POINTER:%.+]] = mark_dependence [[POINTER]] : $UnsafeMutableRawPointer on {{.*}} : $_ContiguousArrayStorageBase
// CHECK: [[FN:%.+]] = function_ref @takesMutableRawPointer
// CHECK-NEXT: apply [[FN]]([[DEP_POINTER]])
// CHECK-NOT: release
Expand All @@ -72,9 +69,8 @@ public func testMutableArray() {
public func testMutableArrayToOptional() {
var array: [Int] = get()
takesOptMutableRawPointer(&array)
// CHECK: [[OWNER:%.+]] = enum $Optional<AnyObject>, #Optional.some!enumelt.1,
// CHECK-NEXT: [[POINTER:%.+]] = struct $UnsafeMutableRawPointer (
// CHECK-NEXT: [[DEP_POINTER:%.+]] = mark_dependence [[POINTER]] : $UnsafeMutableRawPointer on [[OWNER]] : $Optional<AnyObject>
// CHECK: [[POINTER:%.+]] = struct $UnsafeMutableRawPointer (
// CHECK-NEXT: [[DEP_POINTER:%.+]] = mark_dependence [[POINTER]] : $UnsafeMutableRawPointer on {{.*}} : $_ContiguousArrayStorageBase
// CHECK-NEXT: [[OPT_POINTER:%.+]] = enum $Optional<UnsafeMutableRawPointer>, #Optional.some!enumelt.1, [[DEP_POINTER]]
// CHECK: [[FN:%.+]] = function_ref @takesOptMutableRawPointer
// CHECK-NEXT: apply [[FN]]([[OPT_POINTER]])
Expand Down Expand Up @@ -112,3 +108,29 @@ public func testOptionalArray() {
// CHECK-NEXT: [[NO_OWNER:%.+]] = enum $Optional<AnyObject>, #Optional.none!enumelt
// CHECK-NEXT: br [[CALL_BRANCH]]([[NO_POINTER]] : $Optional<UnsafeRawPointer>, [[NO_OWNER]] : $Optional<AnyObject>)
} // CHECK: end sil function '_T018pointer_conversion17testOptionalArrayyyF'


// CHECK-LABEL: sil @_T018pointer_conversion21arrayLiteralPromotionyyF
public func arrayLiteralPromotion() {
takesConstRawPointer([41,42,43,44])

// Stack allocate the array.
// TODO: When stdlib checks are enabled, this becomes heap allocated... :-(
// CHECK: alloc_ref {{.*}}[tail_elems $Int * {{.*}} : $Builtin.Word] $_ContiguousArrayStorage<Int>

// Store the elements.
// CHECK: [[ELT:%.+]] = integer_literal $Builtin.Int{{.*}}, 41
// CHECK: [[ELT:%.+]] = integer_literal $Builtin.Int{{.*}}, 42
// CHECK: [[ELT:%.+]] = integer_literal $Builtin.Int{{.*}}, 43
// CHECK: [[ELT:%.+]] = integer_literal $Builtin.Int{{.*}}, 44

// Call the function.
// CHECK: [[PTR:%.+]] = mark_dependence

// CHECK: [[FN:%.+]] = function_ref @takesConstRawPointer
// CHECK: apply [[FN]]([[PTR]])

// Release the heap value.
// CHECK: strong_release
}

32 changes: 32 additions & 0 deletions test/SILOptimizer/sil_combine.sil
Original file line number Diff line number Diff line change
Expand Up @@ -2083,6 +2083,19 @@ bb0(%0 : $B, %1 : $@sil_unowned B, %2 : $AnyObject, %3: $@sil_unmanaged AnyObjec
return %9999 : $(B, @sil_unowned B, AnyObject, @sil_unmanaged AnyObject)
}

// CHECK-LABEL: sil @collapse_init_open_init_ref_cast
// CHECK: bb0([[Ref:%.*]]: $MyClass):
// CHECK-NEXT: init_existential_ref
// CHECK-NEXT: return
sil @collapse_init_open_init_ref_cast : $@convention(thin) (MyClass) -> (AnyObject) {
bb0(%0: $MyClass):
%1 = init_existential_ref %0 : $MyClass : $MyClass, $AnyObject
%2 = open_existential_ref %1 : $AnyObject to $@opened("2CAE06CE-5F10-11E4-AF13-C82A1428F987") AnyObject
%3 = init_existential_ref %2 : $@opened("2CAE06CE-5F10-11E4-AF13-C82A1428F987") AnyObject : $@opened("2CAE06CE-5F10-11E4-AF13-C82A1428F987") AnyObject, $AnyObject
return %3 : $AnyObject
}


// CHECK-LABEL: sil @collapse_existential_pack_unpack_unchecked_ref_cast
// CHECK: bb0([[Ref:%.*]]: $MyClass):
// CHECK-NOT: init_existential_ref
Expand Down Expand Up @@ -3429,3 +3442,22 @@ bb1(%4 : $Builtin.Int32):
bb2(%5 : $MyErrorType):
throw %5 : $MyErrorType
}

// CHECK-LABEL: sil @mark_dependence_base
// CHECK: bb0(
// CHECK-NOT: init_existential_ref
// CHECK-NOT: enum
// CHECK-NEXT: mark_dependence
// CHECK-NEXT: load
// CHECK: return
sil @mark_dependence_base : $@convention(thin) (@inout Builtin.Int64, @owned B) -> Builtin.Int64 {
bb0(%0 : $*Builtin.Int64, %1 : $B):
%x = init_existential_ref %1 : $B : $B, $AnyObject
%2 = enum $Optional<AnyObject>, #Optional.some!enumelt.1, %x : $AnyObject
%3 = mark_dependence %0 : $*Builtin.Int64 on %2 : $Optional<AnyObject>
%4 = load %3 : $*Builtin.Int64
return %4 : $Builtin.Int64
}