SwiftCompilerSources: add all missing instruction classes

And require that all new instructions must be added and registered in SwiftCompilerSources as well.
This commit is contained in:
Erik Eckstein
2024-02-14 08:31:30 +01:00
parent aadb0598c9
commit 68cc6c8780
4 changed files with 120 additions and 54 deletions

View File

@@ -129,8 +129,6 @@ For example, to add a new instruction class:
* if needed, add bridging functions to access the instruction's data fields.
No yet implemented instruction classes are mapped to a "placeholder" instruction, e.g `UnimplementedInstruction`. This ensures that optimizations can process any kind of SIL, even if some instructions don't have a representation in Swift yet.
## The Optimizer
Similar to SIL, the optimizer also uses a small bridging layer (`OptimizerBridging.h`).

View File

@@ -250,11 +250,6 @@ extension UnaryInstruction {
// no-value instructions
//===----------------------------------------------------------------------===//
/// Used for all non-value instructions which are not implemented here, yet.
/// See registerBridgedClass() in SILBridgingUtils.cpp.
final public class UnimplementedInstruction : Instruction {
}
/// Only one of the operands may have an address type.
public protocol StoringInstruction : Instruction {
var operands: OperandArray { get }
@@ -352,6 +347,9 @@ final public class EndAccessInst : Instruction, UnaryInstruction {
}
}
final public class BeginUnpairedAccessInst : Instruction {}
final public class EndUnpairedAccessInst : Instruction {}
final public class EndBorrowInst : Instruction, UnaryInstruction {}
final public class MarkUninitializedInst : SingleValueInstruction, ConversionInstruction {
@@ -400,6 +398,8 @@ final public class CondFailInst : Instruction, UnaryInstruction {
public var message: StringRef { StringRef(bridged: bridged.CondFailInst_getMessage()) }
}
final public class IncrementProfilerCounterInst : Instruction {}
final public class MarkFunctionEscapeInst : Instruction {}
final public class HopToExecutorInst : Instruction, UnaryInstruction {}
@@ -445,6 +445,8 @@ final public class DebugValueInst : Instruction, UnaryInstruction, DebugVariable
final public class DebugStepInst : Instruction {}
final public class SpecifyTestInst : Instruction {}
final public class UnconditionalCheckedCastAddrInst : Instruction {
public override var mayTrap: Bool { true }
}
@@ -459,6 +461,9 @@ final public class BeginDeallocRefInst : SingleValueInstruction {
final public class EndInitLetRefInst : SingleValueInstruction, UnaryInstruction {}
final public class BindMemoryInst : SingleValueInstruction {}
final public class RebindMemoryInst : SingleValueInstruction {}
public class RefCountingInst : Instruction, UnaryInstruction {
public var isAtomic: Bool { bridged.RefCountingInst_getIsAtomic() }
}
@@ -467,6 +472,8 @@ final public class StrongRetainInst : RefCountingInst {
public var instance: Value { operand.value }
}
final public class StrongRetainUnownedInst : RefCountingInst {}
final public class UnownedRetainInst : RefCountingInst {
public var instance: Value { operand.value }
}
@@ -501,6 +508,9 @@ final public class UnmanagedReleaseValueInst : RefCountingInst {
public var value: Value { return operand.value }
}
final public class AutoreleaseValueInst : RefCountingInst {}
final public class UnmanagedAutoreleaseValueInst : RefCountingInst {}
final public class DestroyValueInst : Instruction, UnaryInstruction {
public var destroyedValue: Value { operand.value }
}
@@ -516,8 +526,6 @@ final public class InjectEnumAddrInst : Instruction, UnaryInstruction, EnumInstr
public var caseIndex: Int { bridged.InjectEnumAddrInst_caseIndex() }
}
final public class UnimplementedRefCountingInst : RefCountingInst {}
//===----------------------------------------------------------------------===//
// no-value deallocation instructions
//===----------------------------------------------------------------------===//
@@ -538,6 +546,7 @@ final public class DeallocStackInst : Instruction, UnaryInstruction, Deallocatio
}
final public class DeallocPackInst : Instruction, UnaryInstruction, Deallocation {}
final public class DeallocPackMetadataInst : Instruction, Deallocation {}
final public class DeallocStackRefInst : Instruction, UnaryInstruction, Deallocation {
public var allocRef: AllocRefInstBase { operand.value as! AllocRefInstBase }
@@ -555,11 +564,6 @@ final public class DeallocExistentialBoxInst : Instruction, UnaryInstruction, De
// single-value instructions
//===----------------------------------------------------------------------===//
/// Used for all SingleValueInstructions which are not implemented here, yet.
/// See registerBridgedClass() in SILBridgingUtils.cpp.
final public class UnimplementedSingleValueInst : SingleValueInstruction {
}
public protocol LoadInstruction: SingleValueInstruction, UnaryInstruction {}
extension LoadInstruction {
@@ -622,6 +626,15 @@ final public class UncheckedTrivialBitCastInst : SingleValueInstruction, UnaryIn
public var fromValue: Value { operand.value }
}
final public class UncheckedBitwiseCastInst : SingleValueInstruction, UnaryInstruction {}
final public class UncheckedValueCastInst : SingleValueInstruction, UnaryInstruction {}
final public class RefToRawPointerInst : SingleValueInstruction, UnaryInstruction {}
final public class RefToUnmanagedInst : SingleValueInstruction, UnaryInstruction {}
final public class RefToUnownedInst : SingleValueInstruction, UnaryInstruction {}
final public class UnmanagedToRefInst : SingleValueInstruction, UnaryInstruction {}
final public class UnownedToRefInst : SingleValueInstruction, UnaryInstruction {}
final public
class RawPointerToRefInst : SingleValueInstruction, UnaryInstruction {
public var pointer: Value { operand.value }
@@ -652,6 +665,8 @@ class IndexAddrInst : SingleValueInstruction {
}
}
final public class IndexRawPointerInst : SingleValueInstruction {}
final public
class TailAddrInst : SingleValueInstruction {
public var base: Value { operands[0].value }
@@ -708,7 +723,15 @@ class ValueMetatypeInst : SingleValueInstruction, UnaryInstruction {}
final public
class ExistentialMetatypeInst : SingleValueInstruction, UnaryInstruction {}
public class GlobalAccessInst : SingleValueInstruction {
final public class OpenPackElementInst : SingleValueInstruction {}
final public class PackLengthInst : SingleValueInstruction {}
final public class DynamicPackIndexInst : SingleValueInstruction {}
final public class PackPackIndexInst : SingleValueInstruction {}
final public class ScalarPackIndexInst : SingleValueInstruction {}
final public class ObjCProtocolInst : SingleValueInstruction {}
public class GlobalAccessInstruction : SingleValueInstruction {
final public var global: GlobalVariable {
bridged.GlobalAccessInst_getGlobal().globalVar
}
@@ -733,7 +756,7 @@ final public class DynamicFunctionRefInst : FunctionRefBaseInst {
final public class PreviousDynamicFunctionRefInst : FunctionRefBaseInst {
}
final public class GlobalAddrInst : GlobalAccessInst, VarDeclInstruction {
final public class GlobalAddrInst : GlobalAccessInstruction, VarDeclInstruction {
public var varDecl: VarDecl? {
VarDecl(bridged: bridged.GlobalAddr_getDecl())
}
@@ -743,10 +766,12 @@ final public class GlobalAddrInst : GlobalAccessInst, VarDeclInstruction {
}
}
final public class GlobalValueInst : GlobalAccessInst {
final public class GlobalValueInst : GlobalAccessInstruction {
public var isBare: Bool { bridged.GlobalValueInst_isBare() }
}
final public class BaseAddrForOffsetInst : SingleValueInstruction {}
final public class AllocGlobalInst : Instruction {
public var global: GlobalVariable {
bridged.AllocGlobalInst_getGlobal().globalVar
@@ -788,6 +813,8 @@ final public class StringLiteralInst : SingleValueInstruction {
}
}
final public class HasSymbolInst : SingleValueInstruction {}
final public class TupleInst : SingleValueInstruction, ForwardingInstruction {}
final public class TupleExtractInst : SingleValueInstruction, UnaryInstruction, ForwardingInstruction {
@@ -897,6 +924,12 @@ class ConvertFunctionInst : SingleValueInstruction, ConversionInstruction {
final public
class ThinToThickFunctionInst : SingleValueInstruction, ConversionInstruction {}
final public class ThickToObjCMetatypeInst : SingleValueInstruction, ConversionInstruction {}
final public class ObjCToThickMetatypeInst : SingleValueInstruction, ConversionInstruction {}
final public class CopyBlockInst : SingleValueInstruction {}
final public class CopyBlockWithoutEscapingInst : SingleValueInstruction {}
final public
class ConvertEscapeToNoEscapeInst : SingleValueInstruction, UnaryInstruction {
public var fromFunction: Value { operand.value }
@@ -920,6 +953,8 @@ class GetAsyncContinuationInst : SingleValueInstruction {}
final public
class GetAsyncContinuationAddrInst : SingleValueInstruction, UnaryInstruction {}
final public class ExtractExecutorInst : SingleValueInstruction {}
final public
class MarkDependenceInst : SingleValueInstruction, ForwardingInstruction {
public enum DependenceKind: Int32 {
@@ -1002,6 +1037,8 @@ final public class ProjectBoxInst : SingleValueInstruction, UnaryInstruction {
public var fieldIndex: Int { bridged.ProjectBoxInst_fieldIndex() }
}
final public class ProjectExistentialBoxInst : SingleValueInstruction, UnaryInstruction {}
public protocol CopyingInstruction : SingleValueInstruction, UnaryInstruction {}
final public class CopyValueInst : SingleValueInstruction, UnaryInstruction, CopyingInstruction {
@@ -1012,6 +1049,11 @@ final public class ExplicitCopyValueInst : SingleValueInstruction, UnaryInstruct
public var fromValue: Value { operand.value }
}
final public class UnownedCopyValueInst : SingleValueInstruction, UnaryInstruction, CopyingInstruction {}
final public class WeakCopyValueInst : SingleValueInstruction, UnaryInstruction, CopyingInstruction {}
final public class UncheckedOwnershipConversionInst : SingleValueInstruction {}
final public class MoveValueInst : SingleValueInstruction, UnaryInstruction {
public var fromValue: Value { operand.value }
@@ -1025,8 +1067,8 @@ final public class DropDeinitInst : SingleValueInstruction, UnaryInstruction {
}
final public class StrongCopyUnownedValueInst : SingleValueInstruction, UnaryInstruction {}
final public class StrongCopyUnmanagedValueInst : SingleValueInstruction, UnaryInstruction {}
final public class StrongCopyWeakValueInst : SingleValueInstruction, UnaryInstruction {}
final public class EndCOWMutationInst : SingleValueInstruction, UnaryInstruction {
public var instance: Value { operand.value }
@@ -1055,6 +1097,8 @@ final public class ApplyInst : SingleValueInstruction, FullApplySite {
public var specializationInfo: SpecializationInfo { bridged.ApplyInst_getSpecializationInfo() }
}
final public class FunctionExtractIsolationInst : SingleValueInstruction {}
final public class ClassMethodInst : SingleValueInstruction, UnaryInstruction {}
final public class SuperMethodInst : SingleValueInstruction, UnaryInstruction {}
@@ -1072,6 +1116,8 @@ final public class IsEscapingClosureInst : SingleValueInstruction, UnaryInstruct
final public class MarkUnresolvedNonCopyableValueInst
: SingleValueInstruction, UnaryInstruction, ConversionInstruction {}
final public class MarkUnresolvedReferenceBindingInst : SingleValueInstruction {}
final public class MarkUnresolvedMoveAddrInst : Instruction, SourceDestAddrInstruction {
public var isTakeOfSrc: Bool { true }
public var isInitializationOfDest: Bool { true }
@@ -1083,6 +1129,8 @@ final public class CopyableToMoveOnlyWrapperValueInst
final public class MoveOnlyWrapperToCopyableValueInst
: SingleValueInstruction, UnaryInstruction, ConversionInstruction {}
final public class MoveOnlyWrapperToCopyableBoxInst : SingleValueInstruction {}
final public class CopyableToMoveOnlyWrapperAddrInst
: SingleValueInstruction, UnaryInstruction {}
@@ -1120,6 +1168,9 @@ final public class PackElementSetInst: SingleValueInstruction {}
final public class DifferentiableFunctionInst: SingleValueInstruction, ForwardingInstruction {}
final public class LinearFunctionInst: SingleValueInstruction, ForwardingInstruction {}
final public class DifferentiableFunctionExtractInst: SingleValueInstruction {}
final public class LinearFunctionExtractInst: SingleValueInstruction {}
final public class DifferentiabilityWitnessFunctionInst: SingleValueInstruction {}
final public class ProjectBlockStorageInst: SingleValueInstruction, UnaryInstruction {}
@@ -1147,6 +1198,9 @@ final public class AllocVectorInst : SingleValueInstruction, Allocation, UnaryIn
public var capacity: Value { operand.value }
}
final public class AllocPackInst : SingleValueInstruction, Allocation {}
final public class AllocPackMetadataInst : SingleValueInstruction, Allocation {}
public class AllocRefInstBase : SingleValueInstruction, Allocation {
final public var isObjC: Bool { bridged.AllocRefInstBase_isObjc() }
@@ -1346,7 +1400,7 @@ final public class SwitchEnumInst : TermInst, ForwardingInstruction {
final public class SwitchEnumAddrInst : TermInst {
}
final public class SelectEnumAddrInst : TermInst {
final public class SelectEnumAddrInst : SingleValueInstruction {
}
final public class DynamicMethodBranchInst : TermInst {

View File

@@ -25,11 +25,6 @@ public func registerSILClasses() {
register(BasicBlock.self)
register(GlobalVariable.self)
// The "unimplemented" registrations must be done before all other node
// registrations. In the order from super -> sub class.
register(UnimplementedInstruction.self)
register(UnimplementedSingleValueInst.self)
register(UnimplementedRefCountingInst.self)
register(MultipleValueInstructionResult.self)
register(Undef.self)
@@ -48,20 +43,27 @@ public func registerSILClasses() {
register(CopyAddrInst.self)
register(ExplicitCopyAddrInst.self)
register(EndAccessInst.self)
register(BeginUnpairedAccessInst.self)
register(EndUnpairedAccessInst.self)
register(EndBorrowInst.self)
register(CondFailInst.self)
register(IncrementProfilerCounterInst.self)
register(MarkFunctionEscapeInst.self)
register(HopToExecutorInst.self)
register(MarkUninitializedInst.self)
register(FixLifetimeInst.self)
register(DebugValueInst.self)
register(DebugStepInst.self)
register(SpecifyTestInst.self)
register(UnconditionalCheckedCastAddrInst.self)
register(BeginDeallocRefInst.self)
register(EndInitLetRefInst.self)
register(BindMemoryInst.self)
register(RebindMemoryInst.self)
register(EndApplyInst.self)
register(AbortApplyInst.self)
register(StrongRetainInst.self)
register(StrongRetainUnownedInst.self)
register(UnownedRetainInst.self)
register(UnmanagedRetainValueInst.self)
register(RetainValueInst.self)
@@ -70,15 +72,19 @@ public func registerSILClasses() {
register(ReleaseValueAddrInst.self)
register(UnownedReleaseInst.self)
register(UnmanagedReleaseValueInst.self)
register(AutoreleaseValueInst.self)
register(UnmanagedAutoreleaseValueInst.self)
register(ReleaseValueInst.self)
register(DestroyValueInst.self)
register(DestroyAddrInst.self)
register(EndLifetimeInst.self)
register(StrongCopyUnownedValueInst.self)
register(StrongCopyUnmanagedValueInst.self)
register(StrongCopyWeakValueInst.self)
register(InjectEnumAddrInst.self)
register(DeallocStackInst.self)
register(DeallocPackInst.self)
register(DeallocPackMetadataInst.self)
register(DeallocStackRefInst.self)
register(DeallocRefInst.self)
register(DeallocPartialRefInst.self)
@@ -94,10 +100,19 @@ public func registerSILClasses() {
register(UncheckedRefCastAddrInst.self)
register(UncheckedAddrCastInst.self)
register(UncheckedTrivialBitCastInst.self)
register(UncheckedBitwiseCastInst.self)
register(UncheckedValueCastInst.self)
register(RefToRawPointerInst.self)
register(RefToUnmanagedInst.self)
register(RefToUnownedInst.self)
register(UnmanagedToRefInst.self)
register(UnownedToRefInst.self)
register(MarkUnresolvedNonCopyableValueInst.self)
register(MarkUnresolvedReferenceBindingInst.self)
register(MarkUnresolvedMoveAddrInst.self)
register(CopyableToMoveOnlyWrapperValueInst.self)
register(MoveOnlyWrapperToCopyableValueInst.self)
register(MoveOnlyWrapperToCopyableBoxInst.self)
register(CopyableToMoveOnlyWrapperAddrInst.self)
register(MoveOnlyWrapperToCopyableAddrInst.self)
register(ObjectInst.self)
@@ -108,12 +123,16 @@ public func registerSILClasses() {
register(PackElementSetInst.self)
register(DifferentiableFunctionInst.self)
register(LinearFunctionInst.self)
register(DifferentiableFunctionExtractInst.self)
register(LinearFunctionExtractInst.self)
register(DifferentiabilityWitnessFunctionInst.self)
register(ProjectBlockStorageInst.self)
register(InitBlockStorageHeaderInst.self)
register(RawPointerToRefInst.self)
register(AddressToPointerInst.self)
register(PointerToAddressInst.self)
register(IndexAddrInst.self)
register(IndexRawPointerInst.self)
register(TailAddrInst.self)
register(InitExistentialRefInst.self)
register(OpenExistentialRefInst.self)
@@ -130,15 +149,23 @@ public func registerSILClasses() {
register(MetatypeInst.self)
register(ValueMetatypeInst.self)
register(ExistentialMetatypeInst.self)
register(OpenPackElementInst.self)
register(PackLengthInst.self)
register(DynamicPackIndexInst.self)
register(PackPackIndexInst.self)
register(ScalarPackIndexInst.self)
register(ObjCProtocolInst.self)
register(FunctionRefInst.self)
register(DynamicFunctionRefInst.self)
register(PreviousDynamicFunctionRefInst.self)
register(GlobalAddrInst.self)
register(GlobalValueInst.self)
register(BaseAddrForOffsetInst.self)
register(AllocGlobalInst.self)
register(IntegerLiteralInst.self)
register(FloatLiteralInst.self)
register(StringLiteralInst.self)
register(HasSymbolInst.self)
register(TupleInst.self)
register(TupleExtractInst.self)
register(TupleElementAddrInst.self)
@@ -157,6 +184,10 @@ public func registerSILClasses() {
register(UnconditionalCheckedCastInst.self)
register(ConvertFunctionInst.self)
register(ThinToThickFunctionInst.self)
register(ThickToObjCMetatypeInst.self)
register(ObjCToThickMetatypeInst.self)
register(CopyBlockInst.self)
register(CopyBlockWithoutEscapingInst.self)
register(ConvertEscapeToNoEscapeInst.self)
register(ObjCExistentialMetatypeToObjectInst.self)
register(ObjCMetatypeToObjectInst.self)
@@ -167,17 +198,23 @@ public func registerSILClasses() {
register(BridgeObjectToWordInst.self)
register(GetAsyncContinuationInst.self)
register(GetAsyncContinuationAddrInst.self)
register(ExtractExecutorInst.self)
register(BeginAccessInst.self)
register(BeginBorrowInst.self)
register(ProjectBoxInst.self)
register(ProjectExistentialBoxInst.self)
register(CopyValueInst.self)
register(ExplicitCopyValueInst.self)
register(UnownedCopyValueInst.self)
register(WeakCopyValueInst.self)
register(UncheckedOwnershipConversionInst.self)
register(MoveValueInst.self)
register(DropDeinitInst.self)
register(EndCOWMutationInst.self)
register(ClassifyBridgeObjectInst.self)
register(PartialApplyInst.self)
register(ApplyInst.self)
register(FunctionExtractIsolationInst.self)
register(ClassMethodInst.self)
register(SuperMethodInst.self)
register(ObjCMethodInst.self)
@@ -187,6 +224,8 @@ public func registerSILClasses() {
register(IsEscapingClosureInst.self)
register(AllocStackInst.self)
register(AllocVectorInst.self)
register(AllocPackInst.self)
register(AllocPackMetadataInst.self)
register(AllocRefInst.self)
register(AllocRefDynamicInst.self)
register(AllocBoxInst.self)

View File

@@ -48,8 +48,10 @@ bool swiftModulesInitialized() {
// Does return null if initializeSwiftModules() is never called.
SwiftMetatype SILNode::getSILNodeMetatype(SILNodeKind kind) {
SwiftMetatype metatype = nodeMetatypes[(unsigned)kind];
assert((!nodeMetatypesInitialized || metatype) &&
"no metatype for bridged SIL node");
if (nodeMetatypesInitialized && !metatype) {
llvm::errs() << "Instruction " << getSILInstructionName((SILInstructionKind)kind) << " not registered\n";
abort();
}
return metatype;
}
@@ -58,19 +60,6 @@ SwiftMetatype SILNode::getSILNodeMetatype(SILNodeKind kind) {
//===----------------------------------------------------------------------===//
static llvm::StringMap<SILNodeKind> valueNamesToKind;
static llvm::SmallPtrSet<SwiftMetatype, 4> unimplementedTypes;
// Utility to fill in a metatype of an "unimplemented" class for a whole range
// of class types.
static void setUnimplementedRange(SwiftMetatype metatype,
SILNodeKind from, SILNodeKind to) {
unimplementedTypes.insert(metatype);
for (unsigned kind = (unsigned)from; kind <= (unsigned)to; ++kind) {
assert((!nodeMetatypes[kind] || unimplementedTypes.count(metatype)) &&
"unimplemented nodes must be registered first");
nodeMetatypes[kind] = metatype;
}
}
/// Registers the metatype of a swift SIL class.
/// Called by initializeSwiftModules().
@@ -92,20 +81,6 @@ void registerBridgedClass(BridgedStringRef bridgedClassName, SwiftMetatype metat
return;
}
// Pre-populate the "unimplemented" ranges of metatypes.
// If a specific class is not implemented in Swift yet, it bridges to an
// "unimplemented" class. This ensures that optimizations handle _all_ kind of
// instructions gracefully, without the need to define the not-yet-used
// classes in Swift.
#define VALUE_RANGE(ID) SILNodeKind::First_##ID, SILNodeKind::Last_##ID
if (className == "UnimplementedRefCountingInst")
return setUnimplementedRange(metatype, VALUE_RANGE(RefCountingInst));
if (className == "UnimplementedSingleValueInst")
return setUnimplementedRange(metatype, VALUE_RANGE(SingleValueInstruction));
if (className == "UnimplementedInstruction")
return setUnimplementedRange(metatype, VALUE_RANGE(SILInstruction));
#undef VALUE_RANGE
if (valueNamesToKind.empty()) {
#define VALUE(ID, PARENT) \
valueNamesToKind[#ID] = SILNodeKind::ID;
@@ -134,7 +109,7 @@ void registerBridgedClass(BridgedStringRef bridgedClassName, SwiftMetatype metat
}
SILNodeKind kind = iter->second;
SwiftMetatype existingTy = nodeMetatypes[(unsigned)kind];
if (existingTy && !unimplementedTypes.count(existingTy)) {
if (existingTy) {
llvm::errs() << "Double registration of class " << className << '\n';
abort();
}