Skip to content

Commit 4e530dd

Browse files
committed
[sil-printer] Only sort the PredIds, UserIDs that we put in comments if we are supposed to emit sorted SIL.
This was done some time ago to make it easier to diff large amounts of SIL output. The problem is that it makes it difficult to know the *true* memory order that the predecessor list is in which can lead to surprise when working with SIL and create test cases. I believe some time after that point we added the notion of "sorted" sil, i.e. SIL that does not guarantee any relation to the actual memory representation of the SIL and is meant to ease diffing. This fits nicely with the true intention of this sort of sorting. Thus this commit puts sorting PredIDs, UserIDs behind that flag.
1 parent f9b5e91 commit 4e530dd

File tree

8 files changed

+38
-24
lines changed

8 files changed

+38
-24
lines changed

lib/SIL/SILPrinter.cpp

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -480,12 +480,17 @@ class SILPrinter : public SILVisitor<SILPrinter> {
480480
*this << 's';
481481
*this << ": ";
482482

483-
// Display the user ids sorted to give a stable use order in the printer's
484-
// output. This makes diffing large sections of SIL significantly easier.
485483
llvm::SmallVector<ID, 32> UserIDs;
486484
for (auto *Op : V->getUses())
487485
UserIDs.push_back(getID(Op->getUser()));
488-
std::sort(UserIDs.begin(), UserIDs.end());
486+
487+
// Display the user ids sorted to give a stable use order in the
488+
// printer's output if we are asked to do so. This makes diffing large
489+
// sections of SIL significantly easier at the expense of not showing
490+
// the _TRUE_ order of the users in the use list.
491+
if (Ctx.sortSIL()) {
492+
std::sort(UserIDs.begin(), UserIDs.end());
493+
}
489494

490495
interleave(UserIDs.begin(), UserIDs.end(),
491496
[&] (ID id) { *this << id; },
@@ -512,13 +517,18 @@ class SILPrinter : public SILVisitor<SILPrinter> {
512517

513518
*this << "// Preds:";
514519

515-
// Display the predecessors ids sorted to give a stable use order in the
516-
// printer's output. This makes diffing large sections of SIL
517-
// significantly easier.
518520
llvm::SmallVector<ID, 32> PredIDs;
519521
for (auto *BBI : BB->getPreds())
520522
PredIDs.push_back(getID(BBI));
521-
std::sort(PredIDs.begin(), PredIDs.end());
523+
524+
// Display the pred ids sorted to give a stable use order in the printer's
525+
// output if we are asked to do so. This makes diffing large sections of
526+
// SIL significantly easier at the expense of not showing the _TRUE_ order
527+
// of the users in the use list.
528+
if (Ctx.sortSIL()) {
529+
std::sort(PredIDs.begin(), PredIDs.end());
530+
}
531+
522532
for (auto Id : PredIDs)
523533
*this << ' ' << Id;
524534
}
@@ -551,12 +561,16 @@ class SILPrinter : public SILVisitor<SILPrinter> {
551561
*this << 's';
552562
*this << ": ";
553563

554-
// Display the user ids sorted to give a stable use order in the printer's
555-
// output. This makes diffing large sections of SIL significantly easier.
556564
llvm::SmallVector<ID, 32> UserIDs;
557565
for (auto *Op : V->getUses())
558566
UserIDs.push_back(getID(Op->getUser()));
559-
std::sort(UserIDs.begin(), UserIDs.end());
567+
568+
// If we are asked to, display the user ids sorted to give a stable use
569+
// order in the printer's output. This makes diffing large sections of SIL
570+
// significantly easier.
571+
if (Ctx.sortSIL()) {
572+
std::sort(UserIDs.begin(), UserIDs.end());
573+
}
560574

561575
interleave(UserIDs.begin(), UserIDs.end(), [&](ID id) { *this << id; },
562576
[&] { *this << ", "; });

test/SILGen/if_while_binding.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -329,7 +329,7 @@ func testCaseBool(_ value : Bool?) {
329329
marker_1()
330330
}
331331

332-
// CHECK: bb3: // Preds: bb0 bb1 bb2
332+
// CHECK: bb3: // Preds: bb2 bb1 bb0
333333
// CHECK: switch_enum %0 : $Optional<Bool>, case #Optional.some!enumelt.1: bb4, default bb6
334334

335335
// CHECK: bb4(
@@ -340,7 +340,7 @@ func testCaseBool(_ value : Bool?) {
340340
// CHECK: function_ref @_TF16if_while_binding8marker_2FT_T_
341341
// CHECK: br bb6{{.*}} // id: %15
342342

343-
// CHECK: bb6: // Preds: bb3 bb4 bb5
343+
// CHECK: bb6: // Preds: bb5 bb4 bb3
344344
if case false? = value {
345345
marker_2()
346346
}

test/SILOptimizer/basic-aa.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ bb0(%0 : $X):
563563

564564
// CHECK-LABEL: @alloc_stack_and_addr_cast
565565
// CHECK: PAIR #1.
566-
// CHECK-NEXT: %0 = alloc_stack $C{{.*}} // users: %1, %2
566+
// CHECK-NEXT: %0 = alloc_stack $C{{.*}} // users: %2, %1
567567
// CHECK-NEXT: %1 = unchecked_addr_cast %0 : $*C to $*Optional<C>
568568
// CHECK-NEXT: MayAlias
569569
sil @alloc_stack_and_addr_cast : $@convention(thin) () -> () {

test/SILOptimizer/diagnose_unreachable.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ bb6:
118118
// CHECK-LABEL:sil @InfLoop
119119
// CHECK: bb0:
120120
// CHECK-NEXT: br bb1
121-
// CHECK:bb1: // Preds: bb0 bb3
121+
// CHECK:bb1: // Preds: bb3 bb0
122122
// CHECK-NEXT: br bb2
123123
// CHECK:bb2: // Preds: bb1
124124
// CHECK-NEXT: br bb3

test/SILOptimizer/eager_specialize.sil

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ bb0(%0 : $*T, %1 : $G<T>, %2 : $*T.Elt):
116116
// CHECK: %10 = apply %9<T, T.Elt>(%0, %2, %1) : $@convention(method) <τ_0_0 where τ_0_0 : HasElt> (@in τ_0_0.Elt, G<τ_0_0>) -> @out τ_0_0
117117
// CHECK: br bb2
118118

119-
// CHECK: bb2: // Preds: bb1 bb3
119+
// CHECK: bb2: // Preds: bb3 bb1
120120
// CHECK: %12 = tuple ()
121121
// CHECK: return %12 : $()
122122

@@ -219,15 +219,15 @@ bb2: // Preds: bb0
219219
// CHECK: bb2: // Preds: bb1
220220
// CHECK: br bb3(%{{.*}} : $Error)
221221

222-
// CHECK: bb3(%{{.*}} : $Error): // Preds: bb2 bb7
222+
// CHECK: bb3(%{{.*}} : $Error): // Preds: bb7 bb2
223223
// CHECK: throw %{{.*}} : $Error
224224

225225
// CHECK: bb4: // Preds: bb1
226226
// CHECK: %{{.*}} = witness_method $T, #IntegerArithmetic."/"!1 : $@convention(witness_method) <τ_0_0 where τ_0_0 : IntegerArithmetic> (@in τ_0_0, @in τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0
227227
// CHECK: apply %{{.*}}<T>({{.*}}) : $@convention(witness_method) <τ_0_0 where τ_0_0 : IntegerArithmetic> (@in τ_0_0, @in τ_0_0, @thick τ_0_0.Type) -> @out τ_0_0
228228
// CHECK: br bb5
229229

230-
// CHECK: bb5: // Preds: bb4 bb8
230+
// CHECK: bb5: // Preds: bb8 bb4
231231
// CHECK: %{{.*}} = tuple ()
232232
// CHECK: return %{{.*}} : $()
233233

@@ -307,7 +307,7 @@ bb0(%0 : $*T):
307307
// CHECK: apply %13<T>(%0) : $@convention(thin) <τ_0_0> (@in τ_0_0) -> Int64
308308
// CHECK: br bb3
309309

310-
// CHECK: bb3: // Preds: bb2 bb4 bb5
310+
// CHECK: bb3: // Preds: bb5 bb4 bb2
311311
// CHECK: tuple ()
312312
// CHECK: return
313313

@@ -361,7 +361,7 @@ sil_scope 37 { parent 36 }
361361
// CHECK: apply %13<T>
362362
// CHECK: br bb3(%{{.*}} : $Int64)
363363

364-
// CHECK: bb3(%{{.*}} : $Int64): // Preds: bb2 bb4 bb5
364+
// CHECK: bb3(%{{.*}} : $Int64): // Preds: bb5 bb4 bb2
365365
// CHECK: return %{{.*}} : $Int64
366366

367367
// CHECK: bb4: // Preds: bb1

test/SILOptimizer/lslocation_expansion.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -287,7 +287,7 @@ bb0:
287287

288288
// CHECK-LABEL: self_loop
289289
// CHECK: #0 store
290-
// CHECK-NEXT: alloc_stack $S5, var, name "b"{{.*}} // users: %4, %7
290+
// CHECK-NEXT: alloc_stack $S5, var, name "b"{{.*}} // users: %7, %4
291291
// CHECK-NEXT: Address Projection Type: $*SelfLoop
292292
// CHECK-NEXT: Field Type: var a: SelfLoop
293293
sil hidden @self_loop : $@convention(thin) () -> () {

test/SILOptimizer/lslocation_reduction.sil

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ bb0:
157157

158158
// CHECK-LABEL: self_loop
159159
// CHECK: #0 store
160-
// CHECK-NEXT: alloc_stack $S5, var, name "b"{{.*}} // users: %4, %7
160+
// CHECK-NEXT: alloc_stack $S5, var, name "b"{{.*}} // users: %7, %4
161161
sil hidden @self_loop : $@convention(thin) () -> () {
162162
bb0:
163163
%0 = alloc_stack $S5, var, name "b" // users: %4, %7

test/SILOptimizer/mem-behavior.sil

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,7 @@ bb0(%0 : $Int32):
130130
// CHECK-LABEL: @allocstack_apply_no_escaping
131131
// CHECK: PAIR #1.
132132
// CHECK-NEXT: %3 = apply %2() : $@convention(thin) () -> ()
133-
// CHECK-NEXT: %1 = alloc_stack $Int32{{.*}} // users: %5, %7
133+
// CHECK-NEXT: %1 = alloc_stack $Int32{{.*}} // users: %7, %5
134134
// CHECK-NEXT: r=0,w=0,se=0
135135
sil @allocstack_apply_no_escaping : $@convention(thin) (Int32) -> () {
136136
bb0(%0 : $Int32):
@@ -147,7 +147,7 @@ bb0(%0 : $Int32):
147147
// CHECK-LABEL: @allocstack_apply_read_only
148148
// CHECK: PAIR #1.
149149
// CHECK-NEXT: %4 = apply %3(%1) : $@convention(thin) (@in X) -> ()
150-
// CHECK-NEXT: %1 = alloc_stack $X{{.*}} // users: %2, %4, %5
150+
// CHECK-NEXT: %1 = alloc_stack $X{{.*}} // users: %5, %4, %2
151151
// CHECK-NEXT: r=1,w=0,se=0
152152
sil @allocstack_apply_read_only : $@convention(thin) (X) -> () {
153153
bb0(%0 : $X):
@@ -175,7 +175,7 @@ struct TwoInts {
175175
// CHECK-LABEL: @combination_of_read_and_write_effects
176176
// CHECK: PAIR #0.
177177
// CHECK-NEXT: %4 = apply %3(%1, %2) : $@convention(thin) (@inout Int32, @inout Int32) -> ()
178-
// CHECK-NEXT: %0 = alloc_stack $TwoInts{{.*}} // users: %1, %2, %5
178+
// CHECK-NEXT: %0 = alloc_stack $TwoInts{{.*}} // users: %5, %2, %1
179179
// CHECK-NEXT: r=1,w=1,se=1
180180
sil @combination_of_read_and_write_effects : $@convention(thin) () -> () {
181181
bb0:

0 commit comments

Comments
 (0)