Skip to content

Commit 598644f

Browse files
committed
Swift Bridging: use C++ instead of C bridging for the bridged witness table classes
1 parent 151f097 commit 598644f

File tree

6 files changed

+86
-98
lines changed

6 files changed

+86
-98
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/ModulePassContext.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ struct ModulePassContext : Context {
5454

5555
mutating func next() -> WitnessTable? {
5656
if let t = currentTable {
57-
currentTable = PassContext_nextWitnessTableInModule(t.bridged).table
57+
currentTable = PassContext_nextWitnessTableInModule(t.bridged).witnessTable
5858
return t
5959
}
6060
return nil
@@ -68,7 +68,7 @@ struct ModulePassContext : Context {
6868

6969
mutating func next() -> DefaultWitnessTable? {
7070
if let t = currentTable {
71-
currentTable = PassContext_nextDefaultWitnessTableInModule(t.bridged).table
71+
currentTable = PassContext_nextDefaultWitnessTableInModule(t.bridged).defaultWitnessTable
7272
return t
7373
}
7474
return nil
@@ -84,11 +84,11 @@ struct ModulePassContext : Context {
8484
}
8585

8686
var witnessTables: WitnessTableList {
87-
WitnessTableList(first: PassContext_firstWitnessTableInModule(_bridged).table)
87+
WitnessTableList(first: PassContext_firstWitnessTableInModule(_bridged).witnessTable)
8888
}
8989

9090
var defaultWitnessTables: DefaultWitnessTableList {
91-
DefaultWitnessTableList(first: PassContext_firstDefaultWitnessTableInModule(_bridged).table)
91+
DefaultWitnessTableList(first: PassContext_firstDefaultWitnessTableInModule(_bridged).defaultWitnessTable)
9292
}
9393

9494
/// Run a closure with a `PassContext` for a function, which allows to modify that function.

SwiftCompilerSources/Sources/SIL/WitnessTable.swift

Lines changed: 20 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -23,38 +23,40 @@ public struct WitnessTable : CustomStringConvertible, NoReflectionChildren {
2323
public typealias Kind = swift.SILWitnessTable.WitnessKind
2424

2525
public var kind: Kind {
26-
return SILWitnessTableEntry_getKind(bridged)
26+
return bridged.getKind()
2727
}
2828

2929
public var methodFunction: Function? {
3030
assert(kind == .Method)
31-
return SILWitnessTableEntry_getMethodFunction(bridged).function
31+
return bridged.getMethodFunction().function
3232
}
3333

3434
public var description: String {
35-
let stdString = SILWitnessTableEntry_debugDescription(bridged)
35+
let stdString = bridged.getDebugDescription()
3636
return String(_cxxString: stdString)
3737
}
3838
}
3939

4040
public struct EntryArray : BridgedRandomAccessCollection {
41-
fileprivate let bridged: BridgedArrayRef
41+
fileprivate let base: BridgedWitnessTableEntry
42+
public let count: Int
4243

4344
public var startIndex: Int { return 0 }
44-
public var endIndex: Int { return Int(bridged.numElements) }
45+
public var endIndex: Int { return count }
4546

4647
public subscript(_ index: Int) -> Entry {
47-
assert(index >= 0 && index < endIndex)
48-
return Entry(bridged: BridgedWitnessTableEntry(ptr: bridged.data! + index &* BridgedWitnessTableEntrySize))
48+
assert(index >= startIndex && index < endIndex)
49+
return Entry(bridged: BridgedWitnessTableEntry(entry: base.entry + index))
4950
}
5051
}
5152

5253
public var entries: EntryArray {
53-
EntryArray(bridged: SILWitnessTable_getEntries(bridged))
54+
let entries = bridged.getEntries()
55+
return EntryArray(base: entries.base, count: entries.count)
5456
}
5557

5658
public var description: String {
57-
let stdString = SILWitnessTable_debugDescription(bridged)
59+
let stdString = bridged.getDebugDescription()
5860
return String(_cxxString: stdString)
5961
}
6062
}
@@ -68,28 +70,29 @@ public struct DefaultWitnessTable : CustomStringConvertible, NoReflectionChildre
6870
public typealias EntryArray = WitnessTable.EntryArray
6971

7072
public var entries: EntryArray {
71-
EntryArray(bridged: SILDefaultWitnessTable_getEntries(bridged))
73+
let entries = bridged.getEntries()
74+
return EntryArray(base: entries.base, count: entries.count)
7275
}
7376

7477
public var description: String {
75-
let stdString = SILDefaultWitnessTable_debugDescription(bridged)
78+
let stdString = bridged.getDebugDescription()
7679
return String(_cxxString: stdString)
7780
}
7881
}
7982

8083
extension OptionalBridgedWitnessTable {
81-
public var table: WitnessTable? {
82-
if let p = ptr {
83-
return WitnessTable(bridged: BridgedWitnessTable(ptr: p))
84+
public var witnessTable: WitnessTable? {
85+
if let table = table {
86+
return WitnessTable(bridged: BridgedWitnessTable(table: table))
8487
}
8588
return nil
8689
}
8790
}
8891

8992
extension OptionalBridgedDefaultWitnessTable {
90-
public var table: DefaultWitnessTable? {
91-
if let p = ptr {
92-
return DefaultWitnessTable(bridged: BridgedDefaultWitnessTable(ptr: p))
93+
public var defaultWitnessTable: DefaultWitnessTable? {
94+
if let table = table {
95+
return DefaultWitnessTable(bridged: BridgedDefaultWitnessTable(table: table))
9396
}
9497
return nil
9598
}

include/swift/SIL/SILBridging.h

Lines changed: 54 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
#include "swift/SIL/SILInstruction.h"
2121
#include "swift/SIL/SILLocation.h"
2222
#include "swift/SIL/SILWitnessTable.h"
23+
#include "swift/SIL/SILDefaultWitnessTable.h"
2324
#include "swift/SIL/SILVTable.h"
2425
#include <stdbool.h>
2526
#include <stddef.h>
@@ -31,10 +32,6 @@ struct BridgedInstruction;
3132
struct OptionalBridgedOperand;
3233
struct OptionalBridgedSuccessor;
3334

34-
enum {
35-
BridgedWitnessTableEntrySize = 5 * sizeof(uintptr_t)
36-
};
37-
3835
enum ChangeNotificationKind {
3936
instructionsChanged,
4037
callsChanged,
@@ -133,26 +130,6 @@ struct BridgedOperandArray {
133130
SwiftInt count;
134131
};
135132

136-
typedef struct {
137-
const void * _Nonnull ptr;
138-
} BridgedWitnessTable;
139-
140-
typedef struct {
141-
const void * _Nullable ptr;
142-
} OptionalBridgedWitnessTable;
143-
144-
typedef struct {
145-
const void * _Nonnull ptr;
146-
} BridgedDefaultWitnessTable;
147-
148-
typedef struct {
149-
const void * _Nullable ptr;
150-
} OptionalBridgedDefaultWitnessTable;
151-
152-
typedef struct {
153-
const void * _Nonnull ptr;
154-
} BridgedWitnessTableEntry;
155-
156133
typedef struct {
157134
SwiftObject obj;
158135
} BridgedFunction;
@@ -257,6 +234,59 @@ struct BridgedVTable {
257234
}
258235
};
259236

237+
struct BridgedWitnessTableEntry {
238+
const swift::SILWitnessTable::Entry * _Nonnull entry;
239+
240+
SWIFT_IMPORT_UNSAFE
241+
std::string getDebugDescription() const;
242+
243+
swift::SILWitnessTable::WitnessKind getKind() const {
244+
return entry->getKind();
245+
}
246+
247+
SWIFT_IMPORT_UNSAFE
248+
OptionalBridgedFunction getMethodFunction() const {
249+
return {entry->getMethodWitness().Witness};
250+
}
251+
};
252+
253+
struct BridgedWitnessTableEntryArray {
254+
BridgedWitnessTableEntry base;
255+
SwiftInt count;
256+
};
257+
258+
struct BridgedWitnessTable {
259+
const swift::SILWitnessTable * _Nonnull table;
260+
261+
std::string getDebugDescription() const;
262+
263+
SWIFT_IMPORT_UNSAFE
264+
BridgedWitnessTableEntryArray getEntries() const {
265+
auto entries = table->getEntries();
266+
return {{entries.data()}, (SwiftInt)entries.size()};
267+
}
268+
};
269+
270+
struct OptionalBridgedWitnessTable {
271+
const swift::SILWitnessTable * _Nullable table;
272+
};
273+
274+
struct BridgedDefaultWitnessTable {
275+
const swift::SILDefaultWitnessTable * _Nonnull table;
276+
277+
std::string getDebugDescription() const;
278+
279+
SWIFT_IMPORT_UNSAFE
280+
BridgedWitnessTableEntryArray getEntries() const {
281+
auto entries = table->getEntries();
282+
return {{entries.data()}, (SwiftInt)entries.size()};
283+
}
284+
};
285+
286+
struct OptionalBridgedDefaultWitnessTable {
287+
const swift::SILDefaultWitnessTable * _Nullable table;
288+
};
289+
260290
typedef struct {
261291
const unsigned char * _Nullable message;
262292
SwiftInt position;
@@ -373,14 +403,6 @@ llvm::StringRef SILGlobalVariable_getName(BridgedGlobalVar global);
373403
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
374404
SwiftInt SILGlobalVariable_isLet(BridgedGlobalVar global);
375405

376-
std::string SILWitnessTable_debugDescription(BridgedWitnessTable table);
377-
BridgedArrayRef SILWitnessTable_getEntries(BridgedWitnessTable table);
378-
std::string SILDefaultWitnessTable_debugDescription(BridgedDefaultWitnessTable table);
379-
BridgedArrayRef SILDefaultWitnessTable_getEntries(BridgedDefaultWitnessTable table);
380-
std::string SILWitnessTableEntry_debugDescription(BridgedWitnessTableEntry entry);
381-
swift::SILWitnessTable::WitnessKind SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry);
382-
OptionalBridgedFunction SILWitnessTableEntry_getMethodFunction(BridgedWitnessTableEntry entry);
383-
384406
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
385407
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
386408
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);

include/swift/SIL/SILBridgingUtils.h

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,20 +67,6 @@ inline SILGlobalVariable *castToGlobal(BridgedGlobalVar global) {
6767
return static_cast<SILGlobalVariable *>(global.obj);
6868
}
6969

70-
inline const SILWitnessTable *castToWitnessTable(BridgedWitnessTable table) {
71-
return static_cast<const SILWitnessTable *>(table.ptr);
72-
}
73-
74-
inline const SILDefaultWitnessTable *
75-
castToDefaultWitnessTable(BridgedDefaultWitnessTable table) {
76-
return static_cast<const SILDefaultWitnessTable *>(table.ptr);
77-
}
78-
79-
inline const SILWitnessTable::Entry *
80-
castToWitnessTableEntry(BridgedWitnessTableEntry entry) {
81-
return static_cast<const SILWitnessTable::Entry *>(entry.ptr);
82-
}
83-
8470
inline ValueOwnershipKind castToOwnership(BridgedValue::Ownership ownership) {
8571
switch (ownership) {
8672
case BridgedValue::Ownership::Unowned: return OwnershipKind::Unowned;

lib/SIL/Utils/SILBridging.cpp

Lines changed: 6 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -733,53 +733,30 @@ std::string BridgedVTableEntry::getDebugDescription() const {
733733
// SILVWitnessTable, SILDefaultWitnessTable
734734
//===----------------------------------------------------------------------===//
735735

736-
static_assert(BridgedWitnessTableEntrySize == sizeof(SILWitnessTable::Entry),
737-
"wrong bridged WitnessTable entry size");
738-
739-
std::string SILWitnessTable_debugDescription(BridgedWitnessTable table) {
736+
std::string BridgedWitnessTableEntry::getDebugDescription() const {
740737
std::string str;
741738
llvm::raw_string_ostream os(str);
742-
castToWitnessTable(table)->print(os);
739+
entry->print(os, /*verbose=*/ false, PrintOptions::printSIL());
743740
str.pop_back(); // Remove trailing newline.
744741
return str;
745742
}
746743

747-
BridgedArrayRef SILWitnessTable_getEntries(BridgedWitnessTable table) {
748-
auto entries = castToWitnessTable(table)->getEntries();
749-
return {(const unsigned char *)entries.data(), entries.size()};
750-
}
751-
752-
std::string SILDefaultWitnessTable_debugDescription(BridgedDefaultWitnessTable table) {
744+
std::string BridgedWitnessTable::getDebugDescription() const {
753745
std::string str;
754746
llvm::raw_string_ostream os(str);
755-
castToDefaultWitnessTable(table)->print(os);
747+
table->print(os);
756748
str.pop_back(); // Remove trailing newline.
757749
return str;
758750
}
759751

760-
BridgedArrayRef SILDefaultWitnessTable_getEntries(BridgedDefaultWitnessTable table) {
761-
auto entries = castToDefaultWitnessTable(table)->getEntries();
762-
return {(const unsigned char *)entries.data(), entries.size()};
763-
}
764-
765-
std::string SILWitnessTableEntry_debugDescription(BridgedWitnessTableEntry entry) {
752+
std::string BridgedDefaultWitnessTable::getDebugDescription() const {
766753
std::string str;
767754
llvm::raw_string_ostream os(str);
768-
castToWitnessTableEntry(entry)->print(os, /*verbose=*/ false,
769-
PrintOptions::printSIL());
755+
table->print(os);
770756
str.pop_back(); // Remove trailing newline.
771757
return str;
772758
}
773759

774-
SILWitnessTable::WitnessKind
775-
SILWitnessTableEntry_getKind(BridgedWitnessTableEntry entry) {
776-
return castToWitnessTableEntry(entry)->getKind();
777-
}
778-
779-
OptionalBridgedFunction SILWitnessTableEntry_getMethodFunction(BridgedWitnessTableEntry entry) {
780-
return {castToWitnessTableEntry(entry)->getMethodWitness().Witness};
781-
}
782-
783760
//===----------------------------------------------------------------------===//
784761
// SILInstruction
785762
//===----------------------------------------------------------------------===//

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ PassContext_firstWitnessTableInModule(BridgedPassContext context) {
16811681

16821682
OptionalBridgedWitnessTable
16831683
PassContext_nextWitnessTableInModule(BridgedWitnessTable table) {
1684-
auto *t = castToWitnessTable(table);
1684+
auto *t = table.table;
16851685
auto nextIter = std::next(t->getIterator());
16861686
if (nextIter == t->getModule().getWitnessTables().end())
16871687
return {nullptr};
@@ -1698,7 +1698,7 @@ PassContext_firstDefaultWitnessTableInModule(BridgedPassContext context) {
16981698

16991699
OptionalBridgedDefaultWitnessTable
17001700
PassContext_nextDefaultWitnessTableInModule(BridgedDefaultWitnessTable table) {
1701-
auto *t = castToDefaultWitnessTable(table);
1701+
auto *t = table.table;
17021702
auto nextIter = std::next(t->getIterator());
17031703
if (nextIter == t->getModule().getDefaultWitnessTables().end())
17041704
return {nullptr};

0 commit comments

Comments
 (0)