Skip to content

[ownership] update let properties opts to support ossa #29738

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 3 commits into from
Feb 28, 2020
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
39 changes: 18 additions & 21 deletions lib/SILOptimizer/IPO/LetPropertiesOpts.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,8 +203,8 @@ void LetPropertiesOpt::optimizeLetPropertyAccess(VarDecl *Property,
};

// Look for any instructions accessing let properties.
if (isa<RefElementAddrInst>(Load) || isa<StructElementAddrInst>(Load)
|| isa<BeginAccessInst>(Load)) {
if (isa<RefElementAddrInst>(Load) || isa<StructElementAddrInst>(Load) ||
isa<BeginAccessInst>(Load) || isa<BeginBorrowInst>(Load)) {
auto proj = cast<SingleValueInstruction>(Load);

// Copy the initializer into the function
Expand All @@ -216,7 +216,7 @@ void LetPropertiesOpt::optimizeLetPropertyAccess(VarDecl *Property,
++UI;

// A nested begin_access will be mapped as a separate "Load".
if (isa<BeginAccessInst>(User))
if (isa<BeginAccessInst>(User) || isa<BeginBorrowInst>(User))
continue;

if (!canReplaceLoadSequence(User))
Expand Down Expand Up @@ -372,18 +372,6 @@ bool LetPropertiesOpt::isConstantLetProperty(VarDecl *Property) {
LLVM_DEBUG(llvm::dbgs() << "Property '" << *Property
<< "' has no unknown uses\n");

// Only properties of simple types can be optimized.

// FIXME: Expansion
auto &TL = Module->Types.getTypeLowering(Property->getType(),
TypeExpansionContext::minimal());
if (!TL.isTrivial()) {
LLVM_DEBUG(llvm::dbgs() << "Property '" << *Property
<< "' is not of trivial type\n");
SkipProcessing.insert(Property);
return false;
}

PotentialConstantLetProperty.insert(Property);

return true;
Expand Down Expand Up @@ -412,6 +400,8 @@ LetPropertiesOpt::analyzeInitValue(SILInstruction *I, VarDecl *Property) {
&& "Store instruction should store into a proper let property");
(void) Dest;
value = SI->getSrc();
} else if (auto *copyAddr = dyn_cast<CopyAddrInst>(I)) {
value = copyAddr->getSrc();
}

// Check if it's just a copy from another instance of the struct.
Expand Down Expand Up @@ -530,8 +520,9 @@ void LetPropertiesOpt::collectPropertyAccess(SILInstruction *I,
<< *Property << "':\n";
llvm::dbgs() << "The instructions are:\n"; I->dumpInContext());

if (isa<RefElementAddrInst>(I) || isa<StructElementAddrInst>(I)
|| isa<BeginAccessInst>(I)) {
if (isa<RefElementAddrInst>(I) || isa<StructElementAddrInst>(I) ||
isa<BeginAccessInst>(I) || isa<CopyAddrInst>(I) ||
isa<BeginBorrowInst>(I)) {
// Check if there is a store to this property.
auto projection = cast<SingleValueInstruction>(I);
for (auto Use : getNonDebugUses(projection)) {
Expand All @@ -541,8 +532,17 @@ void LetPropertiesOpt::collectPropertyAccess(SILInstruction *I,

// Each begin_access is analyzed as a separate property access. Do not
// consider a begin_access a use of the current projection.
if (isa<BeginAccessInst>(User))
if (isa<BeginAccessInst>(User) || isa<BeginBorrowInst>(I))
continue;

if (auto *copyAddr = dyn_cast<CopyAddrInst>(User)) {
if (copyAddr->getSrc() != projection ||
!analyzeInitValue(copyAddr, Property)) {
SkipProcessing.insert(Property);
return;
}
continue;
}

if (auto *SI = dyn_cast<StoreInst>(User)) {
// There is a store into this property.
Expand Down Expand Up @@ -582,9 +582,6 @@ void LetPropertiesOpt::run(SILModuleTransform *T) {
// properties.
bool NonRemovable = !F.shouldOptimize();

// FIXME: We should be able to handle ownership.
NonRemovable &= !F.hasOwnership();

for (auto &BB : F) {
for (auto &I : BB)
// Look for any instructions accessing let properties.
Expand Down
43 changes: 23 additions & 20 deletions test/SILOptimizer/let_properties_opts.sil
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,20 @@ struct Point {
var x: Int64
var y: Int64
}
class HasCenter {

class HasCenter {
let centerPoint: Point = Point(x: 0, y: 0)

public func getCenter() -> Int64
}

sil hidden @$s19let_properties_opts5PointV1x1yACSi_SitcfC : $@convention(method) (Int64, Int64, @thin Point.Type) -> Point {
sil hidden [ossa] @$s19let_properties_opts5PointV1x1yACSi_SitcfC : $@convention(method) (Int64, Int64, @thin Point.Type) -> Point {
bb0(%0 : $Int64, %1 : $Int64, %2 : $@thin Point.Type):
%3 = struct $Point (%0 : $Int64, %1 : $Int64)
return %3 : $Point
}

// variable initialization expression of HasCenter.centerPoint
sil hidden [transparent] @$s19let_properties_opts9HasCenterC11centerPointAA0E0Vvpfi : $@convention(thin) () -> Point {
sil hidden [transparent] [ossa] @$s19let_properties_opts9HasCenterC11centerPointAA0E0Vvpfi : $@convention(thin) () -> Point {
bb0:
%0 = integer_literal $Builtin.Int64, 0
%1 = struct $Int64 (%0 : $Builtin.Int64)
Expand All @@ -35,43 +34,47 @@ bb0:
}

// HasCenter.centerPoint.getter
// CHECK-LABEL: sil hidden [transparent] @$s19let_properties_opts9HasCenterC11centerPointAA0E0Vvg : $@convention(method) (@guaranteed HasCenter) -> Point {
// CHECK: bb0(%0 : $HasCenter):
// CHECK-LABEL: sil hidden [transparent] [ossa] @$s19let_properties_opts9HasCenterC11centerPointAA0E0Vvg : $@convention(method) (@guaranteed HasCenter) -> Point {
// CHECK: bb0(%0 : @guaranteed $HasCenter):
// CHECK: [[LIT:%.*]] = integer_literal $Builtin.Int64, 0
// CHECK: [[INT:%.*]] = struct $Int64 ([[LIT]] : $Builtin.Int64)
// CHECK: [[PNT:%.*]] = struct $Point ([[INT]] : $Int64, [[INT]] : $Int64)
// CHECK: return [[PNT]] : $Point
// CHECK-LABEL: } // end sil function '$s19let_properties_opts9HasCenterC11centerPointAA0E0Vvg'
sil hidden [transparent] @$s19let_properties_opts9HasCenterC11centerPointAA0E0Vvg : $@convention(method) (@guaranteed HasCenter) -> Point {
bb0(%0 : $HasCenter):
sil hidden [transparent] [ossa] @$s19let_properties_opts9HasCenterC11centerPointAA0E0Vvg : $@convention(method) (@guaranteed HasCenter) -> Point {
bb0(%0 : @guaranteed $HasCenter):
%1 = ref_element_addr %0 : $HasCenter, #HasCenter.centerPoint
%2 = load %1 : $*Point
%2 = load [trivial] %1 : $*Point
return %2 : $Point
}

// HasCenter.getCenter()
// CHECK-LABEL: sil hidden @$s19let_properties_opts9HasCenterC9getCenterSiyF : $@convention(method) (@guaranteed HasCenter) -> Int64 {
// CHECK-LABEL: sil hidden [ossa] @$s19let_properties_opts9HasCenterC9getCenterSiyF : $@convention(method) (@guaranteed HasCenter) -> Int64 {
// CHECK: [[LIT:%.*]] = integer_literal $Builtin.Int64, 0
// CHECK: [[INT:%.*]] = struct $Int64 ([[LIT]] : $Builtin.Int64)
// CHECK: [[PNT:%.*]] = struct $Point ([[INT]] : $Int64, [[INT]] : $Int64)
// CHECK: [[X:%.*]] = struct_extract [[PNT]] : $Point, #Point.x
// CHECK: return [[X]] : $Int64
// CHECK-LABEL: } // end sil function '$s19let_properties_opts9HasCenterC9getCenterSiyF'
sil hidden @$s19let_properties_opts9HasCenterC9getCenterSiyF : $@convention(method) (@guaranteed HasCenter) -> Int64 {
bb0(%0 : $HasCenter):
sil hidden [ossa] @$s19let_properties_opts9HasCenterC9getCenterSiyF : $@convention(method) (@guaranteed HasCenter) -> Int64 {
bb0(%0 : @guaranteed $HasCenter):
%1 = ref_element_addr %0 : $HasCenter, #HasCenter.centerPoint
%2 = struct_element_addr %1 : $*Point, #Point.x
%3 = load %2 : $*Int64
%3 = load [trivial] %2 : $*Int64
return %3 : $Int64
}

// HasCenter.init()
sil hidden @$s19let_properties_opts9HasCenterCACycfc : $@convention(method) (@owned HasCenter) -> @owned HasCenter {
bb0(%0 : $HasCenter):
sil hidden [ossa] @$s19let_properties_opts9HasCenterCACycfc : $@convention(method) (@owned HasCenter) -> @owned HasCenter {
bb0(%0 : @owned $HasCenter):
%1 = integer_literal $Builtin.Int64, 0
%2 = struct $Int64 (%1 : $Builtin.Int64)
%3 = struct $Point (%2 : $Int64, %2 : $Int64)
%4 = ref_element_addr %0 : $HasCenter, #HasCenter.centerPoint
store %3 to %4 : $*Point
return %0 : $HasCenter
%3 = struct $Int64 (%1 : $Builtin.Int64)
%4 = struct $Point (%3 : $Int64, %3 : $Int64)
%5 = begin_borrow %0 : $HasCenter
%6 = ref_element_addr %5 : $HasCenter, #HasCenter.centerPoint
store %4 to [trivial] %6 : $*Point
end_borrow %5 : $HasCenter
%9 = copy_value %0 : $HasCenter
destroy_value %0 : $HasCenter
return %9 : $HasCenter
}
87 changes: 87 additions & 0 deletions test/SILOptimizer/let_properties_opts_non_trivial.sil
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
// RUN: %target-sil-opt -let-properties-opt -enable-sil-verify-all %s | %FileCheck %s

sil_stage canonical

import Builtin
import Swift

class Point {
let x: Int64 = 0
let y: Int64 = 0
}

class HasCenter {
let centerPoint: Point = Point()
public func getCenter() -> Int64
}

// Point.init()
sil hidden [ossa] @$s19let_properties_opts5PointCACycfc : $@convention(method) (@owned Point) -> @owned Point {
bb0(%0 : @owned $Point):
%1 = integer_literal $Builtin.Int64, 0
%2 = struct $Int64 (%1 : $Builtin.Int64)

%3 = begin_borrow %0 : $Point
%6 = ref_element_addr %3 : $Point, #Point.x
store %2 to [trivial] %6 : $*Int64
end_borrow %3 : $Point

%9 = begin_borrow %0 : $Point
%12 = ref_element_addr %9 : $Point, #Point.y
store %2 to [trivial] %12 : $*Int64
end_borrow %9 : $Point

%15 = copy_value %0 : $Point
destroy_value %0 : $Point
return %15 : $Point
}

// HasCenter.init()
sil hidden [ossa] @$s19let_properties_opts9HasCenterCACycfc : $@convention(method) (@owned HasCenter) -> @owned HasCenter {
// %0 // users: %2, %1
bb0(%0 : @owned $HasCenter):
%1 = alloc_ref $Point
// function_ref Point.init()
%2 = function_ref @$s19let_properties_opts5PointCACycfc : $@convention(method) (@owned Point) -> @owned Point
%3 = apply %2(%1) : $@convention(method) (@owned Point) -> @owned Point

%5 = begin_borrow %0 : $HasCenter // users: %8, %6
%6 = ref_element_addr %5 : $HasCenter, #HasCenter.centerPoint // user: %7

store %3 to [init] %6 : $*Point // id: %7
end_borrow %5 : $HasCenter // id: %8

%9 = copy_value %0 : $HasCenter // user: %11
destroy_value %0 : $HasCenter // id: %10
return %9 : $HasCenter // id: %11
} // end sil function '$s19let_properties_opts9HasCenterCACycfc'

// HasCenter.centerPoint.getter
sil hidden [transparent] [ossa] @$s19let_properties_opts9HasCenterC11centerPointAA0G0Cvg : $@convention(method) (@guaranteed HasCenter) -> @owned Point {
// %0 // users: %2, %1
bb0(%0 : @guaranteed $HasCenter):
debug_value %0 : $HasCenter, let, name "self", argno 1 // id: %1
%2 = ref_element_addr %0 : $HasCenter, #HasCenter.centerPoint // user: %3
%3 = load [copy] %2 : $*Point // user: %4
return %3 : $Point // id: %4
} // end sil function '$s19let_properties_opts9HasCenterC11centerPointAA0G0Cvg'

// HasCenter.getCenter()
// CHECK-LABEL: sil hidden [ossa] @$s19let_properties_opts9HasCenterC03getE0s5Int64VyF : $@convention(method) (@guaranteed HasCenter) -> Int64 {
// CHECK: [[LIT:%.*]] = integer_literal $Builtin.Int64, 0
// CHECK: [[INT:%.*]] = struct $Int64 ([[LIT]] : $Builtin.Int64)
// CHECK: return [[INT]] : $Int64
// CHECK-LABEL: } // end sil function '$s19let_properties_opts9HasCenterC03getE0s5Int64VyF'
sil hidden [ossa] @$s19let_properties_opts9HasCenterC03getE0s5Int64VyF : $@convention(method) (@guaranteed HasCenter) -> Int64 {
// %0 // users: %2, %1
bb0(%0 : @guaranteed $HasCenter):
debug_value %0 : $HasCenter, let, name "self", argno 1 // id: %1
%2 = ref_element_addr %0 : $HasCenter, #HasCenter.centerPoint // user: %3
%3 = load [copy] %2 : $*Point // users: %8, %4
%4 = begin_borrow %3 : $Point // users: %7, %5
%5 = ref_element_addr %4 : $Point, #Point.x // user: %6
%6 = load [trivial] %5 : $*Int64 // user: %9
end_borrow %4 : $Point // id: %7
destroy_value %3 : $Point // id: %8
return %6 : $Int64 // id: %9
} // end sil function '$s19let_properties_opts9HasCenterC03getE0s5Int64VyF'