Skip to content

[DNM] Experiment: Try introduce a typed ArrayRef bridging wrapper #76642

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

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
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
6 changes: 3 additions & 3 deletions SwiftCompilerSources/Sources/Basic/Utils.swift
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@ extension String {
}

extension Array {
public func withBridgedArrayRef<T>(_ c: (BridgedArrayRef) -> T) -> T {
public func withBridgedArrayRef<T>(_ c: (BridgedErasedArrayRef) -> T) -> T {
return withUnsafeBytes { buf in
return c(BridgedArrayRef(data: buf.baseAddress!, count: count))
return c(BridgedErasedArrayRef(data: buf.baseAddress!, count: count))
}
}
}
Expand All @@ -171,7 +171,7 @@ extension Optional where Wrapped == UnsafeMutablePointer<BridgedSwiftObject> {
}
}

extension BridgedArrayRef {
extension BridgedErasedArrayRef {
public func withElements<T, R>(ofType ty: T.Type, _ c: (UnsafeBufferPointer<T>) -> R) -> R {
let start = data?.bindMemory(to: ty, capacity: count)
let buffer = UnsafeBufferPointer(start: start, count: count)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ func registerBorrowedFromUpdater() {
let function = bridgedFunction.function;
updateBorrowedFrom(in: function, context)
},
{ (bridgedCtxt: BridgedPassContext, bridgedPhiArray: BridgedArrayRef) in
{ (bridgedCtxt: BridgedPassContext, bridgedPhiArray: BridgedErasedArrayRef) in
let context = FunctionPassContext(_bridged: bridgedCtxt)
var guaranteedPhis = Stack<Phi>(context)
defer { guaranteedPhis.deinitialize() }
Expand Down
2 changes: 1 addition & 1 deletion SwiftCompilerSources/Sources/SIL/Function.swift
Original file line number Diff line number Diff line change
Expand Up @@ -418,7 +418,7 @@ extension Function {
s._withBridgedStringRef { $0.write(os) }
},
// parseFn:
{ (f: BridgedFunction, str: BridgedStringRef, mode: BridgedFunction.ParseEffectsMode, argumentIndex: Int, paramNames: BridgedArrayRef) -> BridgedFunction.ParsingError in
{ (f: BridgedFunction, str: BridgedStringRef, mode: BridgedFunction.ParseEffectsMode, argumentIndex: Int, paramNames: BridgedErasedArrayRef) -> BridgedFunction.ParsingError in
do {
var parser = StringParser(String(str))
let function = f.function
Expand Down
136 changes: 72 additions & 64 deletions include/swift/AST/ASTBridging.h

Large diffs are not rendered by default.

67 changes: 60 additions & 7 deletions include/swift/Basic/BasicBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,23 +114,23 @@ void assertFail(const char * _Nonnull msg, const char * _Nonnull file,
// MARK: ArrayRef
//===----------------------------------------------------------------------===//

class BridgedArrayRef {
class BridgedErasedArrayRef {
public:
SWIFT_UNAVAILABLE("Use '.data' instead")
const void *_Nullable Data;

SWIFT_UNAVAILABLE("Use '.count' instead")
size_t Length;

BridgedArrayRef() : Data(nullptr), Length(0) {}
BridgedErasedArrayRef() : Data(nullptr), Length(0) {}

SWIFT_NAME("init(data:count:)")
BridgedArrayRef(const void *_Nullable data, size_t length)
BridgedErasedArrayRef(const void *_Nullable data, size_t length)
: Data(data), Length(length) {}

#ifdef USED_IN_CPP_SOURCE
template <typename T>
BridgedArrayRef(llvm::ArrayRef<T> arr)
BridgedErasedArrayRef(llvm::ArrayRef<T> arr)
: Data(arr.data()), Length(arr.size()) {}

template <typename T>
Expand All @@ -140,12 +140,65 @@ class BridgedArrayRef {
#endif
};

SWIFT_NAME("getter:BridgedArrayRef.data(self:)")
SWIFT_NAME("getter:BridgedErasedArrayRef.data(self:)")
BRIDGED_INLINE
const void *_Nullable BridgedArrayRef_data(BridgedArrayRef arr);
const void *_Nullable BridgedErasedArrayRef_data(BridgedErasedArrayRef arr);

SWIFT_NAME("getter:BridgedErasedArrayRef.count(self:)")
BRIDGED_INLINE SwiftInt BridgedErasedArrayRef_count(BridgedErasedArrayRef arr);


/// Bridgable wrapper for 'llvm::ArrayRef<Element>'.
template <typename _Element>
class BridgedArrayRef {
public:
// Seemingly needed for Swift-side conformance.
using Element = _Element;

SWIFT_UNAVAILABLE("Use '.data' instead")
const void *_Nullable Data;

SWIFT_UNAVAILABLE("Use '.count' instead")
size_t Length;

SWIFT_NAME("init(data:count:)")
BridgedArrayRef(const Element *_Nullable data, SwiftInt length)
: Data(data), Length(length) {}

#ifdef USED_IN_CPP_SOURCE
using ArrRefTy = llvm::ArrayRef<Element>;

BridgedArrayRef(ArrRefTy arrRef)
: Data(arrRef.data()), Length(arrRef.size()) {}

using iterator = typename ArrRefTy::iterator;
iterator begin() const { return unbridged().begin(); }
iterator end() const { return unbridged().end(); }

ArrRefTy unbridged() const {
return ArrRefTy(static_cast<const Element *>(Data), Length);
}
#endif

bool empty() const { return Length == 0; }

}
#ifdef IMPORTING_INTO_ASTGEN
__attribute__((swift_attr("conforms_to:swiftASTGen.BridgedArrayRefProtocol")))
#endif
;

template<typename T>
SWIFT_NAME("getter:BridgedArrayRef.data(self:)")
inline const void *_Nullable BridgedArrayRef_data(BridgedArrayRef<T> arr) {
return arr.Data;
}

template<typename T>
SWIFT_NAME("getter:BridgedArrayRef.count(self:)")
BRIDGED_INLINE SwiftInt BridgedArrayRef_count(BridgedArrayRef arr);
inline SwiftInt BridgedArrayRef_count(BridgedArrayRef<T> arr) {
return arr.Length;
}

//===----------------------------------------------------------------------===//
// MARK: Data
Expand Down
6 changes: 3 additions & 3 deletions include/swift/Basic/BasicBridgingImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@
SWIFT_BEGIN_NULLABILITY_ANNOTATIONS

//===----------------------------------------------------------------------===//
// MARK: BridgedArrayRef
// MARK: BridgedErasedArrayRef
//===----------------------------------------------------------------------===//

const void *_Nullable BridgedArrayRef_data(BridgedArrayRef arr) {
const void *_Nullable BridgedErasedArrayRef_data(BridgedErasedArrayRef arr) {
return arr.Data;
}

SwiftInt BridgedArrayRef_count(BridgedArrayRef arr) {
SwiftInt BridgedErasedArrayRef_count(BridgedErasedArrayRef arr) {
return static_cast<SwiftInt>(arr.Length);
}

Expand Down
17 changes: 8 additions & 9 deletions include/swift/SIL/SILBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ struct OptionalBridgedResultInfo {
};

struct BridgedResultInfoArray {
BridgedArrayRef resultInfoArray;
BridgedErasedArrayRef resultInfoArray;

#ifdef USED_IN_CPP_SOURCE
BridgedResultInfoArray(llvm::ArrayRef<swift::SILResultInfo> results)
Expand Down Expand Up @@ -215,7 +215,7 @@ struct BridgedParameterInfo {
};

struct BridgedParameterInfoArray {
BridgedArrayRef parameterInfoArray;
BridgedErasedArrayRef parameterInfoArray;

#ifdef USED_IN_CPP_SOURCE
BridgedParameterInfoArray(llvm::ArrayRef<swift::SILParameterInfo> parameters)
Expand All @@ -233,7 +233,7 @@ struct BridgedParameterInfoArray {
};

struct BridgedYieldInfoArray {
BridgedArrayRef yieldInfoArray;
BridgedErasedArrayRef yieldInfoArray;

#ifdef USED_IN_CPP_SOURCE
BridgedYieldInfoArray(llvm::ArrayRef<swift::SILYieldInfo> yields)
Expand Down Expand Up @@ -271,7 +271,7 @@ struct BridgedLifetimeDependenceInfo {
};

struct BridgedLifetimeDependenceInfoArray {
BridgedArrayRef lifetimeDependenceInfoArray;
BridgedErasedArrayRef lifetimeDependenceInfoArray;

#ifdef USED_IN_CPP_SOURCE
BridgedLifetimeDependenceInfoArray(
Expand Down Expand Up @@ -704,10 +704,9 @@ struct BridgedFunction {

typedef void (* _Nonnull RegisterFn)(BridgedFunction f, void * _Nonnull data, SwiftInt size);
typedef void (* _Nonnull WriteFn)(BridgedFunction, BridgedOStream, SwiftInt);
typedef ParsingError (*_Nonnull ParseFn)(BridgedFunction,
BridgedStringRef,
typedef ParsingError (*_Nonnull ParseFn)(BridgedFunction, BridgedStringRef,
ParseEffectsMode, SwiftInt,
BridgedArrayRef);
BridgedErasedArrayRef);
typedef SwiftInt (* _Nonnull CopyEffectsFn)(BridgedFunction, BridgedFunction);
typedef EffectInfo (* _Nonnull GetEffectInfoFn)(BridgedFunction, SwiftInt);
typedef BridgedMemoryBehavior (* _Nonnull GetMemBehaviorFn)(BridgedFunction, bool);
Expand Down Expand Up @@ -778,7 +777,7 @@ struct BridgedSubstitutionMap {
};

struct BridgedTypeArray {
BridgedArrayRef typeArray;
BridgedErasedArrayRef typeArray;

// Ensure that this struct value type will be indirectly returned on
// Windows ARM64,
Expand All @@ -802,7 +801,7 @@ struct BridgedTypeArray {
};

struct BridgedSILTypeArray {
BridgedArrayRef typeArray;
BridgedErasedArrayRef typeArray;

#ifdef USED_IN_CPP_SOURCE
BridgedSILTypeArray(llvm::ArrayRef<swift::SILType> silTypes)
Expand Down
10 changes: 6 additions & 4 deletions include/swift/SILOptimizer/OptimizerBridging.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ struct BridgedPostDomTree {
struct BridgedUtilities {
typedef void (* _Nonnull VerifyFunctionFn)(BridgedPassContext, BridgedFunction);
typedef void (* _Nonnull UpdateBorrowedFromFn)(BridgedPassContext, BridgedFunction);
typedef void (* _Nonnull UpdateBorrowedFromPhisFn)(BridgedPassContext, BridgedArrayRef);
typedef void (*_Nonnull UpdateBorrowedFromPhisFn)(BridgedPassContext,
BridgedErasedArrayRef);

static void registerVerifier(VerifyFunctionFn verifyFunctionFn);
static void registerBorrowedFromUpdater(UpdateBorrowedFromFn updateBorrowedFromFn,
Expand Down Expand Up @@ -257,9 +258,10 @@ struct BridgedPassContext {
BridgedOwnedString mangleWithDeadArgs(const SwiftInt * _Nullable deadArgs,
SwiftInt numDeadArgs,
BridgedFunction function) const;
BridgedOwnedString mangleWithClosureArgs(BridgedValueArray closureArgs,
BridgedArrayRef closureArgIndices,
BridgedFunction applySiteCallee) const;
BridgedOwnedString
mangleWithClosureArgs(BridgedValueArray closureArgs,
BridgedErasedArrayRef closureArgIndices,
BridgedFunction applySiteCallee) const;

SWIFT_IMPORT_UNSAFE BridgedGlobalVar createGlobalVariable(BridgedStringRef name, BridgedType type,
bool isPrivate) const;
Expand Down
Loading