Swift SIL: add some APIs

This commit is contained in:
Erik Eckstein
2023-05-19 18:33:11 +02:00
parent b707b5a595
commit ee1c52bc77
9 changed files with 103 additions and 2 deletions

View File

@@ -64,11 +64,21 @@ public struct StringRef : CustomStringConvertible, NoReflectionChildren {
public var string: String { _bridged.string } public var string: String { _bridged.string }
public var description: String { string } public var description: String { string }
public var count: Int {
Int(_bridged.__bytes_endUnsafe() - _bridged.__bytes_beginUnsafe())
}
public subscript(index: Int) -> UInt8 {
let buffer = UnsafeBufferPointer<UInt8>(start: _bridged.__bytes_beginUnsafe(),
count: count)
return buffer[index]
}
public static func ==(lhs: StringRef, rhs: StaticString) -> Bool { public static func ==(lhs: StringRef, rhs: StaticString) -> Bool {
let lhsBuffer = UnsafeBufferPointer<UInt8>( let lhsBuffer = UnsafeBufferPointer<UInt8>(
start: lhs._bridged.__bytes_beginUnsafe(), start: lhs._bridged.__bytes_beginUnsafe(),
count: Int(lhs._bridged.__bytes_endUnsafe() - lhs._bridged.__bytes_beginUnsafe())) count: lhs.count)
return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in return rhs.withUTF8Buffer { (rhsBuffer: UnsafeBufferPointer<UInt8>) in
if lhsBuffer.count != rhsBuffer.count { return false } if lhsBuffer.count != rhsBuffer.count { return false }
return lhsBuffer.elementsEqual(rhsBuffer, by: ==) return lhsBuffer.elementsEqual(rhsBuffer, by: ==)

View File

@@ -115,6 +115,13 @@ extension ApplySite {
} }
return nil return nil
} }
public func hasSemanticsAttribute(_ attr: StaticString) -> Bool {
if let callee = referencedFunction {
return callee.hasSemanticsAttribute(attr)
}
return false
}
} }
public protocol FullApplySite : ApplySite { public protocol FullApplySite : ApplySite {

View File

@@ -126,6 +126,8 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
public var isTransparent: Bool { bridged.isTransparent() } public var isTransparent: Bool { bridged.isTransparent() }
public var isAsync: Bool { bridged.isAsync() }
/// True if this is a `[global_init]` function. /// True if this is a `[global_init]` function.
/// ///
/// Such a function is typically a global addressor which calls the global's /// Such a function is typically a global addressor which calls the global's
@@ -222,6 +224,9 @@ final public class Function : CustomStringConvertible, HasShortDescription, Hash
} }
} }
public var isSerialized: Bool { bridged.isSerialized() }
public var hasValidLinkageForFragileRef: Bool { bridged.hasValidLinkageForFragileRef() }
/// True, if the function runs with a swift 5.1 runtime. /// True, if the function runs with a swift 5.1 runtime.
/// Note that this is function specific, because inlinable functions are de-serialized /// Note that this is function specific, because inlinable functions are de-serialized
/// in a client module, which might be compiled with a different deployment target. /// in a client module, which might be compiled with a different deployment target.

View File

@@ -37,4 +37,8 @@ public struct Location: Equatable, CustomStringConvertible {
public func hasSameSourceLocation(as other: Location) -> Bool { public func hasSameSourceLocation(as other: Location) -> Bool {
bridged.hasSameSourceLocation(other.bridged) bridged.hasSameSourceLocation(other.bridged)
} }
public static var artificialUnreachableLocation: Location {
Location(bridged: swift.SILDebugLocation.getArtificialUnreachableLocation())
}
} }

View File

@@ -107,6 +107,19 @@ public struct UseList : CollectionLikeSequence {
return nil return nil
} }
public func getSingleUser<I: Instruction>(ofType: I.Type) -> I? {
var result: I? = nil
for use in self {
if let user = use.instruction as? I {
if result != nil {
return nil
}
result = user
}
}
return result
}
public var isSingleUse: Bool { singleUse != nil } public var isSingleUse: Bool { singleUse != nil }
public func makeIterator() -> Iterator { public func makeIterator() -> Iterator {

View File

@@ -19,6 +19,9 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
public var isAddress: Bool { bridged.isAddress() } public var isAddress: Bool { bridged.isAddress() }
public var isObject: Bool { !isAddress } public var isObject: Bool { !isAddress }
public var addressType: Type { bridged.getAddressType().type }
public var objectType: Type { bridged.getObjectType().type }
public func isTrivial(in function: Function) -> Bool { public func isTrivial(in function: Function) -> Bool {
return bridged.isTrivial(function.bridged.getFunction()) return bridged.isTrivial(function.bridged.getFunction())
} }
@@ -52,6 +55,19 @@ public struct Type : CustomStringConvertible, NoReflectionChildren {
public var isOrContainsObjectiveCClass: Bool { bridged.isOrContainsObjectiveCClass() } public var isOrContainsObjectiveCClass: Bool { bridged.isOrContainsObjectiveCClass() }
public var isBuiltinInteger: Bool { bridged.isBuiltinInteger() }
public var isBuiltinFloat: Bool { bridged.isBuiltinFloat() }
public var isBuiltinVector: Bool { bridged.isBuiltinVector() }
public var builtinVectorElementType: Type { bridged.getBuiltinVectorElementType().type }
public func isBuiltinInteger(withFixedWidth width: Int) -> Bool {
bridged.isBuiltinFixedWidthInteger(UInt32(width))
}
public func isExactSuperclass(of type: Type) -> Bool {
bridged.isExactSuperclassOf(type.bridged)
}
public var tupleElements: TupleElementArray { TupleElementArray(type: self) } public var tupleElements: TupleElementArray { TupleElementArray(type: self) }
public func getNominalFields(in function: Function) -> NominalFieldsArray { public func getNominalFields(in function: Function) -> NominalFieldsArray {
@@ -167,6 +183,8 @@ extension swift.SILType {
public struct NominalTypeDecl : Equatable { public struct NominalTypeDecl : Equatable {
let bridged: BridgedNominalTypeDecl let bridged: BridgedNominalTypeDecl
public var name: StringRef { StringRef(bridged: bridged.getName()) }
public static func ==(lhs: NominalTypeDecl, rhs: NominalTypeDecl) -> Bool { public static func ==(lhs: NominalTypeDecl, rhs: NominalTypeDecl) -> Bool {
lhs.bridged.decl == rhs.bridged.decl lhs.bridged.decl == rhs.bridged.decl
} }

View File

@@ -241,6 +241,10 @@ struct BridgedFunction {
return getFunction()->isTransparent() == swift::IsTransparent; return getFunction()->isTransparent() == swift::IsTransparent;
} }
bool isAsync() const {
return getFunction()->isAsync();
}
bool isGlobalInitFunction() const { bool isGlobalInitFunction() const {
return getFunction()->isGlobalInit(); return getFunction()->isGlobalInit();
} }
@@ -271,6 +275,14 @@ struct BridgedFunction {
return (InlineStrategy)getFunction()->getInlineStrategy(); return (InlineStrategy)getFunction()->getInlineStrategy();
} }
bool isSerialized() const {
return getFunction()->isSerialized();
}
bool hasValidLinkageForFragileRef() const {
return getFunction()->hasValidLinkageForFragileRef();
}
bool needsStackProtection() const { bool needsStackProtection() const {
return getFunction()->needsStackProtection(); return getFunction()->needsStackProtection();
} }
@@ -1208,6 +1220,11 @@ struct BridgedBuilder{
struct BridgedNominalTypeDecl { struct BridgedNominalTypeDecl {
swift::NominalTypeDecl * _Nonnull decl; swift::NominalTypeDecl * _Nonnull decl;
SWIFT_IMPORT_UNSAFE
llvm::StringRef getName() const {
return decl->getName().str();
}
}; };
// Passmanager and Context // Passmanager and Context

View File

@@ -743,6 +743,10 @@ public:
return getLocation().hasSameSourceLocation(rhs.getLocation()) && return getLocation().hasSameSourceLocation(rhs.getLocation()) &&
getScope() == rhs.getScope(); getScope() == rhs.getScope();
} }
static SILDebugLocation getArtificialUnreachableLocation() {
return SILDebugLocation(ArtificialUnreachableLocation(), nullptr);
}
}; };
} // end swift namespace } // end swift namespace

View File

@@ -267,6 +267,29 @@ public:
}); });
} }
bool isBuiltinInteger() const {
return is<BuiltinIntegerType>();
}
bool isBuiltinFixedWidthInteger(unsigned width) const {
BuiltinIntegerType *bi = getAs<BuiltinIntegerType>();
return bi && bi->isFixedWidth(width);
}
bool isBuiltinFloat() const {
return is<BuiltinFloatType>();
}
bool isBuiltinVector() const {
return is<BuiltinVectorType>();
}
SWIFT_IMPORT_UNSAFE
SILType getBuiltinVectorElementType() const {
auto vector = castTo<BuiltinVectorType>();
return getPrimitiveObjectType(vector.getElementType());
}
/// Retrieve the NominalTypeDecl for a type that maps to a Swift /// Retrieve the NominalTypeDecl for a type that maps to a Swift
/// nominal or bound generic nominal type. /// nominal or bound generic nominal type.
SWIFT_IMPORT_UNSAFE SWIFT_IMPORT_UNSAFE