Skip to content

Commit 151f097

Browse files
committed
Swift Bridging: use C++ instead of C bridging for BridgedVTable and BridgedVTableEntry
1 parent fc2ad09 commit 151f097

File tree

7 files changed

+49
-52
lines changed

7 files changed

+49
-52
lines changed

SwiftCompilerSources/Sources/Optimizer/PassManager/ModulePassContext.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -37,12 +37,12 @@ struct ModulePassContext : Context {
3737

3838
struct VTableArray : BridgedRandomAccessCollection {
3939
fileprivate let bridged: BridgedVTableArray
40-
40+
4141
var startIndex: Int { return 0 }
42-
var endIndex: Int { return Int(bridged.count) }
43-
42+
var endIndex: Int { return bridged.count }
43+
4444
subscript(_ index: Int) -> VTable {
45-
assert(index >= 0 && index < bridged.count)
45+
assert(index >= startIndex && index < endIndex)
4646
return VTable(bridged: bridged.vTables![index])
4747
}
4848
}

SwiftCompilerSources/Sources/SIL/VTable.swift

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,32 +20,34 @@ public struct VTable : CustomStringConvertible, NoReflectionChildren {
2020
public struct Entry : CustomStringConvertible, NoReflectionChildren {
2121
fileprivate let bridged: BridgedVTableEntry
2222

23-
public var function: Function { SILVTableEntry_getFunction(bridged).function }
23+
public var function: Function { bridged.getImplementation().function }
2424

2525
public var description: String {
26-
let stdString = SILVTableEntry_debugDescription(bridged)
26+
let stdString = bridged.getDebugDescription()
2727
return String(_cxxString: stdString)
2828
}
2929
}
3030

3131
public struct EntryArray : BridgedRandomAccessCollection {
32-
fileprivate let bridgedArray: BridgedArrayRef
32+
fileprivate let base: BridgedVTableEntry
33+
public let count: Int
3334

3435
public var startIndex: Int { return 0 }
35-
public var endIndex: Int { return Int(bridgedArray.numElements) }
36+
public var endIndex: Int { return count }
3637

3738
public subscript(_ index: Int) -> Entry {
38-
assert(index >= 0 && index < endIndex)
39-
return Entry(bridged: BridgedVTableEntry(ptr: bridgedArray.data! + index &* BridgedVTableEntrySize))
39+
assert(index >= startIndex && index < endIndex)
40+
return Entry(bridged: BridgedVTableEntry(entry: base.entry + index))
4041
}
4142
}
4243

4344
public var entries: EntryArray {
44-
EntryArray(bridgedArray: SILVTable_getEntries(bridged))
45+
let entries = bridged.getEntries()
46+
return EntryArray(base: entries.base, count: entries.count)
4547
}
4648

4749
public var description: String {
48-
let stdString = SILVTable_debugDescription(bridged)
50+
let stdString = bridged.getDebugDescription()
4951
return String(_cxxString: stdString)
5052
}
5153
}

include/swift/SIL/SILBridging.h

Lines changed: 29 additions & 14 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/SILVTable.h"
2324
#include <stdbool.h>
2425
#include <stddef.h>
2526
#include <string>
@@ -31,7 +32,6 @@ struct OptionalBridgedOperand;
3132
struct OptionalBridgedSuccessor;
3233

3334
enum {
34-
BridgedVTableEntrySize = 5 * sizeof(uintptr_t),
3535
BridgedWitnessTableEntrySize = 5 * sizeof(uintptr_t)
3636
};
3737

@@ -133,14 +133,6 @@ struct BridgedOperandArray {
133133
SwiftInt count;
134134
};
135135

136-
typedef struct {
137-
const void * _Nonnull ptr;
138-
} BridgedVTable;
139-
140-
typedef struct {
141-
const void * _Nonnull ptr;
142-
} BridgedVTableEntry;
143-
144136
typedef struct {
145137
const void * _Nonnull ptr;
146138
} BridgedWitnessTable;
@@ -237,6 +229,34 @@ typedef struct {
237229
SwiftObject obj;
238230
} BridgedMultiValueResult;
239231

232+
struct BridgedVTableEntry {
233+
const swift::SILVTableEntry * _Nonnull entry;
234+
235+
std::string getDebugDescription() const;
236+
237+
SWIFT_IMPORT_UNSAFE
238+
BridgedFunction getImplementation() const {
239+
return {entry->getImplementation()};
240+
}
241+
};
242+
243+
struct BridgedVTableEntryArray {
244+
BridgedVTableEntry base;
245+
SwiftInt count;
246+
};
247+
248+
struct BridgedVTable {
249+
const swift::SILVTable * _Nonnull vTable;
250+
251+
std::string getDebugDescription() const;
252+
253+
SWIFT_IMPORT_UNSAFE
254+
BridgedVTableEntryArray getEntries() const {
255+
auto entries = vTable->getEntries();
256+
return {{entries.data()}, (SwiftInt)entries.size()};
257+
}
258+
};
259+
240260
typedef struct {
241261
const unsigned char * _Nullable message;
242262
SwiftInt position;
@@ -353,11 +373,6 @@ llvm::StringRef SILGlobalVariable_getName(BridgedGlobalVar global);
353373
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
354374
SwiftInt SILGlobalVariable_isLet(BridgedGlobalVar global);
355375

356-
std::string SILVTable_debugDescription(BridgedVTable vTable);
357-
BridgedArrayRef SILVTable_getEntries(BridgedVTable vTable);
358-
std::string SILVTableEntry_debugDescription(BridgedVTableEntry entry);
359-
BridgedFunction SILVTableEntry_getFunction(BridgedVTableEntry entry);
360-
361376
std::string SILWitnessTable_debugDescription(BridgedWitnessTable table);
362377
BridgedArrayRef SILWitnessTable_getEntries(BridgedWitnessTable table);
363378
std::string SILDefaultWitnessTable_debugDescription(BridgedDefaultWitnessTable table);

include/swift/SIL/SILBridgingUtils.h

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

70-
inline const SILVTable *castToVTable(BridgedVTable vTable) {
71-
return static_cast<const SILVTable *>(vTable.ptr);
72-
}
73-
74-
inline const SILVTableEntry *castToVTableEntry(BridgedVTableEntry entry) {
75-
return static_cast<const SILVTableEntry *>(entry.ptr);
76-
}
77-
7870
inline const SILWitnessTable *castToWitnessTable(BridgedWitnessTable table) {
7971
return static_cast<const SILWitnessTable *>(table.ptr);
8072
}

include/swift/SILOptimizer/OptimizerBridging.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ typedef struct {
2929

3030
typedef struct {
3131
const BridgedVTable * _Nullable vTables;
32-
size_t count;
32+
SwiftInt count;
3333
} BridgedVTableArray;
3434

3535
typedef struct {

lib/SIL/Utils/SILBridging.cpp

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -713,34 +713,22 @@ SwiftInt SILGlobalVariable_isLet(BridgedGlobalVar global) {
713713
// SILVTable
714714
//===----------------------------------------------------------------------===//
715715

716-
static_assert(BridgedVTableEntrySize == sizeof(SILVTableEntry),
717-
"wrong bridged VTableEntry size");
718-
719-
std::string SILVTable_debugDescription(BridgedVTable vTable) {
716+
std::string BridgedVTable::getDebugDescription() const {
720717
std::string str;
721718
llvm::raw_string_ostream os(str);
722-
castToVTable(vTable)->print(os);
719+
vTable->print(os);
723720
str.pop_back(); // Remove trailing newline.
724721
return str;
725722
}
726723

727-
BridgedArrayRef SILVTable_getEntries(BridgedVTable vTable) {
728-
auto entries = castToVTable(vTable)->getEntries();
729-
return {(const unsigned char *)entries.data(), entries.size()};
730-
}
731-
732-
std::string SILVTableEntry_debugDescription(BridgedVTableEntry entry) {
724+
std::string BridgedVTableEntry::getDebugDescription() const {
733725
std::string str;
734726
llvm::raw_string_ostream os(str);
735-
castToVTableEntry(entry)->print(os);
727+
entry->print(os);
736728
str.pop_back(); // Remove trailing newline.
737729
return str;
738730
}
739731

740-
BridgedFunction SILVTableEntry_getFunction(BridgedVTableEntry entry) {
741-
return {castToVTableEntry(entry)->getImplementation()};
742-
}
743-
744732
//===----------------------------------------------------------------------===//
745733
// SILVWitnessTable, SILDefaultWitnessTable
746734
//===----------------------------------------------------------------------===//

lib/SILOptimizer/PassManager/PassManager.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1668,7 +1668,7 @@ PassContext_nextFunctionInModule(BridgedFunction function) {
16681668
BridgedVTableArray PassContext_getVTables(BridgedPassContext context) {
16691669
SILModule *mod = castToPassInvocation(context)->getPassManager()->getModule();
16701670
auto vTables = mod->getVTables();
1671-
return {(const BridgedVTable *)vTables.data(), vTables.size()};
1671+
return {(const BridgedVTable *)vTables.data(), (SwiftInt)vTables.size()};
16721672
}
16731673

16741674
OptionalBridgedWitnessTable

0 commit comments

Comments
 (0)