Skip to content

Commit a589c41

Browse files
committed
Merge remote-tracking branch 'origin/main' into rebranch
2 parents d86ce1b + 83d7dbb commit a589c41

File tree

10 files changed

+57
-33
lines changed

10 files changed

+57
-33
lines changed

include/swift/SILOptimizer/Utils/Devirtualize.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ bool canDevirtualizeClassMethod(FullApplySite AI, ClassDecl *CD,
7676
CanType ClassType,
7777
OptRemark::Emitter *ORE = nullptr,
7878
bool isEffectivelyFinalMethod = false);
79-
SILFunction *getTargetClassMethod(SILModule &M, ClassDecl *CD,
79+
SILFunction *getTargetClassMethod(SILModule &M, FullApplySite as, ClassDecl *CD,
8080
CanType ClassType, MethodInst *MI);
8181
CanType getSelfInstanceType(CanType ClassOrMetatypeType);
8282

lib/SILOptimizer/Mandatory/MoveOnlyWrappedTypeEliminator.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -234,6 +234,7 @@ struct SILMoveOnlyWrappedTypeEliminatorVisitor
234234
NO_UPDATE_NEEDED(BridgeObjectToRef)
235235
NO_UPDATE_NEEDED(BeginAccess)
236236
NO_UPDATE_NEEDED(EndAccess)
237+
NO_UPDATE_NEEDED(EndCOWMutationAddr)
237238
NO_UPDATE_NEEDED(ClassMethod)
238239
NO_UPDATE_NEEDED(FixLifetime)
239240
NO_UPDATE_NEEDED(AddressToPointer)

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -116,12 +116,13 @@ static void addMandatoryDiagnosticOptPipeline(SILPassPipelinePlan &P) {
116116
// Select access kind after capture promotion and before stack promotion.
117117
// This guarantees that stack-promotable boxes have [static] enforcement.
118118
P.addAccessEnforcementSelection();
119-
119+
/* Temporarily disabled: rdar://154686063, rdar://154713388
120120
#ifdef SWIFT_ENABLE_SWIFT_IN_SWIFT
121121
P.addMandatoryAllocBoxToStack();
122122
#else
123+
*/
123124
P.addLegacyAllocBoxToStack();
124-
#endif
125+
//#endif
125126
P.addNoReturnFolding();
126127
P.addBooleanLiteralFolding();
127128
addDefiniteInitialization(P);
@@ -417,7 +418,8 @@ void addHighLevelLoopOptPasses(SILPassPipelinePlan &P) {
417418
void addFunctionPasses(SILPassPipelinePlan &P,
418419
OptimizationLevelKind OpLevel) {
419420
// Promote box allocations to stack allocations.
420-
P.addAllocBoxToStack();
421+
// TODO: change this to add addAllocBoxToStack once rdar://154686063, rdar://154713388 is fixed
422+
P.addLegacyAllocBoxToStack();
421423

422424
if (P.getOptions().DestroyHoisting == DestroyHoistingOption::On) {
423425
P.addDestroyAddrHoisting();

lib/SILOptimizer/Transforms/SpeculativeDevirtualizer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -446,7 +446,7 @@ static bool tryToSpeculateTarget(SILPassManager *pm, FullApplySite AI, ClassHier
446446

447447
// Try to devirtualize the static class of instance
448448
// if it is possible.
449-
if (auto F = getTargetClassMethod(M, CD, ClassType, CMI)) {
449+
if (auto F = getTargetClassMethod(M, AI, CD, ClassType, CMI)) {
450450
// Do not devirtualize if a method in the base class is marked
451451
// as non-optimizable. This way it is easy to disable the
452452
// devirtualization of this method in the base class and

lib/SILOptimizer/Utils/Devirtualize.cpp

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -712,7 +712,7 @@ void swift::deleteDevirtualizedApply(ApplySite old) {
712712
recursivelyDeleteTriviallyDeadInstructions(oldApply, true);
713713
}
714714

715-
SILFunction *swift::getTargetClassMethod(SILModule &module, ClassDecl *cd,
715+
SILFunction *swift::getTargetClassMethod(SILModule &module, FullApplySite as, ClassDecl *cd,
716716
CanType classType, MethodInst *mi) {
717717
assert((isa<ClassMethodInst>(mi) || isa<SuperMethodInst>(mi)) &&
718718
"Only class_method and super_method instructions are supported");
@@ -721,6 +721,11 @@ SILFunction *swift::getTargetClassMethod(SILModule &module, ClassDecl *cd,
721721

722722
SILType silType = SILType::getPrimitiveObjectType(classType);
723723
if (auto *vtable = module.lookUpSpecializedVTable(silType)) {
724+
// We cannot de-virtualize a generic method call to a specialized method.
725+
// This would result in wrong argument/return calling conventions.
726+
if (as.getSubstitutionMap().hasAnySubstitutableParams())
727+
return nullptr;
728+
724729
return vtable->getEntry(module, member)->getImplementation();
725730
}
726731

@@ -757,7 +762,7 @@ bool swift::canDevirtualizeClassMethod(FullApplySite applySite, ClassDecl *cd,
757762
auto *mi = cast<MethodInst>(applySite.getCallee());
758763

759764
// Find the implementation of the member which should be invoked.
760-
auto *f = getTargetClassMethod(module, cd, classType, mi);
765+
auto *f = getTargetClassMethod(module, applySite, cd, classType, mi);
761766

762767
// If we do not find any such function, we have no function to devirtualize
763768
// to... so bail.
@@ -843,7 +848,7 @@ swift::devirtualizeClassMethod(SILPassManager *pm, FullApplySite applySite,
843848
SILModule &module = applySite.getModule();
844849
auto *mi = cast<MethodInst>(applySite.getCallee());
845850

846-
auto *f = getTargetClassMethod(module, cd, classType, mi);
851+
auto *f = getTargetClassMethod(module, applySite, cd, classType, mi);
847852

848853
CanSILFunctionType genCalleeType = f->getLoweredFunctionTypeInContext(
849854
TypeExpansionContext(*applySite.getFunction()));

test/IRGen/generic_tuples.swift

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@ func dup<T>(_ x: T) -> (T, T) { var x = x; return (x,x) }
2727
// Copy 'x' into the first result.
2828
// CHECK-NEXT: call ptr [[WITNESS]](ptr noalias %0, ptr noalias [[X_ALLOCA]], ptr %T)
2929
// Copy 'x' into the second element.
30+
// CHECK-NEXT: [[WITNESS_ADDR:%.*]] = getelementptr inbounds ptr, ptr [[VWT]], i32 4
31+
// CHECK-NEXT: [[WITNESS:%.*]] = load ptr, ptr [[WITNESS_ADDR]], align 8
3032
// CHECK-NEXT: call ptr [[WITNESS]](ptr noalias %1, ptr noalias [[X_ALLOCA]], ptr %T)
31-
// CHECK-NEXT: [[WITNESS_ADDR:%.*]] = getelementptr inbounds ptr, ptr [[VWT]], i32 1
32-
// CHECK-NEXT: [[DESTROYWITNESS:%.*]] = load ptr, ptr [[WITNESS_ADDR]], align 8
33-
// CHECK-NEXT: call void [[DESTROYWITNESS]](ptr noalias [[X_ALLOCA]],
3433

3534
struct S {}
3635

test/SILOptimizer/allocboxtostack_localapply.swift

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -103,7 +103,7 @@ public func testboxescapes() -> (() -> ()) {
103103
}
104104

105105
// CHECK-LABEL: sil [noinline] @$s26allocboxtostack_localapply9testrecurSiyF :
106-
// CHECK: alloc_stack [var_decl] $Int, var, name "x"
106+
// CHECK: alloc_box ${ var Int }, var, name "x"
107107
// CHECK-LABEL: } // end sil function '$s26allocboxtostack_localapply9testrecurSiyF'
108108
@inline(never)
109109
public func testrecur() -> Int {
@@ -146,9 +146,14 @@ public func testdfs1() -> Int {
146146
return common()
147147
}
148148

149+
// Test to make sure we don't optimize the case when we have an inner common function call for multiple boxes.
150+
// We don't optimize this case now, because we don't have additional logic to correctly construct AppliesToSpecialize
151+
// Order of function calls constructed in PromotedOperands: bar innercommon local1 bas innercommon local2
152+
// AppliesToSpecialize should have the order: bar bas innercommon local1 local2
153+
// Since we don't maintain any tree like data structure with more info on the call tree, this is not possible to construct today
149154
// CHECK-LABEL: sil [noinline] @$s26allocboxtostack_localapply8testdfs2SiyF :
150-
// CHECK: alloc_stack [var_decl] $Int, var, name "x"
151-
// CHECK: alloc_stack [var_decl] $Int, var, name "y"
155+
// CHECK: alloc_box ${ var Int }, var, name "x"
156+
// CHECK: alloc_box ${ var Int }, var, name "y"
152157
// CHECK-LABEL:} // end sil function '$s26allocboxtostack_localapply8testdfs2SiyF'
153158
@inline(never)
154159
public func testdfs2() -> Int {
@@ -177,20 +182,3 @@ public func testdfs2() -> Int {
177182
return local1() + local2()
178183
}
179184

180-
// CHECK-LABEL: sil @$s26allocboxtostack_localapply15call2localfuncsSiyF :
181-
// CHECK-NOT: alloc_box
182-
// CHECK-LABEL:} // end sil function '$s26allocboxtostack_localapply15call2localfuncsSiyF'
183-
public func call2localfuncs() -> Int {
184-
var a1 = 1
185-
186-
@inline(never)
187-
func innerFunction() {
188-
a1 += 1
189-
}
190-
191-
innerFunction()
192-
innerFunction()
193-
194-
return a1
195-
}
196-

test/SILOptimizer/definite_init_protocol_init.swift

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,7 @@ struct AddressOnlyStruct : TriviallyConstructible {
9999
// CHECK-NEXT: apply [[FN]]<AddressOnlyStruct>([[SELF_BOX]], %1, [[METATYPE]])
100100
// CHECK-NEXT: copy_addr [take] [[SELF_BOX]] to [init] [[SELF]]
101101
// CHECK-NEXT: dealloc_stack [[SELF_BOX]]
102-
// CHECK-NEXT: copy_addr [[SELF]] to [init] %0
103-
// CHECK-NEXT: destroy_addr [[SELF]]
102+
// CHECK-NEXT: copy_addr [take] [[SELF]] to [init] %0
104103
// CHECK-NEXT: dealloc_stack [[SELF]]
105104
// CHECK-NEXT: [[RESULT:%.*]] = tuple ()
106105
// CHECK-NEXT: return [[RESULT]]

test/SILOptimizer/span.swift

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
// RUN: %target-swift-frontend %s -parse-as-library -disable-availability-checking -emit-ir -o /dev/null
2+
3+
// Check that the MoveOnlyWrappedTypeEliminator doesn't crash
4+
func consumingArray(_ arr: consuming [Int]) {
5+
let s = arr.mutableSpan;
6+
_ = consume s
7+
}
8+
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// RUN: %target-swift-frontend %s -parse-as-library -enable-experimental-feature Embedded -Xllvm -sil-disable-pass=mandatory-inlining -emit-ir -o /dev/null
2+
3+
// REQUIRES: swift_feature_Embedded
4+
5+
6+
// Check that the compiler doesn't crash
7+
8+
public class Base<T> {
9+
func foo(_ t: T) -> T {
10+
return t
11+
}
12+
}
13+
14+
@_transparent
15+
func callee(_ i: Int, _ c: Base<Int>) -> Int {
16+
return c.foo(i)
17+
}
18+
19+
public func testit(_ i : Int) -> Int {
20+
return callee(i, Base<Int>())
21+
}
22+

0 commit comments

Comments
 (0)