Skip to content

Commit 8cfb029

Browse files
committed
[sil] Make SILFunctionTypeInfo a struct enum.
I am doing this in preparation for adding the ability to represent in the SIL type system that a function is global actor isolated. Since we have isolated parameters in SIL, we do not need to represent parameter, nonisolated, or nonisolated caller in the type system. So this should be sufficient for our purposes. I am adding this since I need to ensure that we mangle into thunks that convert execution(caller) functions to `global actor` functions what the global actor is. Otherwise, we cannot tell the difference in between such a thunk and a thunk that converts execution(caller) to execution(concurrent).
1 parent 4da795f commit 8cfb029

File tree

12 files changed

+96
-54
lines changed

12 files changed

+96
-54
lines changed

include/swift/AST/ExtInfo.h

Lines changed: 67 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -144,14 +144,58 @@ class FunctionTypeIsolation {
144144
/// are significantly reduced compared to AST function types.
145145
/// Isolation is not part of the SIL function model after the
146146
/// early portion of the pipeline.
147-
enum class SILFunctionTypeIsolation {
148-
/// We don't normally record isolation in SIL function types,
149-
/// so the empty case here is "unknown".
150-
Unknown,
151-
152-
/// The isolation of the function has been statically erased.
153-
/// This corresponds to @isolated(any).
154-
Erased,
147+
class SILFunctionTypeIsolation {
148+
public:
149+
enum Kind : uint8_t {
150+
/// We don't normally record isolation in SIL function types,
151+
/// so the empty case here is "unknown".
152+
Unknown,
153+
154+
/// The isolation of the function has been statically erased.
155+
/// This corresponds to @isolated(any).
156+
Erased,
157+
};
158+
159+
static constexpr size_t NumBits = 3; // future-proof this slightly
160+
static constexpr uintptr_t Mask = (uintptr_t(1) << NumBits) - 1;
161+
162+
private:
163+
// We do not use a pointer int pair, since it is not a literal type.
164+
llvm::PointerIntPair<CanType, NumBits, Kind> value;
165+
166+
SILFunctionTypeIsolation(Kind kind, CanType type = CanType())
167+
: value(type, kind) {}
168+
169+
public:
170+
static SILFunctionTypeIsolation forUnknown() { return {Kind::Unknown}; }
171+
172+
static SILFunctionTypeIsolation forErased() { return {Kind::Erased}; }
173+
174+
bool operator==(const SILFunctionTypeIsolation &other) const {
175+
if (getKind() != other.getKind())
176+
return false;
177+
178+
switch (getKind()) {
179+
case Kind::Unknown:
180+
case Kind::Erased:
181+
return true;
182+
}
183+
}
184+
185+
Kind getKind() const { return value.getInt(); }
186+
187+
bool isUnknown() const { return getKind() == Kind::Unknown; }
188+
bool isErased() const { return getKind() == Kind::Erased; }
189+
190+
// The opaque accessors below are just for the benefit of SILExtInfoBuilder,
191+
// which finds it convenient to break down the type separately. Normal
192+
// clients should use the accessors above.
193+
194+
CanType getOpaqueType() const { return value.getPointer(); }
195+
196+
static SILFunctionTypeIsolation fromOpaqueValues(Kind kind, CanType type) {
197+
return SILFunctionTypeIsolation(kind, type);
198+
}
155199
};
156200

157201
// MARK: - ClangTypeInfo
@@ -967,17 +1011,16 @@ class SILExtInfoBuilder {
9671011
: bits(bits), clangTypeInfo(clangTypeInfo.getCanonical()),
9681012
lifetimeDependencies(lifetimeDependencies) {}
9691013

970-
static constexpr unsigned makeBits(Representation rep, bool isPseudogeneric,
971-
bool isNoEscape, bool isSendable,
972-
bool isAsync, bool isUnimplementable,
973-
SILFunctionTypeIsolation isolation,
974-
DifferentiabilityKind diffKind) {
1014+
static unsigned makeBits(Representation rep, bool isPseudogeneric,
1015+
bool isNoEscape, bool isSendable, bool isAsync,
1016+
bool isUnimplementable,
1017+
SILFunctionTypeIsolation isolation,
1018+
DifferentiabilityKind diffKind) {
9751019
return ((unsigned)rep) | (isPseudogeneric ? PseudogenericMask : 0) |
9761020
(isNoEscape ? NoEscapeMask : 0) | (isSendable ? SendableMask : 0) |
9771021
(isAsync ? AsyncMask : 0) |
9781022
(isUnimplementable ? UnimplementableMask : 0) |
979-
(isolation == SILFunctionTypeIsolation::Erased ? ErasedIsolationMask
980-
: 0) |
1023+
(isolation.isErased() ? ErasedIsolationMask : 0) |
9811024
(((unsigned)diffKind << DifferentiabilityMaskOffset) &
9821025
DifferentiabilityMask);
9831026
}
@@ -988,7 +1031,7 @@ class SILExtInfoBuilder {
9881031
SILExtInfoBuilder()
9891032
: SILExtInfoBuilder(
9901033
makeBits(SILFunctionTypeRepresentation::Thick, false, false, false,
991-
false, false, SILFunctionTypeIsolation::Unknown,
1034+
false, false, SILFunctionTypeIsolation::forUnknown(),
9921035
DifferentiabilityKind::NonDifferentiable),
9931036
ClangTypeInfo(nullptr), /*LifetimeDependenceInfo*/ std::nullopt) {}
9941037

@@ -1008,8 +1051,8 @@ class SILExtInfoBuilder {
10081051
info.isNoEscape(), info.isSendable(),
10091052
info.isAsync(), /*unimplementable*/ false,
10101053
info.getIsolation().isErased()
1011-
? SILFunctionTypeIsolation::Erased
1012-
: SILFunctionTypeIsolation::Unknown,
1054+
? SILFunctionTypeIsolation::forErased()
1055+
: SILFunctionTypeIsolation::forUnknown(),
10131056
info.getDifferentiabilityKind()),
10141057
info.getClangTypeInfo(),
10151058
info.getLifetimeDependencies()) {}
@@ -1059,10 +1102,9 @@ class SILExtInfoBuilder {
10591102
return bits & ErasedIsolationMask;
10601103
}
10611104

1062-
constexpr SILFunctionTypeIsolation getIsolation() const {
1063-
return hasErasedIsolation()
1064-
? SILFunctionTypeIsolation::Erased
1065-
: SILFunctionTypeIsolation::Unknown;
1105+
SILFunctionTypeIsolation getIsolation() const {
1106+
return hasErasedIsolation() ? SILFunctionTypeIsolation::forErased()
1107+
: SILFunctionTypeIsolation::forUnknown();
10661108
}
10671109

10681110
/// Get the underlying ClangTypeInfo value.
@@ -1157,7 +1199,7 @@ class SILExtInfoBuilder {
11571199
}
11581200
[[nodiscard]]
11591201
SILExtInfoBuilder withIsolation(SILFunctionTypeIsolation isolation) const {
1160-
switch (isolation) {
1202+
switch (isolation.getKind()) {
11611203
case SILFunctionTypeIsolation::Unknown:
11621204
return *this;
11631205
case SILFunctionTypeIsolation::Erased:
@@ -1241,7 +1283,7 @@ class SILExtInfo {
12411283
static SILExtInfo getThin() {
12421284
return SILExtInfoBuilder(
12431285
SILExtInfoBuilder::Representation::Thin, false, false, false,
1244-
false, false, SILFunctionTypeIsolation::Unknown,
1286+
false, false, SILFunctionTypeIsolation::forUnknown(),
12451287
DifferentiabilityKind::NonDifferentiable, nullptr, {})
12461288
.build();
12471289
}
@@ -1276,7 +1318,7 @@ class SILExtInfo {
12761318
constexpr bool hasErasedIsolation() const {
12771319
return builder.hasErasedIsolation();
12781320
}
1279-
constexpr SILFunctionTypeIsolation getIsolation() const {
1321+
SILFunctionTypeIsolation getIsolation() const {
12801322
return builder.getIsolation();
12811323
}
12821324

include/swift/SIL/ApplySite.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -368,7 +368,7 @@ class ApplySite {
368368
unsigned appliedArgIndex = getAppliedArgIndex(oper);
369369
if (auto *pai = dyn_cast<PartialApplyInst>(Inst)) {
370370
if (pai->getFunctionType()->getIsolation() ==
371-
SILFunctionTypeIsolation::Erased) {
371+
SILFunctionTypeIsolation::forErased()) {
372372
assert(appliedArgIndex != 0 &&
373373
"isolation(any) does not correspond to an AST argument");
374374
appliedArgIndex -= 1;

include/swift/SIL/SILBridgingImpl.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1276,7 +1276,8 @@ bool BridgedInstruction::PartialApplyInst_isOnStack() const {
12761276
}
12771277

12781278
bool BridgedInstruction::PartialApplyInst_hasUnknownResultIsolation() const {
1279-
return getAs<swift::PartialApplyInst>()->getResultIsolation() == swift::SILFunctionTypeIsolation::Unknown;
1279+
return getAs<swift::PartialApplyInst>()->getResultIsolation() ==
1280+
swift::SILFunctionTypeIsolation::forUnknown();
12801281
}
12811282

12821283
bool BridgedInstruction::AllocStackInst_hasDynamicLifetime() const {
@@ -2264,16 +2265,15 @@ BridgedInstruction BridgedBuilder::createPartialApply(BridgedValue funcRef,
22642265
BridgedSubstitutionMap bridgedSubstitutionMap,
22652266
bool hasUnknownIsolation,
22662267
bool isOnStack) const {
2267-
llvm::SmallVector<swift::SILValue, 8> capturedArgs;
2268+
llvm::SmallVector<swift::SILValue, 8> capturedArgs;
22682269
return {unbridged().createPartialApply(
2269-
regularLoc(),
2270-
funcRef.getSILValue(),
2271-
bridgedSubstitutionMap.unbridged(),
2272-
bridgedCapturedArgs.getValues(capturedArgs),
2273-
getParameterConvention(calleeConvention),
2274-
hasUnknownIsolation ? swift::SILFunctionTypeIsolation::Unknown : swift::SILFunctionTypeIsolation::Erased,
2275-
isOnStack ? swift:: PartialApplyInst::OnStack : swift::PartialApplyInst::NotOnStack
2276-
)};
2270+
regularLoc(), funcRef.getSILValue(), bridgedSubstitutionMap.unbridged(),
2271+
bridgedCapturedArgs.getValues(capturedArgs),
2272+
getParameterConvention(calleeConvention),
2273+
hasUnknownIsolation ? swift::SILFunctionTypeIsolation::forUnknown()
2274+
: swift::SILFunctionTypeIsolation::forErased(),
2275+
isOnStack ? swift::PartialApplyInst::OnStack
2276+
: swift::PartialApplyInst::NotOnStack)};
22772277
}
22782278

22792279
BridgedInstruction BridgedBuilder::createBranch(BridgedBasicBlock destBlock, BridgedValueArray arguments) const {

include/swift/SIL/SILBuilder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -563,7 +563,7 @@ class SILBuilder {
563563
SILLocation Loc, SILValue Fn, SubstitutionMap Subs,
564564
ArrayRef<SILValue> Args, ParameterConvention CalleeConvention,
565565
SILFunctionTypeIsolation ResultIsolation =
566-
SILFunctionTypeIsolation::Unknown,
566+
SILFunctionTypeIsolation::forUnknown(),
567567
PartialApplyInst::OnStackKind OnStack =
568568
PartialApplyInst::OnStackKind::NotOnStack,
569569
const GenericSpecializationInformation *SpecializationInfo = nullptr) {

lib/AST/ASTDemangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -647,9 +647,9 @@ Type ASTBuilder::createImplFunctionType(
647647
#undef SIMPLE_CASE
648648
}
649649

650-
auto isolation = SILFunctionTypeIsolation::Unknown;
650+
auto isolation = SILFunctionTypeIsolation::forUnknown();
651651
if (flags.hasErasedIsolation())
652-
isolation = SILFunctionTypeIsolation::Erased;
652+
isolation = SILFunctionTypeIsolation::forErased();
653653

654654
// There's no representation of this in the mangling because it can't
655655
// occur in well-formed programs.

lib/AST/ASTMangler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -596,7 +596,7 @@ std::string ASTMangler::mangleReabstractionThunkHelper(
596596
appendOperator("Ty");
597597
else
598598
appendOperator("TR");
599-
599+
600600
if (GlobalActorBound) {
601601
appendType(GlobalActorBound, GenSig);
602602
appendOperator("TU");
@@ -2289,7 +2289,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
22892289
if (!fn->isNoEscape())
22902290
OpArgs.push_back('e');
22912291

2292-
switch (fn->getIsolation()) {
2292+
switch (fn->getIsolation().getKind()) {
22932293
case SILFunctionTypeIsolation::Unknown:
22942294
break;
22952295
case SILFunctionTypeIsolation::Erased:

lib/SIL/IR/SILPrinter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1694,7 +1694,7 @@ class SILPrinter : public SILInstructionVisitor<SILPrinter> {
16941694
case ParameterConvention::Pack_Inout:
16951695
llvm_unreachable("unexpected callee convention!");
16961696
}
1697-
switch (fnType->getIsolation()) {
1697+
switch (fnType->getIsolation().getKind()) {
16981698
case SILFunctionTypeIsolation::Unknown:
16991699
break;
17001700
case SILFunctionTypeIsolation::Erased:

lib/SIL/Parser/ParseSIL.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6797,7 +6797,7 @@ bool SILParser::parseCallInstruction(SILLocation InstLoc,
67976797
SmallVector<UnresolvedValueName, 4> ArgNames;
67986798

67996799
auto PartialApplyConvention = ParameterConvention::Direct_Owned;
6800-
auto PartialApplyIsolation = SILFunctionTypeIsolation::Unknown;
6800+
auto PartialApplyIsolation = SILFunctionTypeIsolation::forUnknown();
68016801
ApplyOptions ApplyOpts;
68026802
bool IsNoEscape = false;
68036803

@@ -6828,7 +6828,7 @@ bool SILParser::parseCallInstruction(SILLocation InstLoc,
68286828

68296829
if (AttrName == "isolated_any") {
68306830
assert(!bool(AttrValue));
6831-
PartialApplyIsolation = SILFunctionTypeIsolation::Erased;
6831+
PartialApplyIsolation = SILFunctionTypeIsolation::forErased();
68326832
continue;
68336833
}
68346834

lib/SILGen/SILGenBuilder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,13 +93,13 @@ class SILGenBuilder : public SILBuilder {
9393
ArrayRef<ManagedValue> args,
9494
ParameterConvention calleeConvention,
9595
SILFunctionTypeIsolation resultIsolation =
96-
SILFunctionTypeIsolation::Unknown);
96+
SILFunctionTypeIsolation::forUnknown());
9797
ManagedValue createPartialApply(SILLocation loc, ManagedValue fn,
9898
SubstitutionMap subs,
9999
ArrayRef<ManagedValue> args,
100100
ParameterConvention calleeConvention,
101101
SILFunctionTypeIsolation resultIsolation =
102-
SILFunctionTypeIsolation::Unknown) {
102+
SILFunctionTypeIsolation::forUnknown()) {
103103
return createPartialApply(loc, fn.getValue(), subs, args,
104104
calleeConvention, resultIsolation);
105105
}

lib/SILGen/SILGenFunction.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1073,8 +1073,8 @@ SILGenFunction::emitClosureValue(SILLocation loc, SILDeclRef constant,
10731073
auto calleeConvention = ParameterConvention::Direct_Guaranteed;
10741074

10751075
auto resultIsolation =
1076-
(hasErasedIsolation ? SILFunctionTypeIsolation::Erased
1077-
: SILFunctionTypeIsolation::Unknown);
1076+
(hasErasedIsolation ? SILFunctionTypeIsolation::forErased()
1077+
: SILFunctionTypeIsolation::forUnknown());
10781078
auto toClosure =
10791079
B.createPartialApply(loc, functionRef, subs, forwardedArgs,
10801080
calleeConvention, resultIsolation);
@@ -1847,7 +1847,7 @@ SILGenFunction::emitApplyOfSetterToBase(SILLocation loc, SILDeclRef setter,
18471847
PartialApplyInst *setterPAI =
18481848
B.createPartialApply(loc, setterFRef, substitutions, capturedArgs,
18491849
ParameterConvention::Direct_Guaranteed,
1850-
SILFunctionTypeIsolation::Unknown,
1850+
SILFunctionTypeIsolation::forUnknown(),
18511851
PartialApplyInst::OnStackKind::OnStack);
18521852
return emitManagedRValueWithCleanup(setterPAI).getValue();
18531853
}
@@ -1899,7 +1899,7 @@ void SILGenFunction::emitAssignOrInit(SILLocation loc, ManagedValue selfValue,
18991899
PartialApplyInst *initPAI =
19001900
B.createPartialApply(loc, initFRef, substitutions, selfMetatype,
19011901
ParameterConvention::Direct_Guaranteed,
1902-
SILFunctionTypeIsolation::Unknown,
1902+
SILFunctionTypeIsolation::forUnknown(),
19031903
PartialApplyInst::OnStackKind::OnStack);
19041904
initFRef = emitManagedRValueWithCleanup(initPAI).getValue();
19051905

lib/Sema/TypeCheckType.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4501,7 +4501,7 @@ NeverNullType TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
45014501
bool sendable = claim<SendableTypeAttr>(attrs);
45024502
bool async = claim<AsyncTypeAttr>(attrs);
45034503
bool unimplementable = claim<UnimplementableTypeAttr>(attrs);
4504-
auto isolation = SILFunctionTypeIsolation::Unknown;
4504+
auto isolation = SILFunctionTypeIsolation::forUnknown();
45054505

45064506
if (auto isolatedAttr = claim<IsolatedTypeAttr>(attrs)) {
45074507
switch (isolatedAttr->getIsolationKind()) {
@@ -4513,7 +4513,7 @@ NeverNullType TypeResolver::resolveSILFunctionType(FunctionTypeRepr *repr,
45134513
isolatedAttr->getIsolationKindName(),
45144514
conventionAttr->getConventionName());
45154515
} else {
4516-
isolation = SILFunctionTypeIsolation::Erased;
4516+
isolation = SILFunctionTypeIsolation::forErased();
45174517
}
45184518
break;
45194519
}

lib/Serialization/Deserialization.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7852,9 +7852,9 @@ Expected<Type> DESERIALIZE_TYPE(SIL_FUNCTION_TYPE)(
78527852
clangFunctionType = clangType.get();
78537853
}
78547854

7855-
auto isolation = SILFunctionTypeIsolation::Unknown;
7855+
auto isolation = SILFunctionTypeIsolation::forUnknown();
78567856
if (erasedIsolation)
7857-
isolation = SILFunctionTypeIsolation::Erased;
7857+
isolation = SILFunctionTypeIsolation::forErased();
78587858

78597859
auto extInfo = SILFunctionType::ExtInfoBuilder(
78607860
*representation, pseudogeneric, noescape, sendable, async,

0 commit comments

Comments
 (0)