Skip to content

Commit de7a62e

Browse files
authored
Merge pull request #80312 from gottesmm/pr-126bc735b2d01c0f2f35f27268ff26d404b2fb16
[sil] Make SILFunctionTypeInfo a struct enum.
2 parents 51face1 + 8cfb029 commit de7a62e

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 {
@@ -2276,16 +2277,15 @@ BridgedInstruction BridgedBuilder::createPartialApply(BridgedValue funcRef,
22762277
BridgedSubstitutionMap bridgedSubstitutionMap,
22772278
bool hasUnknownIsolation,
22782279
bool isOnStack) const {
2279-
llvm::SmallVector<swift::SILValue, 8> capturedArgs;
2280+
llvm::SmallVector<swift::SILValue, 8> capturedArgs;
22802281
return {unbridged().createPartialApply(
2281-
regularLoc(),
2282-
funcRef.getSILValue(),
2283-
bridgedSubstitutionMap.unbridged(),
2284-
bridgedCapturedArgs.getValues(capturedArgs),
2285-
getParameterConvention(calleeConvention),
2286-
hasUnknownIsolation ? swift::SILFunctionTypeIsolation::Unknown : swift::SILFunctionTypeIsolation::Erased,
2287-
isOnStack ? swift:: PartialApplyInst::OnStack : swift::PartialApplyInst::NotOnStack
2288-
)};
2282+
regularLoc(), funcRef.getSILValue(), bridgedSubstitutionMap.unbridged(),
2283+
bridgedCapturedArgs.getValues(capturedArgs),
2284+
getParameterConvention(calleeConvention),
2285+
hasUnknownIsolation ? swift::SILFunctionTypeIsolation::forUnknown()
2286+
: swift::SILFunctionTypeIsolation::forErased(),
2287+
isOnStack ? swift::PartialApplyInst::OnStack
2288+
: swift::PartialApplyInst::NotOnStack)};
22892289
}
22902290

22912291
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");
@@ -2287,7 +2287,7 @@ void ASTMangler::appendImplFunctionType(SILFunctionType *fn,
22872287
if (!fn->isNoEscape())
22882288
OpArgs.push_back('e');
22892289

2290-
switch (fn->getIsolation()) {
2290+
switch (fn->getIsolation().getKind()) {
22912291
case SILFunctionTypeIsolation::Unknown:
22922292
break;
22932293
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
@@ -6807,7 +6807,7 @@ bool SILParser::parseCallInstruction(SILLocation InstLoc,
68076807
SmallVector<UnresolvedValueName, 4> ArgNames;
68086808

68096809
auto PartialApplyConvention = ParameterConvention::Direct_Owned;
6810-
auto PartialApplyIsolation = SILFunctionTypeIsolation::Unknown;
6810+
auto PartialApplyIsolation = SILFunctionTypeIsolation::forUnknown();
68116811
ApplyOptions ApplyOpts;
68126812
bool IsNoEscape = false;
68136813

@@ -6838,7 +6838,7 @@ bool SILParser::parseCallInstruction(SILLocation InstLoc,
68386838

68396839
if (AttrName == "isolated_any") {
68406840
assert(!bool(AttrValue));
6841-
PartialApplyIsolation = SILFunctionTypeIsolation::Erased;
6841+
PartialApplyIsolation = SILFunctionTypeIsolation::forErased();
68426842
continue;
68436843
}
68446844

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
@@ -7853,9 +7853,9 @@ Expected<Type> DESERIALIZE_TYPE(SIL_FUNCTION_TYPE)(
78537853
clangFunctionType = clangType.get();
78547854
}
78557855

7856-
auto isolation = SILFunctionTypeIsolation::Unknown;
7856+
auto isolation = SILFunctionTypeIsolation::forUnknown();
78577857
if (erasedIsolation)
7858-
isolation = SILFunctionTypeIsolation::Erased;
7858+
isolation = SILFunctionTypeIsolation::forErased();
78597859

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

0 commit comments

Comments
 (0)