Skip to content

Commit 76ce27d

Browse files
committed
[libswift] Refactor to use extensions instead of bridging objects.
1 parent 191800f commit 76ce27d

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

43 files changed

+935
-1233
lines changed

cmake/modules/AddSwift.cmake

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -716,6 +716,7 @@ function(add_libswift name)
716716
set(libswift_compile_options
717717
"-Xfrontend" "-validate-tbd-against-ir=none"
718718
"-Xfrontend" "-enable-cxx-interop"
719+
"-Xfrontend" "-disable-llvm-verify"
719720
"-Xcc" "-UIBOutlet" "-Xcc" "-UIBAction" "-Xcc" "-UIBInspectable")
720721
721722
if(CMAKE_BUILD_TYPE STREQUAL Debug)
@@ -778,7 +779,18 @@ function(add_libswift name)
778779
"-emit-module-path" "${build_dir}/${module}.swiftmodule"
779780
"-parse-as-library" ${sources}
780781
"-wmo" ${libswift_compile_options}
781-
"-I" "${SWIFT_SOURCE_DIR}/include/swift"
782+
"-I" "${CMAKE_SOURCE_DIR}/include/swift"
783+
# The Swift source header includes:
784+
"-I" "${CMAKE_SOURCE_DIR}/include"
785+
# LLVM and Clang source header includes:
786+
"-I" "${LLVM_MAIN_SRC_DIR}/../clang/include"
787+
"-I" "${LLVM_MAIN_INCLUDE_DIR}"
788+
# Swift build includes:
789+
"-I" "${CMAKE_BINARY_DIR}/include"
790+
# LLVM and Clang build includes:
791+
"-I" "${LLVM_BINARY_DIR}/tools/clang/include"
792+
"-I" "${LLVM_BINARY_DIR}/include"
793+
# libSwift build includes:
782794
"-I" "${build_dir}"
783795
COMMENT "Building libswift module ${module}")
784796

include/swift/SIL/BridgedSwiftObject.h

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,10 +27,9 @@
2727
// Provide macros to temporarily suppress warning about the use of
2828
// _Nullable and _Nonnull.
2929
# define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
30-
_Pragma("clang diagnostic push") \
31-
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"")
30+
_Pragma("clang assume_nonnull begin")
3231
# define SWIFT_END_NULLABILITY_ANNOTATIONS \
33-
_Pragma("clang diagnostic pop")
32+
_Pragma("clang assume_nonnull end")
3433

3534
#else
3635
// #define _Nullable and _Nonnull to nothing if we're not being built

include/swift/SIL/SILArgument.h

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,8 @@ struct SILArgumentKind {
6262
}
6363
};
6464

65-
class SILArgument : public ValueBase {
65+
class __attribute__((swift_attr("import_as_ref"))) SILArgument
66+
: public ValueBase {
6667
friend class SILBasicBlock;
6768

6869
SILBasicBlock *parentBlock;
@@ -202,7 +203,8 @@ inline SILArgument *castToArgument(SwiftObject argument) {
202203
return static_cast<SILArgument *>(argument);
203204
}
204205

205-
class SILPhiArgument : public SILArgument {
206+
class __attribute__((swift_attr("import_as_ref"))) SILPhiArgument
207+
: public SILArgument {
206208
friend class SILBasicBlock;
207209

208210
SILPhiArgument(SILBasicBlock *parentBlock, SILType type,
@@ -310,7 +312,8 @@ class SILPhiArgument : public SILArgument {
310312
}
311313
};
312314

313-
class SILFunctionArgument : public SILArgument {
315+
class __attribute__((swift_attr("import_as_ref"))) SILFunctionArgument
316+
: public SILArgument {
314317
friend class SILBasicBlock;
315318

316319
bool noImplicitCopy = false;

include/swift/SIL/SILBasicBlock.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ class SILFunction;
3131
class SILArgument;
3232
class SILPrintContext;
3333

34-
class SILBasicBlock :
34+
class __attribute__((swift_attr("import_as_ref"))) SILBasicBlock :
3535
public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock>,
3636
public SwiftObjectHeader {
3737
friend class SILSuccessor;

include/swift/SIL/SILBridging.h

Lines changed: 149 additions & 103 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,87 @@
1414
#define SWIFT_SIL_SILBRIDGING_H
1515

1616
#include "BridgedSwiftObject.h"
17+
18+
#include "swift/SIL/SILNode.h"
19+
#include "swift/SIL/SILType.h"
20+
#include "swift/SIL/SILUndef.h"
21+
#include "swift/SIL/SILArgument.h"
22+
#include "swift/SIL/SILInstruction.h"
23+
#include "swift/SIL/SILBasicBlock.h"
24+
#include "swift/SIL/SILFunction.h"
25+
26+
#include "swift/AST/AnyFunctionRef.h"
27+
1728
#include <stddef.h>
1829
#include <string>
1930

31+
using namespace swift;
32+
33+
#define INST(ID, NAME) \
34+
inline SILInstruction * _Nonnull getAsSILInstruction(ID * _Nonnull I) { \
35+
return static_cast<SILInstruction *>(I); \
36+
} \
37+
inline ID * _Nullable getAs##ID(SILInstruction * _Nonnull p) { \
38+
return dyn_cast<ID>(p); \
39+
} \
40+
inline bool isa##ID(SILInstruction * _Nonnull p) { return isa<ID>(p); }
41+
42+
#define ABSTRACT_INST(ID, NAME) \
43+
inline SILInstruction * _Nonnull getAsSILInstruction(ID * _Nonnull I) { \
44+
return static_cast<SILInstruction *>(I); \
45+
} \
46+
inline ID * _Nullable getAs##ID(SILInstruction * _Nonnull p) { \
47+
return dyn_cast<ID>(p); \
48+
} \
49+
inline bool isa##ID(SILInstruction * _Nonnull p) { return isa<ID>(p); }
50+
51+
#include "swift/SIL/SILNodes.def"
52+
#undef INST
53+
54+
#define VALUE(ID, NAME) \
55+
inline ID * _Nullable getAs##ID(ValueBase *v) { \
56+
return dyn_cast<ID>(v); \
57+
} \
58+
inline bool isa##ID(ValueBase *v) { return isa<ID>(v); } \
59+
inline ValueBase * _Nullable getAsValue(ID * _Nonnull v) { \
60+
return dyn_cast<ValueBase>(v); \
61+
}
62+
63+
#include "swift/SIL/SILNodes.def"
64+
#undef SINGLE_VALUE_INST
65+
66+
// There are a couple of holes in the above set of functions.
67+
inline SILArgument * _Nullable getAsSILArgument(ValueBase *a) {
68+
return dyn_cast<SILArgument>(a);
69+
}
70+
71+
inline SILArgument * _Nullable getAsSILArgument(SILPhiArgument *a) {
72+
return dyn_cast<SILArgument>(a);
73+
}
74+
75+
inline SILArgument * _Nullable getAsSILArgument(SILFunctionArgument *a) {
76+
return dyn_cast<SILArgument>(a);
77+
}
78+
79+
inline SingleValueInstruction * _Nullable
80+
getAsSingleValueInstruction(ValueBase *v) {
81+
return dyn_cast<SingleValueInstruction>(v);
82+
}
83+
84+
inline ValueBase * _Nullable getAsValue(SILArgument * _Nonnull v) {
85+
return dyn_cast<ValueBase>(v);
86+
}
87+
88+
inline ValueBase * _Nullable getAsValue(SingleValueInstruction * _Nonnull v) {
89+
return dyn_cast<ValueBase>(v);
90+
}
91+
92+
inline ValueBase * _Nullable getAsValue(SILValue v) { return v; }
93+
2094
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS
2195

96+
template<class T> using OptionalRef = T * _Nullable;
97+
2298
typedef struct {
2399
const unsigned char * _Nullable data;
24100
size_t length;
@@ -62,10 +138,6 @@ typedef struct {
62138
void * _Null_unspecified word2;
63139
} BridgedLocation;
64140

65-
typedef struct {
66-
void * _Nullable typePtr;
67-
} BridgedType;
68-
69141
typedef struct {
70142
const void * _Nullable data;
71143
size_t count;
@@ -87,46 +159,10 @@ typedef struct {
87159
const void * _Nullable succ;
88160
} OptionalBridgedSuccessor;
89161

90-
typedef struct {
91-
SwiftObject obj;
92-
} BridgedFunction;
93-
94162
typedef struct {
95163
SwiftObject obj;
96164
} BridgedGlobalVar;
97165

98-
typedef struct {
99-
SwiftObject obj;
100-
} BridgedBasicBlock;
101-
102-
typedef struct {
103-
OptionalSwiftObject obj;
104-
} OptionalBridgedBasicBlock;
105-
106-
typedef struct {
107-
SwiftObject obj;
108-
} BridgedArgument;
109-
110-
typedef struct {
111-
OptionalSwiftObject obj;
112-
} OptionalBridgedArgument;
113-
114-
typedef struct {
115-
SwiftObject obj;
116-
} BridgedNode;
117-
118-
typedef struct {
119-
SwiftObject obj;
120-
} BridgedValue;
121-
122-
typedef struct {
123-
SwiftObject obj;
124-
} BridgedInstruction;
125-
126-
typedef struct {
127-
OptionalSwiftObject obj;
128-
} OptionalBridgedInstruction;
129-
130166
typedef struct {
131167
SwiftObject obj;
132168
} BridgedMultiValueResult;
@@ -144,6 +180,12 @@ typedef enum {
144180

145181
typedef intptr_t SwiftInt;
146182

183+
// TODO: we can remove these once we auto generate equality operators for
184+
// foreign reference types.
185+
inline bool isPtrEq(ValueBase *a, ValueBase *b) { return a == b; }
186+
inline bool isPtrEq(SILInstruction *a, SILInstruction *b) { return a == b; }
187+
inline bool isPtrEq(SILBasicBlock *a, SILBasicBlock *b) { return a == b; }
188+
147189
void registerBridgedClass(BridgedStringRef className, SwiftMetatype metatype);
148190

149191
void OStream_write(BridgedOStream os, BridgedStringRef str);
@@ -153,90 +195,94 @@ void freeBridgedStringRef(BridgedStringRef str);
153195
void PassContext_notifyChanges(BridgedPassContext passContext,
154196
enum ChangeNotificationKind changeKind);
155197
void PassContext_eraseInstruction(BridgedPassContext passContext,
156-
BridgedInstruction inst);
198+
SILInstruction *inst);
157199
BridgedSlab PassContext_getNextSlab(BridgedSlab slab);
158200
BridgedSlab PassContext_allocSlab(BridgedPassContext passContext,
159201
BridgedSlab afterSlab);
160202
BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,
161203
BridgedSlab slab);
162204

163-
BridgedStringRef SILFunction_getName(BridgedFunction function);
164-
std::string SILFunction_debugDescription(BridgedFunction function);
165-
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
166-
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
167-
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
168-
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function);
205+
BridgedStringRef SILFunction_getName(SILFunction *function);
206+
std::string SILFunction_debugDescription(SILFunction *function);
207+
SILBasicBlock * _Nullable SILFunction_firstBlock(SILFunction *function);
208+
OptionalRef<SILBasicBlock> SILFunction_lastBlock(SILFunction *function);
209+
SwiftInt SILFunction_numIndirectResultArguments(SILFunction *function);
210+
SwiftInt SILFunction_getSelfArgumentIndex(SILFunction *function);
169211

170212
BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
171213
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);
172214

173-
OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
174-
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
175-
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
176-
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
177-
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
178-
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
179-
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
180-
BridgedArgument SILBasicBlock_getArgument(BridgedBasicBlock block, SwiftInt index);
181-
OptionalBridgedSuccessor SILBasicBlock_getFirstPred(BridgedBasicBlock block);
215+
int SILBasicBlock_mytest(SILBasicBlock *b);
216+
217+
OptionalRef<SILBasicBlock> SILBasicBlock_next(SILBasicBlock *block);
218+
OptionalRef<SILBasicBlock> SILBasicBlock_previous(SILBasicBlock *block);
219+
SILFunction *SILBasicBlock_getFunction(SILBasicBlock *block);
220+
std::string SILBasicBlock_debugDescription(SILBasicBlock *block);
221+
SILInstruction *SILBasicBlock_firstInst(SILBasicBlock *block);
222+
// TODO: we could make this a terminator inst.
223+
OptionalRef<SILInstruction> SILBasicBlock_lastInst(SILBasicBlock *block);
224+
SwiftInt SILBasicBlock_getNumArguments(SILBasicBlock *block);
225+
SILArgument *SILBasicBlock_getArgument(SILBasicBlock *block, SwiftInt index);
226+
OptionalBridgedSuccessor SILBasicBlock_getFirstPred(SILBasicBlock *block);
182227
OptionalBridgedSuccessor SILSuccessor_getNext(BridgedSuccessor succ);
183-
BridgedBasicBlock SILSuccessor_getTargetBlock(BridgedSuccessor succ);
184-
BridgedInstruction SILSuccessor_getContainingInst(BridgedSuccessor succ);
228+
SILBasicBlock *SILSuccessor_getTargetBlock(BridgedSuccessor succ);
229+
SILInstruction *SILSuccessor_getContainingInst(BridgedSuccessor succ);
185230

186-
BridgedValue Operand_getValue(BridgedOperand);
231+
ValueBase *Operand_getValue(BridgedOperand);
187232
OptionalBridgedOperand Operand_nextUse(BridgedOperand);
188-
BridgedInstruction Operand_getUser(BridgedOperand);
233+
SILInstruction *Operand_getUser(BridgedOperand);
189234
SwiftInt Operand_isTypeDependent(BridgedOperand);
190235

191-
std::string SILNode_debugDescription(BridgedNode node);
192-
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
193-
BridgedType SILValue_getType(BridgedValue value);
236+
std::string SILNode_debugDescription(ValueBase *node);
237+
std::string SILInstruction_debugDescription(SILInstruction *i);
238+
OptionalBridgedOperand SILValue_firstUse(ValueBase *value);
239+
SILType SILValue_getType(ValueBase *value);
194240

195-
SwiftInt SILType_isAddress(BridgedType);
196-
SwiftInt SILType_isTrivial(BridgedType, BridgedFunction);
241+
SwiftInt SILType_isAddress(SILType);
242+
SwiftInt SILType_isTrivial(SILType, SILFunction *);
197243

198-
BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
244+
SILBasicBlock *SILArgument_getParent(SILArgument *argument);
199245

200-
OptionalBridgedInstruction SILInstruction_next(BridgedInstruction inst);
201-
OptionalBridgedInstruction SILInstruction_previous(BridgedInstruction inst);
202-
BridgedBasicBlock SILInstruction_getParent(BridgedInstruction inst);
203-
BridgedArrayRef SILInstruction_getOperands(BridgedInstruction inst);
204-
void SILInstruction_setOperand(BridgedInstruction inst, SwiftInt index,
205-
BridgedValue value);
206-
BridgedLocation SILInstruction_getLocation(BridgedInstruction inst);
207-
BridgedMemoryBehavior SILInstruction_getMemBehavior(BridgedInstruction inst);
246+
OptionalRef<SILInstruction> SILInstruction_next(SILInstruction *inst);
247+
OptionalRef<SILInstruction> SILInstruction_previous(SILInstruction *inst);
248+
SILBasicBlock *SILInstruction_getParent(SILInstruction *inst);
249+
BridgedArrayRef SILInstruction_getOperands(SILInstruction *inst);
250+
void SILInstruction_setOperand(SILInstruction *inst, SwiftInt index,
251+
ValueBase *value);
252+
BridgedLocation SILInstruction_getLocation(SILInstruction *inst);
253+
BridgedMemoryBehavior SILInstruction_getMemBehavior(SILInstruction *inst);
208254

209-
BridgedInstruction MultiValueInstResult_getParent(BridgedMultiValueResult result);
210-
SwiftInt MultipleValueInstruction_getNumResults(BridgedInstruction inst);
255+
SILInstruction *MultiValueInstResult_getParent(BridgedMultiValueResult result);
256+
SwiftInt MultipleValueInstruction_getNumResults(MultipleValueInstruction *inst);
211257
BridgedMultiValueResult
212-
MultipleValueInstruction_getResult(BridgedInstruction inst, SwiftInt index);
213-
214-
BridgedArrayRef TermInst_getSuccessors(BridgedInstruction term);
215-
216-
BridgedStringRef CondFailInst_getMessage(BridgedInstruction cfi);
217-
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
218-
SwiftInt TupleExtractInst_fieldIndex(BridgedInstruction tei);
219-
SwiftInt TupleElementAddrInst_fieldIndex(BridgedInstruction teai);
220-
SwiftInt StructExtractInst_fieldIndex(BridgedInstruction sei);
221-
SwiftInt StructElementAddrInst_fieldIndex(BridgedInstruction seai);
222-
SwiftInt EnumInst_caseIndex(BridgedInstruction ei);
223-
SwiftInt UncheckedEnumDataInst_caseIndex(BridgedInstruction uedi);
224-
SwiftInt RefElementAddrInst_fieldIndex(BridgedInstruction reai);
225-
SwiftInt PartialApplyInst_numArguments(BridgedInstruction ai);
226-
SwiftInt ApplyInst_numArguments(BridgedInstruction ai);
227-
SwiftInt BeginApplyInst_numArguments(BridgedInstruction ai);
228-
SwiftInt TryApplyInst_numArguments(BridgedInstruction ai);
229-
BridgedBasicBlock BranchInst_getTargetBlock(BridgedInstruction bi);
230-
SwiftInt SwitchEnumInst_getNumCases(BridgedInstruction se);
231-
SwiftInt SwitchEnumInst_getCaseIndex(BridgedInstruction se, SwiftInt idx);
232-
SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store);
233-
234-
BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
235-
BridgedInstruction insertionPoint,
258+
MultipleValueInstruction_getResult(MultipleValueInstruction *inst, SwiftInt index);
259+
260+
BridgedArrayRef TermInst_getSuccessors(TermInst *term);
261+
262+
BridgedStringRef CondFailInst_getMessage(CondFailInst *cfi);
263+
BridgedGlobalVar GlobalAccessInst_getGlobal(GlobalAccessInst *globalInst);
264+
SwiftInt TupleExtractInst_fieldIndex(TupleExtractInst *tei);
265+
SwiftInt TupleElementAddrInst_fieldIndex(TupleElementAddrInst *teai);
266+
SwiftInt StructExtractInst_fieldIndex(StructExtractInst *sei);
267+
SwiftInt StructElementAddrInst_fieldIndex(StructElementAddrInst *seai);
268+
SwiftInt EnumInst_caseIndex(EnumInst *ei);
269+
SwiftInt UncheckedEnumDataInst_caseIndex(UncheckedEnumDataInst *uedi);
270+
SwiftInt RefElementAddrInst_fieldIndex(RefElementAddrInst *reai);
271+
SwiftInt PartialApplyInst_numArguments(PartialApplyInst *ai);
272+
SwiftInt ApplyInst_numArguments(ApplyInst *ai);
273+
SwiftInt BeginApplyInst_numArguments(BeginApplyInst *ai);
274+
SwiftInt TryApplyInst_numArguments(TryApplyInst *ai);
275+
SILBasicBlock *BranchInst_getTargetBlock(BranchInst *bi);
276+
SwiftInt SwitchEnumInst_getNumCases(SwitchEnumInst *se);
277+
SwiftInt SwitchEnumInst_getCaseIndex(SwitchEnumInst *se, SwiftInt idx);
278+
SwiftInt StoreInst_getStoreOwnership(StoreInst *store);
279+
280+
SILInstruction *SILBuilder_createBuiltinBinaryFunction(
281+
SILInstruction *insertionPoint,
236282
BridgedLocation loc, BridgedStringRef name,
237-
BridgedType operandType, BridgedType resultType, BridgedValueArray arguments);
238-
BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
239-
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge);
283+
SILType operandType, SILType resultType, BridgedValueArray arguments);
284+
SILInstruction *SILBuilder_createCondFail(SILInstruction *insertionPoint,
285+
BridgedLocation loc, ValueBase *condition, BridgedStringRef messge);
240286

241287
SWIFT_END_NULLABILITY_ANNOTATIONS
242288

0 commit comments

Comments
 (0)