Skip to content

Commit f7466ea

Browse files
committed
SIL: fix bridging of SILDebugVariable
The optional C++ type was bridged to a non-optional Swift type. The correct way is to bridged the non-optional C++ type to the non-optional Swift type.
1 parent 3105733 commit f7466ea

File tree

3 files changed

+57
-27
lines changed

3 files changed

+57
-27
lines changed

SwiftCompilerSources/Sources/SIL/Instruction.swift

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -484,9 +484,9 @@ extension Instruction {
484484
}
485485

486486
public protocol DebugVariableInstruction : VarDeclInstruction {
487-
typealias DebugVariable = OptionalBridgedSILDebugVariable
487+
typealias DebugVariable = BridgedSILDebugVariable
488488

489-
var debugVariable: DebugVariable { get }
489+
var debugVariable: DebugVariable? { get }
490490
}
491491

492492
/// A meta instruction is an instruction whose location is not interesting as
@@ -503,8 +503,8 @@ final public class DebugValueInst : Instruction, UnaryInstruction, DebugVariable
503503
bridged.DebugValue_getDecl().getAs(VarDecl.self)
504504
}
505505

506-
public var debugVariable: DebugVariable {
507-
return bridged.DebugValue_getVarInfo()
506+
public var debugVariable: DebugVariable? {
507+
return bridged.DebugValue_hasVarInfo() ? bridged.DebugValue_getVarInfo() : nil
508508
}
509509
}
510510

@@ -1291,8 +1291,8 @@ final public class AllocStackInst : SingleValueInstruction, Allocation, DebugVar
12911291
bridged.AllocStack_getDecl().getAs(VarDecl.self)
12921292
}
12931293

1294-
public var debugVariable: DebugVariable {
1295-
return bridged.AllocStack_getVarInfo()
1294+
public var debugVariable: DebugVariable? {
1295+
return bridged.AllocStack_hasVarInfo() ? bridged.AllocStack_getVarInfo() : nil
12961296
}
12971297

12981298
public var deallocations: LazyMapSequence<LazyFilterSequence<UseList>, Instruction> {
@@ -1338,8 +1338,8 @@ final public class AllocBoxInst : SingleValueInstruction, Allocation, DebugVaria
13381338
bridged.AllocBox_getDecl().getAs(VarDecl.self)
13391339
}
13401340

1341-
public var debugVariable: DebugVariable {
1342-
return bridged.AllocBox_getVarInfo()
1341+
public var debugVariable: DebugVariable? {
1342+
return bridged.AllocBox_hasVarInfo() ? bridged.AllocBox_getVarInfo() : nil
13431343
}
13441344
}
13451345

include/swift/SIL/SILBridging.h

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ struct BridgedDeclRef;
4343
namespace swift {
4444
class ValueBase;
4545
class Operand;
46+
struct SILDebugVariable;
4647
class ForwardingInstruction;
4748
class SILType;
4849
class SILFunction;
@@ -626,8 +627,14 @@ struct BridgedGenericSpecializationInformation {
626627
const swift::GenericSpecializationInformation * _Nullable data = nullptr;
627628
};
628629

629-
struct OptionalBridgedSILDebugVariable {
630+
struct BridgedSILDebugVariable {
630631
uint64_t storage[16];
632+
633+
BRIDGED_INLINE BridgedSILDebugVariable(const swift::SILDebugVariable &var);
634+
BRIDGED_INLINE BridgedSILDebugVariable(const BridgedSILDebugVariable &rhs);
635+
BRIDGED_INLINE ~BridgedSILDebugVariable();
636+
BRIDGED_INLINE BridgedSILDebugVariable &operator=(const BridgedSILDebugVariable &rhs);
637+
BRIDGED_INLINE swift::SILDebugVariable unbridge() const;
631638
};
632639

633640
struct BridgedInstruction {
@@ -859,11 +866,14 @@ struct BridgedInstruction {
859866
SWIFT_IMPORT_UNSAFE BRIDGED_INLINE OptionalBridgedDeclObj
860867
RefElementAddr_getDecl() const;
861868

862-
BRIDGED_INLINE OptionalBridgedSILDebugVariable DebugValue_getVarInfo() const;
869+
BRIDGED_INLINE bool DebugValue_hasVarInfo() const;
870+
BRIDGED_INLINE BridgedSILDebugVariable DebugValue_getVarInfo() const;
863871

864-
BRIDGED_INLINE OptionalBridgedSILDebugVariable AllocStack_getVarInfo() const;
872+
BRIDGED_INLINE bool AllocStack_hasVarInfo() const;
873+
BRIDGED_INLINE BridgedSILDebugVariable AllocStack_getVarInfo() const;
865874

866-
BRIDGED_INLINE OptionalBridgedSILDebugVariable AllocBox_getVarInfo() const;
875+
BRIDGED_INLINE bool AllocBox_hasVarInfo() const;
876+
BRIDGED_INLINE BridgedSILDebugVariable AllocBox_getVarInfo() const;
867877
};
868878

869879
struct OptionalBridgedInstruction {

include/swift/SIL/SILBridgingImpl.h

Lines changed: 35 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1652,13 +1652,27 @@ SwiftInt BridgedInstruction::TypeValueInst_getValue() const {
16521652
// VarDeclInst and DebugVariableInst
16531653
//===----------------------------------------------------------------------===//
16541654

1655-
static_assert(sizeof(std::optional<swift::SILDebugVariable>) <= sizeof(OptionalBridgedSILDebugVariable));
1655+
static_assert(sizeof(swift::SILDebugVariable) <= sizeof(BridgedSILDebugVariable));
16561656

1657-
static inline
1658-
OptionalBridgedSILDebugVariable bridge(std::optional<swift::SILDebugVariable> &&debugVariable) {
1659-
OptionalBridgedSILDebugVariable bridgedVar;
1660-
*reinterpret_cast<std::optional<swift::SILDebugVariable> *>(&bridgedVar.storage) = debugVariable;
1661-
return bridgedVar;
1657+
BridgedSILDebugVariable::BridgedSILDebugVariable(const swift::SILDebugVariable &var) {
1658+
new (&storage) swift::SILDebugVariable(var);
1659+
}
1660+
1661+
BridgedSILDebugVariable::BridgedSILDebugVariable(const BridgedSILDebugVariable &rhs) {
1662+
new (&storage) swift::SILDebugVariable(rhs.unbridge());
1663+
}
1664+
1665+
BridgedSILDebugVariable::~BridgedSILDebugVariable() {
1666+
reinterpret_cast<swift::SILDebugVariable *>(&storage)->~SILDebugVariable();
1667+
}
1668+
1669+
BridgedSILDebugVariable &BridgedSILDebugVariable::operator=(const BridgedSILDebugVariable &rhs) {
1670+
*reinterpret_cast<swift::SILDebugVariable *>(&storage) = rhs.unbridge();
1671+
return *this;
1672+
}
1673+
1674+
swift::SILDebugVariable BridgedSILDebugVariable::unbridge() const {
1675+
return *reinterpret_cast<const swift::SILDebugVariable *>(&storage);
16621676
}
16631677

16641678
OptionalBridgedDeclObj BridgedInstruction::DebugValue_getDecl() const {
@@ -1681,19 +1695,25 @@ OptionalBridgedDeclObj BridgedInstruction::RefElementAddr_getDecl() const {
16811695
return {getAs<swift::RefElementAddrInst>()->getField()};
16821696
}
16831697

1684-
OptionalBridgedSILDebugVariable
1685-
BridgedInstruction::DebugValue_getVarInfo() const {
1686-
return bridge(getAs<swift::DebugValueInst>()->getVarInfo());
1698+
bool BridgedInstruction::DebugValue_hasVarInfo() const {
1699+
return getAs<swift::DebugValueInst>()->getVarInfo().has_value();
1700+
}
1701+
BridgedSILDebugVariable BridgedInstruction::DebugValue_getVarInfo() const {
1702+
return BridgedSILDebugVariable(getAs<swift::DebugValueInst>()->getVarInfo().value());
16871703
}
16881704

1689-
OptionalBridgedSILDebugVariable
1690-
BridgedInstruction::AllocStack_getVarInfo() const {
1691-
return bridge(getAs<swift::AllocStackInst>()->getVarInfo());
1705+
bool BridgedInstruction::AllocStack_hasVarInfo() const {
1706+
return getAs<swift::AllocStackInst>()->getVarInfo().has_value();
1707+
}
1708+
BridgedSILDebugVariable BridgedInstruction::AllocStack_getVarInfo() const {
1709+
return BridgedSILDebugVariable(getAs<swift::AllocStackInst>()->getVarInfo().value());
16921710
}
16931711

1694-
OptionalBridgedSILDebugVariable
1695-
BridgedInstruction::AllocBox_getVarInfo() const {
1696-
return bridge(getAs<swift::AllocBoxInst>()->getVarInfo());
1712+
bool BridgedInstruction::AllocBox_hasVarInfo() const {
1713+
return getAs<swift::AllocBoxInst>()->getVarInfo().has_value();
1714+
}
1715+
BridgedSILDebugVariable BridgedInstruction::AllocBox_getVarInfo() const {
1716+
return BridgedSILDebugVariable(getAs<swift::AllocBoxInst>()->getVarInfo().value());
16971717
}
16981718

16991719
//===----------------------------------------------------------------------===//

0 commit comments

Comments
 (0)