Skip to content

CSE: While optimizing lazy property getters, don't inline non-ossa to ossa function #38184

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 1 commit into from
Jul 1, 2021
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
4 changes: 4 additions & 0 deletions lib/SILOptimizer/Transforms/CSE.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -915,6 +915,10 @@ static bool isLazyPropertyGetter(ApplyInst *ai) {
!callee->isLazyPropertyGetter())
return false;

// We cannot inline a non-ossa function into an ossa function
if (ai->getFunction()->hasOwnership() && !callee->hasOwnership())
return false;

// Only handle classes, but not structs.
// Lazy property getters of structs have an indirect inout self parameter.
// We don't know if the whole struct is overwritten between two getter calls.
Expand Down
3 changes: 3 additions & 0 deletions lib/SILOptimizer/Utils/SILInliner.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -348,6 +348,9 @@ SILInliner::inlineFullApply(FullApplySite apply,
SILInliner::InlineKind inlineKind,
SILOptFunctionBuilder &funcBuilder) {
assert(apply.canOptimize());
assert(!apply.getFunction()->hasOwnership() ||
apply.getReferencedFunctionOrNull()->hasOwnership());

SmallVector<SILValue, 8> appliedArgs;
for (const auto arg : apply.getArguments())
appliedArgs.push_back(arg);
Expand Down
75 changes: 75 additions & 0 deletions test/SILOptimizer/cse_ossa.sil
Original file line number Diff line number Diff line change
Expand Up @@ -1223,3 +1223,78 @@ bb0(%0 : $*Builtin.Int64, %1 : @guaranteed $Builtin.NativeObject):
destroy_value %copy : $Builtin.NativeObject
return %6 : $(Builtin.Int64, Builtin.Int64)
}

final class LazyKlass {
final var lazyProperty: Int64 { get set }
@_hasStorage @_hasInitialValue final var lazyPropertyStorage : Int64? { get set }
init()
}

sil private [lazy_getter] [noinline] @lazy_class_getter_nonossa : $@convention(method) (@guaranteed LazyKlass) -> Int64 {
bb0(%0 : $LazyKlass):
%2 = ref_element_addr %0 : $LazyKlass, #LazyKlass.lazyPropertyStorage
%3 = load %2 : $*Optional<Int64>
switch_enum %3 : $Optional<Int64>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2

bb1(%5 : $Int64):
br bb3(%5 : $Int64)

bb2:
%9 = integer_literal $Builtin.Int64, 27
%10 = struct $Int64 (%9 : $Builtin.Int64)
%12 = enum $Optional<Int64>, #Optional.some!enumelt, %10 : $Int64
store %12 to %2 : $*Optional<Int64>
br bb3(%10 : $Int64)

bb3(%15 : $Int64):
return %15 : $Int64
}

sil private [lazy_getter] [noinline] [ossa] @lazy_class_getter_ossa : $@convention(method) (@guaranteed LazyKlass) -> Int64 {
bb0(%0 : @guaranteed $LazyKlass):
%2 = ref_element_addr %0 : $LazyKlass, #LazyKlass.lazyPropertyStorage
%3 = load [trivial] %2 : $*Optional<Int64>
switch_enum %3 : $Optional<Int64>, case #Optional.some!enumelt: bb1, case #Optional.none!enumelt: bb2

bb1(%5 : $Int64):
br bb3(%5 : $Int64)

bb2:
%9 = integer_literal $Builtin.Int64, 27
%10 = struct $Int64 (%9 : $Builtin.Int64)
%12 = enum $Optional<Int64>, #Optional.some!enumelt, %10 : $Int64
store %12 to [trivial] %2 : $*Optional<Int64>
br bb3(%10 : $Int64)

bb3(%15 : $Int64):
return %15 : $Int64
}

// CHECK-LABEL: sil [ossa] @dont_cse_nonossa_getter :
// CHECK: [[GETTER:%[0-9]+]] = function_ref @lazy_class_getter_nonossa
// CHECK: apply [[GETTER]]
// CHECK: apply [[GETTER]]
// CHECK: } // end sil function 'dont_cse_nonossa_getter'
sil [ossa] @dont_cse_nonossa_getter : $@convention(thin) (@guaranteed LazyKlass) -> (Int64, Int64) {
bb0(%0 : @guaranteed $LazyKlass):
%getter = function_ref @lazy_class_getter_nonossa : $@convention(method) (@guaranteed LazyKlass) -> Int64
%call1 = apply %getter(%0) : $@convention(method) (@guaranteed LazyKlass) -> Int64
%call2 = apply %getter(%0) : $@convention(method) (@guaranteed LazyKlass) -> Int64
%res = tuple (%call1 : $Int64, %call2 : $Int64)
return %res : $(Int64, Int64)
}

// CHECK-LABEL: sil @cse_ossa_getter :
// CHECK: [[GETTER:%[0-9]+]] = function_ref @lazy_class_getter_ossa
// CHECK: apply [[GETTER]]
// CHECK-NOT: apply [[GETTER]]
// CHECK: } // end sil function 'cse_ossa_getter'
sil @cse_ossa_getter : $@convention(thin) (@guaranteed LazyKlass) -> (Int64, Int64) {
bb0(%0 : $LazyKlass):
%1 = function_ref @lazy_class_getter_ossa : $@convention(method) (@guaranteed LazyKlass) -> Int64
%2 = apply %1(%0) : $@convention(method) (@guaranteed LazyKlass) -> Int64
%3 = apply %1(%0) : $@convention(method) (@guaranteed LazyKlass) -> Int64
%4 = tuple (%2 : $Int64, %3 : $Int64)
return %4 : $(Int64, Int64)
}