Skip to content

Commit 61e762d

Browse files
authored
Merge pull request #58709 from eeckstein/fix-sil-serialization
SIL: fix serialization of the `unchecked_ref_cast` instruction
2 parents 580ed09 + 26b0309 commit 61e762d

File tree

5 files changed

+36
-3
lines changed

5 files changed

+36
-3
lines changed

lib/Serialization/DeserializeSIL.cpp

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1346,7 +1346,6 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
13461346
ONEOPERAND_ONETYPE_INST(RefTo##Name) \
13471347
ONEOPERAND_ONETYPE_INST(Name##ToRef)
13481348
#include "swift/AST/ReferenceStorage.def"
1349-
ONEOPERAND_ONETYPE_INST(UncheckedRefCast)
13501349
ONEOPERAND_ONETYPE_INST(UncheckedAddrCast)
13511350
ONEOPERAND_ONETYPE_INST(UncheckedTrivialBitCast)
13521351
ONEOPERAND_ONETYPE_INST(UncheckedBitwiseCast)
@@ -2670,6 +2669,17 @@ bool SILDeserializer::readSILInstruction(SILFunction *Fn,
26702669
failureBB);
26712670
break;
26722671
}
2672+
case SILInstructionKind::UncheckedRefCastInst: {
2673+
assert(RecordKind == SIL_ONE_TYPE_ONE_OPERAND &&
2674+
"Layout should be OneTypeOneOperand.");
2675+
auto *urc = Builder.createUncheckedRefCast(Loc,
2676+
getLocalValue(ValID, getSILType(MF->getType(TyID2),
2677+
(SILValueCategory)TyCategory2, Fn)),
2678+
getSILType(MF->getType(TyID), (SILValueCategory)TyCategory, Fn));
2679+
urc->setForwardingOwnershipKind(decodeValueOwnership(Attr));
2680+
ResultInst = urc;
2681+
break;
2682+
}
26732683
case SILInstructionKind::UncheckedRefCastAddrInst: {
26742684
CanType sourceType = MF->getType(ListOfValues[0])->getCanonicalType();
26752685
// ignore attr.

lib/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ const uint16_t SWIFTMODULE_VERSION_MAJOR = 0;
5656
/// describe what change you made. The content of this comment isn't important;
5757
/// it just ensures a conflict if two people change the module format.
5858
/// Don't worry about adhering to the 80-column limit for this line.
59-
const uint16_t SWIFTMODULE_VERSION_MINOR = 688; // SILBoxType extensions
59+
const uint16_t SWIFTMODULE_VERSION_MINOR = 689; // cast ownership serialization
6060

6161
/// A standard hash seed used for all string hashes in a serialized module.
6262
///

lib/Serialization/SILFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,7 @@ namespace sil_block {
331331
using SILOneTypeOneOperandLayout = BCRecordLayout<
332332
SIL_ONE_TYPE_ONE_OPERAND,
333333
SILInstOpCodeField,
334-
BCFixed<1>, // Optional attribute
334+
BCFixed<2>, // Optional attribute
335335
TypeIDField,
336336
SILTypeCategoryField,
337337
TypeIDField,

lib/Serialization/SerializeSIL.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1756,6 +1756,8 @@ void SILSerializer::writeSILInstruction(const SILInstruction &SI) {
17561756
if (SI.getKind() == SILInstructionKind::ConvertFunctionInst) {
17571757
if (cast<ConvertFunctionInst>(SI).withoutActuallyEscaping())
17581758
attrs |= 0x01;
1759+
} else if (auto *refCast = dyn_cast<UncheckedRefCastInst>(&SI)) {
1760+
attrs = encodeValueOwnership(refCast->getOwnershipKind());
17591761
}
17601762
writeConversionLikeInstruction(cast<SingleValueInstruction>(&SI), attrs);
17611763
break;

test/Serialization/ossa_sil.sil

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
// RUN: %empty-directory(%t)
2+
// RUN: %target-build-swift -emit-module -module-name ossa -o %t/ossa.swiftmodule %s
3+
// RUN: %target-sil-opt %t/ossa.swiftmodule | %FileCheck %s
4+
5+
sil_stage canonical
6+
7+
import Builtin
8+
import Swift
9+
import SwiftShims
10+
11+
class X {}
12+
13+
// CHECK-LABEL: sil [serialized] [canonical] [ossa] @test_unchecked_ref_cast
14+
sil [serialized] [ossa] @test_unchecked_ref_cast : $@convention(thin) (@guaranteed AnyObject) -> @owned X {
15+
bb0(%0 : @guaranteed $AnyObject):
16+
// CHECK: %1 = unchecked_ref_cast %0 : $AnyObject to $X, forwarding: @unowned
17+
%1 = unchecked_ref_cast %0 : $AnyObject to $X, forwarding: @unowned
18+
%2 = copy_value %1 : $X
19+
return %2 : $X
20+
}
21+

0 commit comments

Comments
 (0)