Skip to content

Commit b9c719b

Browse files
authored
Merge pull request #68819 from kubamracek/embedded-osize
[embedded] Fix a compiler crash when using generic classes in -O/-Osize
2 parents db6872a + b6c4afa commit b9c719b

File tree

4 files changed

+52
-9
lines changed

4 files changed

+52
-9
lines changed

lib/SILOptimizer/PassManager/PassPipeline.cpp

Lines changed: 18 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -469,10 +469,16 @@ void addFunctionPasses(SILPassPipelinePlan &P,
469469
// makes a change we'll end up restarting the function passes on the
470470
// current function (after optimizing any new callees).
471471
P.addDevirtualizer();
472-
P.addGenericSpecializer();
473-
// Run devirtualizer after the specializer, because many
474-
// class_method/witness_method instructions may use concrete types now.
475-
P.addDevirtualizer();
472+
// MandatoryPerformanceOptimizations already took care of all specializations
473+
// in embedded Swift mode, running the generic specializer might introduce
474+
// more generic calls from non-generic functions, which breaks the assumptions
475+
// of embedded Swift.
476+
if (!P.getOptions().EmbeddedSwift) {
477+
P.addGenericSpecializer();
478+
// Run devirtualizer after the specializer, because many
479+
// class_method/witness_method instructions may use concrete types now.
480+
P.addDevirtualizer();
481+
}
476482
P.addARCSequenceOpts();
477483

478484
if (P.getOptions().EnableOSSAModules) {
@@ -754,8 +760,14 @@ static void addMidLevelFunctionPipeline(SILPassPipelinePlan &P) {
754760
static void addLowLevelPassPipeline(SILPassPipelinePlan &P) {
755761
P.startPipeline("LowLevel,Function", true /*isFunctionPassPipeline*/);
756762

757-
// Should be after FunctionSignatureOpts and before the last inliner.
758-
P.addReleaseDevirtualizer();
763+
// MandatoryPerformanceOptimizations already took care of all specializations
764+
// in embedded Swift mode, running the release devirtualizer might introduce
765+
// more generic calls from non-generic functions, which breaks the assumptions
766+
// of embedded Swift.
767+
if (!P.getOptions().EmbeddedSwift) {
768+
// Should be after FunctionSignatureOpts and before the last inliner.
769+
P.addReleaseDevirtualizer();
770+
}
759771

760772
addFunctionPasses(P, OptimizationLevelKind::LowLevel);
761773

test/embedded/classes-stack-promotion.swift

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -86,9 +86,7 @@ struct Main {
8686
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
8787
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
8888
// CHECK-IR-NEXT: call {{.*}}@"$s4main3bar1oyAA7MyClassC_tF"
89-
// CHECK-IR-NEXT: call {{.*}}@swift_setDeallocating
90-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
91-
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
89+
// CHECK-IR-NEXT: call {{.*}}@swift_release
9290
// CHECK-IR-NEXT: call {{.*}}@llvm.lifetime.end.p0
9391
// CHECK-IR-NEXT: call {{.*}}@"$s4main5print_10terminatorys12StaticStringV_AEtF"
9492
// CHECK-IR-NEXT: call {{.*}}@swift_initStackObject
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
// RUN: %target-swift-emit-ir -Osize %s -enable-experimental-feature Embedded | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
// REQUIRES: VENDOR=apple
5+
// REQUIRES: OS=macosx
6+
7+
public func foo<T>(n: T) {
8+
bar(n: 42)
9+
}
10+
11+
private func bar<T>(n: T) {
12+
baz(n: 42)
13+
}
14+
15+
public func baz<T>(n: T) {
16+
let x: ContiguousArray<Int> = .init(repeating: 0, count: 1)
17+
}
18+
19+
// CHECK: define {{.*}}@main(
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// RUN: %target-swift-emit-ir -Osize %s -enable-experimental-feature Embedded | %FileCheck %s
2+
3+
// REQUIRES: swift_in_compiler
4+
// REQUIRES: VENDOR=apple
5+
// REQUIRES: OS=macosx
6+
7+
public func foo() {
8+
bar([42])
9+
}
10+
11+
func bar(_: UnsafePointer<UInt?>) {
12+
}
13+
14+
// CHECK: define {{.*}}@main(

0 commit comments

Comments
 (0)