Skip to content

[libswift] Refactor libSwift to use SIL types directly and remove bridged versions. #39958

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 13 additions & 1 deletion cmake/modules/AddSwift.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -716,6 +716,7 @@ function(add_libswift name)
set(libswift_compile_options
"-Xfrontend" "-validate-tbd-against-ir=none"
"-Xfrontend" "-enable-cxx-interop"
"-Xfrontend" "-disable-llvm-verify"
"-Xcc" "-UIBOutlet" "-Xcc" "-UIBAction" "-Xcc" "-UIBInspectable")

if(CMAKE_BUILD_TYPE STREQUAL Debug)
Expand Down Expand Up @@ -778,7 +779,18 @@ function(add_libswift name)
"-emit-module-path" "${build_dir}/${module}.swiftmodule"
"-parse-as-library" ${sources}
"-wmo" ${libswift_compile_options}
"-I" "${SWIFT_SOURCE_DIR}/include/swift"
"-I" "${CMAKE_SOURCE_DIR}/include/swift"
# The Swift source header includes:
"-I" "${CMAKE_SOURCE_DIR}/include"
# LLVM and Clang source header includes:
"-I" "${LLVM_MAIN_SRC_DIR}/../clang/include"
"-I" "${LLVM_MAIN_INCLUDE_DIR}"
# Swift build includes:
"-I" "${CMAKE_BINARY_DIR}/include"
# LLVM and Clang build includes:
"-I" "${LLVM_BINARY_DIR}/tools/clang/include"
"-I" "${LLVM_BINARY_DIR}/include"
# libSwift build includes:
"-I" "${build_dir}"
COMMENT "Building libswift module ${module}")

Expand Down
5 changes: 2 additions & 3 deletions include/swift/SIL/BridgedSwiftObject.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,9 @@
// Provide macros to temporarily suppress warning about the use of
// _Nullable and _Nonnull.
# define SWIFT_BEGIN_NULLABILITY_ANNOTATIONS \
_Pragma("clang diagnostic push") \
_Pragma("clang diagnostic ignored \"-Wnullability-extension\"")
_Pragma("clang assume_nonnull begin")
# define SWIFT_END_NULLABILITY_ANNOTATIONS \
_Pragma("clang diagnostic pop")
_Pragma("clang assume_nonnull end")

#else
// #define _Nullable and _Nonnull to nothing if we're not being built
Expand Down
9 changes: 6 additions & 3 deletions include/swift/SIL/SILArgument.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,8 @@ struct SILArgumentKind {
}
};

class SILArgument : public ValueBase {
class __attribute__((swift_attr("import_as_ref"))) SILArgument
: public ValueBase {
friend class SILBasicBlock;

SILBasicBlock *parentBlock;
Expand Down Expand Up @@ -202,7 +203,8 @@ inline SILArgument *castToArgument(SwiftObject argument) {
return static_cast<SILArgument *>(argument);
}

class SILPhiArgument : public SILArgument {
class __attribute__((swift_attr("import_as_ref"))) SILPhiArgument
: public SILArgument {
friend class SILBasicBlock;

SILPhiArgument(SILBasicBlock *parentBlock, SILType type,
Expand Down Expand Up @@ -310,7 +312,8 @@ class SILPhiArgument : public SILArgument {
}
};

class SILFunctionArgument : public SILArgument {
class __attribute__((swift_attr("import_as_ref"))) SILFunctionArgument
: public SILArgument {
friend class SILBasicBlock;

bool noImplicitCopy = false;
Expand Down
2 changes: 1 addition & 1 deletion include/swift/SIL/SILBasicBlock.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ class SILFunction;
class SILArgument;
class SILPrintContext;

class SILBasicBlock :
class __attribute__((swift_attr("import_as_ref"))) SILBasicBlock :
public llvm::ilist_node<SILBasicBlock>, public SILAllocated<SILBasicBlock>,
public SwiftObjectHeader {
friend class SILSuccessor;
Expand Down
252 changes: 149 additions & 103 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,87 @@
#define SWIFT_SIL_SILBRIDGING_H

#include "BridgedSwiftObject.h"

#include "swift/SIL/SILNode.h"
#include "swift/SIL/SILType.h"
#include "swift/SIL/SILUndef.h"
#include "swift/SIL/SILArgument.h"
#include "swift/SIL/SILInstruction.h"
#include "swift/SIL/SILBasicBlock.h"
#include "swift/SIL/SILFunction.h"

#include "swift/AST/AnyFunctionRef.h"

#include <stddef.h>
#include <string>

using namespace swift;

#define INST(ID, NAME) \
inline SILInstruction * _Nonnull getAsSILInstruction(ID * _Nonnull I) { \
return static_cast<SILInstruction *>(I); \
} \
inline ID * _Nullable getAs##ID(SILInstruction * _Nonnull p) { \
return dyn_cast<ID>(p); \
} \
inline bool isa##ID(SILInstruction * _Nonnull p) { return isa<ID>(p); }

#define ABSTRACT_INST(ID, NAME) \
inline SILInstruction * _Nonnull getAsSILInstruction(ID * _Nonnull I) { \
return static_cast<SILInstruction *>(I); \
} \
inline ID * _Nullable getAs##ID(SILInstruction * _Nonnull p) { \
return dyn_cast<ID>(p); \
} \
inline bool isa##ID(SILInstruction * _Nonnull p) { return isa<ID>(p); }

#include "swift/SIL/SILNodes.def"
#undef INST

#define VALUE(ID, NAME) \
inline ID * _Nullable getAs##ID(ValueBase *v) { \
return dyn_cast<ID>(v); \
} \
inline bool isa##ID(ValueBase *v) { return isa<ID>(v); } \
inline ValueBase * _Nullable getAsValue(ID * _Nonnull v) { \
return dyn_cast<ValueBase>(v); \
}

#include "swift/SIL/SILNodes.def"
#undef SINGLE_VALUE_INST

// There are a couple of holes in the above set of functions.
inline SILArgument * _Nullable getAsSILArgument(ValueBase *a) {
return dyn_cast<SILArgument>(a);
}

inline SILArgument * _Nullable getAsSILArgument(SILPhiArgument *a) {
return dyn_cast<SILArgument>(a);
}

inline SILArgument * _Nullable getAsSILArgument(SILFunctionArgument *a) {
return dyn_cast<SILArgument>(a);
}

inline SingleValueInstruction * _Nullable
getAsSingleValueInstruction(ValueBase *v) {
return dyn_cast<SingleValueInstruction>(v);
}

inline ValueBase * _Nullable getAsValue(SILArgument * _Nonnull v) {
return dyn_cast<ValueBase>(v);
}

inline ValueBase * _Nullable getAsValue(SingleValueInstruction * _Nonnull v) {
return dyn_cast<ValueBase>(v);
}

inline ValueBase * _Nullable getAsValue(SILValue v) { return v; }

SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

template<class T> using OptionalRef = T * _Nullable;

typedef struct {
const unsigned char * _Nullable data;
size_t length;
Expand Down Expand Up @@ -62,10 +138,6 @@ typedef struct {
void * _Null_unspecified word2;
} BridgedLocation;

typedef struct {
void * _Nullable typePtr;
} BridgedType;

typedef struct {
const void * _Nullable data;
size_t count;
Expand All @@ -87,46 +159,10 @@ typedef struct {
const void * _Nullable succ;
} OptionalBridgedSuccessor;

typedef struct {
SwiftObject obj;
} BridgedFunction;

typedef struct {
SwiftObject obj;
} BridgedGlobalVar;

typedef struct {
SwiftObject obj;
} BridgedBasicBlock;

typedef struct {
OptionalSwiftObject obj;
} OptionalBridgedBasicBlock;

typedef struct {
SwiftObject obj;
} BridgedArgument;

typedef struct {
OptionalSwiftObject obj;
} OptionalBridgedArgument;

typedef struct {
SwiftObject obj;
} BridgedNode;

typedef struct {
SwiftObject obj;
} BridgedValue;

typedef struct {
SwiftObject obj;
} BridgedInstruction;

typedef struct {
OptionalSwiftObject obj;
} OptionalBridgedInstruction;

typedef struct {
SwiftObject obj;
} BridgedMultiValueResult;
Expand All @@ -144,6 +180,12 @@ typedef enum {

typedef intptr_t SwiftInt;

// TODO: we can remove these once we auto generate equality operators for
// foreign reference types.
inline bool isPtrEq(ValueBase *a, ValueBase *b) { return a == b; }
inline bool isPtrEq(SILInstruction *a, SILInstruction *b) { return a == b; }
inline bool isPtrEq(SILBasicBlock *a, SILBasicBlock *b) { return a == b; }

void registerBridgedClass(BridgedStringRef className, SwiftMetatype metatype);

void OStream_write(BridgedOStream os, BridgedStringRef str);
Expand All @@ -153,90 +195,94 @@ void freeBridgedStringRef(BridgedStringRef str);
void PassContext_notifyChanges(BridgedPassContext passContext,
enum ChangeNotificationKind changeKind);
void PassContext_eraseInstruction(BridgedPassContext passContext,
BridgedInstruction inst);
SILInstruction *inst);
BridgedSlab PassContext_getNextSlab(BridgedSlab slab);
BridgedSlab PassContext_allocSlab(BridgedPassContext passContext,
BridgedSlab afterSlab);
BridgedSlab PassContext_freeSlab(BridgedPassContext passContext,
BridgedSlab slab);

BridgedStringRef SILFunction_getName(BridgedFunction function);
std::string SILFunction_debugDescription(BridgedFunction function);
OptionalBridgedBasicBlock SILFunction_firstBlock(BridgedFunction function);
OptionalBridgedBasicBlock SILFunction_lastBlock(BridgedFunction function);
SwiftInt SILFunction_numIndirectResultArguments(BridgedFunction function);
SwiftInt SILFunction_getSelfArgumentIndex(BridgedFunction function);
BridgedStringRef SILFunction_getName(SILFunction *function);
std::string SILFunction_debugDescription(SILFunction *function);
SILBasicBlock * _Nullable SILFunction_firstBlock(SILFunction *function);
OptionalRef<SILBasicBlock> SILFunction_lastBlock(SILFunction *function);
SwiftInt SILFunction_numIndirectResultArguments(SILFunction *function);
SwiftInt SILFunction_getSelfArgumentIndex(SILFunction *function);

BridgedStringRef SILGlobalVariable_getName(BridgedGlobalVar global);
std::string SILGlobalVariable_debugDescription(BridgedGlobalVar global);

OptionalBridgedBasicBlock SILBasicBlock_next(BridgedBasicBlock block);
OptionalBridgedBasicBlock SILBasicBlock_previous(BridgedBasicBlock block);
BridgedFunction SILBasicBlock_getFunction(BridgedBasicBlock block);
std::string SILBasicBlock_debugDescription(BridgedBasicBlock block);
OptionalBridgedInstruction SILBasicBlock_firstInst(BridgedBasicBlock block);
OptionalBridgedInstruction SILBasicBlock_lastInst(BridgedBasicBlock block);
SwiftInt SILBasicBlock_getNumArguments(BridgedBasicBlock block);
BridgedArgument SILBasicBlock_getArgument(BridgedBasicBlock block, SwiftInt index);
OptionalBridgedSuccessor SILBasicBlock_getFirstPred(BridgedBasicBlock block);
int SILBasicBlock_mytest(SILBasicBlock *b);

OptionalRef<SILBasicBlock> SILBasicBlock_next(SILBasicBlock *block);
OptionalRef<SILBasicBlock> SILBasicBlock_previous(SILBasicBlock *block);
SILFunction *SILBasicBlock_getFunction(SILBasicBlock *block);
std::string SILBasicBlock_debugDescription(SILBasicBlock *block);
SILInstruction *SILBasicBlock_firstInst(SILBasicBlock *block);
// TODO: we could make this a terminator inst.
OptionalRef<SILInstruction> SILBasicBlock_lastInst(SILBasicBlock *block);
SwiftInt SILBasicBlock_getNumArguments(SILBasicBlock *block);
SILArgument *SILBasicBlock_getArgument(SILBasicBlock *block, SwiftInt index);
OptionalBridgedSuccessor SILBasicBlock_getFirstPred(SILBasicBlock *block);
OptionalBridgedSuccessor SILSuccessor_getNext(BridgedSuccessor succ);
BridgedBasicBlock SILSuccessor_getTargetBlock(BridgedSuccessor succ);
BridgedInstruction SILSuccessor_getContainingInst(BridgedSuccessor succ);
SILBasicBlock *SILSuccessor_getTargetBlock(BridgedSuccessor succ);
SILInstruction *SILSuccessor_getContainingInst(BridgedSuccessor succ);

BridgedValue Operand_getValue(BridgedOperand);
ValueBase *Operand_getValue(BridgedOperand);
OptionalBridgedOperand Operand_nextUse(BridgedOperand);
BridgedInstruction Operand_getUser(BridgedOperand);
SILInstruction *Operand_getUser(BridgedOperand);
SwiftInt Operand_isTypeDependent(BridgedOperand);

std::string SILNode_debugDescription(BridgedNode node);
OptionalBridgedOperand SILValue_firstUse(BridgedValue value);
BridgedType SILValue_getType(BridgedValue value);
std::string SILNode_debugDescription(ValueBase *node);
std::string SILInstruction_debugDescription(SILInstruction *i);
OptionalBridgedOperand SILValue_firstUse(ValueBase *value);
SILType SILValue_getType(ValueBase *value);

SwiftInt SILType_isAddress(BridgedType);
SwiftInt SILType_isTrivial(BridgedType, BridgedFunction);
SwiftInt SILType_isAddress(SILType);
SwiftInt SILType_isTrivial(SILType, SILFunction *);

BridgedBasicBlock SILArgument_getParent(BridgedArgument argument);
SILBasicBlock *SILArgument_getParent(SILArgument *argument);

OptionalBridgedInstruction SILInstruction_next(BridgedInstruction inst);
OptionalBridgedInstruction SILInstruction_previous(BridgedInstruction inst);
BridgedBasicBlock SILInstruction_getParent(BridgedInstruction inst);
BridgedArrayRef SILInstruction_getOperands(BridgedInstruction inst);
void SILInstruction_setOperand(BridgedInstruction inst, SwiftInt index,
BridgedValue value);
BridgedLocation SILInstruction_getLocation(BridgedInstruction inst);
BridgedMemoryBehavior SILInstruction_getMemBehavior(BridgedInstruction inst);
OptionalRef<SILInstruction> SILInstruction_next(SILInstruction *inst);
OptionalRef<SILInstruction> SILInstruction_previous(SILInstruction *inst);
SILBasicBlock *SILInstruction_getParent(SILInstruction *inst);
BridgedArrayRef SILInstruction_getOperands(SILInstruction *inst);
void SILInstruction_setOperand(SILInstruction *inst, SwiftInt index,
ValueBase *value);
BridgedLocation SILInstruction_getLocation(SILInstruction *inst);
BridgedMemoryBehavior SILInstruction_getMemBehavior(SILInstruction *inst);

BridgedInstruction MultiValueInstResult_getParent(BridgedMultiValueResult result);
SwiftInt MultipleValueInstruction_getNumResults(BridgedInstruction inst);
SILInstruction *MultiValueInstResult_getParent(BridgedMultiValueResult result);
SwiftInt MultipleValueInstruction_getNumResults(MultipleValueInstruction *inst);
BridgedMultiValueResult
MultipleValueInstruction_getResult(BridgedInstruction inst, SwiftInt index);

BridgedArrayRef TermInst_getSuccessors(BridgedInstruction term);

BridgedStringRef CondFailInst_getMessage(BridgedInstruction cfi);
BridgedGlobalVar GlobalAccessInst_getGlobal(BridgedInstruction globalInst);
SwiftInt TupleExtractInst_fieldIndex(BridgedInstruction tei);
SwiftInt TupleElementAddrInst_fieldIndex(BridgedInstruction teai);
SwiftInt StructExtractInst_fieldIndex(BridgedInstruction sei);
SwiftInt StructElementAddrInst_fieldIndex(BridgedInstruction seai);
SwiftInt EnumInst_caseIndex(BridgedInstruction ei);
SwiftInt UncheckedEnumDataInst_caseIndex(BridgedInstruction uedi);
SwiftInt RefElementAddrInst_fieldIndex(BridgedInstruction reai);
SwiftInt PartialApplyInst_numArguments(BridgedInstruction ai);
SwiftInt ApplyInst_numArguments(BridgedInstruction ai);
SwiftInt BeginApplyInst_numArguments(BridgedInstruction ai);
SwiftInt TryApplyInst_numArguments(BridgedInstruction ai);
BridgedBasicBlock BranchInst_getTargetBlock(BridgedInstruction bi);
SwiftInt SwitchEnumInst_getNumCases(BridgedInstruction se);
SwiftInt SwitchEnumInst_getCaseIndex(BridgedInstruction se, SwiftInt idx);
SwiftInt StoreInst_getStoreOwnership(BridgedInstruction store);

BridgedInstruction SILBuilder_createBuiltinBinaryFunction(
BridgedInstruction insertionPoint,
MultipleValueInstruction_getResult(MultipleValueInstruction *inst, SwiftInt index);

BridgedArrayRef TermInst_getSuccessors(TermInst *term);

BridgedStringRef CondFailInst_getMessage(CondFailInst *cfi);
BridgedGlobalVar GlobalAccessInst_getGlobal(GlobalAccessInst *globalInst);
SwiftInt TupleExtractInst_fieldIndex(TupleExtractInst *tei);
SwiftInt TupleElementAddrInst_fieldIndex(TupleElementAddrInst *teai);
SwiftInt StructExtractInst_fieldIndex(StructExtractInst *sei);
SwiftInt StructElementAddrInst_fieldIndex(StructElementAddrInst *seai);
SwiftInt EnumInst_caseIndex(EnumInst *ei);
SwiftInt UncheckedEnumDataInst_caseIndex(UncheckedEnumDataInst *uedi);
SwiftInt RefElementAddrInst_fieldIndex(RefElementAddrInst *reai);
SwiftInt PartialApplyInst_numArguments(PartialApplyInst *ai);
SwiftInt ApplyInst_numArguments(ApplyInst *ai);
SwiftInt BeginApplyInst_numArguments(BeginApplyInst *ai);
SwiftInt TryApplyInst_numArguments(TryApplyInst *ai);
SILBasicBlock *BranchInst_getTargetBlock(BranchInst *bi);
SwiftInt SwitchEnumInst_getNumCases(SwitchEnumInst *se);
SwiftInt SwitchEnumInst_getCaseIndex(SwitchEnumInst *se, SwiftInt idx);
SwiftInt StoreInst_getStoreOwnership(StoreInst *store);

SILInstruction *SILBuilder_createBuiltinBinaryFunction(
SILInstruction *insertionPoint,
BridgedLocation loc, BridgedStringRef name,
BridgedType operandType, BridgedType resultType, BridgedValueArray arguments);
BridgedInstruction SILBuilder_createCondFail(BridgedInstruction insertionPoint,
BridgedLocation loc, BridgedValue condition, BridgedStringRef messge);
SILType operandType, SILType resultType, BridgedValueArray arguments);
SILInstruction *SILBuilder_createCondFail(SILInstruction *insertionPoint,
BridgedLocation loc, ValueBase *condition, BridgedStringRef messge);

SWIFT_END_NULLABILITY_ANNOTATIONS

Expand Down
Loading