Skip to content

Commit 7743be3

Browse files
committed
Add a "token" type to SIL to allow dependencies to be expressed without
allowing abstraction.
1 parent 8f63e6c commit 7743be3

25 files changed

+81
-9
lines changed

docs/ABI/Mangling.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -317,6 +317,7 @@ Types
317317
type ::= 'BO' // Builtin.UnknownObject
318318
type ::= 'Bo' // Builtin.NativeObject
319319
type ::= 'Bp' // Builtin.RawPointer
320+
type ::= 'Bt' // Builtin.SILToken
320321
type ::= type 'Bv' NATURAL '_' // Builtin.Vec<n>x<type>
321322
type ::= 'Bw' // Builtin.Word
322323
type ::= function-signature 'c' // function type

include/swift/AST/ASTContext.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -573,6 +573,7 @@ class ASTContext {
573573
const CanType TheUnknownObjectType; /// Builtin.UnknownObject
574574
const CanType TheRawPointerType; /// Builtin.RawPointer
575575
const CanType TheUnsafeValueBufferType; /// Builtin.UnsafeValueBuffer
576+
const CanType TheSILTokenType; /// Builtin.SILToken
576577

577578
const CanType TheIEEE32Type; /// 32-bit IEEE floating point
578579
const CanType TheIEEE64Type; /// 64-bit IEEE floating point

include/swift/AST/TypeMatcher.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ class TypeMatcher {
109109
TRIVIAL_CASE(BuiltinUnknownObjectType)
110110
TRIVIAL_CASE(BuiltinUnsafeValueBufferType)
111111
TRIVIAL_CASE(BuiltinVectorType)
112+
TRIVIAL_CASE(SILTokenType)
112113

113114
bool visitTupleType(CanTupleType firstTuple, Type secondType,
114115
Type sugaredFirstType) {

include/swift/AST/TypeNodes.def

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -126,6 +126,7 @@ ABSTRACT_TYPE(AnyFunction, Type)
126126
ARTIFICIAL_TYPE(SILFunction, Type)
127127
ARTIFICIAL_TYPE(SILBlockStorage, Type)
128128
ARTIFICIAL_TYPE(SILBox, Type)
129+
ARTIFICIAL_TYPE(SILToken, Type)
129130
ABSTRACT_SUGARED_TYPE(SyntaxSugar, Type)
130131
SUGARED_TYPE(ArraySlice, SyntaxSugarType)
131132
SUGARED_TYPE(Optional, SyntaxSugarType)

include/swift/AST/Types.h

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3797,7 +3797,23 @@ class SILBlockStorageType : public TypeBase {
37973797
}
37983798
};
37993799
DEFINE_EMPTY_CAN_TYPE_WRAPPER(SILBlockStorageType, Type)
3800-
3800+
3801+
/// A singleton 'token' type, which establishes a formal dependency
3802+
/// between two SIL nodes. A token 'value' cannot be abstracted in
3803+
/// SIL: it cannot be returned, yielded, or passed as a function or
3804+
/// block argument.
3805+
class SILTokenType final : public TypeBase {
3806+
friend class ASTContext;
3807+
SILTokenType(const ASTContext &C)
3808+
: TypeBase(TypeKind::SILToken, &C, RecursiveTypeProperties()) {}
3809+
public:
3810+
// The singleton instance of this type is ASTContext::TheSILTokenType.
3811+
3812+
static bool classof(const TypeBase *T) {
3813+
return T->getKind() == TypeKind::SILToken;
3814+
}
3815+
};
3816+
DEFINE_EMPTY_CAN_TYPE_WRAPPER(SILTokenType, Type)
38013817

38023818
/// A type with a special syntax that is always sugar for a library type.
38033819
///

include/swift/SIL/SILType.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,6 +539,9 @@ class SILType {
539539
/// Get the standard exception type.
540540
static SILType getExceptionType(const ASTContext &C);
541541

542+
/// Get the SIL token type.
543+
static SILType getSILTokenType(const ASTContext &C);
544+
542545
//
543546
// Utilities for treating SILType as a pointer-like type.
544547
//

include/swift/Serialization/ModuleFormat.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ const uint16_t VERSION_MAJOR = 0;
5454
/// in source control, you should also update the comment to briefly
5555
/// describe what change you made. The content of this comment isn't important;
5656
/// it just ensures a conflict if two people change the module format.
57-
const uint16_t VERSION_MINOR = 382; // Last change: SIL_WITNESS_CONDITIONAL_CONFORMANCE
57+
const uint16_t VERSION_MINOR = 383; // Last change: Builtin.SILToken
5858

5959
using DeclIDField = BCFixed<31>;
6060

lib/AST/ASTContext.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,8 @@ ASTContext::ASTContext(LangOptions &langOpts, SearchPathOptions &SearchPathOpts,
461461
BuiltinRawPointerType(*this)),
462462
TheUnsafeValueBufferType(new (*this, AllocationArena::Permanent)
463463
BuiltinUnsafeValueBufferType(*this)),
464+
TheSILTokenType(new (*this, AllocationArena::Permanent)
465+
SILTokenType(*this)),
464466
TheIEEE32Type(new (*this, AllocationArena::Permanent)
465467
BuiltinFloatType(BuiltinFloatType::IEEE32,*this)),
466468
TheIEEE64Type(new (*this, AllocationArena::Permanent)

lib/AST/ASTDumper.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2926,6 +2926,7 @@ namespace {
29262926
TRIVIAL_TYPE_PRINTER(BuiltinBridgeObject, builtin_bridge_object)
29272927
TRIVIAL_TYPE_PRINTER(BuiltinUnknownObject, builtin_unknown_object)
29282928
TRIVIAL_TYPE_PRINTER(BuiltinUnsafeValueBuffer, builtin_unsafe_value_buffer)
2929+
TRIVIAL_TYPE_PRINTER(SILToken, sil_token)
29292930

29302931
void visitBuiltinVectorType(BuiltinVectorType *T, StringRef label) {
29312932
printCommon(label, "builtin_vector_type");

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -705,6 +705,8 @@ void ASTMangler::appendType(Type type) {
705705
return appendOperator("BO");
706706
case TypeKind::BuiltinUnsafeValueBuffer:
707707
return appendOperator("BB");
708+
case TypeKind::SILToken:
709+
return appendOperator("Bt");
708710
case TypeKind::BuiltinVector:
709711
appendType(cast<BuiltinVectorType>(tybase)->getElementType());
710712
return appendOperator("Bv",

lib/AST/ASTPrinter.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,10 @@ class TypePrinter : public TypeVisitor<TypePrinter> {
32553255
}
32563256
}
32573257

3258+
void visitSILTokenType(SILTokenType *T) {
3259+
Printer << "Builtin.SILToken";
3260+
}
3261+
32583262
void visitNameAliasType(NameAliasType *T) {
32593263
if (Options.PrintForSIL || Options.PrintNameAliasUnderlyingType) {
32603264
visit(T->getSinglyDesugaredType());

lib/AST/Builtins.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,8 @@ Type swift::getBuiltinType(ASTContext &Context, StringRef Name) {
8080
return Context.TheUnknownObjectType;
8181
if (Name == "BridgeObject")
8282
return Context.TheBridgeObjectType;
83+
if (Name == "SILToken")
84+
return Context.TheSILTokenType;
8385
if (Name == "UnsafeValueBuffer")
8486
return Context.TheUnsafeValueBufferType;
8587

lib/AST/Type.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,7 @@ bool CanType::isReferenceTypeImpl(CanType type, bool functionsCount) {
220220
case TypeKind::TypeVariable:
221221
case TypeKind::BoundGenericEnum:
222222
case TypeKind::BoundGenericStruct:
223+
case TypeKind::SILToken:
223224
case TypeKind::UnownedStorage:
224225
case TypeKind::UnmanagedStorage:
225226
case TypeKind::WeakStorage:
@@ -1157,6 +1158,7 @@ CanType TypeBase::getCanonicalType() {
11571158
case TypeKind::SILBlockStorage:
11581159
case TypeKind::SILBox:
11591160
case TypeKind::SILFunction:
1161+
case TypeKind::SILToken:
11601162
llvm_unreachable("SIL-only types are always canonical!");
11611163

11621164
case TypeKind::Function: {
@@ -1276,6 +1278,7 @@ TypeBase *TypeBase::getDesugaredType() {
12761278
case TypeKind::SILBlockStorage:
12771279
case TypeKind::SILBox:
12781280
case TypeKind::SILFunction:
1281+
case TypeKind::SILToken:
12791282
case TypeKind::LValue:
12801283
case TypeKind::InOut:
12811284
case TypeKind::ProtocolComposition:
@@ -1494,6 +1497,7 @@ bool TypeBase::isSpelledLike(Type other) {
14941497
case TypeKind::SILFunction:
14951498
case TypeKind::SILBlockStorage:
14961499
case TypeKind::SILBox:
1500+
case TypeKind::SILToken:
14971501
case TypeKind::GenericFunction: {
14981502
// Polymorphic function types should never be explicitly spelled.
14991503
return false;
@@ -3624,6 +3628,7 @@ case TypeKind::Id:
36243628
case TypeKind::Unresolved:
36253629
case TypeKind::TypeVariable:
36263630
case TypeKind::GenericTypeParam:
3631+
case TypeKind::SILToken:
36273632
return *this;
36283633

36293634
case TypeKind::Enum:
@@ -4209,6 +4214,7 @@ bool TypeBase::usesNativeReferenceCounting(ResilienceExpansion resilience) {
42094214
case TypeKind::TypeVariable:
42104215
case TypeKind::BoundGenericEnum:
42114216
case TypeKind::BoundGenericStruct:
4217+
case TypeKind::SILToken:
42124218
case TypeKind::UnownedStorage:
42134219
case TypeKind::UnmanagedStorage:
42144220
case TypeKind::WeakStorage:

lib/AST/TypeWalker.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ class Traversal : public TypeVisitor<Traversal, bool>
3636
bool visitUnresolvedType(UnresolvedType *ty) { return false; }
3737
bool visitBuiltinType(BuiltinType *ty) { return false; }
3838
bool visitNameAliasType(NameAliasType *ty) { return false; }
39+
bool visitSILTokenType(SILTokenType *ty) { return false; }
3940

4041
bool visitParenType(ParenType *ty) {
4142
return doIt(ty->getUnderlyingType());

lib/Demangling/Demangler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -778,6 +778,9 @@ NodePointer Demangler::demangleBuiltinType() {
778778
Ty = createNode(Node::Kind::BuiltinTypeName,
779779
"Builtin.RawPointer");
780780
break;
781+
case 't':
782+
Ty = createNode(Node::Kind::BuiltinTypeName, "Builtin.SILToken");
783+
break;
781784
case 'w':
782785
Ty = createNode(Node::Kind::BuiltinTypeName,
783786
"Builtin.Word");

lib/Demangling/OldDemangler.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1848,6 +1848,9 @@ class OldDemangler {
18481848
if (c == 'p')
18491849
return Factory.createNode(Node::Kind::BuiltinTypeName,
18501850
"Builtin.RawPointer");
1851+
if (c == 't')
1852+
return Factory.createNode(Node::Kind::BuiltinTypeName,
1853+
"Builtin.SILToken");
18511854
if (c == 'w')
18521855
return Factory.createNode(Node::Kind::BuiltinTypeName,
18531856
"Builtin.Word");

lib/Demangling/Remangler.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -579,6 +579,8 @@ void Remangler::mangleBuiltinTypeName(Node *node) {
579579
Buffer << 'o';
580580
} else if (text == "Builtin.RawPointer") {
581581
Buffer << 'p';
582+
} else if (text == "Builtin.SILToken") {
583+
Buffer << 't';
582584
} else if (text == "Builtin.Word") {
583585
Buffer << 'w';
584586
} else if (stripPrefix(text, "Builtin.Int")) {

lib/IRGen/GenMeta.cpp

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -981,6 +981,9 @@ namespace {
981981
llvm_unreachable("should not be asking for metadata of a lowered SIL "
982982
"function type--SILGen should have used the AST type");
983983
}
984+
llvm::Value *visitSILTokenType(CanSILTokenType type) {
985+
llvm_unreachable("should not be asking for metadata of a SILToken type");
986+
}
984987

985988
llvm::Value *visitArchetypeType(CanArchetypeType type) {
986989
return emitArchetypeTypeMetadataRef(IGF, type);

lib/IRGen/GenType.cpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1636,6 +1636,8 @@ TypeCacheEntry TypeConverter::convertType(CanType ty) {
16361636
return convertBlockStorageType(cast<SILBlockStorageType>(ty));
16371637
case TypeKind::SILBox:
16381638
return convertBoxType(cast<SILBoxType>(ty));
1639+
case TypeKind::SILToken:
1640+
llvm_unreachable("should not be asking for representation of a SILToken");
16391641
}
16401642
}
16411643
llvm_unreachable("bad type kind");

lib/IRGen/IRGenDebugInfo.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1333,6 +1333,7 @@ class IRGenDebugInfoImpl : public IRGenDebugInfo {
13331333
case TypeKind::Module:
13341334
case TypeKind::SILBlockStorage:
13351335
case TypeKind::SILBox:
1336+
case TypeKind::SILToken:
13361337
case TypeKind::BuiltinUnsafeValueBuffer:
13371338

13381339
DEBUG(llvm::errs() << "Unhandled type: "; DbgTy.getType()->dump();

lib/SIL/SILType.cpp

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ SILType SILType::getOptionalType(SILType type) {
7171
return getPrimitiveType(CanType(optType), type.getCategory());
7272
}
7373

74+
SILType SILType::getSILTokenType(const ASTContext &C) {
75+
return getPrimitiveObjectType(C.TheSILTokenType);
76+
}
77+
7478
bool SILType::isTrivial(SILModule &M) const {
7579
return M.getTypeLowering(*this).isTrivial();
7680
}

lib/SIL/SILVerifier.cpp

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -671,9 +671,20 @@ class SILVerifier : public SILVerifierBase<SILVerifier> {
671671
/// Check that the types of this value producer are all legal in the function
672672
/// context in which it exists.
673673
void checkLegalType(SILFunction *F, ValueBase *value, SILInstruction *I) {
674-
if (SILType type = value->getType()) {
675-
checkLegalType(F, type, I);
674+
SILType type = value->getType();
675+
if (type.is<SILTokenType>()) {
676+
require(isLegalSILTokenProducer(value),
677+
"SIL tokens can only be produced as the results of specific "
678+
"instructions");
679+
return;
676680
}
681+
682+
checkLegalType(F, type, I);
683+
}
684+
685+
static bool isLegalSILTokenProducer(SILValue value) {
686+
// TODO: add cases here
687+
return false;
677688
}
678689

679690
/// Check that the given type is a legal SIL value type.

lib/SIL/TypeLowering.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,7 @@ namespace {
211211
IMPL(BuiltinUnknownObject, Reference)
212212
IMPL(BuiltinUnsafeValueBuffer, AddressOnly)
213213
IMPL(BuiltinVector, Trivial)
214+
IMPL(SILToken, Trivial)
214215
IMPL(Class, Reference)
215216
IMPL(BoundGenericClass, Reference)
216217
IMPL(AnyMetatype, Trivial)

lib/Serialization/Serialization.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3422,12 +3422,11 @@ static uint8_t getRawStableResultConvention(swift::ResultConvention rc) {
34223422
#undef SIMPLE_CASE
34233423

34243424
/// Find the typealias given a builtin type.
3425-
static TypeAliasDecl *findTypeAliasForBuiltin(ASTContext &Ctx,
3426-
BuiltinType *Bt) {
3425+
static TypeAliasDecl *findTypeAliasForBuiltin(ASTContext &Ctx, Type T) {
34273426
/// Get the type name by chopping off "Builtin.".
34283427
llvm::SmallString<32> FullName;
34293428
llvm::raw_svector_ostream OS(FullName);
3430-
Bt->print(OS);
3429+
T->print(OS);
34313430
assert(FullName.startswith("Builtin."));
34323431
StringRef TypeName = FullName.substr(8);
34333432

@@ -3463,9 +3462,10 @@ void Serializer::writeType(Type ty) {
34633462
case TypeKind::BuiltinBridgeObject:
34643463
case TypeKind::BuiltinUnknownObject:
34653464
case TypeKind::BuiltinUnsafeValueBuffer:
3466-
case TypeKind::BuiltinVector: {
3465+
case TypeKind::BuiltinVector:
3466+
case TypeKind::SILToken: {
34673467
TypeAliasDecl *typeAlias =
3468-
findTypeAliasForBuiltin(M->getASTContext(), ty->castTo<BuiltinType>());
3468+
findTypeAliasForBuiltin(M->getASTContext(), ty);
34693469

34703470
unsigned abbrCode = DeclTypeAbbrCodes[NameAliasTypeLayout::Code];
34713471
NameAliasTypeLayout::emitRecord(Out, ScratchRecord, abbrCode,

test/Demangle/Inputs/manglings.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ _TtBw ---> Builtin.Word
44
_TtBO ---> Builtin.UnknownObject
55
_TtBo ---> Builtin.NativeObject
66
_TtBp ---> Builtin.RawPointer
7+
_TtBt ---> Builtin.SILToken
78
_TtBv4Bi8_ ---> Builtin.Vec4xInt8
89
_TtBv4Bf16_ ---> Builtin.Vec4xFloat16
910
_TtBv4Bp ---> Builtin.Vec4xRawPointer

0 commit comments

Comments
 (0)