Skip to content

Commit 6998ad9

Browse files
authored
Merge pull request #30429 from zoecarver/optimize/remove-dead-in-mand-combine
[opt] remove trivially dead instructions in mandatory combine
2 parents 420c6d1 + 4cc98f0 commit 6998ad9

37 files changed

+123
-50
lines changed

lib/SILOptimizer/Mandatory/MandatoryCombine.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -156,6 +156,7 @@ void MandatoryCombiner::addReachableCodeToWorklist(SILFunction &function) {
156156
++iterator;
157157

158158
if (isInstructionTriviallyDead(instruction)) {
159+
instModCallbacks.deleteInst(instruction);
159160
continue;
160161
}
161162

test/AutoDiff/SIL/differentiability_witness_function_inst.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313

1414
// IRGen test.
1515

16-
// RUN: %target-swift-frontend -emit-ir %s | %FileCheck %s --check-prefix=IRGEN --check-prefix %target-cpu
16+
// RUN: %target-swift-frontend -emit-ir -Xllvm -sil-disable-pass=MandatoryCombine %s | %FileCheck %s --check-prefix=IRGEN --check-prefix %target-cpu
1717
// NOTE: `%target-cpu`-specific FileCheck lines exist because lowered function types in LLVM IR differ between architectures.
1818

1919
// NOTE(SR-12090): `shell` is required only to run `sed` as a SR-12090 workaround.

test/ClangImporter/macro_literals.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -Xllvm -sil-print-debuginfo -emit-sil %s | %FileCheck %s
1+
// RUN: %target-swift-frontend(mock-sdk: %clang-importer-sdk) -Xllvm -sil-print-debuginfo -Xllvm -sil-disable-pass=MandatoryCombine -emit-sil %s | %FileCheck %s
22

33
import macros
44

test/Constraints/keypath_dynamic_member_lookup.swift

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,17 +39,20 @@ var bottomRight = Point(x: 10, y: 10)
3939
var lens = Lens(Rectangle(topLeft: topLeft,
4040
bottomRight: bottomRight))
4141

42+
// A dummy user of variables.
43+
func user<T>(_ _: T) {}
44+
4245
// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig
4346
// CHECK-NEXT: apply %45<Rectangle, Point>({{.*}})
4447
// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs7KeyPathCyxqd__G_tcluig
4548
// CHECK-NEXT: apply %54<Point, Int>({{.*}})
46-
_ = lens.topLeft.x
49+
user(lens.topLeft.x)
4750

48-
// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig
49-
// CHECK-NEXT: apply %69<Rectangle, Point>({{.*}})
50-
// CHECK: function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig
51-
// CHECK-NEXT: apply %76<Point, Int>({{.*}})
52-
_ = lens.topLeft.y
51+
// CHECK: [[LOOKUP_REF1:%.*]] = function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig
52+
// CHECK-NEXT: apply [[LOOKUP_REF1]]<Rectangle, Point>({{.*}})
53+
// CHECK: [[LOOKUP_REF2:%.*]] = function_ref @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs15WritableKeyPathCyxqd__G_tcluig
54+
// CHECK-NEXT: apply [[LOOKUP_REF2]]<Point, Int>({{.*}})
55+
user(lens.topLeft.y)
5356

5457
lens.topLeft = Lens(Point(x: 1, y: 2)) // Ok
5558
lens.bottomRight.y = Lens(12) // Ok
@@ -238,7 +241,7 @@ func test_recursive_dynamic_lookup(_ lens: Lens<Lens<Point>>) {
238241
// CHECK: [[FIRST_OBJ:%.*]] = struct_extract {{.*}} : $Lens<Lens<Point>>, #Lens.obj
239242
// CHECK-NEXT: [[SECOND_OBJ:%.*]] = struct_extract [[FIRST_OBJ]] : $Lens<Point>, #Lens.obj
240243
// CHECK-NEXT: struct_extract [[SECOND_OBJ]] : $Point, #Point.y
241-
_ = lens.obj.obj.y
244+
user(lens.obj.obj.y)
242245
// CHECK: keypath $KeyPath<Point, Int>, (root $Point; stored_property #Point.x : $Int)
243246
// CHECK-NEXT: keypath $KeyPath<Lens<Point>, Lens<Int>>, (root $Lens<Point>; gettable_property $Lens<Int>, id @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs7KeyPathCyxqd__G_tcluig : {{.*}})
244247
// CHECK-NEXT: keypath $KeyPath<Lens<Lens<Point>>, Lens<Lens<Int>>>, (root $Lens<Lens<Point>>; gettable_property $Lens<Lens<Int>>, id @$s29keypath_dynamic_member_lookup4LensV0B6MemberACyqd__Gs7KeyPathCyxqd__G_tcluig : {{.*}})

test/IRGen/access_markers.sil

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ bb0(%0 : $A):
3333
// CHECK-NEXT: getelementptr inbounds %Ts5Int64V, %Ts5Int64V* [[PROPERTY]], i32 0, i32 0
3434
// CHECK-NEXT: load i64, i64*
3535
%4 = load %3 : $*Int64
36+
// Use %4 so it's not optimized away.
37+
// CHECK-NEXT: store
38+
store %4 to undef : $*Int64
3639

3740
// CHECK-NEXT: call void @swift_endAccess([[BUFFER]]* [[SCRATCH1]])
3841
// CHECK-NEXT: [[T0:%.*]] = bitcast [[BUFFER]]* [[SCRATCH1]] to i8*
@@ -48,6 +51,9 @@ bb0(%0 : $A):
4851
// CHECK-NEXT: getelementptr inbounds %Ts5Int64V, %Ts5Int64V* [[PROPERTY]], i32 0, i32 0
4952
// CHECK-NEXT: load i64, i64*
5053
%7 = load %6 : $*Int64
54+
// Use %7 so it's not optimized away.
55+
// CHECK-NEXT: store
56+
store %7 to undef : $*Int64
5157

5258
// CHECK-NEXT: call void @swift_endAccess([[BUFFER]]* [[SCRATCH2]])
5359
// CHECK-NEXT: [[T0:%.*]] = bitcast [[BUFFER]]* [[SCRATCH2]] to i8*

test/IRGen/dynamic_self.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
// RUN: %target-swift-frontend -emit-ir %s | %FileCheck %s
1+
// RUN: %target-swift-frontend -Xllvm -sil-disable-pass=MandatoryCombine -emit-ir %s | %FileCheck %s
22

33
// REQUIRES: CPU=i386 || CPU=x86_64
44

test/IRGen/enum.sil

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -254,6 +254,8 @@ entry(%u : $*Singleton):
254254
switch_enum_addr %u : $*Singleton, case #Singleton.value!enumelt: dest
255255
dest:
256256
%u2 = unchecked_take_enum_data_addr %u : $*Singleton, #Singleton.value!enumelt
257+
// Use u2 so it's not optimized away.
258+
copy_addr %u2 to undef : $*(Builtin.Int64, Builtin.Int64)
257259
%x = tuple ()
258260
return %x : $()
259261
}
@@ -979,6 +981,7 @@ entry(%u : $*SinglePayloadSpareBit):
979981
// CHECK-64: [[DATA_ADDR:%.*]] = bitcast %T4enum21SinglePayloadSpareBitO* %0 to i63*
980982
x_dest:
981983
%u2 = unchecked_take_enum_data_addr %u : $*SinglePayloadSpareBit, #SinglePayloadSpareBit.x!enumelt
984+
copy_addr %u2 to undef : $*Builtin.Int63
982985
%a = function_ref @a : $@convention(thin) () -> ()
983986
apply %a() : $@convention(thin) () -> ()
984987
br end
@@ -1489,14 +1492,20 @@ entry(%u : $*MultiPayloadNoSpareBits):
14891492

14901493
x_dest:
14911494
%x = unchecked_take_enum_data_addr %u : $*MultiPayloadNoSpareBits, #MultiPayloadNoSpareBits.x!enumelt
1495+
// Dummy use of %x so it's not optimized away.
1496+
copy_addr %x to undef : $*Builtin.Int64
14921497
br end
14931498

14941499
y_dest:
14951500
%y = unchecked_take_enum_data_addr %u : $*MultiPayloadNoSpareBits, #MultiPayloadNoSpareBits.y!enumelt
1501+
// Dummy use of %y so it's not optimized away.
1502+
copy_addr %y to undef : $*Builtin.Int32
14961503
br end
14971504

14981505
z_dest:
14991506
%z = unchecked_take_enum_data_addr %u : $*MultiPayloadNoSpareBits, #MultiPayloadNoSpareBits.z!enumelt
1507+
// Dummy use of %z so it's not optimized away.
1508+
copy_addr %z to undef : $*Builtin.Int63
15001509
br end
15011510

15021511
a_dest:
@@ -1742,14 +1751,20 @@ entry(%u : $*MultiPayloadOneSpareBit):
17421751

17431752
x_dest:
17441753
%x = unchecked_take_enum_data_addr %u : $*MultiPayloadOneSpareBit, #MultiPayloadOneSpareBit.x!enumelt
1754+
// Dummy use of %x so it's not optimized away.
1755+
copy_addr %x to undef : $*Builtin.Int62
17451756
br end
17461757

17471758
y_dest:
17481759
%y = unchecked_take_enum_data_addr %u : $*MultiPayloadOneSpareBit, #MultiPayloadOneSpareBit.y!enumelt
1760+
// Dummy use of %y so it's not optimized away.
1761+
copy_addr %y to undef : $*Builtin.Int63
17491762
br end
17501763

17511764
z_dest:
17521765
%z = unchecked_take_enum_data_addr %u : $*MultiPayloadOneSpareBit, #MultiPayloadOneSpareBit.z!enumelt
1766+
// Dummy use of %z so it's not optimized away.
1767+
copy_addr %z to undef : $*Builtin.Int61
17531768
br end
17541769

17551770
a_dest:
@@ -2441,6 +2456,8 @@ entry(%0 : $*DynamicSingleton<T>):
24412456

24422457
dest:
24432458
%1 = unchecked_take_enum_data_addr %0 : $*DynamicSingleton<T>, #DynamicSingleton.value!enumelt
2459+
// Dummy use of %1 so it's not optimized away.
2460+
copy_addr %1 to undef : $*T
24442461
%v = tuple ()
24452462
return %v : $()
24462463
}
@@ -2521,6 +2538,8 @@ entry(%x : $Int32):
25212538
// CHECK-64: [[INT_SHR:%.*]] = lshr i64 [[INT_SHL]], 32
25222539
// CHECK-64: [[INT:%.*]] = trunc i64 [[INT_SHR]] to i32
25232540
%e = unchecked_enum_data %d : $Optional<(Optional<()>, Int32)>, #Optional.some!enumelt
2541+
// Use %e so it's not optimized away.
2542+
store %e to undef : $*(Optional<()>, Int32)
25242543
return %d : $Optional<(Optional<()>, Int32)>
25252544
}
25262545

test/IRGen/enum_dynamic_multi_payload.sil

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -285,8 +285,12 @@ sil @dynamic_project : $@convention(thin) <T> () -> @out EitherOr<T, Builtin.Int
285285
entry(%e : $*EitherOr<T, Builtin.Int64>):
286286
// CHECK: bitcast [[EITHER_OR]]* %0 to %swift.opaque*
287287
%l = unchecked_take_enum_data_addr %e : $*EitherOr<T, Builtin.Int64>, #EitherOr.Left!enumelt
288+
// Dummy use of %l so that it's not optimized away.
289+
copy_addr %l to undef : $*T
288290
// CHECK: bitcast [[EITHER_OR]]* %0 to i64*
289291
%r = unchecked_take_enum_data_addr %e : $*EitherOr<T, Builtin.Int64>, #EitherOr.Right!enumelt
292+
// Dummy use of %r so that it's not optimized away.
293+
copy_addr %r to undef : $*Builtin.Int64
290294

291295
return undef : $()
292296
}

test/IRGen/enum_future.sil

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -258,6 +258,7 @@ entry(%u : $*Singleton):
258258
switch_enum_addr %u : $*Singleton, case #Singleton.value!enumelt: dest
259259
dest:
260260
%u2 = unchecked_take_enum_data_addr %u : $*Singleton, #Singleton.value!enumelt
261+
copy_addr %u2 to undef : $*(Builtin.Int64, Builtin.Int64)
261262
%x = tuple ()
262263
return %x : $()
263264
}
@@ -983,6 +984,7 @@ entry(%u : $*SinglePayloadSpareBit):
983984
// CHECK-64: [[DATA_ADDR:%.*]] = bitcast %T11enum_future21SinglePayloadSpareBitO* %0 to i63*
984985
x_dest:
985986
%u2 = unchecked_take_enum_data_addr %u : $*SinglePayloadSpareBit, #SinglePayloadSpareBit.x!enumelt
987+
copy_addr %u2 to undef : $*Builtin.Int63
986988
%a = function_ref @a : $@convention(thin) () -> ()
987989
apply %a() : $@convention(thin) () -> ()
988990
br end
@@ -1493,14 +1495,20 @@ entry(%u : $*MultiPayloadNoSpareBits):
14931495

14941496
x_dest:
14951497
%x = unchecked_take_enum_data_addr %u : $*MultiPayloadNoSpareBits, #MultiPayloadNoSpareBits.x!enumelt
1498+
// Use %x so it's not optimized away.
1499+
copy_addr %x to undef : $*Builtin.Int64
14961500
br end
14971501

14981502
y_dest:
14991503
%y = unchecked_take_enum_data_addr %u : $*MultiPayloadNoSpareBits, #MultiPayloadNoSpareBits.y!enumelt
1504+
// Use %y so it's not optimized away.
1505+
copy_addr %y to undef : $*Builtin.Int32
15001506
br end
15011507

15021508
z_dest:
15031509
%z = unchecked_take_enum_data_addr %u : $*MultiPayloadNoSpareBits, #MultiPayloadNoSpareBits.z!enumelt
1510+
// Use %z so it's not optimized away.
1511+
copy_addr %z to undef : $*Builtin.Int63
15041512
br end
15051513

15061514
a_dest:
@@ -1746,14 +1754,20 @@ entry(%u : $*MultiPayloadOneSpareBit):
17461754

17471755
x_dest:
17481756
%x = unchecked_take_enum_data_addr %u : $*MultiPayloadOneSpareBit, #MultiPayloadOneSpareBit.x!enumelt
1757+
// Use %x so it's not optimized away.
1758+
copy_addr %x to undef : $*Builtin.Int62
17491759
br end
17501760

17511761
y_dest:
17521762
%y = unchecked_take_enum_data_addr %u : $*MultiPayloadOneSpareBit, #MultiPayloadOneSpareBit.y!enumelt
1763+
// Use %y so it's not optimized away.
1764+
copy_addr %y to undef : $*Builtin.Int63
17531765
br end
17541766

17551767
z_dest:
17561768
%z = unchecked_take_enum_data_addr %u : $*MultiPayloadOneSpareBit, #MultiPayloadOneSpareBit.z!enumelt
1769+
// Use %z so it's not optimized away.
1770+
copy_addr %z to undef : $*Builtin.Int61
17571771
br end
17581772

17591773
a_dest:
@@ -2445,6 +2459,8 @@ entry(%0 : $*DynamicSingleton<T>):
24452459

24462460
dest:
24472461
%1 = unchecked_take_enum_data_addr %0 : $*DynamicSingleton<T>, #DynamicSingleton.value!enumelt
2462+
// Use %1 so it's not optimized away.
2463+
copy_addr %1 to undef : $*T
24482464
%v = tuple ()
24492465
return %v : $()
24502466
}
@@ -2525,6 +2541,8 @@ entry(%x : $Int32):
25252541
// CHECK-64: [[INT_SHR:%.*]] = lshr i64 [[INT_SHL]], 32
25262542
// CHECK-64: [[INT:%.*]] = trunc i64 [[INT_SHR]] to i32
25272543
%e = unchecked_enum_data %d : $Optional<(Optional<()>, Int32)>, #Optional.some!enumelt
2544+
// Use %e so it's not optimized away.
2545+
store %e to undef : $*(Optional<()>, Int32)
25282546
return %d : $Optional<(Optional<()>, Int32)>
25292547
}
25302548

test/IRGen/existentials.sil

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,12 +73,16 @@ entry(%w : $*@sil_weak CP?, %a : $CP?):
7373
// CHECK: [[SRC_WITNESS_ADDR:%.*]] = getelementptr inbounds { %swift.weak, i8** }, { %swift.weak, i8** }* %0, i32 0, i32 1
7474
// CHECK: [[DEST_WITNESS:%.*]] = load i8**, i8*** [[SRC_WITNESS_ADDR]]
7575
%b = load_weak [take] %w : $*@sil_weak CP?
76+
// Use %b so it isn't removed.
77+
store_weak %b to undef : $*@sil_weak CP?
7678

7779
// CHECK: [[SRC_REF_ADDR:%.*]] = getelementptr inbounds { %swift.weak, i8** }, { %swift.weak, i8** }* %0, i32 0, i32 0
7880
// CHECK: [[DEST_REF:%.*]] = call %swift.refcounted* @swift_weakLoadStrong(%swift.weak* [[SRC_REF_ADDR]])
7981
// CHECK: [[SRC_WITNESS_ADDR:%.*]] = getelementptr inbounds { %swift.weak, i8** }, { %swift.weak, i8** }* %0, i32 0, i32 1
8082
// CHECK: [[DEST_WITNESS:%.*]] = load i8**, i8*** [[SRC_WITNESS_ADDR]]
8183
%c = load_weak %w : $*@sil_weak CP?
84+
// Use %c so it isn't removed.
85+
store_weak %c to undef : $*@sil_weak CP?
8286

8387
// CHECK: call { %swift.weak, i8** }* @"$s12existentials2CP_pSgXwWOb"({ %swift.weak, i8** }* %0, { %swift.weak, i8** }* [[V]])
8488
copy_addr [take] %w to [initialization] %v : $*@sil_weak CP?

0 commit comments

Comments
 (0)