Skip to content

Commit 09d122a

Browse files
authored
Merge pull request #76438 from Azoy/vector
[stdlib] Slab
2 parents 999669c + 7f5f2dc commit 09d122a

Some content is hidden

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

49 files changed

+1342
-74
lines changed

include/swift/AST/Decl.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -3948,7 +3948,7 @@ class GenericTypeParamDecl final
39483948
/// type parameter.
39493949
///
39503950
/// \code
3951-
/// struct Vector<Element, let N: Int>
3951+
/// struct Slab<let count: Int, Element: ~Copyable>
39523952
/// \endcode
39533953
bool isValue() const {
39543954
return getParamKind() == GenericTypeParamKind::Value;

include/swift/AST/DiagnosticsSema.def

+7
Original file line numberDiff line numberDiff line change
@@ -8165,6 +8165,13 @@ ERROR(availability_value_generic_type_only_version_newer, none,
81658165
ERROR(invalid_value_for_type_same_type,none,
81668166
"cannot constrain type parameter %0 to be integer %1", (Type, Type))
81678167

8168+
//===----------------------------------------------------------------------===//
8169+
// MARK: Slab
8170+
//===----------------------------------------------------------------------===//
8171+
8172+
ERROR(slab_literal_incorrect_count,none,
8173+
"expected %0 elements in slab literal, but got %1", (Type, Type))
8174+
81688175
//===----------------------------------------------------------------------===//
81698176
// MARK: @abi Attribute
81708177
//===----------------------------------------------------------------------===//

include/swift/AST/KnownStdlibTypes.def

+2
Original file line numberDiff line numberDiff line change
@@ -97,4 +97,6 @@ KNOWN_STDLIB_TYPE_DECL(DecodingError, NominalTypeDecl, 0)
9797

9898
KNOWN_STDLIB_TYPE_DECL(Result, NominalTypeDecl, 2)
9999

100+
KNOWN_STDLIB_TYPE_DECL(Slab, NominalTypeDecl, 2)
101+
100102
#undef KNOWN_STDLIB_TYPE_DECL

include/swift/AST/Types.h

+1-1
Original file line numberDiff line numberDiff line change
@@ -7246,7 +7246,7 @@ class GenericTypeParamType : public SubstitutableType,
72467246
/// Returns \c true if this type parameter is declared as a value.
72477247
///
72487248
/// \code
7249-
/// struct Vector<Element, let N: Int>
7249+
/// struct Slab<let count: Int, Element: ~Copyable>
72507250
/// \endcode
72517251
bool isValue() const {
72527252
return ParamKind == GenericTypeParamKind::Value;

include/swift/RemoteInspection/TypeLowering.h

+19
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,7 @@ enum class TypeInfoKind : unsigned {
117117
Reference,
118118
Invalid,
119119
Enum,
120+
Array,
120121
};
121122

122123
class TypeInfo {
@@ -350,6 +351,24 @@ class ReferenceTypeInfo : public TypeInfo {
350351
}
351352
};
352353

354+
/// Array based layouts like Builtin.FixedArray<N, T>
355+
class ArrayTypeInfo : public TypeInfo {
356+
const TypeInfo *ElementTI;
357+
358+
public:
359+
explicit ArrayTypeInfo(intptr_t size, const TypeInfo *elementTI);
360+
361+
bool readExtraInhabitantIndex(remote::MemoryReader &reader,
362+
remote::RemoteAddress address,
363+
int *extraInhabitantIndex) const override;
364+
365+
BitMask getSpareBits(TypeConverter &TC, bool &hasAddrOnly) const override;
366+
367+
static bool classof(const TypeInfo *TI) {
368+
return TI->getKind() == TypeInfoKind::Array;
369+
}
370+
};
371+
353372
/// This class owns the memory for all TypeInfo instances that it vends.
354373
class TypeConverter {
355374
TypeRefBuilder &Builder;

include/swift/RemoteInspection/TypeRef.h

+62
Original file line numberDiff line numberDiff line change
@@ -1095,6 +1095,68 @@ class SILBoxTypeWithLayoutTypeRef final : public TypeRef {
10951095
}
10961096
};
10971097

1098+
class IntegerTypeRef final : public TypeRef {
1099+
intptr_t Value;
1100+
1101+
static TypeRefID Profile(const intptr_t &Value) {
1102+
TypeRefID ID;
1103+
ID.addInteger((uint64_t)Value);
1104+
return ID;
1105+
}
1106+
1107+
public:
1108+
IntegerTypeRef(const intptr_t &Value)
1109+
: TypeRef(TypeRefKind::Integer), Value(Value) {}
1110+
1111+
template <typename Allocator>
1112+
static const IntegerTypeRef *create(Allocator &A, intptr_t Value) {
1113+
FIND_OR_CREATE_TYPEREF(A, IntegerTypeRef, Value);
1114+
}
1115+
1116+
const intptr_t &getValue() const {
1117+
return Value;
1118+
}
1119+
1120+
static bool classof(const TypeRef *TR) {
1121+
return TR->getKind() == TypeRefKind::Integer;
1122+
}
1123+
};
1124+
1125+
class BuiltinFixedArrayTypeRef final : public TypeRef {
1126+
const TypeRef *Size;
1127+
const TypeRef *Element;
1128+
1129+
static TypeRefID Profile(const TypeRef *Size, const TypeRef *Element) {
1130+
TypeRefID ID;
1131+
ID.addPointer(Size);
1132+
ID.addPointer(Element);
1133+
return ID;
1134+
}
1135+
1136+
public:
1137+
BuiltinFixedArrayTypeRef(const TypeRef *Size, const TypeRef *Element)
1138+
: TypeRef(TypeRefKind::BuiltinFixedArray), Size(Size), Element(Element) {}
1139+
1140+
template <typename Allocator>
1141+
static const BuiltinFixedArrayTypeRef *create(Allocator &A,
1142+
const TypeRef *Size,
1143+
const TypeRef *Element) {
1144+
FIND_OR_CREATE_TYPEREF(A, BuiltinFixedArrayTypeRef, Size, Element);
1145+
}
1146+
1147+
const TypeRef *getSizeType() const {
1148+
return Size;
1149+
}
1150+
1151+
const TypeRef *getElementType() const {
1152+
return Element;
1153+
}
1154+
1155+
static bool classof(const TypeRef *TR) {
1156+
return TR->getKind() == TypeRefKind::BuiltinFixedArray;
1157+
}
1158+
};
1159+
10981160
template <typename ImplClass, typename RetTy = void, typename... Args>
10991161
class TypeRefVisitor {
11001162
public:

include/swift/RemoteInspection/TypeRefBuilder.h

+3-6
Original file line numberDiff line numberDiff line change
@@ -929,19 +929,16 @@ class TypeRefBuilder {
929929
}
930930

931931
const TypeRef *createIntegerType(intptr_t value) {
932-
// FIXME: implement
933-
return nullptr;
932+
return IntegerTypeRef::create(*this, value);
934933
}
935934

936935
const TypeRef *createNegativeIntegerType(intptr_t value) {
937-
// FIXME: implement
938-
return nullptr;
936+
return IntegerTypeRef::create(*this, value);
939937
}
940938

941939
const TypeRef *createBuiltinFixedArrayType(const TypeRef *size,
942940
const TypeRef *element) {
943-
// FIXME: implement
944-
return nullptr;
941+
return BuiltinFixedArrayTypeRef::create(*this, size, element);
945942
}
946943

947944
// Construct a bound generic type ref along with the parent type info

include/swift/RemoteInspection/TypeRefs.def

+2
Original file line numberDiff line numberDiff line change
@@ -37,5 +37,7 @@ TYPEREF(OpaqueArchetype, TypeRef)
3737
#include "swift/AST/ReferenceStorage.def"
3838
TYPEREF(SILBox, TypeRef)
3939
TYPEREF(SILBoxTypeWithLayout, TypeRef)
40+
TYPEREF(Integer, TypeRef)
41+
TYPEREF(BuiltinFixedArray, TypeRef)
4042

4143
#undef TYPEREF

include/swift/Sema/CSFix.h

+28
Original file line numberDiff line numberDiff line change
@@ -483,6 +483,10 @@ enum class FixKind : uint8_t {
483483
/// sending result, but is passed a function typed parameter without a sending
484484
/// result.
485485
AllowSendingMismatch,
486+
487+
/// Ignore when a 'Slab' literal has mismatched number of elements to the
488+
/// type it's attempting to bind to.
489+
AllowSlabLiteralCountMismatch,
486490
};
487491

488492
class ConstraintFix {
@@ -3837,6 +3841,30 @@ class IgnoreKeyPathSubscriptIndexMismatch final : public ConstraintFix {
38373841
}
38383842
};
38393843

3844+
class AllowSlabLiteralCountMismatch final : public ConstraintFix {
3845+
Type lhsCount, rhsCount;
3846+
3847+
AllowSlabLiteralCountMismatch(ConstraintSystem &cs, Type lhsCount,
3848+
Type rhsCount, ConstraintLocator *locator)
3849+
: ConstraintFix(cs, FixKind::AllowSlabLiteralCountMismatch, locator),
3850+
lhsCount(lhsCount), rhsCount(rhsCount) {}
3851+
3852+
public:
3853+
std::string getName() const override {
3854+
return "allow vector literal count mismatch";
3855+
}
3856+
3857+
bool diagnose(const Solution &solution, bool asNote = false) const override;
3858+
3859+
static AllowSlabLiteralCountMismatch *
3860+
create(ConstraintSystem &cs, Type lhsCount, Type rhsCount,
3861+
ConstraintLocator *locator);
3862+
3863+
static bool classof(const ConstraintFix *fix) {
3864+
return fix->getKind() == FixKind::AllowSlabLiteralCountMismatch;
3865+
}
3866+
};
3867+
38403868
} // end namespace constraints
38413869
} // end namespace swift
38423870

include/swift/SwiftRemoteMirror/SwiftRemoteMirrorTypes.h

+3
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,9 @@ typedef enum swift_layout_kind {
143143
// swift_reflection_infoForTypeRef().
144144
SWIFT_CLASS_INSTANCE,
145145
SWIFT_CLOSURE_CONTEXT,
146+
147+
// A contiguous list of N Ts, typically for Builtin.FixedArray<N, T>.
148+
SWIFT_ARRAY,
146149
} swift_layout_kind_t;
147150

148151
struct swift_childinfo;

lib/AST/ASTDumper.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1277,6 +1277,7 @@ namespace {
12771277
break;
12781278
case GenericTypeParamKind::Value:
12791279
printField((StringRef)"value", "param_kind");
1280+
printRec(decl->getValueType(), "value_type");
12801281
break;
12811282
}
12821283
printAttributes(decl);

lib/AST/Builtins.cpp

+26-6
Original file line numberDiff line numberDiff line change
@@ -442,7 +442,7 @@ enum class BuiltinThrowsKind : uint8_t {
442442
static FuncDecl *getBuiltinGenericFunction(
443443
Identifier Id, ArrayRef<AnyFunctionType::Param> ArgParamTypes, Type ResType,
444444
GenericParamList *GenericParams, GenericSignature Sig, bool Async,
445-
BuiltinThrowsKind Throws, bool SendingResult) {
445+
BuiltinThrowsKind Throws, Type ThrownError, bool SendingResult) {
446446
assert(GenericParams && "Missing generic parameters");
447447
auto &Context = ResType->getASTContext();
448448

@@ -471,7 +471,7 @@ static FuncDecl *getBuiltinGenericFunction(
471471
Context, StaticSpellingKind::None, Name,
472472
/*NameLoc=*/SourceLoc(),
473473
Async,
474-
Throws != BuiltinThrowsKind::None, /*thrownType=*/Type(),
474+
Throws != BuiltinThrowsKind::None, ThrownError,
475475
GenericParams, paramList, ResType, DC);
476476

477477
func->setSendingResult(SendingResult);
@@ -696,6 +696,7 @@ namespace {
696696
Type InterfaceResult;
697697
bool Async = false;
698698
BuiltinThrowsKind Throws = BuiltinThrowsKind::None;
699+
Type ThrownError;
699700
bool SendingResult = false;
700701

701702
// Accumulate params and requirements here, so that we can call
@@ -740,6 +741,11 @@ namespace {
740741
InterfaceResult = generator.build(*this);
741742
}
742743

744+
template <class G>
745+
void setThrownError(const G &generator) {
746+
ThrownError = generator.build(*this);
747+
}
748+
743749
template <class G>
744750
void addConformanceRequirement(const G &generator, KnownProtocolKind kp) {
745751
addConformanceRequirement(generator, Context.getProtocol(kp));
@@ -776,7 +782,7 @@ namespace {
776782
/*allowInverses=*/false);
777783
return getBuiltinGenericFunction(name, InterfaceParams, InterfaceResult,
778784
TheGenericParamList, GenericSig, Async,
779-
Throws, SendingResult);
785+
Throws, ThrownError, SendingResult);
780786
}
781787

782788
// Don't use these generator classes directly; call the make{...}
@@ -2231,17 +2237,31 @@ static ValueDecl *getAddressOfRawLayout(ASTContext &ctx, Identifier id) {
22312237
}
22322238

22332239
static ValueDecl *getEmplace(ASTContext &ctx, Identifier id) {
2234-
BuiltinFunctionBuilder builder(ctx, /* genericParamCount */ 1);
2240+
BuiltinFunctionBuilder builder(ctx, /* genericParamCount */ 2);
22352241

2236-
auto T = makeGenericParam();
2242+
// <T: ~Copyable, E: Error>(
2243+
// _: (Builtin.RawPointer) throws(E) -> ()
2244+
// ) throws(E) -> T
2245+
2246+
auto T = makeGenericParam(0);
22372247
builder.addConformanceRequirement(T, KnownProtocolKind::Escapable);
22382248

2249+
auto E = makeGenericParam(1);
2250+
builder.addConformanceRequirement(E, KnownProtocolKind::Error);
2251+
2252+
auto extInfo = ASTExtInfoBuilder()
2253+
.withNoEscape()
2254+
.withThrows(/* throws */ true, E.build(builder))
2255+
.build();
2256+
22392257
auto fnParamTy = FunctionType::get(FunctionType::Param(ctx.TheRawPointerType),
22402258
ctx.TheEmptyTupleType,
2241-
ASTExtInfo().withNoEscape());
2259+
extInfo);
22422260

22432261
builder.addParameter(makeConcrete(fnParamTy), ParamSpecifier::Borrowing);
22442262
builder.setResult(T);
2263+
builder.setThrows();
2264+
builder.setThrownError(E);
22452265

22462266
return builder.build(id);
22472267
}

lib/AST/GenericParamList.cpp

+7
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
#include "swift/AST/GenericParamList.h"
1818

1919
#include "swift/AST/ASTContext.h"
20+
#include "swift/AST/TypeCheckRequests.h"
2021
#include "swift/AST/TypeRepr.h"
2122
#include "swift/Basic/Assertions.h"
2223

@@ -78,6 +79,12 @@ GenericParamList::clone(DeclContext *dc) const {
7879
dc, param->getName(), GenericTypeParamDecl::InvalidDepth,
7980
param->getIndex(), param->getParamKind(), param->getOpaqueTypeRepr());
8081
newParam->setInherited(param->getInherited().getEntries());
82+
83+
// Cache the value type computed from the previous param to the new one.
84+
ctx.evaluator.cacheOutput(
85+
GenericTypeParamDeclGetValueTypeRequest{newParam},
86+
param->getValueType());
87+
8188
params.push_back(newParam);
8289
}
8390

lib/AST/Type.cpp

+4-1
Original file line numberDiff line numberDiff line change
@@ -808,8 +808,11 @@ CanType CanType::wrapInOptionalTypeImpl(CanType type) {
808808

809809
Type TypeBase::isArrayType() {
810810
if (auto boundStruct = getAs<BoundGenericStructType>()) {
811-
if (boundStruct->getDecl() == getASTContext().getArrayDecl())
811+
if (isArray())
812812
return boundStruct->getGenericArgs()[0];
813+
814+
if (isSlab())
815+
return boundStruct->getGenericArgs()[1];
813816
}
814817
return Type();
815818
}

lib/ClangImporter/ImportType.cpp

+2-1
Original file line numberDiff line numberDiff line change
@@ -645,12 +645,13 @@ namespace {
645645
return Type();
646646

647647
auto size = type->getSize().getZExtValue();
648+
648649
// An array of size N is imported as an N-element tuple which
649650
// takes very long to compile. We chose 4096 as the upper limit because
650651
// we don't want to break arrays of size PATH_MAX.
651652
if (size > 4096)
652653
return Type();
653-
654+
654655
if (size == 1)
655656
return elementType;
656657

0 commit comments

Comments
 (0)