Skip to content

Commit e76dc2d

Browse files
committed
[Devirtualizer] Replace begin_apply allocations.
When devirtualizing a yield_once_2 begin_apply, replace all uses of the allocation address, just as is done with the token.
1 parent 9bbb8e8 commit e76dc2d

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1291,7 +1291,9 @@ static std::optional<AccessorKind> getAccessorKind(StringRef ident) {
12911291
.Case("addressor", AccessorKind::Address)
12921292
.Case("mutableAddressor", AccessorKind::MutableAddress)
12931293
.Case("read", AccessorKind::Read)
1294+
.Case("read2", AccessorKind::Read2)
12941295
.Case("modify", AccessorKind::Modify)
1296+
.Case("modify2", AccessorKind::Modify2)
12951297
.Default(std::nullopt);
12961298
}
12971299

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -619,6 +619,10 @@ replaceBeginApplyInst(SILBuilder &builder, SILPassManager *pm, SILLocation loc,
619619
// Forward the token.
620620
oldBAI->getTokenResult()->replaceAllUsesWith(newBAI->getTokenResult());
621621

622+
if (auto *allocation = oldBAI->getCalleeAllocationResult()) {
623+
allocation->replaceAllUsesWith(newBAI->getCalleeAllocationResult());
624+
}
625+
622626
auto oldYields = oldBAI->getYieldedValues();
623627
auto newYields = newBAI->getYieldedValues();
624628
assert(oldYields.size() == newYields.size());
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
// RUN: %target-sil-opt \
2+
// RUN: -devirtualizer \
3+
// RUN: %s \
4+
// RUN: -enable-sil-verify-all \
5+
// RUN: -enable-experimental-feature CoroutineAccessors \
6+
// RUN: | %FileCheck %s
7+
8+
// REQUIRES: swift_feature_CoroutineAccessors
9+
10+
import Builtin
11+
12+
struct Int {
13+
var _value: Builtin.Word
14+
}
15+
16+
class Base {
17+
var x : Int { read }
18+
}
19+
20+
class Derived : Base {
21+
override var x : Int { read }
22+
}
23+
24+
// CHECK-LABEL: sil [ossa] @utilize_begin_apply : {{.*}} {
25+
// CHECK-NOT: class_method
26+
// CHECK-LABEL: } // end sil function 'utilize_begin_apply'
27+
sil [ossa] @utilize_begin_apply : $@convention(thin) () -> () {
28+
bb0:
29+
%derived = alloc_ref $Derived
30+
%base = upcast %derived : $Derived to $Base
31+
%reader = class_method %base : $Base, #Base.x!read2 : (Base) -> () -> (), $@yield_once_2 @convention(method) (@guaranteed Base) -> @yields Int
32+
(%value, %token, %allocation) = begin_apply %reader(%base) : $@yield_once_2 @convention(method) (@guaranteed Base) -> @yields Int
33+
end_apply %token as $()
34+
dealloc_stack %allocation : $*Builtin.SILToken
35+
destroy_value %base : $Base
36+
%retval = tuple ()
37+
return %retval : $()
38+
}
39+
40+
sil [ossa] @Base_x_read2 : $@yield_once_2 @convention(method) (@guaranteed Base) -> @yields Int
41+
42+
sil [ossa] @Derived_x_read2 : $@yield_once_2 @convention(method) (@guaranteed Base) -> @yields Int
43+
44+
sil_vtable Base {
45+
#Base.x!read2: @Base_x_read2
46+
}
47+
48+
sil_vtable Derived {
49+
#Base.x!read2: @Derived_x_read2 [override]
50+
}

0 commit comments

Comments
 (0)