Skip to content

Commit 9d8e5df

Browse files
committed
[Serialization] Preserve the order of basic blocks in SIL deserialization.
This shouldn't affect anything in practice but it's best to be deterministic. (Although I'm not sure why the previous mode was nondeterministic.) Swift SVN r28580
1 parent 21bc219 commit 9d8e5df

File tree

3 files changed

+13
-8
lines changed

3 files changed

+13
-8
lines changed

lib/Serialization/DeserializeSIL.cpp

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -251,18 +251,21 @@ SILValue SILDeserializer::getLocalValue(ValueID Id, unsigned ResultNum,
251251

252252
/// Return the SILBasicBlock of a given ID.
253253
SILBasicBlock *SILDeserializer::getBBForDefinition(SILFunction *Fn,
254+
SILBasicBlock *Prev,
254255
unsigned ID) {
255256
SILBasicBlock *&BB = BlocksByID[ID];
256257
// If the block has never been named yet, just create it.
257258
if (BB == nullptr)
258-
return BB = new (SILMod) SILBasicBlock(Fn);
259+
return BB = new (SILMod) SILBasicBlock(Fn, Prev);
259260

260261
// If it already exists, it was either a forward reference or a redefinition.
261262
// The latter should never happen.
262263
bool wasForwardReferenced = UndefinedBlocks.erase(BB);
263264
assert(wasForwardReferenced);
264265
(void)wasForwardReferenced;
265266

267+
if (Prev)
268+
BB->moveAfter(Prev);
266269
return BB;
267270
}
268271

@@ -531,7 +534,7 @@ SILFunction *SILDeserializer::readSILFunction(DeclID FID,
531534
kind != SIL_WITNESSTABLE) {
532535
if (kind == SIL_BASIC_BLOCK)
533536
// Handle a SILBasicBlock record.
534-
CurrentBB = readSILBasicBlock(fn, scratch);
537+
CurrentBB = readSILBasicBlock(fn, CurrentBB, scratch);
535538
else {
536539
// If CurrentBB is empty, just return fn. The code in readSILInstruction
537540
// assumes that such a situation means that fn is a declaration. Thus it
@@ -570,13 +573,14 @@ SILFunction *SILDeserializer::readSILFunction(DeclID FID,
570573
}
571574

572575
SILBasicBlock *SILDeserializer::readSILBasicBlock(SILFunction *Fn,
576+
SILBasicBlock *Prev,
573577
SmallVectorImpl<uint64_t> &scratch) {
574578
ArrayRef<uint64_t> Args;
575579
SILBasicBlockLayout::readRecord(scratch, Args);
576580

577581
// Args should be a list of pairs, the first number is a TypeID, the
578582
// second number is a ValueID.
579-
SILBasicBlock *CurrentBB = getBBForDefinition(Fn, BasicBlockID++);
583+
SILBasicBlock *CurrentBB = getBBForDefinition(Fn, Prev, BasicBlockID++);
580584
for (unsigned I = 0, E = Args.size(); I < E; I += 3) {
581585
TypeID TyID = Args[I];
582586
if (!TyID) return nullptr;

lib/Serialization/DeserializeSIL.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,14 +65,16 @@ namespace swift {
6565

6666
/// Return the SILBasicBlock of a given ID.
6767
SILBasicBlock *getBBForReference(SILFunction *Fn, unsigned ID);
68-
SILBasicBlock *getBBForDefinition(SILFunction *Fn, unsigned ID);
68+
SILBasicBlock *getBBForDefinition(SILFunction *Fn, SILBasicBlock *Prev,
69+
unsigned ID);
6970

7071
/// Read a SIL function.
7172
SILFunction *readSILFunction(serialization::DeclID, SILFunction *InFunc,
7273
StringRef Name, bool declarationOnly,
7374
bool errorIfEmptyBody = true);
7475
/// Read a SIL basic block within a given SIL function.
7576
SILBasicBlock *readSILBasicBlock(SILFunction *Fn,
77+
SILBasicBlock *Prev,
7678
SmallVectorImpl<uint64_t> &scratch);
7779
/// Read a SIL instruction within a given SIL basic block.
7880
bool readSILInstruction(SILFunction *Fn, SILBasicBlock *BB,

test/Serialization/Inputs/def_basic.sil

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,11 +76,10 @@ bb1:
7676
// CHECK-LABEL: sil public_external [fragile] @test2 : $@convention(thin) (Int) -> ()
7777
sil [fragile] @test2 : $@convention(thin) (Int) -> () {
7878
// CHECK: bb1:
79-
// CHECK: %2 = tuple ()
80-
// CHECK: br bb2
79+
// CHECK: return %5 : $()
8180
// CHECK: bb2:
82-
// FIXME: should be %2
83-
// CHECK: return %{{.*}} : $()
81+
// CHECK: %5 = tuple ()
82+
// CHECK: br bb1
8483
bb0(%0 : $Int):
8584
br bb2
8685
bb1:

0 commit comments

Comments
 (0)